node.pas 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Basic node handling
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit node;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,
  23. cpubase,cgbase,cgutils,
  24. aasmbase,
  25. symtype;
  26. type
  27. tnodetype = (
  28. emptynode, {No node (returns nil when loading from ppu)}
  29. addn, {Represents the + operator}
  30. muln, {Represents the * operator}
  31. subn, {Represents the - operator}
  32. divn, {Represents the div operator}
  33. symdifn, {Represents the >< operator}
  34. modn, {Represents the mod operator}
  35. assignn, {Represents an assignment}
  36. loadn, {Represents the use of a variabele}
  37. rangen, {Represents a range (i.e. 0..9)}
  38. ltn, {Represents the < operator}
  39. lten, {Represents the <= operator}
  40. gtn, {Represents the > operator}
  41. gten, {Represents the >= operator}
  42. equaln, {Represents the = operator}
  43. unequaln, {Represents the <> operator}
  44. inn, {Represents the in operator}
  45. orn, {Represents the or operator}
  46. xorn, {Represents the xor operator}
  47. shrn, {Represents the shr operator}
  48. shln, {Represents the shl operator}
  49. slashn, {Represents the / operator}
  50. andn, {Represents the and operator}
  51. subscriptn, {Field in a record/object}
  52. derefn, {Dereferences a pointer}
  53. addrn, {Represents the @ operator}
  54. ordconstn, {Represents an ordinal value}
  55. typeconvn, {Represents type-conversion/typecast}
  56. calln, {Represents a call node}
  57. callparan, {Represents a parameter}
  58. realconstn, {Represents a real value}
  59. unaryminusn, {Represents a sign change (i.e. -2)}
  60. asmn, {Represents an assembler node }
  61. vecn, {Represents array indexing}
  62. pointerconstn, {Represents a pointer constant}
  63. stringconstn, {Represents a string constant}
  64. notn, {Represents the not operator}
  65. inlinen, {Internal procedures (i.e. writeln)}
  66. niln, {Represents the nil pointer}
  67. errorn, {This part of the tree could not be
  68. parsed because of a compiler error}
  69. typen, {A type name. Used for i.e. typeof(obj)}
  70. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  71. setconstn, {A set constant (i.e. [1,2])}
  72. blockn, {A block of statements}
  73. statementn, {One statement in a block of nodes}
  74. ifn, {An if statement}
  75. breakn, {A break statement}
  76. continuen, {A continue statement}
  77. whilerepeatn, {A while or repeat statement}
  78. forn, {A for loop}
  79. exitn, {An exit statement}
  80. withn, {A with statement}
  81. casen, {A case statement}
  82. labeln, {A label}
  83. goton, {A goto statement}
  84. tryexceptn, {A try except block}
  85. raisen, {A raise statement}
  86. tryfinallyn, {A try finally statement}
  87. onn, {For an on statement in exception code}
  88. isn, {Represents the is operator}
  89. asn, {Represents the as typecast}
  90. caretn, {Represents the ^ operator}
  91. starstarn, {Represents the ** operator exponentiation }
  92. arrayconstructorn, {Construction node for [...] parsing}
  93. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  94. tempcreaten, { for temps in the result/firstpass }
  95. temprefn, { references to temps }
  96. tempdeleten, { for temps in the result/firstpass }
  97. addoptn, { added for optimizations where we cannot suppress }
  98. nothingn, {NOP, Do nothing}
  99. loadvmtaddrn, {Load the address of the VMT of a class/object}
  100. guidconstn, {A GUID COM Interface constant }
  101. rttin, {Rtti information so they can be accessed in result/firstpass}
  102. loadparentfpn { Load the framepointer of the parent for nested procedures }
  103. );
  104. const
  105. nodetype2str : array[tnodetype] of string[24] = (
  106. '<emptynode>',
  107. 'addn',
  108. 'muln',
  109. 'subn',
  110. 'divn',
  111. 'symdifn',
  112. 'modn',
  113. 'assignn',
  114. 'loadn',
  115. 'rangen',
  116. 'ltn',
  117. 'lten',
  118. 'gtn',
  119. 'gten',
  120. 'equaln',
  121. 'unequaln',
  122. 'inn',
  123. 'orn',
  124. 'xorn',
  125. 'shrn',
  126. 'shln',
  127. 'slashn',
  128. 'andn',
  129. 'subscriptn',
  130. 'derefn',
  131. 'addrn',
  132. 'ordconstn',
  133. 'typeconvn',
  134. 'calln',
  135. 'callparan',
  136. 'realconstn',
  137. 'unaryminusn',
  138. 'asmn',
  139. 'vecn',
  140. 'pointerconstn',
  141. 'stringconstn',
  142. 'notn',
  143. 'inlinen',
  144. 'niln',
  145. 'errorn',
  146. 'typen',
  147. 'setelementn',
  148. 'setconstn',
  149. 'blockn',
  150. 'statementn',
  151. 'ifn',
  152. 'breakn',
  153. 'continuen',
  154. 'whilerepeatn',
  155. 'forn',
  156. 'exitn',
  157. 'withn',
  158. 'casen',
  159. 'labeln',
  160. 'goton',
  161. 'tryexceptn',
  162. 'raisen',
  163. 'tryfinallyn',
  164. 'onn',
  165. 'isn',
  166. 'asn',
  167. 'caretn',
  168. 'starstarn',
  169. 'arrayconstructn',
  170. 'arrayconstructrangen',
  171. 'tempcreaten',
  172. 'temprefn',
  173. 'tempdeleten',
  174. 'addoptn',
  175. 'nothingn',
  176. 'loadvmtaddrn',
  177. 'guidconstn',
  178. 'rttin',
  179. 'loadparentfpn');
  180. type
  181. { all boolean field of ttree are now collected in flags }
  182. tnodeflag = (
  183. nf_swapable, { tbinop operands can be swaped }
  184. nf_swaped, { tbinop operands are swaped }
  185. nf_error,
  186. { general }
  187. nf_pass1_done,
  188. nf_write, { Node is written to }
  189. nf_isproperty,
  190. { taddrnode }
  191. nf_typedaddr,
  192. { tderefnode }
  193. nf_no_checkpointer,
  194. { tvecnode }
  195. nf_memindex,
  196. nf_memseg,
  197. nf_callunique,
  198. { tloadnode }
  199. nf_absolute,
  200. nf_is_self,
  201. nf_load_self_pointer,
  202. nf_inherited,
  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. nf_cvarargs,
  213. { ttypeconvnode, and the first one also treal/ord/pointerconstn }
  214. nf_explicit,
  215. nf_internal, { no warnings/hints generated }
  216. nf_load_procvar,
  217. { tinlinenode }
  218. nf_inlineconst,
  219. { tasmnode }
  220. nf_get_asm_position,
  221. { tblocknode }
  222. nf_block_with_exit
  223. );
  224. tnodeflags = set of tnodeflag;
  225. const
  226. { contains the flags which must be equal for the equality }
  227. { of nodes }
  228. flagsequal : tnodeflags = [nf_error];
  229. type
  230. tnodelist = class
  231. end;
  232. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  233. tnode = class
  234. public
  235. { type of this node }
  236. nodetype : tnodetype;
  237. { type of the current code block, general/const/type }
  238. blocktype : tblock_type;
  239. { expected location of the result of this node (pass1) }
  240. expectloc : tcgloc;
  241. { the location of the result of this node (pass2) }
  242. location : tlocation;
  243. { the parent node of this is node }
  244. { this field is set by concattolist }
  245. parent : tnode;
  246. { there are some properties about the node stored }
  247. flags : tnodeflags;
  248. ppuidx : longint;
  249. { the number of registers needed to evalute the node }
  250. registersint,registersfpu,registersmm : longint; { must be longint !!!! }
  251. {$ifdef SUPPORT_MMX}
  252. registersmmx : longint;
  253. {$endif SUPPORT_MMX}
  254. resulttype : ttype;
  255. fileinfo : tfileposinfo;
  256. localswitches : tlocalswitches;
  257. {$ifdef extdebug}
  258. maxfirstpasscount,
  259. firstpasscount : longint;
  260. {$endif extdebug}
  261. constructor create(t:tnodetype);
  262. { this constructor is only for creating copies of class }
  263. { the fields are copied by getcopy }
  264. constructor createforcopy;
  265. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  266. destructor destroy;override;
  267. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  268. procedure buildderefimpl;virtual;
  269. procedure derefimpl;virtual;
  270. procedure derefnode;virtual;
  271. { toggles the flag }
  272. procedure toggleflag(f : tnodeflag);
  273. { the 1.1 code generator may override pass_1 }
  274. { and it need not to implement det_* then }
  275. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  276. { 2.0: runs det_resulttype and det_temp }
  277. function pass_1 : tnode;virtual;abstract;
  278. { dermines the resulttype of the node }
  279. function det_resulttype : tnode;virtual;abstract;
  280. { tries to simplify the node, returns a value <>nil if a simplified
  281. node has been created }
  282. function simplify : tnode;virtual;
  283. {$ifdef state_tracking}
  284. { Does optimizations by keeping track of the variable states
  285. in a procedure }
  286. function track_state_pass(exec_known:boolean):boolean;virtual;
  287. {$endif}
  288. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  289. written to (normally the loadnode) as write access. }
  290. procedure mark_write;virtual;
  291. { dermines the number of necessary temp. locations to evaluate
  292. the node }
  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. { wrapper for getcopy }
  300. function getcopy : tnode;
  301. { does the real copying of a node }
  302. function _getcopy : tnode;virtual;
  303. procedure insertintolist(l : tnodelist);virtual;
  304. { writes a node for debugging purpose, shouldn't be called }
  305. { direct, because there is no test for nil, use printnode }
  306. { to write a complete tree }
  307. procedure printnodeinfo(var t:text);virtual;
  308. procedure printnodedata(var t:text);virtual;
  309. procedure printnodetree(var t:text);virtual;
  310. procedure concattolist(l : tlinkedlist);virtual;
  311. function ischild(p : tnode) : boolean;virtual;
  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 current_procinfo.CurrFalseLabel }
  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. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  369. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  370. const
  371. printnodespacing = ' ';
  372. var
  373. { indention used when writing the tree to the screen }
  374. printnodeindention : string;
  375. procedure printnodeindent;
  376. procedure printnodeunindent;
  377. procedure printnode(var t:text;n:tnode);
  378. function is_constnode(p : tnode) : boolean;
  379. function is_constintnode(p : tnode) : boolean;
  380. function is_constcharnode(p : tnode) : boolean;
  381. function is_constrealnode(p : tnode) : boolean;
  382. function is_constboolnode(p : tnode) : boolean;
  383. function is_constenumnode(p : tnode) : boolean;
  384. function is_constwidecharnode(p : tnode) : boolean;
  385. implementation
  386. uses
  387. cutils,verbose,ppu,
  388. symconst,
  389. defutil;
  390. const
  391. ppunodemarker = 255;
  392. {****************************************************************************
  393. Helpers
  394. ****************************************************************************}
  395. var
  396. nodeppudata : tdynamicarray;
  397. nodeppuidx : longint;
  398. procedure nodeppuidxcreate;
  399. begin
  400. nodeppudata:=tdynamicarray.create(1024);
  401. nodeppuidx:=0;
  402. end;
  403. procedure nodeppuidxfree;
  404. begin
  405. nodeppudata.free;
  406. nodeppudata:=nil;
  407. end;
  408. procedure nodeppuidxadd(n:tnode);
  409. begin
  410. if n.ppuidx<0 then
  411. internalerror(200311072);
  412. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  413. nodeppudata.write(n,sizeof(pointer));
  414. end;
  415. function nodeppuidxget(i:longint):tnode;
  416. begin
  417. if i<0 then
  418. internalerror(200311072);
  419. nodeppudata.seek(i*sizeof(pointer));
  420. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  421. internalerror(200311073);
  422. end;
  423. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  424. var
  425. b : byte;
  426. t : tnodetype;
  427. hppuidx : longint;
  428. begin
  429. { marker }
  430. b:=ppufile.getbyte;
  431. if b<>ppunodemarker then
  432. internalerror(200208151);
  433. { load nodetype }
  434. t:=tnodetype(ppufile.getbyte);
  435. if t>high(tnodetype) then
  436. internalerror(200208152);
  437. if t<>emptynode then
  438. begin
  439. if not assigned(nodeclass[t]) then
  440. internalerror(200208153);
  441. hppuidx:=ppufile.getlongint;
  442. //writeln('load: ',nodetype2str[t]);
  443. { generate node of the correct class }
  444. result:=nodeclass[t].ppuload(t,ppufile);
  445. result.ppuidx:=hppuidx;
  446. nodeppuidxadd(result);
  447. end
  448. else
  449. result:=nil;
  450. end;
  451. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  452. begin
  453. { marker, read by ppuloadnode }
  454. ppufile.putbyte(ppunodemarker);
  455. { type, read by ppuloadnode }
  456. if assigned(n) then
  457. begin
  458. if n.ppuidx=-1 then
  459. internalerror(200311071);
  460. n.ppuidx:=nodeppuidx;
  461. inc(nodeppuidx);
  462. ppufile.putbyte(byte(n.nodetype));
  463. ppufile.putlongint(n.ppuidx);
  464. //writeln('write: ',nodetype2str[n.nodetype]);
  465. n.ppuwrite(ppufile);
  466. end
  467. else
  468. ppufile.putbyte(byte(emptynode));
  469. end;
  470. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  471. begin
  472. { writing of node references isn't implemented yet (FK) }
  473. internalerror(200506181);
  474. end;
  475. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  476. begin
  477. { reading of node references isn't implemented yet (FK) }
  478. internalerror(200506182);
  479. end;
  480. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  481. begin
  482. if ppufile.readentry<>ibnodetree then
  483. Message(unit_f_ppu_read_error);
  484. nodeppuidxcreate;
  485. result:=ppuloadnode(ppufile);
  486. result.derefnode;
  487. nodeppuidxfree;
  488. end;
  489. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  490. begin
  491. nodeppuidx:=0;
  492. ppuwritenode(ppufile,n);
  493. ppufile.writeentry(ibnodetree);
  494. end;
  495. procedure printnodeindent;
  496. begin
  497. printnodeindention:=printnodeindention+printnodespacing;
  498. end;
  499. procedure printnodeunindent;
  500. begin
  501. delete(printnodeindention,1,length(printnodespacing));
  502. end;
  503. procedure printnode(var t:text;n:tnode);
  504. begin
  505. if assigned(n) then
  506. n.printnodetree(t)
  507. else
  508. writeln(t,printnodeindention,'nil');
  509. end;
  510. function is_constnode(p : tnode) : boolean;
  511. begin
  512. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  513. end;
  514. function is_constintnode(p : tnode) : boolean;
  515. begin
  516. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resulttype.def);
  517. end;
  518. function is_constcharnode(p : tnode) : boolean;
  519. begin
  520. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resulttype.def);
  521. end;
  522. function is_constwidecharnode(p : tnode) : boolean;
  523. begin
  524. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resulttype.def);
  525. end;
  526. function is_constrealnode(p : tnode) : boolean;
  527. begin
  528. is_constrealnode:=(p.nodetype=realconstn);
  529. end;
  530. function is_constboolnode(p : tnode) : boolean;
  531. begin
  532. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resulttype.def);
  533. end;
  534. function is_constenumnode(p : tnode) : boolean;
  535. begin
  536. is_constenumnode:=(p.nodetype=ordconstn) and (p.resulttype.def.deftype=enumdef);
  537. end;
  538. {****************************************************************************
  539. TNODE
  540. ****************************************************************************}
  541. constructor tnode.create(t:tnodetype);
  542. begin
  543. inherited create;
  544. nodetype:=t;
  545. blocktype:=block_type;
  546. { updated by firstpass }
  547. expectloc:=LOC_INVALID;
  548. { updated by secondpass }
  549. location.loc:=LOC_INVALID;
  550. { save local info }
  551. fileinfo:=aktfilepos;
  552. localswitches:=aktlocalswitches;
  553. resulttype.reset;
  554. registersint:=0;
  555. registersfpu:=0;
  556. {$ifdef SUPPORT_MMX}
  557. registersmmx:=0;
  558. {$endif SUPPORT_MMX}
  559. {$ifdef EXTDEBUG}
  560. maxfirstpasscount:=0;
  561. firstpasscount:=0;
  562. {$endif EXTDEBUG}
  563. flags:=[];
  564. ppuidx:=-1;
  565. end;
  566. constructor tnode.createforcopy;
  567. begin
  568. end;
  569. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  570. begin
  571. nodetype:=t;
  572. { tnode fields }
  573. blocktype:=tblock_type(ppufile.getbyte);
  574. ppufile.getposinfo(fileinfo);
  575. ppufile.getsmallset(localswitches);
  576. ppufile.gettype(resulttype);
  577. ppufile.getsmallset(flags);
  578. { updated by firstpass }
  579. expectloc:=LOC_INVALID;
  580. { updated by secondpass }
  581. location.loc:=LOC_INVALID;
  582. registersint:=0;
  583. registersfpu:=0;
  584. {$ifdef SUPPORT_MMX}
  585. registersmmx:=0;
  586. {$endif SUPPORT_MMX}
  587. {$ifdef EXTDEBUG}
  588. maxfirstpasscount:=0;
  589. firstpasscount:=0;
  590. {$endif EXTDEBUG}
  591. ppuidx:=-1;
  592. end;
  593. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  594. begin
  595. ppufile.putbyte(byte(block_type));
  596. ppufile.putposinfo(fileinfo);
  597. ppufile.putsmallset(localswitches);
  598. ppufile.puttype(resulttype);
  599. ppufile.putsmallset(flags);
  600. end;
  601. procedure tnode.buildderefimpl;
  602. begin
  603. resulttype.buildderef;
  604. end;
  605. procedure tnode.derefimpl;
  606. begin
  607. resulttype.resolve;
  608. end;
  609. procedure tnode.derefnode;
  610. begin
  611. end;
  612. procedure tnode.toggleflag(f : tnodeflag);
  613. begin
  614. if f in flags then
  615. exclude(flags,f)
  616. else
  617. include(flags,f);
  618. end;
  619. function tnode.simplify : tnode;
  620. begin
  621. result:=nil;
  622. end;
  623. destructor tnode.destroy;
  624. begin
  625. {$ifdef EXTDEBUG}
  626. if firstpasscount>maxfirstpasscount then
  627. maxfirstpasscount:=firstpasscount;
  628. {$endif EXTDEBUG}
  629. end;
  630. procedure tnode.concattolist(l : tlinkedlist);
  631. begin
  632. end;
  633. function tnode.ischild(p : tnode) : boolean;
  634. begin
  635. ischild:=false;
  636. end;
  637. procedure tnode.mark_write;
  638. begin
  639. {$ifdef EXTDEBUG}
  640. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  641. {$endif EXTDEBUG}
  642. end;
  643. procedure tnode.printnodeinfo(var t:text);
  644. begin
  645. write(t,nodetype2str[nodetype]);
  646. if assigned(resulttype.def) then
  647. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  648. else
  649. write(t,', resulttype = <nil>');
  650. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  651. ', loc = ',tcgloc2str[location.loc],
  652. ', expectloc = ',tcgloc2str[expectloc],
  653. ', intregs = ',registersint,
  654. ', fpuregs = ',registersfpu);
  655. end;
  656. procedure tnode.printnodedata(var t:text);
  657. begin
  658. end;
  659. procedure tnode.printnodetree(var t:text);
  660. begin
  661. write(t,printnodeindention,'(');
  662. printnodeinfo(t);
  663. writeln(t);
  664. printnodeindent;
  665. printnodedata(t);
  666. printnodeunindent;
  667. writeln(t,printnodeindention,')');
  668. end;
  669. function tnode.isequal(p : tnode) : boolean;
  670. begin
  671. isequal:=
  672. (not assigned(self) and not assigned(p)) or
  673. (assigned(self) and assigned(p) and
  674. { optimized subclasses have the same nodetype as their }
  675. { superclass (for compatibility), so also check the classtype (JM) }
  676. (p.classtype=classtype) and
  677. (p.nodetype=nodetype) and
  678. (flags*flagsequal=p.flags*flagsequal) and
  679. docompare(p));
  680. end;
  681. {$ifdef state_tracking}
  682. function Tnode.track_state_pass(exec_known:boolean):boolean;
  683. begin
  684. track_state_pass:=false;
  685. end;
  686. {$endif state_tracking}
  687. function tnode.docompare(p : tnode) : boolean;
  688. begin
  689. docompare:=true;
  690. end;
  691. function tnode.getcopy : tnode;
  692. begin
  693. result:=_getcopy;
  694. end;
  695. function tnode._getcopy : tnode;
  696. var
  697. p : tnode;
  698. begin
  699. { this is quite tricky because we need a node of the current }
  700. { node type and not one of tnode! }
  701. p:=tnodeclass(classtype).createforcopy;
  702. p.nodetype:=nodetype;
  703. p.expectloc:=expectloc;
  704. p.location:=location;
  705. p.parent:=parent;
  706. p.flags:=flags;
  707. p.registersint:=registersint;
  708. p.registersfpu:=registersfpu;
  709. {$ifdef SUPPORT_MMX}
  710. p.registersmmx:=registersmmx;
  711. p.registersmm:=registersmm;
  712. {$endif SUPPORT_MMX}
  713. p.resulttype:=resulttype;
  714. p.fileinfo:=fileinfo;
  715. p.localswitches:=localswitches;
  716. {$ifdef extdebug}
  717. p.firstpasscount:=firstpasscount;
  718. {$endif extdebug}
  719. { p.list:=list; }
  720. result:=p;
  721. end;
  722. procedure tnode.insertintolist(l : tnodelist);
  723. begin
  724. end;
  725. {****************************************************************************
  726. TUNARYNODE
  727. ****************************************************************************}
  728. constructor tunarynode.create(t:tnodetype;l : tnode);
  729. begin
  730. inherited create(t);
  731. left:=l;
  732. end;
  733. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  734. begin
  735. inherited ppuload(t,ppufile);
  736. left:=ppuloadnode(ppufile);
  737. end;
  738. destructor tunarynode.destroy;
  739. begin
  740. left.free;
  741. inherited destroy;
  742. end;
  743. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  744. begin
  745. inherited ppuwrite(ppufile);
  746. ppuwritenode(ppufile,left);
  747. end;
  748. procedure tunarynode.buildderefimpl;
  749. begin
  750. inherited buildderefimpl;
  751. if assigned(left) then
  752. left.buildderefimpl;
  753. end;
  754. procedure tunarynode.derefimpl;
  755. begin
  756. inherited derefimpl;
  757. if assigned(left) then
  758. left.derefimpl;
  759. end;
  760. procedure tunarynode.derefnode;
  761. begin
  762. inherited derefnode;
  763. if assigned(left) then
  764. left.derefnode;
  765. end;
  766. function tunarynode.docompare(p : tnode) : boolean;
  767. begin
  768. docompare:=(inherited docompare(p) and
  769. ((left=nil) or left.isequal(tunarynode(p).left))
  770. );
  771. end;
  772. function tunarynode._getcopy : tnode;
  773. var
  774. p : tunarynode;
  775. begin
  776. p:=tunarynode(inherited _getcopy);
  777. if assigned(left) then
  778. p.left:=left._getcopy
  779. else
  780. p.left:=nil;
  781. result:=p;
  782. end;
  783. procedure tunarynode.insertintolist(l : tnodelist);
  784. begin
  785. end;
  786. procedure tunarynode.printnodedata(var t:text);
  787. begin
  788. inherited printnodedata(t);
  789. printnode(t,left);
  790. end;
  791. procedure tunarynode.left_max;
  792. begin
  793. registersint:=left.registersint;
  794. registersfpu:=left.registersfpu;
  795. {$ifdef SUPPORT_MMX}
  796. registersmmx:=left.registersmmx;
  797. {$endif SUPPORT_MMX}
  798. end;
  799. procedure tunarynode.concattolist(l : tlinkedlist);
  800. begin
  801. left.parent:=self;
  802. left.concattolist(l);
  803. inherited concattolist(l);
  804. end;
  805. function tunarynode.ischild(p : tnode) : boolean;
  806. begin
  807. ischild:=p=left;
  808. end;
  809. {****************************************************************************
  810. TBINARYNODE
  811. ****************************************************************************}
  812. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  813. begin
  814. inherited create(t,l);
  815. right:=r
  816. end;
  817. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  818. begin
  819. inherited ppuload(t,ppufile);
  820. right:=ppuloadnode(ppufile);
  821. end;
  822. destructor tbinarynode.destroy;
  823. begin
  824. right.free;
  825. inherited destroy;
  826. end;
  827. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  828. begin
  829. inherited ppuwrite(ppufile);
  830. ppuwritenode(ppufile,right);
  831. end;
  832. procedure tbinarynode.buildderefimpl;
  833. begin
  834. inherited buildderefimpl;
  835. if assigned(right) then
  836. right.buildderefimpl;
  837. end;
  838. procedure tbinarynode.derefimpl;
  839. begin
  840. inherited derefimpl;
  841. if assigned(right) then
  842. right.derefimpl;
  843. end;
  844. procedure tbinarynode.derefnode;
  845. begin
  846. inherited derefnode;
  847. if assigned(right) then
  848. right.derefnode;
  849. end;
  850. procedure tbinarynode.concattolist(l : tlinkedlist);
  851. begin
  852. { we could change that depending on the number of }
  853. { required registers }
  854. left.parent:=self;
  855. left.concattolist(l);
  856. left.parent:=self;
  857. left.concattolist(l);
  858. inherited concattolist(l);
  859. end;
  860. function tbinarynode.ischild(p : tnode) : boolean;
  861. begin
  862. ischild:=(p=right);
  863. end;
  864. function tbinarynode.docompare(p : tnode) : boolean;
  865. begin
  866. docompare:=(inherited docompare(p) and
  867. ((right=nil) or right.isequal(tbinarynode(p).right))
  868. );
  869. end;
  870. function tbinarynode._getcopy : tnode;
  871. var
  872. p : tbinarynode;
  873. begin
  874. p:=tbinarynode(inherited _getcopy);
  875. if assigned(right) then
  876. p.right:=right._getcopy
  877. else
  878. p.right:=nil;
  879. result:=p;
  880. end;
  881. procedure tbinarynode.insertintolist(l : tnodelist);
  882. begin
  883. end;
  884. procedure tbinarynode.swapleftright;
  885. var
  886. swapp : tnode;
  887. begin
  888. swapp:=right;
  889. right:=left;
  890. left:=swapp;
  891. if nf_swaped in flags then
  892. exclude(flags,nf_swaped)
  893. else
  894. include(flags,nf_swaped);
  895. end;
  896. procedure tbinarynode.left_right_max;
  897. begin
  898. if assigned(left) then
  899. begin
  900. if assigned(right) then
  901. begin
  902. registersint:=max(left.registersint,right.registersint);
  903. registersfpu:=max(left.registersfpu,right.registersfpu);
  904. {$ifdef SUPPORT_MMX}
  905. registersmmx:=max(left.registersmmx,right.registersmmx);
  906. {$endif SUPPORT_MMX}
  907. end
  908. else
  909. begin
  910. registersint:=left.registersint;
  911. registersfpu:=left.registersfpu;
  912. {$ifdef SUPPORT_MMX}
  913. registersmmx:=left.registersmmx;
  914. {$endif SUPPORT_MMX}
  915. end;
  916. end;
  917. end;
  918. procedure tbinarynode.printnodedata(var t:text);
  919. begin
  920. inherited printnodedata(t);
  921. printnode(t,right);
  922. end;
  923. procedure tbinarynode.printnodelist(var t:text);
  924. var
  925. hp : tbinarynode;
  926. begin
  927. hp:=self;
  928. while assigned(hp) do
  929. begin
  930. write(t,printnodeindention,'(');
  931. printnodeindent;
  932. hp.printnodeinfo(t);
  933. writeln(t);
  934. printnode(t,hp.left);
  935. writeln(t);
  936. printnodeunindent;
  937. writeln(t,printnodeindention,')');
  938. hp:=tbinarynode(hp.right);
  939. end;
  940. end;
  941. {****************************************************************************
  942. TBINOPYNODE
  943. ****************************************************************************}
  944. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  945. begin
  946. inherited create(t,l,r);
  947. end;
  948. function tbinopnode.docompare(p : tnode) : boolean;
  949. begin
  950. docompare:=(inherited docompare(p)) or
  951. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  952. ((nf_swapable in flags) and
  953. left.isequal(tbinopnode(p).right) and
  954. right.isequal(tbinopnode(p).left));
  955. end;
  956. end.