node.pas 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Basic node handling
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit node;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. globtype,globals,
  24. cpubase,
  25. aasmbase,
  26. symtype,symppu;
  27. type
  28. pconstset = ^tconstset;
  29. {$ifdef oldset}
  30. tconstset = array[0..31] of byte;
  31. pconst32bitset = ^tconst32bitset;
  32. tconst32bitset = array[0..7] of longint;
  33. {$else}
  34. tconstset = set of 0..255;
  35. {$endif}
  36. tnodetype = (
  37. emptynode, {No node (returns nil when loading from ppu)}
  38. addn, {Represents the + operator}
  39. muln, {Represents the * operator}
  40. subn, {Represents the - operator}
  41. divn, {Represents the div operator}
  42. symdifn, {Represents the >< operator}
  43. modn, {Represents the mod operator}
  44. assignn, {Represents an assignment}
  45. loadn, {Represents the use of a variabele}
  46. rangen, {Represents a range (i.e. 0..9)}
  47. ltn, {Represents the < operator}
  48. lten, {Represents the <= operator}
  49. gtn, {Represents the > operator}
  50. gten, {Represents the >= operator}
  51. equaln, {Represents the = operator}
  52. unequaln, {Represents the <> operator}
  53. inn, {Represents the in operator}
  54. orn, {Represents the or operator}
  55. xorn, {Represents the xor operator}
  56. shrn, {Represents the shr operator}
  57. shln, {Represents the shl operator}
  58. slashn, {Represents the / operator}
  59. andn, {Represents the and operator}
  60. subscriptn, {Field in a record/object}
  61. derefn, {Dereferences a pointer}
  62. addrn, {Represents the @ operator}
  63. doubleaddrn, {Represents the @@ operator}
  64. ordconstn, {Represents an ordinal value}
  65. typeconvn, {Represents type-conversion/typecast}
  66. calln, {Represents a call node}
  67. callparan, {Represents a parameter}
  68. realconstn, {Represents a real value}
  69. unaryminusn, {Represents a sign change (i.e. -2)}
  70. asmn, {Represents an assembler node }
  71. vecn, {Represents array indexing}
  72. pointerconstn, {Represents a pointer constant}
  73. stringconstn, {Represents a string constant}
  74. funcretn, {Represents the function result var}
  75. selfn, {Represents the self parameter}
  76. notn, {Represents the not operator}
  77. inlinen, {Internal procedures (i.e. writeln)}
  78. niln, {Represents the nil pointer}
  79. errorn, {This part of the tree could not be
  80. parsed because of a compiler error}
  81. typen, {A type name. Used for i.e. typeof(obj)}
  82. hnewn, {The new operation, constructor call}
  83. hdisposen, {The dispose operation with destructor call}
  84. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  85. setconstn, {A set constant (i.e. [1,2])}
  86. blockn, {A block of statements}
  87. statementn, {One statement in a block of nodes}
  88. ifn, {An if statement}
  89. breakn, {A break statement}
  90. continuen, {A continue statement}
  91. whilerepeatn, {A while or repeat statement}
  92. forn, {A for loop}
  93. exitn, {An exit statement}
  94. withn, {A with statement}
  95. casen, {A case statement}
  96. labeln, {A label}
  97. goton, {A goto statement}
  98. tryexceptn, {A try except block}
  99. raisen, {A raise statement}
  100. tryfinallyn, {A try finally statement}
  101. onn, {For an on statement in exception code}
  102. isn, {Represents the is operator}
  103. asn, {Represents the as typecast}
  104. caretn, {Represents the ^ operator}
  105. failn, {Represents the fail statement}
  106. starstarn, {Represents the ** operator exponentiation }
  107. procinlinen, {Procedures that can be inlined }
  108. arrayconstructorn, {Construction node for [...] parsing}
  109. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  110. tempcreaten, { for temps in the result/firstpass }
  111. temprefn, { references to temps }
  112. tempdeleten, { for temps in the result/firstpass }
  113. addoptn, { added for optimizations where we cannot suppress }
  114. nothingn, {NOP, Do nothing}
  115. loadvmtn, {Load the address of the VMT of a class/object}
  116. guidconstn, {A GUID COM Interface constant }
  117. rttin {Rtti information so they can be accessed in result/firstpass}
  118. );
  119. const
  120. nodetype2str : array[tnodetype] of string[20] = (
  121. '<emptynode>',
  122. 'addn',
  123. 'muln',
  124. 'subn',
  125. 'divn',
  126. 'symdifn',
  127. 'modn',
  128. 'assignn',
  129. 'loadn',
  130. 'rangen',
  131. 'ltn',
  132. 'lten',
  133. 'gtn',
  134. 'gten',
  135. 'equaln',
  136. 'unequaln',
  137. 'inn',
  138. 'orn',
  139. 'xorn',
  140. 'shrn',
  141. 'shln',
  142. 'slashn',
  143. 'andn',
  144. 'subscriptn',
  145. 'derefn',
  146. 'addrn',
  147. 'doubleaddrn',
  148. 'ordconstn',
  149. 'typeconvn',
  150. 'calln',
  151. 'callparan',
  152. 'realconstn',
  153. 'unaryminusn',
  154. 'asmn',
  155. 'vecn',
  156. 'pointerconstn',
  157. 'stringconstn',
  158. 'funcretn',
  159. 'selfn',
  160. 'notn',
  161. 'inlinen',
  162. 'niln',
  163. 'errorn',
  164. 'typen',
  165. 'hnewn',
  166. 'hdisposen',
  167. 'setelementn',
  168. 'setconstn',
  169. 'blockn',
  170. 'statementn',
  171. 'ifn',
  172. 'breakn',
  173. 'continuen',
  174. 'whilerepeatn',
  175. 'forn',
  176. 'exitn',
  177. 'withn',
  178. 'casen',
  179. 'labeln',
  180. 'goton',
  181. 'tryexceptn',
  182. 'raisen',
  183. 'tryfinallyn',
  184. 'onn',
  185. 'isn',
  186. 'asn',
  187. 'caretn',
  188. 'failn',
  189. 'starstarn',
  190. 'procinlinen',
  191. 'arrayconstructn',
  192. 'arrayconstructrangen',
  193. 'tempcreaten',
  194. 'temprefn',
  195. 'tempdeleten',
  196. 'addoptn',
  197. 'nothingn',
  198. 'loadvmtn',
  199. 'guidconstn',
  200. 'rttin');
  201. type
  202. { all boolean field of ttree are now collected in flags }
  203. tnodeflags = (
  204. nf_needs_truefalselabel,
  205. nf_swapable, { tbinop operands can be swaped }
  206. nf_swaped, { tbinop operands are swaped }
  207. nf_error,
  208. nf_write, { Node is written to }
  209. { flags used by tcallnode }
  210. nf_return_value_used,
  211. nf_static_call,
  212. { flags used by tcallparanode }
  213. nf_varargs_para, { belongs this para to varargs }
  214. { flags used by loop nodes }
  215. nf_backward, { set if it is a for ... downto ... do loop }
  216. nf_varstate, { do we need to parse childs to set var state }
  217. nf_testatbegin,{ Do a test at the begin of the loop?}
  218. nf_checknegate,{ Negate the loop test?}
  219. { taddrnode }
  220. nf_procvarload,
  221. { tvecnode }
  222. nf_memindex,
  223. nf_memseg,
  224. nf_callunique,
  225. { twithnode }
  226. nf_islocal,
  227. { tloadnode }
  228. nf_absolute,
  229. nf_first,
  230. { tassignmentnode }
  231. nf_concat_string,
  232. { tfuncretnode }
  233. nf_is_first_funcret,
  234. { tarrayconstructnode }
  235. nf_cargs, { 20th }
  236. nf_cargswap,
  237. nf_forcevaria,
  238. nf_novariaallowed,
  239. { ttypeconvnode }
  240. nf_explizit,
  241. { tinlinenode }
  242. nf_inlineconst,
  243. { general }
  244. nf_isproperty,
  245. nf_varstateset,
  246. { tasmnode }
  247. nf_object_preserved, { 30th }
  248. { taddnode }
  249. nf_use_strconcat
  250. );
  251. tnodeflagset = set of tnodeflags;
  252. const
  253. { contains the flags which must be equal for the equality }
  254. { of nodes }
  255. flagsequal : tnodeflagset = [nf_error,nf_static_call,nf_backward];
  256. type
  257. tnodelist = class
  258. end;
  259. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  260. tnode = class
  261. public
  262. { type of this node }
  263. nodetype : tnodetype;
  264. { type of the current code block, general/const/type }
  265. blocktype : tblock_type;
  266. { the location of the result of this node }
  267. location : tlocation;
  268. { the parent node of this is node }
  269. { this field is set by concattolist }
  270. parent : tnode;
  271. { there are some properties about the node stored }
  272. flags : tnodeflagset;
  273. { the number of registers needed to evalute the node }
  274. registers32,registersfpu : longint; { must be longint !!!! }
  275. {$ifdef SUPPORT_MMX}
  276. registersmmx,registerskni : longint;
  277. {$endif SUPPORT_MMX}
  278. resulttype : ttype;
  279. fileinfo : tfileposinfo;
  280. localswitches : tlocalswitches;
  281. {$ifdef extdebug}
  282. maxfirstpasscount,
  283. firstpasscount : longint;
  284. {$endif extdebug}
  285. constructor create(t:tnodetype);
  286. { this constructor is only for creating copies of class }
  287. { the fields are copied by getcopy }
  288. constructor createforcopy;
  289. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  290. destructor destroy;override;
  291. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  292. procedure derefimpl;virtual;
  293. { toggles the flag }
  294. procedure toggleflag(f : tnodeflags);
  295. { the 1.1 code generator may override pass_1 }
  296. { and it need not to implement det_* then }
  297. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  298. { 2.0: runs det_resulttype and det_temp }
  299. function pass_1 : tnode;virtual;abstract;
  300. { dermines the resulttype of the node }
  301. function det_resulttype : tnode;virtual;abstract;
  302. { dermines the number of necessary temp. locations to evaluate
  303. the node }
  304. {$ifdef state_tracking}
  305. { Does optimizations by keeping track of the variable states
  306. in a procedure }
  307. function track_state_pass(exec_known:boolean):boolean;virtual;
  308. {$endif}
  309. {$ifdef var_notification}
  310. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  311. written to (normally the loadnode) as write access. }
  312. procedure mark_write;virtual;
  313. {$endif}
  314. procedure det_temp;virtual;abstract;
  315. procedure pass_2;virtual;abstract;
  316. { comparing of nodes }
  317. function isequal(p : tnode) : boolean;
  318. { to implement comparisation, override this method }
  319. function docompare(p : tnode) : boolean;virtual;
  320. { gets a copy of the node }
  321. function getcopy : tnode;virtual;
  322. procedure insertintolist(l : tnodelist);virtual;
  323. {$ifdef EXTDEBUG}
  324. { writes a node for debugging purpose, shouldn't be called }
  325. { direct, because there is no test for nil, use writenode }
  326. { to write a complete tree }
  327. procedure dowrite;
  328. procedure dowritenodetype;virtual;
  329. procedure _dowrite;virtual;
  330. {$endif EXTDEBUG}
  331. procedure concattolist(l : tlinkedlist);virtual;
  332. function ischild(p : tnode) : boolean;virtual;
  333. procedure set_file_line(from : tnode);
  334. procedure set_tree_filepos(const filepos : tfileposinfo);
  335. end;
  336. tnodeclass = class of tnode;
  337. tnodeclassarray = array[tnodetype] of tnodeclass;
  338. { this node is the anchestor for all nodes with at least }
  339. { one child, you have to use it if you want to use }
  340. { true- and falselabel }
  341. punarynode = ^tunarynode;
  342. tunarynode = class(tnode)
  343. left : tnode;
  344. constructor create(t:tnodetype;l : tnode);
  345. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  346. destructor destroy;override;
  347. procedure ppuwrite(ppufile:tcompilerppufile);override;
  348. procedure derefimpl;override;
  349. procedure concattolist(l : tlinkedlist);override;
  350. function ischild(p : tnode) : boolean;override;
  351. function docompare(p : tnode) : boolean;override;
  352. function getcopy : tnode;override;
  353. procedure insertintolist(l : tnodelist);override;
  354. procedure left_max;
  355. {$ifdef extdebug}
  356. procedure _dowrite;override;
  357. {$endif extdebug}
  358. end;
  359. pbinarynode = ^tbinarynode;
  360. tbinarynode = class(tunarynode)
  361. right : tnode;
  362. constructor create(t:tnodetype;l,r : tnode);
  363. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  364. destructor destroy;override;
  365. procedure ppuwrite(ppufile:tcompilerppufile);override;
  366. procedure derefimpl;override;
  367. procedure concattolist(l : tlinkedlist);override;
  368. function ischild(p : tnode) : boolean;override;
  369. function docompare(p : tnode) : boolean;override;
  370. procedure swapleftright;
  371. function getcopy : tnode;override;
  372. procedure insertintolist(l : tnodelist);override;
  373. procedure left_right_max;
  374. {$ifdef extdebug}
  375. procedure _dowrite;override;
  376. {$endif extdebug}
  377. end;
  378. tbinopnode = class(tbinarynode)
  379. constructor create(t:tnodetype;l,r : tnode);virtual;
  380. function docompare(p : tnode) : boolean;override;
  381. end;
  382. {$ifdef tempregdebug}
  383. type
  384. pptree = ^tnode;
  385. var
  386. curptree: pptree;
  387. {$endif tempregdebug}
  388. var
  389. { array with all class types for tnodes }
  390. nodeclass : tnodeclassarray;
  391. {$ifdef EXTDEBUG}
  392. { indention used when writing the tree to the screen }
  393. writenodeindention : string;
  394. {$endif EXTDEBUG}
  395. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  396. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  397. {$ifdef EXTDEBUG}
  398. procedure writenode(t:tnode);
  399. {$endif EXTDEBUG}
  400. implementation
  401. uses
  402. cutils,verbose;
  403. const
  404. ppunodemarker = 255;
  405. {****************************************************************************
  406. Helpers
  407. ****************************************************************************}
  408. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  409. var
  410. b : byte;
  411. t : tnodetype;
  412. begin
  413. { marker }
  414. b:=ppufile.getbyte;
  415. if b<>ppunodemarker then
  416. internalerror(200208151);
  417. { load nodetype }
  418. t:=tnodetype(ppufile.getbyte);
  419. if t>high(tnodetype) then
  420. internalerror(200208152);
  421. if t<>emptynode then
  422. begin
  423. if not assigned(nodeclass[t]) then
  424. internalerror(200208153);
  425. //writeln('load: ',nodetype2str[t]);
  426. { generate node of the correct class }
  427. ppuloadnode:=nodeclass[t].ppuload(t,ppufile);
  428. end
  429. else
  430. ppuloadnode:=nil;
  431. end;
  432. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  433. begin
  434. { marker, read by ppuloadnode }
  435. ppufile.putbyte(ppunodemarker);
  436. { type, read by ppuloadnode }
  437. if assigned(n) then
  438. begin
  439. ppufile.putbyte(byte(n.nodetype));
  440. //writeln('write: ',nodetype2str[n.nodetype]);
  441. n.ppuwrite(ppufile);
  442. end
  443. else
  444. ppufile.putbyte(byte(emptynode));
  445. end;
  446. {$ifdef EXTDEBUG}
  447. procedure writenode(t:tnode);
  448. begin
  449. if assigned(t) then
  450. t.dowrite
  451. else
  452. write(writenodeindention,'nil');
  453. if writenodeindention='' then
  454. writeln;
  455. end;
  456. {$endif EXTDEBUG}
  457. {****************************************************************************
  458. TNODE
  459. ****************************************************************************}
  460. constructor tnode.create(t:tnodetype);
  461. begin
  462. inherited create;
  463. nodetype:=t;
  464. blocktype:=block_type;
  465. { this allows easier error tracing }
  466. location.loc:=LOC_INVALID;
  467. { save local info }
  468. fileinfo:=aktfilepos;
  469. localswitches:=aktlocalswitches;
  470. resulttype.reset;
  471. registers32:=0;
  472. registersfpu:=0;
  473. {$ifdef SUPPORT_MMX}
  474. registersmmx:=0;
  475. {$endif SUPPORT_MMX}
  476. {$ifdef EXTDEBUG}
  477. maxfirstpasscount:=0;
  478. firstpasscount:=0;
  479. {$endif EXTDEBUG}
  480. flags:=[];
  481. end;
  482. constructor tnode.createforcopy;
  483. begin
  484. end;
  485. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  486. begin
  487. nodetype:=t;
  488. { tnode fields }
  489. blocktype:=tblock_type(ppufile.getbyte);
  490. ppufile.getposinfo(fileinfo);
  491. ppufile.getsmallset(localswitches);
  492. ppufile.gettype(resulttype);
  493. ppufile.getsmallset(flags);
  494. { updated by firstpass }
  495. location.loc:=LOC_INVALID;
  496. registers32:=0;
  497. registersfpu:=0;
  498. {$ifdef SUPPORT_MMX}
  499. registersmmx:=0;
  500. {$endif SUPPORT_MMX}
  501. {$ifdef EXTDEBUG}
  502. maxfirstpasscount:=0;
  503. firstpasscount:=0;
  504. {$endif EXTDEBUG}
  505. end;
  506. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  507. begin
  508. ppufile.putbyte(byte(block_type));
  509. ppufile.putposinfo(fileinfo);
  510. ppufile.putsmallset(localswitches);
  511. ppufile.puttype(resulttype);
  512. ppufile.putsmallset(flags);
  513. end;
  514. procedure tnode.derefimpl;
  515. begin
  516. resulttype.resolve;
  517. end;
  518. procedure tnode.toggleflag(f : tnodeflags);
  519. begin
  520. if f in flags then
  521. exclude(flags,f)
  522. else
  523. include(flags,f);
  524. end;
  525. destructor tnode.destroy;
  526. begin
  527. {$ifdef EXTDEBUG}
  528. if firstpasscount>maxfirstpasscount then
  529. maxfirstpasscount:=firstpasscount;
  530. {$endif EXTDEBUG}
  531. end;
  532. procedure tnode.concattolist(l : tlinkedlist);
  533. begin
  534. end;
  535. function tnode.ischild(p : tnode) : boolean;
  536. begin
  537. ischild:=false;
  538. end;
  539. {$ifdef EXTDEBUG}
  540. procedure tnode._dowrite;
  541. begin
  542. dowritenodetype;
  543. if assigned(resulttype.def) then
  544. write(',resulttype = "',resulttype.def.gettypename,'"')
  545. else
  546. write(',resulttype = <nil>');
  547. write(',location.loc = ',ord(location.loc));
  548. write(',registersint = ',registers32);
  549. write(',registersfpu = ',registersfpu);
  550. end;
  551. procedure tnode.dowritenodetype;
  552. begin
  553. write(nodetype2str[nodetype]);
  554. end;
  555. procedure tnode.dowrite;
  556. begin
  557. write(writenodeindention,'(');
  558. writenodeindention:=writenodeindention+' ';
  559. _dowrite;
  560. writeln;
  561. delete(writenodeindention,1,4);
  562. write(writenodeindention,')');
  563. end;
  564. {$endif EXTDEBUG}
  565. function tnode.isequal(p : tnode) : boolean;
  566. begin
  567. isequal:=
  568. (not assigned(self) and not assigned(p)) or
  569. (assigned(self) and assigned(p) and
  570. { optimized subclasses have the same nodetype as their }
  571. { superclass (for compatibility), so also check the classtype (JM) }
  572. (p.classtype=classtype) and
  573. (p.nodetype=nodetype) and
  574. (flags*flagsequal=p.flags*flagsequal) and
  575. docompare(p));
  576. end;
  577. {$ifdef state_tracking}
  578. function Tnode.track_state_pass(exec_known:boolean):boolean;
  579. begin
  580. track_state_pass:=false;
  581. end;
  582. {$endif state_tracking}
  583. function tnode.docompare(p : tnode) : boolean;
  584. begin
  585. docompare:=true;
  586. end;
  587. function tnode.getcopy : tnode;
  588. var
  589. p : tnode;
  590. begin
  591. { this is quite tricky because we need a node of the current }
  592. { node type and not one of tnode! }
  593. p:=tnodeclass(classtype).createforcopy;
  594. p.nodetype:=nodetype;
  595. p.location:=location;
  596. p.parent:=parent;
  597. p.flags:=flags;
  598. p.registers32:=registers32;
  599. p.registersfpu:=registersfpu;
  600. {$ifdef SUPPORT_MMX}
  601. p.registersmmx:=registersmmx;
  602. p.registerskni:=registerskni;
  603. {$endif SUPPORT_MMX}
  604. p.resulttype:=resulttype;
  605. p.fileinfo:=fileinfo;
  606. p.localswitches:=localswitches;
  607. {$ifdef extdebug}
  608. p.firstpasscount:=firstpasscount;
  609. {$endif extdebug}
  610. { p.list:=list; }
  611. getcopy:=p;
  612. end;
  613. procedure tnode.insertintolist(l : tnodelist);
  614. begin
  615. end;
  616. procedure tnode.set_file_line(from : tnode);
  617. begin
  618. if assigned(from) then
  619. fileinfo:=from.fileinfo;
  620. end;
  621. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  622. begin
  623. fileinfo:=filepos;
  624. end;
  625. {$ifdef var_notification}
  626. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  627. written to (normally the loadnode) as write access. }
  628. procedure Tnode.mark_write;
  629. begin
  630. writenode(self);
  631. runerror(211);
  632. end;
  633. {$endif}
  634. {****************************************************************************
  635. TUNARYNODE
  636. ****************************************************************************}
  637. constructor tunarynode.create(t:tnodetype;l : tnode);
  638. begin
  639. inherited create(t);
  640. left:=l;
  641. end;
  642. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  643. begin
  644. inherited ppuload(t,ppufile);
  645. left:=ppuloadnode(ppufile);
  646. end;
  647. destructor tunarynode.destroy;
  648. begin
  649. left.free;
  650. inherited destroy;
  651. end;
  652. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  653. begin
  654. inherited ppuwrite(ppufile);
  655. ppuwritenode(ppufile,left);
  656. end;
  657. procedure tunarynode.derefimpl;
  658. begin
  659. inherited derefimpl;
  660. if assigned(left) then
  661. left.derefimpl;
  662. end;
  663. function tunarynode.docompare(p : tnode) : boolean;
  664. begin
  665. docompare:=(inherited docompare(p) and
  666. ((left=nil) or left.isequal(tunarynode(p).left))
  667. );
  668. end;
  669. function tunarynode.getcopy : tnode;
  670. var
  671. p : tunarynode;
  672. begin
  673. p:=tunarynode(inherited getcopy);
  674. if assigned(left) then
  675. p.left:=left.getcopy
  676. else
  677. p.left:=nil;
  678. getcopy:=p;
  679. end;
  680. procedure tunarynode.insertintolist(l : tnodelist);
  681. begin
  682. end;
  683. {$ifdef extdebug}
  684. procedure tunarynode._dowrite;
  685. begin
  686. inherited _dowrite;
  687. writeln(',');
  688. writenode(left);
  689. end;
  690. {$endif}
  691. procedure tunarynode.left_max;
  692. begin
  693. registers32:=left.registers32;
  694. registersfpu:=left.registersfpu;
  695. {$ifdef SUPPORT_MMX}
  696. registersmmx:=left.registersmmx;
  697. {$endif SUPPORT_MMX}
  698. end;
  699. procedure tunarynode.concattolist(l : tlinkedlist);
  700. begin
  701. left.parent:=self;
  702. left.concattolist(l);
  703. inherited concattolist(l);
  704. end;
  705. function tunarynode.ischild(p : tnode) : boolean;
  706. begin
  707. ischild:=p=left;
  708. end;
  709. {****************************************************************************
  710. TBINARYNODE
  711. ****************************************************************************}
  712. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  713. begin
  714. inherited create(t,l);
  715. right:=r
  716. end;
  717. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  718. begin
  719. inherited ppuload(t,ppufile);
  720. right:=ppuloadnode(ppufile);
  721. end;
  722. destructor tbinarynode.destroy;
  723. begin
  724. right.free;
  725. inherited destroy;
  726. end;
  727. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  728. begin
  729. inherited ppuwrite(ppufile);
  730. ppuwritenode(ppufile,right);
  731. end;
  732. procedure tbinarynode.derefimpl;
  733. begin
  734. inherited derefimpl;
  735. if assigned(right) then
  736. right.derefimpl;
  737. end;
  738. procedure tbinarynode.concattolist(l : tlinkedlist);
  739. begin
  740. { we could change that depending on the number of }
  741. { required registers }
  742. left.parent:=self;
  743. left.concattolist(l);
  744. left.parent:=self;
  745. left.concattolist(l);
  746. inherited concattolist(l);
  747. end;
  748. function tbinarynode.ischild(p : tnode) : boolean;
  749. begin
  750. ischild:=(p=right);
  751. end;
  752. function tbinarynode.docompare(p : tnode) : boolean;
  753. begin
  754. docompare:=(inherited docompare(p) and
  755. ((right=nil) or right.isequal(tbinarynode(p).right))
  756. );
  757. end;
  758. function tbinarynode.getcopy : tnode;
  759. var
  760. p : tbinarynode;
  761. begin
  762. p:=tbinarynode(inherited getcopy);
  763. if assigned(right) then
  764. p.right:=right.getcopy
  765. else
  766. p.right:=nil;
  767. getcopy:=p;
  768. end;
  769. procedure tbinarynode.insertintolist(l : tnodelist);
  770. begin
  771. end;
  772. procedure tbinarynode.swapleftright;
  773. var
  774. swapp : tnode;
  775. begin
  776. swapp:=right;
  777. right:=left;
  778. left:=swapp;
  779. if nf_swaped in flags then
  780. exclude(flags,nf_swaped)
  781. else
  782. include(flags,nf_swaped);
  783. end;
  784. procedure tbinarynode.left_right_max;
  785. begin
  786. if assigned(left) then
  787. begin
  788. if assigned(right) then
  789. begin
  790. registers32:=max(left.registers32,right.registers32);
  791. registersfpu:=max(left.registersfpu,right.registersfpu);
  792. {$ifdef SUPPORT_MMX}
  793. registersmmx:=max(left.registersmmx,right.registersmmx);
  794. {$endif SUPPORT_MMX}
  795. end
  796. else
  797. begin
  798. registers32:=left.registers32;
  799. registersfpu:=left.registersfpu;
  800. {$ifdef SUPPORT_MMX}
  801. registersmmx:=left.registersmmx;
  802. {$endif SUPPORT_MMX}
  803. end;
  804. end;
  805. end;
  806. {$ifdef extdebug}
  807. procedure tbinarynode._dowrite;
  808. begin
  809. inherited _dowrite;
  810. writeln(',');
  811. writenode(right);
  812. end;
  813. {$endif}
  814. {****************************************************************************
  815. TBINOPYNODE
  816. ****************************************************************************}
  817. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  818. begin
  819. inherited create(t,l,r);
  820. end;
  821. function tbinopnode.docompare(p : tnode) : boolean;
  822. begin
  823. docompare:=(inherited docompare(p)) or
  824. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  825. ((nf_swapable in flags) and
  826. left.isequal(tbinopnode(p).right) and
  827. right.isequal(tbinopnode(p).left));
  828. end;
  829. end.
  830. {
  831. $Log$
  832. Revision 1.41 2002-09-01 13:28:38 daniel
  833. - write_access fields removed in favor of a flag
  834. Revision 1.40 2002/09/01 08:01:16 daniel
  835. * Removed sets from Tcallnode.det_resulttype
  836. + Added read/write notifications of variables. These will be usefull
  837. for providing information for several optimizations. For example
  838. the value of the loop variable of a for loop does matter is the
  839. variable is read after the for loop, but if it's no longer used
  840. or written, it doesn't matter and this can be used to optimize
  841. the loop code generation.
  842. Revision 1.39 2002/08/22 11:21:45 florian
  843. + register32 is now written by tnode.dowrite
  844. * fixed write of value of tconstnode
  845. Revision 1.38 2002/08/19 19:36:44 peter
  846. * More fixes for cross unit inlining, all tnodes are now implemented
  847. * Moved pocall_internconst to po_internconst because it is not a
  848. calling type at all and it conflicted when inlining of these small
  849. functions was requested
  850. Revision 1.37 2002/08/18 20:06:24 peter
  851. * inlining is now also allowed in interface
  852. * renamed write/load to ppuwrite/ppuload
  853. * tnode storing in ppu
  854. * nld,ncon,nbas are already updated for storing in ppu
  855. Revision 1.36 2002/08/17 22:09:46 florian
  856. * result type handling in tcgcal.pass_2 overhauled
  857. * better tnode.dowrite
  858. * some ppc stuff fixed
  859. Revision 1.35 2002/08/15 19:10:35 peter
  860. * first things tai,tnode storing in ppu
  861. Revision 1.34 2002/08/09 19:15:41 carl
  862. - removed newcg define
  863. Revision 1.33 2002/07/23 12:34:30 daniel
  864. * Readded old set code. To use it define 'oldset'. Activated by default
  865. for ppc.
  866. Revision 1.32 2002/07/22 11:48:04 daniel
  867. * Sets are now internally sets.
  868. Revision 1.31 2002/07/21 06:58:49 daniel
  869. * Changed booleans into flags
  870. Revision 1.30 2002/07/19 11:41:36 daniel
  871. * State tracker work
  872. * The whilen and repeatn are now completely unified into whilerepeatn. This
  873. allows the state tracker to change while nodes automatically into
  874. repeat nodes.
  875. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  876. 'not(a>b)' is optimized into 'a<=b'.
  877. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  878. by removing the notn and later switchting the true and falselabels. The
  879. same is done with 'repeat until not a'.
  880. Revision 1.29 2002/07/14 18:00:44 daniel
  881. + Added the beginning of a state tracker. This will track the values of
  882. variables through procedures and optimize things away.
  883. Revision 1.28 2002/07/01 18:46:24 peter
  884. * internal linker
  885. * reorganized aasm layer
  886. Revision 1.27 2002/05/18 13:34:10 peter
  887. * readded missing revisions
  888. Revision 1.26 2002/05/16 19:46:39 carl
  889. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  890. + try to fix temp allocation (still in ifdef)
  891. + generic constructor calls
  892. + start of tassembler / tmodulebase class cleanup
  893. Revision 1.24 2002/04/21 19:02:04 peter
  894. * removed newn and disposen nodes, the code is now directly
  895. inlined from pexpr
  896. * -an option that will write the secondpass nodes to the .s file, this
  897. requires EXTDEBUG define to actually write the info
  898. * fixed various internal errors and crashes due recent code changes
  899. Revision 1.23 2002/04/06 18:13:01 jonas
  900. * several powerpc-related additions and fixes
  901. Revision 1.22 2002/03/31 20:26:35 jonas
  902. + a_loadfpu_* and a_loadmm_* methods in tcg
  903. * register allocation is now handled by a class and is mostly processor
  904. independent (+rgobj.pas and i386/rgcpu.pas)
  905. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  906. * some small improvements and fixes to the optimizer
  907. * some register allocation fixes
  908. * some fpuvaroffset fixes in the unary minus node
  909. * push/popusedregisters is now called rg.save/restoreusedregisters and
  910. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  911. also better optimizable)
  912. * fixed and optimized register saving/restoring for new/dispose nodes
  913. * LOC_FPU locations now also require their "register" field to be set to
  914. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  915. - list field removed of the tnode class because it's not used currently
  916. and can cause hard-to-find bugs
  917. Revision 1.21 2002/01/19 11:52:32 peter
  918. * dynarr:=nil support added
  919. }