2
0

node.pas 37 KB

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