node.pas 37 KB

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