node.pas 37 KB

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