node.pas 38 KB

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