node.pas 32 KB

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