2
0

node.pas 37 KB

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