node.pas 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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. tnodetypeset = set of tnodetype;
  105. pnodetypeset = ^tnodetypeset;
  106. const
  107. nodetype2str : array[tnodetype] of string[24] = (
  108. '<emptynode>',
  109. 'addn',
  110. 'muln',
  111. 'subn',
  112. 'divn',
  113. 'symdifn',
  114. 'modn',
  115. 'assignn',
  116. 'loadn',
  117. 'rangen',
  118. 'ltn',
  119. 'lten',
  120. 'gtn',
  121. 'gten',
  122. 'equaln',
  123. 'unequaln',
  124. 'inn',
  125. 'orn',
  126. 'xorn',
  127. 'shrn',
  128. 'shln',
  129. 'slashn',
  130. 'andn',
  131. 'subscriptn',
  132. 'derefn',
  133. 'addrn',
  134. 'ordconstn',
  135. 'typeconvn',
  136. 'calln',
  137. 'callparan',
  138. 'realconstn',
  139. 'unaryminusn',
  140. 'asmn',
  141. 'vecn',
  142. 'pointerconstn',
  143. 'stringconstn',
  144. 'notn',
  145. 'inlinen',
  146. 'niln',
  147. 'errorn',
  148. 'typen',
  149. 'setelementn',
  150. 'setconstn',
  151. 'blockn',
  152. 'statementn',
  153. 'ifn',
  154. 'breakn',
  155. 'continuen',
  156. 'whilerepeatn',
  157. 'forn',
  158. 'exitn',
  159. 'withn',
  160. 'casen',
  161. 'labeln',
  162. 'goton',
  163. 'tryexceptn',
  164. 'raisen',
  165. 'tryfinallyn',
  166. 'onn',
  167. 'isn',
  168. 'asn',
  169. 'caretn',
  170. 'starstarn',
  171. 'arrayconstructn',
  172. 'arrayconstructrangen',
  173. 'tempcreaten',
  174. 'temprefn',
  175. 'tempdeleten',
  176. 'addoptn',
  177. 'nothingn',
  178. 'loadvmtaddrn',
  179. 'guidconstn',
  180. 'rttin',
  181. 'loadparentfpn');
  182. type
  183. { all boolean field of ttree are now collected in flags }
  184. tnodeflag = (
  185. nf_swapable, { tbinop operands can be swaped }
  186. nf_swaped, { tbinop operands are swaped }
  187. nf_error,
  188. { general }
  189. nf_pass1_done,
  190. nf_write, { Node is written to }
  191. nf_isproperty,
  192. { taddrnode }
  193. nf_typedaddr,
  194. { tderefnode }
  195. nf_no_checkpointer,
  196. { tvecnode }
  197. nf_memindex,
  198. nf_memseg,
  199. nf_callunique,
  200. { tloadnode }
  201. nf_absolute,
  202. nf_is_self,
  203. nf_load_self_pointer,
  204. nf_inherited,
  205. { the loadnode is generated internally and a varspez=vs_const should be ignore,
  206. this requires that the parameter is actually passed by value
  207. Be really carefull when using this flag! }
  208. nf_isinternal_ignoreconst,
  209. { taddnode }
  210. nf_is_currency,
  211. nf_has_pointerdiv,
  212. nf_short_bool,
  213. { tassignmentnode }
  214. nf_assign_done_in_right,
  215. { tarrayconstructnode }
  216. nf_forcevaria,
  217. nf_novariaallowed,
  218. { ttypeconvnode, and the first one also treal/ord/pointerconstn }
  219. nf_explicit,
  220. nf_internal, { no warnings/hints generated }
  221. nf_load_procvar,
  222. { tinlinenode }
  223. nf_inlineconst,
  224. { tasmnode }
  225. nf_get_asm_position,
  226. { tblocknode }
  227. nf_block_with_exit
  228. );
  229. tnodeflags = set of tnodeflag;
  230. const
  231. { contains the flags which must be equal for the equality }
  232. { of nodes }
  233. flagsequal : tnodeflags = [nf_error];
  234. type
  235. tnodelist = class
  236. end;
  237. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  238. tnode = class
  239. public
  240. { type of this node }
  241. nodetype : tnodetype;
  242. { type of the current code block, general/const/type }
  243. blocktype : tblock_type;
  244. { expected location of the result of this node (pass1) }
  245. expectloc : tcgloc;
  246. { the location of the result of this node (pass2) }
  247. location : tlocation;
  248. { the parent node of this is node }
  249. { this field is set by concattolist }
  250. parent : tnode;
  251. { there are some properties about the node stored }
  252. flags : tnodeflags;
  253. ppuidx : longint;
  254. { the number of registers needed to evalute the node }
  255. registersint,registersfpu,registersmm : longint; { must be longint !!!! }
  256. {$ifdef SUPPORT_MMX}
  257. registersmmx : longint;
  258. {$endif SUPPORT_MMX}
  259. resulttype : ttype;
  260. fileinfo : tfileposinfo;
  261. localswitches : tlocalswitches;
  262. {$ifdef extdebug}
  263. maxfirstpasscount,
  264. firstpasscount : longint;
  265. {$endif extdebug}
  266. constructor create(t:tnodetype);
  267. { this constructor is only for creating copies of class }
  268. { the fields are copied by getcopy }
  269. constructor createforcopy;
  270. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  271. destructor destroy;override;
  272. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  273. procedure buildderefimpl;virtual;
  274. procedure derefimpl;virtual;
  275. procedure derefnode;virtual;
  276. { toggles the flag }
  277. procedure toggleflag(f : tnodeflag);
  278. { the 1.1 code generator may override pass_1 }
  279. { and it need not to implement det_* then }
  280. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  281. { 2.0: runs det_resulttype and det_temp }
  282. function pass_1 : tnode;virtual;abstract;
  283. { dermines the resulttype of the node }
  284. function det_resulttype : tnode;virtual;abstract;
  285. { tries to simplify the node, returns a value <>nil if a simplified
  286. node has been created }
  287. function simplify : tnode;virtual;
  288. {$ifdef state_tracking}
  289. { Does optimizations by keeping track of the variable states
  290. in a procedure }
  291. function track_state_pass(exec_known:boolean):boolean;virtual;
  292. {$endif}
  293. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  294. written to (normally the loadnode) as write access. }
  295. procedure mark_write;virtual;
  296. { dermines the number of necessary temp. locations to evaluate
  297. the node }
  298. procedure det_temp;virtual;abstract;
  299. procedure pass_2;virtual;abstract;
  300. { comparing of nodes }
  301. function isequal(p : tnode) : boolean;
  302. { to implement comparisation, override this method }
  303. function docompare(p : tnode) : boolean;virtual;
  304. { wrapper for getcopy }
  305. function getcopy : tnode;
  306. { does the real copying of a node }
  307. function _getcopy : tnode;virtual;
  308. procedure insertintolist(l : tnodelist);virtual;
  309. { writes a node for debugging purpose, shouldn't be called }
  310. { direct, because there is no test for nil, use printnode }
  311. { to write a complete tree }
  312. procedure printnodeinfo(var t:text);virtual;
  313. procedure printnodedata(var t:text);virtual;
  314. procedure printnodetree(var t:text);virtual;
  315. procedure concattolist(l : tlinkedlist);virtual;
  316. function ischild(p : tnode) : boolean;virtual;
  317. end;
  318. tnodeclass = class of tnode;
  319. tnodeclassarray = array[tnodetype] of tnodeclass;
  320. { this node is the anchestor for all nodes with at least }
  321. { one child, you have to use it if you want to use }
  322. { true- and current_procinfo.CurrFalseLabel }
  323. punarynode = ^tunarynode;
  324. tunarynode = class(tnode)
  325. left : tnode;
  326. constructor create(t:tnodetype;l : tnode);
  327. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  328. destructor destroy;override;
  329. procedure ppuwrite(ppufile:tcompilerppufile);override;
  330. procedure buildderefimpl;override;
  331. procedure derefimpl;override;
  332. procedure derefnode;override;
  333. procedure concattolist(l : tlinkedlist);override;
  334. function ischild(p : tnode) : boolean;override;
  335. function docompare(p : tnode) : boolean;override;
  336. function _getcopy : tnode;override;
  337. procedure insertintolist(l : tnodelist);override;
  338. procedure left_max;
  339. procedure printnodedata(var t:text);override;
  340. end;
  341. pbinarynode = ^tbinarynode;
  342. tbinarynode = class(tunarynode)
  343. right : tnode;
  344. constructor create(t:tnodetype;l,r : tnode);
  345. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  346. destructor destroy;override;
  347. procedure ppuwrite(ppufile:tcompilerppufile);override;
  348. procedure buildderefimpl;override;
  349. procedure derefimpl;override;
  350. procedure derefnode;override;
  351. procedure concattolist(l : tlinkedlist);override;
  352. function ischild(p : tnode) : boolean;override;
  353. function docompare(p : tnode) : boolean;override;
  354. procedure swapleftright;
  355. function _getcopy : tnode;override;
  356. procedure insertintolist(l : tnodelist);override;
  357. procedure left_right_max;
  358. procedure printnodedata(var t:text);override;
  359. procedure printnodelist(var t:text);
  360. end;
  361. tbinopnode = class(tbinarynode)
  362. constructor create(t:tnodetype;l,r : tnode);virtual;
  363. function docompare(p : tnode) : boolean;override;
  364. end;
  365. var
  366. { array with all class types for tnodes }
  367. nodeclass : tnodeclassarray;
  368. function nodeppuidxget(i:longint):tnode;
  369. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  370. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  371. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  372. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  373. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  374. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  375. const
  376. printnodespacing = ' ';
  377. var
  378. { indention used when writing the tree to the screen }
  379. printnodeindention : string;
  380. procedure printnodeindent;
  381. procedure printnodeunindent;
  382. procedure printnode(var t:text;n:tnode);
  383. function is_constnode(p : tnode) : boolean;
  384. function is_constintnode(p : tnode) : boolean;
  385. function is_constcharnode(p : tnode) : boolean;
  386. function is_constrealnode(p : tnode) : boolean;
  387. function is_constboolnode(p : tnode) : boolean;
  388. function is_constenumnode(p : tnode) : boolean;
  389. function is_constwidecharnode(p : tnode) : boolean;
  390. implementation
  391. uses
  392. cutils,verbose,ppu,
  393. symconst,
  394. nutils,nflw,
  395. defutil;
  396. const
  397. ppunodemarker = 255;
  398. {****************************************************************************
  399. Helpers
  400. ****************************************************************************}
  401. var
  402. nodeppudata : tdynamicarray;
  403. nodeppuidx : longint;
  404. procedure nodeppuidxcreate;
  405. begin
  406. nodeppudata:=tdynamicarray.create(1024);
  407. nodeppuidx:=0;
  408. end;
  409. procedure nodeppuidxfree;
  410. begin
  411. nodeppudata.free;
  412. nodeppudata:=nil;
  413. end;
  414. procedure nodeppuidxadd(n:tnode);
  415. begin
  416. if n.ppuidx<0 then
  417. internalerror(200311072);
  418. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  419. nodeppudata.write(n,sizeof(pointer));
  420. end;
  421. function nodeppuidxget(i:longint):tnode;
  422. begin
  423. if i<0 then
  424. internalerror(200311072);
  425. nodeppudata.seek(i*sizeof(pointer));
  426. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  427. internalerror(200311073);
  428. end;
  429. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  430. var
  431. b : byte;
  432. t : tnodetype;
  433. hppuidx : longint;
  434. begin
  435. { marker }
  436. b:=ppufile.getbyte;
  437. if b<>ppunodemarker then
  438. internalerror(200208151);
  439. { load nodetype }
  440. t:=tnodetype(ppufile.getbyte);
  441. if t>high(tnodetype) then
  442. internalerror(200208152);
  443. if t<>emptynode then
  444. begin
  445. if not assigned(nodeclass[t]) then
  446. internalerror(200208153);
  447. hppuidx:=ppufile.getlongint;
  448. //writeln('load: ',nodetype2str[t]);
  449. { generate node of the correct class }
  450. result:=nodeclass[t].ppuload(t,ppufile);
  451. result.ppuidx:=hppuidx;
  452. nodeppuidxadd(result);
  453. end
  454. else
  455. result:=nil;
  456. end;
  457. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  458. begin
  459. { marker, read by ppuloadnode }
  460. ppufile.putbyte(ppunodemarker);
  461. { type, read by ppuloadnode }
  462. if assigned(n) then
  463. begin
  464. if n.ppuidx=-1 then
  465. internalerror(200311071);
  466. n.ppuidx:=nodeppuidx;
  467. inc(nodeppuidx);
  468. ppufile.putbyte(byte(n.nodetype));
  469. ppufile.putlongint(n.ppuidx);
  470. //writeln('write: ',nodetype2str[n.nodetype]);
  471. n.ppuwrite(ppufile);
  472. end
  473. else
  474. ppufile.putbyte(byte(emptynode));
  475. end;
  476. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  477. begin
  478. { writing of node references isn't implemented yet (FK) }
  479. internalerror(200506181);
  480. end;
  481. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  482. begin
  483. { reading of node references isn't implemented yet (FK) }
  484. internalerror(200506182);
  485. { avoid warning }
  486. result := nil;
  487. end;
  488. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  489. begin
  490. if ppufile.readentry<>ibnodetree then
  491. Message(unit_f_ppu_read_error);
  492. nodeppuidxcreate;
  493. result:=ppuloadnode(ppufile);
  494. result.derefnode;
  495. nodeppuidxfree;
  496. end;
  497. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  498. begin
  499. nodeppuidx:=0;
  500. ppuwritenode(ppufile,n);
  501. ppufile.writeentry(ibnodetree);
  502. end;
  503. procedure printnodeindent;
  504. begin
  505. printnodeindention:=printnodeindention+printnodespacing;
  506. end;
  507. procedure printnodeunindent;
  508. begin
  509. delete(printnodeindention,1,length(printnodespacing));
  510. end;
  511. procedure printnode(var t:text;n:tnode);
  512. begin
  513. if assigned(n) then
  514. n.printnodetree(t)
  515. else
  516. writeln(t,printnodeindention,'nil');
  517. end;
  518. function is_constnode(p : tnode) : boolean;
  519. begin
  520. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  521. end;
  522. function is_constintnode(p : tnode) : boolean;
  523. begin
  524. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resulttype.def);
  525. end;
  526. function is_constcharnode(p : tnode) : boolean;
  527. begin
  528. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resulttype.def);
  529. end;
  530. function is_constwidecharnode(p : tnode) : boolean;
  531. begin
  532. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resulttype.def);
  533. end;
  534. function is_constrealnode(p : tnode) : boolean;
  535. begin
  536. is_constrealnode:=(p.nodetype=realconstn);
  537. end;
  538. function is_constboolnode(p : tnode) : boolean;
  539. begin
  540. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resulttype.def);
  541. end;
  542. function is_constenumnode(p : tnode) : boolean;
  543. begin
  544. is_constenumnode:=(p.nodetype=ordconstn) and (p.resulttype.def.deftype=enumdef);
  545. end;
  546. {****************************************************************************
  547. TNODE
  548. ****************************************************************************}
  549. constructor tnode.create(t:tnodetype);
  550. begin
  551. inherited create;
  552. nodetype:=t;
  553. blocktype:=block_type;
  554. { updated by firstpass }
  555. expectloc:=LOC_INVALID;
  556. { updated by secondpass }
  557. location.loc:=LOC_INVALID;
  558. { save local info }
  559. fileinfo:=aktfilepos;
  560. localswitches:=aktlocalswitches;
  561. resulttype.reset;
  562. registersint:=0;
  563. registersfpu:=0;
  564. {$ifdef SUPPORT_MMX}
  565. registersmmx:=0;
  566. {$endif SUPPORT_MMX}
  567. {$ifdef EXTDEBUG}
  568. maxfirstpasscount:=0;
  569. firstpasscount:=0;
  570. {$endif EXTDEBUG}
  571. flags:=[];
  572. ppuidx:=-1;
  573. end;
  574. constructor tnode.createforcopy;
  575. begin
  576. end;
  577. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  578. begin
  579. nodetype:=t;
  580. { tnode fields }
  581. blocktype:=tblock_type(ppufile.getbyte);
  582. ppufile.getposinfo(fileinfo);
  583. ppufile.getsmallset(localswitches);
  584. ppufile.gettype(resulttype);
  585. ppufile.getsmallset(flags);
  586. { updated by firstpass }
  587. expectloc:=LOC_INVALID;
  588. { updated by secondpass }
  589. location.loc:=LOC_INVALID;
  590. registersint:=0;
  591. registersfpu:=0;
  592. {$ifdef SUPPORT_MMX}
  593. registersmmx:=0;
  594. {$endif SUPPORT_MMX}
  595. {$ifdef EXTDEBUG}
  596. maxfirstpasscount:=0;
  597. firstpasscount:=0;
  598. {$endif EXTDEBUG}
  599. ppuidx:=-1;
  600. end;
  601. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  602. begin
  603. ppufile.putbyte(byte(block_type));
  604. ppufile.putposinfo(fileinfo);
  605. ppufile.putsmallset(localswitches);
  606. ppufile.puttype(resulttype);
  607. ppufile.putsmallset(flags);
  608. end;
  609. procedure tnode.buildderefimpl;
  610. begin
  611. resulttype.buildderef;
  612. end;
  613. procedure tnode.derefimpl;
  614. begin
  615. resulttype.resolve;
  616. end;
  617. procedure tnode.derefnode;
  618. begin
  619. end;
  620. procedure tnode.toggleflag(f : tnodeflag);
  621. begin
  622. if f in flags then
  623. exclude(flags,f)
  624. else
  625. include(flags,f);
  626. end;
  627. function tnode.simplify : tnode;
  628. begin
  629. result:=nil;
  630. end;
  631. destructor tnode.destroy;
  632. begin
  633. {$ifdef EXTDEBUG}
  634. if firstpasscount>maxfirstpasscount then
  635. maxfirstpasscount:=firstpasscount;
  636. {$endif EXTDEBUG}
  637. end;
  638. procedure tnode.concattolist(l : tlinkedlist);
  639. begin
  640. end;
  641. function tnode.ischild(p : tnode) : boolean;
  642. begin
  643. ischild:=false;
  644. end;
  645. procedure tnode.mark_write;
  646. begin
  647. {$ifdef EXTDEBUG}
  648. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  649. {$endif EXTDEBUG}
  650. end;
  651. procedure tnode.printnodeinfo(var t:text);
  652. begin
  653. write(t,nodetype2str[nodetype]);
  654. if assigned(resulttype.def) then
  655. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  656. else
  657. write(t,', resulttype = <nil>');
  658. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  659. ', loc = ',tcgloc2str[location.loc],
  660. ', expectloc = ',tcgloc2str[expectloc],
  661. ', intregs = ',registersint,
  662. ', fpuregs = ',registersfpu);
  663. end;
  664. procedure tnode.printnodedata(var t:text);
  665. begin
  666. end;
  667. procedure tnode.printnodetree(var t:text);
  668. begin
  669. write(t,printnodeindention,'(');
  670. printnodeinfo(t);
  671. writeln(t);
  672. printnodeindent;
  673. printnodedata(t);
  674. printnodeunindent;
  675. writeln(t,printnodeindention,')');
  676. end;
  677. function tnode.isequal(p : tnode) : boolean;
  678. begin
  679. isequal:=
  680. (not assigned(self) and not assigned(p)) or
  681. (assigned(self) and assigned(p) and
  682. { optimized subclasses have the same nodetype as their }
  683. { superclass (for compatibility), so also check the classtype (JM) }
  684. (p.classtype=classtype) and
  685. (p.nodetype=nodetype) and
  686. (flags*flagsequal=p.flags*flagsequal) and
  687. docompare(p));
  688. end;
  689. {$ifdef state_tracking}
  690. function Tnode.track_state_pass(exec_known:boolean):boolean;
  691. begin
  692. track_state_pass:=false;
  693. end;
  694. {$endif state_tracking}
  695. function tnode.docompare(p : tnode) : boolean;
  696. begin
  697. docompare:=true;
  698. end;
  699. function cleanupcopiedto(var n : tnode;arg : pointer) : foreachnoderesult;
  700. begin
  701. result:=fen_true;
  702. if n.nodetype=labeln then
  703. tlabelnode(n).copiedto:=nil;
  704. end;
  705. function tnode.getcopy : tnode;
  706. begin
  707. result:=_getcopy;
  708. foreachnodestatic(pm_postprocess,self,@cleanupcopiedto,nil);
  709. end;
  710. function tnode._getcopy : tnode;
  711. var
  712. p : tnode;
  713. begin
  714. { this is quite tricky because we need a node of the current }
  715. { node type and not one of tnode! }
  716. p:=tnodeclass(classtype).createforcopy;
  717. p.nodetype:=nodetype;
  718. p.expectloc:=expectloc;
  719. p.location:=location;
  720. p.parent:=parent;
  721. p.flags:=flags;
  722. p.registersint:=registersint;
  723. p.registersfpu:=registersfpu;
  724. {$ifdef SUPPORT_MMX}
  725. p.registersmmx:=registersmmx;
  726. p.registersmm:=registersmm;
  727. {$endif SUPPORT_MMX}
  728. p.resulttype:=resulttype;
  729. p.fileinfo:=fileinfo;
  730. p.localswitches:=localswitches;
  731. {$ifdef extdebug}
  732. p.firstpasscount:=firstpasscount;
  733. {$endif extdebug}
  734. { p.list:=list; }
  735. result:=p;
  736. end;
  737. procedure tnode.insertintolist(l : tnodelist);
  738. begin
  739. end;
  740. {****************************************************************************
  741. TUNARYNODE
  742. ****************************************************************************}
  743. constructor tunarynode.create(t:tnodetype;l : tnode);
  744. begin
  745. inherited create(t);
  746. left:=l;
  747. end;
  748. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  749. begin
  750. inherited ppuload(t,ppufile);
  751. left:=ppuloadnode(ppufile);
  752. end;
  753. destructor tunarynode.destroy;
  754. begin
  755. left.free;
  756. inherited destroy;
  757. end;
  758. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  759. begin
  760. inherited ppuwrite(ppufile);
  761. ppuwritenode(ppufile,left);
  762. end;
  763. procedure tunarynode.buildderefimpl;
  764. begin
  765. inherited buildderefimpl;
  766. if assigned(left) then
  767. left.buildderefimpl;
  768. end;
  769. procedure tunarynode.derefimpl;
  770. begin
  771. inherited derefimpl;
  772. if assigned(left) then
  773. left.derefimpl;
  774. end;
  775. procedure tunarynode.derefnode;
  776. begin
  777. inherited derefnode;
  778. if assigned(left) then
  779. left.derefnode;
  780. end;
  781. function tunarynode.docompare(p : tnode) : boolean;
  782. begin
  783. docompare:=(inherited docompare(p) and
  784. ((left=nil) or left.isequal(tunarynode(p).left))
  785. );
  786. end;
  787. function tunarynode._getcopy : tnode;
  788. var
  789. p : tunarynode;
  790. begin
  791. p:=tunarynode(inherited _getcopy);
  792. if assigned(left) then
  793. p.left:=left._getcopy
  794. else
  795. p.left:=nil;
  796. result:=p;
  797. end;
  798. procedure tunarynode.insertintolist(l : tnodelist);
  799. begin
  800. end;
  801. procedure tunarynode.printnodedata(var t:text);
  802. begin
  803. inherited printnodedata(t);
  804. printnode(t,left);
  805. end;
  806. procedure tunarynode.left_max;
  807. begin
  808. registersint:=left.registersint;
  809. registersfpu:=left.registersfpu;
  810. {$ifdef SUPPORT_MMX}
  811. registersmmx:=left.registersmmx;
  812. {$endif SUPPORT_MMX}
  813. end;
  814. procedure tunarynode.concattolist(l : tlinkedlist);
  815. begin
  816. left.parent:=self;
  817. left.concattolist(l);
  818. inherited concattolist(l);
  819. end;
  820. function tunarynode.ischild(p : tnode) : boolean;
  821. begin
  822. ischild:=p=left;
  823. end;
  824. {****************************************************************************
  825. TBINARYNODE
  826. ****************************************************************************}
  827. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  828. begin
  829. inherited create(t,l);
  830. right:=r
  831. end;
  832. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  833. begin
  834. inherited ppuload(t,ppufile);
  835. right:=ppuloadnode(ppufile);
  836. end;
  837. destructor tbinarynode.destroy;
  838. begin
  839. right.free;
  840. inherited destroy;
  841. end;
  842. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  843. begin
  844. inherited ppuwrite(ppufile);
  845. ppuwritenode(ppufile,right);
  846. end;
  847. procedure tbinarynode.buildderefimpl;
  848. begin
  849. inherited buildderefimpl;
  850. if assigned(right) then
  851. right.buildderefimpl;
  852. end;
  853. procedure tbinarynode.derefimpl;
  854. begin
  855. inherited derefimpl;
  856. if assigned(right) then
  857. right.derefimpl;
  858. end;
  859. procedure tbinarynode.derefnode;
  860. begin
  861. inherited derefnode;
  862. if assigned(right) then
  863. right.derefnode;
  864. end;
  865. procedure tbinarynode.concattolist(l : tlinkedlist);
  866. begin
  867. { we could change that depending on the number of }
  868. { required registers }
  869. left.parent:=self;
  870. left.concattolist(l);
  871. left.parent:=self;
  872. left.concattolist(l);
  873. inherited concattolist(l);
  874. end;
  875. function tbinarynode.ischild(p : tnode) : boolean;
  876. begin
  877. ischild:=(p=right);
  878. end;
  879. function tbinarynode.docompare(p : tnode) : boolean;
  880. begin
  881. docompare:=(inherited docompare(p) and
  882. ((right=nil) or right.isequal(tbinarynode(p).right))
  883. );
  884. end;
  885. function tbinarynode._getcopy : tnode;
  886. var
  887. p : tbinarynode;
  888. begin
  889. p:=tbinarynode(inherited _getcopy);
  890. if assigned(right) then
  891. p.right:=right._getcopy
  892. else
  893. p.right:=nil;
  894. result:=p;
  895. end;
  896. procedure tbinarynode.insertintolist(l : tnodelist);
  897. begin
  898. end;
  899. procedure tbinarynode.swapleftright;
  900. var
  901. swapp : tnode;
  902. begin
  903. swapp:=right;
  904. right:=left;
  905. left:=swapp;
  906. if nf_swaped in flags then
  907. exclude(flags,nf_swaped)
  908. else
  909. include(flags,nf_swaped);
  910. end;
  911. procedure tbinarynode.left_right_max;
  912. begin
  913. if assigned(left) then
  914. begin
  915. if assigned(right) then
  916. begin
  917. registersint:=max(left.registersint,right.registersint);
  918. registersfpu:=max(left.registersfpu,right.registersfpu);
  919. {$ifdef SUPPORT_MMX}
  920. registersmmx:=max(left.registersmmx,right.registersmmx);
  921. {$endif SUPPORT_MMX}
  922. end
  923. else
  924. begin
  925. registersint:=left.registersint;
  926. registersfpu:=left.registersfpu;
  927. {$ifdef SUPPORT_MMX}
  928. registersmmx:=left.registersmmx;
  929. {$endif SUPPORT_MMX}
  930. end;
  931. end;
  932. end;
  933. procedure tbinarynode.printnodedata(var t:text);
  934. begin
  935. inherited printnodedata(t);
  936. printnode(t,right);
  937. end;
  938. procedure tbinarynode.printnodelist(var t:text);
  939. var
  940. hp : tbinarynode;
  941. begin
  942. hp:=self;
  943. while assigned(hp) do
  944. begin
  945. write(t,printnodeindention,'(');
  946. printnodeindent;
  947. hp.printnodeinfo(t);
  948. writeln(t);
  949. printnode(t,hp.left);
  950. writeln(t);
  951. printnodeunindent;
  952. writeln(t,printnodeindention,')');
  953. hp:=tbinarynode(hp.right);
  954. end;
  955. end;
  956. {****************************************************************************
  957. TBINOPYNODE
  958. ****************************************************************************}
  959. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  960. begin
  961. inherited create(t,l,r);
  962. end;
  963. function tbinopnode.docompare(p : tnode) : boolean;
  964. begin
  965. docompare:=(inherited docompare(p)) or
  966. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  967. ((nf_swapable in flags) and
  968. left.isequal(tbinopnode(p).right) and
  969. right.isequal(tbinopnode(p).left));
  970. end;
  971. end.