node.pas 36 KB

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