node.pas 37 KB

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