node.pas 33 KB

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