node.pas 32 KB

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