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