node.pas 33 KB

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