node.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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_is_funcret,
  193. nf_isproperty,
  194. nf_processing,
  195. { taddrnode }
  196. nf_typedaddr,
  197. { tderefnode }
  198. nf_no_checkpointer,
  199. { tvecnode }
  200. nf_memindex,
  201. nf_memseg,
  202. nf_callunique,
  203. { tloadnode }
  204. nf_absolute,
  205. nf_is_self,
  206. nf_load_self_pointer,
  207. nf_inherited,
  208. { the loadnode is generated internally and a varspez=vs_const should be ignore,
  209. this requires that the parameter is actually passed by value
  210. Be really carefull when using this flag! }
  211. nf_isinternal_ignoreconst,
  212. { taddnode }
  213. nf_is_currency,
  214. nf_has_pointerdiv,
  215. nf_short_bool,
  216. { tassignmentnode }
  217. nf_assign_done_in_right,
  218. { tarrayconstructnode }
  219. nf_forcevaria,
  220. nf_novariaallowed,
  221. { ttypeconvnode, and the first one also treal/ord/pointerconstn }
  222. nf_explicit,
  223. nf_internal, { no warnings/hints generated }
  224. nf_load_procvar,
  225. { tinlinenode }
  226. nf_inlineconst,
  227. { tasmnode }
  228. nf_get_asm_position,
  229. { tblocknode }
  230. nf_block_with_exit
  231. );
  232. tnodeflags = set of tnodeflag;
  233. const
  234. { contains the flags which must be equal for the equality }
  235. { of nodes }
  236. flagsequal : tnodeflags = [nf_error];
  237. type
  238. tnodelist = class
  239. end;
  240. pnode = ^tnode;
  241. { basic class for the intermediated representation fpc uses }
  242. tnode = class
  243. public
  244. { type of this node }
  245. nodetype : tnodetype;
  246. { type of the current code block, general/const/type }
  247. blocktype : tblock_type;
  248. { expected location of the result of this node (pass1) }
  249. expectloc : tcgloc;
  250. { the location of the result of this node (pass2) }
  251. location : tlocation;
  252. { the parent node of this is node }
  253. { this field is set by concattolist }
  254. parent : tnode;
  255. { next node in control flow on the same block level, i.e.
  256. for loop nodes, this is the next node after the end of the loop,
  257. same for if and case, if this field is nil, the next node is the procedure exit,
  258. for the last node in a loop this is set to the loop header
  259. this field is set only for control flow nodes }
  260. successor : tnode;
  261. { there are some properties about the node stored }
  262. flags : tnodeflags;
  263. ppuidx : longint;
  264. resultdef : tdef;
  265. resultdefderef : tderef;
  266. fileinfo : tfileposinfo;
  267. localswitches : tlocalswitches;
  268. optinfo : poptinfo;
  269. constructor create(t:tnodetype);
  270. { this constructor is only for creating copies of class }
  271. { the fields are copied by getcopy }
  272. constructor createforcopy;
  273. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  274. destructor destroy;override;
  275. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  276. procedure buildderefimpl;virtual;
  277. procedure derefimpl;virtual;
  278. procedure derefnode;virtual;
  279. { toggles the flag }
  280. procedure toggleflag(f : tnodeflag);
  281. { the 1.1 code generator may override pass_1 }
  282. { and it need not to implement det_* then }
  283. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  284. { 2.0: runs pass_typecheck and det_temp }
  285. function pass_1 : tnode;virtual;abstract;
  286. { dermines the resultdef of the node }
  287. function pass_typecheck : tnode;virtual;abstract;
  288. { tries to simplify the node, returns a value <>nil if a simplified
  289. node has been created }
  290. function simplify : tnode;virtual;
  291. {$ifdef state_tracking}
  292. { Does optimizations by keeping track of the variable states
  293. in a procedure }
  294. function track_state_pass(exec_known:boolean):boolean;virtual;
  295. {$endif}
  296. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  297. written to (normally the loadnode) as write access. }
  298. procedure mark_write;virtual;
  299. { dermines the number of necessary temp. locations to evaluate
  300. the node }
  301. procedure det_temp;virtual;abstract;
  302. procedure pass_generate_code;virtual;abstract;
  303. { comparing of nodes }
  304. function isequal(p : tnode) : boolean;
  305. { to implement comparisation, override this method }
  306. function docompare(p : tnode) : boolean;virtual;
  307. { wrapper for getcopy }
  308. function getcopy : tnode;
  309. { does the real copying of a node }
  310. function dogetcopy : tnode;virtual;
  311. { returns the real loadn/temprefn a node refers to,
  312. skipping (absolute) equal type conversions }
  313. function actualtargetnode: tnode;virtual;
  314. procedure insertintolist(l : tnodelist);virtual;
  315. { writes a node for debugging purpose, shouldn't be called }
  316. { direct, because there is no test for nil, use printnode }
  317. { to write a complete tree }
  318. procedure printnodeinfo(var t:text);virtual;
  319. procedure printnodedata(var t:text);virtual;
  320. procedure printnodetree(var t:text);virtual;
  321. procedure concattolist(l : tlinkedlist);virtual;
  322. function ischild(p : tnode) : boolean;virtual;
  323. { ensures that the optimizer info record is allocated }
  324. function allocoptinfo : poptinfo;inline;
  325. end;
  326. tnodeclass = class of tnode;
  327. tnodeclassarray = array[tnodetype] of tnodeclass;
  328. { this node is the anchestor for all nodes with at least }
  329. { one child, you have to use it if you want to use }
  330. { true- and current_procinfo.CurrFalseLabel }
  331. punarynode = ^tunarynode;
  332. tunarynode = class(tnode)
  333. left : tnode;
  334. constructor create(t:tnodetype;l : tnode);
  335. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  336. destructor destroy;override;
  337. procedure ppuwrite(ppufile:tcompilerppufile);override;
  338. procedure buildderefimpl;override;
  339. procedure derefimpl;override;
  340. procedure derefnode;override;
  341. procedure concattolist(l : tlinkedlist);override;
  342. function ischild(p : tnode) : boolean;override;
  343. function docompare(p : tnode) : boolean;override;
  344. function dogetcopy : tnode;override;
  345. procedure insertintolist(l : tnodelist);override;
  346. procedure printnodedata(var t:text);override;
  347. end;
  348. pbinarynode = ^tbinarynode;
  349. tbinarynode = class(tunarynode)
  350. right : tnode;
  351. constructor create(t:tnodetype;l,r : tnode);
  352. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  353. destructor destroy;override;
  354. procedure ppuwrite(ppufile:tcompilerppufile);override;
  355. procedure buildderefimpl;override;
  356. procedure derefimpl;override;
  357. procedure derefnode;override;
  358. procedure concattolist(l : tlinkedlist);override;
  359. function ischild(p : tnode) : boolean;override;
  360. function docompare(p : tnode) : boolean;override;
  361. procedure swapleftright;
  362. function dogetcopy : tnode;override;
  363. procedure insertintolist(l : tnodelist);override;
  364. procedure printnodedata(var t:text);override;
  365. procedure printnodelist(var t:text);
  366. end;
  367. ptertiarynode = ^ttertiarynode;
  368. ttertiarynode = class(tbinarynode)
  369. third : tnode;
  370. constructor create(_t:tnodetype;l,r,t : tnode);
  371. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  372. destructor destroy;override;
  373. procedure ppuwrite(ppufile:tcompilerppufile);override;
  374. procedure buildderefimpl;override;
  375. procedure derefimpl;override;
  376. procedure derefnode;override;
  377. procedure concattolist(l : tlinkedlist);override;
  378. function ischild(p : tnode) : boolean;override;
  379. function docompare(p : tnode) : boolean;override;
  380. function dogetcopy : tnode;override;
  381. procedure insertintolist(l : tnodelist);override;
  382. procedure printnodedata(var t:text);override;
  383. end;
  384. tbinopnode = class(tbinarynode)
  385. constructor create(t:tnodetype;l,r : tnode);virtual;
  386. function docompare(p : tnode) : boolean;override;
  387. end;
  388. var
  389. { array with all class types for tnodes }
  390. nodeclass : tnodeclassarray;
  391. function nodeppuidxget(i:longint):tnode;
  392. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  393. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  394. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  395. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  396. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  397. function ppuloadnoderef(ppufile:tcompilerppufile) : 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. nodeppudata : tdynamicarray;
  427. nodeppuidx : longint;
  428. procedure nodeppuidxcreate;
  429. begin
  430. nodeppudata:=tdynamicarray.create(1024);
  431. nodeppuidx:=0;
  432. end;
  433. procedure nodeppuidxfree;
  434. begin
  435. nodeppudata.free;
  436. nodeppudata:=nil;
  437. end;
  438. procedure nodeppuidxadd(n:tnode);
  439. begin
  440. if n.ppuidx<0 then
  441. internalerror(200311072);
  442. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  443. nodeppudata.write(n,sizeof(pointer));
  444. end;
  445. function nodeppuidxget(i:longint):tnode;
  446. begin
  447. if i<0 then
  448. internalerror(200311072);
  449. nodeppudata.seek(i*sizeof(pointer));
  450. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  451. internalerror(200311073);
  452. end;
  453. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  454. var
  455. b : byte;
  456. t : tnodetype;
  457. hppuidx : longint;
  458. begin
  459. { marker }
  460. b:=ppufile.getbyte;
  461. if b<>ppunodemarker then
  462. internalerror(200208151);
  463. { load nodetype }
  464. t:=tnodetype(ppufile.getbyte);
  465. if t>high(tnodetype) then
  466. internalerror(200208152);
  467. if t<>emptynode then
  468. begin
  469. if not assigned(nodeclass[t]) then
  470. internalerror(200208153);
  471. hppuidx:=ppufile.getlongint;
  472. //writeln('load: ',nodetype2str[t]);
  473. { generate node of the correct class }
  474. result:=nodeclass[t].ppuload(t,ppufile);
  475. result.ppuidx:=hppuidx;
  476. nodeppuidxadd(result);
  477. end
  478. else
  479. result:=nil;
  480. end;
  481. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  482. begin
  483. { marker, read by ppuloadnode }
  484. ppufile.putbyte(ppunodemarker);
  485. { type, read by ppuloadnode }
  486. if assigned(n) then
  487. begin
  488. if n.ppuidx=-1 then
  489. internalerror(200311071);
  490. n.ppuidx:=nodeppuidx;
  491. inc(nodeppuidx);
  492. ppufile.putbyte(byte(n.nodetype));
  493. ppufile.putlongint(n.ppuidx);
  494. //writeln('write: ',nodetype2str[n.nodetype]);
  495. n.ppuwrite(ppufile);
  496. end
  497. else
  498. ppufile.putbyte(byte(emptynode));
  499. end;
  500. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  501. begin
  502. { writing of node references isn't implemented yet (FK) }
  503. internalerror(200506181);
  504. end;
  505. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  506. begin
  507. { reading of node references isn't implemented yet (FK) }
  508. internalerror(200506182);
  509. { avoid warning }
  510. result := nil;
  511. end;
  512. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  513. begin
  514. if ppufile.readentry<>ibnodetree then
  515. Message(unit_f_ppu_read_error);
  516. nodeppuidxcreate;
  517. result:=ppuloadnode(ppufile);
  518. result.derefnode;
  519. nodeppuidxfree;
  520. end;
  521. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  522. begin
  523. nodeppuidx:=0;
  524. ppuwritenode(ppufile,n);
  525. ppufile.writeentry(ibnodetree);
  526. end;
  527. procedure printnodeindent;
  528. begin
  529. printnodeindention:=printnodeindention+printnodespacing;
  530. end;
  531. procedure printnodeunindent;
  532. begin
  533. delete(printnodeindention,1,length(printnodespacing));
  534. end;
  535. procedure printnode(var t:text;n:tnode);
  536. begin
  537. if assigned(n) then
  538. n.printnodetree(t)
  539. else
  540. writeln(t,printnodeindention,'nil');
  541. end;
  542. procedure printnode(n:tnode);
  543. begin
  544. printnode(output,n);
  545. end;
  546. function is_constnode(p : tnode) : boolean;
  547. begin
  548. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  549. end;
  550. function is_constintnode(p : tnode) : boolean;
  551. begin
  552. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resultdef);
  553. end;
  554. function is_constcharnode(p : tnode) : boolean;
  555. begin
  556. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resultdef);
  557. end;
  558. function is_constwidecharnode(p : tnode) : boolean;
  559. begin
  560. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resultdef);
  561. end;
  562. function is_constrealnode(p : tnode) : boolean;
  563. begin
  564. is_constrealnode:=(p.nodetype=realconstn);
  565. end;
  566. function is_constboolnode(p : tnode) : boolean;
  567. begin
  568. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resultdef);
  569. end;
  570. function is_constenumnode(p : tnode) : boolean;
  571. begin
  572. is_constenumnode:=(p.nodetype=ordconstn) and (p.resultdef.typ=enumdef);
  573. end;
  574. {****************************************************************************
  575. TNODE
  576. ****************************************************************************}
  577. constructor tnode.create(t:tnodetype);
  578. begin
  579. inherited create;
  580. nodetype:=t;
  581. blocktype:=block_type;
  582. { updated by firstpass }
  583. expectloc:=LOC_INVALID;
  584. { updated by secondpass }
  585. location.loc:=LOC_INVALID;
  586. { save local info }
  587. fileinfo:=current_filepos;
  588. localswitches:=current_settings.localswitches;
  589. resultdef:=nil;
  590. flags:=[];
  591. ppuidx:=-1;
  592. end;
  593. constructor tnode.createforcopy;
  594. begin
  595. end;
  596. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  597. begin
  598. nodetype:=t;
  599. { tnode fields }
  600. blocktype:=tblock_type(ppufile.getbyte);
  601. ppufile.getposinfo(fileinfo);
  602. ppufile.getsmallset(localswitches);
  603. ppufile.getderef(resultdefderef);
  604. ppufile.getsmallset(flags);
  605. { updated by firstpass }
  606. expectloc:=LOC_INVALID;
  607. { updated by secondpass }
  608. location.loc:=LOC_INVALID;
  609. ppuidx:=-1;
  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. procedure tnode.buildderefimpl;
  620. begin
  621. resultdefderef.build(resultdef);
  622. end;
  623. procedure tnode.derefimpl;
  624. begin
  625. resultdef:=tdef(resultdefderef.resolve);
  626. end;
  627. procedure tnode.derefnode;
  628. begin
  629. end;
  630. procedure tnode.toggleflag(f : tnodeflag);
  631. begin
  632. if f in flags then
  633. exclude(flags,f)
  634. else
  635. include(flags,f);
  636. end;
  637. function tnode.simplify : tnode;
  638. begin
  639. result:=nil;
  640. end;
  641. destructor tnode.destroy;
  642. begin
  643. if assigned(optinfo) then
  644. dispose(optinfo);
  645. end;
  646. procedure tnode.concattolist(l : tlinkedlist);
  647. begin
  648. end;
  649. function tnode.ischild(p : tnode) : boolean;
  650. begin
  651. ischild:=false;
  652. end;
  653. procedure tnode.mark_write;
  654. begin
  655. {$ifdef EXTDEBUG}
  656. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  657. {$endif EXTDEBUG}
  658. end;
  659. procedure tnode.printnodeinfo(var t:text);
  660. begin
  661. write(t,nodetype2str[nodetype]);
  662. if assigned(resultdef) then
  663. write(t,', resultdef = "',resultdef.GetTypeName,'"')
  664. else
  665. write(t,', resultdef = <nil>');
  666. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  667. ', loc = ',tcgloc2str[location.loc],
  668. ', expectloc = ',tcgloc2str[expectloc]);
  669. end;
  670. procedure tnode.printnodedata(var t:text);
  671. begin
  672. end;
  673. procedure tnode.printnodetree(var t:text);
  674. begin
  675. write(t,printnodeindention,'(');
  676. printnodeinfo(t);
  677. writeln(t);
  678. printnodeindent;
  679. printnodedata(t);
  680. printnodeunindent;
  681. writeln(t,printnodeindention,')');
  682. end;
  683. function tnode.isequal(p : tnode) : boolean;
  684. begin
  685. isequal:=
  686. (not assigned(self) and not assigned(p)) or
  687. (assigned(self) and assigned(p) and
  688. { optimized subclasses have the same nodetype as their }
  689. { superclass (for compatibility), so also check the classtype (JM) }
  690. (p.classtype=classtype) and
  691. (p.nodetype=nodetype) and
  692. (flags*flagsequal=p.flags*flagsequal) and
  693. docompare(p));
  694. end;
  695. {$ifdef state_tracking}
  696. function Tnode.track_state_pass(exec_known:boolean):boolean;
  697. begin
  698. track_state_pass:=false;
  699. end;
  700. {$endif state_tracking}
  701. function tnode.docompare(p : tnode) : boolean;
  702. begin
  703. docompare:=true;
  704. end;
  705. function cleanupcopiedto(var n : tnode;arg : pointer) : foreachnoderesult;
  706. begin
  707. result:=fen_true;
  708. if n.nodetype=labeln then
  709. tlabelnode(n).copiedto:=nil;
  710. end;
  711. function tnode.getcopy : tnode;
  712. begin
  713. result:=dogetcopy;
  714. foreachnodestatic(pm_postprocess,self,@cleanupcopiedto,nil);
  715. end;
  716. function tnode.dogetcopy : tnode;
  717. var
  718. p : tnode;
  719. begin
  720. { this is quite tricky because we need a node of the current }
  721. { node type and not one of tnode! }
  722. p:=tnodeclass(classtype).createforcopy;
  723. p.nodetype:=nodetype;
  724. p.expectloc:=expectloc;
  725. p.location:=location;
  726. p.parent:=parent;
  727. p.flags:=flags;
  728. p.resultdef:=resultdef;
  729. p.fileinfo:=fileinfo;
  730. p.localswitches:=localswitches;
  731. { p.list:=list; }
  732. result:=p;
  733. end;
  734. function tnode.actualtargetnode: tnode;
  735. begin
  736. result:=self;
  737. end;
  738. procedure tnode.insertintolist(l : tnodelist);
  739. begin
  740. end;
  741. { ensures that the optimizer info record is allocated }
  742. function tnode.allocoptinfo : poptinfo;inline;
  743. begin
  744. if not(assigned(optinfo)) then
  745. new(optinfo);
  746. result:=optinfo;
  747. end;
  748. {****************************************************************************
  749. TUNARYNODE
  750. ****************************************************************************}
  751. constructor tunarynode.create(t:tnodetype;l : tnode);
  752. begin
  753. inherited create(t);
  754. left:=l;
  755. end;
  756. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  757. begin
  758. inherited ppuload(t,ppufile);
  759. left:=ppuloadnode(ppufile);
  760. end;
  761. destructor tunarynode.destroy;
  762. begin
  763. left.free;
  764. inherited destroy;
  765. end;
  766. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  767. begin
  768. inherited ppuwrite(ppufile);
  769. ppuwritenode(ppufile,left);
  770. end;
  771. procedure tunarynode.buildderefimpl;
  772. begin
  773. inherited buildderefimpl;
  774. if assigned(left) then
  775. left.buildderefimpl;
  776. end;
  777. procedure tunarynode.derefimpl;
  778. begin
  779. inherited derefimpl;
  780. if assigned(left) then
  781. left.derefimpl;
  782. end;
  783. procedure tunarynode.derefnode;
  784. begin
  785. inherited derefnode;
  786. if assigned(left) then
  787. left.derefnode;
  788. end;
  789. function tunarynode.docompare(p : tnode) : boolean;
  790. begin
  791. docompare:=(inherited docompare(p) and
  792. ((left=nil) or left.isequal(tunarynode(p).left))
  793. );
  794. end;
  795. function tunarynode.dogetcopy : tnode;
  796. var
  797. p : tunarynode;
  798. begin
  799. p:=tunarynode(inherited dogetcopy);
  800. if assigned(left) then
  801. p.left:=left.dogetcopy
  802. else
  803. p.left:=nil;
  804. result:=p;
  805. end;
  806. procedure tunarynode.insertintolist(l : tnodelist);
  807. begin
  808. end;
  809. procedure tunarynode.printnodedata(var t:text);
  810. begin
  811. inherited printnodedata(t);
  812. printnode(t,left);
  813. end;
  814. procedure tunarynode.concattolist(l : tlinkedlist);
  815. begin
  816. left.parent:=self;
  817. left.concattolist(l);
  818. inherited concattolist(l);
  819. end;
  820. function tunarynode.ischild(p : tnode) : boolean;
  821. begin
  822. ischild:=p=left;
  823. end;
  824. {****************************************************************************
  825. TBINARYNODE
  826. ****************************************************************************}
  827. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  828. begin
  829. inherited create(t,l);
  830. right:=r
  831. end;
  832. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  833. begin
  834. inherited ppuload(t,ppufile);
  835. right:=ppuloadnode(ppufile);
  836. end;
  837. destructor tbinarynode.destroy;
  838. begin
  839. right.free;
  840. inherited destroy;
  841. end;
  842. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  843. begin
  844. inherited ppuwrite(ppufile);
  845. ppuwritenode(ppufile,right);
  846. end;
  847. procedure tbinarynode.buildderefimpl;
  848. begin
  849. inherited buildderefimpl;
  850. if assigned(right) then
  851. right.buildderefimpl;
  852. end;
  853. procedure tbinarynode.derefimpl;
  854. begin
  855. inherited derefimpl;
  856. if assigned(right) then
  857. right.derefimpl;
  858. end;
  859. procedure tbinarynode.derefnode;
  860. begin
  861. inherited derefnode;
  862. if assigned(right) then
  863. right.derefnode;
  864. end;
  865. procedure tbinarynode.concattolist(l : tlinkedlist);
  866. begin
  867. { we could change that depending on the number of }
  868. { required registers }
  869. left.parent:=self;
  870. left.concattolist(l);
  871. left.parent:=self;
  872. left.concattolist(l);
  873. inherited concattolist(l);
  874. end;
  875. function tbinarynode.ischild(p : tnode) : boolean;
  876. begin
  877. ischild:=(p=right);
  878. end;
  879. function tbinarynode.docompare(p : tnode) : boolean;
  880. begin
  881. docompare:=(inherited docompare(p) and
  882. ((right=nil) or right.isequal(tbinarynode(p).right))
  883. );
  884. end;
  885. function tbinarynode.dogetcopy : tnode;
  886. var
  887. p : tbinarynode;
  888. begin
  889. p:=tbinarynode(inherited dogetcopy);
  890. if assigned(right) then
  891. p.right:=right.dogetcopy
  892. else
  893. p.right:=nil;
  894. result:=p;
  895. end;
  896. procedure tbinarynode.insertintolist(l : tnodelist);
  897. begin
  898. end;
  899. procedure tbinarynode.swapleftright;
  900. var
  901. swapp : tnode;
  902. begin
  903. swapp:=right;
  904. right:=left;
  905. left:=swapp;
  906. if nf_swapped in flags then
  907. exclude(flags,nf_swapped)
  908. else
  909. include(flags,nf_swapped);
  910. end;
  911. procedure tbinarynode.printnodedata(var t:text);
  912. begin
  913. inherited printnodedata(t);
  914. printnode(t,right);
  915. end;
  916. procedure tbinarynode.printnodelist(var t:text);
  917. var
  918. hp : tbinarynode;
  919. begin
  920. hp:=self;
  921. while assigned(hp) do
  922. begin
  923. write(t,printnodeindention,'(');
  924. printnodeindent;
  925. hp.printnodeinfo(t);
  926. writeln(t);
  927. printnode(t,hp.left);
  928. writeln(t);
  929. printnodeunindent;
  930. writeln(t,printnodeindention,')');
  931. hp:=tbinarynode(hp.right);
  932. end;
  933. end;
  934. {****************************************************************************
  935. TTERTIARYNODE
  936. ****************************************************************************}
  937. constructor ttertiarynode.create(_t:tnodetype;l,r,t : tnode);
  938. begin
  939. inherited create(_t,l,r);
  940. third:=t;
  941. end;
  942. constructor ttertiarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  943. begin
  944. inherited ppuload(t,ppufile);
  945. third:=ppuloadnode(ppufile);
  946. end;
  947. destructor ttertiarynode.destroy;
  948. begin
  949. third.free;
  950. inherited destroy;
  951. end;
  952. procedure ttertiarynode.ppuwrite(ppufile:tcompilerppufile);
  953. begin
  954. inherited ppuwrite(ppufile);
  955. ppuwritenode(ppufile,third);
  956. end;
  957. procedure ttertiarynode.buildderefimpl;
  958. begin
  959. inherited buildderefimpl;
  960. if assigned(third) then
  961. third.buildderefimpl;
  962. end;
  963. procedure ttertiarynode.derefimpl;
  964. begin
  965. inherited derefimpl;
  966. if assigned(third) then
  967. third.derefimpl;
  968. end;
  969. procedure ttertiarynode.derefnode;
  970. begin
  971. inherited derefnode;
  972. if assigned(third) then
  973. third.derefnode;
  974. end;
  975. function ttertiarynode.docompare(p : tnode) : boolean;
  976. begin
  977. docompare:=(inherited docompare(p) and
  978. ((third=nil) or third.isequal(ttertiarynode(p).third))
  979. );
  980. end;
  981. function ttertiarynode.dogetcopy : tnode;
  982. var
  983. p : ttertiarynode;
  984. begin
  985. p:=ttertiarynode(inherited dogetcopy);
  986. if assigned(third) then
  987. p.third:=third.dogetcopy
  988. else
  989. p.third:=nil;
  990. result:=p;
  991. end;
  992. procedure ttertiarynode.insertintolist(l : tnodelist);
  993. begin
  994. end;
  995. procedure ttertiarynode.printnodedata(var t:text);
  996. begin
  997. inherited printnodedata(t);
  998. printnode(t,third);
  999. end;
  1000. procedure ttertiarynode.concattolist(l : tlinkedlist);
  1001. begin
  1002. third.parent:=self;
  1003. third.concattolist(l);
  1004. inherited concattolist(l);
  1005. end;
  1006. function ttertiarynode.ischild(p : tnode) : boolean;
  1007. begin
  1008. ischild:=p=third;
  1009. end;
  1010. {****************************************************************************
  1011. TBINOPNODE
  1012. ****************************************************************************}
  1013. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  1014. begin
  1015. inherited create(t,l,r);
  1016. end;
  1017. function tbinopnode.docompare(p : tnode) : boolean;
  1018. begin
  1019. docompare:=(inherited docompare(p)) or
  1020. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  1021. ((nf_swapable in flags) and
  1022. left.isequal(tbinopnode(p).right) and
  1023. right.isequal(tbinopnode(p).left));
  1024. end;
  1025. end.