node.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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,cginfo,
  25. aasmbase,
  26. symtype,symppu;
  27. type
  28. pconstset = ^tconstset;
  29. {$ifdef oldset}
  30. tconstset = array[0..31] of byte;
  31. pconst32bitset = ^tconst32bitset;
  32. tconst32bitset = array[0..7] of longint;
  33. {$else}
  34. tconstset = set of 0..255;
  35. {$endif}
  36. tnodetype = (
  37. emptynode, {No node (returns nil when loading from ppu)}
  38. addn, {Represents the + operator}
  39. muln, {Represents the * operator}
  40. subn, {Represents the - operator}
  41. divn, {Represents the div operator}
  42. symdifn, {Represents the >< operator}
  43. modn, {Represents the mod operator}
  44. assignn, {Represents an assignment}
  45. loadn, {Represents the use of a variabele}
  46. rangen, {Represents a range (i.e. 0..9)}
  47. ltn, {Represents the < operator}
  48. lten, {Represents the <= operator}
  49. gtn, {Represents the > operator}
  50. gten, {Represents the >= operator}
  51. equaln, {Represents the = operator}
  52. unequaln, {Represents the <> operator}
  53. inn, {Represents the in operator}
  54. orn, {Represents the or operator}
  55. xorn, {Represents the xor operator}
  56. shrn, {Represents the shr operator}
  57. shln, {Represents the shl operator}
  58. slashn, {Represents the / operator}
  59. andn, {Represents the and operator}
  60. subscriptn, {Field in a record/object}
  61. derefn, {Dereferences a pointer}
  62. addrn, {Represents the @ operator}
  63. doubleaddrn, {Represents the @@ operator}
  64. ordconstn, {Represents an ordinal value}
  65. typeconvn, {Represents type-conversion/typecast}
  66. calln, {Represents a call node}
  67. callparan, {Represents a parameter}
  68. realconstn, {Represents a real value}
  69. unaryminusn, {Represents a sign change (i.e. -2)}
  70. asmn, {Represents an assembler node }
  71. vecn, {Represents array indexing}
  72. pointerconstn, {Represents a pointer constant}
  73. stringconstn, {Represents a string constant}
  74. selfn, {Represents the self parameter}
  75. notn, {Represents the not operator}
  76. inlinen, {Internal procedures (i.e. writeln)}
  77. niln, {Represents the nil pointer}
  78. errorn, {This part of the tree could not be
  79. parsed because of a compiler error}
  80. typen, {A type name. Used for i.e. typeof(obj)}
  81. hnewn, {The new operation, constructor call}
  82. hdisposen, {The dispose operation with destructor call}
  83. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  84. setconstn, {A set constant (i.e. [1,2])}
  85. blockn, {A block of statements}
  86. statementn, {One statement in a block of nodes}
  87. ifn, {An if statement}
  88. breakn, {A break statement}
  89. continuen, {A continue statement}
  90. whilerepeatn, {A while or repeat statement}
  91. forn, {A for loop}
  92. exitn, {An exit statement}
  93. withn, {A with statement}
  94. casen, {A case statement}
  95. labeln, {A label}
  96. goton, {A goto statement}
  97. tryexceptn, {A try except block}
  98. raisen, {A raise statement}
  99. tryfinallyn, {A try finally statement}
  100. onn, {For an on statement in exception code}
  101. isn, {Represents the is operator}
  102. asn, {Represents the as typecast}
  103. caretn, {Represents the ^ operator}
  104. failn, {Represents the fail statement}
  105. starstarn, {Represents the ** operator exponentiation }
  106. procinlinen, {Procedures that can be inlined }
  107. arrayconstructorn, {Construction node for [...] parsing}
  108. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  109. tempcreaten, { for temps in the result/firstpass }
  110. temprefn, { references to temps }
  111. tempdeleten, { for temps in the result/firstpass }
  112. addoptn, { added for optimizations where we cannot suppress }
  113. nothingn, {NOP, Do nothing}
  114. loadvmtn, {Load the address of the VMT of a class/object}
  115. guidconstn, {A GUID COM Interface constant }
  116. rttin {Rtti information so they can be accessed in result/firstpass}
  117. );
  118. const
  119. nodetype2str : array[tnodetype] of string[20] = (
  120. '<emptynode>',
  121. 'addn',
  122. 'muln',
  123. 'subn',
  124. 'divn',
  125. 'symdifn',
  126. 'modn',
  127. 'assignn',
  128. 'loadn',
  129. 'rangen',
  130. 'ltn',
  131. 'lten',
  132. 'gtn',
  133. 'gten',
  134. 'equaln',
  135. 'unequaln',
  136. 'inn',
  137. 'orn',
  138. 'xorn',
  139. 'shrn',
  140. 'shln',
  141. 'slashn',
  142. 'andn',
  143. 'subscriptn',
  144. 'derefn',
  145. 'addrn',
  146. 'doubleaddrn',
  147. 'ordconstn',
  148. 'typeconvn',
  149. 'calln',
  150. 'callparan',
  151. 'realconstn',
  152. 'unaryminusn',
  153. 'asmn',
  154. 'vecn',
  155. 'pointerconstn',
  156. 'stringconstn',
  157. 'selfn',
  158. 'notn',
  159. 'inlinen',
  160. 'niln',
  161. 'errorn',
  162. 'typen',
  163. 'hnewn',
  164. 'hdisposen',
  165. 'setelementn',
  166. 'setconstn',
  167. 'blockn',
  168. 'statementn',
  169. 'ifn',
  170. 'breakn',
  171. 'continuen',
  172. 'whilerepeatn',
  173. 'forn',
  174. 'exitn',
  175. 'withn',
  176. 'casen',
  177. 'labeln',
  178. 'goton',
  179. 'tryexceptn',
  180. 'raisen',
  181. 'tryfinallyn',
  182. 'onn',
  183. 'isn',
  184. 'asn',
  185. 'caretn',
  186. 'failn',
  187. 'starstarn',
  188. 'procinlinen',
  189. 'arrayconstructn',
  190. 'arrayconstructrangen',
  191. 'tempcreaten',
  192. 'temprefn',
  193. 'tempdeleten',
  194. 'addoptn',
  195. 'nothingn',
  196. 'loadvmtn',
  197. 'guidconstn',
  198. 'rttin');
  199. type
  200. { all boolean field of ttree are now collected in flags }
  201. tnodeflags = (
  202. nf_swapable, { tbinop operands can be swaped }
  203. nf_swaped, { tbinop operands are swaped }
  204. nf_error,
  205. { general }
  206. nf_write, { Node is written to }
  207. nf_first_use, { First node that uses a variable after declared }
  208. nf_varstateset,
  209. nf_isproperty,
  210. { flags used by tcallnode }
  211. nf_return_value_used,
  212. nf_anon_inherited,
  213. { flags used by tcallparanode }
  214. nf_varargs_para, { belongs this para to varargs }
  215. { taddrnode }
  216. nf_procvarload,
  217. { tvecnode }
  218. nf_memindex,
  219. nf_memseg,
  220. nf_callunique,
  221. { twithnode }
  222. nf_islocal,
  223. { tloadnode }
  224. nf_absolute,
  225. { taddnode }
  226. nf_is_currency,
  227. { tassignmentnode }
  228. nf_concat_string,
  229. nf_use_strconcat,
  230. { tarrayconstructnode }
  231. nf_cargs,
  232. nf_cargswap,
  233. nf_forcevaria,
  234. nf_novariaallowed,
  235. { ttypeconvnode }
  236. nf_explicit,
  237. { tinlinenode }
  238. nf_inlineconst,
  239. { tblocknode }
  240. nf_releasetemps
  241. );
  242. tnodeflagset = set of tnodeflags;
  243. const
  244. { contains the flags which must be equal for the equality }
  245. { of nodes }
  246. flagsequal : tnodeflagset = [nf_error];
  247. type
  248. tnodelist = class
  249. end;
  250. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  251. tnode = class
  252. public
  253. { type of this node }
  254. nodetype : tnodetype;
  255. { type of the current code block, general/const/type }
  256. blocktype : tblock_type;
  257. { expected location of the result of this node (pass1) }
  258. expectloc : tcgloc;
  259. { the location of the result of this node (pass2) }
  260. location : tlocation;
  261. { the parent node of this is node }
  262. { this field is set by concattolist }
  263. parent : tnode;
  264. { there are some properties about the node stored }
  265. flags : tnodeflagset;
  266. { the number of registers needed to evalute the node }
  267. registers32,registersfpu : longint; { must be longint !!!! }
  268. {$ifdef SUPPORT_MMX}
  269. registersmmx,registerskni : longint;
  270. {$endif SUPPORT_MMX}
  271. resulttype : ttype;
  272. fileinfo : tfileposinfo;
  273. localswitches : tlocalswitches;
  274. {$ifdef extdebug}
  275. maxfirstpasscount,
  276. firstpasscount : longint;
  277. {$endif extdebug}
  278. constructor create(t:tnodetype);
  279. { this constructor is only for creating copies of class }
  280. { the fields are copied by getcopy }
  281. constructor createforcopy;
  282. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  283. destructor destroy;override;
  284. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  285. procedure derefimpl;virtual;
  286. { toggles the flag }
  287. procedure toggleflag(f : tnodeflags);
  288. { the 1.1 code generator may override pass_1 }
  289. { and it need not to implement det_* then }
  290. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  291. { 2.0: runs det_resulttype and det_temp }
  292. function pass_1 : tnode;virtual;abstract;
  293. { dermines the resulttype of the node }
  294. function det_resulttype : tnode;virtual;abstract;
  295. { dermines the number of necessary temp. locations to evaluate
  296. the node }
  297. {$ifdef state_tracking}
  298. { Does optimizations by keeping track of the variable states
  299. in a procedure }
  300. function track_state_pass(exec_known:boolean):boolean;virtual;
  301. {$endif}
  302. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  303. written to (normally the loadnode) as write access. }
  304. procedure mark_write;virtual;
  305. procedure det_temp;virtual;abstract;
  306. procedure pass_2;virtual;abstract;
  307. { comparing of nodes }
  308. function isequal(p : tnode) : boolean;
  309. { to implement comparisation, override this method }
  310. function docompare(p : tnode) : boolean;virtual;
  311. { gets a copy of the node }
  312. function getcopy : tnode;virtual;
  313. procedure insertintolist(l : tnodelist);virtual;
  314. { writes a node for debugging purpose, shouldn't be called }
  315. { direct, because there is no test for nil, use printnode }
  316. { to write a complete tree }
  317. procedure printnodeinfo(var t:text);
  318. procedure printnodedata(var t:text);virtual;
  319. procedure printnodetree(var t:text);virtual;
  320. procedure concattolist(l : tlinkedlist);virtual;
  321. function ischild(p : tnode) : boolean;virtual;
  322. procedure set_file_line(from : tnode);
  323. procedure set_tree_filepos(const filepos : tfileposinfo);
  324. end;
  325. tnodeclass = class of tnode;
  326. tnodeclassarray = array[tnodetype] of tnodeclass;
  327. { this node is the anchestor for all nodes with at least }
  328. { one child, you have to use it if you want to use }
  329. { true- and falselabel }
  330. punarynode = ^tunarynode;
  331. tunarynode = class(tnode)
  332. left : tnode;
  333. constructor create(t:tnodetype;l : tnode);
  334. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  335. destructor destroy;override;
  336. procedure ppuwrite(ppufile:tcompilerppufile);override;
  337. procedure derefimpl;override;
  338. procedure concattolist(l : tlinkedlist);override;
  339. function ischild(p : tnode) : boolean;override;
  340. function docompare(p : tnode) : boolean;override;
  341. function getcopy : tnode;override;
  342. procedure insertintolist(l : tnodelist);override;
  343. procedure left_max;
  344. procedure printnodedata(var t:text);override;
  345. end;
  346. pbinarynode = ^tbinarynode;
  347. tbinarynode = class(tunarynode)
  348. right : tnode;
  349. constructor create(t:tnodetype;l,r : tnode);
  350. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  351. destructor destroy;override;
  352. procedure ppuwrite(ppufile:tcompilerppufile);override;
  353. procedure derefimpl;override;
  354. procedure concattolist(l : tlinkedlist);override;
  355. function ischild(p : tnode) : boolean;override;
  356. function docompare(p : tnode) : boolean;override;
  357. procedure swapleftright;
  358. function getcopy : tnode;override;
  359. procedure insertintolist(l : tnodelist);override;
  360. procedure left_right_max;
  361. procedure printnodedata(var t:text);override;
  362. procedure printnodelist(var t:text);
  363. end;
  364. tbinopnode = class(tbinarynode)
  365. constructor create(t:tnodetype;l,r : tnode);virtual;
  366. function docompare(p : tnode) : boolean;override;
  367. end;
  368. {$ifdef tempregdebug}
  369. type
  370. pptree = ^tnode;
  371. var
  372. curptree: pptree;
  373. {$endif tempregdebug}
  374. var
  375. { array with all class types for tnodes }
  376. nodeclass : tnodeclassarray;
  377. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  378. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  379. const
  380. printnodespacing = ' ';
  381. var
  382. { indention used when writing the tree to the screen }
  383. printnodeindention : string;
  384. procedure printnodeindent;
  385. procedure printnodeunindent;
  386. procedure printnode(var t:text;n:tnode);
  387. implementation
  388. uses
  389. cutils,verbose;
  390. const
  391. ppunodemarker = 255;
  392. {****************************************************************************
  393. Helpers
  394. ****************************************************************************}
  395. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  396. var
  397. b : byte;
  398. t : tnodetype;
  399. begin
  400. { marker }
  401. b:=ppufile.getbyte;
  402. if b<>ppunodemarker then
  403. internalerror(200208151);
  404. { load nodetype }
  405. t:=tnodetype(ppufile.getbyte);
  406. if t>high(tnodetype) then
  407. internalerror(200208152);
  408. if t<>emptynode then
  409. begin
  410. if not assigned(nodeclass[t]) then
  411. internalerror(200208153);
  412. //writeln('load: ',nodetype2str[t]);
  413. { generate node of the correct class }
  414. ppuloadnode:=nodeclass[t].ppuload(t,ppufile);
  415. end
  416. else
  417. ppuloadnode:=nil;
  418. end;
  419. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  420. begin
  421. { marker, read by ppuloadnode }
  422. ppufile.putbyte(ppunodemarker);
  423. { type, read by ppuloadnode }
  424. if assigned(n) then
  425. begin
  426. ppufile.putbyte(byte(n.nodetype));
  427. //writeln('write: ',nodetype2str[n.nodetype]);
  428. n.ppuwrite(ppufile);
  429. end
  430. else
  431. ppufile.putbyte(byte(emptynode));
  432. end;
  433. procedure printnodeindent;
  434. begin
  435. printnodeindention:=printnodeindention+printnodespacing;
  436. end;
  437. procedure printnodeunindent;
  438. begin
  439. delete(printnodeindention,1,length(printnodespacing));
  440. end;
  441. procedure printnode(var t:text;n:tnode);
  442. begin
  443. if assigned(n) then
  444. n.printnodetree(t)
  445. else
  446. writeln(t,printnodeindention,'nil');
  447. end;
  448. {****************************************************************************
  449. TNODE
  450. ****************************************************************************}
  451. constructor tnode.create(t:tnodetype);
  452. begin
  453. inherited create;
  454. nodetype:=t;
  455. blocktype:=block_type;
  456. { updated by firstpass }
  457. expectloc:=LOC_INVALID;
  458. { updated by secondpass }
  459. location.loc:=LOC_INVALID;
  460. { save local info }
  461. fileinfo:=aktfilepos;
  462. localswitches:=aktlocalswitches;
  463. resulttype.reset;
  464. registers32:=0;
  465. registersfpu:=0;
  466. {$ifdef SUPPORT_MMX}
  467. registersmmx:=0;
  468. {$endif SUPPORT_MMX}
  469. {$ifdef EXTDEBUG}
  470. maxfirstpasscount:=0;
  471. firstpasscount:=0;
  472. {$endif EXTDEBUG}
  473. flags:=[];
  474. end;
  475. constructor tnode.createforcopy;
  476. begin
  477. end;
  478. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  479. begin
  480. nodetype:=t;
  481. { tnode fields }
  482. blocktype:=tblock_type(ppufile.getbyte);
  483. ppufile.getposinfo(fileinfo);
  484. ppufile.getsmallset(localswitches);
  485. ppufile.gettype(resulttype);
  486. ppufile.getsmallset(flags);
  487. { updated by firstpass }
  488. expectloc:=LOC_INVALID;
  489. { updated by secondpass }
  490. location.loc:=LOC_INVALID;
  491. registers32:=0;
  492. registersfpu:=0;
  493. {$ifdef SUPPORT_MMX}
  494. registersmmx:=0;
  495. {$endif SUPPORT_MMX}
  496. {$ifdef EXTDEBUG}
  497. maxfirstpasscount:=0;
  498. firstpasscount:=0;
  499. {$endif EXTDEBUG}
  500. end;
  501. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  502. begin
  503. ppufile.putbyte(byte(block_type));
  504. ppufile.putposinfo(fileinfo);
  505. ppufile.putsmallset(localswitches);
  506. ppufile.puttype(resulttype);
  507. ppufile.putsmallset(flags);
  508. end;
  509. procedure tnode.derefimpl;
  510. begin
  511. resulttype.resolve;
  512. end;
  513. procedure tnode.toggleflag(f : tnodeflags);
  514. begin
  515. if f in flags then
  516. exclude(flags,f)
  517. else
  518. include(flags,f);
  519. end;
  520. destructor tnode.destroy;
  521. begin
  522. {$ifdef EXTDEBUG}
  523. if firstpasscount>maxfirstpasscount then
  524. maxfirstpasscount:=firstpasscount;
  525. {$endif EXTDEBUG}
  526. end;
  527. procedure tnode.concattolist(l : tlinkedlist);
  528. begin
  529. end;
  530. function tnode.ischild(p : tnode) : boolean;
  531. begin
  532. ischild:=false;
  533. end;
  534. procedure tnode.mark_write;
  535. begin
  536. {$ifdef EXTDEBUG}
  537. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  538. {$endif EXTDEBUG}
  539. end;
  540. procedure tnode.printnodeinfo(var t:text);
  541. begin
  542. write(t,nodetype2str[nodetype]);
  543. if assigned(resulttype.def) then
  544. write(t,' ,resulttype = "',resulttype.def.gettypename,'"')
  545. else
  546. write(t,' ,resulttype = <nil>');
  547. writeln(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  548. ', loc = ',tcgloc2str[location.loc],
  549. ', inttgobj: = ',registers32,
  550. ', fpuregs = ',registersfpu);
  551. end;
  552. procedure tnode.printnodedata(var t:text);
  553. begin
  554. end;
  555. procedure tnode.printnodetree(var t:text);
  556. begin
  557. write(t,printnodeindention,'(');
  558. printnodeinfo(t);
  559. printnodeindent;
  560. printnodedata(t);
  561. printnodeunindent;
  562. writeln(t,printnodeindention,')');
  563. end;
  564. function tnode.isequal(p : tnode) : boolean;
  565. begin
  566. isequal:=
  567. (not assigned(self) and not assigned(p)) or
  568. (assigned(self) and assigned(p) and
  569. { optimized subclasses have the same nodetype as their }
  570. { superclass (for compatibility), so also check the classtype (JM) }
  571. (p.classtype=classtype) and
  572. (p.nodetype=nodetype) and
  573. (flags*flagsequal=p.flags*flagsequal) and
  574. docompare(p));
  575. end;
  576. {$ifdef state_tracking}
  577. function Tnode.track_state_pass(exec_known:boolean):boolean;
  578. begin
  579. track_state_pass:=false;
  580. end;
  581. {$endif state_tracking}
  582. function tnode.docompare(p : tnode) : boolean;
  583. begin
  584. docompare:=true;
  585. end;
  586. function tnode.getcopy : tnode;
  587. var
  588. p : tnode;
  589. begin
  590. { this is quite tricky because we need a node of the current }
  591. { node type and not one of tnode! }
  592. p:=tnodeclass(classtype).createforcopy;
  593. p.nodetype:=nodetype;
  594. p.location:=location;
  595. p.parent:=parent;
  596. p.flags:=flags;
  597. p.registers32:=registers32;
  598. p.registersfpu:=registersfpu;
  599. {$ifdef SUPPORT_MMX}
  600. p.registersmmx:=registersmmx;
  601. p.registerskni:=registerskni;
  602. {$endif SUPPORT_MMX}
  603. p.resulttype:=resulttype;
  604. p.fileinfo:=fileinfo;
  605. p.localswitches:=localswitches;
  606. {$ifdef extdebug}
  607. p.firstpasscount:=firstpasscount;
  608. {$endif extdebug}
  609. { p.list:=list; }
  610. getcopy:=p;
  611. end;
  612. procedure tnode.insertintolist(l : tnodelist);
  613. begin
  614. end;
  615. procedure tnode.set_file_line(from : tnode);
  616. begin
  617. if assigned(from) then
  618. fileinfo:=from.fileinfo;
  619. end;
  620. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  621. begin
  622. fileinfo:=filepos;
  623. end;
  624. {****************************************************************************
  625. TUNARYNODE
  626. ****************************************************************************}
  627. constructor tunarynode.create(t:tnodetype;l : tnode);
  628. begin
  629. inherited create(t);
  630. left:=l;
  631. end;
  632. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  633. begin
  634. inherited ppuload(t,ppufile);
  635. left:=ppuloadnode(ppufile);
  636. end;
  637. destructor tunarynode.destroy;
  638. begin
  639. left.free;
  640. inherited destroy;
  641. end;
  642. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  643. begin
  644. inherited ppuwrite(ppufile);
  645. ppuwritenode(ppufile,left);
  646. end;
  647. procedure tunarynode.derefimpl;
  648. begin
  649. inherited derefimpl;
  650. if assigned(left) then
  651. left.derefimpl;
  652. end;
  653. function tunarynode.docompare(p : tnode) : boolean;
  654. begin
  655. docompare:=(inherited docompare(p) and
  656. ((left=nil) or left.isequal(tunarynode(p).left))
  657. );
  658. end;
  659. function tunarynode.getcopy : tnode;
  660. var
  661. p : tunarynode;
  662. begin
  663. p:=tunarynode(inherited getcopy);
  664. if assigned(left) then
  665. p.left:=left.getcopy
  666. else
  667. p.left:=nil;
  668. getcopy:=p;
  669. end;
  670. procedure tunarynode.insertintolist(l : tnodelist);
  671. begin
  672. end;
  673. procedure tunarynode.printnodedata(var t:text);
  674. begin
  675. inherited printnodedata(t);
  676. printnode(t,left);
  677. end;
  678. procedure tunarynode.left_max;
  679. begin
  680. registers32:=left.registers32;
  681. registersfpu:=left.registersfpu;
  682. {$ifdef SUPPORT_MMX}
  683. registersmmx:=left.registersmmx;
  684. {$endif SUPPORT_MMX}
  685. end;
  686. procedure tunarynode.concattolist(l : tlinkedlist);
  687. begin
  688. left.parent:=self;
  689. left.concattolist(l);
  690. inherited concattolist(l);
  691. end;
  692. function tunarynode.ischild(p : tnode) : boolean;
  693. begin
  694. ischild:=p=left;
  695. end;
  696. {****************************************************************************
  697. TBINARYNODE
  698. ****************************************************************************}
  699. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  700. begin
  701. inherited create(t,l);
  702. right:=r
  703. end;
  704. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  705. begin
  706. inherited ppuload(t,ppufile);
  707. right:=ppuloadnode(ppufile);
  708. end;
  709. destructor tbinarynode.destroy;
  710. begin
  711. right.free;
  712. inherited destroy;
  713. end;
  714. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  715. begin
  716. inherited ppuwrite(ppufile);
  717. ppuwritenode(ppufile,right);
  718. end;
  719. procedure tbinarynode.derefimpl;
  720. begin
  721. inherited derefimpl;
  722. if assigned(right) then
  723. right.derefimpl;
  724. end;
  725. procedure tbinarynode.concattolist(l : tlinkedlist);
  726. begin
  727. { we could change that depending on the number of }
  728. { required registers }
  729. left.parent:=self;
  730. left.concattolist(l);
  731. left.parent:=self;
  732. left.concattolist(l);
  733. inherited concattolist(l);
  734. end;
  735. function tbinarynode.ischild(p : tnode) : boolean;
  736. begin
  737. ischild:=(p=right);
  738. end;
  739. function tbinarynode.docompare(p : tnode) : boolean;
  740. begin
  741. docompare:=(inherited docompare(p) and
  742. ((right=nil) or right.isequal(tbinarynode(p).right))
  743. );
  744. end;
  745. function tbinarynode.getcopy : tnode;
  746. var
  747. p : tbinarynode;
  748. begin
  749. p:=tbinarynode(inherited getcopy);
  750. if assigned(right) then
  751. p.right:=right.getcopy
  752. else
  753. p.right:=nil;
  754. getcopy:=p;
  755. end;
  756. procedure tbinarynode.insertintolist(l : tnodelist);
  757. begin
  758. end;
  759. procedure tbinarynode.swapleftright;
  760. var
  761. swapp : tnode;
  762. begin
  763. swapp:=right;
  764. right:=left;
  765. left:=swapp;
  766. if nf_swaped in flags then
  767. exclude(flags,nf_swaped)
  768. else
  769. include(flags,nf_swaped);
  770. end;
  771. procedure tbinarynode.left_right_max;
  772. begin
  773. if assigned(left) then
  774. begin
  775. if assigned(right) then
  776. begin
  777. registers32:=max(left.registers32,right.registers32);
  778. registersfpu:=max(left.registersfpu,right.registersfpu);
  779. {$ifdef SUPPORT_MMX}
  780. registersmmx:=max(left.registersmmx,right.registersmmx);
  781. {$endif SUPPORT_MMX}
  782. end
  783. else
  784. begin
  785. registers32:=left.registers32;
  786. registersfpu:=left.registersfpu;
  787. {$ifdef SUPPORT_MMX}
  788. registersmmx:=left.registersmmx;
  789. {$endif SUPPORT_MMX}
  790. end;
  791. end;
  792. end;
  793. procedure tbinarynode.printnodedata(var t:text);
  794. begin
  795. inherited printnodedata(t);
  796. printnode(t,right);
  797. end;
  798. procedure tbinarynode.printnodelist(var t:text);
  799. var
  800. hp : tbinarynode;
  801. begin
  802. hp:=self;
  803. while assigned(hp) do
  804. begin
  805. write(t,printnodeindention,'(');
  806. printnodeindent;
  807. hp.printnodeinfo(t);
  808. printnode(t,hp.left);
  809. printnodeunindent;
  810. writeln(t,printnodeindention,')');
  811. hp:=tbinarynode(hp.right);
  812. end;
  813. end;
  814. {****************************************************************************
  815. TBINOPYNODE
  816. ****************************************************************************}
  817. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  818. begin
  819. inherited create(t,l,r);
  820. end;
  821. function tbinopnode.docompare(p : tnode) : boolean;
  822. begin
  823. docompare:=(inherited docompare(p)) or
  824. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  825. ((nf_swapable in flags) and
  826. left.isequal(tbinopnode(p).right) and
  827. right.isequal(tbinopnode(p).left));
  828. end;
  829. end.
  830. {
  831. $Log$
  832. Revision 1.58 2003-04-25 20:59:33 peter
  833. * removed funcretn,funcretsym, function result is now in varsym
  834. and aliases for result and function name are added using absolutesym
  835. * vs_hidden parameter for funcret passed in parameter
  836. * vs_hidden fixes
  837. * writenode changed to printnode and released from extdebug
  838. * -vp option added to generate a tree.log with the nodetree
  839. * nicer printnode for statements, callnode
  840. Revision 1.57 2002/04/25 20:15:39 florian
  841. * block nodes within expressions shouldn't release the used registers,
  842. fixed using a flag till the new rg is ready
  843. Revision 1.56 2003/04/24 22:29:58 florian
  844. * fixed a lot of PowerPC related stuff
  845. Revision 1.55 2003/04/23 10:12:14 peter
  846. * allow multi pass2 changed to global boolean instead of node flag
  847. Revision 1.54 2003/04/22 23:50:23 peter
  848. * firstpass uses expectloc
  849. * checks if there are differences between the expectloc and
  850. location.loc from secondpass in EXTDEBUG
  851. Revision 1.53 2003/04/22 09:52:00 peter
  852. * mark_write implemented for default with a warning in EXTDEBUG, this
  853. is required for error recovery where the left node can be also a non
  854. writable node
  855. Revision 1.52 2003/04/10 17:57:52 peter
  856. * vs_hidden released
  857. Revision 1.51 2003/03/28 19:16:56 peter
  858. * generic constructor working for i386
  859. * remove fixed self register
  860. * esi added as address register for i386
  861. Revision 1.50 2003/03/17 16:54:41 peter
  862. * support DefaultHandler and anonymous inheritance fixed
  863. for message methods
  864. Revision 1.49 2003/01/04 15:54:03 daniel
  865. * Fixed mark_write for @ operator
  866. (can happen when compiling @procvar:=nil (Delphi mode construction))
  867. Revision 1.48 2003/01/03 21:03:02 peter
  868. * made mark_write dummy instead of abstract
  869. Revision 1.47 2003/01/03 12:15:56 daniel
  870. * Removed ifdefs around notifications
  871. ifdefs around for loop optimizations remain
  872. Revision 1.46 2002/12/26 18:24:33 jonas
  873. * fixed check for whether or not a high parameter was already generated
  874. * no type checking/conversions for invisible parameters
  875. Revision 1.45 2002/11/28 11:17:04 florian
  876. * loop node flags from node flags splitted
  877. Revision 1.44 2002/10/05 00:48:57 peter
  878. * support inherited; support for overload as it is handled by
  879. delphi. This is only for delphi mode as it is working is
  880. undocumented and hard to predict what is done
  881. Revision 1.43 2002/09/07 15:25:03 peter
  882. * old logs removed and tabs fixed
  883. Revision 1.42 2002/09/03 16:26:26 daniel
  884. * Make Tprocdef.defs protected
  885. Revision 1.41 2002/09/01 13:28:38 daniel
  886. - write_access fields removed in favor of a flag
  887. Revision 1.40 2002/09/01 08:01:16 daniel
  888. * Removed sets from Tcallnode.det_resulttype
  889. + Added read/write notifications of variables. These will be usefull
  890. for providing information for several optimizations. For example
  891. the value of the loop variable of a for loop does matter is the
  892. variable is read after the for loop, but if it's no longer used
  893. or written, it doesn't matter and this can be used to optimize
  894. the loop code generation.
  895. Revision 1.39 2002/08/22 11:21:45 florian
  896. + register32 is now written by tnode.dowrite
  897. * fixed write of value of tconstnode
  898. Revision 1.38 2002/08/19 19:36:44 peter
  899. * More fixes for cross unit inlining, all tnodes are now implemented
  900. * Moved pocall_internconst to po_internconst because it is not a
  901. calling type at all and it conflicted when inlining of these small
  902. functions was requested
  903. Revision 1.37 2002/08/18 20:06:24 peter
  904. * inlining is now also allowed in interface
  905. * renamed write/load to ppuwrite/ppuload
  906. * tnode storing in ppu
  907. * nld,ncon,nbas are already updated for storing in ppu
  908. Revision 1.36 2002/08/17 22:09:46 florian
  909. * result type handling in tcgcal.pass_2 overhauled
  910. * better tnode.dowrite
  911. * some ppc stuff fixed
  912. Revision 1.35 2002/08/15 19:10:35 peter
  913. * first things tai,tnode storing in ppu
  914. Revision 1.34 2002/08/09 19:15:41 carl
  915. - removed newcg define
  916. Revision 1.33 2002/07/23 12:34:30 daniel
  917. * Readded old set code. To use it define 'oldset'. Activated by default
  918. for ppc.
  919. Revision 1.32 2002/07/22 11:48:04 daniel
  920. * Sets are now internally sets.
  921. Revision 1.31 2002/07/21 06:58:49 daniel
  922. * Changed booleans into flags
  923. Revision 1.30 2002/07/19 11:41:36 daniel
  924. * State tracker work
  925. * The whilen and repeatn are now completely unified into whilerepeatn. This
  926. allows the state tracker to change while nodes automatically into
  927. repeat nodes.
  928. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  929. 'not(a>b)' is optimized into 'a<=b'.
  930. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  931. by removing the notn and later switchting the true and falselabels. The
  932. same is done with 'repeat until not a'.
  933. Revision 1.29 2002/07/14 18:00:44 daniel
  934. + Added the beginning of a state tracker. This will track the values of
  935. variables through procedures and optimize things away.
  936. Revision 1.28 2002/07/01 18:46:24 peter
  937. * internal linker
  938. * reorganized aasm layer
  939. Revision 1.27 2002/05/18 13:34:10 peter
  940. * readded missing revisions
  941. Revision 1.26 2002/05/16 19:46:39 carl
  942. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  943. + try to fix temp allocation (still in ifdef)
  944. + generic constructor calls
  945. + start of tassembler / tmodulebase class cleanup
  946. Revision 1.24 2002/04/21 19:02:04 peter
  947. * removed newn and disposen nodes, the code is now directly
  948. inlined from pexpr
  949. * -an option that will write the secondpass nodes to the .s file, this
  950. requires EXTDEBUG define to actually write the info
  951. * fixed various internal errors and crashes due recent code changes
  952. Revision 1.23 2002/04/06 18:13:01 jonas
  953. * several powerpc-related additions and fixes
  954. Revision 1.22 2002/03/31 20:26:35 jonas
  955. + a_loadfpu_* and a_loadmm_* methods in tcg
  956. * register allocation is now handled by a class and is mostly processor
  957. independent (+rgobj.pas and i386/rgcpu.pas)
  958. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  959. * some small improvements and fixes to the optimizer
  960. * some register allocation fixes
  961. * some fpuvaroffset fixes in the unary minus node
  962. * push/popusedregisters is now called rg.save/restoreusedregisters and
  963. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  964. also better optimizable)
  965. * fixed and optimized register saving/restoring for new/dispose nodes
  966. * LOC_FPU locations now also require their "register" field to be set to
  967. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  968. - list field removed of the tnode class because it's not used currently
  969. and can cause hard-to-find bugs
  970. }