node.pas 36 KB

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