node.pas 37 KB

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