node.pas 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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,cgbase,cgutils,
  25. aasmbase,
  26. symtype;
  27. type
  28. pconstset = ^tconstset;
  29. tconstset = set of 0..255;
  30. tnodetype = (
  31. emptynode, {No node (returns nil when loading from ppu)}
  32. addn, {Represents the + operator}
  33. muln, {Represents the * operator}
  34. subn, {Represents the - operator}
  35. divn, {Represents the div operator}
  36. symdifn, {Represents the >< operator}
  37. modn, {Represents the mod operator}
  38. assignn, {Represents an assignment}
  39. loadn, {Represents the use of a variabele}
  40. rangen, {Represents a range (i.e. 0..9)}
  41. ltn, {Represents the < operator}
  42. lten, {Represents the <= operator}
  43. gtn, {Represents the > operator}
  44. gten, {Represents the >= operator}
  45. equaln, {Represents the = operator}
  46. unequaln, {Represents the <> operator}
  47. inn, {Represents the in operator}
  48. orn, {Represents the or operator}
  49. xorn, {Represents the xor operator}
  50. shrn, {Represents the shr operator}
  51. shln, {Represents the shl operator}
  52. slashn, {Represents the / operator}
  53. andn, {Represents the and operator}
  54. subscriptn, {Field in a record/object}
  55. derefn, {Dereferences a pointer}
  56. addrn, {Represents the @ operator}
  57. ordconstn, {Represents an ordinal value}
  58. typeconvn, {Represents type-conversion/typecast}
  59. calln, {Represents a call node}
  60. callparan, {Represents a parameter}
  61. realconstn, {Represents a real value}
  62. unaryminusn, {Represents a sign change (i.e. -2)}
  63. asmn, {Represents an assembler node }
  64. vecn, {Represents array indexing}
  65. pointerconstn, {Represents a pointer constant}
  66. stringconstn, {Represents a string constant}
  67. notn, {Represents the not operator}
  68. inlinen, {Internal procedures (i.e. writeln)}
  69. niln, {Represents the nil pointer}
  70. errorn, {This part of the tree could not be
  71. parsed because of a compiler error}
  72. typen, {A type name. Used for i.e. typeof(obj)}
  73. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  74. setconstn, {A set constant (i.e. [1,2])}
  75. blockn, {A block of statements}
  76. statementn, {One statement in a block of nodes}
  77. ifn, {An if statement}
  78. breakn, {A break statement}
  79. continuen, {A continue statement}
  80. whilerepeatn, {A while or repeat statement}
  81. forn, {A for loop}
  82. exitn, {An exit statement}
  83. withn, {A with statement}
  84. casen, {A case statement}
  85. labeln, {A label}
  86. goton, {A goto statement}
  87. tryexceptn, {A try except block}
  88. raisen, {A raise statement}
  89. tryfinallyn, {A try finally statement}
  90. onn, {For an on statement in exception code}
  91. isn, {Represents the is operator}
  92. asn, {Represents the as typecast}
  93. caretn, {Represents the ^ operator}
  94. starstarn, {Represents the ** operator exponentiation }
  95. arrayconstructorn, {Construction node for [...] parsing}
  96. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  97. tempcreaten, { for temps in the result/firstpass }
  98. temprefn, { references to temps }
  99. tempdeleten, { for temps in the result/firstpass }
  100. addoptn, { added for optimizations where we cannot suppress }
  101. nothingn, {NOP, Do nothing}
  102. loadvmtaddrn, {Load the address of the VMT of a class/object}
  103. guidconstn, {A GUID COM Interface constant }
  104. rttin, {Rtti information so they can be accessed in result/firstpass}
  105. loadparentfpn { Load the framepointer of the parent for nested procedures }
  106. );
  107. const
  108. nodetype2str : array[tnodetype] of string[24] = (
  109. '<emptynode>',
  110. 'addn',
  111. 'muln',
  112. 'subn',
  113. 'divn',
  114. 'symdifn',
  115. 'modn',
  116. 'assignn',
  117. 'loadn',
  118. 'rangen',
  119. 'ltn',
  120. 'lten',
  121. 'gtn',
  122. 'gten',
  123. 'equaln',
  124. 'unequaln',
  125. 'inn',
  126. 'orn',
  127. 'xorn',
  128. 'shrn',
  129. 'shln',
  130. 'slashn',
  131. 'andn',
  132. 'subscriptn',
  133. 'derefn',
  134. 'addrn',
  135. 'ordconstn',
  136. 'typeconvn',
  137. 'calln',
  138. 'callparan',
  139. 'realconstn',
  140. 'unaryminusn',
  141. 'asmn',
  142. 'vecn',
  143. 'pointerconstn',
  144. 'stringconstn',
  145. 'notn',
  146. 'inlinen',
  147. 'niln',
  148. 'errorn',
  149. 'typen',
  150. 'setelementn',
  151. 'setconstn',
  152. 'blockn',
  153. 'statementn',
  154. 'ifn',
  155. 'breakn',
  156. 'continuen',
  157. 'whilerepeatn',
  158. 'forn',
  159. 'exitn',
  160. 'withn',
  161. 'casen',
  162. 'labeln',
  163. 'goton',
  164. 'tryexceptn',
  165. 'raisen',
  166. 'tryfinallyn',
  167. 'onn',
  168. 'isn',
  169. 'asn',
  170. 'caretn',
  171. 'starstarn',
  172. 'arrayconstructn',
  173. 'arrayconstructrangen',
  174. 'tempcreaten',
  175. 'temprefn',
  176. 'tempdeleten',
  177. 'addoptn',
  178. 'nothingn',
  179. 'loadvmtaddrn',
  180. 'guidconstn',
  181. 'rttin',
  182. 'loadparentfpn');
  183. type
  184. { all boolean field of ttree are now collected in flags }
  185. tnodeflag = (
  186. nf_swapable, { tbinop operands can be swaped }
  187. nf_swaped, { tbinop operands are swaped }
  188. nf_error,
  189. { general }
  190. nf_pass1_done,
  191. nf_write, { Node is written to }
  192. nf_isproperty,
  193. { taddrnode }
  194. nf_procvarload,
  195. nf_typedaddr,
  196. { tderefnode }
  197. nf_no_checkpointer,
  198. { tvecnode }
  199. nf_memindex,
  200. nf_memseg,
  201. nf_callunique,
  202. { tloadnode }
  203. nf_absolute,
  204. nf_load_self_pointer,
  205. { taddnode }
  206. nf_is_currency,
  207. nf_has_pointerdiv,
  208. { tassignmentnode }
  209. nf_concat_string,
  210. nf_use_strconcat,
  211. { tarrayconstructnode }
  212. nf_forcevaria,
  213. nf_novariaallowed,
  214. { ttypeconvnode }
  215. nf_explicit,
  216. { tinlinenode }
  217. nf_inlineconst,
  218. { tasmnode }
  219. nf_get_asm_position
  220. );
  221. tnodeflags = set of tnodeflag;
  222. const
  223. { contains the flags which must be equal for the equality }
  224. { of nodes }
  225. flagsequal : tnodeflags = [nf_error];
  226. type
  227. tnodelist = class
  228. end;
  229. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  230. tnode = class
  231. public
  232. { type of this node }
  233. nodetype : tnodetype;
  234. { type of the current code block, general/const/type }
  235. blocktype : tblock_type;
  236. { expected location of the result of this node (pass1) }
  237. expectloc : tcgloc;
  238. { the location of the result of this node (pass2) }
  239. location : tlocation;
  240. { the parent node of this is node }
  241. { this field is set by concattolist }
  242. parent : tnode;
  243. { there are some properties about the node stored }
  244. flags : tnodeflags;
  245. ppuidx : longint;
  246. { the number of registers needed to evalute the node }
  247. registersint,registersfpu,registersmm : longint; { must be longint !!!! }
  248. {$ifdef SUPPORT_MMX}
  249. registersmmx : longint;
  250. {$endif SUPPORT_MMX}
  251. resulttype : ttype;
  252. fileinfo : tfileposinfo;
  253. localswitches : tlocalswitches;
  254. {$ifdef extdebug}
  255. maxfirstpasscount,
  256. firstpasscount : longint;
  257. {$endif extdebug}
  258. constructor create(t:tnodetype);
  259. { this constructor is only for creating copies of class }
  260. { the fields are copied by getcopy }
  261. constructor createforcopy;
  262. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  263. destructor destroy;override;
  264. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  265. procedure buildderefimpl;virtual;
  266. procedure derefimpl;virtual;
  267. procedure derefnode;virtual;
  268. { toggles the flag }
  269. procedure toggleflag(f : tnodeflag);
  270. { the 1.1 code generator may override pass_1 }
  271. { and it need not to implement det_* then }
  272. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  273. { 2.0: runs det_resulttype and det_temp }
  274. function pass_1 : tnode;virtual;abstract;
  275. { dermines the resulttype of the node }
  276. function det_resulttype : tnode;virtual;abstract;
  277. { dermines the number of necessary temp. locations to evaluate
  278. the node }
  279. {$ifdef state_tracking}
  280. { Does optimizations by keeping track of the variable states
  281. in a procedure }
  282. function track_state_pass(exec_known:boolean):boolean;virtual;
  283. {$endif}
  284. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  285. written to (normally the loadnode) as write access. }
  286. procedure mark_write;virtual;
  287. procedure det_temp;virtual;abstract;
  288. procedure pass_2;virtual;abstract;
  289. { comparing of nodes }
  290. function isequal(p : tnode) : boolean;
  291. { to implement comparisation, override this method }
  292. function docompare(p : tnode) : boolean;virtual;
  293. { gets a copy of the node }
  294. function getcopy : tnode;virtual;
  295. procedure insertintolist(l : tnodelist);virtual;
  296. { writes a node for debugging purpose, shouldn't be called }
  297. { direct, because there is no test for nil, use printnode }
  298. { to write a complete tree }
  299. procedure printnodeinfo(var t:text);
  300. procedure printnodedata(var t:text);virtual;
  301. procedure printnodetree(var t:text);virtual;
  302. procedure concattolist(l : tlinkedlist);virtual;
  303. function ischild(p : tnode) : boolean;virtual;
  304. procedure set_file_line(from : tnode);
  305. procedure set_tree_filepos(const filepos : tfileposinfo);
  306. end;
  307. tnodeclass = class of tnode;
  308. tnodeclassarray = array[tnodetype] of tnodeclass;
  309. { this node is the anchestor for all nodes with at least }
  310. { one child, you have to use it if you want to use }
  311. { true- and falselabel }
  312. punarynode = ^tunarynode;
  313. tunarynode = class(tnode)
  314. left : tnode;
  315. constructor create(t:tnodetype;l : tnode);
  316. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  317. destructor destroy;override;
  318. procedure ppuwrite(ppufile:tcompilerppufile);override;
  319. procedure buildderefimpl;override;
  320. procedure derefimpl;override;
  321. procedure derefnode;override;
  322. procedure concattolist(l : tlinkedlist);override;
  323. function ischild(p : tnode) : boolean;override;
  324. function docompare(p : tnode) : boolean;override;
  325. function getcopy : tnode;override;
  326. procedure insertintolist(l : tnodelist);override;
  327. procedure left_max;
  328. procedure printnodedata(var t:text);override;
  329. end;
  330. pbinarynode = ^tbinarynode;
  331. tbinarynode = class(tunarynode)
  332. right : tnode;
  333. constructor create(t:tnodetype;l,r : tnode);
  334. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  335. destructor destroy;override;
  336. procedure ppuwrite(ppufile:tcompilerppufile);override;
  337. procedure buildderefimpl;override;
  338. procedure derefimpl;override;
  339. procedure derefnode;override;
  340. procedure concattolist(l : tlinkedlist);override;
  341. function ischild(p : tnode) : boolean;override;
  342. function docompare(p : tnode) : boolean;override;
  343. procedure swapleftright;
  344. function getcopy : tnode;override;
  345. procedure insertintolist(l : tnodelist);override;
  346. procedure left_right_max;
  347. procedure printnodedata(var t:text);override;
  348. procedure printnodelist(var t:text);
  349. end;
  350. tbinopnode = class(tbinarynode)
  351. constructor create(t:tnodetype;l,r : tnode);virtual;
  352. function docompare(p : tnode) : boolean;override;
  353. end;
  354. var
  355. { array with all class types for tnodes }
  356. nodeclass : tnodeclassarray;
  357. function nodeppuidxget(i:longint):tnode;
  358. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  359. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  360. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  361. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  362. const
  363. printnodespacing = ' ';
  364. var
  365. { indention used when writing the tree to the screen }
  366. printnodeindention : string;
  367. procedure printnodeindent;
  368. procedure printnodeunindent;
  369. procedure printnode(var t:text;n:tnode);
  370. function is_constnode(p : tnode) : boolean;
  371. function is_constintnode(p : tnode) : boolean;
  372. function is_constcharnode(p : tnode) : boolean;
  373. function is_constrealnode(p : tnode) : boolean;
  374. function is_constboolnode(p : tnode) : boolean;
  375. function is_constenumnode(p : tnode) : boolean;
  376. function is_constwidecharnode(p : tnode) : boolean;
  377. implementation
  378. uses
  379. cutils,verbose,ppu,
  380. symconst,
  381. defutil;
  382. const
  383. ppunodemarker = 255;
  384. {****************************************************************************
  385. Helpers
  386. ****************************************************************************}
  387. var
  388. nodeppudata : tdynamicarray;
  389. nodeppuidx : longint;
  390. procedure nodeppuidxcreate;
  391. begin
  392. nodeppudata:=tdynamicarray.create(1024);
  393. nodeppuidx:=0;
  394. end;
  395. procedure nodeppuidxfree;
  396. begin
  397. nodeppudata.free;
  398. nodeppudata:=nil;
  399. end;
  400. procedure nodeppuidxadd(n:tnode);
  401. begin
  402. if n.ppuidx<0 then
  403. internalerror(200311072);
  404. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  405. nodeppudata.write(n,sizeof(pointer));
  406. end;
  407. function nodeppuidxget(i:longint):tnode;
  408. var
  409. l : longint;
  410. begin
  411. if i<0 then
  412. internalerror(200311072);
  413. nodeppudata.seek(i*sizeof(pointer));
  414. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  415. internalerror(200311073);
  416. end;
  417. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  418. var
  419. b : byte;
  420. t : tnodetype;
  421. hppuidx : longint;
  422. begin
  423. { marker }
  424. b:=ppufile.getbyte;
  425. if b<>ppunodemarker then
  426. internalerror(200208151);
  427. { load nodetype }
  428. t:=tnodetype(ppufile.getbyte);
  429. if t>high(tnodetype) then
  430. internalerror(200208152);
  431. if t<>emptynode then
  432. begin
  433. if not assigned(nodeclass[t]) then
  434. internalerror(200208153);
  435. hppuidx:=ppufile.getlongint;
  436. //writeln('load: ',nodetype2str[t]);
  437. { generate node of the correct class }
  438. result:=nodeclass[t].ppuload(t,ppufile);
  439. result.ppuidx:=hppuidx;
  440. nodeppuidxadd(result);
  441. end
  442. else
  443. result:=nil;
  444. end;
  445. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  446. begin
  447. { marker, read by ppuloadnode }
  448. ppufile.putbyte(ppunodemarker);
  449. { type, read by ppuloadnode }
  450. if assigned(n) then
  451. begin
  452. if n.ppuidx=-1 then
  453. internalerror(200311071);
  454. n.ppuidx:=nodeppuidx;
  455. inc(nodeppuidx);
  456. ppufile.putbyte(byte(n.nodetype));
  457. ppufile.putlongint(n.ppuidx);
  458. //writeln('write: ',nodetype2str[n.nodetype]);
  459. n.ppuwrite(ppufile);
  460. end
  461. else
  462. ppufile.putbyte(byte(emptynode));
  463. end;
  464. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  465. begin
  466. if ppufile.readentry<>ibnodetree then
  467. Message(unit_f_ppu_read_error);
  468. nodeppuidxcreate;
  469. result:=ppuloadnode(ppufile);
  470. result.derefnode;
  471. nodeppuidxfree;
  472. end;
  473. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  474. begin
  475. nodeppuidx:=0;
  476. ppuwritenode(ppufile,n);
  477. ppufile.writeentry(ibnodetree);
  478. end;
  479. procedure printnodeindent;
  480. begin
  481. printnodeindention:=printnodeindention+printnodespacing;
  482. end;
  483. procedure printnodeunindent;
  484. begin
  485. delete(printnodeindention,1,length(printnodespacing));
  486. end;
  487. procedure printnode(var t:text;n:tnode);
  488. begin
  489. if assigned(n) then
  490. n.printnodetree(t)
  491. else
  492. writeln(t,printnodeindention,'nil');
  493. end;
  494. function is_constnode(p : tnode) : boolean;
  495. begin
  496. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  497. end;
  498. function is_constintnode(p : tnode) : boolean;
  499. begin
  500. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resulttype.def);
  501. end;
  502. function is_constcharnode(p : tnode) : boolean;
  503. begin
  504. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resulttype.def);
  505. end;
  506. function is_constwidecharnode(p : tnode) : boolean;
  507. begin
  508. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resulttype.def);
  509. end;
  510. function is_constrealnode(p : tnode) : boolean;
  511. begin
  512. is_constrealnode:=(p.nodetype=realconstn);
  513. end;
  514. function is_constboolnode(p : tnode) : boolean;
  515. begin
  516. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resulttype.def);
  517. end;
  518. function is_constenumnode(p : tnode) : boolean;
  519. begin
  520. is_constenumnode:=(p.nodetype=ordconstn) and (p.resulttype.def.deftype=enumdef);
  521. end;
  522. {****************************************************************************
  523. TNODE
  524. ****************************************************************************}
  525. constructor tnode.create(t:tnodetype);
  526. begin
  527. inherited create;
  528. nodetype:=t;
  529. blocktype:=block_type;
  530. { updated by firstpass }
  531. expectloc:=LOC_INVALID;
  532. { updated by secondpass }
  533. location.loc:=LOC_INVALID;
  534. { save local info }
  535. fileinfo:=aktfilepos;
  536. localswitches:=aktlocalswitches;
  537. resulttype.reset;
  538. registersint:=0;
  539. registersfpu:=0;
  540. {$ifdef SUPPORT_MMX}
  541. registersmmx:=0;
  542. {$endif SUPPORT_MMX}
  543. {$ifdef EXTDEBUG}
  544. maxfirstpasscount:=0;
  545. firstpasscount:=0;
  546. {$endif EXTDEBUG}
  547. flags:=[];
  548. ppuidx:=-1;
  549. end;
  550. constructor tnode.createforcopy;
  551. begin
  552. end;
  553. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  554. begin
  555. nodetype:=t;
  556. { tnode fields }
  557. blocktype:=tblock_type(ppufile.getbyte);
  558. ppufile.getposinfo(fileinfo);
  559. ppufile.getsmallset(localswitches);
  560. ppufile.gettype(resulttype);
  561. ppufile.getsmallset(flags);
  562. { updated by firstpass }
  563. expectloc:=LOC_INVALID;
  564. { updated by secondpass }
  565. location.loc:=LOC_INVALID;
  566. registersint:=0;
  567. registersfpu:=0;
  568. {$ifdef SUPPORT_MMX}
  569. registersmmx:=0;
  570. {$endif SUPPORT_MMX}
  571. {$ifdef EXTDEBUG}
  572. maxfirstpasscount:=0;
  573. firstpasscount:=0;
  574. {$endif EXTDEBUG}
  575. ppuidx:=-1;
  576. end;
  577. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  578. begin
  579. ppufile.putbyte(byte(block_type));
  580. ppufile.putposinfo(fileinfo);
  581. ppufile.putsmallset(localswitches);
  582. ppufile.puttype(resulttype);
  583. ppufile.putsmallset(flags);
  584. end;
  585. procedure tnode.buildderefimpl;
  586. begin
  587. resulttype.buildderef;
  588. end;
  589. procedure tnode.derefimpl;
  590. begin
  591. resulttype.resolve;
  592. end;
  593. procedure tnode.derefnode;
  594. begin
  595. end;
  596. procedure tnode.toggleflag(f : tnodeflag);
  597. begin
  598. if f in flags then
  599. exclude(flags,f)
  600. else
  601. include(flags,f);
  602. end;
  603. destructor tnode.destroy;
  604. begin
  605. {$ifdef EXTDEBUG}
  606. if firstpasscount>maxfirstpasscount then
  607. maxfirstpasscount:=firstpasscount;
  608. {$endif EXTDEBUG}
  609. end;
  610. procedure tnode.concattolist(l : tlinkedlist);
  611. begin
  612. end;
  613. function tnode.ischild(p : tnode) : boolean;
  614. begin
  615. ischild:=false;
  616. end;
  617. procedure tnode.mark_write;
  618. begin
  619. {$ifdef EXTDEBUG}
  620. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  621. {$endif EXTDEBUG}
  622. end;
  623. procedure tnode.printnodeinfo(var t:text);
  624. begin
  625. write(t,nodetype2str[nodetype]);
  626. if assigned(resulttype.def) then
  627. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  628. else
  629. write(t,', resulttype = <nil>');
  630. writeln(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  631. ', loc = ',tcgloc2str[location.loc],
  632. ', expectloc = ',tcgloc2str[expectloc],
  633. ', intregs = ',registersint,
  634. ', fpuregs = ',registersfpu);
  635. end;
  636. procedure tnode.printnodedata(var t:text);
  637. begin
  638. end;
  639. procedure tnode.printnodetree(var t:text);
  640. begin
  641. write(t,printnodeindention,'(');
  642. printnodeinfo(t);
  643. printnodeindent;
  644. printnodedata(t);
  645. printnodeunindent;
  646. writeln(t,printnodeindention,')');
  647. end;
  648. function tnode.isequal(p : tnode) : boolean;
  649. begin
  650. isequal:=
  651. (not assigned(self) and not assigned(p)) or
  652. (assigned(self) and assigned(p) and
  653. { optimized subclasses have the same nodetype as their }
  654. { superclass (for compatibility), so also check the classtype (JM) }
  655. (p.classtype=classtype) and
  656. (p.nodetype=nodetype) and
  657. (flags*flagsequal=p.flags*flagsequal) and
  658. docompare(p));
  659. end;
  660. {$ifdef state_tracking}
  661. function Tnode.track_state_pass(exec_known:boolean):boolean;
  662. begin
  663. track_state_pass:=false;
  664. end;
  665. {$endif state_tracking}
  666. function tnode.docompare(p : tnode) : boolean;
  667. begin
  668. docompare:=true;
  669. end;
  670. function tnode.getcopy : tnode;
  671. var
  672. p : tnode;
  673. begin
  674. { this is quite tricky because we need a node of the current }
  675. { node type and not one of tnode! }
  676. p:=tnodeclass(classtype).createforcopy;
  677. p.nodetype:=nodetype;
  678. p.expectloc:=expectloc;
  679. p.location:=location;
  680. p.parent:=parent;
  681. p.flags:=flags;
  682. p.registersint:=registersint;
  683. p.registersfpu:=registersfpu;
  684. {$ifdef SUPPORT_MMX}
  685. p.registersmmx:=registersmmx;
  686. p.registerskni:=registerskni;
  687. {$endif SUPPORT_MMX}
  688. p.resulttype:=resulttype;
  689. p.fileinfo:=fileinfo;
  690. p.localswitches:=localswitches;
  691. {$ifdef extdebug}
  692. p.firstpasscount:=firstpasscount;
  693. {$endif extdebug}
  694. { p.list:=list; }
  695. getcopy:=p;
  696. end;
  697. procedure tnode.insertintolist(l : tnodelist);
  698. begin
  699. end;
  700. procedure tnode.set_file_line(from : tnode);
  701. begin
  702. if assigned(from) then
  703. fileinfo:=from.fileinfo;
  704. end;
  705. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  706. begin
  707. fileinfo:=filepos;
  708. end;
  709. {****************************************************************************
  710. TUNARYNODE
  711. ****************************************************************************}
  712. constructor tunarynode.create(t:tnodetype;l : tnode);
  713. begin
  714. inherited create(t);
  715. left:=l;
  716. end;
  717. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  718. begin
  719. inherited ppuload(t,ppufile);
  720. left:=ppuloadnode(ppufile);
  721. end;
  722. destructor tunarynode.destroy;
  723. begin
  724. left.free;
  725. inherited destroy;
  726. end;
  727. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  728. begin
  729. inherited ppuwrite(ppufile);
  730. ppuwritenode(ppufile,left);
  731. end;
  732. procedure tunarynode.buildderefimpl;
  733. begin
  734. inherited buildderefimpl;
  735. if assigned(left) then
  736. left.buildderefimpl;
  737. end;
  738. procedure tunarynode.derefimpl;
  739. begin
  740. inherited derefimpl;
  741. if assigned(left) then
  742. left.derefimpl;
  743. end;
  744. procedure tunarynode.derefnode;
  745. begin
  746. inherited derefnode;
  747. if assigned(left) then
  748. left.derefnode;
  749. end;
  750. function tunarynode.docompare(p : tnode) : boolean;
  751. begin
  752. docompare:=(inherited docompare(p) and
  753. ((left=nil) or left.isequal(tunarynode(p).left))
  754. );
  755. end;
  756. function tunarynode.getcopy : tnode;
  757. var
  758. p : tunarynode;
  759. begin
  760. p:=tunarynode(inherited getcopy);
  761. if assigned(left) then
  762. p.left:=left.getcopy
  763. else
  764. p.left:=nil;
  765. getcopy:=p;
  766. end;
  767. procedure tunarynode.insertintolist(l : tnodelist);
  768. begin
  769. end;
  770. procedure tunarynode.printnodedata(var t:text);
  771. begin
  772. inherited printnodedata(t);
  773. printnode(t,left);
  774. end;
  775. procedure tunarynode.left_max;
  776. begin
  777. registersint:=left.registersint;
  778. registersfpu:=left.registersfpu;
  779. {$ifdef SUPPORT_MMX}
  780. registersmmx:=left.registersmmx;
  781. {$endif SUPPORT_MMX}
  782. end;
  783. procedure tunarynode.concattolist(l : tlinkedlist);
  784. begin
  785. left.parent:=self;
  786. left.concattolist(l);
  787. inherited concattolist(l);
  788. end;
  789. function tunarynode.ischild(p : tnode) : boolean;
  790. begin
  791. ischild:=p=left;
  792. end;
  793. {****************************************************************************
  794. TBINARYNODE
  795. ****************************************************************************}
  796. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  797. begin
  798. inherited create(t,l);
  799. right:=r
  800. end;
  801. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  802. begin
  803. inherited ppuload(t,ppufile);
  804. right:=ppuloadnode(ppufile);
  805. end;
  806. destructor tbinarynode.destroy;
  807. begin
  808. right.free;
  809. inherited destroy;
  810. end;
  811. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  812. begin
  813. inherited ppuwrite(ppufile);
  814. ppuwritenode(ppufile,right);
  815. end;
  816. procedure tbinarynode.buildderefimpl;
  817. begin
  818. inherited buildderefimpl;
  819. if assigned(right) then
  820. right.buildderefimpl;
  821. end;
  822. procedure tbinarynode.derefimpl;
  823. begin
  824. inherited derefimpl;
  825. if assigned(right) then
  826. right.derefimpl;
  827. end;
  828. procedure tbinarynode.derefnode;
  829. begin
  830. inherited derefnode;
  831. if assigned(right) then
  832. right.derefnode;
  833. end;
  834. procedure tbinarynode.concattolist(l : tlinkedlist);
  835. begin
  836. { we could change that depending on the number of }
  837. { required registers }
  838. left.parent:=self;
  839. left.concattolist(l);
  840. left.parent:=self;
  841. left.concattolist(l);
  842. inherited concattolist(l);
  843. end;
  844. function tbinarynode.ischild(p : tnode) : boolean;
  845. begin
  846. ischild:=(p=right);
  847. end;
  848. function tbinarynode.docompare(p : tnode) : boolean;
  849. begin
  850. docompare:=(inherited docompare(p) and
  851. ((right=nil) or right.isequal(tbinarynode(p).right))
  852. );
  853. end;
  854. function tbinarynode.getcopy : tnode;
  855. var
  856. p : tbinarynode;
  857. begin
  858. p:=tbinarynode(inherited getcopy);
  859. if assigned(right) then
  860. p.right:=right.getcopy
  861. else
  862. p.right:=nil;
  863. getcopy:=p;
  864. end;
  865. procedure tbinarynode.insertintolist(l : tnodelist);
  866. begin
  867. end;
  868. procedure tbinarynode.swapleftright;
  869. var
  870. swapp : tnode;
  871. begin
  872. swapp:=right;
  873. right:=left;
  874. left:=swapp;
  875. if nf_swaped in flags then
  876. exclude(flags,nf_swaped)
  877. else
  878. include(flags,nf_swaped);
  879. end;
  880. procedure tbinarynode.left_right_max;
  881. begin
  882. if assigned(left) then
  883. begin
  884. if assigned(right) then
  885. begin
  886. registersint:=max(left.registersint,right.registersint);
  887. registersfpu:=max(left.registersfpu,right.registersfpu);
  888. {$ifdef SUPPORT_MMX}
  889. registersmmx:=max(left.registersmmx,right.registersmmx);
  890. {$endif SUPPORT_MMX}
  891. end
  892. else
  893. begin
  894. registersint:=left.registersint;
  895. registersfpu:=left.registersfpu;
  896. {$ifdef SUPPORT_MMX}
  897. registersmmx:=left.registersmmx;
  898. {$endif SUPPORT_MMX}
  899. end;
  900. end;
  901. end;
  902. procedure tbinarynode.printnodedata(var t:text);
  903. begin
  904. inherited printnodedata(t);
  905. printnode(t,right);
  906. end;
  907. procedure tbinarynode.printnodelist(var t:text);
  908. var
  909. hp : tbinarynode;
  910. begin
  911. hp:=self;
  912. while assigned(hp) do
  913. begin
  914. write(t,printnodeindention,'(');
  915. printnodeindent;
  916. hp.printnodeinfo(t);
  917. printnode(t,hp.left);
  918. printnodeunindent;
  919. writeln(t,printnodeindention,')');
  920. hp:=tbinarynode(hp.right);
  921. end;
  922. end;
  923. {****************************************************************************
  924. TBINOPYNODE
  925. ****************************************************************************}
  926. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  927. begin
  928. inherited create(t,l,r);
  929. end;
  930. function tbinopnode.docompare(p : tnode) : boolean;
  931. begin
  932. docompare:=(inherited docompare(p)) or
  933. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  934. ((nf_swapable in flags) and
  935. left.isequal(tbinopnode(p).right) and
  936. right.isequal(tbinopnode(p).left));
  937. end;
  938. end.
  939. {
  940. $Log$
  941. Revision 1.89 2004-11-01 17:15:47 peter
  942. * no checkpointer code for dynarr to openarr
  943. Revision 1.88 2004/10/31 21:45:03 peter
  944. * generic tlocation
  945. * move tlocation to cgutils
  946. Revision 1.87 2004/06/20 08:55:29 florian
  947. * logs truncated
  948. Revision 1.86 2004/06/16 20:07:09 florian
  949. * dwarf branch merged
  950. Revision 1.85 2004/05/24 20:39:41 florian
  951. * stricter handling of formal const parameters and IE fixed
  952. Revision 1.84 2004/05/23 18:28:41 peter
  953. * methodpointer is loaded into a temp when it was a calln
  954. Revision 1.83 2004/05/23 15:06:21 peter
  955. * implicit_finally flag must be set in pass1
  956. * add check whether the implicit frame is generated when expected
  957. Revision 1.82 2004/05/20 21:54:33 florian
  958. + <pointer> - <pointer> result is divided by the pointer element size now
  959. this is delphi compatible as well as resulting in the expected result for p1+(p2-p1)
  960. }