node.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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. ordconstn, {Represents an ordinal value}
  64. typeconvn, {Represents type-conversion/typecast}
  65. calln, {Represents a call node}
  66. callparan, {Represents a parameter}
  67. realconstn, {Represents a real value}
  68. unaryminusn, {Represents a sign change (i.e. -2)}
  69. asmn, {Represents an assembler node }
  70. vecn, {Represents array indexing}
  71. pointerconstn, {Represents a pointer constant}
  72. stringconstn, {Represents a string constant}
  73. notn, {Represents the not operator}
  74. inlinen, {Internal procedures (i.e. writeln)}
  75. niln, {Represents the nil pointer}
  76. errorn, {This part of the tree could not be
  77. parsed because of a compiler error}
  78. typen, {A type name. Used for i.e. typeof(obj)}
  79. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  80. setconstn, {A set constant (i.e. [1,2])}
  81. blockn, {A block of statements}
  82. statementn, {One statement in a block of nodes}
  83. ifn, {An if statement}
  84. breakn, {A break statement}
  85. continuen, {A continue statement}
  86. whilerepeatn, {A while or repeat statement}
  87. forn, {A for loop}
  88. exitn, {An exit statement}
  89. withn, {A with statement}
  90. casen, {A case statement}
  91. labeln, {A label}
  92. goton, {A goto statement}
  93. tryexceptn, {A try except block}
  94. raisen, {A raise statement}
  95. tryfinallyn, {A try finally statement}
  96. onn, {For an on statement in exception code}
  97. isn, {Represents the is operator}
  98. asn, {Represents the as typecast}
  99. caretn, {Represents the ^ operator}
  100. starstarn, {Represents the ** operator exponentiation }
  101. arrayconstructorn, {Construction node for [...] parsing}
  102. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  103. tempcreaten, { for temps in the result/firstpass }
  104. temprefn, { references to temps }
  105. tempdeleten, { for temps in the result/firstpass }
  106. addoptn, { added for optimizations where we cannot suppress }
  107. nothingn, {NOP, Do nothing}
  108. loadvmtaddrn, {Load the address of the VMT of a class/object}
  109. guidconstn, {A GUID COM Interface constant }
  110. rttin {Rtti information so they can be accessed in result/firstpass}
  111. );
  112. const
  113. nodetype2str : array[tnodetype] of string[20] = (
  114. '<emptynode>',
  115. 'addn',
  116. 'muln',
  117. 'subn',
  118. 'divn',
  119. 'symdifn',
  120. 'modn',
  121. 'assignn',
  122. 'loadn',
  123. 'rangen',
  124. 'ltn',
  125. 'lten',
  126. 'gtn',
  127. 'gten',
  128. 'equaln',
  129. 'unequaln',
  130. 'inn',
  131. 'orn',
  132. 'xorn',
  133. 'shrn',
  134. 'shln',
  135. 'slashn',
  136. 'andn',
  137. 'subscriptn',
  138. 'derefn',
  139. 'addrn',
  140. 'ordconstn',
  141. 'typeconvn',
  142. 'calln',
  143. 'callparan',
  144. 'realconstn',
  145. 'unaryminusn',
  146. 'asmn',
  147. 'vecn',
  148. 'pointerconstn',
  149. 'stringconstn',
  150. 'notn',
  151. 'inlinen',
  152. 'niln',
  153. 'errorn',
  154. 'typen',
  155. 'setelementn',
  156. 'setconstn',
  157. 'blockn',
  158. 'statementn',
  159. 'ifn',
  160. 'breakn',
  161. 'continuen',
  162. 'whilerepeatn',
  163. 'forn',
  164. 'exitn',
  165. 'withn',
  166. 'casen',
  167. 'labeln',
  168. 'goton',
  169. 'tryexceptn',
  170. 'raisen',
  171. 'tryfinallyn',
  172. 'onn',
  173. 'isn',
  174. 'asn',
  175. 'caretn',
  176. 'starstarn',
  177. 'arrayconstructn',
  178. 'arrayconstructrangen',
  179. 'tempcreaten',
  180. 'temprefn',
  181. 'tempdeleten',
  182. 'addoptn',
  183. 'nothingn',
  184. 'loadvmtaddrn',
  185. 'guidconstn',
  186. 'rttin');
  187. type
  188. { all boolean field of ttree are now collected in flags }
  189. tnodeflag = (
  190. nf_swapable, { tbinop operands can be swaped }
  191. nf_swaped, { tbinop operands are swaped }
  192. nf_error,
  193. { general }
  194. nf_write, { Node is written to }
  195. nf_first_use, { First node that uses a variable after declared }
  196. nf_varstateset,
  197. nf_isproperty,
  198. { flags used by tcallnode }
  199. nf_return_value_used,
  200. nf_inherited,
  201. nf_anon_inherited,
  202. nf_new_call,
  203. nf_dispose_call,
  204. nf_member_call, { called with implicit methodpointer tree }
  205. { flags used by tcallparanode }
  206. nf_varargs_para, { belongs this para to varargs }
  207. { taddrnode }
  208. nf_procvarload,
  209. { tvecnode }
  210. nf_memindex,
  211. nf_memseg,
  212. nf_callunique,
  213. { tloadnode }
  214. nf_absolute,
  215. nf_load_self_pointer,
  216. { taddnode }
  217. nf_is_currency,
  218. { tassignmentnode }
  219. nf_concat_string,
  220. nf_use_strconcat,
  221. { tarrayconstructnode }
  222. nf_cargs,
  223. nf_cargswap,
  224. nf_forcevaria,
  225. nf_novariaallowed,
  226. { ttypeconvnode }
  227. nf_explicit,
  228. { tinlinenode }
  229. nf_inlineconst,
  230. { tblocknode }
  231. nf_releasetemps
  232. );
  233. tnodeflags = set of tnodeflag;
  234. const
  235. { contains the flags which must be equal for the equality }
  236. { of nodes }
  237. flagsequal : tnodeflags = [nf_error];
  238. type
  239. tnodelist = class
  240. end;
  241. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  242. tnode = class
  243. public
  244. { type of this node }
  245. nodetype : tnodetype;
  246. { type of the current code block, general/const/type }
  247. blocktype : tblock_type;
  248. { expected location of the result of this node (pass1) }
  249. expectloc : tcgloc;
  250. { the location of the result of this node (pass2) }
  251. location : tlocation;
  252. { the parent node of this is node }
  253. { this field is set by concattolist }
  254. parent : tnode;
  255. { there are some properties about the node stored }
  256. flags : tnodeflags;
  257. { the number of registers needed to evalute the node }
  258. registers32,registersfpu : longint; { must be longint !!!! }
  259. {$ifdef SUPPORT_MMX}
  260. registersmmx,registerskni : longint;
  261. {$endif SUPPORT_MMX}
  262. resulttype : ttype;
  263. fileinfo : tfileposinfo;
  264. localswitches : tlocalswitches;
  265. {$ifdef extdebug}
  266. maxfirstpasscount,
  267. firstpasscount : longint;
  268. {$endif extdebug}
  269. constructor create(t:tnodetype);
  270. { this constructor is only for creating copies of class }
  271. { the fields are copied by getcopy }
  272. constructor createforcopy;
  273. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  274. destructor destroy;override;
  275. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  276. procedure derefimpl;virtual;
  277. { toggles the flag }
  278. procedure toggleflag(f : tnodeflag);
  279. { the 1.1 code generator may override pass_1 }
  280. { and it need not to implement det_* then }
  281. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  282. { 2.0: runs det_resulttype and det_temp }
  283. function pass_1 : tnode;virtual;abstract;
  284. { dermines the resulttype of the node }
  285. function det_resulttype : tnode;virtual;abstract;
  286. { dermines the number of necessary temp. locations to evaluate
  287. the node }
  288. {$ifdef state_tracking}
  289. { Does optimizations by keeping track of the variable states
  290. in a procedure }
  291. function track_state_pass(exec_known:boolean):boolean;virtual;
  292. {$endif}
  293. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  294. written to (normally the loadnode) as write access. }
  295. procedure mark_write;virtual;
  296. procedure det_temp;virtual;abstract;
  297. procedure pass_2;virtual;abstract;
  298. { comparing of nodes }
  299. function isequal(p : tnode) : boolean;
  300. { to implement comparisation, override this method }
  301. function docompare(p : tnode) : boolean;virtual;
  302. { gets a copy of the node }
  303. function getcopy : tnode;virtual;
  304. procedure insertintolist(l : tnodelist);virtual;
  305. { writes a node for debugging purpose, shouldn't be called }
  306. { direct, because there is no test for nil, use printnode }
  307. { to write a complete tree }
  308. procedure printnodeinfo(var t:text);
  309. procedure printnodedata(var t:text);virtual;
  310. procedure printnodetree(var t:text);virtual;
  311. procedure concattolist(l : tlinkedlist);virtual;
  312. function ischild(p : tnode) : boolean;virtual;
  313. procedure set_file_line(from : tnode);
  314. procedure set_tree_filepos(const filepos : tfileposinfo);
  315. end;
  316. tnodeclass = class of tnode;
  317. tnodeclassarray = array[tnodetype] of tnodeclass;
  318. { this node is the anchestor for all nodes with at least }
  319. { one child, you have to use it if you want to use }
  320. { true- and falselabel }
  321. punarynode = ^tunarynode;
  322. tunarynode = class(tnode)
  323. left : tnode;
  324. constructor create(t:tnodetype;l : tnode);
  325. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  326. destructor destroy;override;
  327. procedure ppuwrite(ppufile:tcompilerppufile);override;
  328. procedure derefimpl;override;
  329. procedure concattolist(l : tlinkedlist);override;
  330. function ischild(p : tnode) : boolean;override;
  331. function docompare(p : tnode) : boolean;override;
  332. function getcopy : tnode;override;
  333. procedure insertintolist(l : tnodelist);override;
  334. procedure left_max;
  335. procedure printnodedata(var t:text);override;
  336. end;
  337. pbinarynode = ^tbinarynode;
  338. tbinarynode = class(tunarynode)
  339. right : tnode;
  340. constructor create(t:tnodetype;l,r : tnode);
  341. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  342. destructor destroy;override;
  343. procedure ppuwrite(ppufile:tcompilerppufile);override;
  344. procedure derefimpl;override;
  345. procedure concattolist(l : tlinkedlist);override;
  346. function ischild(p : tnode) : boolean;override;
  347. function docompare(p : tnode) : boolean;override;
  348. procedure swapleftright;
  349. function getcopy : tnode;override;
  350. procedure insertintolist(l : tnodelist);override;
  351. procedure left_right_max;
  352. procedure printnodedata(var t:text);override;
  353. procedure printnodelist(var t:text);
  354. end;
  355. tbinopnode = class(tbinarynode)
  356. constructor create(t:tnodetype;l,r : tnode);virtual;
  357. function docompare(p : tnode) : boolean;override;
  358. end;
  359. {$ifdef tempregdebug}
  360. type
  361. pptree = ^tnode;
  362. var
  363. curptree: pptree;
  364. {$endif tempregdebug}
  365. var
  366. { array with all class types for tnodes }
  367. nodeclass : tnodeclassarray;
  368. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  369. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  370. const
  371. printnodespacing = ' ';
  372. var
  373. { indention used when writing the tree to the screen }
  374. printnodeindention : string;
  375. procedure printnodeindent;
  376. procedure printnodeunindent;
  377. procedure printnode(var t:text;n:tnode);
  378. implementation
  379. uses
  380. cutils,verbose;
  381. const
  382. ppunodemarker = 255;
  383. {****************************************************************************
  384. Helpers
  385. ****************************************************************************}
  386. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  387. var
  388. b : byte;
  389. t : tnodetype;
  390. begin
  391. { marker }
  392. b:=ppufile.getbyte;
  393. if b<>ppunodemarker then
  394. internalerror(200208151);
  395. { load nodetype }
  396. t:=tnodetype(ppufile.getbyte);
  397. if t>high(tnodetype) then
  398. internalerror(200208152);
  399. if t<>emptynode then
  400. begin
  401. if not assigned(nodeclass[t]) then
  402. internalerror(200208153);
  403. //writeln('load: ',nodetype2str[t]);
  404. { generate node of the correct class }
  405. ppuloadnode:=nodeclass[t].ppuload(t,ppufile);
  406. end
  407. else
  408. ppuloadnode:=nil;
  409. end;
  410. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  411. begin
  412. { marker, read by ppuloadnode }
  413. ppufile.putbyte(ppunodemarker);
  414. { type, read by ppuloadnode }
  415. if assigned(n) then
  416. begin
  417. ppufile.putbyte(byte(n.nodetype));
  418. //writeln('write: ',nodetype2str[n.nodetype]);
  419. n.ppuwrite(ppufile);
  420. end
  421. else
  422. ppufile.putbyte(byte(emptynode));
  423. end;
  424. procedure printnodeindent;
  425. begin
  426. printnodeindention:=printnodeindention+printnodespacing;
  427. end;
  428. procedure printnodeunindent;
  429. begin
  430. delete(printnodeindention,1,length(printnodespacing));
  431. end;
  432. procedure printnode(var t:text;n:tnode);
  433. begin
  434. if assigned(n) then
  435. n.printnodetree(t)
  436. else
  437. writeln(t,printnodeindention,'nil');
  438. end;
  439. {****************************************************************************
  440. TNODE
  441. ****************************************************************************}
  442. constructor tnode.create(t:tnodetype);
  443. begin
  444. inherited create;
  445. nodetype:=t;
  446. blocktype:=block_type;
  447. { updated by firstpass }
  448. expectloc:=LOC_INVALID;
  449. { updated by secondpass }
  450. location.loc:=LOC_INVALID;
  451. { save local info }
  452. fileinfo:=aktfilepos;
  453. localswitches:=aktlocalswitches;
  454. resulttype.reset;
  455. registers32:=0;
  456. registersfpu:=0;
  457. {$ifdef SUPPORT_MMX}
  458. registersmmx:=0;
  459. {$endif SUPPORT_MMX}
  460. {$ifdef EXTDEBUG}
  461. maxfirstpasscount:=0;
  462. firstpasscount:=0;
  463. {$endif EXTDEBUG}
  464. flags:=[];
  465. end;
  466. constructor tnode.createforcopy;
  467. begin
  468. end;
  469. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  470. begin
  471. nodetype:=t;
  472. { tnode fields }
  473. blocktype:=tblock_type(ppufile.getbyte);
  474. ppufile.getposinfo(fileinfo);
  475. ppufile.getsmallset(localswitches);
  476. ppufile.gettype(resulttype);
  477. ppufile.getsmallset(flags);
  478. { updated by firstpass }
  479. expectloc:=LOC_INVALID;
  480. { updated by secondpass }
  481. location.loc:=LOC_INVALID;
  482. registers32:=0;
  483. registersfpu:=0;
  484. {$ifdef SUPPORT_MMX}
  485. registersmmx:=0;
  486. {$endif SUPPORT_MMX}
  487. {$ifdef EXTDEBUG}
  488. maxfirstpasscount:=0;
  489. firstpasscount:=0;
  490. {$endif EXTDEBUG}
  491. end;
  492. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  493. begin
  494. ppufile.putbyte(byte(block_type));
  495. ppufile.putposinfo(fileinfo);
  496. ppufile.putsmallset(localswitches);
  497. ppufile.puttype(resulttype);
  498. ppufile.putsmallset(flags);
  499. end;
  500. procedure tnode.derefimpl;
  501. begin
  502. resulttype.resolve;
  503. end;
  504. procedure tnode.toggleflag(f : tnodeflag);
  505. begin
  506. if f in flags then
  507. exclude(flags,f)
  508. else
  509. include(flags,f);
  510. end;
  511. destructor tnode.destroy;
  512. begin
  513. {$ifdef EXTDEBUG}
  514. if firstpasscount>maxfirstpasscount then
  515. maxfirstpasscount:=firstpasscount;
  516. {$endif EXTDEBUG}
  517. end;
  518. procedure tnode.concattolist(l : tlinkedlist);
  519. begin
  520. end;
  521. function tnode.ischild(p : tnode) : boolean;
  522. begin
  523. ischild:=false;
  524. end;
  525. procedure tnode.mark_write;
  526. begin
  527. {$ifdef EXTDEBUG}
  528. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  529. {$endif EXTDEBUG}
  530. end;
  531. procedure tnode.printnodeinfo(var t:text);
  532. begin
  533. write(t,nodetype2str[nodetype]);
  534. if assigned(resulttype.def) then
  535. write(t,' ,resulttype = "',resulttype.def.gettypename,'"')
  536. else
  537. write(t,' ,resulttype = <nil>');
  538. writeln(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  539. ', loc = ',tcgloc2str[location.loc],
  540. ', intregs = ',registers32,
  541. ', fpuregs = ',registersfpu);
  542. end;
  543. procedure tnode.printnodedata(var t:text);
  544. begin
  545. end;
  546. procedure tnode.printnodetree(var t:text);
  547. begin
  548. write(t,printnodeindention,'(');
  549. printnodeinfo(t);
  550. printnodeindent;
  551. printnodedata(t);
  552. printnodeunindent;
  553. writeln(t,printnodeindention,')');
  554. end;
  555. function tnode.isequal(p : tnode) : boolean;
  556. begin
  557. isequal:=
  558. (not assigned(self) and not assigned(p)) or
  559. (assigned(self) and assigned(p) and
  560. { optimized subclasses have the same nodetype as their }
  561. { superclass (for compatibility), so also check the classtype (JM) }
  562. (p.classtype=classtype) and
  563. (p.nodetype=nodetype) and
  564. (flags*flagsequal=p.flags*flagsequal) and
  565. docompare(p));
  566. end;
  567. {$ifdef state_tracking}
  568. function Tnode.track_state_pass(exec_known:boolean):boolean;
  569. begin
  570. track_state_pass:=false;
  571. end;
  572. {$endif state_tracking}
  573. function tnode.docompare(p : tnode) : boolean;
  574. begin
  575. docompare:=true;
  576. end;
  577. function tnode.getcopy : tnode;
  578. var
  579. p : tnode;
  580. begin
  581. { this is quite tricky because we need a node of the current }
  582. { node type and not one of tnode! }
  583. p:=tnodeclass(classtype).createforcopy;
  584. p.nodetype:=nodetype;
  585. p.location:=location;
  586. p.parent:=parent;
  587. p.flags:=flags;
  588. p.registers32:=registers32;
  589. p.registersfpu:=registersfpu;
  590. {$ifdef SUPPORT_MMX}
  591. p.registersmmx:=registersmmx;
  592. p.registerskni:=registerskni;
  593. {$endif SUPPORT_MMX}
  594. p.resulttype:=resulttype;
  595. p.fileinfo:=fileinfo;
  596. p.localswitches:=localswitches;
  597. {$ifdef extdebug}
  598. p.firstpasscount:=firstpasscount;
  599. {$endif extdebug}
  600. { p.list:=list; }
  601. getcopy:=p;
  602. end;
  603. procedure tnode.insertintolist(l : tnodelist);
  604. begin
  605. end;
  606. procedure tnode.set_file_line(from : tnode);
  607. begin
  608. if assigned(from) then
  609. fileinfo:=from.fileinfo;
  610. end;
  611. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  612. begin
  613. fileinfo:=filepos;
  614. end;
  615. {****************************************************************************
  616. TUNARYNODE
  617. ****************************************************************************}
  618. constructor tunarynode.create(t:tnodetype;l : tnode);
  619. begin
  620. inherited create(t);
  621. left:=l;
  622. end;
  623. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  624. begin
  625. inherited ppuload(t,ppufile);
  626. left:=ppuloadnode(ppufile);
  627. end;
  628. destructor tunarynode.destroy;
  629. begin
  630. left.free;
  631. inherited destroy;
  632. end;
  633. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  634. begin
  635. inherited ppuwrite(ppufile);
  636. ppuwritenode(ppufile,left);
  637. end;
  638. procedure tunarynode.derefimpl;
  639. begin
  640. inherited derefimpl;
  641. if assigned(left) then
  642. left.derefimpl;
  643. end;
  644. function tunarynode.docompare(p : tnode) : boolean;
  645. begin
  646. docompare:=(inherited docompare(p) and
  647. ((left=nil) or left.isequal(tunarynode(p).left))
  648. );
  649. end;
  650. function tunarynode.getcopy : tnode;
  651. var
  652. p : tunarynode;
  653. begin
  654. p:=tunarynode(inherited getcopy);
  655. if assigned(left) then
  656. p.left:=left.getcopy
  657. else
  658. p.left:=nil;
  659. getcopy:=p;
  660. end;
  661. procedure tunarynode.insertintolist(l : tnodelist);
  662. begin
  663. end;
  664. procedure tunarynode.printnodedata(var t:text);
  665. begin
  666. inherited printnodedata(t);
  667. printnode(t,left);
  668. end;
  669. procedure tunarynode.left_max;
  670. begin
  671. registers32:=left.registers32;
  672. registersfpu:=left.registersfpu;
  673. {$ifdef SUPPORT_MMX}
  674. registersmmx:=left.registersmmx;
  675. {$endif SUPPORT_MMX}
  676. end;
  677. procedure tunarynode.concattolist(l : tlinkedlist);
  678. begin
  679. left.parent:=self;
  680. left.concattolist(l);
  681. inherited concattolist(l);
  682. end;
  683. function tunarynode.ischild(p : tnode) : boolean;
  684. begin
  685. ischild:=p=left;
  686. end;
  687. {****************************************************************************
  688. TBINARYNODE
  689. ****************************************************************************}
  690. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  691. begin
  692. inherited create(t,l);
  693. right:=r
  694. end;
  695. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  696. begin
  697. inherited ppuload(t,ppufile);
  698. right:=ppuloadnode(ppufile);
  699. end;
  700. destructor tbinarynode.destroy;
  701. begin
  702. right.free;
  703. inherited destroy;
  704. end;
  705. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  706. begin
  707. inherited ppuwrite(ppufile);
  708. ppuwritenode(ppufile,right);
  709. end;
  710. procedure tbinarynode.derefimpl;
  711. begin
  712. inherited derefimpl;
  713. if assigned(right) then
  714. right.derefimpl;
  715. end;
  716. procedure tbinarynode.concattolist(l : tlinkedlist);
  717. begin
  718. { we could change that depending on the number of }
  719. { required registers }
  720. left.parent:=self;
  721. left.concattolist(l);
  722. left.parent:=self;
  723. left.concattolist(l);
  724. inherited concattolist(l);
  725. end;
  726. function tbinarynode.ischild(p : tnode) : boolean;
  727. begin
  728. ischild:=(p=right);
  729. end;
  730. function tbinarynode.docompare(p : tnode) : boolean;
  731. begin
  732. docompare:=(inherited docompare(p) and
  733. ((right=nil) or right.isequal(tbinarynode(p).right))
  734. );
  735. end;
  736. function tbinarynode.getcopy : tnode;
  737. var
  738. p : tbinarynode;
  739. begin
  740. p:=tbinarynode(inherited getcopy);
  741. if assigned(right) then
  742. p.right:=right.getcopy
  743. else
  744. p.right:=nil;
  745. getcopy:=p;
  746. end;
  747. procedure tbinarynode.insertintolist(l : tnodelist);
  748. begin
  749. end;
  750. procedure tbinarynode.swapleftright;
  751. var
  752. swapp : tnode;
  753. begin
  754. swapp:=right;
  755. right:=left;
  756. left:=swapp;
  757. if nf_swaped in flags then
  758. exclude(flags,nf_swaped)
  759. else
  760. include(flags,nf_swaped);
  761. end;
  762. procedure tbinarynode.left_right_max;
  763. begin
  764. if assigned(left) then
  765. begin
  766. if assigned(right) then
  767. begin
  768. registers32:=max(left.registers32,right.registers32);
  769. registersfpu:=max(left.registersfpu,right.registersfpu);
  770. {$ifdef SUPPORT_MMX}
  771. registersmmx:=max(left.registersmmx,right.registersmmx);
  772. {$endif SUPPORT_MMX}
  773. end
  774. else
  775. begin
  776. registers32:=left.registers32;
  777. registersfpu:=left.registersfpu;
  778. {$ifdef SUPPORT_MMX}
  779. registersmmx:=left.registersmmx;
  780. {$endif SUPPORT_MMX}
  781. end;
  782. end;
  783. end;
  784. procedure tbinarynode.printnodedata(var t:text);
  785. begin
  786. inherited printnodedata(t);
  787. printnode(t,right);
  788. end;
  789. procedure tbinarynode.printnodelist(var t:text);
  790. var
  791. hp : tbinarynode;
  792. begin
  793. hp:=self;
  794. while assigned(hp) do
  795. begin
  796. write(t,printnodeindention,'(');
  797. printnodeindent;
  798. hp.printnodeinfo(t);
  799. printnode(t,hp.left);
  800. printnodeunindent;
  801. writeln(t,printnodeindention,')');
  802. hp:=tbinarynode(hp.right);
  803. end;
  804. end;
  805. {****************************************************************************
  806. TBINOPYNODE
  807. ****************************************************************************}
  808. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  809. begin
  810. inherited create(t,l,r);
  811. end;
  812. function tbinopnode.docompare(p : tnode) : boolean;
  813. begin
  814. docompare:=(inherited docompare(p)) or
  815. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  816. ((nf_swapable in flags) and
  817. left.isequal(tbinopnode(p).right) and
  818. right.isequal(tbinopnode(p).left));
  819. end;
  820. end.
  821. {
  822. $Log$
  823. Revision 1.63 2003-08-10 17:25:23 peter
  824. * fixed some reported bugs
  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. }