node.pas 35 KB

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