node.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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. { 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. nf_inherited,
  206. { the loadnode is generated internally and a varspez=vs_const should be ignore,
  207. this requires that the parameter is actually passed by value
  208. Be really carefull when using this flag! }
  209. nf_isinternal_ignoreconst,
  210. { taddnode }
  211. nf_is_currency,
  212. nf_has_pointerdiv,
  213. nf_short_bool,
  214. { tassignmentnode }
  215. nf_assign_done_in_right,
  216. { tarrayconstructnode }
  217. nf_forcevaria,
  218. nf_novariaallowed,
  219. { ttypeconvnode, and the first one also treal/ord/pointerconstn }
  220. nf_explicit,
  221. nf_internal, { no warnings/hints generated }
  222. nf_load_procvar,
  223. { tinlinenode }
  224. nf_inlineconst,
  225. { tasmnode }
  226. nf_get_asm_position,
  227. { tblocknode }
  228. nf_block_with_exit
  229. );
  230. tnodeflags = set of tnodeflag;
  231. const
  232. { contains the flags which must be equal for the equality }
  233. { of nodes }
  234. flagsequal : tnodeflags = [nf_error];
  235. type
  236. tnodelist = class
  237. end;
  238. pnode = ^tnode;
  239. { basic class for the intermediated representation fpc uses }
  240. tnode = class
  241. public
  242. { type of this node }
  243. nodetype : tnodetype;
  244. { type of the current code block, general/const/type }
  245. blocktype : tblock_type;
  246. { expected location of the result of this node (pass1) }
  247. expectloc : tcgloc;
  248. { the location of the result of this node (pass2) }
  249. location : tlocation;
  250. { the parent node of this is node }
  251. { this field is set by concattolist }
  252. parent : tnode;
  253. { next node in control flow on the same block level, i.e.
  254. for loop nodes, this is the next node after the end of the loop,
  255. same for if and case, if this field is nil, the next node is the procedure exit,
  256. for the last node in a loop this is set to the loop header
  257. this field is set only for control flow nodes }
  258. successor : tnode;
  259. { there are some properties about the node stored }
  260. flags : tnodeflags;
  261. ppuidx : longint;
  262. { the number of registers needed to evalute the node }
  263. registersint,registersfpu,registersmm : longint; { must be longint !!!! }
  264. {$ifdef SUPPORT_MMX}
  265. registersmmx : longint;
  266. {$endif SUPPORT_MMX}
  267. resultdef : tdef;
  268. resultdefderef : tderef;
  269. fileinfo : tfileposinfo;
  270. localswitches : tlocalswitches;
  271. optinfo : poptinfo;
  272. {$ifdef extdebug}
  273. maxfirstpasscount,
  274. firstpasscount : longint;
  275. {$endif extdebug}
  276. constructor create(t:tnodetype);
  277. { this constructor is only for creating copies of class }
  278. { the fields are copied by getcopy }
  279. constructor createforcopy;
  280. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  281. destructor destroy;override;
  282. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  283. procedure buildderefimpl;virtual;
  284. procedure derefimpl;virtual;
  285. procedure derefnode;virtual;
  286. { toggles the flag }
  287. procedure toggleflag(f : tnodeflag);
  288. { the 1.1 code generator may override pass_1 }
  289. { and it need not to implement det_* then }
  290. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  291. { 2.0: runs pass_typecheck and det_temp }
  292. function pass_1 : tnode;virtual;abstract;
  293. { dermines the resultdef of the node }
  294. function pass_typecheck : tnode;virtual;abstract;
  295. { tries to simplify the node, returns a value <>nil if a simplified
  296. node has been created }
  297. function simplify : tnode;virtual;
  298. {$ifdef state_tracking}
  299. { Does optimizations by keeping track of the variable states
  300. in a procedure }
  301. function track_state_pass(exec_known:boolean):boolean;virtual;
  302. {$endif}
  303. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  304. written to (normally the loadnode) as write access. }
  305. procedure mark_write;virtual;
  306. { dermines the number of necessary temp. locations to evaluate
  307. the node }
  308. procedure det_temp;virtual;abstract;
  309. procedure pass_generate_code;virtual;abstract;
  310. { comparing of nodes }
  311. function isequal(p : tnode) : boolean;
  312. { to implement comparisation, override this method }
  313. function docompare(p : tnode) : boolean;virtual;
  314. { wrapper for getcopy }
  315. function getcopy : tnode;
  316. { does the real copying of a node }
  317. function dogetcopy : tnode;virtual;
  318. procedure insertintolist(l : tnodelist);virtual;
  319. { writes a node for debugging purpose, shouldn't be called }
  320. { direct, because there is no test for nil, use printnode }
  321. { to write a complete tree }
  322. procedure printnodeinfo(var t:text);virtual;
  323. procedure printnodedata(var t:text);virtual;
  324. procedure printnodetree(var t:text);virtual;
  325. procedure concattolist(l : tlinkedlist);virtual;
  326. function ischild(p : tnode) : boolean;virtual;
  327. end;
  328. tnodeclass = class of tnode;
  329. tnodeclassarray = array[tnodetype] of tnodeclass;
  330. { this node is the anchestor for all nodes with at least }
  331. { one child, you have to use it if you want to use }
  332. { true- and current_procinfo.CurrFalseLabel }
  333. punarynode = ^tunarynode;
  334. tunarynode = class(tnode)
  335. left : tnode;
  336. constructor create(t:tnodetype;l : tnode);
  337. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  338. destructor destroy;override;
  339. procedure ppuwrite(ppufile:tcompilerppufile);override;
  340. procedure buildderefimpl;override;
  341. procedure derefimpl;override;
  342. procedure derefnode;override;
  343. procedure concattolist(l : tlinkedlist);override;
  344. function ischild(p : tnode) : boolean;override;
  345. function docompare(p : tnode) : boolean;override;
  346. function dogetcopy : tnode;override;
  347. procedure insertintolist(l : tnodelist);override;
  348. procedure left_max;
  349. procedure printnodedata(var t:text);override;
  350. end;
  351. pbinarynode = ^tbinarynode;
  352. tbinarynode = class(tunarynode)
  353. right : tnode;
  354. constructor create(t:tnodetype;l,r : tnode);
  355. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  356. destructor destroy;override;
  357. procedure ppuwrite(ppufile:tcompilerppufile);override;
  358. procedure buildderefimpl;override;
  359. procedure derefimpl;override;
  360. procedure derefnode;override;
  361. procedure concattolist(l : tlinkedlist);override;
  362. function ischild(p : tnode) : boolean;override;
  363. function docompare(p : tnode) : boolean;override;
  364. procedure swapleftright;
  365. function dogetcopy : tnode;override;
  366. procedure insertintolist(l : tnodelist);override;
  367. procedure left_right_max;
  368. procedure printnodedata(var t:text);override;
  369. procedure printnodelist(var t:text);
  370. end;
  371. ptertiarynode = ^ttertiarynode;
  372. ttertiarynode = class(tbinarynode)
  373. third : tnode;
  374. constructor create(_t:tnodetype;l,r,t : tnode);
  375. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  376. destructor destroy;override;
  377. procedure ppuwrite(ppufile:tcompilerppufile);override;
  378. procedure buildderefimpl;override;
  379. procedure derefimpl;override;
  380. procedure derefnode;override;
  381. procedure concattolist(l : tlinkedlist);override;
  382. function ischild(p : tnode) : boolean;override;
  383. function docompare(p : tnode) : boolean;override;
  384. function dogetcopy : tnode;override;
  385. procedure insertintolist(l : tnodelist);override;
  386. procedure printnodedata(var t:text);override;
  387. end;
  388. tbinopnode = class(tbinarynode)
  389. constructor create(t:tnodetype;l,r : tnode);virtual;
  390. function docompare(p : tnode) : boolean;override;
  391. end;
  392. var
  393. { array with all class types for tnodes }
  394. nodeclass : tnodeclassarray;
  395. function nodeppuidxget(i:longint):tnode;
  396. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  397. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  398. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  399. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  400. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  401. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  402. const
  403. printnodespacing = ' ';
  404. var
  405. { indention used when writing the tree to the screen }
  406. printnodeindention : string;
  407. procedure printnodeindent;
  408. procedure printnodeunindent;
  409. procedure printnode(var t:text;n:tnode);
  410. function is_constnode(p : tnode) : boolean;
  411. function is_constintnode(p : tnode) : boolean;
  412. function is_constcharnode(p : tnode) : boolean;
  413. function is_constrealnode(p : tnode) : boolean;
  414. function is_constboolnode(p : tnode) : boolean;
  415. function is_constenumnode(p : tnode) : boolean;
  416. function is_constwidecharnode(p : tnode) : boolean;
  417. implementation
  418. uses
  419. cutils,verbose,ppu,
  420. symconst,
  421. nutils,nflw,
  422. defutil;
  423. const
  424. ppunodemarker = 255;
  425. {****************************************************************************
  426. Helpers
  427. ****************************************************************************}
  428. var
  429. nodeppudata : tdynamicarray;
  430. nodeppuidx : longint;
  431. procedure nodeppuidxcreate;
  432. begin
  433. nodeppudata:=tdynamicarray.create(1024);
  434. nodeppuidx:=0;
  435. end;
  436. procedure nodeppuidxfree;
  437. begin
  438. nodeppudata.free;
  439. nodeppudata:=nil;
  440. end;
  441. procedure nodeppuidxadd(n:tnode);
  442. begin
  443. if n.ppuidx<0 then
  444. internalerror(200311072);
  445. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  446. nodeppudata.write(n,sizeof(pointer));
  447. end;
  448. function nodeppuidxget(i:longint):tnode;
  449. begin
  450. if i<0 then
  451. internalerror(200311072);
  452. nodeppudata.seek(i*sizeof(pointer));
  453. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  454. internalerror(200311073);
  455. end;
  456. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  457. var
  458. b : byte;
  459. t : tnodetype;
  460. hppuidx : longint;
  461. begin
  462. { marker }
  463. b:=ppufile.getbyte;
  464. if b<>ppunodemarker then
  465. internalerror(200208151);
  466. { load nodetype }
  467. t:=tnodetype(ppufile.getbyte);
  468. if t>high(tnodetype) then
  469. internalerror(200208152);
  470. if t<>emptynode then
  471. begin
  472. if not assigned(nodeclass[t]) then
  473. internalerror(200208153);
  474. hppuidx:=ppufile.getlongint;
  475. //writeln('load: ',nodetype2str[t]);
  476. { generate node of the correct class }
  477. result:=nodeclass[t].ppuload(t,ppufile);
  478. result.ppuidx:=hppuidx;
  479. nodeppuidxadd(result);
  480. end
  481. else
  482. result:=nil;
  483. end;
  484. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  485. begin
  486. { marker, read by ppuloadnode }
  487. ppufile.putbyte(ppunodemarker);
  488. { type, read by ppuloadnode }
  489. if assigned(n) then
  490. begin
  491. if n.ppuidx=-1 then
  492. internalerror(200311071);
  493. n.ppuidx:=nodeppuidx;
  494. inc(nodeppuidx);
  495. ppufile.putbyte(byte(n.nodetype));
  496. ppufile.putlongint(n.ppuidx);
  497. //writeln('write: ',nodetype2str[n.nodetype]);
  498. n.ppuwrite(ppufile);
  499. end
  500. else
  501. ppufile.putbyte(byte(emptynode));
  502. end;
  503. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  504. begin
  505. { writing of node references isn't implemented yet (FK) }
  506. internalerror(200506181);
  507. end;
  508. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  509. begin
  510. { reading of node references isn't implemented yet (FK) }
  511. internalerror(200506182);
  512. { avoid warning }
  513. result := nil;
  514. end;
  515. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  516. begin
  517. if ppufile.readentry<>ibnodetree then
  518. Message(unit_f_ppu_read_error);
  519. nodeppuidxcreate;
  520. result:=ppuloadnode(ppufile);
  521. result.derefnode;
  522. nodeppuidxfree;
  523. end;
  524. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  525. begin
  526. nodeppuidx:=0;
  527. ppuwritenode(ppufile,n);
  528. ppufile.writeentry(ibnodetree);
  529. end;
  530. procedure printnodeindent;
  531. begin
  532. printnodeindention:=printnodeindention+printnodespacing;
  533. end;
  534. procedure printnodeunindent;
  535. begin
  536. delete(printnodeindention,1,length(printnodespacing));
  537. end;
  538. procedure printnode(var t:text;n:tnode);
  539. begin
  540. if assigned(n) then
  541. n.printnodetree(t)
  542. else
  543. writeln(t,printnodeindention,'nil');
  544. end;
  545. function is_constnode(p : tnode) : boolean;
  546. begin
  547. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  548. end;
  549. function is_constintnode(p : tnode) : boolean;
  550. begin
  551. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resultdef);
  552. end;
  553. function is_constcharnode(p : tnode) : boolean;
  554. begin
  555. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resultdef);
  556. end;
  557. function is_constwidecharnode(p : tnode) : boolean;
  558. begin
  559. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resultdef);
  560. end;
  561. function is_constrealnode(p : tnode) : boolean;
  562. begin
  563. is_constrealnode:=(p.nodetype=realconstn);
  564. end;
  565. function is_constboolnode(p : tnode) : boolean;
  566. begin
  567. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resultdef);
  568. end;
  569. function is_constenumnode(p : tnode) : boolean;
  570. begin
  571. is_constenumnode:=(p.nodetype=ordconstn) and (p.resultdef.typ=enumdef);
  572. end;
  573. {****************************************************************************
  574. TNODE
  575. ****************************************************************************}
  576. constructor tnode.create(t:tnodetype);
  577. begin
  578. inherited create;
  579. nodetype:=t;
  580. blocktype:=block_type;
  581. { updated by firstpass }
  582. expectloc:=LOC_INVALID;
  583. { updated by secondpass }
  584. location.loc:=LOC_INVALID;
  585. { save local info }
  586. fileinfo:=current_filepos;
  587. localswitches:=current_settings.localswitches;
  588. resultdef:=nil;
  589. registersint:=0;
  590. registersfpu:=0;
  591. {$ifdef SUPPORT_MMX}
  592. registersmmx:=0;
  593. {$endif SUPPORT_MMX}
  594. {$ifdef EXTDEBUG}
  595. maxfirstpasscount:=0;
  596. firstpasscount:=0;
  597. {$endif EXTDEBUG}
  598. flags:=[];
  599. ppuidx:=-1;
  600. end;
  601. constructor tnode.createforcopy;
  602. begin
  603. end;
  604. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  605. begin
  606. nodetype:=t;
  607. { tnode fields }
  608. blocktype:=tblock_type(ppufile.getbyte);
  609. ppufile.getposinfo(fileinfo);
  610. ppufile.getsmallset(localswitches);
  611. ppufile.getderef(resultdefderef);
  612. ppufile.getsmallset(flags);
  613. { updated by firstpass }
  614. expectloc:=LOC_INVALID;
  615. { updated by secondpass }
  616. location.loc:=LOC_INVALID;
  617. registersint:=0;
  618. registersfpu:=0;
  619. {$ifdef SUPPORT_MMX}
  620. registersmmx:=0;
  621. {$endif SUPPORT_MMX}
  622. {$ifdef EXTDEBUG}
  623. maxfirstpasscount:=0;
  624. firstpasscount:=0;
  625. {$endif EXTDEBUG}
  626. ppuidx:=-1;
  627. end;
  628. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  629. begin
  630. ppufile.putbyte(byte(block_type));
  631. ppufile.putposinfo(fileinfo);
  632. ppufile.putsmallset(localswitches);
  633. ppufile.putderef(resultdefderef);
  634. ppufile.putsmallset(flags);
  635. end;
  636. procedure tnode.buildderefimpl;
  637. begin
  638. resultdefderef.build(resultdef);
  639. end;
  640. procedure tnode.derefimpl;
  641. begin
  642. resultdef:=tdef(resultdefderef.resolve);
  643. end;
  644. procedure tnode.derefnode;
  645. begin
  646. end;
  647. procedure tnode.toggleflag(f : tnodeflag);
  648. begin
  649. if f in flags then
  650. exclude(flags,f)
  651. else
  652. include(flags,f);
  653. end;
  654. function tnode.simplify : tnode;
  655. begin
  656. result:=nil;
  657. end;
  658. destructor tnode.destroy;
  659. begin
  660. {$ifdef EXTDEBUG}
  661. if firstpasscount>maxfirstpasscount then
  662. maxfirstpasscount:=firstpasscount;
  663. {$endif EXTDEBUG}
  664. end;
  665. procedure tnode.concattolist(l : tlinkedlist);
  666. begin
  667. end;
  668. function tnode.ischild(p : tnode) : boolean;
  669. begin
  670. ischild:=false;
  671. end;
  672. procedure tnode.mark_write;
  673. begin
  674. {$ifdef EXTDEBUG}
  675. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  676. {$endif EXTDEBUG}
  677. end;
  678. procedure tnode.printnodeinfo(var t:text);
  679. begin
  680. write(t,nodetype2str[nodetype]);
  681. if assigned(resultdef) then
  682. write(t,', resultdef = "',resultdef.GetTypeName,'"')
  683. else
  684. write(t,', resultdef = <nil>');
  685. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  686. ', loc = ',tcgloc2str[location.loc],
  687. ', expectloc = ',tcgloc2str[expectloc],
  688. ', intregs = ',registersint,
  689. ', fpuregs = ',registersfpu);
  690. end;
  691. procedure tnode.printnodedata(var t:text);
  692. begin
  693. end;
  694. procedure tnode.printnodetree(var t:text);
  695. begin
  696. write(t,printnodeindention,'(');
  697. printnodeinfo(t);
  698. writeln(t);
  699. printnodeindent;
  700. printnodedata(t);
  701. printnodeunindent;
  702. writeln(t,printnodeindention,')');
  703. end;
  704. function tnode.isequal(p : tnode) : boolean;
  705. begin
  706. isequal:=
  707. (not assigned(self) and not assigned(p)) or
  708. (assigned(self) and assigned(p) and
  709. { optimized subclasses have the same nodetype as their }
  710. { superclass (for compatibility), so also check the classtype (JM) }
  711. (p.classtype=classtype) and
  712. (p.nodetype=nodetype) and
  713. (flags*flagsequal=p.flags*flagsequal) and
  714. docompare(p));
  715. end;
  716. {$ifdef state_tracking}
  717. function Tnode.track_state_pass(exec_known:boolean):boolean;
  718. begin
  719. track_state_pass:=false;
  720. end;
  721. {$endif state_tracking}
  722. function tnode.docompare(p : tnode) : boolean;
  723. begin
  724. docompare:=true;
  725. end;
  726. function cleanupcopiedto(var n : tnode;arg : pointer) : foreachnoderesult;
  727. begin
  728. result:=fen_true;
  729. if n.nodetype=labeln then
  730. tlabelnode(n).copiedto:=nil;
  731. end;
  732. function tnode.getcopy : tnode;
  733. begin
  734. result:=dogetcopy;
  735. foreachnodestatic(pm_postprocess,self,@cleanupcopiedto,nil);
  736. end;
  737. function tnode.dogetcopy : tnode;
  738. var
  739. p : tnode;
  740. begin
  741. { this is quite tricky because we need a node of the current }
  742. { node type and not one of tnode! }
  743. p:=tnodeclass(classtype).createforcopy;
  744. p.nodetype:=nodetype;
  745. p.expectloc:=expectloc;
  746. p.location:=location;
  747. p.parent:=parent;
  748. p.flags:=flags;
  749. p.registersint:=registersint;
  750. p.registersfpu:=registersfpu;
  751. {$ifdef SUPPORT_MMX}
  752. p.registersmmx:=registersmmx;
  753. p.registersmm:=registersmm;
  754. {$endif SUPPORT_MMX}
  755. p.resultdef:=resultdef;
  756. p.fileinfo:=fileinfo;
  757. p.localswitches:=localswitches;
  758. {$ifdef extdebug}
  759. p.firstpasscount:=firstpasscount;
  760. {$endif extdebug}
  761. { p.list:=list; }
  762. result:=p;
  763. end;
  764. procedure tnode.insertintolist(l : tnodelist);
  765. begin
  766. end;
  767. {****************************************************************************
  768. TUNARYNODE
  769. ****************************************************************************}
  770. constructor tunarynode.create(t:tnodetype;l : tnode);
  771. begin
  772. inherited create(t);
  773. left:=l;
  774. end;
  775. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  776. begin
  777. inherited ppuload(t,ppufile);
  778. left:=ppuloadnode(ppufile);
  779. end;
  780. destructor tunarynode.destroy;
  781. begin
  782. left.free;
  783. inherited destroy;
  784. end;
  785. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  786. begin
  787. inherited ppuwrite(ppufile);
  788. ppuwritenode(ppufile,left);
  789. end;
  790. procedure tunarynode.buildderefimpl;
  791. begin
  792. inherited buildderefimpl;
  793. if assigned(left) then
  794. left.buildderefimpl;
  795. end;
  796. procedure tunarynode.derefimpl;
  797. begin
  798. inherited derefimpl;
  799. if assigned(left) then
  800. left.derefimpl;
  801. end;
  802. procedure tunarynode.derefnode;
  803. begin
  804. inherited derefnode;
  805. if assigned(left) then
  806. left.derefnode;
  807. end;
  808. function tunarynode.docompare(p : tnode) : boolean;
  809. begin
  810. docompare:=(inherited docompare(p) and
  811. ((left=nil) or left.isequal(tunarynode(p).left))
  812. );
  813. end;
  814. function tunarynode.dogetcopy : tnode;
  815. var
  816. p : tunarynode;
  817. begin
  818. p:=tunarynode(inherited dogetcopy);
  819. if assigned(left) then
  820. p.left:=left.dogetcopy
  821. else
  822. p.left:=nil;
  823. result:=p;
  824. end;
  825. procedure tunarynode.insertintolist(l : tnodelist);
  826. begin
  827. end;
  828. procedure tunarynode.printnodedata(var t:text);
  829. begin
  830. inherited printnodedata(t);
  831. printnode(t,left);
  832. end;
  833. procedure tunarynode.left_max;
  834. begin
  835. registersint:=left.registersint;
  836. registersfpu:=left.registersfpu;
  837. {$ifdef SUPPORT_MMX}
  838. registersmmx:=left.registersmmx;
  839. {$endif SUPPORT_MMX}
  840. end;
  841. procedure tunarynode.concattolist(l : tlinkedlist);
  842. begin
  843. left.parent:=self;
  844. left.concattolist(l);
  845. inherited concattolist(l);
  846. end;
  847. function tunarynode.ischild(p : tnode) : boolean;
  848. begin
  849. ischild:=p=left;
  850. end;
  851. {****************************************************************************
  852. TBINARYNODE
  853. ****************************************************************************}
  854. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  855. begin
  856. inherited create(t,l);
  857. right:=r
  858. end;
  859. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  860. begin
  861. inherited ppuload(t,ppufile);
  862. right:=ppuloadnode(ppufile);
  863. end;
  864. destructor tbinarynode.destroy;
  865. begin
  866. right.free;
  867. inherited destroy;
  868. end;
  869. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  870. begin
  871. inherited ppuwrite(ppufile);
  872. ppuwritenode(ppufile,right);
  873. end;
  874. procedure tbinarynode.buildderefimpl;
  875. begin
  876. inherited buildderefimpl;
  877. if assigned(right) then
  878. right.buildderefimpl;
  879. end;
  880. procedure tbinarynode.derefimpl;
  881. begin
  882. inherited derefimpl;
  883. if assigned(right) then
  884. right.derefimpl;
  885. end;
  886. procedure tbinarynode.derefnode;
  887. begin
  888. inherited derefnode;
  889. if assigned(right) then
  890. right.derefnode;
  891. end;
  892. procedure tbinarynode.concattolist(l : tlinkedlist);
  893. begin
  894. { we could change that depending on the number of }
  895. { required registers }
  896. left.parent:=self;
  897. left.concattolist(l);
  898. left.parent:=self;
  899. left.concattolist(l);
  900. inherited concattolist(l);
  901. end;
  902. function tbinarynode.ischild(p : tnode) : boolean;
  903. begin
  904. ischild:=(p=right);
  905. end;
  906. function tbinarynode.docompare(p : tnode) : boolean;
  907. begin
  908. docompare:=(inherited docompare(p) and
  909. ((right=nil) or right.isequal(tbinarynode(p).right))
  910. );
  911. end;
  912. function tbinarynode.dogetcopy : tnode;
  913. var
  914. p : tbinarynode;
  915. begin
  916. p:=tbinarynode(inherited dogetcopy);
  917. if assigned(right) then
  918. p.right:=right.dogetcopy
  919. else
  920. p.right:=nil;
  921. result:=p;
  922. end;
  923. procedure tbinarynode.insertintolist(l : tnodelist);
  924. begin
  925. end;
  926. procedure tbinarynode.swapleftright;
  927. var
  928. swapp : tnode;
  929. begin
  930. swapp:=right;
  931. right:=left;
  932. left:=swapp;
  933. if nf_swapped in flags then
  934. exclude(flags,nf_swapped)
  935. else
  936. include(flags,nf_swapped);
  937. end;
  938. procedure tbinarynode.left_right_max;
  939. begin
  940. if assigned(left) then
  941. begin
  942. if assigned(right) then
  943. begin
  944. registersint:=max(left.registersint,right.registersint);
  945. registersfpu:=max(left.registersfpu,right.registersfpu);
  946. {$ifdef SUPPORT_MMX}
  947. registersmmx:=max(left.registersmmx,right.registersmmx);
  948. {$endif SUPPORT_MMX}
  949. end
  950. else
  951. begin
  952. registersint:=left.registersint;
  953. registersfpu:=left.registersfpu;
  954. {$ifdef SUPPORT_MMX}
  955. registersmmx:=left.registersmmx;
  956. {$endif SUPPORT_MMX}
  957. end;
  958. end;
  959. end;
  960. procedure tbinarynode.printnodedata(var t:text);
  961. begin
  962. inherited printnodedata(t);
  963. printnode(t,right);
  964. end;
  965. procedure tbinarynode.printnodelist(var t:text);
  966. var
  967. hp : tbinarynode;
  968. begin
  969. hp:=self;
  970. while assigned(hp) do
  971. begin
  972. write(t,printnodeindention,'(');
  973. printnodeindent;
  974. hp.printnodeinfo(t);
  975. writeln(t);
  976. printnode(t,hp.left);
  977. writeln(t);
  978. printnodeunindent;
  979. writeln(t,printnodeindention,')');
  980. hp:=tbinarynode(hp.right);
  981. end;
  982. end;
  983. {****************************************************************************
  984. TTERTIARYNODE
  985. ****************************************************************************}
  986. constructor ttertiarynode.create(_t:tnodetype;l,r,t : tnode);
  987. begin
  988. inherited create(_t,l,r);
  989. third:=t;
  990. end;
  991. constructor ttertiarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  992. begin
  993. inherited ppuload(t,ppufile);
  994. third:=ppuloadnode(ppufile);
  995. end;
  996. destructor ttertiarynode.destroy;
  997. begin
  998. third.free;
  999. inherited destroy;
  1000. end;
  1001. procedure ttertiarynode.ppuwrite(ppufile:tcompilerppufile);
  1002. begin
  1003. inherited ppuwrite(ppufile);
  1004. ppuwritenode(ppufile,third);
  1005. end;
  1006. procedure ttertiarynode.buildderefimpl;
  1007. begin
  1008. inherited buildderefimpl;
  1009. if assigned(third) then
  1010. third.buildderefimpl;
  1011. end;
  1012. procedure ttertiarynode.derefimpl;
  1013. begin
  1014. inherited derefimpl;
  1015. if assigned(third) then
  1016. third.derefimpl;
  1017. end;
  1018. procedure ttertiarynode.derefnode;
  1019. begin
  1020. inherited derefnode;
  1021. if assigned(third) then
  1022. third.derefnode;
  1023. end;
  1024. function ttertiarynode.docompare(p : tnode) : boolean;
  1025. begin
  1026. docompare:=(inherited docompare(p) and
  1027. ((third=nil) or third.isequal(ttertiarynode(p).third))
  1028. );
  1029. end;
  1030. function ttertiarynode.dogetcopy : tnode;
  1031. var
  1032. p : ttertiarynode;
  1033. begin
  1034. p:=ttertiarynode(inherited dogetcopy);
  1035. if assigned(third) then
  1036. p.third:=third.dogetcopy
  1037. else
  1038. p.third:=nil;
  1039. result:=p;
  1040. end;
  1041. procedure ttertiarynode.insertintolist(l : tnodelist);
  1042. begin
  1043. end;
  1044. procedure ttertiarynode.printnodedata(var t:text);
  1045. begin
  1046. inherited printnodedata(t);
  1047. printnode(t,third);
  1048. end;
  1049. procedure ttertiarynode.concattolist(l : tlinkedlist);
  1050. begin
  1051. third.parent:=self;
  1052. third.concattolist(l);
  1053. inherited concattolist(l);
  1054. end;
  1055. function ttertiarynode.ischild(p : tnode) : boolean;
  1056. begin
  1057. ischild:=p=third;
  1058. end;
  1059. {****************************************************************************
  1060. TBINOPNODE
  1061. ****************************************************************************}
  1062. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  1063. begin
  1064. inherited create(t,l,r);
  1065. end;
  1066. function tbinopnode.docompare(p : tnode) : boolean;
  1067. begin
  1068. docompare:=(inherited docompare(p)) or
  1069. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  1070. ((nf_swapable in flags) and
  1071. left.isequal(tbinopnode(p).right) and
  1072. right.isequal(tbinopnode(p).left));
  1073. end;
  1074. end.