node.pas 37 KB

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