node.pas 33 KB

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