node.pas 38 KB

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