node.pas 33 KB

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