node.pas 33 KB

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