node.pas 38 KB

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