node.pas 38 KB

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