node.pas 36 KB

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