node.pas 53 KB

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