node.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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. );
  105. tnodetypeset = set of tnodetype;
  106. pnodetypeset = ^tnodetypeset;
  107. const
  108. nodetype2str : array[tnodetype] of string[24] = (
  109. '<emptynode>',
  110. 'addn',
  111. 'muln',
  112. 'subn',
  113. 'divn',
  114. 'symdifn',
  115. 'modn',
  116. 'assignn',
  117. 'loadn',
  118. 'rangen',
  119. 'ltn',
  120. 'lten',
  121. 'gtn',
  122. 'gten',
  123. 'equaln',
  124. 'unequaln',
  125. 'inn',
  126. 'orn',
  127. 'xorn',
  128. 'shrn',
  129. 'shln',
  130. 'slashn',
  131. 'andn',
  132. 'subscriptn',
  133. 'derefn',
  134. 'addrn',
  135. 'ordconstn',
  136. 'typeconvn',
  137. 'calln',
  138. 'callparan',
  139. 'realconstn',
  140. 'unaryminusn',
  141. 'asmn',
  142. 'vecn',
  143. 'pointerconstn',
  144. 'stringconstn',
  145. 'notn',
  146. 'inlinen',
  147. 'niln',
  148. 'errorn',
  149. 'typen',
  150. 'setelementn',
  151. 'setconstn',
  152. 'blockn',
  153. 'statementn',
  154. 'ifn',
  155. 'breakn',
  156. 'continuen',
  157. 'whilerepeatn',
  158. 'forn',
  159. 'exitn',
  160. 'withn',
  161. 'casen',
  162. 'labeln',
  163. 'goton',
  164. 'tryexceptn',
  165. 'raisen',
  166. 'tryfinallyn',
  167. 'onn',
  168. 'isn',
  169. 'asn',
  170. 'starstarn',
  171. 'arrayconstructn',
  172. 'arrayconstructrangen',
  173. 'tempcreaten',
  174. 'temprefn',
  175. 'tempdeleten',
  176. 'addoptn',
  177. 'nothingn',
  178. 'loadvmtaddrn',
  179. 'guidconstn',
  180. 'rttin',
  181. 'loadparentfpn',
  182. 'dataconstn');
  183. type
  184. { all boolean field of ttree are now collected in flags }
  185. tnodeflag = (
  186. nf_swapable, { tbinop operands can be swaped }
  187. nf_swapped, { tbinop operands are swaped }
  188. nf_error,
  189. { general }
  190. nf_pass1_done,
  191. nf_write, { Node is written to }
  192. nf_modify, { Node is modified }
  193. nf_is_funcret,
  194. nf_isproperty,
  195. nf_processing,
  196. { taddrnode }
  197. nf_typedaddr,
  198. { tderefnode }
  199. nf_no_checkpointer,
  200. { tvecnode }
  201. nf_memindex,
  202. nf_memseg,
  203. nf_callunique,
  204. { tloadnode }
  205. nf_absolute,
  206. nf_is_self,
  207. nf_load_self_pointer,
  208. nf_inherited,
  209. { the loadnode is generated internally and a varspez=vs_const should be ignore,
  210. this requires that the parameter is actually passed by value
  211. Be really carefull when using this flag! }
  212. nf_isinternal_ignoreconst,
  213. { taddnode }
  214. nf_is_currency,
  215. nf_has_pointerdiv,
  216. nf_short_bool,
  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 32 elements in this type, and a set of this
  236. type is written to the PPU. So before adding any more 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 : 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. implementation
  424. uses
  425. cutils,verbose,ppu,comphook,
  426. symconst,
  427. nutils,nflw,
  428. defutil;
  429. const
  430. ppunodemarker = 255;
  431. {****************************************************************************
  432. Helpers
  433. ****************************************************************************}
  434. var
  435. nodeppulist : TFPObjectList;
  436. nodeppuidx : longint;
  437. procedure nodeppuidxcreate;
  438. begin
  439. nodeppulist:=TFPObjectList.Create(false);
  440. nodeppuidx:=0;
  441. end;
  442. procedure nodeppuidxresolve;
  443. var
  444. i : longint;
  445. n : tnode;
  446. begin
  447. for i:=0 to nodeppulist.count-1 do
  448. begin
  449. n:=tnode(nodeppulist[i]);
  450. if assigned(n) then
  451. n.resolveppuidx;
  452. end;
  453. end;
  454. procedure nodeppuidxfree;
  455. begin
  456. nodeppulist.free;
  457. nodeppulist:=nil;
  458. nodeppuidx:=0;
  459. end;
  460. procedure nodeppuidxadd(n:tnode);
  461. var
  462. i : longint;
  463. begin
  464. i:=n.ppuidx;
  465. if i<=0 then
  466. internalerror(200311072);
  467. if i>=nodeppulist.capacity then
  468. nodeppulist.capacity:=((i div 1024)+1)*1024;
  469. if i>=nodeppulist.count then
  470. nodeppulist.count:=i+1;
  471. nodeppulist[i]:=n;
  472. end;
  473. function nodeppuidxget(i:longint):tnode;
  474. begin
  475. if i<=0 then
  476. internalerror(200311073);
  477. result:=tnode(nodeppulist[i]);
  478. end;
  479. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  480. var
  481. b : byte;
  482. t : tnodetype;
  483. hppuidx : longint;
  484. begin
  485. { marker }
  486. b:=ppufile.getbyte;
  487. if b<>ppunodemarker then
  488. internalerror(200208151);
  489. { load nodetype }
  490. t:=tnodetype(ppufile.getbyte);
  491. if t>high(tnodetype) then
  492. internalerror(200208152);
  493. if t<>emptynode then
  494. begin
  495. if not assigned(nodeclass[t]) then
  496. internalerror(200208153);
  497. hppuidx:=ppufile.getlongint;
  498. //writeln('load: ',nodetype2str[t]);
  499. { generate node of the correct class }
  500. result:=nodeclass[t].ppuload(t,ppufile);
  501. result.fppuidx:=hppuidx;
  502. nodeppuidxadd(result);
  503. end
  504. else
  505. result:=nil;
  506. end;
  507. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  508. begin
  509. { marker, read by ppuloadnode }
  510. ppufile.putbyte(ppunodemarker);
  511. { type, read by ppuloadnode }
  512. if assigned(n) then
  513. begin
  514. ppufile.putbyte(byte(n.nodetype));
  515. ppufile.putlongint(n.ppuidx);
  516. //writeln('write: ',nodetype2str[n.nodetype]);
  517. n.ppuwrite(ppufile);
  518. end
  519. else
  520. ppufile.putbyte(byte(emptynode));
  521. end;
  522. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  523. begin
  524. if ppufile.readentry<>ibnodetree then
  525. Message(unit_f_ppu_read_error);
  526. nodeppuidxcreate;
  527. result:=ppuloadnode(ppufile);
  528. nodeppuidxresolve;
  529. nodeppuidxfree;
  530. end;
  531. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  532. begin
  533. nodeppuidxcreate;
  534. ppuwritenode(ppufile,n);
  535. ppufile.writeentry(ibnodetree);
  536. nodeppuidxfree;
  537. end;
  538. procedure printnodeindent;
  539. begin
  540. printnodeindention:=printnodeindention+printnodespacing;
  541. end;
  542. procedure printnodeunindent;
  543. begin
  544. delete(printnodeindention,1,length(printnodespacing));
  545. end;
  546. procedure printnode(var t:text;n:tnode);
  547. begin
  548. if assigned(n) then
  549. n.printnodetree(t)
  550. else
  551. writeln(t,printnodeindention,'nil');
  552. end;
  553. procedure printnode(n:tnode);
  554. begin
  555. printnode(output,n);
  556. end;
  557. function is_constnode(p : tnode) : boolean;
  558. begin
  559. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  560. end;
  561. function is_constintnode(p : tnode) : boolean;
  562. begin
  563. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resultdef);
  564. end;
  565. function is_constcharnode(p : tnode) : boolean;
  566. begin
  567. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resultdef);
  568. end;
  569. function is_constwidecharnode(p : tnode) : boolean;
  570. begin
  571. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resultdef);
  572. end;
  573. function is_constrealnode(p : tnode) : boolean;
  574. begin
  575. is_constrealnode:=(p.nodetype=realconstn);
  576. end;
  577. function is_constboolnode(p : tnode) : boolean;
  578. begin
  579. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resultdef);
  580. end;
  581. function is_constenumnode(p : tnode) : boolean;
  582. begin
  583. is_constenumnode:=(p.nodetype=ordconstn) and (p.resultdef.typ=enumdef);
  584. end;
  585. function is_constpointernode(p : tnode) : boolean;
  586. begin
  587. is_constpointernode:=(p.nodetype=pointerconstn);
  588. end;
  589. {****************************************************************************
  590. TNODE
  591. ****************************************************************************}
  592. constructor tnode.create(t:tnodetype);
  593. begin
  594. inherited create;
  595. nodetype:=t;
  596. blocktype:=block_type;
  597. { updated by firstpass }
  598. expectloc:=LOC_INVALID;
  599. { updated by secondpass }
  600. location.loc:=LOC_INVALID;
  601. { save local info }
  602. fileinfo:=current_filepos;
  603. localswitches:=current_settings.localswitches;
  604. verbosity:=status.verbosity;
  605. resultdef:=nil;
  606. flags:=[];
  607. end;
  608. constructor tnode.createforcopy;
  609. begin
  610. end;
  611. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  612. begin
  613. nodetype:=t;
  614. { tnode fields }
  615. blocktype:=tblock_type(ppufile.getbyte);
  616. ppufile.getposinfo(fileinfo);
  617. ppufile.getsmallset(localswitches);
  618. verbosity:=ppufile.getlongint;
  619. ppufile.getderef(resultdefderef);
  620. ppufile.getsmallset(flags);
  621. { updated by firstpass }
  622. expectloc:=LOC_INVALID;
  623. { updated by secondpass }
  624. location.loc:=LOC_INVALID;
  625. end;
  626. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  627. begin
  628. ppufile.putbyte(byte(block_type));
  629. ppufile.putposinfo(fileinfo);
  630. ppufile.putsmallset(localswitches);
  631. ppufile.putlongint(verbosity);
  632. ppufile.putderef(resultdefderef);
  633. ppufile.putsmallset(flags);
  634. end;
  635. function tnode.getppuidx:longint;
  636. begin
  637. if fppuidx=0 then
  638. begin
  639. inc(nodeppuidx);
  640. fppuidx:=nodeppuidx;
  641. end;
  642. result:=fppuidx;
  643. end;
  644. procedure tnode.resolveppuidx;
  645. begin
  646. end;
  647. procedure tnode.buildderefimpl;
  648. begin
  649. resultdefderef.build(resultdef);
  650. end;
  651. procedure tnode.derefimpl;
  652. begin
  653. resultdef:=tdef(resultdefderef.resolve);
  654. end;
  655. procedure tnode.toggleflag(f : tnodeflag);
  656. begin
  657. if f in flags then
  658. exclude(flags,f)
  659. else
  660. include(flags,f);
  661. end;
  662. function tnode.simplify : tnode;
  663. begin
  664. result:=nil;
  665. end;
  666. destructor tnode.destroy;
  667. begin
  668. if assigned(optinfo) then
  669. dispose(optinfo);
  670. end;
  671. procedure tnode.concattolist(l : tlinkedlist);
  672. begin
  673. end;
  674. function tnode.ischild(p : tnode) : boolean;
  675. begin
  676. ischild:=false;
  677. end;
  678. procedure tnode.mark_write;
  679. begin
  680. {$ifdef EXTDEBUG}
  681. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  682. {$endif EXTDEBUG}
  683. end;
  684. procedure tnode.printnodeinfo(var t:text);
  685. begin
  686. write(t,nodetype2str[nodetype]);
  687. if assigned(resultdef) then
  688. write(t,', resultdef = "',resultdef.GetTypeName,'"')
  689. else
  690. write(t,', resultdef = <nil>');
  691. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  692. ', loc = ',tcgloc2str[location.loc],
  693. ', expectloc = ',tcgloc2str[expectloc]);
  694. end;
  695. procedure tnode.printnodedata(var t:text);
  696. begin
  697. end;
  698. procedure tnode.printnodetree(var t:text);
  699. begin
  700. write(t,printnodeindention,'(');
  701. printnodeinfo(t);
  702. writeln(t);
  703. printnodeindent;
  704. printnodedata(t);
  705. printnodeunindent;
  706. writeln(t,printnodeindention,')');
  707. end;
  708. function tnode.isequal(p : tnode) : boolean;
  709. begin
  710. isequal:=
  711. (not assigned(self) and not assigned(p)) or
  712. (assigned(self) and assigned(p) and
  713. { optimized subclasses have the same nodetype as their }
  714. { superclass (for compatibility), so also check the classtype (JM) }
  715. (p.classtype=classtype) and
  716. (p.nodetype=nodetype) and
  717. (flags*flagsequal=p.flags*flagsequal) and
  718. docompare(p));
  719. end;
  720. {$ifdef state_tracking}
  721. function Tnode.track_state_pass(exec_known:boolean):boolean;
  722. begin
  723. track_state_pass:=false;
  724. end;
  725. {$endif state_tracking}
  726. function tnode.docompare(p : tnode) : boolean;
  727. begin
  728. docompare:=true;
  729. end;
  730. function cleanupcopiedto(var n : tnode;arg : pointer) : foreachnoderesult;
  731. begin
  732. result:=fen_true;
  733. if n.nodetype=labeln then
  734. tlabelnode(n).copiedto:=nil;
  735. end;
  736. function tnode.getcopy : tnode;
  737. begin
  738. result:=dogetcopy;
  739. foreachnodestatic(pm_postprocess,self,@cleanupcopiedto,nil);
  740. end;
  741. function tnode.dogetcopy : tnode;
  742. var
  743. p : tnode;
  744. begin
  745. { this is quite tricky because we need a node of the current }
  746. { node type and not one of tnode! }
  747. p:=tnodeclass(classtype).createforcopy;
  748. p.nodetype:=nodetype;
  749. p.expectloc:=expectloc;
  750. p.location:=location;
  751. p.parent:=parent;
  752. p.flags:=flags;
  753. p.resultdef:=resultdef;
  754. p.fileinfo:=fileinfo;
  755. p.localswitches:=localswitches;
  756. p.verbosity:=verbosity;
  757. { p.list:=list; }
  758. result:=p;
  759. end;
  760. function tnode.actualtargetnode: tnode;
  761. begin
  762. result:=self;
  763. end;
  764. procedure tnode.insertintolist(l : tnodelist);
  765. begin
  766. end;
  767. { ensures that the optimizer info record is allocated }
  768. function tnode.allocoptinfo : poptinfo;inline;
  769. begin
  770. if not(assigned(optinfo)) then
  771. new(optinfo);
  772. result:=optinfo;
  773. end;
  774. {****************************************************************************
  775. TUNARYNODE
  776. ****************************************************************************}
  777. constructor tunarynode.create(t:tnodetype;l : tnode);
  778. begin
  779. inherited create(t);
  780. left:=l;
  781. end;
  782. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  783. begin
  784. inherited ppuload(t,ppufile);
  785. left:=ppuloadnode(ppufile);
  786. end;
  787. destructor tunarynode.destroy;
  788. begin
  789. left.free;
  790. inherited destroy;
  791. end;
  792. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  793. begin
  794. inherited ppuwrite(ppufile);
  795. ppuwritenode(ppufile,left);
  796. end;
  797. procedure tunarynode.buildderefimpl;
  798. begin
  799. inherited buildderefimpl;
  800. if assigned(left) then
  801. left.buildderefimpl;
  802. end;
  803. procedure tunarynode.derefimpl;
  804. begin
  805. inherited derefimpl;
  806. if assigned(left) then
  807. left.derefimpl;
  808. end;
  809. function tunarynode.docompare(p : tnode) : boolean;
  810. begin
  811. docompare:=(inherited docompare(p) and
  812. ((left=nil) or left.isequal(tunarynode(p).left))
  813. );
  814. end;
  815. function tunarynode.dogetcopy : tnode;
  816. var
  817. p : tunarynode;
  818. begin
  819. p:=tunarynode(inherited dogetcopy);
  820. if assigned(left) then
  821. p.left:=left.dogetcopy
  822. else
  823. p.left:=nil;
  824. result:=p;
  825. end;
  826. procedure tunarynode.insertintolist(l : tnodelist);
  827. begin
  828. end;
  829. procedure tunarynode.printnodedata(var t:text);
  830. begin
  831. inherited printnodedata(t);
  832. printnode(t,left);
  833. end;
  834. procedure tunarynode.concattolist(l : tlinkedlist);
  835. begin
  836. left.parent:=self;
  837. left.concattolist(l);
  838. inherited concattolist(l);
  839. end;
  840. function tunarynode.ischild(p : tnode) : boolean;
  841. begin
  842. ischild:=p=left;
  843. end;
  844. {****************************************************************************
  845. TBINARYNODE
  846. ****************************************************************************}
  847. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  848. begin
  849. inherited create(t,l);
  850. right:=r
  851. end;
  852. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  853. begin
  854. inherited ppuload(t,ppufile);
  855. right:=ppuloadnode(ppufile);
  856. end;
  857. destructor tbinarynode.destroy;
  858. begin
  859. right.free;
  860. inherited destroy;
  861. end;
  862. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  863. begin
  864. inherited ppuwrite(ppufile);
  865. ppuwritenode(ppufile,right);
  866. end;
  867. procedure tbinarynode.buildderefimpl;
  868. begin
  869. inherited buildderefimpl;
  870. if assigned(right) then
  871. right.buildderefimpl;
  872. end;
  873. procedure tbinarynode.derefimpl;
  874. begin
  875. inherited derefimpl;
  876. if assigned(right) then
  877. right.derefimpl;
  878. end;
  879. procedure tbinarynode.concattolist(l : tlinkedlist);
  880. begin
  881. { we could change that depending on the number of }
  882. { required registers }
  883. left.parent:=self;
  884. left.concattolist(l);
  885. left.parent:=self;
  886. left.concattolist(l);
  887. inherited concattolist(l);
  888. end;
  889. function tbinarynode.ischild(p : tnode) : boolean;
  890. begin
  891. ischild:=(p=right);
  892. end;
  893. function tbinarynode.docompare(p : tnode) : boolean;
  894. begin
  895. docompare:=(inherited docompare(p) and
  896. ((right=nil) or right.isequal(tbinarynode(p).right))
  897. );
  898. end;
  899. function tbinarynode.dogetcopy : tnode;
  900. var
  901. p : tbinarynode;
  902. begin
  903. p:=tbinarynode(inherited dogetcopy);
  904. if assigned(right) then
  905. p.right:=right.dogetcopy
  906. else
  907. p.right:=nil;
  908. result:=p;
  909. end;
  910. procedure tbinarynode.insertintolist(l : tnodelist);
  911. begin
  912. end;
  913. procedure tbinarynode.swapleftright;
  914. var
  915. swapp : tnode;
  916. begin
  917. swapp:=right;
  918. right:=left;
  919. left:=swapp;
  920. if nf_swapped in flags then
  921. exclude(flags,nf_swapped)
  922. else
  923. include(flags,nf_swapped);
  924. end;
  925. procedure tbinarynode.printnodedata(var t:text);
  926. begin
  927. inherited printnodedata(t);
  928. printnode(t,right);
  929. end;
  930. procedure tbinarynode.printnodelist(var t:text);
  931. var
  932. hp : tbinarynode;
  933. begin
  934. hp:=self;
  935. while assigned(hp) do
  936. begin
  937. write(t,printnodeindention,'(');
  938. printnodeindent;
  939. hp.printnodeinfo(t);
  940. writeln(t);
  941. printnode(t,hp.left);
  942. writeln(t);
  943. printnodeunindent;
  944. writeln(t,printnodeindention,')');
  945. hp:=tbinarynode(hp.right);
  946. end;
  947. end;
  948. {****************************************************************************
  949. TTERTIARYNODE
  950. ****************************************************************************}
  951. constructor ttertiarynode.create(_t:tnodetype;l,r,t : tnode);
  952. begin
  953. inherited create(_t,l,r);
  954. third:=t;
  955. end;
  956. constructor ttertiarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  957. begin
  958. inherited ppuload(t,ppufile);
  959. third:=ppuloadnode(ppufile);
  960. end;
  961. destructor ttertiarynode.destroy;
  962. begin
  963. third.free;
  964. inherited destroy;
  965. end;
  966. procedure ttertiarynode.ppuwrite(ppufile:tcompilerppufile);
  967. begin
  968. inherited ppuwrite(ppufile);
  969. ppuwritenode(ppufile,third);
  970. end;
  971. procedure ttertiarynode.buildderefimpl;
  972. begin
  973. inherited buildderefimpl;
  974. if assigned(third) then
  975. third.buildderefimpl;
  976. end;
  977. procedure ttertiarynode.derefimpl;
  978. begin
  979. inherited derefimpl;
  980. if assigned(third) then
  981. third.derefimpl;
  982. end;
  983. function ttertiarynode.docompare(p : tnode) : boolean;
  984. begin
  985. docompare:=(inherited docompare(p) and
  986. ((third=nil) or third.isequal(ttertiarynode(p).third))
  987. );
  988. end;
  989. function ttertiarynode.dogetcopy : tnode;
  990. var
  991. p : ttertiarynode;
  992. begin
  993. p:=ttertiarynode(inherited dogetcopy);
  994. if assigned(third) then
  995. p.third:=third.dogetcopy
  996. else
  997. p.third:=nil;
  998. result:=p;
  999. end;
  1000. procedure ttertiarynode.insertintolist(l : tnodelist);
  1001. begin
  1002. end;
  1003. procedure ttertiarynode.printnodedata(var t:text);
  1004. begin
  1005. inherited printnodedata(t);
  1006. printnode(t,third);
  1007. end;
  1008. procedure ttertiarynode.concattolist(l : tlinkedlist);
  1009. begin
  1010. third.parent:=self;
  1011. third.concattolist(l);
  1012. inherited concattolist(l);
  1013. end;
  1014. function ttertiarynode.ischild(p : tnode) : boolean;
  1015. begin
  1016. ischild:=p=third;
  1017. end;
  1018. {****************************************************************************
  1019. TBINOPNODE
  1020. ****************************************************************************}
  1021. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  1022. begin
  1023. inherited create(t,l,r);
  1024. end;
  1025. function tbinopnode.docompare(p : tnode) : boolean;
  1026. begin
  1027. docompare:=(inherited docompare(p)) or
  1028. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  1029. ((nf_swapable in flags) and
  1030. left.isequal(tbinopnode(p).right) and
  1031. right.isequal(tbinopnode(p).left));
  1032. end;
  1033. end.