node.pas 38 KB

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