node.pas 36 KB

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