node.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. const
  362. printnodespacing = ' ';
  363. var
  364. { indention used when writing the tree to the screen }
  365. printnodeindention : string;
  366. procedure printnodeindent;
  367. procedure printnodeunindent;
  368. procedure printnode(var t:text;n:tnode);
  369. function is_constnode(p : tnode) : boolean;
  370. function is_constintnode(p : tnode) : boolean;
  371. function is_constcharnode(p : tnode) : boolean;
  372. function is_constrealnode(p : tnode) : boolean;
  373. function is_constboolnode(p : tnode) : boolean;
  374. function is_constenumnode(p : tnode) : boolean;
  375. function is_constwidecharnode(p : tnode) : boolean;
  376. implementation
  377. uses
  378. cutils,verbose,ppu,
  379. symconst,
  380. defutil;
  381. const
  382. ppunodemarker = 255;
  383. {****************************************************************************
  384. Helpers
  385. ****************************************************************************}
  386. var
  387. nodeppudata : tdynamicarray;
  388. nodeppuidx : longint;
  389. procedure nodeppuidxcreate;
  390. begin
  391. nodeppudata:=tdynamicarray.create(1024);
  392. nodeppuidx:=0;
  393. end;
  394. procedure nodeppuidxfree;
  395. begin
  396. nodeppudata.free;
  397. nodeppudata:=nil;
  398. end;
  399. procedure nodeppuidxadd(n:tnode);
  400. begin
  401. if n.ppuidx<0 then
  402. internalerror(200311072);
  403. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  404. nodeppudata.write(n,sizeof(pointer));
  405. end;
  406. function nodeppuidxget(i:longint):tnode;
  407. begin
  408. if i<0 then
  409. internalerror(200311072);
  410. nodeppudata.seek(i*sizeof(pointer));
  411. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  412. internalerror(200311073);
  413. end;
  414. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  415. var
  416. b : byte;
  417. t : tnodetype;
  418. hppuidx : longint;
  419. begin
  420. { marker }
  421. b:=ppufile.getbyte;
  422. if b<>ppunodemarker then
  423. internalerror(200208151);
  424. { load nodetype }
  425. t:=tnodetype(ppufile.getbyte);
  426. if t>high(tnodetype) then
  427. internalerror(200208152);
  428. if t<>emptynode then
  429. begin
  430. if not assigned(nodeclass[t]) then
  431. internalerror(200208153);
  432. hppuidx:=ppufile.getlongint;
  433. //writeln('load: ',nodetype2str[t]);
  434. { generate node of the correct class }
  435. result:=nodeclass[t].ppuload(t,ppufile);
  436. result.ppuidx:=hppuidx;
  437. nodeppuidxadd(result);
  438. end
  439. else
  440. result:=nil;
  441. end;
  442. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  443. begin
  444. { marker, read by ppuloadnode }
  445. ppufile.putbyte(ppunodemarker);
  446. { type, read by ppuloadnode }
  447. if assigned(n) then
  448. begin
  449. if n.ppuidx=-1 then
  450. internalerror(200311071);
  451. n.ppuidx:=nodeppuidx;
  452. inc(nodeppuidx);
  453. ppufile.putbyte(byte(n.nodetype));
  454. ppufile.putlongint(n.ppuidx);
  455. //writeln('write: ',nodetype2str[n.nodetype]);
  456. n.ppuwrite(ppufile);
  457. end
  458. else
  459. ppufile.putbyte(byte(emptynode));
  460. end;
  461. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  462. begin
  463. if ppufile.readentry<>ibnodetree then
  464. Message(unit_f_ppu_read_error);
  465. nodeppuidxcreate;
  466. result:=ppuloadnode(ppufile);
  467. result.derefnode;
  468. nodeppuidxfree;
  469. end;
  470. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  471. begin
  472. nodeppuidx:=0;
  473. ppuwritenode(ppufile,n);
  474. ppufile.writeentry(ibnodetree);
  475. end;
  476. procedure printnodeindent;
  477. begin
  478. printnodeindention:=printnodeindention+printnodespacing;
  479. end;
  480. procedure printnodeunindent;
  481. begin
  482. delete(printnodeindention,1,length(printnodespacing));
  483. end;
  484. procedure printnode(var t:text;n:tnode);
  485. begin
  486. if assigned(n) then
  487. n.printnodetree(t)
  488. else
  489. writeln(t,printnodeindention,'nil');
  490. end;
  491. function is_constnode(p : tnode) : boolean;
  492. begin
  493. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  494. end;
  495. function is_constintnode(p : tnode) : boolean;
  496. begin
  497. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resulttype.def);
  498. end;
  499. function is_constcharnode(p : tnode) : boolean;
  500. begin
  501. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resulttype.def);
  502. end;
  503. function is_constwidecharnode(p : tnode) : boolean;
  504. begin
  505. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resulttype.def);
  506. end;
  507. function is_constrealnode(p : tnode) : boolean;
  508. begin
  509. is_constrealnode:=(p.nodetype=realconstn);
  510. end;
  511. function is_constboolnode(p : tnode) : boolean;
  512. begin
  513. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resulttype.def);
  514. end;
  515. function is_constenumnode(p : tnode) : boolean;
  516. begin
  517. is_constenumnode:=(p.nodetype=ordconstn) and (p.resulttype.def.deftype=enumdef);
  518. end;
  519. {****************************************************************************
  520. TNODE
  521. ****************************************************************************}
  522. constructor tnode.create(t:tnodetype);
  523. begin
  524. inherited create;
  525. nodetype:=t;
  526. blocktype:=block_type;
  527. { updated by firstpass }
  528. expectloc:=LOC_INVALID;
  529. { updated by secondpass }
  530. location.loc:=LOC_INVALID;
  531. { save local info }
  532. fileinfo:=aktfilepos;
  533. localswitches:=aktlocalswitches;
  534. resulttype.reset;
  535. registersint:=0;
  536. registersfpu:=0;
  537. {$ifdef SUPPORT_MMX}
  538. registersmmx:=0;
  539. {$endif SUPPORT_MMX}
  540. {$ifdef EXTDEBUG}
  541. maxfirstpasscount:=0;
  542. firstpasscount:=0;
  543. {$endif EXTDEBUG}
  544. flags:=[];
  545. ppuidx:=-1;
  546. end;
  547. constructor tnode.createforcopy;
  548. begin
  549. end;
  550. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  551. begin
  552. nodetype:=t;
  553. { tnode fields }
  554. blocktype:=tblock_type(ppufile.getbyte);
  555. ppufile.getposinfo(fileinfo);
  556. ppufile.getsmallset(localswitches);
  557. ppufile.gettype(resulttype);
  558. ppufile.getsmallset(flags);
  559. { updated by firstpass }
  560. expectloc:=LOC_INVALID;
  561. { updated by secondpass }
  562. location.loc:=LOC_INVALID;
  563. registersint:=0;
  564. registersfpu:=0;
  565. {$ifdef SUPPORT_MMX}
  566. registersmmx:=0;
  567. {$endif SUPPORT_MMX}
  568. {$ifdef EXTDEBUG}
  569. maxfirstpasscount:=0;
  570. firstpasscount:=0;
  571. {$endif EXTDEBUG}
  572. ppuidx:=-1;
  573. end;
  574. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  575. begin
  576. ppufile.putbyte(byte(block_type));
  577. ppufile.putposinfo(fileinfo);
  578. ppufile.putsmallset(localswitches);
  579. ppufile.puttype(resulttype);
  580. ppufile.putsmallset(flags);
  581. end;
  582. procedure tnode.buildderefimpl;
  583. begin
  584. resulttype.buildderef;
  585. end;
  586. procedure tnode.derefimpl;
  587. begin
  588. resulttype.resolve;
  589. end;
  590. procedure tnode.derefnode;
  591. begin
  592. end;
  593. procedure tnode.toggleflag(f : tnodeflag);
  594. begin
  595. if f in flags then
  596. exclude(flags,f)
  597. else
  598. include(flags,f);
  599. end;
  600. destructor tnode.destroy;
  601. begin
  602. {$ifdef EXTDEBUG}
  603. if firstpasscount>maxfirstpasscount then
  604. maxfirstpasscount:=firstpasscount;
  605. {$endif EXTDEBUG}
  606. end;
  607. procedure tnode.concattolist(l : tlinkedlist);
  608. begin
  609. end;
  610. function tnode.ischild(p : tnode) : boolean;
  611. begin
  612. ischild:=false;
  613. end;
  614. procedure tnode.mark_write;
  615. begin
  616. {$ifdef EXTDEBUG}
  617. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  618. {$endif EXTDEBUG}
  619. end;
  620. procedure tnode.printnodeinfo(var t:text);
  621. begin
  622. write(t,nodetype2str[nodetype]);
  623. if assigned(resulttype.def) then
  624. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  625. else
  626. write(t,', resulttype = <nil>');
  627. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  628. ', loc = ',tcgloc2str[location.loc],
  629. ', expectloc = ',tcgloc2str[expectloc],
  630. ', intregs = ',registersint,
  631. ', fpuregs = ',registersfpu);
  632. end;
  633. procedure tnode.printnodedata(var t:text);
  634. begin
  635. end;
  636. procedure tnode.printnodetree(var t:text);
  637. begin
  638. write(t,printnodeindention,'(');
  639. printnodeinfo(t);
  640. writeln(t);
  641. printnodeindent;
  642. printnodedata(t);
  643. printnodeunindent;
  644. writeln(t,printnodeindention,')');
  645. end;
  646. function tnode.isequal(p : tnode) : boolean;
  647. begin
  648. isequal:=
  649. (not assigned(self) and not assigned(p)) or
  650. (assigned(self) and assigned(p) and
  651. { optimized subclasses have the same nodetype as their }
  652. { superclass (for compatibility), so also check the classtype (JM) }
  653. (p.classtype=classtype) and
  654. (p.nodetype=nodetype) and
  655. (flags*flagsequal=p.flags*flagsequal) and
  656. docompare(p));
  657. end;
  658. {$ifdef state_tracking}
  659. function Tnode.track_state_pass(exec_known:boolean):boolean;
  660. begin
  661. track_state_pass:=false;
  662. end;
  663. {$endif state_tracking}
  664. function tnode.docompare(p : tnode) : boolean;
  665. begin
  666. docompare:=true;
  667. end;
  668. function tnode.getcopy : tnode;
  669. var
  670. p : tnode;
  671. begin
  672. { this is quite tricky because we need a node of the current }
  673. { node type and not one of tnode! }
  674. p:=tnodeclass(classtype).createforcopy;
  675. p.nodetype:=nodetype;
  676. p.expectloc:=expectloc;
  677. p.location:=location;
  678. p.parent:=parent;
  679. p.flags:=flags;
  680. p.registersint:=registersint;
  681. p.registersfpu:=registersfpu;
  682. {$ifdef SUPPORT_MMX}
  683. p.registersmmx:=registersmmx;
  684. p.registerskni:=registerskni;
  685. {$endif SUPPORT_MMX}
  686. p.resulttype:=resulttype;
  687. p.fileinfo:=fileinfo;
  688. p.localswitches:=localswitches;
  689. {$ifdef extdebug}
  690. p.firstpasscount:=firstpasscount;
  691. {$endif extdebug}
  692. { p.list:=list; }
  693. getcopy:=p;
  694. end;
  695. procedure tnode.insertintolist(l : tnodelist);
  696. begin
  697. end;
  698. {****************************************************************************
  699. TUNARYNODE
  700. ****************************************************************************}
  701. constructor tunarynode.create(t:tnodetype;l : tnode);
  702. begin
  703. inherited create(t);
  704. left:=l;
  705. end;
  706. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  707. begin
  708. inherited ppuload(t,ppufile);
  709. left:=ppuloadnode(ppufile);
  710. end;
  711. destructor tunarynode.destroy;
  712. begin
  713. left.free;
  714. inherited destroy;
  715. end;
  716. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  717. begin
  718. inherited ppuwrite(ppufile);
  719. ppuwritenode(ppufile,left);
  720. end;
  721. procedure tunarynode.buildderefimpl;
  722. begin
  723. inherited buildderefimpl;
  724. if assigned(left) then
  725. left.buildderefimpl;
  726. end;
  727. procedure tunarynode.derefimpl;
  728. begin
  729. inherited derefimpl;
  730. if assigned(left) then
  731. left.derefimpl;
  732. end;
  733. procedure tunarynode.derefnode;
  734. begin
  735. inherited derefnode;
  736. if assigned(left) then
  737. left.derefnode;
  738. end;
  739. function tunarynode.docompare(p : tnode) : boolean;
  740. begin
  741. docompare:=(inherited docompare(p) and
  742. ((left=nil) or left.isequal(tunarynode(p).left))
  743. );
  744. end;
  745. function tunarynode.getcopy : tnode;
  746. var
  747. p : tunarynode;
  748. begin
  749. p:=tunarynode(inherited getcopy);
  750. if assigned(left) then
  751. p.left:=left.getcopy
  752. else
  753. p.left:=nil;
  754. getcopy:=p;
  755. end;
  756. procedure tunarynode.insertintolist(l : tnodelist);
  757. begin
  758. end;
  759. procedure tunarynode.printnodedata(var t:text);
  760. begin
  761. inherited printnodedata(t);
  762. printnode(t,left);
  763. end;
  764. procedure tunarynode.left_max;
  765. begin
  766. registersint:=left.registersint;
  767. registersfpu:=left.registersfpu;
  768. {$ifdef SUPPORT_MMX}
  769. registersmmx:=left.registersmmx;
  770. {$endif SUPPORT_MMX}
  771. end;
  772. procedure tunarynode.concattolist(l : tlinkedlist);
  773. begin
  774. left.parent:=self;
  775. left.concattolist(l);
  776. inherited concattolist(l);
  777. end;
  778. function tunarynode.ischild(p : tnode) : boolean;
  779. begin
  780. ischild:=p=left;
  781. end;
  782. {****************************************************************************
  783. TBINARYNODE
  784. ****************************************************************************}
  785. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  786. begin
  787. inherited create(t,l);
  788. right:=r
  789. end;
  790. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  791. begin
  792. inherited ppuload(t,ppufile);
  793. right:=ppuloadnode(ppufile);
  794. end;
  795. destructor tbinarynode.destroy;
  796. begin
  797. right.free;
  798. inherited destroy;
  799. end;
  800. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  801. begin
  802. inherited ppuwrite(ppufile);
  803. ppuwritenode(ppufile,right);
  804. end;
  805. procedure tbinarynode.buildderefimpl;
  806. begin
  807. inherited buildderefimpl;
  808. if assigned(right) then
  809. right.buildderefimpl;
  810. end;
  811. procedure tbinarynode.derefimpl;
  812. begin
  813. inherited derefimpl;
  814. if assigned(right) then
  815. right.derefimpl;
  816. end;
  817. procedure tbinarynode.derefnode;
  818. begin
  819. inherited derefnode;
  820. if assigned(right) then
  821. right.derefnode;
  822. end;
  823. procedure tbinarynode.concattolist(l : tlinkedlist);
  824. begin
  825. { we could change that depending on the number of }
  826. { required registers }
  827. left.parent:=self;
  828. left.concattolist(l);
  829. left.parent:=self;
  830. left.concattolist(l);
  831. inherited concattolist(l);
  832. end;
  833. function tbinarynode.ischild(p : tnode) : boolean;
  834. begin
  835. ischild:=(p=right);
  836. end;
  837. function tbinarynode.docompare(p : tnode) : boolean;
  838. begin
  839. docompare:=(inherited docompare(p) and
  840. ((right=nil) or right.isequal(tbinarynode(p).right))
  841. );
  842. end;
  843. function tbinarynode.getcopy : tnode;
  844. var
  845. p : tbinarynode;
  846. begin
  847. p:=tbinarynode(inherited getcopy);
  848. if assigned(right) then
  849. p.right:=right.getcopy
  850. else
  851. p.right:=nil;
  852. getcopy:=p;
  853. end;
  854. procedure tbinarynode.insertintolist(l : tnodelist);
  855. begin
  856. end;
  857. procedure tbinarynode.swapleftright;
  858. var
  859. swapp : tnode;
  860. begin
  861. swapp:=right;
  862. right:=left;
  863. left:=swapp;
  864. if nf_swaped in flags then
  865. exclude(flags,nf_swaped)
  866. else
  867. include(flags,nf_swaped);
  868. end;
  869. procedure tbinarynode.left_right_max;
  870. begin
  871. if assigned(left) then
  872. begin
  873. if assigned(right) then
  874. begin
  875. registersint:=max(left.registersint,right.registersint);
  876. registersfpu:=max(left.registersfpu,right.registersfpu);
  877. {$ifdef SUPPORT_MMX}
  878. registersmmx:=max(left.registersmmx,right.registersmmx);
  879. {$endif SUPPORT_MMX}
  880. end
  881. else
  882. begin
  883. registersint:=left.registersint;
  884. registersfpu:=left.registersfpu;
  885. {$ifdef SUPPORT_MMX}
  886. registersmmx:=left.registersmmx;
  887. {$endif SUPPORT_MMX}
  888. end;
  889. end;
  890. end;
  891. procedure tbinarynode.printnodedata(var t:text);
  892. begin
  893. inherited printnodedata(t);
  894. printnode(t,right);
  895. end;
  896. procedure tbinarynode.printnodelist(var t:text);
  897. var
  898. hp : tbinarynode;
  899. begin
  900. hp:=self;
  901. while assigned(hp) do
  902. begin
  903. write(t,printnodeindention,'(');
  904. printnodeindent;
  905. hp.printnodeinfo(t);
  906. writeln(t);
  907. printnode(t,hp.left);
  908. writeln(t);
  909. printnodeunindent;
  910. writeln(t,printnodeindention,')');
  911. hp:=tbinarynode(hp.right);
  912. end;
  913. end;
  914. {****************************************************************************
  915. TBINOPYNODE
  916. ****************************************************************************}
  917. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  918. begin
  919. inherited create(t,l,r);
  920. end;
  921. function tbinopnode.docompare(p : tnode) : boolean;
  922. begin
  923. docompare:=(inherited docompare(p)) or
  924. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  925. ((nf_swapable in flags) and
  926. left.isequal(tbinopnode(p).right) and
  927. right.isequal(tbinopnode(p).left));
  928. end;
  929. end.