node.pas 35 KB

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