node.pas 54 KB

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