node.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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_typedaddr,
  195. { tderefnode }
  196. nf_no_checkpointer,
  197. { tvecnode }
  198. nf_memindex,
  199. nf_memseg,
  200. nf_callunique,
  201. { tloadnode }
  202. nf_absolute,
  203. nf_is_self,
  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. nf_internal, { no warnings/hints generated }
  217. nf_load_procvar,
  218. { tinlinenode }
  219. nf_inlineconst,
  220. { tasmnode }
  221. nf_get_asm_position,
  222. { tblocknode }
  223. nf_block_with_exit
  224. );
  225. tnodeflags = set of tnodeflag;
  226. const
  227. { contains the flags which must be equal for the equality }
  228. { of nodes }
  229. flagsequal : tnodeflags = [nf_error];
  230. type
  231. tnodelist = class
  232. end;
  233. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  234. tnode = class
  235. public
  236. { type of this node }
  237. nodetype : tnodetype;
  238. { type of the current code block, general/const/type }
  239. blocktype : tblock_type;
  240. { expected location of the result of this node (pass1) }
  241. expectloc : tcgloc;
  242. { the location of the result of this node (pass2) }
  243. location : tlocation;
  244. { the parent node of this is node }
  245. { this field is set by concattolist }
  246. parent : tnode;
  247. { there are some properties about the node stored }
  248. flags : tnodeflags;
  249. ppuidx : longint;
  250. { the number of registers needed to evalute the node }
  251. registersint,registersfpu,registersmm : longint; { must be longint !!!! }
  252. {$ifdef SUPPORT_MMX}
  253. registersmmx : longint;
  254. {$endif SUPPORT_MMX}
  255. resulttype : ttype;
  256. fileinfo : tfileposinfo;
  257. localswitches : tlocalswitches;
  258. {$ifdef extdebug}
  259. maxfirstpasscount,
  260. firstpasscount : longint;
  261. {$endif extdebug}
  262. constructor create(t:tnodetype);
  263. { this constructor is only for creating copies of class }
  264. { the fields are copied by getcopy }
  265. constructor createforcopy;
  266. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  267. destructor destroy;override;
  268. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  269. procedure buildderefimpl;virtual;
  270. procedure derefimpl;virtual;
  271. procedure derefnode;virtual;
  272. { toggles the flag }
  273. procedure toggleflag(f : tnodeflag);
  274. { the 1.1 code generator may override pass_1 }
  275. { and it need not to implement det_* then }
  276. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  277. { 2.0: runs det_resulttype and det_temp }
  278. function pass_1 : tnode;virtual;abstract;
  279. { dermines the resulttype of the node }
  280. function det_resulttype : tnode;virtual;abstract;
  281. { dermines the number of necessary temp. locations to evaluate
  282. the node }
  283. {$ifdef state_tracking}
  284. { Does optimizations by keeping track of the variable states
  285. in a procedure }
  286. function track_state_pass(exec_known:boolean):boolean;virtual;
  287. {$endif}
  288. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  289. written to (normally the loadnode) as write access. }
  290. procedure mark_write;virtual;
  291. procedure det_temp;virtual;abstract;
  292. procedure pass_2;virtual;abstract;
  293. { comparing of nodes }
  294. function isequal(p : tnode) : boolean;
  295. { to implement comparisation, override this method }
  296. function docompare(p : tnode) : boolean;virtual;
  297. { gets a copy of the node }
  298. function getcopy : tnode;virtual;
  299. procedure insertintolist(l : tnodelist);virtual;
  300. { writes a node for debugging purpose, shouldn't be called }
  301. { direct, because there is no test for nil, use printnode }
  302. { to write a complete tree }
  303. procedure printnodeinfo(var t:text);virtual;
  304. procedure printnodedata(var t:text);virtual;
  305. procedure printnodetree(var t:text);virtual;
  306. procedure concattolist(l : tlinkedlist);virtual;
  307. function ischild(p : tnode) : boolean;virtual;
  308. end;
  309. tnodeclass = class of tnode;
  310. tnodeclassarray = array[tnodetype] of tnodeclass;
  311. { this node is the anchestor for all nodes with at least }
  312. { one child, you have to use it if you want to use }
  313. { true- and falselabel }
  314. punarynode = ^tunarynode;
  315. tunarynode = class(tnode)
  316. left : tnode;
  317. constructor create(t:tnodetype;l : tnode);
  318. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  319. destructor destroy;override;
  320. procedure ppuwrite(ppufile:tcompilerppufile);override;
  321. procedure buildderefimpl;override;
  322. procedure derefimpl;override;
  323. procedure derefnode;override;
  324. procedure concattolist(l : tlinkedlist);override;
  325. function ischild(p : tnode) : boolean;override;
  326. function docompare(p : tnode) : boolean;override;
  327. function getcopy : tnode;override;
  328. procedure insertintolist(l : tnodelist);override;
  329. procedure left_max;
  330. procedure printnodedata(var t:text);override;
  331. end;
  332. pbinarynode = ^tbinarynode;
  333. tbinarynode = class(tunarynode)
  334. right : tnode;
  335. constructor create(t:tnodetype;l,r : tnode);
  336. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  337. destructor destroy;override;
  338. procedure ppuwrite(ppufile:tcompilerppufile);override;
  339. procedure buildderefimpl;override;
  340. procedure derefimpl;override;
  341. procedure derefnode;override;
  342. procedure concattolist(l : tlinkedlist);override;
  343. function ischild(p : tnode) : boolean;override;
  344. function docompare(p : tnode) : boolean;override;
  345. procedure swapleftright;
  346. function getcopy : tnode;override;
  347. procedure insertintolist(l : tnodelist);override;
  348. procedure left_right_max;
  349. procedure printnodedata(var t:text);override;
  350. procedure printnodelist(var t:text);
  351. end;
  352. tbinopnode = class(tbinarynode)
  353. constructor create(t:tnodetype;l,r : tnode);virtual;
  354. function docompare(p : tnode) : boolean;override;
  355. end;
  356. var
  357. { array with all class types for tnodes }
  358. nodeclass : tnodeclassarray;
  359. function nodeppuidxget(i:longint):tnode;
  360. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  361. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  362. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  363. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  364. const
  365. printnodespacing = ' ';
  366. var
  367. { indention used when writing the tree to the screen }
  368. printnodeindention : string;
  369. procedure printnodeindent;
  370. procedure printnodeunindent;
  371. procedure printnode(var t:text;n:tnode);
  372. function is_constnode(p : tnode) : boolean;
  373. function is_constintnode(p : tnode) : boolean;
  374. function is_constcharnode(p : tnode) : boolean;
  375. function is_constrealnode(p : tnode) : boolean;
  376. function is_constboolnode(p : tnode) : boolean;
  377. function is_constenumnode(p : tnode) : boolean;
  378. function is_constwidecharnode(p : tnode) : boolean;
  379. implementation
  380. uses
  381. cutils,verbose,ppu,
  382. symconst,
  383. defutil;
  384. const
  385. ppunodemarker = 255;
  386. {****************************************************************************
  387. Helpers
  388. ****************************************************************************}
  389. var
  390. nodeppudata : tdynamicarray;
  391. nodeppuidx : longint;
  392. procedure nodeppuidxcreate;
  393. begin
  394. nodeppudata:=tdynamicarray.create(1024);
  395. nodeppuidx:=0;
  396. end;
  397. procedure nodeppuidxfree;
  398. begin
  399. nodeppudata.free;
  400. nodeppudata:=nil;
  401. end;
  402. procedure nodeppuidxadd(n:tnode);
  403. begin
  404. if n.ppuidx<0 then
  405. internalerror(200311072);
  406. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  407. nodeppudata.write(n,sizeof(pointer));
  408. end;
  409. function nodeppuidxget(i:longint):tnode;
  410. var
  411. l : longint;
  412. begin
  413. if i<0 then
  414. internalerror(200311072);
  415. nodeppudata.seek(i*sizeof(pointer));
  416. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  417. internalerror(200311073);
  418. end;
  419. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  420. var
  421. b : byte;
  422. t : tnodetype;
  423. hppuidx : longint;
  424. begin
  425. { marker }
  426. b:=ppufile.getbyte;
  427. if b<>ppunodemarker then
  428. internalerror(200208151);
  429. { load nodetype }
  430. t:=tnodetype(ppufile.getbyte);
  431. if t>high(tnodetype) then
  432. internalerror(200208152);
  433. if t<>emptynode then
  434. begin
  435. if not assigned(nodeclass[t]) then
  436. internalerror(200208153);
  437. hppuidx:=ppufile.getlongint;
  438. //writeln('load: ',nodetype2str[t]);
  439. { generate node of the correct class }
  440. result:=nodeclass[t].ppuload(t,ppufile);
  441. result.ppuidx:=hppuidx;
  442. nodeppuidxadd(result);
  443. end
  444. else
  445. result:=nil;
  446. end;
  447. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  448. begin
  449. { marker, read by ppuloadnode }
  450. ppufile.putbyte(ppunodemarker);
  451. { type, read by ppuloadnode }
  452. if assigned(n) then
  453. begin
  454. if n.ppuidx=-1 then
  455. internalerror(200311071);
  456. n.ppuidx:=nodeppuidx;
  457. inc(nodeppuidx);
  458. ppufile.putbyte(byte(n.nodetype));
  459. ppufile.putlongint(n.ppuidx);
  460. //writeln('write: ',nodetype2str[n.nodetype]);
  461. n.ppuwrite(ppufile);
  462. end
  463. else
  464. ppufile.putbyte(byte(emptynode));
  465. end;
  466. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  467. begin
  468. if ppufile.readentry<>ibnodetree then
  469. Message(unit_f_ppu_read_error);
  470. nodeppuidxcreate;
  471. result:=ppuloadnode(ppufile);
  472. result.derefnode;
  473. nodeppuidxfree;
  474. end;
  475. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  476. begin
  477. nodeppuidx:=0;
  478. ppuwritenode(ppufile,n);
  479. ppufile.writeentry(ibnodetree);
  480. end;
  481. procedure printnodeindent;
  482. begin
  483. printnodeindention:=printnodeindention+printnodespacing;
  484. end;
  485. procedure printnodeunindent;
  486. begin
  487. delete(printnodeindention,1,length(printnodespacing));
  488. end;
  489. procedure printnode(var t:text;n:tnode);
  490. begin
  491. if assigned(n) then
  492. n.printnodetree(t)
  493. else
  494. writeln(t,printnodeindention,'nil');
  495. end;
  496. function is_constnode(p : tnode) : boolean;
  497. begin
  498. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  499. end;
  500. function is_constintnode(p : tnode) : boolean;
  501. begin
  502. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resulttype.def);
  503. end;
  504. function is_constcharnode(p : tnode) : boolean;
  505. begin
  506. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resulttype.def);
  507. end;
  508. function is_constwidecharnode(p : tnode) : boolean;
  509. begin
  510. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resulttype.def);
  511. end;
  512. function is_constrealnode(p : tnode) : boolean;
  513. begin
  514. is_constrealnode:=(p.nodetype=realconstn);
  515. end;
  516. function is_constboolnode(p : tnode) : boolean;
  517. begin
  518. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resulttype.def);
  519. end;
  520. function is_constenumnode(p : tnode) : boolean;
  521. begin
  522. is_constenumnode:=(p.nodetype=ordconstn) and (p.resulttype.def.deftype=enumdef);
  523. end;
  524. {****************************************************************************
  525. TNODE
  526. ****************************************************************************}
  527. constructor tnode.create(t:tnodetype);
  528. begin
  529. inherited create;
  530. nodetype:=t;
  531. blocktype:=block_type;
  532. { updated by firstpass }
  533. expectloc:=LOC_INVALID;
  534. { updated by secondpass }
  535. location.loc:=LOC_INVALID;
  536. { save local info }
  537. fileinfo:=aktfilepos;
  538. localswitches:=aktlocalswitches;
  539. resulttype.reset;
  540. registersint:=0;
  541. registersfpu:=0;
  542. {$ifdef SUPPORT_MMX}
  543. registersmmx:=0;
  544. {$endif SUPPORT_MMX}
  545. {$ifdef EXTDEBUG}
  546. maxfirstpasscount:=0;
  547. firstpasscount:=0;
  548. {$endif EXTDEBUG}
  549. flags:=[];
  550. ppuidx:=-1;
  551. end;
  552. constructor tnode.createforcopy;
  553. begin
  554. end;
  555. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  556. begin
  557. nodetype:=t;
  558. { tnode fields }
  559. blocktype:=tblock_type(ppufile.getbyte);
  560. ppufile.getposinfo(fileinfo);
  561. ppufile.getsmallset(localswitches);
  562. ppufile.gettype(resulttype);
  563. ppufile.getsmallset(flags);
  564. { updated by firstpass }
  565. expectloc:=LOC_INVALID;
  566. { updated by secondpass }
  567. location.loc:=LOC_INVALID;
  568. registersint:=0;
  569. registersfpu:=0;
  570. {$ifdef SUPPORT_MMX}
  571. registersmmx:=0;
  572. {$endif SUPPORT_MMX}
  573. {$ifdef EXTDEBUG}
  574. maxfirstpasscount:=0;
  575. firstpasscount:=0;
  576. {$endif EXTDEBUG}
  577. ppuidx:=-1;
  578. end;
  579. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  580. begin
  581. ppufile.putbyte(byte(block_type));
  582. ppufile.putposinfo(fileinfo);
  583. ppufile.putsmallset(localswitches);
  584. ppufile.puttype(resulttype);
  585. ppufile.putsmallset(flags);
  586. end;
  587. procedure tnode.buildderefimpl;
  588. begin
  589. resulttype.buildderef;
  590. end;
  591. procedure tnode.derefimpl;
  592. begin
  593. resulttype.resolve;
  594. end;
  595. procedure tnode.derefnode;
  596. begin
  597. end;
  598. procedure tnode.toggleflag(f : tnodeflag);
  599. begin
  600. if f in flags then
  601. exclude(flags,f)
  602. else
  603. include(flags,f);
  604. end;
  605. destructor tnode.destroy;
  606. begin
  607. {$ifdef EXTDEBUG}
  608. if firstpasscount>maxfirstpasscount then
  609. maxfirstpasscount:=firstpasscount;
  610. {$endif EXTDEBUG}
  611. end;
  612. procedure tnode.concattolist(l : tlinkedlist);
  613. begin
  614. end;
  615. function tnode.ischild(p : tnode) : boolean;
  616. begin
  617. ischild:=false;
  618. end;
  619. procedure tnode.mark_write;
  620. begin
  621. {$ifdef EXTDEBUG}
  622. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  623. {$endif EXTDEBUG}
  624. end;
  625. procedure tnode.printnodeinfo(var t:text);
  626. begin
  627. write(t,nodetype2str[nodetype]);
  628. if assigned(resulttype.def) then
  629. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  630. else
  631. write(t,', resulttype = <nil>');
  632. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  633. ', loc = ',tcgloc2str[location.loc],
  634. ', expectloc = ',tcgloc2str[expectloc],
  635. ', intregs = ',registersint,
  636. ', fpuregs = ',registersfpu);
  637. end;
  638. procedure tnode.printnodedata(var t:text);
  639. begin
  640. end;
  641. procedure tnode.printnodetree(var t:text);
  642. begin
  643. write(t,printnodeindention,'(');
  644. printnodeinfo(t);
  645. writeln(t);
  646. printnodeindent;
  647. printnodedata(t);
  648. printnodeunindent;
  649. writeln(t,printnodeindention,')');
  650. end;
  651. function tnode.isequal(p : tnode) : boolean;
  652. begin
  653. isequal:=
  654. (not assigned(self) and not assigned(p)) or
  655. (assigned(self) and assigned(p) and
  656. { optimized subclasses have the same nodetype as their }
  657. { superclass (for compatibility), so also check the classtype (JM) }
  658. (p.classtype=classtype) and
  659. (p.nodetype=nodetype) and
  660. (flags*flagsequal=p.flags*flagsequal) and
  661. docompare(p));
  662. end;
  663. {$ifdef state_tracking}
  664. function Tnode.track_state_pass(exec_known:boolean):boolean;
  665. begin
  666. track_state_pass:=false;
  667. end;
  668. {$endif state_tracking}
  669. function tnode.docompare(p : tnode) : boolean;
  670. begin
  671. docompare:=true;
  672. end;
  673. function tnode.getcopy : tnode;
  674. var
  675. p : tnode;
  676. begin
  677. { this is quite tricky because we need a node of the current }
  678. { node type and not one of tnode! }
  679. p:=tnodeclass(classtype).createforcopy;
  680. p.nodetype:=nodetype;
  681. p.expectloc:=expectloc;
  682. p.location:=location;
  683. p.parent:=parent;
  684. p.flags:=flags;
  685. p.registersint:=registersint;
  686. p.registersfpu:=registersfpu;
  687. {$ifdef SUPPORT_MMX}
  688. p.registersmmx:=registersmmx;
  689. p.registerskni:=registerskni;
  690. {$endif SUPPORT_MMX}
  691. p.resulttype:=resulttype;
  692. p.fileinfo:=fileinfo;
  693. p.localswitches:=localswitches;
  694. {$ifdef extdebug}
  695. p.firstpasscount:=firstpasscount;
  696. {$endif extdebug}
  697. { p.list:=list; }
  698. getcopy:=p;
  699. end;
  700. procedure tnode.insertintolist(l : tnodelist);
  701. begin
  702. end;
  703. {****************************************************************************
  704. TUNARYNODE
  705. ****************************************************************************}
  706. constructor tunarynode.create(t:tnodetype;l : tnode);
  707. begin
  708. inherited create(t);
  709. left:=l;
  710. end;
  711. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  712. begin
  713. inherited ppuload(t,ppufile);
  714. left:=ppuloadnode(ppufile);
  715. end;
  716. destructor tunarynode.destroy;
  717. begin
  718. left.free;
  719. inherited destroy;
  720. end;
  721. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  722. begin
  723. inherited ppuwrite(ppufile);
  724. ppuwritenode(ppufile,left);
  725. end;
  726. procedure tunarynode.buildderefimpl;
  727. begin
  728. inherited buildderefimpl;
  729. if assigned(left) then
  730. left.buildderefimpl;
  731. end;
  732. procedure tunarynode.derefimpl;
  733. begin
  734. inherited derefimpl;
  735. if assigned(left) then
  736. left.derefimpl;
  737. end;
  738. procedure tunarynode.derefnode;
  739. begin
  740. inherited derefnode;
  741. if assigned(left) then
  742. left.derefnode;
  743. end;
  744. function tunarynode.docompare(p : tnode) : boolean;
  745. begin
  746. docompare:=(inherited docompare(p) and
  747. ((left=nil) or left.isequal(tunarynode(p).left))
  748. );
  749. end;
  750. function tunarynode.getcopy : tnode;
  751. var
  752. p : tunarynode;
  753. begin
  754. p:=tunarynode(inherited getcopy);
  755. if assigned(left) then
  756. p.left:=left.getcopy
  757. else
  758. p.left:=nil;
  759. getcopy:=p;
  760. end;
  761. procedure tunarynode.insertintolist(l : tnodelist);
  762. begin
  763. end;
  764. procedure tunarynode.printnodedata(var t:text);
  765. begin
  766. inherited printnodedata(t);
  767. printnode(t,left);
  768. end;
  769. procedure tunarynode.left_max;
  770. begin
  771. registersint:=left.registersint;
  772. registersfpu:=left.registersfpu;
  773. {$ifdef SUPPORT_MMX}
  774. registersmmx:=left.registersmmx;
  775. {$endif SUPPORT_MMX}
  776. end;
  777. procedure tunarynode.concattolist(l : tlinkedlist);
  778. begin
  779. left.parent:=self;
  780. left.concattolist(l);
  781. inherited concattolist(l);
  782. end;
  783. function tunarynode.ischild(p : tnode) : boolean;
  784. begin
  785. ischild:=p=left;
  786. end;
  787. {****************************************************************************
  788. TBINARYNODE
  789. ****************************************************************************}
  790. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  791. begin
  792. inherited create(t,l);
  793. right:=r
  794. end;
  795. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  796. begin
  797. inherited ppuload(t,ppufile);
  798. right:=ppuloadnode(ppufile);
  799. end;
  800. destructor tbinarynode.destroy;
  801. begin
  802. right.free;
  803. inherited destroy;
  804. end;
  805. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  806. begin
  807. inherited ppuwrite(ppufile);
  808. ppuwritenode(ppufile,right);
  809. end;
  810. procedure tbinarynode.buildderefimpl;
  811. begin
  812. inherited buildderefimpl;
  813. if assigned(right) then
  814. right.buildderefimpl;
  815. end;
  816. procedure tbinarynode.derefimpl;
  817. begin
  818. inherited derefimpl;
  819. if assigned(right) then
  820. right.derefimpl;
  821. end;
  822. procedure tbinarynode.derefnode;
  823. begin
  824. inherited derefnode;
  825. if assigned(right) then
  826. right.derefnode;
  827. end;
  828. procedure tbinarynode.concattolist(l : tlinkedlist);
  829. begin
  830. { we could change that depending on the number of }
  831. { required registers }
  832. left.parent:=self;
  833. left.concattolist(l);
  834. left.parent:=self;
  835. left.concattolist(l);
  836. inherited concattolist(l);
  837. end;
  838. function tbinarynode.ischild(p : tnode) : boolean;
  839. begin
  840. ischild:=(p=right);
  841. end;
  842. function tbinarynode.docompare(p : tnode) : boolean;
  843. begin
  844. docompare:=(inherited docompare(p) and
  845. ((right=nil) or right.isequal(tbinarynode(p).right))
  846. );
  847. end;
  848. function tbinarynode.getcopy : tnode;
  849. var
  850. p : tbinarynode;
  851. begin
  852. p:=tbinarynode(inherited getcopy);
  853. if assigned(right) then
  854. p.right:=right.getcopy
  855. else
  856. p.right:=nil;
  857. getcopy:=p;
  858. end;
  859. procedure tbinarynode.insertintolist(l : tnodelist);
  860. begin
  861. end;
  862. procedure tbinarynode.swapleftright;
  863. var
  864. swapp : tnode;
  865. begin
  866. swapp:=right;
  867. right:=left;
  868. left:=swapp;
  869. if nf_swaped in flags then
  870. exclude(flags,nf_swaped)
  871. else
  872. include(flags,nf_swaped);
  873. end;
  874. procedure tbinarynode.left_right_max;
  875. begin
  876. if assigned(left) then
  877. begin
  878. if assigned(right) then
  879. begin
  880. registersint:=max(left.registersint,right.registersint);
  881. registersfpu:=max(left.registersfpu,right.registersfpu);
  882. {$ifdef SUPPORT_MMX}
  883. registersmmx:=max(left.registersmmx,right.registersmmx);
  884. {$endif SUPPORT_MMX}
  885. end
  886. else
  887. begin
  888. registersint:=left.registersint;
  889. registersfpu:=left.registersfpu;
  890. {$ifdef SUPPORT_MMX}
  891. registersmmx:=left.registersmmx;
  892. {$endif SUPPORT_MMX}
  893. end;
  894. end;
  895. end;
  896. procedure tbinarynode.printnodedata(var t:text);
  897. begin
  898. inherited printnodedata(t);
  899. printnode(t,right);
  900. end;
  901. procedure tbinarynode.printnodelist(var t:text);
  902. var
  903. hp : tbinarynode;
  904. begin
  905. hp:=self;
  906. while assigned(hp) do
  907. begin
  908. write(t,printnodeindention,'(');
  909. printnodeindent;
  910. hp.printnodeinfo(t);
  911. printnode(t,hp.left);
  912. writeln(t);
  913. printnodeunindent;
  914. writeln(t,printnodeindention,')');
  915. hp:=tbinarynode(hp.right);
  916. end;
  917. end;
  918. {****************************************************************************
  919. TBINOPYNODE
  920. ****************************************************************************}
  921. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  922. begin
  923. inherited create(t,l,r);
  924. end;
  925. function tbinopnode.docompare(p : tnode) : boolean;
  926. begin
  927. docompare:=(inherited docompare(p)) or
  928. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  929. ((nf_swapable in flags) and
  930. left.isequal(tbinopnode(p).right) and
  931. right.isequal(tbinopnode(p).left));
  932. end;
  933. end.
  934. {
  935. $Log$
  936. Revision 1.95 2005-01-04 16:39:46 peter
  937. * set nf_is_self node flag when self is loaded
  938. Revision 1.94 2005/01/03 17:55:57 florian
  939. + first batch of patches to support tdef.getcopy fully
  940. Revision 1.93 2004/12/26 16:22:01 peter
  941. * fix lineinfo for with blocks
  942. Revision 1.92 2004/12/05 12:28:11 peter
  943. * procvar handling for tp procvar mode fixed
  944. * proc to procvar moved from addrnode to typeconvnode
  945. * inlininginfo is now allocated only for inline routines that
  946. can be inlined, introduced a new flag po_has_inlining_info
  947. Revision 1.91 2004/12/02 19:26:15 peter
  948. * disable pass2inline
  949. Revision 1.90 2004/11/02 12:55:16 peter
  950. * nf_internal flag for internal inserted typeconvs. This will
  951. supress the generation of warning/hints
  952. Revision 1.89 2004/11/01 17:15:47 peter
  953. * no checkpointer code for dynarr to openarr
  954. Revision 1.88 2004/10/31 21:45:03 peter
  955. * generic tlocation
  956. * move tlocation to cgutils
  957. Revision 1.87 2004/06/20 08:55:29 florian
  958. * logs truncated
  959. Revision 1.86 2004/06/16 20:07:09 florian
  960. * dwarf branch merged
  961. Revision 1.85 2004/05/24 20:39:41 florian
  962. * stricter handling of formal const parameters and IE fixed
  963. Revision 1.84 2004/05/23 18:28:41 peter
  964. * methodpointer is loaded into a temp when it was a calln
  965. Revision 1.83 2004/05/23 15:06:21 peter
  966. * implicit_finally flag must be set in pass1
  967. * add check whether the implicit frame is generated when expected
  968. Revision 1.82 2004/05/20 21:54:33 florian
  969. + <pointer> - <pointer> result is divided by the pointer element size now
  970. this is delphi compatible as well as resulting in the expected result for p1+(p2-p1)
  971. }