node.pas 33 KB

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