node.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Basic node handling
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit node;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,
  23. cpubase,cgbase,cgutils,
  24. aasmbase,
  25. symtype,
  26. optbase;
  27. type
  28. tnodetype = (
  29. emptynode, {No node (returns nil when loading from ppu)}
  30. addn, {Represents the + operator}
  31. muln, {Represents the * operator}
  32. subn, {Represents the - operator}
  33. divn, {Represents the div operator}
  34. symdifn, {Represents the >< operator}
  35. modn, {Represents the mod operator}
  36. assignn, {Represents an assignment}
  37. loadn, {Represents the use of a variabele}
  38. rangen, {Represents a range (i.e. 0..9)}
  39. ltn, {Represents the < operator}
  40. lten, {Represents the <= operator}
  41. gtn, {Represents the > operator}
  42. gten, {Represents the >= operator}
  43. equaln, {Represents the = operator}
  44. unequaln, {Represents the <> operator}
  45. inn, {Represents the in operator}
  46. orn, {Represents the or operator}
  47. xorn, {Represents the xor operator}
  48. shrn, {Represents the shr operator}
  49. shln, {Represents the shl operator}
  50. slashn, {Represents the / operator}
  51. andn, {Represents the and operator}
  52. subscriptn, {Field in a record/object}
  53. derefn, {Dereferences a pointer}
  54. addrn, {Represents the @ operator}
  55. ordconstn, {Represents an ordinal value}
  56. typeconvn, {Represents type-conversion/typecast}
  57. calln, {Represents a call node}
  58. callparan, {Represents a parameter}
  59. realconstn, {Represents a real value}
  60. unaryminusn, {Represents a sign change (i.e. -2)}
  61. asmn, {Represents an assembler node }
  62. vecn, {Represents array indexing}
  63. pointerconstn, {Represents a pointer constant}
  64. stringconstn, {Represents a string constant}
  65. notn, {Represents the not operator}
  66. inlinen, {Internal procedures (i.e. writeln)}
  67. niln, {Represents the nil pointer}
  68. errorn, {This part of the tree could not be
  69. parsed because of a compiler error}
  70. typen, {A type name. Used for i.e. typeof(obj)}
  71. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  72. setconstn, {A set constant (i.e. [1,2])}
  73. blockn, {A block of statements}
  74. statementn, {One statement in a block of nodes}
  75. ifn, {An if statement}
  76. breakn, {A break statement}
  77. continuen, {A continue statement}
  78. whilerepeatn, {A while or repeat statement}
  79. forn, {A for loop}
  80. exitn, {An exit statement}
  81. withn, {A with statement}
  82. casen, {A case statement}
  83. labeln, {A label}
  84. goton, {A goto statement}
  85. tryexceptn, {A try except block}
  86. raisen, {A raise statement}
  87. tryfinallyn, {A try finally statement}
  88. onn, {For an on statement in exception code}
  89. isn, {Represents the is operator}
  90. asn, {Represents the as typecast}
  91. starstarn, {Represents the ** operator exponentiation }
  92. arrayconstructorn, {Construction node for [...] parsing}
  93. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  94. tempcreaten, { for temps in the result/firstpass }
  95. temprefn, { references to temps }
  96. tempdeleten, { for temps in the result/firstpass }
  97. addoptn, { added for optimizations where we cannot suppress }
  98. nothingn, { NOP, Do nothing}
  99. loadvmtaddrn, { Load the address of the VMT of a class/object}
  100. guidconstn, { A GUID COM Interface constant }
  101. rttin, { Rtti information so they can be accessed in result/firstpass}
  102. loadparentfpn, { Load the framepointer of the parent for nested procedures }
  103. dataconstn { node storing some binary data }
  104. );
  105. tnodetypeset = set of tnodetype;
  106. pnodetypeset = ^tnodetypeset;
  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. 'starstarn',
  171. 'arrayconstructn',
  172. 'arrayconstructrangen',
  173. 'tempcreaten',
  174. 'temprefn',
  175. 'tempdeleten',
  176. 'addoptn',
  177. 'nothingn',
  178. 'loadvmtaddrn',
  179. 'guidconstn',
  180. 'rttin',
  181. 'loadparentfpn',
  182. 'dataconstn');
  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_swapped, { tbinop operands are swaped }
  188. nf_error,
  189. { general }
  190. nf_pass1_done,
  191. nf_write, { Node is written to }
  192. nf_isproperty,
  193. nf_processing,
  194. { taddrnode }
  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_is_self,
  205. nf_load_self_pointer,
  206. nf_inherited,
  207. { the loadnode is generated internally and a varspez=vs_const should be ignore,
  208. this requires that the parameter is actually passed by value
  209. Be really carefull when using this flag! }
  210. nf_isinternal_ignoreconst,
  211. { taddnode }
  212. nf_is_currency,
  213. nf_has_pointerdiv,
  214. nf_short_bool,
  215. { tassignmentnode }
  216. nf_assign_done_in_right,
  217. { tarrayconstructnode }
  218. nf_forcevaria,
  219. nf_novariaallowed,
  220. { ttypeconvnode, and the first one also treal/ord/pointerconstn }
  221. nf_explicit,
  222. nf_internal, { no warnings/hints generated }
  223. nf_load_procvar,
  224. { tinlinenode }
  225. nf_inlineconst,
  226. { tasmnode }
  227. nf_get_asm_position,
  228. { tblocknode }
  229. nf_block_with_exit
  230. );
  231. tnodeflags = set of tnodeflag;
  232. const
  233. { contains the flags which must be equal for the equality }
  234. { of nodes }
  235. flagsequal : tnodeflags = [nf_error];
  236. type
  237. tnodelist = class
  238. end;
  239. pnode = ^tnode;
  240. { basic class for the intermediated representation fpc uses }
  241. tnode = class
  242. public
  243. { type of this node }
  244. nodetype : tnodetype;
  245. { type of the current code block, general/const/type }
  246. blocktype : tblock_type;
  247. { expected location of the result of this node (pass1) }
  248. expectloc : tcgloc;
  249. { the location of the result of this node (pass2) }
  250. location : tlocation;
  251. { the parent node of this is node }
  252. { this field is set by concattolist }
  253. parent : tnode;
  254. { next node in control flow on the same block level, i.e.
  255. for loop nodes, this is the next node after the end of the loop,
  256. same for if and case, if this field is nil, the next node is the procedure exit,
  257. for the last node in a loop this is set to the loop header
  258. this field is set only for control flow nodes }
  259. successor : tnode;
  260. { there are some properties about the node stored }
  261. flags : tnodeflags;
  262. ppuidx : longint;
  263. { the number of registers needed to evalute the node }
  264. registersint,registersfpu,registersmm : longint; { must be longint !!!! }
  265. {$ifdef SUPPORT_MMX}
  266. registersmmx : longint;
  267. {$endif SUPPORT_MMX}
  268. resultdef : tdef;
  269. resultdefderef : tderef;
  270. fileinfo : tfileposinfo;
  271. localswitches : tlocalswitches;
  272. optinfo : poptinfo;
  273. {$ifdef extdebug}
  274. maxfirstpasscount,
  275. firstpasscount : longint;
  276. {$endif extdebug}
  277. constructor create(t:tnodetype);
  278. { this constructor is only for creating copies of class }
  279. { the fields are copied by getcopy }
  280. constructor createforcopy;
  281. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  282. destructor destroy;override;
  283. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  284. procedure buildderefimpl;virtual;
  285. procedure derefimpl;virtual;
  286. procedure derefnode;virtual;
  287. { toggles the flag }
  288. procedure toggleflag(f : tnodeflag);
  289. { the 1.1 code generator may override pass_1 }
  290. { and it need not to implement det_* then }
  291. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  292. { 2.0: runs pass_typecheck and det_temp }
  293. function pass_1 : tnode;virtual;abstract;
  294. { dermines the resultdef of the node }
  295. function pass_typecheck : tnode;virtual;abstract;
  296. { tries to simplify the node, returns a value <>nil if a simplified
  297. node has been created }
  298. function simplify : tnode;virtual;
  299. {$ifdef state_tracking}
  300. { Does optimizations by keeping track of the variable states
  301. in a procedure }
  302. function track_state_pass(exec_known:boolean):boolean;virtual;
  303. {$endif}
  304. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  305. written to (normally the loadnode) as write access. }
  306. procedure mark_write;virtual;
  307. { dermines the number of necessary temp. locations to evaluate
  308. the node }
  309. procedure det_temp;virtual;abstract;
  310. procedure pass_generate_code;virtual;abstract;
  311. { comparing of nodes }
  312. function isequal(p : tnode) : boolean;
  313. { to implement comparisation, override this method }
  314. function docompare(p : tnode) : boolean;virtual;
  315. { wrapper for getcopy }
  316. function getcopy : tnode;
  317. { does the real copying of a node }
  318. function dogetcopy : tnode;virtual;
  319. procedure insertintolist(l : tnodelist);virtual;
  320. { writes a node for debugging purpose, shouldn't be called }
  321. { direct, because there is no test for nil, use printnode }
  322. { to write a complete tree }
  323. procedure printnodeinfo(var t:text);virtual;
  324. procedure printnodedata(var t:text);virtual;
  325. procedure printnodetree(var t:text);virtual;
  326. procedure concattolist(l : tlinkedlist);virtual;
  327. function ischild(p : tnode) : boolean;virtual;
  328. { ensures that the optimizer info record is allocated }
  329. function allocoptinfo : poptinfo;inline;
  330. end;
  331. tnodeclass = class of tnode;
  332. tnodeclassarray = array[tnodetype] of tnodeclass;
  333. { this node is the anchestor for all nodes with at least }
  334. { one child, you have to use it if you want to use }
  335. { true- and current_procinfo.CurrFalseLabel }
  336. punarynode = ^tunarynode;
  337. tunarynode = class(tnode)
  338. left : tnode;
  339. constructor create(t:tnodetype;l : tnode);
  340. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  341. destructor destroy;override;
  342. procedure ppuwrite(ppufile:tcompilerppufile);override;
  343. procedure buildderefimpl;override;
  344. procedure derefimpl;override;
  345. procedure derefnode;override;
  346. procedure concattolist(l : tlinkedlist);override;
  347. function ischild(p : tnode) : boolean;override;
  348. function docompare(p : tnode) : boolean;override;
  349. function dogetcopy : tnode;override;
  350. procedure insertintolist(l : tnodelist);override;
  351. procedure left_max;
  352. procedure printnodedata(var t:text);override;
  353. end;
  354. pbinarynode = ^tbinarynode;
  355. tbinarynode = class(tunarynode)
  356. right : tnode;
  357. constructor create(t:tnodetype;l,r : tnode);
  358. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  359. destructor destroy;override;
  360. procedure ppuwrite(ppufile:tcompilerppufile);override;
  361. procedure buildderefimpl;override;
  362. procedure derefimpl;override;
  363. procedure derefnode;override;
  364. procedure concattolist(l : tlinkedlist);override;
  365. function ischild(p : tnode) : boolean;override;
  366. function docompare(p : tnode) : boolean;override;
  367. procedure swapleftright;
  368. function dogetcopy : tnode;override;
  369. procedure insertintolist(l : tnodelist);override;
  370. procedure left_right_max;
  371. procedure printnodedata(var t:text);override;
  372. procedure printnodelist(var t:text);
  373. end;
  374. ptertiarynode = ^ttertiarynode;
  375. ttertiarynode = class(tbinarynode)
  376. third : tnode;
  377. constructor create(_t:tnodetype;l,r,t : tnode);
  378. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  379. destructor destroy;override;
  380. procedure ppuwrite(ppufile:tcompilerppufile);override;
  381. procedure buildderefimpl;override;
  382. procedure derefimpl;override;
  383. procedure derefnode;override;
  384. procedure concattolist(l : tlinkedlist);override;
  385. function ischild(p : tnode) : boolean;override;
  386. function docompare(p : tnode) : boolean;override;
  387. function dogetcopy : tnode;override;
  388. procedure insertintolist(l : tnodelist);override;
  389. procedure printnodedata(var t:text);override;
  390. end;
  391. tbinopnode = class(tbinarynode)
  392. constructor create(t:tnodetype;l,r : tnode);virtual;
  393. function docompare(p : tnode) : boolean;override;
  394. end;
  395. var
  396. { array with all class types for tnodes }
  397. nodeclass : tnodeclassarray;
  398. function nodeppuidxget(i:longint):tnode;
  399. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  400. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  401. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  402. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  403. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  404. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  405. const
  406. printnodespacing = ' ';
  407. var
  408. { indention used when writing the tree to the screen }
  409. printnodeindention : string;
  410. procedure printnodeindent;
  411. procedure printnodeunindent;
  412. procedure printnode(var t:text;n:tnode);
  413. function is_constnode(p : tnode) : boolean;
  414. function is_constintnode(p : tnode) : boolean;
  415. function is_constcharnode(p : tnode) : boolean;
  416. function is_constrealnode(p : tnode) : boolean;
  417. function is_constboolnode(p : tnode) : boolean;
  418. function is_constenumnode(p : tnode) : boolean;
  419. function is_constwidecharnode(p : tnode) : boolean;
  420. implementation
  421. uses
  422. cutils,verbose,ppu,
  423. symconst,
  424. nutils,nflw,
  425. defutil;
  426. const
  427. ppunodemarker = 255;
  428. {****************************************************************************
  429. Helpers
  430. ****************************************************************************}
  431. var
  432. nodeppudata : tdynamicarray;
  433. nodeppuidx : longint;
  434. procedure nodeppuidxcreate;
  435. begin
  436. nodeppudata:=tdynamicarray.create(1024);
  437. nodeppuidx:=0;
  438. end;
  439. procedure nodeppuidxfree;
  440. begin
  441. nodeppudata.free;
  442. nodeppudata:=nil;
  443. end;
  444. procedure nodeppuidxadd(n:tnode);
  445. begin
  446. if n.ppuidx<0 then
  447. internalerror(200311072);
  448. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  449. nodeppudata.write(n,sizeof(pointer));
  450. end;
  451. function nodeppuidxget(i:longint):tnode;
  452. begin
  453. if i<0 then
  454. internalerror(200311072);
  455. nodeppudata.seek(i*sizeof(pointer));
  456. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  457. internalerror(200311073);
  458. end;
  459. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  460. var
  461. b : byte;
  462. t : tnodetype;
  463. hppuidx : longint;
  464. begin
  465. { marker }
  466. b:=ppufile.getbyte;
  467. if b<>ppunodemarker then
  468. internalerror(200208151);
  469. { load nodetype }
  470. t:=tnodetype(ppufile.getbyte);
  471. if t>high(tnodetype) then
  472. internalerror(200208152);
  473. if t<>emptynode then
  474. begin
  475. if not assigned(nodeclass[t]) then
  476. internalerror(200208153);
  477. hppuidx:=ppufile.getlongint;
  478. //writeln('load: ',nodetype2str[t]);
  479. { generate node of the correct class }
  480. result:=nodeclass[t].ppuload(t,ppufile);
  481. result.ppuidx:=hppuidx;
  482. nodeppuidxadd(result);
  483. end
  484. else
  485. result:=nil;
  486. end;
  487. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  488. begin
  489. { marker, read by ppuloadnode }
  490. ppufile.putbyte(ppunodemarker);
  491. { type, read by ppuloadnode }
  492. if assigned(n) then
  493. begin
  494. if n.ppuidx=-1 then
  495. internalerror(200311071);
  496. n.ppuidx:=nodeppuidx;
  497. inc(nodeppuidx);
  498. ppufile.putbyte(byte(n.nodetype));
  499. ppufile.putlongint(n.ppuidx);
  500. //writeln('write: ',nodetype2str[n.nodetype]);
  501. n.ppuwrite(ppufile);
  502. end
  503. else
  504. ppufile.putbyte(byte(emptynode));
  505. end;
  506. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  507. begin
  508. { writing of node references isn't implemented yet (FK) }
  509. internalerror(200506181);
  510. end;
  511. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  512. begin
  513. { reading of node references isn't implemented yet (FK) }
  514. internalerror(200506182);
  515. { avoid warning }
  516. result := nil;
  517. end;
  518. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  519. begin
  520. if ppufile.readentry<>ibnodetree then
  521. Message(unit_f_ppu_read_error);
  522. nodeppuidxcreate;
  523. result:=ppuloadnode(ppufile);
  524. result.derefnode;
  525. nodeppuidxfree;
  526. end;
  527. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  528. begin
  529. nodeppuidx:=0;
  530. ppuwritenode(ppufile,n);
  531. ppufile.writeentry(ibnodetree);
  532. end;
  533. procedure printnodeindent;
  534. begin
  535. printnodeindention:=printnodeindention+printnodespacing;
  536. end;
  537. procedure printnodeunindent;
  538. begin
  539. delete(printnodeindention,1,length(printnodespacing));
  540. end;
  541. procedure printnode(var t:text;n:tnode);
  542. begin
  543. if assigned(n) then
  544. n.printnodetree(t)
  545. else
  546. writeln(t,printnodeindention,'nil');
  547. end;
  548. function is_constnode(p : tnode) : boolean;
  549. begin
  550. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  551. end;
  552. function is_constintnode(p : tnode) : boolean;
  553. begin
  554. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resultdef);
  555. end;
  556. function is_constcharnode(p : tnode) : boolean;
  557. begin
  558. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resultdef);
  559. end;
  560. function is_constwidecharnode(p : tnode) : boolean;
  561. begin
  562. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resultdef);
  563. end;
  564. function is_constrealnode(p : tnode) : boolean;
  565. begin
  566. is_constrealnode:=(p.nodetype=realconstn);
  567. end;
  568. function is_constboolnode(p : tnode) : boolean;
  569. begin
  570. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resultdef);
  571. end;
  572. function is_constenumnode(p : tnode) : boolean;
  573. begin
  574. is_constenumnode:=(p.nodetype=ordconstn) and (p.resultdef.typ=enumdef);
  575. end;
  576. {****************************************************************************
  577. TNODE
  578. ****************************************************************************}
  579. constructor tnode.create(t:tnodetype);
  580. begin
  581. inherited create;
  582. nodetype:=t;
  583. blocktype:=block_type;
  584. { updated by firstpass }
  585. expectloc:=LOC_INVALID;
  586. { updated by secondpass }
  587. location.loc:=LOC_INVALID;
  588. { save local info }
  589. fileinfo:=current_filepos;
  590. localswitches:=current_settings.localswitches;
  591. resultdef:=nil;
  592. registersint:=0;
  593. registersfpu:=0;
  594. {$ifdef SUPPORT_MMX}
  595. registersmmx:=0;
  596. {$endif SUPPORT_MMX}
  597. {$ifdef EXTDEBUG}
  598. maxfirstpasscount:=0;
  599. firstpasscount:=0;
  600. {$endif EXTDEBUG}
  601. flags:=[];
  602. ppuidx:=-1;
  603. end;
  604. constructor tnode.createforcopy;
  605. begin
  606. end;
  607. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  608. begin
  609. nodetype:=t;
  610. { tnode fields }
  611. blocktype:=tblock_type(ppufile.getbyte);
  612. ppufile.getposinfo(fileinfo);
  613. ppufile.getsmallset(localswitches);
  614. ppufile.getderef(resultdefderef);
  615. ppufile.getsmallset(flags);
  616. { updated by firstpass }
  617. expectloc:=LOC_INVALID;
  618. { updated by secondpass }
  619. location.loc:=LOC_INVALID;
  620. registersint:=0;
  621. registersfpu:=0;
  622. {$ifdef SUPPORT_MMX}
  623. registersmmx:=0;
  624. {$endif SUPPORT_MMX}
  625. {$ifdef EXTDEBUG}
  626. maxfirstpasscount:=0;
  627. firstpasscount:=0;
  628. {$endif EXTDEBUG}
  629. ppuidx:=-1;
  630. end;
  631. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  632. begin
  633. ppufile.putbyte(byte(block_type));
  634. ppufile.putposinfo(fileinfo);
  635. ppufile.putsmallset(localswitches);
  636. ppufile.putderef(resultdefderef);
  637. ppufile.putsmallset(flags);
  638. end;
  639. procedure tnode.buildderefimpl;
  640. begin
  641. resultdefderef.build(resultdef);
  642. end;
  643. procedure tnode.derefimpl;
  644. begin
  645. resultdef:=tdef(resultdefderef.resolve);
  646. end;
  647. procedure tnode.derefnode;
  648. begin
  649. end;
  650. procedure tnode.toggleflag(f : tnodeflag);
  651. begin
  652. if f in flags then
  653. exclude(flags,f)
  654. else
  655. include(flags,f);
  656. end;
  657. function tnode.simplify : tnode;
  658. begin
  659. result:=nil;
  660. end;
  661. destructor tnode.destroy;
  662. begin
  663. {$ifdef EXTDEBUG}
  664. if firstpasscount>maxfirstpasscount then
  665. maxfirstpasscount:=firstpasscount;
  666. {$endif EXTDEBUG}
  667. if assigned(optinfo) then
  668. dispose(optinfo);
  669. end;
  670. procedure tnode.concattolist(l : tlinkedlist);
  671. begin
  672. end;
  673. function tnode.ischild(p : tnode) : boolean;
  674. begin
  675. ischild:=false;
  676. end;
  677. procedure tnode.mark_write;
  678. begin
  679. {$ifdef EXTDEBUG}
  680. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  681. {$endif EXTDEBUG}
  682. end;
  683. procedure tnode.printnodeinfo(var t:text);
  684. begin
  685. write(t,nodetype2str[nodetype]);
  686. if assigned(resultdef) then
  687. write(t,', resultdef = "',resultdef.GetTypeName,'"')
  688. else
  689. write(t,', resultdef = <nil>');
  690. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  691. ', loc = ',tcgloc2str[location.loc],
  692. ', expectloc = ',tcgloc2str[expectloc],
  693. ', intregs = ',registersint,
  694. ', fpuregs = ',registersfpu);
  695. end;
  696. procedure tnode.printnodedata(var t:text);
  697. begin
  698. end;
  699. procedure tnode.printnodetree(var t:text);
  700. begin
  701. write(t,printnodeindention,'(');
  702. printnodeinfo(t);
  703. writeln(t);
  704. printnodeindent;
  705. printnodedata(t);
  706. printnodeunindent;
  707. writeln(t,printnodeindention,')');
  708. end;
  709. function tnode.isequal(p : tnode) : boolean;
  710. begin
  711. isequal:=
  712. (not assigned(self) and not assigned(p)) or
  713. (assigned(self) and assigned(p) and
  714. { optimized subclasses have the same nodetype as their }
  715. { superclass (for compatibility), so also check the classtype (JM) }
  716. (p.classtype=classtype) and
  717. (p.nodetype=nodetype) and
  718. (flags*flagsequal=p.flags*flagsequal) and
  719. docompare(p));
  720. end;
  721. {$ifdef state_tracking}
  722. function Tnode.track_state_pass(exec_known:boolean):boolean;
  723. begin
  724. track_state_pass:=false;
  725. end;
  726. {$endif state_tracking}
  727. function tnode.docompare(p : tnode) : boolean;
  728. begin
  729. docompare:=true;
  730. end;
  731. function cleanupcopiedto(var n : tnode;arg : pointer) : foreachnoderesult;
  732. begin
  733. result:=fen_true;
  734. if n.nodetype=labeln then
  735. tlabelnode(n).copiedto:=nil;
  736. end;
  737. function tnode.getcopy : tnode;
  738. begin
  739. result:=dogetcopy;
  740. foreachnodestatic(pm_postprocess,self,@cleanupcopiedto,nil);
  741. end;
  742. function tnode.dogetcopy : tnode;
  743. var
  744. p : tnode;
  745. begin
  746. { this is quite tricky because we need a node of the current }
  747. { node type and not one of tnode! }
  748. p:=tnodeclass(classtype).createforcopy;
  749. p.nodetype:=nodetype;
  750. p.expectloc:=expectloc;
  751. p.location:=location;
  752. p.parent:=parent;
  753. p.flags:=flags;
  754. p.registersint:=registersint;
  755. p.registersfpu:=registersfpu;
  756. {$ifdef SUPPORT_MMX}
  757. p.registersmmx:=registersmmx;
  758. p.registersmm:=registersmm;
  759. {$endif SUPPORT_MMX}
  760. p.resultdef:=resultdef;
  761. p.fileinfo:=fileinfo;
  762. p.localswitches:=localswitches;
  763. {$ifdef extdebug}
  764. p.firstpasscount:=firstpasscount;
  765. {$endif extdebug}
  766. { p.list:=list; }
  767. result:=p;
  768. end;
  769. procedure tnode.insertintolist(l : tnodelist);
  770. begin
  771. end;
  772. { ensures that the optimizer info record is allocated }
  773. function tnode.allocoptinfo : poptinfo;inline;
  774. begin
  775. if not(assigned(optinfo)) then
  776. new(optinfo);
  777. result:=optinfo;
  778. end;
  779. {****************************************************************************
  780. TUNARYNODE
  781. ****************************************************************************}
  782. constructor tunarynode.create(t:tnodetype;l : tnode);
  783. begin
  784. inherited create(t);
  785. left:=l;
  786. end;
  787. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  788. begin
  789. inherited ppuload(t,ppufile);
  790. left:=ppuloadnode(ppufile);
  791. end;
  792. destructor tunarynode.destroy;
  793. begin
  794. left.free;
  795. inherited destroy;
  796. end;
  797. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  798. begin
  799. inherited ppuwrite(ppufile);
  800. ppuwritenode(ppufile,left);
  801. end;
  802. procedure tunarynode.buildderefimpl;
  803. begin
  804. inherited buildderefimpl;
  805. if assigned(left) then
  806. left.buildderefimpl;
  807. end;
  808. procedure tunarynode.derefimpl;
  809. begin
  810. inherited derefimpl;
  811. if assigned(left) then
  812. left.derefimpl;
  813. end;
  814. procedure tunarynode.derefnode;
  815. begin
  816. inherited derefnode;
  817. if assigned(left) then
  818. left.derefnode;
  819. end;
  820. function tunarynode.docompare(p : tnode) : boolean;
  821. begin
  822. docompare:=(inherited docompare(p) and
  823. ((left=nil) or left.isequal(tunarynode(p).left))
  824. );
  825. end;
  826. function tunarynode.dogetcopy : tnode;
  827. var
  828. p : tunarynode;
  829. begin
  830. p:=tunarynode(inherited dogetcopy);
  831. if assigned(left) then
  832. p.left:=left.dogetcopy
  833. else
  834. p.left:=nil;
  835. result:=p;
  836. end;
  837. procedure tunarynode.insertintolist(l : tnodelist);
  838. begin
  839. end;
  840. procedure tunarynode.printnodedata(var t:text);
  841. begin
  842. inherited printnodedata(t);
  843. printnode(t,left);
  844. end;
  845. procedure tunarynode.left_max;
  846. begin
  847. registersint:=left.registersint;
  848. registersfpu:=left.registersfpu;
  849. {$ifdef SUPPORT_MMX}
  850. registersmmx:=left.registersmmx;
  851. {$endif SUPPORT_MMX}
  852. end;
  853. procedure tunarynode.concattolist(l : tlinkedlist);
  854. begin
  855. left.parent:=self;
  856. left.concattolist(l);
  857. inherited concattolist(l);
  858. end;
  859. function tunarynode.ischild(p : tnode) : boolean;
  860. begin
  861. ischild:=p=left;
  862. end;
  863. {****************************************************************************
  864. TBINARYNODE
  865. ****************************************************************************}
  866. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  867. begin
  868. inherited create(t,l);
  869. right:=r
  870. end;
  871. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  872. begin
  873. inherited ppuload(t,ppufile);
  874. right:=ppuloadnode(ppufile);
  875. end;
  876. destructor tbinarynode.destroy;
  877. begin
  878. right.free;
  879. inherited destroy;
  880. end;
  881. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  882. begin
  883. inherited ppuwrite(ppufile);
  884. ppuwritenode(ppufile,right);
  885. end;
  886. procedure tbinarynode.buildderefimpl;
  887. begin
  888. inherited buildderefimpl;
  889. if assigned(right) then
  890. right.buildderefimpl;
  891. end;
  892. procedure tbinarynode.derefimpl;
  893. begin
  894. inherited derefimpl;
  895. if assigned(right) then
  896. right.derefimpl;
  897. end;
  898. procedure tbinarynode.derefnode;
  899. begin
  900. inherited derefnode;
  901. if assigned(right) then
  902. right.derefnode;
  903. end;
  904. procedure tbinarynode.concattolist(l : tlinkedlist);
  905. begin
  906. { we could change that depending on the number of }
  907. { required registers }
  908. left.parent:=self;
  909. left.concattolist(l);
  910. left.parent:=self;
  911. left.concattolist(l);
  912. inherited concattolist(l);
  913. end;
  914. function tbinarynode.ischild(p : tnode) : boolean;
  915. begin
  916. ischild:=(p=right);
  917. end;
  918. function tbinarynode.docompare(p : tnode) : boolean;
  919. begin
  920. docompare:=(inherited docompare(p) and
  921. ((right=nil) or right.isequal(tbinarynode(p).right))
  922. );
  923. end;
  924. function tbinarynode.dogetcopy : tnode;
  925. var
  926. p : tbinarynode;
  927. begin
  928. p:=tbinarynode(inherited dogetcopy);
  929. if assigned(right) then
  930. p.right:=right.dogetcopy
  931. else
  932. p.right:=nil;
  933. result:=p;
  934. end;
  935. procedure tbinarynode.insertintolist(l : tnodelist);
  936. begin
  937. end;
  938. procedure tbinarynode.swapleftright;
  939. var
  940. swapp : tnode;
  941. begin
  942. swapp:=right;
  943. right:=left;
  944. left:=swapp;
  945. if nf_swapped in flags then
  946. exclude(flags,nf_swapped)
  947. else
  948. include(flags,nf_swapped);
  949. end;
  950. procedure tbinarynode.left_right_max;
  951. begin
  952. if assigned(left) then
  953. begin
  954. if assigned(right) then
  955. begin
  956. registersint:=max(left.registersint,right.registersint);
  957. registersfpu:=max(left.registersfpu,right.registersfpu);
  958. {$ifdef SUPPORT_MMX}
  959. registersmmx:=max(left.registersmmx,right.registersmmx);
  960. {$endif SUPPORT_MMX}
  961. end
  962. else
  963. begin
  964. registersint:=left.registersint;
  965. registersfpu:=left.registersfpu;
  966. {$ifdef SUPPORT_MMX}
  967. registersmmx:=left.registersmmx;
  968. {$endif SUPPORT_MMX}
  969. end;
  970. end;
  971. end;
  972. procedure tbinarynode.printnodedata(var t:text);
  973. begin
  974. inherited printnodedata(t);
  975. printnode(t,right);
  976. end;
  977. procedure tbinarynode.printnodelist(var t:text);
  978. var
  979. hp : tbinarynode;
  980. begin
  981. hp:=self;
  982. while assigned(hp) do
  983. begin
  984. write(t,printnodeindention,'(');
  985. printnodeindent;
  986. hp.printnodeinfo(t);
  987. writeln(t);
  988. printnode(t,hp.left);
  989. writeln(t);
  990. printnodeunindent;
  991. writeln(t,printnodeindention,')');
  992. hp:=tbinarynode(hp.right);
  993. end;
  994. end;
  995. {****************************************************************************
  996. TTERTIARYNODE
  997. ****************************************************************************}
  998. constructor ttertiarynode.create(_t:tnodetype;l,r,t : tnode);
  999. begin
  1000. inherited create(_t,l,r);
  1001. third:=t;
  1002. end;
  1003. constructor ttertiarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1004. begin
  1005. inherited ppuload(t,ppufile);
  1006. third:=ppuloadnode(ppufile);
  1007. end;
  1008. destructor ttertiarynode.destroy;
  1009. begin
  1010. third.free;
  1011. inherited destroy;
  1012. end;
  1013. procedure ttertiarynode.ppuwrite(ppufile:tcompilerppufile);
  1014. begin
  1015. inherited ppuwrite(ppufile);
  1016. ppuwritenode(ppufile,third);
  1017. end;
  1018. procedure ttertiarynode.buildderefimpl;
  1019. begin
  1020. inherited buildderefimpl;
  1021. if assigned(third) then
  1022. third.buildderefimpl;
  1023. end;
  1024. procedure ttertiarynode.derefimpl;
  1025. begin
  1026. inherited derefimpl;
  1027. if assigned(third) then
  1028. third.derefimpl;
  1029. end;
  1030. procedure ttertiarynode.derefnode;
  1031. begin
  1032. inherited derefnode;
  1033. if assigned(third) then
  1034. third.derefnode;
  1035. end;
  1036. function ttertiarynode.docompare(p : tnode) : boolean;
  1037. begin
  1038. docompare:=(inherited docompare(p) and
  1039. ((third=nil) or third.isequal(ttertiarynode(p).third))
  1040. );
  1041. end;
  1042. function ttertiarynode.dogetcopy : tnode;
  1043. var
  1044. p : ttertiarynode;
  1045. begin
  1046. p:=ttertiarynode(inherited dogetcopy);
  1047. if assigned(third) then
  1048. p.third:=third.dogetcopy
  1049. else
  1050. p.third:=nil;
  1051. result:=p;
  1052. end;
  1053. procedure ttertiarynode.insertintolist(l : tnodelist);
  1054. begin
  1055. end;
  1056. procedure ttertiarynode.printnodedata(var t:text);
  1057. begin
  1058. inherited printnodedata(t);
  1059. printnode(t,third);
  1060. end;
  1061. procedure ttertiarynode.concattolist(l : tlinkedlist);
  1062. begin
  1063. third.parent:=self;
  1064. third.concattolist(l);
  1065. inherited concattolist(l);
  1066. end;
  1067. function ttertiarynode.ischild(p : tnode) : boolean;
  1068. begin
  1069. ischild:=p=third;
  1070. end;
  1071. {****************************************************************************
  1072. TBINOPNODE
  1073. ****************************************************************************}
  1074. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  1075. begin
  1076. inherited create(t,l,r);
  1077. end;
  1078. function tbinopnode.docompare(p : tnode) : boolean;
  1079. begin
  1080. docompare:=(inherited docompare(p)) or
  1081. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  1082. ((nf_swapable in flags) and
  1083. left.isequal(tbinopnode(p).right) and
  1084. right.isequal(tbinopnode(p).left));
  1085. end;
  1086. end.