2
0

node.pas 38 KB

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