2
0

node.pas 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  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. ppu,
  472. symconst,
  473. nutils,nflw,
  474. defutil;
  475. const
  476. ppunodemarker = 255;
  477. {****************************************************************************
  478. Helpers
  479. ****************************************************************************}
  480. var
  481. nodeppulist : TFPObjectList;
  482. nodeppuidx : longint;
  483. procedure nodeppuidxcreate;
  484. begin
  485. nodeppulist:=TFPObjectList.Create(false);
  486. nodeppuidx:=0;
  487. end;
  488. procedure nodeppuidxresolve;
  489. var
  490. i : longint;
  491. n : tnode;
  492. begin
  493. for i:=0 to nodeppulist.count-1 do
  494. begin
  495. n:=tnode(nodeppulist[i]);
  496. if assigned(n) then
  497. n.resolveppuidx;
  498. end;
  499. end;
  500. procedure nodeppuidxfree;
  501. begin
  502. nodeppulist.free;
  503. nodeppulist:=nil;
  504. nodeppuidx:=0;
  505. end;
  506. procedure nodeppuidxadd(n:tnode);
  507. var
  508. i : longint;
  509. begin
  510. i:=n.ppuidx;
  511. if i<=0 then
  512. internalerror(200311072);
  513. if i>=nodeppulist.capacity then
  514. nodeppulist.capacity:=((i div 1024)+1)*1024;
  515. if i>=nodeppulist.count then
  516. nodeppulist.count:=i+1;
  517. nodeppulist[i]:=n;
  518. end;
  519. function nodeppuidxget(i:longint):tnode;
  520. begin
  521. if i<=0 then
  522. internalerror(200311073);
  523. result:=tnode(nodeppulist[i]);
  524. end;
  525. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  526. var
  527. b : byte;
  528. t : tnodetype;
  529. hppuidx : longint;
  530. begin
  531. { marker }
  532. b:=ppufile.getbyte;
  533. if b<>ppunodemarker then
  534. internalerror(200208151);
  535. { load nodetype }
  536. t:=tnodetype(ppufile.getbyte);
  537. if t>high(tnodetype) then
  538. internalerror(200208152);
  539. if t<>emptynode then
  540. begin
  541. if not assigned(nodeclass[t]) then
  542. internalerror(200208153);
  543. hppuidx:=ppufile.getlongint;
  544. //writeln('load: ',nodetype2str[t]);
  545. { generate node of the correct class }
  546. result:=nodeclass[t].ppuload(t,ppufile);
  547. result.fppuidx:=hppuidx;
  548. nodeppuidxadd(result);
  549. end
  550. else
  551. result:=nil;
  552. end;
  553. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  554. begin
  555. { marker, read by ppuloadnode }
  556. ppufile.putbyte(ppunodemarker);
  557. { type, read by ppuloadnode }
  558. if assigned(n) then
  559. begin
  560. ppufile.putbyte(byte(n.nodetype));
  561. ppufile.putlongint(n.ppuidx);
  562. //writeln('write: ',nodetype2str[n.nodetype]);
  563. n.ppuwrite(ppufile);
  564. end
  565. else
  566. ppufile.putbyte(byte(emptynode));
  567. end;
  568. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  569. begin
  570. if ppufile.readentry<>ibnodetree then
  571. Message(unit_f_ppu_read_error);
  572. nodeppuidxcreate;
  573. result:=ppuloadnode(ppufile);
  574. nodeppuidxresolve;
  575. nodeppuidxfree;
  576. end;
  577. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  578. begin
  579. nodeppuidxcreate;
  580. ppuwritenode(ppufile,n);
  581. ppufile.writeentry(ibnodetree);
  582. nodeppuidxfree;
  583. end;
  584. procedure printnodeindent;
  585. begin
  586. printnodeindention:=printnodeindention+printnodespacing;
  587. end;
  588. procedure printnodeunindent;
  589. begin
  590. delete(printnodeindention,1,length(printnodespacing));
  591. end;
  592. procedure printnode(var t:text;n:tnode);
  593. begin
  594. if assigned(n) then
  595. n.printnodetree(t)
  596. else
  597. writeln(t,printnodeindention,'nil');
  598. end;
  599. procedure printnode(n:tnode);
  600. begin
  601. printnode(output,n);
  602. end;
  603. {$ifdef DEBUG_NODE_XML}
  604. procedure XMLPrintNode(var T: Text; N: TNode);
  605. begin
  606. if Assigned(N) then
  607. N.XMLPrintNodeTree(T);
  608. end;
  609. {$endif DEBUG_NODE_XML}
  610. function is_constnode(p : tnode) : boolean;
  611. begin
  612. is_constnode:=(p.nodetype in nodetype_const);
  613. end;
  614. function is_constintnode(p : tnode) : boolean;
  615. begin
  616. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resultdef);
  617. end;
  618. function is_constcharnode(p : tnode) : boolean;
  619. begin
  620. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resultdef);
  621. end;
  622. function is_constwidecharnode(p : tnode) : boolean;
  623. begin
  624. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resultdef);
  625. end;
  626. function is_constrealnode(p : tnode) : boolean;
  627. begin
  628. is_constrealnode:=(p.nodetype=realconstn);
  629. end;
  630. function is_constboolnode(p : tnode) : boolean;
  631. begin
  632. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resultdef);
  633. end;
  634. function is_constenumnode(p : tnode) : boolean;
  635. begin
  636. is_constenumnode:=(p.nodetype=ordconstn) and (p.resultdef.typ=enumdef);
  637. end;
  638. function is_constpointernode(p : tnode) : boolean;
  639. begin
  640. is_constpointernode:=(p.nodetype=pointerconstn);
  641. end;
  642. function is_conststringnode(p : tnode) : boolean;
  643. begin
  644. is_conststringnode :=
  645. (p.nodetype = stringconstn) and
  646. (is_chararray(p.resultdef) or
  647. is_shortstring(p.resultdef) or
  648. is_ansistring(p.resultdef));
  649. end;
  650. function is_constwidestringnode(p : tnode) : boolean;
  651. begin
  652. is_constwidestringnode :=
  653. (p.nodetype = stringconstn) and
  654. (is_widechararray(p.resultdef) or
  655. is_wide_or_unicode_string(p.resultdef));
  656. end;
  657. function is_conststring_or_constcharnode(p : tnode) : boolean;
  658. begin
  659. is_conststring_or_constcharnode :=
  660. is_conststringnode(p) or is_constcharnode(p) or
  661. is_constwidestringnode(p) or is_constwidecharnode(p);
  662. end;
  663. {****************************************************************************
  664. TNODE
  665. ****************************************************************************}
  666. constructor tnode.create(t:tnodetype);
  667. begin
  668. inherited create;
  669. nodetype:=t;
  670. blocktype:=block_type;
  671. { updated by firstpass }
  672. expectloc:=LOC_INVALID;
  673. { updated by secondpass }
  674. location.loc:=LOC_INVALID;
  675. { save local info }
  676. fileinfo:=current_filepos;
  677. localswitches:=current_settings.localswitches;
  678. verbosity:=status.verbosity;
  679. resultdef:=nil;
  680. flags:=[];
  681. end;
  682. constructor tnode.createforcopy;
  683. begin
  684. end;
  685. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  686. begin
  687. nodetype:=t;
  688. { tnode fields }
  689. blocktype:=tblock_type(ppufile.getbyte);
  690. ppufile.getposinfo(fileinfo);
  691. ppufile.getset(tppuset5(localswitches));
  692. verbosity:=ppufile.getlongint;
  693. ppufile.getderef(resultdefderef);
  694. ppufile.getset(tppuset4(flags));
  695. { updated by firstpass }
  696. expectloc:=LOC_INVALID;
  697. { updated by secondpass }
  698. location.loc:=LOC_INVALID;
  699. end;
  700. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  701. begin
  702. ppufile.putbyte(byte(block_type));
  703. ppufile.putposinfo(fileinfo);
  704. ppufile.putset(tppuset5(localswitches));
  705. ppufile.putlongint(verbosity);
  706. ppufile.putderef(resultdefderef);
  707. ppufile.putset(tppuset4(flags));
  708. end;
  709. function tnode.getppuidx:longint;
  710. begin
  711. if fppuidx=0 then
  712. begin
  713. inc(nodeppuidx);
  714. fppuidx:=nodeppuidx;
  715. end;
  716. result:=fppuidx;
  717. end;
  718. procedure tnode.resolveppuidx;
  719. begin
  720. end;
  721. procedure tnode.buildderefimpl;
  722. begin
  723. resultdefderef.build(resultdef);
  724. end;
  725. procedure tnode.derefimpl;
  726. begin
  727. resultdef:=tdef(resultdefderef.resolve);
  728. end;
  729. procedure tnode.toggleflag(f : tnodeflag);
  730. begin
  731. if f in flags then
  732. exclude(flags,f)
  733. else
  734. include(flags,f);
  735. end;
  736. function tnode.simplify(forinline : boolean) : tnode;
  737. begin
  738. result:=nil;
  739. end;
  740. destructor tnode.destroy;
  741. begin
  742. if assigned(optinfo) then
  743. dispose(optinfo);
  744. end;
  745. procedure tnode.concattolist(l : tlinkedlist);
  746. begin
  747. end;
  748. function tnode.ischild(p : tnode) : boolean;
  749. begin
  750. ischild:=false;
  751. end;
  752. procedure tnode.mark_write;
  753. begin
  754. {$ifdef EXTDEBUG}
  755. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  756. {$endif EXTDEBUG}
  757. end;
  758. procedure tnode.printnodeinfo(var t:text);
  759. var
  760. i : tnodeflag;
  761. first : boolean;
  762. begin
  763. write(t,nodetype2str[nodetype]);
  764. if assigned(resultdef) then
  765. write(t,', resultdef = ',resultdef.typesymbolprettyname,' = "',resultdef.GetTypeName,'"')
  766. else
  767. write(t,', resultdef = <nil>');
  768. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  769. ', loc = ',tcgloc2str[location.loc],
  770. ', expectloc = ',tcgloc2str[expectloc],
  771. ', flags = [');
  772. first:=true;
  773. for i:=low(tnodeflag) to high(tnodeflag) do
  774. if i in flags then
  775. begin
  776. if not(first) then
  777. write(t,',')
  778. else
  779. first:=false;
  780. write(t, i);
  781. end;
  782. write(t,'], cmplx = ',node_complexity(self));
  783. end;
  784. procedure tnode.printnodedata(var t:text);
  785. begin
  786. end;
  787. procedure tnode.printnodetree(var t:text);
  788. begin
  789. write(t,printnodeindention,'(');
  790. printnodeinfo(t);
  791. writeln(t);
  792. printnodeindent;
  793. printnodedata(t);
  794. printnodeunindent;
  795. writeln(t,printnodeindention,')');
  796. end;
  797. {$ifdef DEBUG_NODE_XML}
  798. { For writing nodes to XML files - do not call directly, but
  799. instead call XMLPrintNode to write a complete tree }
  800. procedure tnode.XMLPrintNodeInfo(var T: Text);
  801. var
  802. i: TNodeFlag;
  803. first: Boolean;
  804. begin
  805. if Assigned(resultdef) then
  806. Write(T,' resultdef="', SanitiseXMLString(resultdef.typesymbolprettyname), '"');
  807. Write(T,' pos="',fileinfo.line,',',fileinfo.column);
  808. First := True;
  809. for i := Low(TNodeFlag) to High(TNodeFlag) do
  810. if i in flags then
  811. begin
  812. if First then
  813. begin
  814. Write(T, '" flags="', i);
  815. First := False;
  816. end
  817. else
  818. Write(T, ',', i)
  819. end;
  820. write(t,'" complexity="',node_complexity(self),'"');
  821. end;
  822. procedure tnode.XMLPrintNodeData(var T: Text);
  823. begin
  824. { Nothing by default }
  825. end;
  826. procedure tnode.XMLPrintNodeTree(var T: Text);
  827. begin
  828. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  829. XMLPrintNodeInfo(T);
  830. WriteLn(T, '>');
  831. PrintNodeIndent;
  832. XMLPrintNodeData(T);
  833. PrintNodeUnindent;
  834. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  835. end;
  836. class function TNode.WritePointer(const P: Pointer): ansistring;
  837. begin
  838. case PtrUInt(P) of
  839. 0:
  840. WritePointer := 'nil';
  841. 1..$FFFF:
  842. WritePointer := '$' + hexstr(PtrUInt(P), 4);
  843. $10000..$FFFFFFFF:
  844. WritePointer := '$' + hexstr(PtrUInt(P), 8);
  845. {$ifdef CPU64}
  846. else
  847. WritePointer := '$' + hexstr(PtrUInt(P), 16);
  848. {$endif CPU64}
  849. end;
  850. end;
  851. class function TNode.SanitiseXMLString(const S: ansistring): ansistring;
  852. var
  853. X, UTF8Len, UTF8Char, CurrentChar: Integer;
  854. needs_quoting, in_quotes, add_end_quote: Boolean;
  855. DoASCII: Boolean;
  856. { Write the given byte as #xxx }
  857. procedure EncodeControlChar(Value: Byte);
  858. begin
  859. if X = Length(Result) then
  860. add_end_quote := False;
  861. Delete(Result, X, 1);
  862. if in_quotes then
  863. begin
  864. Insert('#' + tostr(Value) + '''', Result, X);
  865. { If the entire string consists of control characters, it
  866. doesn't need quoting, so only set the flag here }
  867. needs_quoting := True;
  868. in_quotes := False;
  869. end
  870. else
  871. Insert('#' + tostr(Value), Result, X);
  872. end;
  873. { Write the given byte as either a plain character or an XML keyword }
  874. procedure EncodeStandardChar(Value: Byte);
  875. begin
  876. if not in_quotes then
  877. begin
  878. in_quotes := True;
  879. if (X < Length(Result)) then
  880. begin
  881. needs_quoting := True;
  882. Insert('''', Result, X + 1)
  883. end;
  884. end;
  885. { Check the character for anything that could be mistaken for an XML element }
  886. case CurrentChar of
  887. Ord('#'):
  888. { Required to differentiate '#27' from the escape code #27, for example }
  889. needs_quoting:=true;
  890. Ord('<'):
  891. begin
  892. Delete(Result, X, 1);
  893. Insert('&lt;', Result, X);
  894. end;
  895. Ord('>'):
  896. begin
  897. Delete(Result, X, 1);
  898. Insert('&gt;', Result, X);
  899. end;
  900. Ord('&'):
  901. begin
  902. Delete(Result, X, 1);
  903. Insert('&amp;', Result, X);
  904. end;
  905. Ord('"'):
  906. begin
  907. needs_quoting := True;
  908. Delete(Result, X, 1);
  909. Insert('&quot;', Result, X);
  910. end;
  911. Ord(''''):
  912. begin
  913. needs_quoting:=true;
  914. { Simply double it like in pascal strings }
  915. Insert('''', Result, X);
  916. end;
  917. else
  918. { Do nothing };
  919. end;
  920. end;
  921. { Convert character between $80 and $FF to UTF-8 }
  922. procedure EncodeExtendedChar(Value: Byte);
  923. begin
  924. if not in_quotes then
  925. begin
  926. in_quotes := True;
  927. if (X < Length(Result)) then
  928. begin
  929. needs_quoting := True;
  930. Insert('''', Result, X + 1)
  931. end;
  932. end;
  933. case Value of
  934. $80..$BF: { Add $C2 before the value }
  935. Insert(#$C2, Result, X);
  936. $C0..$FF: { Zero the $40 bit and add $C3 before the value }
  937. begin
  938. Result[X] := Char(Byte(Result[X]) and $BF);
  939. Insert(#$C3, Result, X);
  940. end;
  941. else
  942. { Previous conditions should prevent this procedure from being
  943. called if Value < $80 }
  944. InternalError(2019061901);
  945. end;
  946. end;
  947. begin
  948. needs_quoting := False;
  949. Result := S;
  950. { Gets set to True if an invalid UTF-8 sequence is found }
  951. DoASCII := False;
  952. { By setting in_quotes to false here, we can exclude the single
  953. quotation marks surrounding the string if it doesn't contain any
  954. control characters, or consists entirely of control characters. }
  955. in_quotes := False;
  956. add_end_quote := True;
  957. X := Length(Result);
  958. while X > 0 do
  959. begin
  960. CurrentChar := Ord(Result[X]);
  961. { Control characters and extended characters need special handling }
  962. case CurrentChar of
  963. $00..$1F, $7F:
  964. EncodeControlChar(CurrentChar);
  965. $20..$7E:
  966. EncodeStandardChar(CurrentChar);
  967. { UTF-8 continuation byte }
  968. $80..$BF:
  969. begin
  970. if not in_quotes then
  971. begin
  972. in_quotes := True;
  973. if (X < Length(Result)) then
  974. begin
  975. needs_quoting := True;
  976. Insert('''', Result, X + 1)
  977. end;
  978. end;
  979. UTF8Char := CurrentChar and $3F; { The data bits of the continuation byte }
  980. UTF8Len := 1; { This variable actually holds 1 less than the length }
  981. { By setting DoASCII to true, it marks the string as 'invalid UTF-8'
  982. automatically if it reaches the beginning of the string unexpectedly }
  983. DoASCII := True;
  984. Dec(X);
  985. while X > 0 do
  986. begin
  987. CurrentChar := Ord(Result[X]);
  988. case CurrentChar of
  989. { A standard character here is invalid UTF-8 }
  990. $00..$7F:
  991. Break;
  992. { Another continuation byte }
  993. $80..$BF:
  994. begin
  995. UTF8Char := UTF8Char or ((CurrentChar and $3F) shl (6 * UTF8Len));
  996. Inc(UTF8Len);
  997. if UTF8Len >= 4 then
  998. { Sequence too long }
  999. Break;
  1000. end;
  1001. { Lead byte for 2-byte sequences }
  1002. $C2..$DF:
  1003. begin
  1004. if UTF8Len <> 1 then Break;
  1005. UTF8Char := UTF8Char or ((CurrentChar and $1F) shl 6);
  1006. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1007. case UTF8Char of
  1008. $0080..$07FF:
  1009. DoASCII := False;
  1010. else
  1011. { Do nothing - DoASCII is already true }
  1012. end;
  1013. Break;
  1014. end;
  1015. { Lead byte for 3-byte sequences }
  1016. $E0..$EF:
  1017. begin
  1018. if UTF8Len <> 2 then Break;
  1019. UTF8Char := UTF8Char or ((CurrentChar and $0F) shl 12);
  1020. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1021. case UTF8Char of
  1022. $0800..$D7FF, $E000..$FFFF: { $D800..$DFFF is reserved and hence invalid }
  1023. DoASCII := False;
  1024. else
  1025. { Do nothing - DoASCII is already true }
  1026. end;
  1027. Break;
  1028. end;
  1029. { Lead byte for 4-byte sequences }
  1030. $F0..$F4:
  1031. begin
  1032. if UTF8Len <> 3 then Break;
  1033. UTF8Char := UTF8Char or ((CurrentChar and $07) shl 18);
  1034. { Check to see if the code is in range and not part of an 'overlong' sequence }
  1035. case UTF8Char of
  1036. $010000..$10FFFF:
  1037. DoASCII := False;
  1038. else
  1039. { Do nothing - DoASCII is already true }
  1040. end;
  1041. Break;
  1042. end;
  1043. { Invalid character }
  1044. else
  1045. Break;
  1046. end;
  1047. end;
  1048. if DoASCII then
  1049. Break;
  1050. { If all is fine, we don't need to encode any more characters }
  1051. end;
  1052. { Invalid UTF-8 bytes and lead bytes without continuation bytes }
  1053. $C0..$FF:
  1054. begin
  1055. DoASCII := True;
  1056. Break;
  1057. end;
  1058. end;
  1059. Dec(X);
  1060. end;
  1061. { UTF-8 failed, so encode the string as plain ASCII }
  1062. if DoASCII then
  1063. begin
  1064. { Reset the flags and Result }
  1065. needs_quoting := False;
  1066. Result := S;
  1067. in_quotes := False;
  1068. add_end_quote := True;
  1069. for X := Length(Result) downto 1 do
  1070. begin
  1071. CurrentChar := Ord(Result[X]);
  1072. { Control characters and extended characters need special handling }
  1073. case CurrentChar of
  1074. $00..$1F, $7F:
  1075. EncodeControlChar(CurrentChar);
  1076. $20..$7E:
  1077. EncodeStandardChar(CurrentChar);
  1078. { Extended characters }
  1079. else
  1080. EncodeExtendedChar(CurrentChar);
  1081. end;
  1082. end;
  1083. end;
  1084. if needs_quoting then
  1085. begin
  1086. if in_quotes then
  1087. Result := '''' + Result;
  1088. if add_end_quote then
  1089. Result := Result + '''';
  1090. end;
  1091. end;
  1092. {$endif DEBUG_NODE_XML}
  1093. function tnode.isequal(p : tnode) : boolean;
  1094. begin
  1095. isequal:=
  1096. (not assigned(self) and not assigned(p)) or
  1097. (assigned(self) and assigned(p) and
  1098. { optimized subclasses have the same nodetype as their }
  1099. { superclass (for compatibility), so also check the classtype (JM) }
  1100. (p.classtype=classtype) and
  1101. (p.nodetype=nodetype) and
  1102. (flags*flagsequal=p.flags*flagsequal) and
  1103. docompare(p));
  1104. end;
  1105. {$ifdef state_tracking}
  1106. function Tnode.track_state_pass(exec_known:boolean):boolean;
  1107. begin
  1108. track_state_pass:=false;
  1109. end;
  1110. {$endif state_tracking}
  1111. function tnode.docompare(p : tnode) : boolean;
  1112. begin
  1113. docompare:=true;
  1114. end;
  1115. function cleanupcopiedto(var n : tnode;arg : pointer) : foreachnoderesult;
  1116. begin
  1117. result:=fen_true;
  1118. if n.nodetype=labeln then
  1119. tlabelnode(n).copiedto:=nil;
  1120. end;
  1121. function tnode.getcopy : tnode;
  1122. begin
  1123. result:=dogetcopy;
  1124. foreachnodestatic(pm_postprocess,self,@cleanupcopiedto,nil);
  1125. end;
  1126. function tnode.dogetcopy : tnode;
  1127. var
  1128. p : tnode;
  1129. begin
  1130. { this is quite tricky because we need a node of the current }
  1131. { node type and not one of tnode! }
  1132. p:=tnodeclass(classtype).createforcopy;
  1133. p.nodetype:=nodetype;
  1134. p.expectloc:=expectloc;
  1135. p.location:=location;
  1136. p.parent:=parent;
  1137. p.flags:=flags;
  1138. p.resultdef:=resultdef;
  1139. p.fileinfo:=fileinfo;
  1140. p.localswitches:=localswitches;
  1141. p.verbosity:=verbosity;
  1142. { p.list:=list; }
  1143. result:=p;
  1144. end;
  1145. procedure tnode.insertintolist(l : tnodelist);
  1146. begin
  1147. end;
  1148. { ensures that the optimizer info record is allocated }
  1149. function tnode.allocoptinfo : poptinfo;inline;
  1150. begin
  1151. if not(assigned(optinfo)) then
  1152. begin
  1153. new(optinfo);
  1154. fillchar(optinfo^,sizeof(optinfo^),0);
  1155. end;
  1156. result:=optinfo;
  1157. end;
  1158. {****************************************************************************
  1159. TUNARYNODE
  1160. ****************************************************************************}
  1161. constructor tunarynode.create(t:tnodetype;l : tnode);
  1162. begin
  1163. inherited create(t);
  1164. left:=l;
  1165. end;
  1166. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1167. begin
  1168. inherited ppuload(t,ppufile);
  1169. left:=ppuloadnode(ppufile);
  1170. end;
  1171. destructor tunarynode.destroy;
  1172. begin
  1173. left.free;
  1174. inherited destroy;
  1175. end;
  1176. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  1177. begin
  1178. inherited ppuwrite(ppufile);
  1179. ppuwritenode(ppufile,left);
  1180. end;
  1181. procedure tunarynode.buildderefimpl;
  1182. begin
  1183. inherited buildderefimpl;
  1184. if assigned(left) then
  1185. left.buildderefimpl;
  1186. end;
  1187. procedure tunarynode.derefimpl;
  1188. begin
  1189. inherited derefimpl;
  1190. if assigned(left) then
  1191. left.derefimpl;
  1192. end;
  1193. function tunarynode.docompare(p : tnode) : boolean;
  1194. begin
  1195. docompare:=(inherited docompare(p) and
  1196. ((left=nil) or left.isequal(tunarynode(p).left))
  1197. );
  1198. end;
  1199. function tunarynode.dogetcopy : tnode;
  1200. var
  1201. p : tunarynode;
  1202. begin
  1203. p:=tunarynode(inherited dogetcopy);
  1204. if assigned(left) then
  1205. p.left:=left.dogetcopy
  1206. else
  1207. p.left:=nil;
  1208. result:=p;
  1209. end;
  1210. procedure tunarynode.insertintolist(l : tnodelist);
  1211. begin
  1212. end;
  1213. procedure tunarynode.printnodedata(var t:text);
  1214. begin
  1215. inherited printnodedata(t);
  1216. printnode(t,left);
  1217. end;
  1218. {$ifdef DEBUG_NODE_XML}
  1219. procedure TUnaryNode.XMLPrintNodeData(var T: Text);
  1220. begin
  1221. inherited XMLPrintNodeData(T);
  1222. XMLPrintNode(T, Left);
  1223. end;
  1224. {$endif DEBUG_NODE_XML}
  1225. procedure tunarynode.concattolist(l : tlinkedlist);
  1226. begin
  1227. left.parent:=self;
  1228. left.concattolist(l);
  1229. inherited concattolist(l);
  1230. end;
  1231. function tunarynode.ischild(p : tnode) : boolean;
  1232. begin
  1233. ischild:=p=left;
  1234. end;
  1235. {****************************************************************************
  1236. TBINARYNODE
  1237. ****************************************************************************}
  1238. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  1239. begin
  1240. inherited create(t,l);
  1241. right:=r
  1242. end;
  1243. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1244. begin
  1245. inherited ppuload(t,ppufile);
  1246. right:=ppuloadnode(ppufile);
  1247. end;
  1248. destructor tbinarynode.destroy;
  1249. begin
  1250. right.free;
  1251. inherited destroy;
  1252. end;
  1253. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  1254. begin
  1255. inherited ppuwrite(ppufile);
  1256. ppuwritenode(ppufile,right);
  1257. end;
  1258. procedure tbinarynode.buildderefimpl;
  1259. begin
  1260. inherited buildderefimpl;
  1261. if assigned(right) then
  1262. right.buildderefimpl;
  1263. end;
  1264. procedure tbinarynode.derefimpl;
  1265. begin
  1266. inherited derefimpl;
  1267. if assigned(right) then
  1268. right.derefimpl;
  1269. end;
  1270. procedure tbinarynode.concattolist(l : tlinkedlist);
  1271. begin
  1272. { we could change that depending on the number of }
  1273. { required registers }
  1274. left.parent:=self;
  1275. left.concattolist(l);
  1276. left.parent:=self;
  1277. left.concattolist(l);
  1278. inherited concattolist(l);
  1279. end;
  1280. function tbinarynode.ischild(p : tnode) : boolean;
  1281. begin
  1282. ischild:=(p=right);
  1283. end;
  1284. function tbinarynode.docompare(p : tnode) : boolean;
  1285. begin
  1286. docompare:=(inherited docompare(p) and
  1287. ((right=nil) or right.isequal(tbinarynode(p).right))
  1288. );
  1289. end;
  1290. function tbinarynode.dogetcopy : tnode;
  1291. var
  1292. p : tbinarynode;
  1293. begin
  1294. p:=tbinarynode(inherited dogetcopy);
  1295. if assigned(right) then
  1296. p.right:=right.dogetcopy
  1297. else
  1298. p.right:=nil;
  1299. result:=p;
  1300. end;
  1301. procedure tbinarynode.insertintolist(l : tnodelist);
  1302. begin
  1303. end;
  1304. procedure tbinarynode.swapleftright;
  1305. var
  1306. swapp : tnode;
  1307. begin
  1308. swapp:=right;
  1309. right:=left;
  1310. left:=swapp;
  1311. if nf_swapped in flags then
  1312. exclude(flags,nf_swapped)
  1313. else
  1314. include(flags,nf_swapped);
  1315. end;
  1316. procedure tbinarynode.printnodedata(var t:text);
  1317. begin
  1318. inherited printnodedata(t);
  1319. printnode(t,right);
  1320. end;
  1321. {$ifdef DEBUG_NODE_XML}
  1322. procedure TBinaryNode.XMLPrintNodeTree(var T: Text);
  1323. begin
  1324. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  1325. XMLPrintNodeInfo(T);
  1326. WriteLn(T, '>');
  1327. PrintNodeIndent;
  1328. XMLPrintNodeData(T);
  1329. end;
  1330. procedure TBinaryNode.XMLPrintNodeData(var T: Text);
  1331. begin
  1332. inherited XMLPrintNodeData(T);
  1333. PrintNodeUnindent;
  1334. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  1335. { Right nodes are on the same indentation level }
  1336. XMLPrintNode(T, Right);
  1337. end;
  1338. {$endif DEBUG_NODE_XML}
  1339. procedure tbinarynode.printnodelist(var t:text);
  1340. var
  1341. hp : tbinarynode;
  1342. begin
  1343. hp:=self;
  1344. while assigned(hp) do
  1345. begin
  1346. write(t,printnodeindention,'(');
  1347. printnodeindent;
  1348. hp.printnodeinfo(t);
  1349. writeln(t);
  1350. printnode(t,hp.left);
  1351. writeln(t);
  1352. printnodeunindent;
  1353. writeln(t,printnodeindention,')');
  1354. hp:=tbinarynode(hp.right);
  1355. end;
  1356. end;
  1357. {****************************************************************************
  1358. TTERTIARYNODE
  1359. ****************************************************************************}
  1360. constructor ttertiarynode.create(_t:tnodetype;l,r,t : tnode);
  1361. begin
  1362. inherited create(_t,l,r);
  1363. third:=t;
  1364. end;
  1365. constructor ttertiarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1366. begin
  1367. inherited ppuload(t,ppufile);
  1368. third:=ppuloadnode(ppufile);
  1369. end;
  1370. destructor ttertiarynode.destroy;
  1371. begin
  1372. third.free;
  1373. inherited destroy;
  1374. end;
  1375. procedure ttertiarynode.ppuwrite(ppufile:tcompilerppufile);
  1376. begin
  1377. inherited ppuwrite(ppufile);
  1378. ppuwritenode(ppufile,third);
  1379. end;
  1380. procedure ttertiarynode.buildderefimpl;
  1381. begin
  1382. inherited buildderefimpl;
  1383. if assigned(third) then
  1384. third.buildderefimpl;
  1385. end;
  1386. procedure ttertiarynode.derefimpl;
  1387. begin
  1388. inherited derefimpl;
  1389. if assigned(third) then
  1390. third.derefimpl;
  1391. end;
  1392. function ttertiarynode.docompare(p : tnode) : boolean;
  1393. begin
  1394. docompare:=(inherited docompare(p) and
  1395. ((third=nil) or third.isequal(ttertiarynode(p).third))
  1396. );
  1397. end;
  1398. function ttertiarynode.dogetcopy : tnode;
  1399. var
  1400. p : ttertiarynode;
  1401. begin
  1402. p:=ttertiarynode(inherited dogetcopy);
  1403. if assigned(third) then
  1404. p.third:=third.dogetcopy
  1405. else
  1406. p.third:=nil;
  1407. result:=p;
  1408. end;
  1409. procedure ttertiarynode.insertintolist(l : tnodelist);
  1410. begin
  1411. end;
  1412. procedure ttertiarynode.printnodedata(var t:text);
  1413. begin
  1414. inherited printnodedata(t);
  1415. printnode(t,third);
  1416. end;
  1417. {$ifdef DEBUG_NODE_XML}
  1418. procedure TTertiaryNode.XMLPrintNodeData(var T: Text);
  1419. begin
  1420. if Assigned(Third) then
  1421. begin
  1422. WriteLn(T, PrintNodeIndention, '<third-branch>');
  1423. PrintNodeIndent;
  1424. XMLPrintNode(T, Third);
  1425. PrintNodeUnindent;
  1426. WriteLn(T, PrintNodeIndention, '</third-branch>');
  1427. end;
  1428. inherited XMLPrintNodeData(T);
  1429. end;
  1430. {$endif DEBUG_NODE_XML}
  1431. procedure ttertiarynode.concattolist(l : tlinkedlist);
  1432. begin
  1433. third.parent:=self;
  1434. third.concattolist(l);
  1435. inherited concattolist(l);
  1436. end;
  1437. function ttertiarynode.ischild(p : tnode) : boolean;
  1438. begin
  1439. ischild:=p=third;
  1440. end;
  1441. {****************************************************************************
  1442. TBINOPNODE
  1443. ****************************************************************************}
  1444. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  1445. begin
  1446. inherited create(t,l,r);
  1447. end;
  1448. function tbinopnode.docompare(p : tnode) : boolean;
  1449. begin
  1450. docompare:=(inherited docompare(p)) or
  1451. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  1452. ((nf_swapable in flags) and
  1453. left.isequal(tbinopnode(p).right) and
  1454. right.isequal(tbinopnode(p).left));
  1455. end;
  1456. {$ifdef DEBUG_NODE_XML}
  1457. procedure TBinOpNode.XMLPrintNodeData(var T: Text);
  1458. begin
  1459. { For binary operations, put the left and right branches on the same level for clarity }
  1460. XMLPrintNode(T, Left);
  1461. XMLPrintNode(T, Right);
  1462. PrintNodeUnindent;
  1463. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  1464. end;
  1465. {$endif DEBUG_NODE_XML}
  1466. begin
  1467. {$push}{$warnings off}
  1468. { tvaroption must fit into a 4 byte set for speed reasons }
  1469. if ord(high(tvaroption))>31 then
  1470. internalerror(201110301);
  1471. { tnodeflags must fit into a 4 byte set for speed reasons }
  1472. if ord(high(tnodeflags))>31 then
  1473. internalerror(2014020701);
  1474. {$pop}
  1475. end.