node.pas 36 KB

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