2
0

node.pas 37 KB

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