node.pas 41 KB

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