node.pas 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Basic node handling
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit node;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,cgbase,cgutils,
  23. symtype,
  24. optbase;
  25. type
  26. tnodetype = (
  27. emptynode, {No node (returns nil when loading from ppu)}
  28. addn, {Represents the + operator}
  29. muln, {Represents the * operator}
  30. subn, {Represents the - operator}
  31. divn, {Represents the div operator}
  32. symdifn, {Represents the >< operator}
  33. modn, {Represents the mod operator}
  34. assignn, {Represents an assignment}
  35. loadn, {Represents the use of a variabele}
  36. rangen, {Represents a range (i.e. 0..9)}
  37. ltn, {Represents the < operator}
  38. lten, {Represents the <= operator}
  39. gtn, {Represents the > operator}
  40. gten, {Represents the >= operator}
  41. equaln, {Represents the = operator}
  42. unequaln, {Represents the <> operator}
  43. inn, {Represents the in operator}
  44. orn, {Represents the or operator}
  45. xorn, {Represents the xor operator}
  46. shrn, {Represents the shr operator}
  47. shln, {Represents the shl operator}
  48. slashn, {Represents the / operator}
  49. andn, {Represents the and operator}
  50. subscriptn, {Field in a record/object}
  51. derefn, {Dereferences a pointer}
  52. addrn, {Represents the @ operator}
  53. ordconstn, {Represents an ordinal value}
  54. typeconvn, {Represents type-conversion/typecast}
  55. calln, {Represents a call node}
  56. callparan, {Represents a parameter}
  57. realconstn, {Represents a real value}
  58. unaryminusn, {Represents a sign change (i.e. -2)}
  59. unaryplusn, {Represents a check for +Value}
  60. asmn, {Represents an assembler node }
  61. vecn, {Represents array indexing}
  62. pointerconstn, {Represents a pointer constant}
  63. stringconstn, {Represents a string constant}
  64. notn, {Represents the not operator}
  65. inlinen, {Internal procedures (i.e. writeln)}
  66. niln, {Represents the nil pointer}
  67. errorn, {This part of the tree could not be
  68. parsed because of a compiler error}
  69. typen, {A type name. Used for i.e. typeof(obj)}
  70. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  71. setconstn, {A set constant (i.e. [1,2])}
  72. blockn, {A block of statements}
  73. statementn, {One statement in a block of nodes}
  74. ifn, {An if statement}
  75. breakn, {A break statement}
  76. continuen, {A continue statement}
  77. whilerepeatn, {A while or repeat statement}
  78. forn, {A for loop}
  79. exitn, {An exit statement}
  80. casen, {A case statement}
  81. labeln, {A label}
  82. goton, {A goto statement}
  83. tryexceptn, {A try except block}
  84. raisen, {A raise statement}
  85. tryfinallyn, {A try finally statement}
  86. onn, {For an on statement in exception code}
  87. isn, {Represents the is operator}
  88. asn, {Represents the as typecast}
  89. starstarn, {Represents the ** operator exponentiation }
  90. arrayconstructorn, {Construction node for [...] parsing}
  91. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  92. tempcreaten, { for temps in the result/firstpass }
  93. temprefn, { references to temps }
  94. tempdeleten, { for temps in the result/firstpass }
  95. addoptn, { added for optimizations where we cannot suppress }
  96. nothingn, { NOP, Do nothing}
  97. loadvmtaddrn, { Load the address of the VMT of a class/object}
  98. guidconstn, { A GUID COM Interface constant }
  99. rttin, { Rtti information so they can be accessed in result/firstpass}
  100. loadparentfpn, { Load the framepointer of the parent for nested procedures }
  101. objcselectorn, { node for an Objective-C message selector }
  102. objcprotocoln, { node for an Objective-C @protocol() expression (returns metaclass associated with protocol) }
  103. specializen, { parser-only node to handle Delphi-mode inline specializations }
  104. finalizetempsn { Internal node used to clean up code generator temps (warning: must NOT create additional tepms that may need to be finalised!) }
  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. 'casen',
  163. 'labeln',
  164. 'goton',
  165. 'tryexceptn',
  166. 'raisen',
  167. 'tryfinallyn',
  168. 'onn',
  169. 'isn',
  170. 'asn',
  171. 'starstarn',
  172. 'arrayconstructn',
  173. 'arrayconstructrangen',
  174. 'tempcreaten',
  175. 'temprefn',
  176. 'tempdeleten',
  177. 'addoptn',
  178. 'nothingn',
  179. 'loadvmtaddrn',
  180. 'guidconstn',
  181. 'rttin',
  182. 'loadparentfpn',
  183. 'objcselectorn',
  184. 'objcprotocoln',
  185. 'specializen',
  186. 'finalizetempsn');
  187. { a set containing all const nodes }
  188. nodetype_const = [niln,
  189. ordconstn,
  190. pointerconstn,
  191. stringconstn,
  192. guidconstn,
  193. realconstn,
  194. setconstn];
  195. type
  196. { all boolean field of ttree are now collected in flags }
  197. tnodeflag = (
  198. { tbinop operands can be swaped }
  199. nf_swapable,
  200. { tbinop operands are swaped }
  201. nf_swapped,
  202. nf_error,
  203. { general }
  204. nf_pass1_done,
  205. { Node is written to }
  206. nf_write,
  207. { Node is modified }
  208. nf_modify,
  209. { address of node is taken }
  210. nf_address_taken,
  211. nf_is_funcret,
  212. nf_isproperty,
  213. nf_processing,
  214. { Node cannot be assigned to }
  215. nf_no_lvalue,
  216. { this node is the user code entry, if a node with this flag is removed
  217. during simplify, the flag must be moved to another node }
  218. nf_usercode_entry,
  219. { tderefnode }
  220. nf_no_checkpointer,
  221. { tvecnode }
  222. nf_memindex,
  223. nf_memseg,
  224. nf_callunique,
  225. { tloadnode/ttypeconvnode }
  226. nf_absolute,
  227. { taddnode }
  228. nf_is_currency,
  229. nf_has_pointerdiv,
  230. nf_short_bool,
  231. { tmoddivnode }
  232. nf_isomod,
  233. { tassignmentnode }
  234. nf_assign_done_in_right,
  235. { tarrayconstructnode }
  236. nf_forcevaria,
  237. nf_novariaallowed,
  238. { ttypeconvnode, and the first one also treal/ord/pointerconstn }
  239. { second one also for subtractions of u32-u32 implicitly upcasted to s64 }
  240. { last one also used on addnode to inhibit procvar calling }
  241. nf_explicit,
  242. nf_internal, { no warnings/hints generated }
  243. nf_load_procvar,
  244. { tinlinenode }
  245. nf_inlineconst,
  246. { tasmnode }
  247. nf_get_asm_position,
  248. { tblocknode }
  249. nf_block_with_exit,
  250. { tloadvmtaddrnode }
  251. nf_ignore_for_wpo { we know that this loadvmtaddrnode cannot be used to construct a class instance }
  252. { WARNING: there are now 31 elements in this type, and a set of this
  253. type is written to the PPU. So before adding more than 32 elements,
  254. either move some flags to specific nodes, or stream a normalset
  255. to the ppu
  256. }
  257. );
  258. tnodeflags = set of tnodeflag;
  259. const
  260. { contains the flags which must be equal for the equality }
  261. { of nodes }
  262. flagsequal : tnodeflags = [nf_error];
  263. type
  264. tnodelist = class
  265. end;
  266. pnode = ^tnode;
  267. { basic class for the intermediated representation fpc uses }
  268. tnode = class
  269. private
  270. fppuidx : longint;
  271. function getppuidx:longint;
  272. public
  273. { type of this node }
  274. nodetype : tnodetype;
  275. { type of the current code block, general/const/type }
  276. blocktype : tblock_type;
  277. { expected location of the result of this node (pass1) }
  278. expectloc : tcgloc;
  279. { the location of the result of this node (pass2) }
  280. location : tlocation;
  281. { the parent node of this is node }
  282. { this field is set by concattolist }
  283. parent : tnode;
  284. { next node in control flow on the same block level, i.e.
  285. for loop nodes, this is the next node after the end of the loop,
  286. same for if and case, if this field is nil, the next node is the procedure exit,
  287. for the last node in a loop this is set to the loop header
  288. this field is set only for control flow nodes }
  289. successor : tnode;
  290. { there are some properties about the node stored }
  291. flags : tnodeflags;
  292. resultdef : tdef;
  293. resultdefderef : tderef;
  294. fileinfo : tfileposinfo;
  295. localswitches : tlocalswitches;
  296. verbosity : longint;
  297. optinfo : poptinfo;
  298. constructor create(t:tnodetype);
  299. { this constructor is only for creating copies of class }
  300. { the fields are copied by getcopy }
  301. constructor createforcopy;
  302. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  303. destructor destroy;override;
  304. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  305. procedure buildderefimpl;virtual;
  306. procedure derefimpl;virtual;
  307. procedure resolveppuidx;virtual;
  308. { toggles the flag }
  309. procedure toggleflag(f : tnodeflag);
  310. { the 1.1 code generator may override pass_1 }
  311. { and it need not to implement det_* then }
  312. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  313. { 2.0: runs pass_typecheck and det_temp }
  314. function pass_1 : tnode;virtual;abstract;
  315. { dermines the resultdef of the node }
  316. function pass_typecheck : tnode;virtual;abstract;
  317. { tries to simplify the node, returns a value <>nil if a simplified
  318. node has been created }
  319. function simplify(forinline : boolean) : tnode;virtual;
  320. {$ifdef state_tracking}
  321. { Does optimizations by keeping track of the variable states
  322. in a procedure }
  323. function track_state_pass(exec_known:boolean):boolean;virtual;
  324. {$endif}
  325. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  326. written to (normally the loadnode) as write access. }
  327. procedure mark_write;virtual;
  328. { dermines the number of necessary temp. locations to evaluate
  329. the node }
  330. procedure det_temp;virtual;abstract;
  331. procedure pass_generate_code;virtual;abstract;
  332. { comparing of nodes }
  333. function isequal(p : tnode) : boolean;
  334. { to implement comparisation, override this method }
  335. function docompare(p : tnode) : boolean;virtual;
  336. { wrapper for getcopy }
  337. function getcopy : tnode;
  338. { does the real copying of a node }
  339. function dogetcopy : tnode;virtual;
  340. procedure insertintolist(l : tnodelist);virtual;
  341. { writes a node for debugging purpose, shouldn't be called }
  342. { direct, because there is no test for nil, use printnode }
  343. { to write a complete tree }
  344. procedure printnodeinfo(var t:text);virtual;
  345. procedure printnodedata(var t:text);virtual;
  346. procedure printnodetree(var t:text);virtual;
  347. {$ifdef DEBUG_NODE_XML}
  348. { For writing nodes to XML files - do not call directly, but
  349. instead call XMLPrintNode to write a complete tree }
  350. procedure XMLPrintNodeInfo(var T: Text); dynamic;
  351. procedure XMLPrintNodeData(var T: Text); virtual;
  352. procedure XMLPrintNodeTree(var T: Text); virtual;
  353. class function SanitiseXMLString(const S: ansistring): ansistring; static;
  354. class function WritePointer(const P: Pointer): ansistring; static;
  355. {$endif DEBUG_NODE_XML}
  356. procedure concattolist(l : tlinkedlist);virtual;
  357. function ischild(p : tnode) : boolean;virtual;
  358. { ensures that the optimizer info record is allocated }
  359. function allocoptinfo : poptinfo;inline;
  360. property ppuidx:longint read getppuidx;
  361. end;
  362. tnodeclass = class of tnode;
  363. tnodeclassarray = array[tnodetype] of tnodeclass;
  364. { this node is the anchestor for all nodes with at least }
  365. { one child, you have to use it if you want to use }
  366. { true- and current_procinfo.CurrFalseLabel }
  367. //punarynode = ^tunarynode;
  368. tunarynode = class(tnode)
  369. left : tnode;
  370. constructor create(t:tnodetype;l : tnode);
  371. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  372. destructor destroy;override;
  373. procedure ppuwrite(ppufile:tcompilerppufile);override;
  374. procedure buildderefimpl;override;
  375. procedure derefimpl;override;
  376. procedure concattolist(l : tlinkedlist);override;
  377. function ischild(p : tnode) : boolean;override;
  378. function docompare(p : tnode) : boolean;override;
  379. function dogetcopy : tnode;override;
  380. procedure insertintolist(l : tnodelist);override;
  381. procedure printnodedata(var t:text);override;
  382. {$ifdef DEBUG_NODE_XML}
  383. procedure XMLPrintNodeData(var T: Text); override;
  384. {$endif DEBUG_NODE_XML}
  385. end;
  386. //pbinarynode = ^tbinarynode;
  387. tbinarynode = class(tunarynode)
  388. right : tnode;
  389. constructor create(t:tnodetype;l,r : tnode);
  390. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  391. destructor destroy;override;
  392. procedure ppuwrite(ppufile:tcompilerppufile);override;
  393. procedure buildderefimpl;override;
  394. procedure derefimpl;override;
  395. procedure concattolist(l : tlinkedlist);override;
  396. function ischild(p : tnode) : boolean;override;
  397. function docompare(p : tnode) : boolean;override;
  398. procedure swapleftright;
  399. function dogetcopy : tnode;override;
  400. procedure insertintolist(l : tnodelist);override;
  401. procedure printnodedata(var t:text);override;
  402. {$ifdef DEBUG_NODE_XML}
  403. procedure XMLPrintNodeTree(var T: Text); override;
  404. procedure XMLPrintNodeData(var T: Text); override;
  405. {$endif DEBUG_NODE_XML}
  406. procedure printnodelist(var t:text);
  407. end;
  408. //ptertiarynode = ^ttertiarynode;
  409. ttertiarynode = class(tbinarynode)
  410. third : tnode;
  411. constructor create(_t:tnodetype;l,r,t : tnode);
  412. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  413. destructor destroy;override;
  414. procedure ppuwrite(ppufile:tcompilerppufile);override;
  415. procedure buildderefimpl;override;
  416. procedure derefimpl;override;
  417. procedure concattolist(l : tlinkedlist);override;
  418. function ischild(p : tnode) : boolean;override;
  419. function docompare(p : tnode) : boolean;override;
  420. function dogetcopy : tnode;override;
  421. procedure insertintolist(l : tnodelist);override;
  422. procedure printnodedata(var t:text);override;
  423. {$ifdef DEBUG_NODE_XML}
  424. procedure XMLPrintNodeData(var T: Text); override;
  425. {$endif DEBUG_NODE_XML}
  426. end;
  427. tbinopnode = class(tbinarynode)
  428. constructor create(t:tnodetype;l,r : tnode);virtual;
  429. function docompare(p : tnode) : boolean;override;
  430. {$ifdef DEBUG_NODE_XML}
  431. procedure XMLPrintNodeData(var T: Text); override;
  432. {$endif DEBUG_NODE_XML}
  433. end;
  434. var
  435. { array with all class types for tnodes }
  436. nodeclass : tnodeclassarray;
  437. function nodeppuidxget(i:longint):tnode;
  438. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  439. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  440. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  441. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  442. const
  443. printnodespacing = ' ';
  444. var
  445. { indention used when writing the tree to the screen }
  446. printnodeindention : string;
  447. procedure printnodeindent;
  448. procedure printnodeunindent;
  449. procedure printnode(var t:text;n:tnode);
  450. procedure printnode(n:tnode);
  451. {$ifdef DEBUG_NODE_XML}
  452. procedure XMLPrintNode(var T: Text; N: TNode);
  453. {$endif DEBUG_NODE_XML}
  454. function is_constnode(p : tnode) : boolean;
  455. function is_constintnode(p : tnode) : boolean;
  456. function is_constcharnode(p : tnode) : boolean;
  457. function is_constrealnode(p : tnode) : boolean;
  458. function is_constboolnode(p : tnode) : boolean;
  459. function is_constenumnode(p : tnode) : boolean;
  460. function is_constwidecharnode(p : tnode) : boolean;
  461. function is_constpointernode(p : tnode) : boolean;
  462. function is_conststringnode(p : tnode) : boolean;
  463. function is_constwidestringnode(p : tnode) : boolean;
  464. function is_conststring_or_constcharnode(p : tnode) : boolean;
  465. implementation
  466. uses
  467. verbose,entfile,comphook,
  468. {$ifdef DEBUG_NODE_XML}
  469. cutils,
  470. {$endif DEBUG_NODE_XML}
  471. symconst,
  472. nutils,nflw,
  473. defutil;
  474. const
  475. ppunodemarker = 255;
  476. {****************************************************************************
  477. Helpers
  478. ****************************************************************************}
  479. var
  480. nodeppulist : TFPObjectList;
  481. nodeppuidx : longint;
  482. procedure nodeppuidxcreate;
  483. begin
  484. nodeppulist:=TFPObjectList.Create(false);
  485. nodeppuidx:=0;
  486. end;
  487. procedure nodeppuidxresolve;
  488. var
  489. i : longint;
  490. n : tnode;
  491. begin
  492. for i:=0 to nodeppulist.count-1 do
  493. begin
  494. n:=tnode(nodeppulist[i]);
  495. if assigned(n) then
  496. n.resolveppuidx;
  497. end;
  498. end;
  499. procedure nodeppuidxfree;
  500. begin
  501. nodeppulist.free;
  502. nodeppulist:=nil;
  503. nodeppuidx:=0;
  504. end;
  505. procedure nodeppuidxadd(n:tnode);
  506. var
  507. i : longint;
  508. begin
  509. i:=n.ppuidx;
  510. if i<=0 then
  511. internalerror(200311072);
  512. if i>=nodeppulist.capacity then
  513. nodeppulist.capacity:=((i div 1024)+1)*1024;
  514. if i>=nodeppulist.count then
  515. nodeppulist.count:=i+1;
  516. nodeppulist[i]:=n;
  517. end;
  518. function nodeppuidxget(i:longint):tnode;
  519. begin
  520. if i<=0 then
  521. internalerror(200311073);
  522. result:=tnode(nodeppulist[i]);
  523. end;
  524. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  525. var
  526. b : byte;
  527. t : tnodetype;
  528. hppuidx : longint;
  529. begin
  530. { marker }
  531. b:=ppufile.getbyte;
  532. if b<>ppunodemarker then
  533. internalerror(200208151);
  534. { load nodetype }
  535. t:=tnodetype(ppufile.getbyte);
  536. if t>high(tnodetype) then
  537. internalerror(200208152);
  538. if t<>emptynode then
  539. begin
  540. if not assigned(nodeclass[t]) then
  541. internalerror(200208153);
  542. hppuidx:=ppufile.getlongint;
  543. //writeln('load: ',nodetype2str[t]);
  544. { generate node of the correct class }
  545. result:=nodeclass[t].ppuload(t,ppufile);
  546. result.fppuidx:=hppuidx;
  547. nodeppuidxadd(result);
  548. end
  549. else
  550. result:=nil;
  551. end;
  552. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  553. begin
  554. { marker, read by ppuloadnode }
  555. ppufile.putbyte(ppunodemarker);
  556. { type, read by ppuloadnode }
  557. if assigned(n) then
  558. begin
  559. ppufile.putbyte(byte(n.nodetype));
  560. ppufile.putlongint(n.ppuidx);
  561. //writeln('write: ',nodetype2str[n.nodetype]);
  562. n.ppuwrite(ppufile);
  563. end
  564. else
  565. ppufile.putbyte(byte(emptynode));
  566. end;
  567. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  568. begin
  569. if ppufile.readentry<>ibnodetree then
  570. Message(unit_f_ppu_read_error);
  571. nodeppuidxcreate;
  572. result:=ppuloadnode(ppufile);
  573. nodeppuidxresolve;
  574. nodeppuidxfree;
  575. end;
  576. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  577. begin
  578. nodeppuidxcreate;
  579. ppuwritenode(ppufile,n);
  580. ppufile.writeentry(ibnodetree);
  581. nodeppuidxfree;
  582. end;
  583. procedure printnodeindent;
  584. begin
  585. printnodeindention:=printnodeindention+printnodespacing;
  586. end;
  587. procedure printnodeunindent;
  588. begin
  589. delete(printnodeindention,1,length(printnodespacing));
  590. end;
  591. procedure printnode(var t:text;n:tnode);
  592. begin
  593. if assigned(n) then
  594. n.printnodetree(t)
  595. else
  596. writeln(t,printnodeindention,'nil');
  597. end;
  598. procedure printnode(n:tnode);
  599. begin
  600. printnode(output,n);
  601. end;
  602. {$ifdef DEBUG_NODE_XML}
  603. procedure XMLPrintNode(var T: Text; N: TNode);
  604. begin
  605. if Assigned(N) then
  606. N.XMLPrintNodeTree(T);
  607. end;
  608. {$endif DEBUG_NODE_XML}
  609. function is_constnode(p : tnode) : boolean;
  610. begin
  611. is_constnode:=(p.nodetype in nodetype_const);
  612. end;
  613. function is_constintnode(p : tnode) : boolean;
  614. begin
  615. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resultdef);
  616. end;
  617. function is_constcharnode(p : tnode) : boolean;
  618. begin
  619. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resultdef);
  620. end;
  621. function is_constwidecharnode(p : tnode) : boolean;
  622. begin
  623. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resultdef);
  624. end;
  625. function is_constrealnode(p : tnode) : boolean;
  626. begin
  627. is_constrealnode:=(p.nodetype=realconstn);
  628. end;
  629. function is_constboolnode(p : tnode) : boolean;
  630. begin
  631. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resultdef);
  632. end;
  633. function is_constenumnode(p : tnode) : boolean;
  634. begin
  635. is_constenumnode:=(p.nodetype=ordconstn) and (p.resultdef.typ=enumdef);
  636. end;
  637. function is_constpointernode(p : tnode) : boolean;
  638. begin
  639. is_constpointernode:=(p.nodetype=pointerconstn);
  640. end;
  641. function is_conststringnode(p : tnode) : boolean;
  642. begin
  643. is_conststringnode :=
  644. (p.nodetype = stringconstn) and
  645. (is_chararray(p.resultdef) or
  646. is_shortstring(p.resultdef) or
  647. is_ansistring(p.resultdef));
  648. end;
  649. function is_constwidestringnode(p : tnode) : boolean;
  650. begin
  651. is_constwidestringnode :=
  652. (p.nodetype = stringconstn) and
  653. (is_widechararray(p.resultdef) or
  654. is_wide_or_unicode_string(p.resultdef));
  655. end;
  656. function is_conststring_or_constcharnode(p : tnode) : boolean;
  657. begin
  658. is_conststring_or_constcharnode :=
  659. is_conststringnode(p) or is_constcharnode(p) or
  660. is_constwidestringnode(p) or is_constwidecharnode(p);
  661. end;
  662. {****************************************************************************
  663. TNODE
  664. ****************************************************************************}
  665. constructor tnode.create(t:tnodetype);
  666. begin
  667. inherited create;
  668. nodetype:=t;
  669. blocktype:=block_type;
  670. { updated by firstpass }
  671. expectloc:=LOC_INVALID;
  672. { updated by secondpass }
  673. location.loc:=LOC_INVALID;
  674. { save local info }
  675. fileinfo:=current_filepos;
  676. localswitches:=current_settings.localswitches;
  677. verbosity:=status.verbosity;
  678. resultdef:=nil;
  679. flags:=[];
  680. end;
  681. constructor tnode.createforcopy;
  682. begin
  683. end;
  684. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  685. begin
  686. nodetype:=t;
  687. { tnode fields }
  688. blocktype:=tblock_type(ppufile.getbyte);
  689. ppufile.getposinfo(fileinfo);
  690. ppufile.getsmallset(localswitches);
  691. verbosity:=ppufile.getlongint;
  692. ppufile.getderef(resultdefderef);
  693. ppufile.getsmallset(flags);
  694. { updated by firstpass }
  695. expectloc:=LOC_INVALID;
  696. { updated by secondpass }
  697. location.loc:=LOC_INVALID;
  698. end;
  699. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  700. begin
  701. ppufile.putbyte(byte(block_type));
  702. ppufile.putposinfo(fileinfo);
  703. ppufile.putsmallset(localswitches);
  704. ppufile.putlongint(verbosity);
  705. ppufile.putderef(resultdefderef);
  706. ppufile.putsmallset(flags);
  707. end;
  708. function tnode.getppuidx:longint;
  709. begin
  710. if fppuidx=0 then
  711. begin
  712. inc(nodeppuidx);
  713. fppuidx:=nodeppuidx;
  714. end;
  715. result:=fppuidx;
  716. end;
  717. procedure tnode.resolveppuidx;
  718. begin
  719. end;
  720. procedure tnode.buildderefimpl;
  721. begin
  722. resultdefderef.build(resultdef);
  723. end;
  724. procedure tnode.derefimpl;
  725. begin
  726. resultdef:=tdef(resultdefderef.resolve);
  727. end;
  728. procedure tnode.toggleflag(f : tnodeflag);
  729. begin
  730. if f in flags then
  731. exclude(flags,f)
  732. else
  733. include(flags,f);
  734. end;
  735. function tnode.simplify(forinline : boolean) : tnode;
  736. begin
  737. result:=nil;
  738. end;
  739. destructor tnode.destroy;
  740. begin
  741. if assigned(optinfo) then
  742. dispose(optinfo);
  743. end;
  744. procedure tnode.concattolist(l : tlinkedlist);
  745. begin
  746. end;
  747. function tnode.ischild(p : tnode) : boolean;
  748. begin
  749. ischild:=false;
  750. end;
  751. procedure tnode.mark_write;
  752. begin
  753. {$ifdef EXTDEBUG}
  754. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  755. {$endif EXTDEBUG}
  756. end;
  757. procedure tnode.printnodeinfo(var t:text);
  758. var
  759. i : tnodeflag;
  760. first : boolean;
  761. begin
  762. write(t,nodetype2str[nodetype]);
  763. if assigned(resultdef) then
  764. write(t,', resultdef = ',resultdef.typesymbolprettyname,' = "',resultdef.GetTypeName,'"')
  765. else
  766. write(t,', resultdef = <nil>');
  767. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  768. ', loc = ',tcgloc2str[location.loc],
  769. ', expectloc = ',tcgloc2str[expectloc],
  770. ', flags = [');
  771. first:=true;
  772. for i:=low(tnodeflag) to high(tnodeflag) do
  773. if i in flags then
  774. begin
  775. if not(first) then
  776. write(t,',')
  777. else
  778. first:=false;
  779. write(t, i);
  780. end;
  781. write(t,'], cmplx = ',node_complexity(self));
  782. end;
  783. procedure tnode.printnodedata(var t:text);
  784. begin
  785. end;
  786. procedure tnode.printnodetree(var t:text);
  787. begin
  788. write(t,printnodeindention,'(');
  789. printnodeinfo(t);
  790. writeln(t);
  791. printnodeindent;
  792. printnodedata(t);
  793. printnodeunindent;
  794. writeln(t,printnodeindention,')');
  795. end;
  796. {$ifdef DEBUG_NODE_XML}
  797. { For writing nodes to XML files - do not call directly, but
  798. instead call XMLPrintNode to write a complete tree }
  799. procedure tnode.XMLPrintNodeInfo(var T: Text);
  800. var
  801. i: TNodeFlag;
  802. first: Boolean;
  803. begin
  804. if Assigned(resultdef) then
  805. Write(T,' resultdef="', SanitiseXMLString(resultdef.typesymbolprettyname), '"');
  806. Write(T,' pos="',fileinfo.line,',',fileinfo.column);
  807. First := True;
  808. for i := Low(TNodeFlag) to High(TNodeFlag) do
  809. if i in flags then
  810. begin
  811. if First then
  812. begin
  813. Write(T, '" flags="', i);
  814. First := False;
  815. end
  816. else
  817. Write(T, ',', i)
  818. end;
  819. write(t,'" complexity="',node_complexity(self),'"');
  820. end;
  821. procedure tnode.XMLPrintNodeData(var T: Text);
  822. begin
  823. { Nothing by default }
  824. end;
  825. procedure tnode.XMLPrintNodeTree(var T: Text);
  826. begin
  827. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  828. XMLPrintNodeInfo(T);
  829. WriteLn(T, '>');
  830. PrintNodeIndent;
  831. XMLPrintNodeData(T);
  832. PrintNodeUnindent;
  833. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  834. end;
  835. class function TNode.WritePointer(const P: Pointer): ansistring;
  836. begin
  837. case PtrUInt(P) of
  838. 0:
  839. WritePointer := 'nil';
  840. 1..$FFFF:
  841. WritePointer := '$' + hexstr(PtrUInt(P), 4);
  842. $10000..$FFFFFFFF:
  843. WritePointer := '$' + hexstr(PtrUInt(P), 8);
  844. {$ifdef CPU64}
  845. else
  846. WritePointer := '$' + hexstr(PtrUInt(P), 16);
  847. {$endif CPU64}
  848. end;
  849. end;
  850. class function TNode.SanitiseXMLString(const S: ansistring): ansistring;
  851. var
  852. X, UTF8Len, UTF8Char, CurrentChar: Integer;
  853. needs_quoting, in_quotes, add_end_quote: Boolean;
  854. DoASCII: Boolean;
  855. { Write the given byte as #xxx }
  856. procedure EncodeControlChar(Value: Byte);
  857. begin
  858. if X = Length(Result) then
  859. add_end_quote := False;
  860. Delete(Result, X, 1);
  861. if in_quotes then
  862. begin
  863. Insert('#' + tostr(Value) + '''', Result, X);
  864. { If the entire string consists of control characters, it
  865. doesn't need quoting, so only set the flag here }
  866. needs_quoting := True;
  867. in_quotes := False;
  868. end
  869. else
  870. Insert('#' + tostr(Value), Result, X);
  871. end;
  872. { Write the given byte as either a plain character or an XML keyword }
  873. procedure EncodeStandardChar(Value: Byte);
  874. begin
  875. if not in_quotes then
  876. begin
  877. in_quotes := True;
  878. if (X < Length(Result)) then
  879. begin
  880. needs_quoting := True;
  881. Insert('''', Result, X + 1)
  882. end;
  883. end;
  884. { Check the character for anything that could be mistaken for an XML element }
  885. case CurrentChar of
  886. Ord('#'):
  887. { Required to differentiate '#27' from the escape code #27, for example }
  888. needs_quoting:=true;
  889. Ord('<'):
  890. begin
  891. Delete(Result, X, 1);
  892. Insert('&lt;', Result, X);
  893. end;
  894. Ord('>'):
  895. begin
  896. Delete(Result, X, 1);
  897. Insert('&gt;', Result, X);
  898. end;
  899. Ord('&'):
  900. begin
  901. Delete(Result, X, 1);
  902. Insert('&amp;', Result, X);
  903. end;
  904. Ord('"'):
  905. begin
  906. needs_quoting := True;
  907. Delete(Result, X, 1);
  908. Insert('&quot;', Result, X);
  909. end;
  910. Ord(''''):
  911. begin
  912. needs_quoting:=true;
  913. { Simply double it like in pascal strings }
  914. Insert('''', Result, X);
  915. end;
  916. else
  917. { Do nothing };
  918. end;
  919. end;
  920. { Convert character between $80 and $FF to UTF-8 }
  921. procedure EncodeExtendedChar(Value: Byte);
  922. begin
  923. if not in_quotes then
  924. begin
  925. in_quotes := True;
  926. if (X < Length(Result)) then
  927. begin
  928. needs_quoting := True;
  929. Insert('''', Result, X + 1)
  930. end;
  931. end;
  932. case Value of
  933. $80..$BF: { Add $C2 before the value }
  934. Insert(#$C2, Result, X);
  935. $C0..$FF: { Zero the $40 bit and add $C3 before the value }
  936. begin
  937. Result[X] := Char(Byte(Result[X]) and $BF);
  938. Insert(#$C3, Result, X);
  939. end;
  940. else
  941. { Previous conditions should prevent this procedure from being
  942. called if Value < $80 }
  943. InternalError(2019061901);
  944. end;
  945. end;
  946. begin
  947. needs_quoting := False;
  948. Result := S;
  949. { Gets set to True if an invalid UTF-8 sequence is found }
  950. DoASCII := False;
  951. { By setting in_quotes to false here, we can exclude the single
  952. quotation marks surrounding the string if it doesn't contain any
  953. control characters, or consists entirely of control characters. }
  954. in_quotes := False;
  955. add_end_quote := True;
  956. X := Length(Result);
  957. while X > 0 do
  958. begin
  959. CurrentChar := Ord(Result[X]);
  960. { Control characters and extended characters need special handling }
  961. case CurrentChar of
  962. $00..$1F, $7F:
  963. EncodeControlChar(CurrentChar);
  964. $20..$7E:
  965. EncodeStandardChar(CurrentChar);
  966. { UTF-8 continuation byte }
  967. $80..$BF:
  968. begin
  969. if not in_quotes then
  970. begin
  971. in_quotes := True;
  972. if (X < Length(Result)) then
  973. begin
  974. needs_quoting := True;
  975. Insert('''', Result, X + 1)
  976. end;
  977. end;
  978. UTF8Char := CurrentChar and $3F; { The data bits of the continuation byte }
  979. UTF8Len := 1; { This variable actually holds 1 less than the length }
  980. { By setting DoASCII to true, it marks the string as 'invalid UTF-8'
  981. automatically if it reaches the beginning of the string unexpectedly }
  982. DoASCII := True;
  983. Dec(X);
  984. while X > 0 do
  985. begin
  986. CurrentChar := Ord(Result[X]);
  987. case CurrentChar of
  988. { A standard character here is invalid UTF-8 }
  989. $00..$7F:
  990. Break;
  991. { Another continuation byte }
  992. $80..$BF:
  993. begin
  994. UTF8Char := UTF8Char or ((CurrentChar and $3F) shl (6 * UTF8Len));
  995. Inc(UTF8Len);
  996. if UTF8Len >= 4 then
  997. { Sequence too long }
  998. Break;
  999. end;
  1000. { Lead byte for 2-byte sequences }
  1001. $C2..$DF:
  1002. begin
  1003. if UTF8Len <> 1 then Break;
  1004. UTF8Char := UTF8Char or ((CurrentChar and $1F) shl 6);
  1005. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1006. case UTF8Char of
  1007. $0080..$07FF:
  1008. DoASCII := False;
  1009. else
  1010. { Do nothing - DoASCII is already true }
  1011. end;
  1012. Break;
  1013. end;
  1014. { Lead byte for 3-byte sequences }
  1015. $E0..$EF:
  1016. begin
  1017. if UTF8Len <> 2 then Break;
  1018. UTF8Char := UTF8Char or ((CurrentChar and $0F) shl 12);
  1019. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1020. case UTF8Char of
  1021. $0800..$D7FF, $E000..$FFFF: { $D800..$DFFF is reserved and hence invalid }
  1022. DoASCII := False;
  1023. else
  1024. { Do nothing - DoASCII is already true }
  1025. end;
  1026. Break;
  1027. end;
  1028. { Lead byte for 4-byte sequences }
  1029. $F0..$F4:
  1030. begin
  1031. if UTF8Len <> 3 then Break;
  1032. UTF8Char := UTF8Char or ((CurrentChar and $07) shl 18);
  1033. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1034. case UTF8Char of
  1035. $010000..$10FFFF:
  1036. DoASCII := False;
  1037. else
  1038. { Do nothing - DoASCII is already true }
  1039. end;
  1040. Break;
  1041. end;
  1042. { Invalid character }
  1043. else
  1044. Break;
  1045. end;
  1046. end;
  1047. if DoASCII then
  1048. Break;
  1049. { If all is fine, we don't need to encode any more characters }
  1050. end;
  1051. { Invalid UTF-8 bytes and lead bytes without continuation bytes }
  1052. $C0..$FF:
  1053. begin
  1054. DoASCII := True;
  1055. Break;
  1056. end;
  1057. end;
  1058. Dec(X);
  1059. end;
  1060. { UTF-8 failed, so encode the string as plain ASCII }
  1061. if DoASCII then
  1062. begin
  1063. { Reset the flags and Result }
  1064. needs_quoting := False;
  1065. Result := S;
  1066. in_quotes := False;
  1067. add_end_quote := True;
  1068. for X := Length(Result) downto 1 do
  1069. begin
  1070. CurrentChar := Ord(Result[X]);
  1071. { Control characters and extended characters need special handling }
  1072. case CurrentChar of
  1073. $00..$1F, $7F:
  1074. EncodeControlChar(CurrentChar);
  1075. $20..$7E:
  1076. EncodeStandardChar(CurrentChar);
  1077. { Extended characters }
  1078. else
  1079. EncodeExtendedChar(CurrentChar);
  1080. end;
  1081. end;
  1082. end;
  1083. if needs_quoting then
  1084. begin
  1085. if in_quotes then
  1086. Result := '''' + Result;
  1087. if add_end_quote then
  1088. Result := Result + '''';
  1089. end;
  1090. end;
  1091. {$endif DEBUG_NODE_XML}
  1092. function tnode.isequal(p : tnode) : boolean;
  1093. begin
  1094. isequal:=
  1095. (not assigned(self) and not assigned(p)) or
  1096. (assigned(self) and assigned(p) and
  1097. { optimized subclasses have the same nodetype as their }
  1098. { superclass (for compatibility), so also check the classtype (JM) }
  1099. (p.classtype=classtype) and
  1100. (p.nodetype=nodetype) and
  1101. (flags*flagsequal=p.flags*flagsequal) and
  1102. docompare(p));
  1103. end;
  1104. {$ifdef state_tracking}
  1105. function Tnode.track_state_pass(exec_known:boolean):boolean;
  1106. begin
  1107. track_state_pass:=false;
  1108. end;
  1109. {$endif state_tracking}
  1110. function tnode.docompare(p : tnode) : boolean;
  1111. begin
  1112. docompare:=true;
  1113. end;
  1114. function cleanupcopiedto(var n : tnode;arg : pointer) : foreachnoderesult;
  1115. begin
  1116. result:=fen_true;
  1117. if n.nodetype=labeln then
  1118. tlabelnode(n).copiedto:=nil;
  1119. end;
  1120. function tnode.getcopy : tnode;
  1121. begin
  1122. result:=dogetcopy;
  1123. foreachnodestatic(pm_postprocess,self,@cleanupcopiedto,nil);
  1124. end;
  1125. function tnode.dogetcopy : tnode;
  1126. var
  1127. p : tnode;
  1128. begin
  1129. { this is quite tricky because we need a node of the current }
  1130. { node type and not one of tnode! }
  1131. p:=tnodeclass(classtype).createforcopy;
  1132. p.nodetype:=nodetype;
  1133. p.expectloc:=expectloc;
  1134. p.location:=location;
  1135. p.parent:=parent;
  1136. p.flags:=flags;
  1137. p.resultdef:=resultdef;
  1138. p.fileinfo:=fileinfo;
  1139. p.localswitches:=localswitches;
  1140. p.verbosity:=verbosity;
  1141. { p.list:=list; }
  1142. result:=p;
  1143. end;
  1144. procedure tnode.insertintolist(l : tnodelist);
  1145. begin
  1146. end;
  1147. { ensures that the optimizer info record is allocated }
  1148. function tnode.allocoptinfo : poptinfo;inline;
  1149. begin
  1150. if not(assigned(optinfo)) then
  1151. begin
  1152. new(optinfo);
  1153. fillchar(optinfo^,sizeof(optinfo^),0);
  1154. end;
  1155. result:=optinfo;
  1156. end;
  1157. {****************************************************************************
  1158. TUNARYNODE
  1159. ****************************************************************************}
  1160. constructor tunarynode.create(t:tnodetype;l : tnode);
  1161. begin
  1162. inherited create(t);
  1163. left:=l;
  1164. end;
  1165. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1166. begin
  1167. inherited ppuload(t,ppufile);
  1168. left:=ppuloadnode(ppufile);
  1169. end;
  1170. destructor tunarynode.destroy;
  1171. begin
  1172. left.free;
  1173. inherited destroy;
  1174. end;
  1175. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  1176. begin
  1177. inherited ppuwrite(ppufile);
  1178. ppuwritenode(ppufile,left);
  1179. end;
  1180. procedure tunarynode.buildderefimpl;
  1181. begin
  1182. inherited buildderefimpl;
  1183. if assigned(left) then
  1184. left.buildderefimpl;
  1185. end;
  1186. procedure tunarynode.derefimpl;
  1187. begin
  1188. inherited derefimpl;
  1189. if assigned(left) then
  1190. left.derefimpl;
  1191. end;
  1192. function tunarynode.docompare(p : tnode) : boolean;
  1193. begin
  1194. docompare:=(inherited docompare(p) and
  1195. ((left=nil) or left.isequal(tunarynode(p).left))
  1196. );
  1197. end;
  1198. function tunarynode.dogetcopy : tnode;
  1199. var
  1200. p : tunarynode;
  1201. begin
  1202. p:=tunarynode(inherited dogetcopy);
  1203. if assigned(left) then
  1204. p.left:=left.dogetcopy
  1205. else
  1206. p.left:=nil;
  1207. result:=p;
  1208. end;
  1209. procedure tunarynode.insertintolist(l : tnodelist);
  1210. begin
  1211. end;
  1212. procedure tunarynode.printnodedata(var t:text);
  1213. begin
  1214. inherited printnodedata(t);
  1215. printnode(t,left);
  1216. end;
  1217. {$ifdef DEBUG_NODE_XML}
  1218. procedure TUnaryNode.XMLPrintNodeData(var T: Text);
  1219. begin
  1220. inherited XMLPrintNodeData(T);
  1221. XMLPrintNode(T, Left);
  1222. end;
  1223. {$endif DEBUG_NODE_XML}
  1224. procedure tunarynode.concattolist(l : tlinkedlist);
  1225. begin
  1226. left.parent:=self;
  1227. left.concattolist(l);
  1228. inherited concattolist(l);
  1229. end;
  1230. function tunarynode.ischild(p : tnode) : boolean;
  1231. begin
  1232. ischild:=p=left;
  1233. end;
  1234. {****************************************************************************
  1235. TBINARYNODE
  1236. ****************************************************************************}
  1237. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  1238. begin
  1239. inherited create(t,l);
  1240. right:=r
  1241. end;
  1242. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1243. begin
  1244. inherited ppuload(t,ppufile);
  1245. right:=ppuloadnode(ppufile);
  1246. end;
  1247. destructor tbinarynode.destroy;
  1248. begin
  1249. right.free;
  1250. inherited destroy;
  1251. end;
  1252. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  1253. begin
  1254. inherited ppuwrite(ppufile);
  1255. ppuwritenode(ppufile,right);
  1256. end;
  1257. procedure tbinarynode.buildderefimpl;
  1258. begin
  1259. inherited buildderefimpl;
  1260. if assigned(right) then
  1261. right.buildderefimpl;
  1262. end;
  1263. procedure tbinarynode.derefimpl;
  1264. begin
  1265. inherited derefimpl;
  1266. if assigned(right) then
  1267. right.derefimpl;
  1268. end;
  1269. procedure tbinarynode.concattolist(l : tlinkedlist);
  1270. begin
  1271. { we could change that depending on the number of }
  1272. { required registers }
  1273. left.parent:=self;
  1274. left.concattolist(l);
  1275. left.parent:=self;
  1276. left.concattolist(l);
  1277. inherited concattolist(l);
  1278. end;
  1279. function tbinarynode.ischild(p : tnode) : boolean;
  1280. begin
  1281. ischild:=(p=right);
  1282. end;
  1283. function tbinarynode.docompare(p : tnode) : boolean;
  1284. begin
  1285. docompare:=(inherited docompare(p) and
  1286. ((right=nil) or right.isequal(tbinarynode(p).right))
  1287. );
  1288. end;
  1289. function tbinarynode.dogetcopy : tnode;
  1290. var
  1291. p : tbinarynode;
  1292. begin
  1293. p:=tbinarynode(inherited dogetcopy);
  1294. if assigned(right) then
  1295. p.right:=right.dogetcopy
  1296. else
  1297. p.right:=nil;
  1298. result:=p;
  1299. end;
  1300. procedure tbinarynode.insertintolist(l : tnodelist);
  1301. begin
  1302. end;
  1303. procedure tbinarynode.swapleftright;
  1304. var
  1305. swapp : tnode;
  1306. begin
  1307. swapp:=right;
  1308. right:=left;
  1309. left:=swapp;
  1310. if nf_swapped in flags then
  1311. exclude(flags,nf_swapped)
  1312. else
  1313. include(flags,nf_swapped);
  1314. end;
  1315. procedure tbinarynode.printnodedata(var t:text);
  1316. begin
  1317. inherited printnodedata(t);
  1318. printnode(t,right);
  1319. end;
  1320. {$ifdef DEBUG_NODE_XML}
  1321. procedure TBinaryNode.XMLPrintNodeTree(var T: Text);
  1322. begin
  1323. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  1324. XMLPrintNodeInfo(T);
  1325. WriteLn(T, '>');
  1326. PrintNodeIndent;
  1327. XMLPrintNodeData(T);
  1328. end;
  1329. procedure TBinaryNode.XMLPrintNodeData(var T: Text);
  1330. begin
  1331. inherited XMLPrintNodeData(T);
  1332. PrintNodeUnindent;
  1333. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  1334. { Right nodes are on the same indentation level }
  1335. XMLPrintNode(T, Right);
  1336. end;
  1337. {$endif DEBUG_NODE_XML}
  1338. procedure tbinarynode.printnodelist(var t:text);
  1339. var
  1340. hp : tbinarynode;
  1341. begin
  1342. hp:=self;
  1343. while assigned(hp) do
  1344. begin
  1345. write(t,printnodeindention,'(');
  1346. printnodeindent;
  1347. hp.printnodeinfo(t);
  1348. writeln(t);
  1349. printnode(t,hp.left);
  1350. writeln(t);
  1351. printnodeunindent;
  1352. writeln(t,printnodeindention,')');
  1353. hp:=tbinarynode(hp.right);
  1354. end;
  1355. end;
  1356. {****************************************************************************
  1357. TTERTIARYNODE
  1358. ****************************************************************************}
  1359. constructor ttertiarynode.create(_t:tnodetype;l,r,t : tnode);
  1360. begin
  1361. inherited create(_t,l,r);
  1362. third:=t;
  1363. end;
  1364. constructor ttertiarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1365. begin
  1366. inherited ppuload(t,ppufile);
  1367. third:=ppuloadnode(ppufile);
  1368. end;
  1369. destructor ttertiarynode.destroy;
  1370. begin
  1371. third.free;
  1372. inherited destroy;
  1373. end;
  1374. procedure ttertiarynode.ppuwrite(ppufile:tcompilerppufile);
  1375. begin
  1376. inherited ppuwrite(ppufile);
  1377. ppuwritenode(ppufile,third);
  1378. end;
  1379. procedure ttertiarynode.buildderefimpl;
  1380. begin
  1381. inherited buildderefimpl;
  1382. if assigned(third) then
  1383. third.buildderefimpl;
  1384. end;
  1385. procedure ttertiarynode.derefimpl;
  1386. begin
  1387. inherited derefimpl;
  1388. if assigned(third) then
  1389. third.derefimpl;
  1390. end;
  1391. function ttertiarynode.docompare(p : tnode) : boolean;
  1392. begin
  1393. docompare:=(inherited docompare(p) and
  1394. ((third=nil) or third.isequal(ttertiarynode(p).third))
  1395. );
  1396. end;
  1397. function ttertiarynode.dogetcopy : tnode;
  1398. var
  1399. p : ttertiarynode;
  1400. begin
  1401. p:=ttertiarynode(inherited dogetcopy);
  1402. if assigned(third) then
  1403. p.third:=third.dogetcopy
  1404. else
  1405. p.third:=nil;
  1406. result:=p;
  1407. end;
  1408. procedure ttertiarynode.insertintolist(l : tnodelist);
  1409. begin
  1410. end;
  1411. procedure ttertiarynode.printnodedata(var t:text);
  1412. begin
  1413. inherited printnodedata(t);
  1414. printnode(t,third);
  1415. end;
  1416. {$ifdef DEBUG_NODE_XML}
  1417. procedure TTertiaryNode.XMLPrintNodeData(var T: Text);
  1418. begin
  1419. if Assigned(Third) then
  1420. begin
  1421. WriteLn(T, PrintNodeIndention, '<third-branch>');
  1422. PrintNodeIndent;
  1423. XMLPrintNode(T, Third);
  1424. PrintNodeUnindent;
  1425. WriteLn(T, PrintNodeIndention, '</third-branch>');
  1426. end;
  1427. inherited XMLPrintNodeData(T);
  1428. end;
  1429. {$endif DEBUG_NODE_XML}
  1430. procedure ttertiarynode.concattolist(l : tlinkedlist);
  1431. begin
  1432. third.parent:=self;
  1433. third.concattolist(l);
  1434. inherited concattolist(l);
  1435. end;
  1436. function ttertiarynode.ischild(p : tnode) : boolean;
  1437. begin
  1438. ischild:=p=third;
  1439. end;
  1440. {****************************************************************************
  1441. TBINOPNODE
  1442. ****************************************************************************}
  1443. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  1444. begin
  1445. inherited create(t,l,r);
  1446. end;
  1447. function tbinopnode.docompare(p : tnode) : boolean;
  1448. begin
  1449. docompare:=(inherited docompare(p)) or
  1450. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  1451. ((nf_swapable in flags) and
  1452. left.isequal(tbinopnode(p).right) and
  1453. right.isequal(tbinopnode(p).left));
  1454. end;
  1455. {$ifdef DEBUG_NODE_XML}
  1456. procedure TBinOpNode.XMLPrintNodeData(var T: Text);
  1457. begin
  1458. { For binary operations, put the left and right branches on the same level for clarity }
  1459. XMLPrintNode(T, Left);
  1460. XMLPrintNode(T, Right);
  1461. PrintNodeUnindent;
  1462. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  1463. end;
  1464. {$endif DEBUG_NODE_XML}
  1465. begin
  1466. {$push}{$warnings off}
  1467. { tvaroption must fit into a 4 byte set for speed reasons }
  1468. if ord(high(tvaroption))>31 then
  1469. internalerror(201110301);
  1470. { tnodeflags must fit into a 4 byte set for speed reasons }
  1471. if ord(high(tnodeflags))>31 then
  1472. internalerror(2014020701);
  1473. {$pop}
  1474. end.