node.pas 41 KB

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