node.pas 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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,cgbase,
  25. aasmbase,
  26. symtype,symppu;
  27. type
  28. pconstset = ^tconstset;
  29. tconstset = set of 0..255;
  30. tnodetype = (
  31. emptynode, {No node (returns nil when loading from ppu)}
  32. addn, {Represents the + operator}
  33. muln, {Represents the * operator}
  34. subn, {Represents the - operator}
  35. divn, {Represents the div operator}
  36. symdifn, {Represents the >< operator}
  37. modn, {Represents the mod operator}
  38. assignn, {Represents an assignment}
  39. loadn, {Represents the use of a variabele}
  40. rangen, {Represents a range (i.e. 0..9)}
  41. ltn, {Represents the < operator}
  42. lten, {Represents the <= operator}
  43. gtn, {Represents the > operator}
  44. gten, {Represents the >= operator}
  45. equaln, {Represents the = operator}
  46. unequaln, {Represents the <> operator}
  47. inn, {Represents the in operator}
  48. orn, {Represents the or operator}
  49. xorn, {Represents the xor operator}
  50. shrn, {Represents the shr operator}
  51. shln, {Represents the shl operator}
  52. slashn, {Represents the / operator}
  53. andn, {Represents the and operator}
  54. subscriptn, {Field in a record/object}
  55. derefn, {Dereferences a pointer}
  56. addrn, {Represents the @ operator}
  57. ordconstn, {Represents an ordinal value}
  58. typeconvn, {Represents type-conversion/typecast}
  59. calln, {Represents a call node}
  60. callparan, {Represents a parameter}
  61. realconstn, {Represents a real value}
  62. unaryminusn, {Represents a sign change (i.e. -2)}
  63. asmn, {Represents an assembler node }
  64. vecn, {Represents array indexing}
  65. pointerconstn, {Represents a pointer constant}
  66. stringconstn, {Represents a string constant}
  67. notn, {Represents the not operator}
  68. inlinen, {Internal procedures (i.e. writeln)}
  69. niln, {Represents the nil pointer}
  70. errorn, {This part of the tree could not be
  71. parsed because of a compiler error}
  72. typen, {A type name. Used for i.e. typeof(obj)}
  73. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  74. setconstn, {A set constant (i.e. [1,2])}
  75. blockn, {A block of statements}
  76. statementn, {One statement in a block of nodes}
  77. ifn, {An if statement}
  78. breakn, {A break statement}
  79. continuen, {A continue statement}
  80. whilerepeatn, {A while or repeat statement}
  81. forn, {A for loop}
  82. exitn, {An exit statement}
  83. withn, {A with statement}
  84. casen, {A case statement}
  85. labeln, {A label}
  86. goton, {A goto statement}
  87. tryexceptn, {A try except block}
  88. raisen, {A raise statement}
  89. tryfinallyn, {A try finally statement}
  90. onn, {For an on statement in exception code}
  91. isn, {Represents the is operator}
  92. asn, {Represents the as typecast}
  93. caretn, {Represents the ^ operator}
  94. starstarn, {Represents the ** operator exponentiation }
  95. arrayconstructorn, {Construction node for [...] parsing}
  96. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  97. tempcreaten, { for temps in the result/firstpass }
  98. temprefn, { references to temps }
  99. tempdeleten, { for temps in the result/firstpass }
  100. addoptn, { added for optimizations where we cannot suppress }
  101. nothingn, {NOP, Do nothing}
  102. loadvmtaddrn, {Load the address of the VMT of a class/object}
  103. guidconstn, {A GUID COM Interface constant }
  104. rttin, {Rtti information so they can be accessed in result/firstpass}
  105. loadparentfpn { Load the framepointer of the parent for nested procedures }
  106. );
  107. const
  108. nodetype2str : array[tnodetype] of string[24] = (
  109. '<emptynode>',
  110. 'addn',
  111. 'muln',
  112. 'subn',
  113. 'divn',
  114. 'symdifn',
  115. 'modn',
  116. 'assignn',
  117. 'loadn',
  118. 'rangen',
  119. 'ltn',
  120. 'lten',
  121. 'gtn',
  122. 'gten',
  123. 'equaln',
  124. 'unequaln',
  125. 'inn',
  126. 'orn',
  127. 'xorn',
  128. 'shrn',
  129. 'shln',
  130. 'slashn',
  131. 'andn',
  132. 'subscriptn',
  133. 'derefn',
  134. 'addrn',
  135. 'ordconstn',
  136. 'typeconvn',
  137. 'calln',
  138. 'callparan',
  139. 'realconstn',
  140. 'unaryminusn',
  141. 'asmn',
  142. 'vecn',
  143. 'pointerconstn',
  144. 'stringconstn',
  145. 'notn',
  146. 'inlinen',
  147. 'niln',
  148. 'errorn',
  149. 'typen',
  150. 'setelementn',
  151. 'setconstn',
  152. 'blockn',
  153. 'statementn',
  154. 'ifn',
  155. 'breakn',
  156. 'continuen',
  157. 'whilerepeatn',
  158. 'forn',
  159. 'exitn',
  160. 'withn',
  161. 'casen',
  162. 'labeln',
  163. 'goton',
  164. 'tryexceptn',
  165. 'raisen',
  166. 'tryfinallyn',
  167. 'onn',
  168. 'isn',
  169. 'asn',
  170. 'caretn',
  171. 'starstarn',
  172. 'arrayconstructn',
  173. 'arrayconstructrangen',
  174. 'tempcreaten',
  175. 'temprefn',
  176. 'tempdeleten',
  177. 'addoptn',
  178. 'nothingn',
  179. 'loadvmtaddrn',
  180. 'guidconstn',
  181. 'rttin',
  182. 'loadparentfpn');
  183. type
  184. { all boolean field of ttree are now collected in flags }
  185. tnodeflag = (
  186. nf_swapable, { tbinop operands can be swaped }
  187. nf_swaped, { tbinop operands are swaped }
  188. nf_error,
  189. { general }
  190. nf_write, { Node is written to }
  191. nf_varstateset,
  192. nf_isproperty,
  193. { flags used by tcallnode }
  194. nf_return_value_used,
  195. nf_inherited,
  196. nf_anon_inherited,
  197. nf_new_call,
  198. nf_dispose_call,
  199. nf_member_call, { called with implicit methodpointer tree }
  200. { flags used by tcallparanode }
  201. nf_varargs_para, { belongs this para to varargs }
  202. { taddrnode }
  203. nf_procvarload,
  204. { tvecnode }
  205. nf_memindex,
  206. nf_memseg,
  207. nf_callunique,
  208. { tloadnode }
  209. nf_absolute,
  210. nf_load_self_pointer,
  211. { taddnode }
  212. nf_is_currency,
  213. { tassignmentnode }
  214. nf_concat_string,
  215. nf_use_strconcat,
  216. { tarrayconstructnode }
  217. nf_cargs,
  218. nf_cargswap,
  219. nf_forcevaria,
  220. nf_novariaallowed,
  221. { ttypeconvnode }
  222. nf_explicit,
  223. { tinlinenode }
  224. nf_inlineconst,
  225. { tblocknode }
  226. nf_releasetemps
  227. );
  228. tnodeflags = set of tnodeflag;
  229. const
  230. { contains the flags which must be equal for the equality }
  231. { of nodes }
  232. flagsequal : tnodeflags = [nf_error];
  233. type
  234. tnodelist = class
  235. end;
  236. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  237. tnode = class
  238. public
  239. { type of this node }
  240. nodetype : tnodetype;
  241. { type of the current code block, general/const/type }
  242. blocktype : tblock_type;
  243. { expected location of the result of this node (pass1) }
  244. expectloc : tcgloc;
  245. { the location of the result of this node (pass2) }
  246. location : tlocation;
  247. { the parent node of this is node }
  248. { this field is set by concattolist }
  249. parent : tnode;
  250. { there are some properties about the node stored }
  251. flags : tnodeflags;
  252. ppuidx : longint;
  253. { the number of registers needed to evalute the node }
  254. registers32,registersfpu : longint; { must be longint !!!! }
  255. {$ifdef SUPPORT_MMX}
  256. registersmmx,registerskni : longint;
  257. {$endif SUPPORT_MMX}
  258. resulttype : ttype;
  259. fileinfo : tfileposinfo;
  260. localswitches : tlocalswitches;
  261. {$ifdef extdebug}
  262. maxfirstpasscount,
  263. firstpasscount : longint;
  264. {$endif extdebug}
  265. constructor create(t:tnodetype);
  266. { this constructor is only for creating copies of class }
  267. { the fields are copied by getcopy }
  268. constructor createforcopy;
  269. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  270. destructor destroy;override;
  271. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  272. procedure buildderefimpl;virtual;
  273. procedure derefimpl;virtual;
  274. procedure derefnode;virtual;
  275. { toggles the flag }
  276. procedure toggleflag(f : tnodeflag);
  277. { the 1.1 code generator may override pass_1 }
  278. { and it need not to implement det_* then }
  279. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  280. { 2.0: runs det_resulttype and det_temp }
  281. function pass_1 : tnode;virtual;abstract;
  282. { dermines the resulttype of the node }
  283. function det_resulttype : tnode;virtual;abstract;
  284. { dermines the number of necessary temp. locations to evaluate
  285. the node }
  286. {$ifdef state_tracking}
  287. { Does optimizations by keeping track of the variable states
  288. in a procedure }
  289. function track_state_pass(exec_known:boolean):boolean;virtual;
  290. {$endif}
  291. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  292. written to (normally the loadnode) as write access. }
  293. procedure mark_write;virtual;
  294. procedure det_temp;virtual;abstract;
  295. procedure pass_2;virtual;abstract;
  296. { comparing of nodes }
  297. function isequal(p : tnode) : boolean;
  298. { to implement comparisation, override this method }
  299. function docompare(p : tnode) : boolean;virtual;
  300. { gets a copy of the node }
  301. function getcopy : tnode;virtual;
  302. procedure insertintolist(l : tnodelist);virtual;
  303. { writes a node for debugging purpose, shouldn't be called }
  304. { direct, because there is no test for nil, use printnode }
  305. { to write a complete tree }
  306. procedure printnodeinfo(var t:text);
  307. procedure printnodedata(var t:text);virtual;
  308. procedure printnodetree(var t:text);virtual;
  309. procedure concattolist(l : tlinkedlist);virtual;
  310. function ischild(p : tnode) : boolean;virtual;
  311. procedure set_file_line(from : tnode);
  312. procedure set_tree_filepos(const filepos : tfileposinfo);
  313. end;
  314. tnodeclass = class of tnode;
  315. tnodeclassarray = array[tnodetype] of tnodeclass;
  316. { this node is the anchestor for all nodes with at least }
  317. { one child, you have to use it if you want to use }
  318. { true- and falselabel }
  319. punarynode = ^tunarynode;
  320. tunarynode = class(tnode)
  321. left : tnode;
  322. constructor create(t:tnodetype;l : tnode);
  323. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  324. destructor destroy;override;
  325. procedure ppuwrite(ppufile:tcompilerppufile);override;
  326. procedure buildderefimpl;override;
  327. procedure derefimpl;override;
  328. procedure derefnode;override;
  329. procedure concattolist(l : tlinkedlist);override;
  330. function ischild(p : tnode) : boolean;override;
  331. function docompare(p : tnode) : boolean;override;
  332. function getcopy : tnode;override;
  333. procedure insertintolist(l : tnodelist);override;
  334. procedure left_max;
  335. procedure printnodedata(var t:text);override;
  336. end;
  337. pbinarynode = ^tbinarynode;
  338. tbinarynode = class(tunarynode)
  339. right : tnode;
  340. constructor create(t:tnodetype;l,r : tnode);
  341. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  342. destructor destroy;override;
  343. procedure ppuwrite(ppufile:tcompilerppufile);override;
  344. procedure buildderefimpl;override;
  345. procedure derefimpl;override;
  346. procedure derefnode;override;
  347. procedure concattolist(l : tlinkedlist);override;
  348. function ischild(p : tnode) : boolean;override;
  349. function docompare(p : tnode) : boolean;override;
  350. procedure swapleftright;
  351. function getcopy : tnode;override;
  352. procedure insertintolist(l : tnodelist);override;
  353. procedure left_right_max;
  354. procedure printnodedata(var t:text);override;
  355. procedure printnodelist(var t:text);
  356. end;
  357. tbinopnode = class(tbinarynode)
  358. constructor create(t:tnodetype;l,r : tnode);virtual;
  359. function docompare(p : tnode) : boolean;override;
  360. end;
  361. var
  362. { array with all class types for tnodes }
  363. nodeclass : tnodeclassarray;
  364. function nodeppuidxget(i:longint):tnode;
  365. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  366. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  367. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  368. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  369. const
  370. printnodespacing = ' ';
  371. var
  372. { indention used when writing the tree to the screen }
  373. printnodeindention : string;
  374. procedure printnodeindent;
  375. procedure printnodeunindent;
  376. procedure printnode(var t:text;n:tnode);
  377. implementation
  378. uses
  379. cutils,verbose,ppu;
  380. const
  381. ppunodemarker = 255;
  382. {****************************************************************************
  383. Helpers
  384. ****************************************************************************}
  385. var
  386. nodeppudata : tdynamicarray;
  387. nodeppuidx : longint;
  388. procedure nodeppuidxcreate;
  389. begin
  390. nodeppudata:=tdynamicarray.create(1024);
  391. nodeppuidx:=0;
  392. end;
  393. procedure nodeppuidxfree;
  394. begin
  395. nodeppudata.free;
  396. nodeppudata:=nil;
  397. end;
  398. procedure nodeppuidxadd(n:tnode);
  399. begin
  400. if n.ppuidx<0 then
  401. internalerror(200311072);
  402. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  403. nodeppudata.write(n,sizeof(pointer));
  404. end;
  405. function nodeppuidxget(i:longint):tnode;
  406. var
  407. l : longint;
  408. begin
  409. if i<0 then
  410. internalerror(200311072);
  411. nodeppudata.seek(i*sizeof(pointer));
  412. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  413. internalerror(200311073);
  414. end;
  415. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  416. var
  417. b : byte;
  418. t : tnodetype;
  419. hppuidx : longint;
  420. begin
  421. { marker }
  422. b:=ppufile.getbyte;
  423. if b<>ppunodemarker then
  424. internalerror(200208151);
  425. { load nodetype }
  426. t:=tnodetype(ppufile.getbyte);
  427. if t>high(tnodetype) then
  428. internalerror(200208152);
  429. if t<>emptynode then
  430. begin
  431. if not assigned(nodeclass[t]) then
  432. internalerror(200208153);
  433. hppuidx:=ppufile.getlongint;
  434. //writeln('load: ',nodetype2str[t]);
  435. { generate node of the correct class }
  436. result:=nodeclass[t].ppuload(t,ppufile);
  437. result.ppuidx:=hppuidx;
  438. nodeppuidxadd(result);
  439. end
  440. else
  441. result:=nil;
  442. end;
  443. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  444. begin
  445. { marker, read by ppuloadnode }
  446. ppufile.putbyte(ppunodemarker);
  447. { type, read by ppuloadnode }
  448. if assigned(n) then
  449. begin
  450. if n.ppuidx=-1 then
  451. internalerror(200311071);
  452. n.ppuidx:=nodeppuidx;
  453. inc(nodeppuidx);
  454. ppufile.putbyte(byte(n.nodetype));
  455. ppufile.putlongint(n.ppuidx);
  456. //writeln('write: ',nodetype2str[n.nodetype]);
  457. n.ppuwrite(ppufile);
  458. end
  459. else
  460. ppufile.putbyte(byte(emptynode));
  461. end;
  462. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  463. begin
  464. if ppufile.readentry<>ibnodetree then
  465. Message(unit_f_ppu_read_error);
  466. nodeppuidxcreate;
  467. result:=ppuloadnode(ppufile);
  468. result.derefnode;
  469. nodeppuidxfree;
  470. end;
  471. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  472. begin
  473. nodeppuidx:=0;
  474. ppuwritenode(ppufile,n);
  475. ppufile.writeentry(ibnodetree);
  476. end;
  477. procedure printnodeindent;
  478. begin
  479. printnodeindention:=printnodeindention+printnodespacing;
  480. end;
  481. procedure printnodeunindent;
  482. begin
  483. delete(printnodeindention,1,length(printnodespacing));
  484. end;
  485. procedure printnode(var t:text;n:tnode);
  486. begin
  487. if assigned(n) then
  488. n.printnodetree(t)
  489. else
  490. writeln(t,printnodeindention,'nil');
  491. end;
  492. {****************************************************************************
  493. TNODE
  494. ****************************************************************************}
  495. constructor tnode.create(t:tnodetype);
  496. begin
  497. inherited create;
  498. nodetype:=t;
  499. blocktype:=block_type;
  500. { updated by firstpass }
  501. expectloc:=LOC_INVALID;
  502. { updated by secondpass }
  503. location.loc:=LOC_INVALID;
  504. { save local info }
  505. fileinfo:=aktfilepos;
  506. localswitches:=aktlocalswitches;
  507. resulttype.reset;
  508. registers32:=0;
  509. registersfpu:=0;
  510. {$ifdef SUPPORT_MMX}
  511. registersmmx:=0;
  512. {$endif SUPPORT_MMX}
  513. {$ifdef EXTDEBUG}
  514. maxfirstpasscount:=0;
  515. firstpasscount:=0;
  516. {$endif EXTDEBUG}
  517. flags:=[];
  518. ppuidx:=-1;
  519. end;
  520. constructor tnode.createforcopy;
  521. begin
  522. end;
  523. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  524. begin
  525. nodetype:=t;
  526. { tnode fields }
  527. blocktype:=tblock_type(ppufile.getbyte);
  528. ppufile.getposinfo(fileinfo);
  529. ppufile.getsmallset(localswitches);
  530. ppufile.gettype(resulttype);
  531. ppufile.getsmallset(flags);
  532. { updated by firstpass }
  533. expectloc:=LOC_INVALID;
  534. { updated by secondpass }
  535. location.loc:=LOC_INVALID;
  536. registers32:=0;
  537. registersfpu:=0;
  538. {$ifdef SUPPORT_MMX}
  539. registersmmx:=0;
  540. {$endif SUPPORT_MMX}
  541. {$ifdef EXTDEBUG}
  542. maxfirstpasscount:=0;
  543. firstpasscount:=0;
  544. {$endif EXTDEBUG}
  545. ppuidx:=-1;
  546. end;
  547. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  548. begin
  549. ppufile.putbyte(byte(block_type));
  550. ppufile.putposinfo(fileinfo);
  551. ppufile.putsmallset(localswitches);
  552. ppufile.puttype(resulttype);
  553. ppufile.putsmallset(flags);
  554. end;
  555. procedure tnode.buildderefimpl;
  556. begin
  557. resulttype.buildderef;
  558. end;
  559. procedure tnode.derefimpl;
  560. begin
  561. resulttype.resolve;
  562. end;
  563. procedure tnode.derefnode;
  564. begin
  565. end;
  566. procedure tnode.toggleflag(f : tnodeflag);
  567. begin
  568. if f in flags then
  569. exclude(flags,f)
  570. else
  571. include(flags,f);
  572. end;
  573. destructor tnode.destroy;
  574. begin
  575. {$ifdef EXTDEBUG}
  576. if firstpasscount>maxfirstpasscount then
  577. maxfirstpasscount:=firstpasscount;
  578. {$endif EXTDEBUG}
  579. end;
  580. procedure tnode.concattolist(l : tlinkedlist);
  581. begin
  582. end;
  583. function tnode.ischild(p : tnode) : boolean;
  584. begin
  585. ischild:=false;
  586. end;
  587. procedure tnode.mark_write;
  588. begin
  589. {$ifdef EXTDEBUG}
  590. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  591. {$endif EXTDEBUG}
  592. end;
  593. procedure tnode.printnodeinfo(var t:text);
  594. begin
  595. write(t,nodetype2str[nodetype]);
  596. if assigned(resulttype.def) then
  597. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  598. else
  599. write(t,', resulttype = <nil>');
  600. writeln(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  601. ', loc = ',tcgloc2str[location.loc],
  602. ', expectloc = ',tcgloc2str[expectloc],
  603. ', intregs = ',registers32,
  604. ', fpuregs = ',registersfpu);
  605. end;
  606. procedure tnode.printnodedata(var t:text);
  607. begin
  608. end;
  609. procedure tnode.printnodetree(var t:text);
  610. begin
  611. write(t,printnodeindention,'(');
  612. printnodeinfo(t);
  613. printnodeindent;
  614. printnodedata(t);
  615. printnodeunindent;
  616. writeln(t,printnodeindention,')');
  617. end;
  618. function tnode.isequal(p : tnode) : boolean;
  619. begin
  620. isequal:=
  621. (not assigned(self) and not assigned(p)) or
  622. (assigned(self) and assigned(p) and
  623. { optimized subclasses have the same nodetype as their }
  624. { superclass (for compatibility), so also check the classtype (JM) }
  625. (p.classtype=classtype) and
  626. (p.nodetype=nodetype) and
  627. (flags*flagsequal=p.flags*flagsequal) and
  628. docompare(p));
  629. end;
  630. {$ifdef state_tracking}
  631. function Tnode.track_state_pass(exec_known:boolean):boolean;
  632. begin
  633. track_state_pass:=false;
  634. end;
  635. {$endif state_tracking}
  636. function tnode.docompare(p : tnode) : boolean;
  637. begin
  638. docompare:=true;
  639. end;
  640. function tnode.getcopy : tnode;
  641. var
  642. p : tnode;
  643. begin
  644. { this is quite tricky because we need a node of the current }
  645. { node type and not one of tnode! }
  646. p:=tnodeclass(classtype).createforcopy;
  647. p.nodetype:=nodetype;
  648. p.location:=location;
  649. p.parent:=parent;
  650. p.flags:=flags;
  651. p.registers32:=registers32;
  652. p.registersfpu:=registersfpu;
  653. {$ifdef SUPPORT_MMX}
  654. p.registersmmx:=registersmmx;
  655. p.registerskni:=registerskni;
  656. {$endif SUPPORT_MMX}
  657. p.resulttype:=resulttype;
  658. p.fileinfo:=fileinfo;
  659. p.localswitches:=localswitches;
  660. {$ifdef extdebug}
  661. p.firstpasscount:=firstpasscount;
  662. {$endif extdebug}
  663. { p.list:=list; }
  664. getcopy:=p;
  665. end;
  666. procedure tnode.insertintolist(l : tnodelist);
  667. begin
  668. end;
  669. procedure tnode.set_file_line(from : tnode);
  670. begin
  671. if assigned(from) then
  672. fileinfo:=from.fileinfo;
  673. end;
  674. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  675. begin
  676. fileinfo:=filepos;
  677. end;
  678. {****************************************************************************
  679. TUNARYNODE
  680. ****************************************************************************}
  681. constructor tunarynode.create(t:tnodetype;l : tnode);
  682. begin
  683. inherited create(t);
  684. left:=l;
  685. end;
  686. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  687. begin
  688. inherited ppuload(t,ppufile);
  689. left:=ppuloadnode(ppufile);
  690. end;
  691. destructor tunarynode.destroy;
  692. begin
  693. left.free;
  694. inherited destroy;
  695. end;
  696. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  697. begin
  698. inherited ppuwrite(ppufile);
  699. ppuwritenode(ppufile,left);
  700. end;
  701. procedure tunarynode.buildderefimpl;
  702. begin
  703. inherited buildderefimpl;
  704. if assigned(left) then
  705. left.buildderefimpl;
  706. end;
  707. procedure tunarynode.derefimpl;
  708. begin
  709. inherited derefimpl;
  710. if assigned(left) then
  711. left.derefimpl;
  712. end;
  713. procedure tunarynode.derefnode;
  714. begin
  715. inherited derefnode;
  716. if assigned(left) then
  717. left.derefnode;
  718. end;
  719. function tunarynode.docompare(p : tnode) : boolean;
  720. begin
  721. docompare:=(inherited docompare(p) and
  722. ((left=nil) or left.isequal(tunarynode(p).left))
  723. );
  724. end;
  725. function tunarynode.getcopy : tnode;
  726. var
  727. p : tunarynode;
  728. begin
  729. p:=tunarynode(inherited getcopy);
  730. if assigned(left) then
  731. p.left:=left.getcopy
  732. else
  733. p.left:=nil;
  734. getcopy:=p;
  735. end;
  736. procedure tunarynode.insertintolist(l : tnodelist);
  737. begin
  738. end;
  739. procedure tunarynode.printnodedata(var t:text);
  740. begin
  741. inherited printnodedata(t);
  742. printnode(t,left);
  743. end;
  744. procedure tunarynode.left_max;
  745. begin
  746. registers32:=left.registers32;
  747. registersfpu:=left.registersfpu;
  748. {$ifdef SUPPORT_MMX}
  749. registersmmx:=left.registersmmx;
  750. {$endif SUPPORT_MMX}
  751. end;
  752. procedure tunarynode.concattolist(l : tlinkedlist);
  753. begin
  754. left.parent:=self;
  755. left.concattolist(l);
  756. inherited concattolist(l);
  757. end;
  758. function tunarynode.ischild(p : tnode) : boolean;
  759. begin
  760. ischild:=p=left;
  761. end;
  762. {****************************************************************************
  763. TBINARYNODE
  764. ****************************************************************************}
  765. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  766. begin
  767. inherited create(t,l);
  768. right:=r
  769. end;
  770. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  771. begin
  772. inherited ppuload(t,ppufile);
  773. right:=ppuloadnode(ppufile);
  774. end;
  775. destructor tbinarynode.destroy;
  776. begin
  777. right.free;
  778. inherited destroy;
  779. end;
  780. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  781. begin
  782. inherited ppuwrite(ppufile);
  783. ppuwritenode(ppufile,right);
  784. end;
  785. procedure tbinarynode.buildderefimpl;
  786. begin
  787. inherited buildderefimpl;
  788. if assigned(right) then
  789. right.buildderefimpl;
  790. end;
  791. procedure tbinarynode.derefimpl;
  792. begin
  793. inherited derefimpl;
  794. if assigned(right) then
  795. right.derefimpl;
  796. end;
  797. procedure tbinarynode.derefnode;
  798. begin
  799. inherited derefnode;
  800. if assigned(right) then
  801. right.derefnode;
  802. end;
  803. procedure tbinarynode.concattolist(l : tlinkedlist);
  804. begin
  805. { we could change that depending on the number of }
  806. { required registers }
  807. left.parent:=self;
  808. left.concattolist(l);
  809. left.parent:=self;
  810. left.concattolist(l);
  811. inherited concattolist(l);
  812. end;
  813. function tbinarynode.ischild(p : tnode) : boolean;
  814. begin
  815. ischild:=(p=right);
  816. end;
  817. function tbinarynode.docompare(p : tnode) : boolean;
  818. begin
  819. docompare:=(inherited docompare(p) and
  820. ((right=nil) or right.isequal(tbinarynode(p).right))
  821. );
  822. end;
  823. function tbinarynode.getcopy : tnode;
  824. var
  825. p : tbinarynode;
  826. begin
  827. p:=tbinarynode(inherited getcopy);
  828. if assigned(right) then
  829. p.right:=right.getcopy
  830. else
  831. p.right:=nil;
  832. getcopy:=p;
  833. end;
  834. procedure tbinarynode.insertintolist(l : tnodelist);
  835. begin
  836. end;
  837. procedure tbinarynode.swapleftright;
  838. var
  839. swapp : tnode;
  840. begin
  841. swapp:=right;
  842. right:=left;
  843. left:=swapp;
  844. if nf_swaped in flags then
  845. exclude(flags,nf_swaped)
  846. else
  847. include(flags,nf_swaped);
  848. end;
  849. procedure tbinarynode.left_right_max;
  850. begin
  851. if assigned(left) then
  852. begin
  853. if assigned(right) then
  854. begin
  855. registers32:=max(left.registers32,right.registers32);
  856. registersfpu:=max(left.registersfpu,right.registersfpu);
  857. {$ifdef SUPPORT_MMX}
  858. registersmmx:=max(left.registersmmx,right.registersmmx);
  859. {$endif SUPPORT_MMX}
  860. end
  861. else
  862. begin
  863. registers32:=left.registers32;
  864. registersfpu:=left.registersfpu;
  865. {$ifdef SUPPORT_MMX}
  866. registersmmx:=left.registersmmx;
  867. {$endif SUPPORT_MMX}
  868. end;
  869. end;
  870. end;
  871. procedure tbinarynode.printnodedata(var t:text);
  872. begin
  873. inherited printnodedata(t);
  874. printnode(t,right);
  875. end;
  876. procedure tbinarynode.printnodelist(var t:text);
  877. var
  878. hp : tbinarynode;
  879. begin
  880. hp:=self;
  881. while assigned(hp) do
  882. begin
  883. write(t,printnodeindention,'(');
  884. printnodeindent;
  885. hp.printnodeinfo(t);
  886. printnode(t,hp.left);
  887. printnodeunindent;
  888. writeln(t,printnodeindention,')');
  889. hp:=tbinarynode(hp.right);
  890. end;
  891. end;
  892. {****************************************************************************
  893. TBINOPYNODE
  894. ****************************************************************************}
  895. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  896. begin
  897. inherited create(t,l,r);
  898. end;
  899. function tbinopnode.docompare(p : tnode) : boolean;
  900. begin
  901. docompare:=(inherited docompare(p)) or
  902. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  903. ((nf_swapable in flags) and
  904. left.isequal(tbinopnode(p).right) and
  905. right.isequal(tbinopnode(p).left));
  906. end;
  907. end.
  908. {
  909. $Log$
  910. Revision 1.74 2003-11-10 22:02:52 peter
  911. * cross unit inlining fixed
  912. Revision 1.73 2003/10/23 14:44:07 peter
  913. * splitted buildderef and buildderefimpl to fix interface crc
  914. calculation
  915. Revision 1.72 2003/10/22 20:40:00 peter
  916. * write derefdata in a separate ppu entry
  917. Revision 1.71 2003/10/18 15:41:26 peter
  918. * made worklists dynamic in size
  919. Revision 1.70 2003/10/17 01:22:08 florian
  920. * compilation of the powerpc compiler fixed
  921. Revision 1.69 2003/10/08 19:19:45 peter
  922. * set_varstate cleanup
  923. Revision 1.68 2003/10/01 20:34:49 peter
  924. * procinfo unit contains tprocinfo
  925. * cginfo renamed to cgbase
  926. * moved cgmessage to verbose
  927. * fixed ppc and sparc compiles
  928. Revision 1.67 2003/09/28 17:55:04 peter
  929. * parent framepointer changed to hidden parameter
  930. * tloadparentfpnode added
  931. Revision 1.66 2003/09/06 22:27:08 florian
  932. * fixed web bug 2669
  933. * cosmetic fix in printnode
  934. * tobjectdef.gettypename implemented
  935. Revision 1.65 2003/09/03 15:55:01 peter
  936. * NEWRA branch merged
  937. Revision 1.64 2003/09/03 11:18:37 florian
  938. * fixed arm concatcopy
  939. + arm support in the common compiler sources added
  940. * moved some generic cg code around
  941. + tfputype added
  942. * ...
  943. Revision 1.63 2003/08/10 17:25:23 peter
  944. * fixed some reported bugs
  945. Revision 1.62 2003/05/26 21:17:17 peter
  946. * procinlinenode removed
  947. * aktexit2label removed, fast exit removed
  948. + tcallnode.inlined_pass_2 added
  949. Revision 1.61 2003/05/13 19:14:41 peter
  950. * failn removed
  951. * inherited result code check moven to pexpr
  952. Revision 1.60 2003/05/11 21:37:03 peter
  953. * moved implicit exception frame from ncgutil to psub
  954. * constructor/destructor helpers moved from cobj/ncgutil to psub
  955. Revision 1.59 2003/05/09 17:47:02 peter
  956. * self moved to hidden parameter
  957. * removed hdisposen,hnewn,selfn
  958. Revision 1.58 2003/04/25 20:59:33 peter
  959. * removed funcretn,funcretsym, function result is now in varsym
  960. and aliases for result and function name are added using absolutesym
  961. * vs_hidden parameter for funcret passed in parameter
  962. * vs_hidden fixes
  963. * writenode changed to printnode and released from extdebug
  964. * -vp option added to generate a tree.log with the nodetree
  965. * nicer printnode for statements, callnode
  966. Revision 1.57 2002/04/25 20:15:39 florian
  967. * block nodes within expressions shouldn't release the used registers,
  968. fixed using a flag till the new rg is ready
  969. Revision 1.56 2003/04/24 22:29:58 florian
  970. * fixed a lot of PowerPC related stuff
  971. Revision 1.55 2003/04/23 10:12:14 peter
  972. * allow multi pass2 changed to global boolean instead of node flag
  973. Revision 1.54 2003/04/22 23:50:23 peter
  974. * firstpass uses expectloc
  975. * checks if there are differences between the expectloc and
  976. location.loc from secondpass in EXTDEBUG
  977. Revision 1.53 2003/04/22 09:52:00 peter
  978. * mark_write implemented for default with a warning in EXTDEBUG, this
  979. is required for error recovery where the left node can be also a non
  980. writable node
  981. Revision 1.52 2003/04/10 17:57:52 peter
  982. * vs_hidden released
  983. Revision 1.51 2003/03/28 19:16:56 peter
  984. * generic constructor working for i386
  985. * remove fixed self register
  986. * esi added as address register for i386
  987. Revision 1.50 2003/03/17 16:54:41 peter
  988. * support DefaultHandler and anonymous inheritance fixed
  989. for message methods
  990. Revision 1.49 2003/01/04 15:54:03 daniel
  991. * Fixed mark_write for @ operator
  992. (can happen when compiling @procvar:=nil (Delphi mode construction))
  993. Revision 1.48 2003/01/03 21:03:02 peter
  994. * made mark_write dummy instead of abstract
  995. Revision 1.47 2003/01/03 12:15:56 daniel
  996. * Removed ifdefs around notifications
  997. ifdefs around for loop optimizations remain
  998. Revision 1.46 2002/12/26 18:24:33 jonas
  999. * fixed check for whether or not a high parameter was already generated
  1000. * no type checking/conversions for invisible parameters
  1001. Revision 1.45 2002/11/28 11:17:04 florian
  1002. * loop node flags from node flags splitted
  1003. Revision 1.44 2002/10/05 00:48:57 peter
  1004. * support inherited; support for overload as it is handled by
  1005. delphi. This is only for delphi mode as it is working is
  1006. undocumented and hard to predict what is done
  1007. Revision 1.43 2002/09/07 15:25:03 peter
  1008. * old logs removed and tabs fixed
  1009. Revision 1.42 2002/09/03 16:26:26 daniel
  1010. * Make Tprocdef.defs protected
  1011. Revision 1.41 2002/09/01 13:28:38 daniel
  1012. - write_access fields removed in favor of a flag
  1013. Revision 1.40 2002/09/01 08:01:16 daniel
  1014. * Removed sets from Tcallnode.det_resulttype
  1015. + Added read/write notifications of variables. These will be usefull
  1016. for providing information for several optimizations. For example
  1017. the value of the loop variable of a for loop does matter is the
  1018. variable is read after the for loop, but if it's no longer used
  1019. or written, it doesn't matter and this can be used to optimize
  1020. the loop code generation.
  1021. Revision 1.39 2002/08/22 11:21:45 florian
  1022. + register32 is now written by tnode.dowrite
  1023. * fixed write of value of tconstnode
  1024. Revision 1.38 2002/08/19 19:36:44 peter
  1025. * More fixes for cross unit inlining, all tnodes are now implemented
  1026. * Moved pocall_internconst to po_internconst because it is not a
  1027. calling type at all and it conflicted when inlining of these small
  1028. functions was requested
  1029. Revision 1.37 2002/08/18 20:06:24 peter
  1030. * inlining is now also allowed in interface
  1031. * renamed write/load to ppuwrite/ppuload
  1032. * tnode storing in ppu
  1033. * nld,ncon,nbas are already updated for storing in ppu
  1034. Revision 1.36 2002/08/17 22:09:46 florian
  1035. * result type handling in tcgcal.pass_2 overhauled
  1036. * better tnode.dowrite
  1037. * some ppc stuff fixed
  1038. Revision 1.35 2002/08/15 19:10:35 peter
  1039. * first things tai,tnode storing in ppu
  1040. Revision 1.34 2002/08/09 19:15:41 carl
  1041. - removed newcg define
  1042. Revision 1.33 2002/07/23 12:34:30 daniel
  1043. * Readded old set code. To use it define 'oldset'. Activated by default
  1044. for ppc.
  1045. Revision 1.32 2002/07/22 11:48:04 daniel
  1046. * Sets are now internally sets.
  1047. Revision 1.31 2002/07/21 06:58:49 daniel
  1048. * Changed booleans into flags
  1049. Revision 1.30 2002/07/19 11:41:36 daniel
  1050. * State tracker work
  1051. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1052. allows the state tracker to change while nodes automatically into
  1053. repeat nodes.
  1054. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1055. 'not(a>b)' is optimized into 'a<=b'.
  1056. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1057. by removing the notn and later switchting the true and falselabels. The
  1058. same is done with 'repeat until not a'.
  1059. Revision 1.29 2002/07/14 18:00:44 daniel
  1060. + Added the beginning of a state tracker. This will track the values of
  1061. variables through procedures and optimize things away.
  1062. Revision 1.28 2002/07/01 18:46:24 peter
  1063. * internal linker
  1064. * reorganized aasm layer
  1065. Revision 1.27 2002/05/18 13:34:10 peter
  1066. * readded missing revisions
  1067. Revision 1.26 2002/05/16 19:46:39 carl
  1068. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1069. + try to fix temp allocation (still in ifdef)
  1070. + generic constructor calls
  1071. + start of tassembler / tmodulebase class cleanup
  1072. Revision 1.24 2002/04/21 19:02:04 peter
  1073. * removed newn and disposen nodes, the code is now directly
  1074. inlined from pexpr
  1075. * -an option that will write the secondpass nodes to the .s file, this
  1076. requires EXTDEBUG define to actually write the info
  1077. * fixed various internal errors and crashes due recent code changes
  1078. Revision 1.23 2002/04/06 18:13:01 jonas
  1079. * several powerpc-related additions and fixes
  1080. Revision 1.22 2002/03/31 20:26:35 jonas
  1081. + a_loadfpu_* and a_loadmm_* methods in tcg
  1082. * register allocation is now handled by a class and is mostly processor
  1083. independent (+rgobj.pas and i386/rgcpu.pas)
  1084. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1085. * some small improvements and fixes to the optimizer
  1086. * some register allocation fixes
  1087. * some fpuvaroffset fixes in the unary minus node
  1088. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1089. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1090. also better optimizable)
  1091. * fixed and optimized register saving/restoring for new/dispose nodes
  1092. * LOC_FPU locations now also require their "register" field to be set to
  1093. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1094. - list field removed of the tnode class because it's not used currently
  1095. and can cause hard-to-find bugs
  1096. }