node.pas 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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,cginfo,
  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_swapable, { tbinop operands can be swaped }
  205. nf_swaped, { tbinop operands are swaped }
  206. nf_error,
  207. { general }
  208. nf_write, { Node is written to }
  209. nf_first_use, { First node that uses a variable after declared }
  210. nf_varstateset,
  211. nf_isproperty,
  212. { flags used by tcallnode }
  213. nf_return_value_used,
  214. nf_anon_inherited,
  215. { flags used by tcallparanode }
  216. nf_varargs_para, { belongs this para to varargs }
  217. { taddrnode }
  218. nf_procvarload,
  219. { tvecnode }
  220. nf_memindex,
  221. nf_memseg,
  222. nf_callunique,
  223. { twithnode }
  224. nf_islocal,
  225. { tloadnode }
  226. nf_absolute,
  227. { taddnode }
  228. nf_is_currency,
  229. { tassignmentnode }
  230. nf_concat_string,
  231. nf_use_strconcat,
  232. { tarrayconstructnode }
  233. nf_cargs,
  234. nf_cargswap,
  235. nf_forcevaria,
  236. nf_novariaallowed,
  237. { ttypeconvnode }
  238. nf_explicit,
  239. { tinlinenode }
  240. nf_inlineconst,
  241. { tblocknode }
  242. nf_releasetemps
  243. );
  244. tnodeflagset = set of tnodeflags;
  245. const
  246. { contains the flags which must be equal for the equality }
  247. { of nodes }
  248. flagsequal : tnodeflagset = [nf_error];
  249. type
  250. tnodelist = class
  251. end;
  252. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  253. tnode = class
  254. public
  255. { type of this node }
  256. nodetype : tnodetype;
  257. { type of the current code block, general/const/type }
  258. blocktype : tblock_type;
  259. { expected location of the result of this node (pass1) }
  260. expectloc : tcgloc;
  261. { the location of the result of this node (pass2) }
  262. location : tlocation;
  263. { the parent node of this is node }
  264. { this field is set by concattolist }
  265. parent : tnode;
  266. { there are some properties about the node stored }
  267. flags : tnodeflagset;
  268. { the number of registers needed to evalute the node }
  269. registers32,registersfpu : longint; { must be longint !!!! }
  270. {$ifdef SUPPORT_MMX}
  271. registersmmx,registerskni : longint;
  272. {$endif SUPPORT_MMX}
  273. resulttype : ttype;
  274. fileinfo : tfileposinfo;
  275. localswitches : tlocalswitches;
  276. {$ifdef extdebug}
  277. maxfirstpasscount,
  278. firstpasscount : longint;
  279. {$endif extdebug}
  280. constructor create(t:tnodetype);
  281. { this constructor is only for creating copies of class }
  282. { the fields are copied by getcopy }
  283. constructor createforcopy;
  284. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  285. destructor destroy;override;
  286. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  287. procedure derefimpl;virtual;
  288. { toggles the flag }
  289. procedure toggleflag(f : tnodeflags);
  290. { the 1.1 code generator may override pass_1 }
  291. { and it need not to implement det_* then }
  292. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  293. { 2.0: runs det_resulttype and det_temp }
  294. function pass_1 : tnode;virtual;abstract;
  295. { dermines the resulttype of the node }
  296. function det_resulttype : tnode;virtual;abstract;
  297. { dermines the number of necessary temp. locations to evaluate
  298. the node }
  299. {$ifdef state_tracking}
  300. { Does optimizations by keeping track of the variable states
  301. in a procedure }
  302. function track_state_pass(exec_known:boolean):boolean;virtual;
  303. {$endif}
  304. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  305. written to (normally the loadnode) as write access. }
  306. procedure mark_write;virtual;
  307. procedure det_temp;virtual;abstract;
  308. procedure pass_2;virtual;abstract;
  309. { comparing of nodes }
  310. function isequal(p : tnode) : boolean;
  311. { to implement comparisation, override this method }
  312. function docompare(p : tnode) : boolean;virtual;
  313. { gets a copy of the node }
  314. function getcopy : tnode;virtual;
  315. procedure insertintolist(l : tnodelist);virtual;
  316. {$ifdef EXTDEBUG}
  317. { writes a node for debugging purpose, shouldn't be called }
  318. { direct, because there is no test for nil, use writenode }
  319. { to write a complete tree }
  320. procedure dowrite;
  321. procedure dowritenodetype;virtual;
  322. procedure _dowrite;virtual;
  323. {$endif EXTDEBUG}
  324. procedure concattolist(l : tlinkedlist);virtual;
  325. function ischild(p : tnode) : boolean;virtual;
  326. procedure set_file_line(from : tnode);
  327. procedure set_tree_filepos(const filepos : tfileposinfo);
  328. end;
  329. tnodeclass = class of tnode;
  330. tnodeclassarray = array[tnodetype] of tnodeclass;
  331. { this node is the anchestor for all nodes with at least }
  332. { one child, you have to use it if you want to use }
  333. { true- and falselabel }
  334. punarynode = ^tunarynode;
  335. tunarynode = class(tnode)
  336. left : tnode;
  337. constructor create(t:tnodetype;l : tnode);
  338. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  339. destructor destroy;override;
  340. procedure ppuwrite(ppufile:tcompilerppufile);override;
  341. procedure derefimpl;override;
  342. procedure concattolist(l : tlinkedlist);override;
  343. function ischild(p : tnode) : boolean;override;
  344. function docompare(p : tnode) : boolean;override;
  345. function getcopy : tnode;override;
  346. procedure insertintolist(l : tnodelist);override;
  347. procedure left_max;
  348. {$ifdef extdebug}
  349. procedure _dowrite;override;
  350. {$endif extdebug}
  351. end;
  352. pbinarynode = ^tbinarynode;
  353. tbinarynode = class(tunarynode)
  354. right : tnode;
  355. constructor create(t:tnodetype;l,r : tnode);
  356. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  357. destructor destroy;override;
  358. procedure ppuwrite(ppufile:tcompilerppufile);override;
  359. procedure derefimpl;override;
  360. procedure concattolist(l : tlinkedlist);override;
  361. function ischild(p : tnode) : boolean;override;
  362. function docompare(p : tnode) : boolean;override;
  363. procedure swapleftright;
  364. function getcopy : tnode;override;
  365. procedure insertintolist(l : tnodelist);override;
  366. procedure left_right_max;
  367. {$ifdef extdebug}
  368. procedure _dowrite;override;
  369. {$endif extdebug}
  370. end;
  371. tbinopnode = class(tbinarynode)
  372. constructor create(t:tnodetype;l,r : tnode);virtual;
  373. function docompare(p : tnode) : boolean;override;
  374. end;
  375. {$ifdef tempregdebug}
  376. type
  377. pptree = ^tnode;
  378. var
  379. curptree: pptree;
  380. {$endif tempregdebug}
  381. var
  382. { array with all class types for tnodes }
  383. nodeclass : tnodeclassarray;
  384. {$ifdef EXTDEBUG}
  385. { indention used when writing the tree to the screen }
  386. writenodeindention : string;
  387. {$endif EXTDEBUG}
  388. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  389. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  390. {$ifdef EXTDEBUG}
  391. procedure writenode(t:tnode);
  392. {$endif EXTDEBUG}
  393. implementation
  394. uses
  395. cutils,verbose;
  396. const
  397. ppunodemarker = 255;
  398. {****************************************************************************
  399. Helpers
  400. ****************************************************************************}
  401. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  402. var
  403. b : byte;
  404. t : tnodetype;
  405. begin
  406. { marker }
  407. b:=ppufile.getbyte;
  408. if b<>ppunodemarker then
  409. internalerror(200208151);
  410. { load nodetype }
  411. t:=tnodetype(ppufile.getbyte);
  412. if t>high(tnodetype) then
  413. internalerror(200208152);
  414. if t<>emptynode then
  415. begin
  416. if not assigned(nodeclass[t]) then
  417. internalerror(200208153);
  418. //writeln('load: ',nodetype2str[t]);
  419. { generate node of the correct class }
  420. ppuloadnode:=nodeclass[t].ppuload(t,ppufile);
  421. end
  422. else
  423. ppuloadnode:=nil;
  424. end;
  425. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  426. begin
  427. { marker, read by ppuloadnode }
  428. ppufile.putbyte(ppunodemarker);
  429. { type, read by ppuloadnode }
  430. if assigned(n) then
  431. begin
  432. ppufile.putbyte(byte(n.nodetype));
  433. //writeln('write: ',nodetype2str[n.nodetype]);
  434. n.ppuwrite(ppufile);
  435. end
  436. else
  437. ppufile.putbyte(byte(emptynode));
  438. end;
  439. {$ifdef EXTDEBUG}
  440. procedure writenode(t:tnode);
  441. begin
  442. if assigned(t) then
  443. t.dowrite
  444. else
  445. write(writenodeindention,'nil');
  446. if writenodeindention='' then
  447. writeln;
  448. end;
  449. {$endif EXTDEBUG}
  450. {****************************************************************************
  451. TNODE
  452. ****************************************************************************}
  453. constructor tnode.create(t:tnodetype);
  454. begin
  455. inherited create;
  456. nodetype:=t;
  457. blocktype:=block_type;
  458. { updated by firstpass }
  459. expectloc:=LOC_INVALID;
  460. { updated by secondpass }
  461. location.loc:=LOC_INVALID;
  462. { save local info }
  463. fileinfo:=aktfilepos;
  464. localswitches:=aktlocalswitches;
  465. resulttype.reset;
  466. registers32:=0;
  467. registersfpu:=0;
  468. {$ifdef SUPPORT_MMX}
  469. registersmmx:=0;
  470. {$endif SUPPORT_MMX}
  471. {$ifdef EXTDEBUG}
  472. maxfirstpasscount:=0;
  473. firstpasscount:=0;
  474. {$endif EXTDEBUG}
  475. flags:=[];
  476. end;
  477. constructor tnode.createforcopy;
  478. begin
  479. end;
  480. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  481. begin
  482. nodetype:=t;
  483. { tnode fields }
  484. blocktype:=tblock_type(ppufile.getbyte);
  485. ppufile.getposinfo(fileinfo);
  486. ppufile.getsmallset(localswitches);
  487. ppufile.gettype(resulttype);
  488. ppufile.getsmallset(flags);
  489. { updated by firstpass }
  490. expectloc:=LOC_INVALID;
  491. { updated by secondpass }
  492. location.loc:=LOC_INVALID;
  493. registers32:=0;
  494. registersfpu:=0;
  495. {$ifdef SUPPORT_MMX}
  496. registersmmx:=0;
  497. {$endif SUPPORT_MMX}
  498. {$ifdef EXTDEBUG}
  499. maxfirstpasscount:=0;
  500. firstpasscount:=0;
  501. {$endif EXTDEBUG}
  502. end;
  503. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  504. begin
  505. ppufile.putbyte(byte(block_type));
  506. ppufile.putposinfo(fileinfo);
  507. ppufile.putsmallset(localswitches);
  508. ppufile.puttype(resulttype);
  509. ppufile.putsmallset(flags);
  510. end;
  511. procedure tnode.derefimpl;
  512. begin
  513. resulttype.resolve;
  514. end;
  515. procedure tnode.toggleflag(f : tnodeflags);
  516. begin
  517. if f in flags then
  518. exclude(flags,f)
  519. else
  520. include(flags,f);
  521. end;
  522. destructor tnode.destroy;
  523. begin
  524. {$ifdef EXTDEBUG}
  525. if firstpasscount>maxfirstpasscount then
  526. maxfirstpasscount:=firstpasscount;
  527. {$endif EXTDEBUG}
  528. end;
  529. procedure tnode.concattolist(l : tlinkedlist);
  530. begin
  531. end;
  532. function tnode.ischild(p : tnode) : boolean;
  533. begin
  534. ischild:=false;
  535. end;
  536. procedure tnode.mark_write;
  537. begin
  538. {$ifdef EXTDEBUG}
  539. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  540. {$endif EXTDEBUG}
  541. end;
  542. {$ifdef EXTDEBUG}
  543. procedure tnode._dowrite;
  544. const
  545. loc2str : array[TCGLoc] of string[18] = (
  546. 'LOC_INVALID',
  547. 'LOC_VOID',
  548. 'LOC_CONSTANT',
  549. 'LOC_JUMP',
  550. 'LOC_FLAGS',
  551. 'LOC_CREFERENCE',
  552. 'LOC_REFERENCE',
  553. 'LOC_REGISTER',
  554. 'LOC_CREGISTER',
  555. 'LOC_FPUREGISTER',
  556. 'LOC_CFPUREGISTER',
  557. 'LOC_MMXREGISTER',
  558. 'LOC_CMMXREGISTER',
  559. 'LOC_SSEREGISTER',
  560. 'LOC_CSSEREGISTER',
  561. 'LOC_MMREGISTER',
  562. 'LOC_CMMREGISTER');
  563. begin
  564. dowritenodetype;
  565. if assigned(resulttype.def) then
  566. write(',resulttype = "',resulttype.def.gettypename,'"')
  567. else
  568. write(',resulttype = <nil>');
  569. write(',location.loc = ',loc2str[location.loc]);
  570. write(',registersint = ',registers32);
  571. write(',registersfpu = ',registersfpu);
  572. end;
  573. procedure tnode.dowritenodetype;
  574. begin
  575. write(nodetype2str[nodetype]);
  576. end;
  577. procedure tnode.dowrite;
  578. begin
  579. write(writenodeindention,'(');
  580. writenodeindention:=writenodeindention+' ';
  581. _dowrite;
  582. writeln;
  583. delete(writenodeindention,1,4);
  584. write(writenodeindention,')');
  585. end;
  586. {$endif EXTDEBUG}
  587. function tnode.isequal(p : tnode) : boolean;
  588. begin
  589. isequal:=
  590. (not assigned(self) and not assigned(p)) or
  591. (assigned(self) and assigned(p) and
  592. { optimized subclasses have the same nodetype as their }
  593. { superclass (for compatibility), so also check the classtype (JM) }
  594. (p.classtype=classtype) and
  595. (p.nodetype=nodetype) and
  596. (flags*flagsequal=p.flags*flagsequal) and
  597. docompare(p));
  598. end;
  599. {$ifdef state_tracking}
  600. function Tnode.track_state_pass(exec_known:boolean):boolean;
  601. begin
  602. track_state_pass:=false;
  603. end;
  604. {$endif state_tracking}
  605. function tnode.docompare(p : tnode) : boolean;
  606. begin
  607. docompare:=true;
  608. end;
  609. function tnode.getcopy : tnode;
  610. var
  611. p : tnode;
  612. begin
  613. { this is quite tricky because we need a node of the current }
  614. { node type and not one of tnode! }
  615. p:=tnodeclass(classtype).createforcopy;
  616. p.nodetype:=nodetype;
  617. p.location:=location;
  618. p.parent:=parent;
  619. p.flags:=flags;
  620. p.registers32:=registers32;
  621. p.registersfpu:=registersfpu;
  622. {$ifdef SUPPORT_MMX}
  623. p.registersmmx:=registersmmx;
  624. p.registerskni:=registerskni;
  625. {$endif SUPPORT_MMX}
  626. p.resulttype:=resulttype;
  627. p.fileinfo:=fileinfo;
  628. p.localswitches:=localswitches;
  629. {$ifdef extdebug}
  630. p.firstpasscount:=firstpasscount;
  631. {$endif extdebug}
  632. { p.list:=list; }
  633. getcopy:=p;
  634. end;
  635. { procedure tnode.mark_write;
  636. begin
  637. end;}
  638. procedure tnode.insertintolist(l : tnodelist);
  639. begin
  640. end;
  641. procedure tnode.set_file_line(from : tnode);
  642. begin
  643. if assigned(from) then
  644. fileinfo:=from.fileinfo;
  645. end;
  646. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  647. begin
  648. fileinfo:=filepos;
  649. end;
  650. {****************************************************************************
  651. TUNARYNODE
  652. ****************************************************************************}
  653. constructor tunarynode.create(t:tnodetype;l : tnode);
  654. begin
  655. inherited create(t);
  656. left:=l;
  657. end;
  658. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  659. begin
  660. inherited ppuload(t,ppufile);
  661. left:=ppuloadnode(ppufile);
  662. end;
  663. destructor tunarynode.destroy;
  664. begin
  665. left.free;
  666. inherited destroy;
  667. end;
  668. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  669. begin
  670. inherited ppuwrite(ppufile);
  671. ppuwritenode(ppufile,left);
  672. end;
  673. procedure tunarynode.derefimpl;
  674. begin
  675. inherited derefimpl;
  676. if assigned(left) then
  677. left.derefimpl;
  678. end;
  679. function tunarynode.docompare(p : tnode) : boolean;
  680. begin
  681. docompare:=(inherited docompare(p) and
  682. ((left=nil) or left.isequal(tunarynode(p).left))
  683. );
  684. end;
  685. function tunarynode.getcopy : tnode;
  686. var
  687. p : tunarynode;
  688. begin
  689. p:=tunarynode(inherited getcopy);
  690. if assigned(left) then
  691. p.left:=left.getcopy
  692. else
  693. p.left:=nil;
  694. getcopy:=p;
  695. end;
  696. procedure tunarynode.insertintolist(l : tnodelist);
  697. begin
  698. end;
  699. {$ifdef extdebug}
  700. procedure tunarynode._dowrite;
  701. begin
  702. inherited _dowrite;
  703. writeln(',');
  704. writenode(left);
  705. end;
  706. {$endif}
  707. procedure tunarynode.left_max;
  708. begin
  709. registers32:=left.registers32;
  710. registersfpu:=left.registersfpu;
  711. {$ifdef SUPPORT_MMX}
  712. registersmmx:=left.registersmmx;
  713. {$endif SUPPORT_MMX}
  714. end;
  715. procedure tunarynode.concattolist(l : tlinkedlist);
  716. begin
  717. left.parent:=self;
  718. left.concattolist(l);
  719. inherited concattolist(l);
  720. end;
  721. function tunarynode.ischild(p : tnode) : boolean;
  722. begin
  723. ischild:=p=left;
  724. end;
  725. {****************************************************************************
  726. TBINARYNODE
  727. ****************************************************************************}
  728. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  729. begin
  730. inherited create(t,l);
  731. right:=r
  732. end;
  733. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  734. begin
  735. inherited ppuload(t,ppufile);
  736. right:=ppuloadnode(ppufile);
  737. end;
  738. destructor tbinarynode.destroy;
  739. begin
  740. right.free;
  741. inherited destroy;
  742. end;
  743. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  744. begin
  745. inherited ppuwrite(ppufile);
  746. ppuwritenode(ppufile,right);
  747. end;
  748. procedure tbinarynode.derefimpl;
  749. begin
  750. inherited derefimpl;
  751. if assigned(right) then
  752. right.derefimpl;
  753. end;
  754. procedure tbinarynode.concattolist(l : tlinkedlist);
  755. begin
  756. { we could change that depending on the number of }
  757. { required registers }
  758. left.parent:=self;
  759. left.concattolist(l);
  760. left.parent:=self;
  761. left.concattolist(l);
  762. inherited concattolist(l);
  763. end;
  764. function tbinarynode.ischild(p : tnode) : boolean;
  765. begin
  766. ischild:=(p=right);
  767. end;
  768. function tbinarynode.docompare(p : tnode) : boolean;
  769. begin
  770. docompare:=(inherited docompare(p) and
  771. ((right=nil) or right.isequal(tbinarynode(p).right))
  772. );
  773. end;
  774. function tbinarynode.getcopy : tnode;
  775. var
  776. p : tbinarynode;
  777. begin
  778. p:=tbinarynode(inherited getcopy);
  779. if assigned(right) then
  780. p.right:=right.getcopy
  781. else
  782. p.right:=nil;
  783. getcopy:=p;
  784. end;
  785. procedure tbinarynode.insertintolist(l : tnodelist);
  786. begin
  787. end;
  788. procedure tbinarynode.swapleftright;
  789. var
  790. swapp : tnode;
  791. begin
  792. swapp:=right;
  793. right:=left;
  794. left:=swapp;
  795. if nf_swaped in flags then
  796. exclude(flags,nf_swaped)
  797. else
  798. include(flags,nf_swaped);
  799. end;
  800. procedure tbinarynode.left_right_max;
  801. begin
  802. if assigned(left) then
  803. begin
  804. if assigned(right) then
  805. begin
  806. registers32:=max(left.registers32,right.registers32);
  807. registersfpu:=max(left.registersfpu,right.registersfpu);
  808. {$ifdef SUPPORT_MMX}
  809. registersmmx:=max(left.registersmmx,right.registersmmx);
  810. {$endif SUPPORT_MMX}
  811. end
  812. else
  813. begin
  814. registers32:=left.registers32;
  815. registersfpu:=left.registersfpu;
  816. {$ifdef SUPPORT_MMX}
  817. registersmmx:=left.registersmmx;
  818. {$endif SUPPORT_MMX}
  819. end;
  820. end;
  821. end;
  822. {$ifdef extdebug}
  823. procedure tbinarynode._dowrite;
  824. begin
  825. inherited _dowrite;
  826. writeln(',');
  827. writenode(right);
  828. end;
  829. {$endif}
  830. {****************************************************************************
  831. TBINOPYNODE
  832. ****************************************************************************}
  833. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  834. begin
  835. inherited create(t,l,r);
  836. end;
  837. function tbinopnode.docompare(p : tnode) : boolean;
  838. begin
  839. docompare:=(inherited docompare(p)) or
  840. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  841. ((nf_swapable in flags) and
  842. left.isequal(tbinopnode(p).right) and
  843. right.isequal(tbinopnode(p).left));
  844. end;
  845. end.
  846. {
  847. $Log$
  848. Revision 1.57 2002-04-25 20:15:39 florian
  849. * block nodes within expressions shouldn't release the used registers,
  850. fixed using a flag till the new rg is ready
  851. Revision 1.56 2003/04/24 22:29:58 florian
  852. * fixed a lot of PowerPC related stuff
  853. Revision 1.55 2003/04/23 10:12:14 peter
  854. * allow multi pass2 changed to global boolean instead of node flag
  855. Revision 1.54 2003/04/22 23:50:23 peter
  856. * firstpass uses expectloc
  857. * checks if there are differences between the expectloc and
  858. location.loc from secondpass in EXTDEBUG
  859. Revision 1.53 2003/04/22 09:52:00 peter
  860. * mark_write implemented for default with a warning in EXTDEBUG, this
  861. is required for error recovery where the left node can be also a non
  862. writable node
  863. Revision 1.52 2003/04/10 17:57:52 peter
  864. * vs_hidden released
  865. Revision 1.51 2003/03/28 19:16:56 peter
  866. * generic constructor working for i386
  867. * remove fixed self register
  868. * esi added as address register for i386
  869. Revision 1.50 2003/03/17 16:54:41 peter
  870. * support DefaultHandler and anonymous inheritance fixed
  871. for message methods
  872. Revision 1.49 2003/01/04 15:54:03 daniel
  873. * Fixed mark_write for @ operator
  874. (can happen when compiling @procvar:=nil (Delphi mode construction))
  875. Revision 1.48 2003/01/03 21:03:02 peter
  876. * made mark_write dummy instead of abstract
  877. Revision 1.47 2003/01/03 12:15:56 daniel
  878. * Removed ifdefs around notifications
  879. ifdefs around for loop optimizations remain
  880. Revision 1.46 2002/12/26 18:24:33 jonas
  881. * fixed check for whether or not a high parameter was already generated
  882. * no type checking/conversions for invisible parameters
  883. Revision 1.45 2002/11/28 11:17:04 florian
  884. * loop node flags from node flags splitted
  885. Revision 1.44 2002/10/05 00:48:57 peter
  886. * support inherited; support for overload as it is handled by
  887. delphi. This is only for delphi mode as it is working is
  888. undocumented and hard to predict what is done
  889. Revision 1.43 2002/09/07 15:25:03 peter
  890. * old logs removed and tabs fixed
  891. Revision 1.42 2002/09/03 16:26:26 daniel
  892. * Make Tprocdef.defs protected
  893. Revision 1.41 2002/09/01 13:28:38 daniel
  894. - write_access fields removed in favor of a flag
  895. Revision 1.40 2002/09/01 08:01:16 daniel
  896. * Removed sets from Tcallnode.det_resulttype
  897. + Added read/write notifications of variables. These will be usefull
  898. for providing information for several optimizations. For example
  899. the value of the loop variable of a for loop does matter is the
  900. variable is read after the for loop, but if it's no longer used
  901. or written, it doesn't matter and this can be used to optimize
  902. the loop code generation.
  903. Revision 1.39 2002/08/22 11:21:45 florian
  904. + register32 is now written by tnode.dowrite
  905. * fixed write of value of tconstnode
  906. Revision 1.38 2002/08/19 19:36:44 peter
  907. * More fixes for cross unit inlining, all tnodes are now implemented
  908. * Moved pocall_internconst to po_internconst because it is not a
  909. calling type at all and it conflicted when inlining of these small
  910. functions was requested
  911. Revision 1.37 2002/08/18 20:06:24 peter
  912. * inlining is now also allowed in interface
  913. * renamed write/load to ppuwrite/ppuload
  914. * tnode storing in ppu
  915. * nld,ncon,nbas are already updated for storing in ppu
  916. Revision 1.36 2002/08/17 22:09:46 florian
  917. * result type handling in tcgcal.pass_2 overhauled
  918. * better tnode.dowrite
  919. * some ppc stuff fixed
  920. Revision 1.35 2002/08/15 19:10:35 peter
  921. * first things tai,tnode storing in ppu
  922. Revision 1.34 2002/08/09 19:15:41 carl
  923. - removed newcg define
  924. Revision 1.33 2002/07/23 12:34:30 daniel
  925. * Readded old set code. To use it define 'oldset'. Activated by default
  926. for ppc.
  927. Revision 1.32 2002/07/22 11:48:04 daniel
  928. * Sets are now internally sets.
  929. Revision 1.31 2002/07/21 06:58:49 daniel
  930. * Changed booleans into flags
  931. Revision 1.30 2002/07/19 11:41:36 daniel
  932. * State tracker work
  933. * The whilen and repeatn are now completely unified into whilerepeatn. This
  934. allows the state tracker to change while nodes automatically into
  935. repeat nodes.
  936. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  937. 'not(a>b)' is optimized into 'a<=b'.
  938. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  939. by removing the notn and later switchting the true and falselabels. The
  940. same is done with 'repeat until not a'.
  941. Revision 1.29 2002/07/14 18:00:44 daniel
  942. + Added the beginning of a state tracker. This will track the values of
  943. variables through procedures and optimize things away.
  944. Revision 1.28 2002/07/01 18:46:24 peter
  945. * internal linker
  946. * reorganized aasm layer
  947. Revision 1.27 2002/05/18 13:34:10 peter
  948. * readded missing revisions
  949. Revision 1.26 2002/05/16 19:46:39 carl
  950. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  951. + try to fix temp allocation (still in ifdef)
  952. + generic constructor calls
  953. + start of tassembler / tmodulebase class cleanup
  954. Revision 1.24 2002/04/21 19:02:04 peter
  955. * removed newn and disposen nodes, the code is now directly
  956. inlined from pexpr
  957. * -an option that will write the secondpass nodes to the .s file, this
  958. requires EXTDEBUG define to actually write the info
  959. * fixed various internal errors and crashes due recent code changes
  960. Revision 1.23 2002/04/06 18:13:01 jonas
  961. * several powerpc-related additions and fixes
  962. Revision 1.22 2002/03/31 20:26:35 jonas
  963. + a_loadfpu_* and a_loadmm_* methods in tcg
  964. * register allocation is now handled by a class and is mostly processor
  965. independent (+rgobj.pas and i386/rgcpu.pas)
  966. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  967. * some small improvements and fixes to the optimizer
  968. * some register allocation fixes
  969. * some fpuvaroffset fixes in the unary minus node
  970. * push/popusedregisters is now called rg.save/restoreusedregisters and
  971. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  972. also better optimizable)
  973. * fixed and optimized register saving/restoring for new/dispose nodes
  974. * LOC_FPU locations now also require their "register" field to be set to
  975. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  976. - list field removed of the tnode class because it's not used currently
  977. and can cause hard-to-find bugs
  978. }