node.pas 33 KB

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