2
0

node.pas 38 KB

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