node.pas 40 KB

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