node.pas 33 KB

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