node.pas 32 KB

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