node.pas 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. pconstset = ^tconstset;
  28. tconstset = set of 0..255;
  29. tnodetype = (
  30. emptynode, {No node (returns nil when loading from ppu)}
  31. addn, {Represents the + operator}
  32. muln, {Represents the * operator}
  33. subn, {Represents the - operator}
  34. divn, {Represents the div operator}
  35. symdifn, {Represents the >< operator}
  36. modn, {Represents the mod operator}
  37. assignn, {Represents an assignment}
  38. loadn, {Represents the use of a variabele}
  39. rangen, {Represents a range (i.e. 0..9)}
  40. ltn, {Represents the < operator}
  41. lten, {Represents the <= operator}
  42. gtn, {Represents the > operator}
  43. gten, {Represents the >= operator}
  44. equaln, {Represents the = operator}
  45. unequaln, {Represents the <> operator}
  46. inn, {Represents the in operator}
  47. orn, {Represents the or operator}
  48. xorn, {Represents the xor operator}
  49. shrn, {Represents the shr operator}
  50. shln, {Represents the shl operator}
  51. slashn, {Represents the / operator}
  52. andn, {Represents the and operator}
  53. subscriptn, {Field in a record/object}
  54. derefn, {Dereferences a pointer}
  55. addrn, {Represents the @ operator}
  56. ordconstn, {Represents an ordinal value}
  57. typeconvn, {Represents type-conversion/typecast}
  58. calln, {Represents a call node}
  59. callparan, {Represents a parameter}
  60. realconstn, {Represents a real value}
  61. unaryminusn, {Represents a sign change (i.e. -2)}
  62. asmn, {Represents an assembler node }
  63. vecn, {Represents array indexing}
  64. pointerconstn, {Represents a pointer constant}
  65. stringconstn, {Represents a string constant}
  66. notn, {Represents the not operator}
  67. inlinen, {Internal procedures (i.e. writeln)}
  68. niln, {Represents the nil pointer}
  69. errorn, {This part of the tree could not be
  70. parsed because of a compiler error}
  71. typen, {A type name. Used for i.e. typeof(obj)}
  72. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  73. setconstn, {A set constant (i.e. [1,2])}
  74. blockn, {A block of statements}
  75. statementn, {One statement in a block of nodes}
  76. ifn, {An if statement}
  77. breakn, {A break statement}
  78. continuen, {A continue statement}
  79. whilerepeatn, {A while or repeat statement}
  80. forn, {A for loop}
  81. exitn, {An exit statement}
  82. withn, {A with statement}
  83. casen, {A case statement}
  84. labeln, {A label}
  85. goton, {A goto statement}
  86. tryexceptn, {A try except block}
  87. raisen, {A raise statement}
  88. tryfinallyn, {A try finally statement}
  89. onn, {For an on statement in exception code}
  90. isn, {Represents the is operator}
  91. asn, {Represents the as typecast}
  92. caretn, {Represents the ^ operator}
  93. starstarn, {Represents the ** operator exponentiation }
  94. arrayconstructorn, {Construction node for [...] parsing}
  95. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  96. tempcreaten, { for temps in the result/firstpass }
  97. temprefn, { references to temps }
  98. tempdeleten, { for temps in the result/firstpass }
  99. addoptn, { added for optimizations where we cannot suppress }
  100. nothingn, {NOP, Do nothing}
  101. loadvmtaddrn, {Load the address of the VMT of a class/object}
  102. guidconstn, {A GUID COM Interface constant }
  103. rttin, {Rtti information so they can be accessed in result/firstpass}
  104. loadparentfpn { Load the framepointer of the parent for nested procedures }
  105. );
  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. { taddnode }
  205. nf_is_currency,
  206. nf_has_pointerdiv,
  207. { tassignmentnode }
  208. nf_concat_string,
  209. nf_use_strconcat,
  210. { tarrayconstructnode }
  211. nf_forcevaria,
  212. nf_novariaallowed,
  213. { ttypeconvnode }
  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. { gets a copy of the node }
  300. function getcopy : tnode;virtual;
  301. procedure insertintolist(l : tnodelist);virtual;
  302. { writes a node for debugging purpose, shouldn't be called }
  303. { direct, because there is no test for nil, use printnode }
  304. { to write a complete tree }
  305. procedure printnodeinfo(var t:text);virtual;
  306. procedure printnodedata(var t:text);virtual;
  307. procedure printnodetree(var t:text);virtual;
  308. procedure concattolist(l : tlinkedlist);virtual;
  309. function ischild(p : tnode) : boolean;virtual;
  310. end;
  311. tnodeclass = class of tnode;
  312. tnodeclassarray = array[tnodetype] of tnodeclass;
  313. { this node is the anchestor for all nodes with at least }
  314. { one child, you have to use it if you want to use }
  315. { true- and falselabel }
  316. punarynode = ^tunarynode;
  317. tunarynode = class(tnode)
  318. left : tnode;
  319. constructor create(t:tnodetype;l : tnode);
  320. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  321. destructor destroy;override;
  322. procedure ppuwrite(ppufile:tcompilerppufile);override;
  323. procedure buildderefimpl;override;
  324. procedure derefimpl;override;
  325. procedure derefnode;override;
  326. procedure concattolist(l : tlinkedlist);override;
  327. function ischild(p : tnode) : boolean;override;
  328. function docompare(p : tnode) : boolean;override;
  329. function getcopy : tnode;override;
  330. procedure insertintolist(l : tnodelist);override;
  331. procedure left_max;
  332. procedure printnodedata(var t:text);override;
  333. end;
  334. pbinarynode = ^tbinarynode;
  335. tbinarynode = class(tunarynode)
  336. right : tnode;
  337. constructor create(t:tnodetype;l,r : tnode);
  338. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  339. destructor destroy;override;
  340. procedure ppuwrite(ppufile:tcompilerppufile);override;
  341. procedure buildderefimpl;override;
  342. procedure derefimpl;override;
  343. procedure derefnode;override;
  344. procedure concattolist(l : tlinkedlist);override;
  345. function ischild(p : tnode) : boolean;override;
  346. function docompare(p : tnode) : boolean;override;
  347. procedure swapleftright;
  348. function getcopy : tnode;override;
  349. procedure insertintolist(l : tnodelist);override;
  350. procedure left_right_max;
  351. procedure printnodedata(var t:text);override;
  352. procedure printnodelist(var t:text);
  353. end;
  354. tbinopnode = class(tbinarynode)
  355. constructor create(t:tnodetype;l,r : tnode);virtual;
  356. function docompare(p : tnode) : boolean;override;
  357. end;
  358. var
  359. { array with all class types for tnodes }
  360. nodeclass : tnodeclassarray;
  361. function nodeppuidxget(i:longint):tnode;
  362. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  363. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  364. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  365. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  366. const
  367. printnodespacing = ' ';
  368. var
  369. { indention used when writing the tree to the screen }
  370. printnodeindention : string;
  371. procedure printnodeindent;
  372. procedure printnodeunindent;
  373. procedure printnode(var t:text;n:tnode);
  374. function is_constnode(p : tnode) : boolean;
  375. function is_constintnode(p : tnode) : boolean;
  376. function is_constcharnode(p : tnode) : boolean;
  377. function is_constrealnode(p : tnode) : boolean;
  378. function is_constboolnode(p : tnode) : boolean;
  379. function is_constenumnode(p : tnode) : boolean;
  380. function is_constwidecharnode(p : tnode) : boolean;
  381. implementation
  382. uses
  383. cutils,verbose,ppu,
  384. symconst,
  385. defutil;
  386. const
  387. ppunodemarker = 255;
  388. {****************************************************************************
  389. Helpers
  390. ****************************************************************************}
  391. var
  392. nodeppudata : tdynamicarray;
  393. nodeppuidx : longint;
  394. procedure nodeppuidxcreate;
  395. begin
  396. nodeppudata:=tdynamicarray.create(1024);
  397. nodeppuidx:=0;
  398. end;
  399. procedure nodeppuidxfree;
  400. begin
  401. nodeppudata.free;
  402. nodeppudata:=nil;
  403. end;
  404. procedure nodeppuidxadd(n:tnode);
  405. begin
  406. if n.ppuidx<0 then
  407. internalerror(200311072);
  408. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  409. nodeppudata.write(n,sizeof(pointer));
  410. end;
  411. function nodeppuidxget(i:longint):tnode;
  412. begin
  413. if i<0 then
  414. internalerror(200311072);
  415. nodeppudata.seek(i*sizeof(pointer));
  416. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  417. internalerror(200311073);
  418. end;
  419. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  420. var
  421. b : byte;
  422. t : tnodetype;
  423. hppuidx : longint;
  424. begin
  425. { marker }
  426. b:=ppufile.getbyte;
  427. if b<>ppunodemarker then
  428. internalerror(200208151);
  429. { load nodetype }
  430. t:=tnodetype(ppufile.getbyte);
  431. if t>high(tnodetype) then
  432. internalerror(200208152);
  433. if t<>emptynode then
  434. begin
  435. if not assigned(nodeclass[t]) then
  436. internalerror(200208153);
  437. hppuidx:=ppufile.getlongint;
  438. //writeln('load: ',nodetype2str[t]);
  439. { generate node of the correct class }
  440. result:=nodeclass[t].ppuload(t,ppufile);
  441. result.ppuidx:=hppuidx;
  442. nodeppuidxadd(result);
  443. end
  444. else
  445. result:=nil;
  446. end;
  447. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  448. begin
  449. { marker, read by ppuloadnode }
  450. ppufile.putbyte(ppunodemarker);
  451. { type, read by ppuloadnode }
  452. if assigned(n) then
  453. begin
  454. if n.ppuidx=-1 then
  455. internalerror(200311071);
  456. n.ppuidx:=nodeppuidx;
  457. inc(nodeppuidx);
  458. ppufile.putbyte(byte(n.nodetype));
  459. ppufile.putlongint(n.ppuidx);
  460. //writeln('write: ',nodetype2str[n.nodetype]);
  461. n.ppuwrite(ppufile);
  462. end
  463. else
  464. ppufile.putbyte(byte(emptynode));
  465. end;
  466. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  467. begin
  468. if ppufile.readentry<>ibnodetree then
  469. Message(unit_f_ppu_read_error);
  470. nodeppuidxcreate;
  471. result:=ppuloadnode(ppufile);
  472. result.derefnode;
  473. nodeppuidxfree;
  474. end;
  475. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  476. begin
  477. nodeppuidx:=0;
  478. ppuwritenode(ppufile,n);
  479. ppufile.writeentry(ibnodetree);
  480. end;
  481. procedure printnodeindent;
  482. begin
  483. printnodeindention:=printnodeindention+printnodespacing;
  484. end;
  485. procedure printnodeunindent;
  486. begin
  487. delete(printnodeindention,1,length(printnodespacing));
  488. end;
  489. procedure printnode(var t:text;n:tnode);
  490. begin
  491. if assigned(n) then
  492. n.printnodetree(t)
  493. else
  494. writeln(t,printnodeindention,'nil');
  495. end;
  496. function is_constnode(p : tnode) : boolean;
  497. begin
  498. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  499. end;
  500. function is_constintnode(p : tnode) : boolean;
  501. begin
  502. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resulttype.def);
  503. end;
  504. function is_constcharnode(p : tnode) : boolean;
  505. begin
  506. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resulttype.def);
  507. end;
  508. function is_constwidecharnode(p : tnode) : boolean;
  509. begin
  510. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resulttype.def);
  511. end;
  512. function is_constrealnode(p : tnode) : boolean;
  513. begin
  514. is_constrealnode:=(p.nodetype=realconstn);
  515. end;
  516. function is_constboolnode(p : tnode) : boolean;
  517. begin
  518. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resulttype.def);
  519. end;
  520. function is_constenumnode(p : tnode) : boolean;
  521. begin
  522. is_constenumnode:=(p.nodetype=ordconstn) and (p.resulttype.def.deftype=enumdef);
  523. end;
  524. {****************************************************************************
  525. TNODE
  526. ****************************************************************************}
  527. constructor tnode.create(t:tnodetype);
  528. begin
  529. inherited create;
  530. nodetype:=t;
  531. blocktype:=block_type;
  532. { updated by firstpass }
  533. expectloc:=LOC_INVALID;
  534. { updated by secondpass }
  535. location.loc:=LOC_INVALID;
  536. { save local info }
  537. fileinfo:=aktfilepos;
  538. localswitches:=aktlocalswitches;
  539. resulttype.reset;
  540. registersint:=0;
  541. registersfpu:=0;
  542. {$ifdef SUPPORT_MMX}
  543. registersmmx:=0;
  544. {$endif SUPPORT_MMX}
  545. {$ifdef EXTDEBUG}
  546. maxfirstpasscount:=0;
  547. firstpasscount:=0;
  548. {$endif EXTDEBUG}
  549. flags:=[];
  550. ppuidx:=-1;
  551. end;
  552. constructor tnode.createforcopy;
  553. begin
  554. end;
  555. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  556. begin
  557. nodetype:=t;
  558. { tnode fields }
  559. blocktype:=tblock_type(ppufile.getbyte);
  560. ppufile.getposinfo(fileinfo);
  561. ppufile.getsmallset(localswitches);
  562. ppufile.gettype(resulttype);
  563. ppufile.getsmallset(flags);
  564. { updated by firstpass }
  565. expectloc:=LOC_INVALID;
  566. { updated by secondpass }
  567. location.loc:=LOC_INVALID;
  568. registersint:=0;
  569. registersfpu:=0;
  570. {$ifdef SUPPORT_MMX}
  571. registersmmx:=0;
  572. {$endif SUPPORT_MMX}
  573. {$ifdef EXTDEBUG}
  574. maxfirstpasscount:=0;
  575. firstpasscount:=0;
  576. {$endif EXTDEBUG}
  577. ppuidx:=-1;
  578. end;
  579. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  580. begin
  581. ppufile.putbyte(byte(block_type));
  582. ppufile.putposinfo(fileinfo);
  583. ppufile.putsmallset(localswitches);
  584. ppufile.puttype(resulttype);
  585. ppufile.putsmallset(flags);
  586. end;
  587. procedure tnode.buildderefimpl;
  588. begin
  589. resulttype.buildderef;
  590. end;
  591. procedure tnode.derefimpl;
  592. begin
  593. resulttype.resolve;
  594. end;
  595. procedure tnode.derefnode;
  596. begin
  597. end;
  598. procedure tnode.toggleflag(f : tnodeflag);
  599. begin
  600. if f in flags then
  601. exclude(flags,f)
  602. else
  603. include(flags,f);
  604. end;
  605. function tnode.simplify : tnode;
  606. begin
  607. result:=nil;
  608. end;
  609. destructor tnode.destroy;
  610. begin
  611. {$ifdef EXTDEBUG}
  612. if firstpasscount>maxfirstpasscount then
  613. maxfirstpasscount:=firstpasscount;
  614. {$endif EXTDEBUG}
  615. end;
  616. procedure tnode.concattolist(l : tlinkedlist);
  617. begin
  618. end;
  619. function tnode.ischild(p : tnode) : boolean;
  620. begin
  621. ischild:=false;
  622. end;
  623. procedure tnode.mark_write;
  624. begin
  625. {$ifdef EXTDEBUG}
  626. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  627. {$endif EXTDEBUG}
  628. end;
  629. procedure tnode.printnodeinfo(var t:text);
  630. begin
  631. write(t,nodetype2str[nodetype]);
  632. if assigned(resulttype.def) then
  633. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  634. else
  635. write(t,', resulttype = <nil>');
  636. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  637. ', loc = ',tcgloc2str[location.loc],
  638. ', expectloc = ',tcgloc2str[expectloc],
  639. ', intregs = ',registersint,
  640. ', fpuregs = ',registersfpu);
  641. end;
  642. procedure tnode.printnodedata(var t:text);
  643. begin
  644. end;
  645. procedure tnode.printnodetree(var t:text);
  646. begin
  647. write(t,printnodeindention,'(');
  648. printnodeinfo(t);
  649. writeln(t);
  650. printnodeindent;
  651. printnodedata(t);
  652. printnodeunindent;
  653. writeln(t,printnodeindention,')');
  654. end;
  655. function tnode.isequal(p : tnode) : boolean;
  656. begin
  657. isequal:=
  658. (not assigned(self) and not assigned(p)) or
  659. (assigned(self) and assigned(p) and
  660. { optimized subclasses have the same nodetype as their }
  661. { superclass (for compatibility), so also check the classtype (JM) }
  662. (p.classtype=classtype) and
  663. (p.nodetype=nodetype) and
  664. (flags*flagsequal=p.flags*flagsequal) and
  665. docompare(p));
  666. end;
  667. {$ifdef state_tracking}
  668. function Tnode.track_state_pass(exec_known:boolean):boolean;
  669. begin
  670. track_state_pass:=false;
  671. end;
  672. {$endif state_tracking}
  673. function tnode.docompare(p : tnode) : boolean;
  674. begin
  675. docompare:=true;
  676. end;
  677. function tnode.getcopy : tnode;
  678. var
  679. p : tnode;
  680. begin
  681. { this is quite tricky because we need a node of the current }
  682. { node type and not one of tnode! }
  683. p:=tnodeclass(classtype).createforcopy;
  684. p.nodetype:=nodetype;
  685. p.expectloc:=expectloc;
  686. p.location:=location;
  687. p.parent:=parent;
  688. p.flags:=flags;
  689. p.registersint:=registersint;
  690. p.registersfpu:=registersfpu;
  691. {$ifdef SUPPORT_MMX}
  692. p.registersmmx:=registersmmx;
  693. p.registerskni:=registerskni;
  694. {$endif SUPPORT_MMX}
  695. p.resulttype:=resulttype;
  696. p.fileinfo:=fileinfo;
  697. p.localswitches:=localswitches;
  698. {$ifdef extdebug}
  699. p.firstpasscount:=firstpasscount;
  700. {$endif extdebug}
  701. { p.list:=list; }
  702. getcopy:=p;
  703. end;
  704. procedure tnode.insertintolist(l : tnodelist);
  705. begin
  706. end;
  707. {****************************************************************************
  708. TUNARYNODE
  709. ****************************************************************************}
  710. constructor tunarynode.create(t:tnodetype;l : tnode);
  711. begin
  712. inherited create(t);
  713. left:=l;
  714. end;
  715. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  716. begin
  717. inherited ppuload(t,ppufile);
  718. left:=ppuloadnode(ppufile);
  719. end;
  720. destructor tunarynode.destroy;
  721. begin
  722. left.free;
  723. inherited destroy;
  724. end;
  725. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  726. begin
  727. inherited ppuwrite(ppufile);
  728. ppuwritenode(ppufile,left);
  729. end;
  730. procedure tunarynode.buildderefimpl;
  731. begin
  732. inherited buildderefimpl;
  733. if assigned(left) then
  734. left.buildderefimpl;
  735. end;
  736. procedure tunarynode.derefimpl;
  737. begin
  738. inherited derefimpl;
  739. if assigned(left) then
  740. left.derefimpl;
  741. end;
  742. procedure tunarynode.derefnode;
  743. begin
  744. inherited derefnode;
  745. if assigned(left) then
  746. left.derefnode;
  747. end;
  748. function tunarynode.docompare(p : tnode) : boolean;
  749. begin
  750. docompare:=(inherited docompare(p) and
  751. ((left=nil) or left.isequal(tunarynode(p).left))
  752. );
  753. end;
  754. function tunarynode.getcopy : tnode;
  755. var
  756. p : tunarynode;
  757. begin
  758. p:=tunarynode(inherited getcopy);
  759. if assigned(left) then
  760. p.left:=left.getcopy
  761. else
  762. p.left:=nil;
  763. getcopy:=p;
  764. end;
  765. procedure tunarynode.insertintolist(l : tnodelist);
  766. begin
  767. end;
  768. procedure tunarynode.printnodedata(var t:text);
  769. begin
  770. inherited printnodedata(t);
  771. printnode(t,left);
  772. end;
  773. procedure tunarynode.left_max;
  774. begin
  775. registersint:=left.registersint;
  776. registersfpu:=left.registersfpu;
  777. {$ifdef SUPPORT_MMX}
  778. registersmmx:=left.registersmmx;
  779. {$endif SUPPORT_MMX}
  780. end;
  781. procedure tunarynode.concattolist(l : tlinkedlist);
  782. begin
  783. left.parent:=self;
  784. left.concattolist(l);
  785. inherited concattolist(l);
  786. end;
  787. function tunarynode.ischild(p : tnode) : boolean;
  788. begin
  789. ischild:=p=left;
  790. end;
  791. {****************************************************************************
  792. TBINARYNODE
  793. ****************************************************************************}
  794. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  795. begin
  796. inherited create(t,l);
  797. right:=r
  798. end;
  799. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  800. begin
  801. inherited ppuload(t,ppufile);
  802. right:=ppuloadnode(ppufile);
  803. end;
  804. destructor tbinarynode.destroy;
  805. begin
  806. right.free;
  807. inherited destroy;
  808. end;
  809. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  810. begin
  811. inherited ppuwrite(ppufile);
  812. ppuwritenode(ppufile,right);
  813. end;
  814. procedure tbinarynode.buildderefimpl;
  815. begin
  816. inherited buildderefimpl;
  817. if assigned(right) then
  818. right.buildderefimpl;
  819. end;
  820. procedure tbinarynode.derefimpl;
  821. begin
  822. inherited derefimpl;
  823. if assigned(right) then
  824. right.derefimpl;
  825. end;
  826. procedure tbinarynode.derefnode;
  827. begin
  828. inherited derefnode;
  829. if assigned(right) then
  830. right.derefnode;
  831. end;
  832. procedure tbinarynode.concattolist(l : tlinkedlist);
  833. begin
  834. { we could change that depending on the number of }
  835. { required registers }
  836. left.parent:=self;
  837. left.concattolist(l);
  838. left.parent:=self;
  839. left.concattolist(l);
  840. inherited concattolist(l);
  841. end;
  842. function tbinarynode.ischild(p : tnode) : boolean;
  843. begin
  844. ischild:=(p=right);
  845. end;
  846. function tbinarynode.docompare(p : tnode) : boolean;
  847. begin
  848. docompare:=(inherited docompare(p) and
  849. ((right=nil) or right.isequal(tbinarynode(p).right))
  850. );
  851. end;
  852. function tbinarynode.getcopy : tnode;
  853. var
  854. p : tbinarynode;
  855. begin
  856. p:=tbinarynode(inherited getcopy);
  857. if assigned(right) then
  858. p.right:=right.getcopy
  859. else
  860. p.right:=nil;
  861. getcopy:=p;
  862. end;
  863. procedure tbinarynode.insertintolist(l : tnodelist);
  864. begin
  865. end;
  866. procedure tbinarynode.swapleftright;
  867. var
  868. swapp : tnode;
  869. begin
  870. swapp:=right;
  871. right:=left;
  872. left:=swapp;
  873. if nf_swaped in flags then
  874. exclude(flags,nf_swaped)
  875. else
  876. include(flags,nf_swaped);
  877. end;
  878. procedure tbinarynode.left_right_max;
  879. begin
  880. if assigned(left) then
  881. begin
  882. if assigned(right) then
  883. begin
  884. registersint:=max(left.registersint,right.registersint);
  885. registersfpu:=max(left.registersfpu,right.registersfpu);
  886. {$ifdef SUPPORT_MMX}
  887. registersmmx:=max(left.registersmmx,right.registersmmx);
  888. {$endif SUPPORT_MMX}
  889. end
  890. else
  891. begin
  892. registersint:=left.registersint;
  893. registersfpu:=left.registersfpu;
  894. {$ifdef SUPPORT_MMX}
  895. registersmmx:=left.registersmmx;
  896. {$endif SUPPORT_MMX}
  897. end;
  898. end;
  899. end;
  900. procedure tbinarynode.printnodedata(var t:text);
  901. begin
  902. inherited printnodedata(t);
  903. printnode(t,right);
  904. end;
  905. procedure tbinarynode.printnodelist(var t:text);
  906. var
  907. hp : tbinarynode;
  908. begin
  909. hp:=self;
  910. while assigned(hp) do
  911. begin
  912. write(t,printnodeindention,'(');
  913. printnodeindent;
  914. hp.printnodeinfo(t);
  915. writeln(t);
  916. printnode(t,hp.left);
  917. writeln(t);
  918. printnodeunindent;
  919. writeln(t,printnodeindention,')');
  920. hp:=tbinarynode(hp.right);
  921. end;
  922. end;
  923. {****************************************************************************
  924. TBINOPYNODE
  925. ****************************************************************************}
  926. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  927. begin
  928. inherited create(t,l,r);
  929. end;
  930. function tbinopnode.docompare(p : tnode) : boolean;
  931. begin
  932. docompare:=(inherited docompare(p)) or
  933. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  934. ((nf_swapable in flags) and
  935. left.isequal(tbinopnode(p).right) and
  936. right.isequal(tbinopnode(p).left));
  937. end;
  938. end.