node.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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,symppu;
  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. nf_copy,
  190. { general }
  191. nf_write, { Node is written to }
  192. nf_isproperty,
  193. { flags used by tcallnode }
  194. nf_return_value_used,
  195. nf_inherited,
  196. nf_anon_inherited,
  197. nf_new_call,
  198. nf_dispose_call,
  199. nf_member_call, { called with implicit methodpointer tree }
  200. { flags used by tcallparanode }
  201. nf_varargs_para, { belongs this para to varargs }
  202. { taddrnode }
  203. nf_procvarload,
  204. nf_typedaddr,
  205. { tvecnode }
  206. nf_memindex,
  207. nf_memseg,
  208. nf_callunique,
  209. { tloadnode }
  210. nf_absolute,
  211. nf_load_self_pointer,
  212. { taddnode }
  213. nf_is_currency,
  214. { tassignmentnode }
  215. nf_concat_string,
  216. nf_use_strconcat,
  217. { tarrayconstructnode }
  218. nf_forcevaria,
  219. nf_novariaallowed,
  220. { ttypeconvnode }
  221. nf_explicit,
  222. { tinlinenode }
  223. nf_inlineconst
  224. );
  225. tnodeflags = set of tnodeflag;
  226. const
  227. { contains the flags which must be equal for the equality }
  228. { of nodes }
  229. flagsequal : tnodeflags = [nf_error];
  230. type
  231. tnodelist = class
  232. end;
  233. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  234. tnode = class
  235. public
  236. { type of this node }
  237. nodetype : tnodetype;
  238. { type of the current code block, general/const/type }
  239. blocktype : tblock_type;
  240. { expected location of the result of this node (pass1) }
  241. expectloc : tcgloc;
  242. { the location of the result of this node (pass2) }
  243. location : tlocation;
  244. { the parent node of this is node }
  245. { this field is set by concattolist }
  246. parent : tnode;
  247. { there are some properties about the node stored }
  248. flags : tnodeflags;
  249. ppuidx : longint;
  250. { the number of registers needed to evalute the node }
  251. registers32,registersfpu : longint; { must be longint !!!! }
  252. {$ifdef SUPPORT_MMX}
  253. registersmmx,registerskni : longint;
  254. {$endif SUPPORT_MMX}
  255. resulttype : ttype;
  256. fileinfo : tfileposinfo;
  257. localswitches : tlocalswitches;
  258. {$ifdef extdebug}
  259. maxfirstpasscount,
  260. firstpasscount : longint;
  261. {$endif extdebug}
  262. constructor create(t:tnodetype);
  263. { this constructor is only for creating copies of class }
  264. { the fields are copied by getcopy }
  265. constructor createforcopy;
  266. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  267. destructor destroy;override;
  268. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  269. procedure buildderefimpl;virtual;
  270. procedure derefimpl;virtual;
  271. procedure derefnode;virtual;
  272. { toggles the flag }
  273. procedure toggleflag(f : tnodeflag);
  274. { the 1.1 code generator may override pass_1 }
  275. { and it need not to implement det_* then }
  276. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  277. { 2.0: runs det_resulttype and det_temp }
  278. function pass_1 : tnode;virtual;abstract;
  279. { dermines the resulttype of the node }
  280. function det_resulttype : tnode;virtual;abstract;
  281. { dermines the number of necessary temp. locations to evaluate
  282. the node }
  283. {$ifdef state_tracking}
  284. { Does optimizations by keeping track of the variable states
  285. in a procedure }
  286. function track_state_pass(exec_known:boolean):boolean;virtual;
  287. {$endif}
  288. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  289. written to (normally the loadnode) as write access. }
  290. procedure mark_write;virtual;
  291. procedure det_temp;virtual;abstract;
  292. procedure pass_2;virtual;abstract;
  293. { comparing of nodes }
  294. function isequal(p : tnode) : boolean;
  295. { to implement comparisation, override this method }
  296. function docompare(p : tnode) : boolean;virtual;
  297. { gets a copy of the node }
  298. function getcopy : tnode;virtual;
  299. procedure insertintolist(l : tnodelist);virtual;
  300. { writes a node for debugging purpose, shouldn't be called }
  301. { direct, because there is no test for nil, use printnode }
  302. { to write a complete tree }
  303. procedure printnodeinfo(var t:text);
  304. procedure printnodedata(var t:text);virtual;
  305. procedure printnodetree(var t:text);virtual;
  306. procedure concattolist(l : tlinkedlist);virtual;
  307. function ischild(p : tnode) : boolean;virtual;
  308. procedure set_file_line(from : tnode);
  309. procedure set_tree_filepos(const filepos : tfileposinfo);
  310. end;
  311. tnodeclass = class of tnode;
  312. tnodeclassarray = array[tnodetype] of tnodeclass;
  313. { this node is the anchestor for all nodes with at least }
  314. { one child, you have to use it if you want to use }
  315. { true- and falselabel }
  316. punarynode = ^tunarynode;
  317. tunarynode = class(tnode)
  318. left : tnode;
  319. constructor create(t:tnodetype;l : tnode);
  320. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  321. destructor destroy;override;
  322. procedure ppuwrite(ppufile:tcompilerppufile);override;
  323. procedure buildderefimpl;override;
  324. procedure derefimpl;override;
  325. procedure derefnode;override;
  326. procedure concattolist(l : tlinkedlist);override;
  327. function ischild(p : tnode) : boolean;override;
  328. function docompare(p : tnode) : boolean;override;
  329. function getcopy : tnode;override;
  330. procedure insertintolist(l : tnodelist);override;
  331. procedure left_max;
  332. procedure printnodedata(var t:text);override;
  333. end;
  334. pbinarynode = ^tbinarynode;
  335. tbinarynode = class(tunarynode)
  336. right : tnode;
  337. constructor create(t:tnodetype;l,r : tnode);
  338. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  339. destructor destroy;override;
  340. procedure ppuwrite(ppufile:tcompilerppufile);override;
  341. procedure buildderefimpl;override;
  342. procedure derefimpl;override;
  343. procedure derefnode;override;
  344. procedure concattolist(l : tlinkedlist);override;
  345. function ischild(p : tnode) : boolean;override;
  346. function docompare(p : tnode) : boolean;override;
  347. procedure swapleftright;
  348. function getcopy : tnode;override;
  349. procedure insertintolist(l : tnodelist);override;
  350. procedure left_right_max;
  351. procedure printnodedata(var t:text);override;
  352. procedure printnodelist(var t:text);
  353. end;
  354. tbinopnode = class(tbinarynode)
  355. constructor create(t:tnodetype;l,r : tnode);virtual;
  356. function docompare(p : tnode) : boolean;override;
  357. end;
  358. var
  359. { array with all class types for tnodes }
  360. nodeclass : tnodeclassarray;
  361. function nodeppuidxget(i:longint):tnode;
  362. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  363. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  364. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  365. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  366. const
  367. printnodespacing = ' ';
  368. var
  369. { indention used when writing the tree to the screen }
  370. printnodeindention : string;
  371. procedure printnodeindent;
  372. procedure printnodeunindent;
  373. procedure printnode(var t:text;n:tnode);
  374. implementation
  375. uses
  376. cutils,verbose,ppu;
  377. const
  378. ppunodemarker = 255;
  379. {****************************************************************************
  380. Helpers
  381. ****************************************************************************}
  382. var
  383. nodeppudata : tdynamicarray;
  384. nodeppuidx : longint;
  385. procedure nodeppuidxcreate;
  386. begin
  387. nodeppudata:=tdynamicarray.create(1024);
  388. nodeppuidx:=0;
  389. end;
  390. procedure nodeppuidxfree;
  391. begin
  392. nodeppudata.free;
  393. nodeppudata:=nil;
  394. end;
  395. procedure nodeppuidxadd(n:tnode);
  396. begin
  397. if n.ppuidx<0 then
  398. internalerror(200311072);
  399. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  400. nodeppudata.write(n,sizeof(pointer));
  401. end;
  402. function nodeppuidxget(i:longint):tnode;
  403. var
  404. l : longint;
  405. begin
  406. if i<0 then
  407. internalerror(200311072);
  408. nodeppudata.seek(i*sizeof(pointer));
  409. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  410. internalerror(200311073);
  411. end;
  412. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  413. var
  414. b : byte;
  415. t : tnodetype;
  416. hppuidx : longint;
  417. begin
  418. { marker }
  419. b:=ppufile.getbyte;
  420. if b<>ppunodemarker then
  421. internalerror(200208151);
  422. { load nodetype }
  423. t:=tnodetype(ppufile.getbyte);
  424. if t>high(tnodetype) then
  425. internalerror(200208152);
  426. if t<>emptynode then
  427. begin
  428. if not assigned(nodeclass[t]) then
  429. internalerror(200208153);
  430. hppuidx:=ppufile.getlongint;
  431. //writeln('load: ',nodetype2str[t]);
  432. { generate node of the correct class }
  433. result:=nodeclass[t].ppuload(t,ppufile);
  434. result.ppuidx:=hppuidx;
  435. nodeppuidxadd(result);
  436. end
  437. else
  438. result:=nil;
  439. end;
  440. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  441. begin
  442. { marker, read by ppuloadnode }
  443. ppufile.putbyte(ppunodemarker);
  444. { type, read by ppuloadnode }
  445. if assigned(n) then
  446. begin
  447. if n.ppuidx=-1 then
  448. internalerror(200311071);
  449. n.ppuidx:=nodeppuidx;
  450. inc(nodeppuidx);
  451. ppufile.putbyte(byte(n.nodetype));
  452. ppufile.putlongint(n.ppuidx);
  453. //writeln('write: ',nodetype2str[n.nodetype]);
  454. n.ppuwrite(ppufile);
  455. end
  456. else
  457. ppufile.putbyte(byte(emptynode));
  458. end;
  459. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  460. begin
  461. if ppufile.readentry<>ibnodetree then
  462. Message(unit_f_ppu_read_error);
  463. nodeppuidxcreate;
  464. result:=ppuloadnode(ppufile);
  465. result.derefnode;
  466. nodeppuidxfree;
  467. end;
  468. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  469. begin
  470. nodeppuidx:=0;
  471. ppuwritenode(ppufile,n);
  472. ppufile.writeentry(ibnodetree);
  473. end;
  474. procedure printnodeindent;
  475. begin
  476. printnodeindention:=printnodeindention+printnodespacing;
  477. end;
  478. procedure printnodeunindent;
  479. begin
  480. delete(printnodeindention,1,length(printnodespacing));
  481. end;
  482. procedure printnode(var t:text;n:tnode);
  483. begin
  484. if assigned(n) then
  485. n.printnodetree(t)
  486. else
  487. writeln(t,printnodeindention,'nil');
  488. end;
  489. {****************************************************************************
  490. TNODE
  491. ****************************************************************************}
  492. constructor tnode.create(t:tnodetype);
  493. begin
  494. inherited create;
  495. nodetype:=t;
  496. blocktype:=block_type;
  497. { updated by firstpass }
  498. expectloc:=LOC_INVALID;
  499. { updated by secondpass }
  500. location.loc:=LOC_INVALID;
  501. { save local info }
  502. fileinfo:=aktfilepos;
  503. localswitches:=aktlocalswitches;
  504. resulttype.reset;
  505. registers32:=0;
  506. registersfpu:=0;
  507. {$ifdef SUPPORT_MMX}
  508. registersmmx:=0;
  509. {$endif SUPPORT_MMX}
  510. {$ifdef EXTDEBUG}
  511. maxfirstpasscount:=0;
  512. firstpasscount:=0;
  513. {$endif EXTDEBUG}
  514. flags:=[];
  515. ppuidx:=-1;
  516. end;
  517. constructor tnode.createforcopy;
  518. begin
  519. end;
  520. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  521. begin
  522. nodetype:=t;
  523. { tnode fields }
  524. blocktype:=tblock_type(ppufile.getbyte);
  525. ppufile.getposinfo(fileinfo);
  526. ppufile.getsmallset(localswitches);
  527. ppufile.gettype(resulttype);
  528. ppufile.getsmallset(flags);
  529. { updated by firstpass }
  530. expectloc:=LOC_INVALID;
  531. { updated by secondpass }
  532. location.loc:=LOC_INVALID;
  533. registers32:=0;
  534. registersfpu:=0;
  535. {$ifdef SUPPORT_MMX}
  536. registersmmx:=0;
  537. {$endif SUPPORT_MMX}
  538. {$ifdef EXTDEBUG}
  539. maxfirstpasscount:=0;
  540. firstpasscount:=0;
  541. {$endif EXTDEBUG}
  542. ppuidx:=-1;
  543. end;
  544. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  545. begin
  546. ppufile.putbyte(byte(block_type));
  547. ppufile.putposinfo(fileinfo);
  548. ppufile.putsmallset(localswitches);
  549. ppufile.puttype(resulttype);
  550. ppufile.putsmallset(flags);
  551. end;
  552. procedure tnode.buildderefimpl;
  553. begin
  554. resulttype.buildderef;
  555. end;
  556. procedure tnode.derefimpl;
  557. begin
  558. resulttype.resolve;
  559. end;
  560. procedure tnode.derefnode;
  561. begin
  562. end;
  563. procedure tnode.toggleflag(f : tnodeflag);
  564. begin
  565. if f in flags then
  566. exclude(flags,f)
  567. else
  568. include(flags,f);
  569. end;
  570. destructor tnode.destroy;
  571. begin
  572. {$ifdef EXTDEBUG}
  573. if firstpasscount>maxfirstpasscount then
  574. maxfirstpasscount:=firstpasscount;
  575. {$endif EXTDEBUG}
  576. end;
  577. procedure tnode.concattolist(l : tlinkedlist);
  578. begin
  579. end;
  580. function tnode.ischild(p : tnode) : boolean;
  581. begin
  582. ischild:=false;
  583. end;
  584. procedure tnode.mark_write;
  585. begin
  586. {$ifdef EXTDEBUG}
  587. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  588. {$endif EXTDEBUG}
  589. end;
  590. procedure tnode.printnodeinfo(var t:text);
  591. begin
  592. write(t,nodetype2str[nodetype]);
  593. if assigned(resulttype.def) then
  594. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  595. else
  596. write(t,', resulttype = <nil>');
  597. writeln(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  598. ', loc = ',tcgloc2str[location.loc],
  599. ', expectloc = ',tcgloc2str[expectloc],
  600. ', intregs = ',registers32,
  601. ', fpuregs = ',registersfpu);
  602. end;
  603. procedure tnode.printnodedata(var t:text);
  604. begin
  605. end;
  606. procedure tnode.printnodetree(var t:text);
  607. begin
  608. write(t,printnodeindention,'(');
  609. printnodeinfo(t);
  610. printnodeindent;
  611. printnodedata(t);
  612. printnodeunindent;
  613. writeln(t,printnodeindention,')');
  614. end;
  615. function tnode.isequal(p : tnode) : boolean;
  616. begin
  617. isequal:=
  618. (not assigned(self) and not assigned(p)) or
  619. (assigned(self) and assigned(p) and
  620. { optimized subclasses have the same nodetype as their }
  621. { superclass (for compatibility), so also check the classtype (JM) }
  622. (p.classtype=classtype) and
  623. (p.nodetype=nodetype) and
  624. (flags*flagsequal=p.flags*flagsequal) and
  625. docompare(p));
  626. end;
  627. {$ifdef state_tracking}
  628. function Tnode.track_state_pass(exec_known:boolean):boolean;
  629. begin
  630. track_state_pass:=false;
  631. end;
  632. {$endif state_tracking}
  633. function tnode.docompare(p : tnode) : boolean;
  634. begin
  635. docompare:=true;
  636. end;
  637. function tnode.getcopy : tnode;
  638. var
  639. p : tnode;
  640. begin
  641. { this is quite tricky because we need a node of the current }
  642. { node type and not one of tnode! }
  643. p:=tnodeclass(classtype).createforcopy;
  644. p.nodetype:=nodetype;
  645. p.location:=location;
  646. p.parent:=parent;
  647. p.flags:=flags;
  648. p.registers32:=registers32;
  649. p.registersfpu:=registersfpu;
  650. {$ifdef SUPPORT_MMX}
  651. p.registersmmx:=registersmmx;
  652. p.registerskni:=registerskni;
  653. {$endif SUPPORT_MMX}
  654. p.resulttype:=resulttype;
  655. p.fileinfo:=fileinfo;
  656. p.localswitches:=localswitches;
  657. {$ifdef extdebug}
  658. p.firstpasscount:=firstpasscount;
  659. {$endif extdebug}
  660. { mark node as being a copy }
  661. include(p.flags,nf_copy);
  662. { p.list:=list; }
  663. getcopy:=p;
  664. end;
  665. procedure tnode.insertintolist(l : tnodelist);
  666. begin
  667. end;
  668. procedure tnode.set_file_line(from : tnode);
  669. begin
  670. if assigned(from) then
  671. fileinfo:=from.fileinfo;
  672. end;
  673. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  674. begin
  675. fileinfo:=filepos;
  676. end;
  677. {****************************************************************************
  678. TUNARYNODE
  679. ****************************************************************************}
  680. constructor tunarynode.create(t:tnodetype;l : tnode);
  681. begin
  682. inherited create(t);
  683. left:=l;
  684. end;
  685. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  686. begin
  687. inherited ppuload(t,ppufile);
  688. left:=ppuloadnode(ppufile);
  689. end;
  690. destructor tunarynode.destroy;
  691. begin
  692. left.free;
  693. inherited destroy;
  694. end;
  695. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  696. begin
  697. inherited ppuwrite(ppufile);
  698. ppuwritenode(ppufile,left);
  699. end;
  700. procedure tunarynode.buildderefimpl;
  701. begin
  702. inherited buildderefimpl;
  703. if assigned(left) then
  704. left.buildderefimpl;
  705. end;
  706. procedure tunarynode.derefimpl;
  707. begin
  708. inherited derefimpl;
  709. if assigned(left) then
  710. left.derefimpl;
  711. end;
  712. procedure tunarynode.derefnode;
  713. begin
  714. inherited derefnode;
  715. if assigned(left) then
  716. left.derefnode;
  717. end;
  718. function tunarynode.docompare(p : tnode) : boolean;
  719. begin
  720. docompare:=(inherited docompare(p) and
  721. ((left=nil) or left.isequal(tunarynode(p).left))
  722. );
  723. end;
  724. function tunarynode.getcopy : tnode;
  725. var
  726. p : tunarynode;
  727. begin
  728. p:=tunarynode(inherited getcopy);
  729. if assigned(left) then
  730. p.left:=left.getcopy
  731. else
  732. p.left:=nil;
  733. getcopy:=p;
  734. end;
  735. procedure tunarynode.insertintolist(l : tnodelist);
  736. begin
  737. end;
  738. procedure tunarynode.printnodedata(var t:text);
  739. begin
  740. inherited printnodedata(t);
  741. printnode(t,left);
  742. end;
  743. procedure tunarynode.left_max;
  744. begin
  745. registers32:=left.registers32;
  746. registersfpu:=left.registersfpu;
  747. {$ifdef SUPPORT_MMX}
  748. registersmmx:=left.registersmmx;
  749. {$endif SUPPORT_MMX}
  750. end;
  751. procedure tunarynode.concattolist(l : tlinkedlist);
  752. begin
  753. left.parent:=self;
  754. left.concattolist(l);
  755. inherited concattolist(l);
  756. end;
  757. function tunarynode.ischild(p : tnode) : boolean;
  758. begin
  759. ischild:=p=left;
  760. end;
  761. {****************************************************************************
  762. TBINARYNODE
  763. ****************************************************************************}
  764. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  765. begin
  766. inherited create(t,l);
  767. right:=r
  768. end;
  769. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  770. begin
  771. inherited ppuload(t,ppufile);
  772. right:=ppuloadnode(ppufile);
  773. end;
  774. destructor tbinarynode.destroy;
  775. begin
  776. right.free;
  777. inherited destroy;
  778. end;
  779. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  780. begin
  781. inherited ppuwrite(ppufile);
  782. ppuwritenode(ppufile,right);
  783. end;
  784. procedure tbinarynode.buildderefimpl;
  785. begin
  786. inherited buildderefimpl;
  787. if assigned(right) then
  788. right.buildderefimpl;
  789. end;
  790. procedure tbinarynode.derefimpl;
  791. begin
  792. inherited derefimpl;
  793. if assigned(right) then
  794. right.derefimpl;
  795. end;
  796. procedure tbinarynode.derefnode;
  797. begin
  798. inherited derefnode;
  799. if assigned(right) then
  800. right.derefnode;
  801. end;
  802. procedure tbinarynode.concattolist(l : tlinkedlist);
  803. begin
  804. { we could change that depending on the number of }
  805. { required registers }
  806. left.parent:=self;
  807. left.concattolist(l);
  808. left.parent:=self;
  809. left.concattolist(l);
  810. inherited concattolist(l);
  811. end;
  812. function tbinarynode.ischild(p : tnode) : boolean;
  813. begin
  814. ischild:=(p=right);
  815. end;
  816. function tbinarynode.docompare(p : tnode) : boolean;
  817. begin
  818. docompare:=(inherited docompare(p) and
  819. ((right=nil) or right.isequal(tbinarynode(p).right))
  820. );
  821. end;
  822. function tbinarynode.getcopy : tnode;
  823. var
  824. p : tbinarynode;
  825. begin
  826. p:=tbinarynode(inherited getcopy);
  827. if assigned(right) then
  828. p.right:=right.getcopy
  829. else
  830. p.right:=nil;
  831. getcopy:=p;
  832. end;
  833. procedure tbinarynode.insertintolist(l : tnodelist);
  834. begin
  835. end;
  836. procedure tbinarynode.swapleftright;
  837. var
  838. swapp : tnode;
  839. begin
  840. swapp:=right;
  841. right:=left;
  842. left:=swapp;
  843. if nf_swaped in flags then
  844. exclude(flags,nf_swaped)
  845. else
  846. include(flags,nf_swaped);
  847. end;
  848. procedure tbinarynode.left_right_max;
  849. begin
  850. if assigned(left) then
  851. begin
  852. if assigned(right) then
  853. begin
  854. registers32:=max(left.registers32,right.registers32);
  855. registersfpu:=max(left.registersfpu,right.registersfpu);
  856. {$ifdef SUPPORT_MMX}
  857. registersmmx:=max(left.registersmmx,right.registersmmx);
  858. {$endif SUPPORT_MMX}
  859. end
  860. else
  861. begin
  862. registers32:=left.registers32;
  863. registersfpu:=left.registersfpu;
  864. {$ifdef SUPPORT_MMX}
  865. registersmmx:=left.registersmmx;
  866. {$endif SUPPORT_MMX}
  867. end;
  868. end;
  869. end;
  870. procedure tbinarynode.printnodedata(var t:text);
  871. begin
  872. inherited printnodedata(t);
  873. printnode(t,right);
  874. end;
  875. procedure tbinarynode.printnodelist(var t:text);
  876. var
  877. hp : tbinarynode;
  878. begin
  879. hp:=self;
  880. while assigned(hp) do
  881. begin
  882. write(t,printnodeindention,'(');
  883. printnodeindent;
  884. hp.printnodeinfo(t);
  885. printnode(t,hp.left);
  886. printnodeunindent;
  887. writeln(t,printnodeindention,')');
  888. hp:=tbinarynode(hp.right);
  889. end;
  890. end;
  891. {****************************************************************************
  892. TBINOPYNODE
  893. ****************************************************************************}
  894. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  895. begin
  896. inherited create(t,l,r);
  897. end;
  898. function tbinopnode.docompare(p : tnode) : boolean;
  899. begin
  900. docompare:=(inherited docompare(p)) or
  901. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  902. ((nf_swapable in flags) and
  903. left.isequal(tbinopnode(p).right) and
  904. right.isequal(tbinopnode(p).left));
  905. end;
  906. end.
  907. {
  908. $Log$
  909. Revision 1.77 2003-11-29 14:33:13 peter
  910. * typed address only used for @ and addr() that are parsed
  911. Revision 1.76 2003/11/23 17:38:48 peter
  912. * mark nodes that are copies
  913. Revision 1.75 2003/11/12 15:48:27 peter
  914. * fix set_varstate in for loops
  915. * fix set_varstate from case statements
  916. Revision 1.74 2003/11/10 22:02:52 peter
  917. * cross unit inlining fixed
  918. Revision 1.73 2003/10/23 14:44:07 peter
  919. * splitted buildderef and buildderefimpl to fix interface crc
  920. calculation
  921. Revision 1.72 2003/10/22 20:40:00 peter
  922. * write derefdata in a separate ppu entry
  923. Revision 1.71 2003/10/18 15:41:26 peter
  924. * made worklists dynamic in size
  925. Revision 1.70 2003/10/17 01:22:08 florian
  926. * compilation of the powerpc compiler fixed
  927. Revision 1.69 2003/10/08 19:19:45 peter
  928. * set_varstate cleanup
  929. Revision 1.68 2003/10/01 20:34:49 peter
  930. * procinfo unit contains tprocinfo
  931. * cginfo renamed to cgbase
  932. * moved cgmessage to verbose
  933. * fixed ppc and sparc compiles
  934. Revision 1.67 2003/09/28 17:55:04 peter
  935. * parent framepointer changed to hidden parameter
  936. * tloadparentfpnode added
  937. Revision 1.66 2003/09/06 22:27:08 florian
  938. * fixed web bug 2669
  939. * cosmetic fix in printnode
  940. * tobjectdef.gettypename implemented
  941. Revision 1.65 2003/09/03 15:55:01 peter
  942. * NEWRA branch merged
  943. Revision 1.64 2003/09/03 11:18:37 florian
  944. * fixed arm concatcopy
  945. + arm support in the common compiler sources added
  946. * moved some generic cg code around
  947. + tfputype added
  948. * ...
  949. Revision 1.63 2003/08/10 17:25:23 peter
  950. * fixed some reported bugs
  951. Revision 1.62 2003/05/26 21:17:17 peter
  952. * procinlinenode removed
  953. * aktexit2label removed, fast exit removed
  954. + tcallnode.inlined_pass_2 added
  955. Revision 1.61 2003/05/13 19:14:41 peter
  956. * failn removed
  957. * inherited result code check moven to pexpr
  958. Revision 1.60 2003/05/11 21:37:03 peter
  959. * moved implicit exception frame from ncgutil to psub
  960. * constructor/destructor helpers moved from cobj/ncgutil to psub
  961. Revision 1.59 2003/05/09 17:47:02 peter
  962. * self moved to hidden parameter
  963. * removed hdisposen,hnewn,selfn
  964. Revision 1.58 2003/04/25 20:59:33 peter
  965. * removed funcretn,funcretsym, function result is now in varsym
  966. and aliases for result and function name are added using absolutesym
  967. * vs_hidden parameter for funcret passed in parameter
  968. * vs_hidden fixes
  969. * writenode changed to printnode and released from extdebug
  970. * -vp option added to generate a tree.log with the nodetree
  971. * nicer printnode for statements, callnode
  972. Revision 1.57 2002/04/25 20:15:39 florian
  973. * block nodes within expressions shouldn't release the used registers,
  974. fixed using a flag till the new rg is ready
  975. Revision 1.56 2003/04/24 22:29:58 florian
  976. * fixed a lot of PowerPC related stuff
  977. Revision 1.55 2003/04/23 10:12:14 peter
  978. * allow multi pass2 changed to global boolean instead of node flag
  979. Revision 1.54 2003/04/22 23:50:23 peter
  980. * firstpass uses expectloc
  981. * checks if there are differences between the expectloc and
  982. location.loc from secondpass in EXTDEBUG
  983. Revision 1.53 2003/04/22 09:52:00 peter
  984. * mark_write implemented for default with a warning in EXTDEBUG, this
  985. is required for error recovery where the left node can be also a non
  986. writable node
  987. Revision 1.52 2003/04/10 17:57:52 peter
  988. * vs_hidden released
  989. Revision 1.51 2003/03/28 19:16:56 peter
  990. * generic constructor working for i386
  991. * remove fixed self register
  992. * esi added as address register for i386
  993. Revision 1.50 2003/03/17 16:54:41 peter
  994. * support DefaultHandler and anonymous inheritance fixed
  995. for message methods
  996. Revision 1.49 2003/01/04 15:54:03 daniel
  997. * Fixed mark_write for @ operator
  998. (can happen when compiling @procvar:=nil (Delphi mode construction))
  999. Revision 1.48 2003/01/03 21:03:02 peter
  1000. * made mark_write dummy instead of abstract
  1001. Revision 1.47 2003/01/03 12:15:56 daniel
  1002. * Removed ifdefs around notifications
  1003. ifdefs around for loop optimizations remain
  1004. Revision 1.46 2002/12/26 18:24:33 jonas
  1005. * fixed check for whether or not a high parameter was already generated
  1006. * no type checking/conversions for invisible parameters
  1007. Revision 1.45 2002/11/28 11:17:04 florian
  1008. * loop node flags from node flags splitted
  1009. Revision 1.44 2002/10/05 00:48:57 peter
  1010. * support inherited; support for overload as it is handled by
  1011. delphi. This is only for delphi mode as it is working is
  1012. undocumented and hard to predict what is done
  1013. Revision 1.43 2002/09/07 15:25:03 peter
  1014. * old logs removed and tabs fixed
  1015. Revision 1.42 2002/09/03 16:26:26 daniel
  1016. * Make Tprocdef.defs protected
  1017. Revision 1.41 2002/09/01 13:28:38 daniel
  1018. - write_access fields removed in favor of a flag
  1019. Revision 1.40 2002/09/01 08:01:16 daniel
  1020. * Removed sets from Tcallnode.det_resulttype
  1021. + Added read/write notifications of variables. These will be usefull
  1022. for providing information for several optimizations. For example
  1023. the value of the loop variable of a for loop does matter is the
  1024. variable is read after the for loop, but if it's no longer used
  1025. or written, it doesn't matter and this can be used to optimize
  1026. the loop code generation.
  1027. Revision 1.39 2002/08/22 11:21:45 florian
  1028. + register32 is now written by tnode.dowrite
  1029. * fixed write of value of tconstnode
  1030. Revision 1.38 2002/08/19 19:36:44 peter
  1031. * More fixes for cross unit inlining, all tnodes are now implemented
  1032. * Moved pocall_internconst to po_internconst because it is not a
  1033. calling type at all and it conflicted when inlining of these small
  1034. functions was requested
  1035. Revision 1.37 2002/08/18 20:06:24 peter
  1036. * inlining is now also allowed in interface
  1037. * renamed write/load to ppuwrite/ppuload
  1038. * tnode storing in ppu
  1039. * nld,ncon,nbas are already updated for storing in ppu
  1040. Revision 1.36 2002/08/17 22:09:46 florian
  1041. * result type handling in tcgcal.pass_2 overhauled
  1042. * better tnode.dowrite
  1043. * some ppc stuff fixed
  1044. Revision 1.35 2002/08/15 19:10:35 peter
  1045. * first things tai,tnode storing in ppu
  1046. Revision 1.34 2002/08/09 19:15:41 carl
  1047. - removed newcg define
  1048. Revision 1.33 2002/07/23 12:34:30 daniel
  1049. * Readded old set code. To use it define 'oldset'. Activated by default
  1050. for ppc.
  1051. Revision 1.32 2002/07/22 11:48:04 daniel
  1052. * Sets are now internally sets.
  1053. Revision 1.31 2002/07/21 06:58:49 daniel
  1054. * Changed booleans into flags
  1055. Revision 1.30 2002/07/19 11:41:36 daniel
  1056. * State tracker work
  1057. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1058. allows the state tracker to change while nodes automatically into
  1059. repeat nodes.
  1060. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1061. 'not(a>b)' is optimized into 'a<=b'.
  1062. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1063. by removing the notn and later switchting the true and falselabels. The
  1064. same is done with 'repeat until not a'.
  1065. Revision 1.29 2002/07/14 18:00:44 daniel
  1066. + Added the beginning of a state tracker. This will track the values of
  1067. variables through procedures and optimize things away.
  1068. Revision 1.28 2002/07/01 18:46:24 peter
  1069. * internal linker
  1070. * reorganized aasm layer
  1071. Revision 1.27 2002/05/18 13:34:10 peter
  1072. * readded missing revisions
  1073. Revision 1.26 2002/05/16 19:46:39 carl
  1074. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1075. + try to fix temp allocation (still in ifdef)
  1076. + generic constructor calls
  1077. + start of tassembler / tmodulebase class cleanup
  1078. Revision 1.24 2002/04/21 19:02:04 peter
  1079. * removed newn and disposen nodes, the code is now directly
  1080. inlined from pexpr
  1081. * -an option that will write the secondpass nodes to the .s file, this
  1082. requires EXTDEBUG define to actually write the info
  1083. * fixed various internal errors and crashes due recent code changes
  1084. Revision 1.23 2002/04/06 18:13:01 jonas
  1085. * several powerpc-related additions and fixes
  1086. Revision 1.22 2002/03/31 20:26:35 jonas
  1087. + a_loadfpu_* and a_loadmm_* methods in tcg
  1088. * register allocation is now handled by a class and is mostly processor
  1089. independent (+rgobj.pas and i386/rgcpu.pas)
  1090. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1091. * some small improvements and fixes to the optimizer
  1092. * some register allocation fixes
  1093. * some fpuvaroffset fixes in the unary minus node
  1094. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1095. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1096. also better optimizable)
  1097. * fixed and optimized register saving/restoring for new/dispose nodes
  1098. * LOC_FPU locations now also require their "register" field to be set to
  1099. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1100. - list field removed of the tnode class because it's not used currently
  1101. and can cause hard-to-find bugs
  1102. }