node.pas 38 KB

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