node.pas 34 KB

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