node.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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. {$ifdef extdebug}
  270. maxfirstpasscount,
  271. firstpasscount : longint;
  272. {$endif extdebug}
  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 derefnode;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. procedure insertintolist(l : tnodelist);virtual;
  316. { writes a node for debugging purpose, shouldn't be called }
  317. { direct, because there is no test for nil, use printnode }
  318. { to write a complete tree }
  319. procedure printnodeinfo(var t:text);virtual;
  320. procedure printnodedata(var t:text);virtual;
  321. procedure printnodetree(var t:text);virtual;
  322. procedure concattolist(l : tlinkedlist);virtual;
  323. function ischild(p : tnode) : boolean;virtual;
  324. { ensures that the optimizer info record is allocated }
  325. function allocoptinfo : poptinfo;inline;
  326. end;
  327. tnodeclass = class of tnode;
  328. tnodeclassarray = array[tnodetype] of tnodeclass;
  329. { this node is the anchestor for all nodes with at least }
  330. { one child, you have to use it if you want to use }
  331. { true- and current_procinfo.CurrFalseLabel }
  332. punarynode = ^tunarynode;
  333. tunarynode = class(tnode)
  334. left : tnode;
  335. constructor create(t:tnodetype;l : tnode);
  336. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  337. destructor destroy;override;
  338. procedure ppuwrite(ppufile:tcompilerppufile);override;
  339. procedure buildderefimpl;override;
  340. procedure derefimpl;override;
  341. procedure derefnode;override;
  342. procedure concattolist(l : tlinkedlist);override;
  343. function ischild(p : tnode) : boolean;override;
  344. function docompare(p : tnode) : boolean;override;
  345. function dogetcopy : tnode;override;
  346. procedure insertintolist(l : tnodelist);override;
  347. procedure printnodedata(var t:text);override;
  348. end;
  349. pbinarynode = ^tbinarynode;
  350. tbinarynode = class(tunarynode)
  351. right : tnode;
  352. constructor create(t:tnodetype;l,r : tnode);
  353. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  354. destructor destroy;override;
  355. procedure ppuwrite(ppufile:tcompilerppufile);override;
  356. procedure buildderefimpl;override;
  357. procedure derefimpl;override;
  358. procedure derefnode;override;
  359. procedure concattolist(l : tlinkedlist);override;
  360. function ischild(p : tnode) : boolean;override;
  361. function docompare(p : tnode) : boolean;override;
  362. procedure swapleftright;
  363. function dogetcopy : tnode;override;
  364. procedure insertintolist(l : tnodelist);override;
  365. procedure printnodedata(var t:text);override;
  366. procedure printnodelist(var t:text);
  367. end;
  368. ptertiarynode = ^ttertiarynode;
  369. ttertiarynode = class(tbinarynode)
  370. third : tnode;
  371. constructor create(_t:tnodetype;l,r,t : tnode);
  372. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  373. destructor destroy;override;
  374. procedure ppuwrite(ppufile:tcompilerppufile);override;
  375. procedure buildderefimpl;override;
  376. procedure derefimpl;override;
  377. procedure derefnode;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. end;
  385. tbinopnode = class(tbinarynode)
  386. constructor create(t:tnodetype;l,r : tnode);virtual;
  387. function docompare(p : tnode) : boolean;override;
  388. end;
  389. var
  390. { array with all class types for tnodes }
  391. nodeclass : tnodeclassarray;
  392. function nodeppuidxget(i:longint):tnode;
  393. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  394. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  395. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  396. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  397. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  398. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  399. const
  400. printnodespacing = ' ';
  401. var
  402. { indention used when writing the tree to the screen }
  403. printnodeindention : string;
  404. procedure printnodeindent;
  405. procedure printnodeunindent;
  406. procedure printnode(var t:text;n:tnode);
  407. procedure printnode(n:tnode);
  408. function is_constnode(p : tnode) : boolean;
  409. function is_constintnode(p : tnode) : boolean;
  410. function is_constcharnode(p : tnode) : boolean;
  411. function is_constrealnode(p : tnode) : boolean;
  412. function is_constboolnode(p : tnode) : boolean;
  413. function is_constenumnode(p : tnode) : boolean;
  414. function is_constwidecharnode(p : tnode) : boolean;
  415. implementation
  416. uses
  417. cutils,verbose,ppu,
  418. symconst,
  419. nutils,nflw,
  420. defutil;
  421. const
  422. ppunodemarker = 255;
  423. {****************************************************************************
  424. Helpers
  425. ****************************************************************************}
  426. var
  427. nodeppudata : tdynamicarray;
  428. nodeppuidx : longint;
  429. procedure nodeppuidxcreate;
  430. begin
  431. nodeppudata:=tdynamicarray.create(1024);
  432. nodeppuidx:=0;
  433. end;
  434. procedure nodeppuidxfree;
  435. begin
  436. nodeppudata.free;
  437. nodeppudata:=nil;
  438. end;
  439. procedure nodeppuidxadd(n:tnode);
  440. begin
  441. if n.ppuidx<0 then
  442. internalerror(200311072);
  443. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  444. nodeppudata.write(n,sizeof(pointer));
  445. end;
  446. function nodeppuidxget(i:longint):tnode;
  447. begin
  448. if i<0 then
  449. internalerror(200311072);
  450. nodeppudata.seek(i*sizeof(pointer));
  451. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  452. internalerror(200311073);
  453. end;
  454. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  455. var
  456. b : byte;
  457. t : tnodetype;
  458. hppuidx : longint;
  459. begin
  460. { marker }
  461. b:=ppufile.getbyte;
  462. if b<>ppunodemarker then
  463. internalerror(200208151);
  464. { load nodetype }
  465. t:=tnodetype(ppufile.getbyte);
  466. if t>high(tnodetype) then
  467. internalerror(200208152);
  468. if t<>emptynode then
  469. begin
  470. if not assigned(nodeclass[t]) then
  471. internalerror(200208153);
  472. hppuidx:=ppufile.getlongint;
  473. //writeln('load: ',nodetype2str[t]);
  474. { generate node of the correct class }
  475. result:=nodeclass[t].ppuload(t,ppufile);
  476. result.ppuidx:=hppuidx;
  477. nodeppuidxadd(result);
  478. end
  479. else
  480. result:=nil;
  481. end;
  482. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  483. begin
  484. { marker, read by ppuloadnode }
  485. ppufile.putbyte(ppunodemarker);
  486. { type, read by ppuloadnode }
  487. if assigned(n) then
  488. begin
  489. if n.ppuidx=-1 then
  490. internalerror(200311071);
  491. n.ppuidx:=nodeppuidx;
  492. inc(nodeppuidx);
  493. ppufile.putbyte(byte(n.nodetype));
  494. ppufile.putlongint(n.ppuidx);
  495. //writeln('write: ',nodetype2str[n.nodetype]);
  496. n.ppuwrite(ppufile);
  497. end
  498. else
  499. ppufile.putbyte(byte(emptynode));
  500. end;
  501. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  502. begin
  503. { writing of node references isn't implemented yet (FK) }
  504. internalerror(200506181);
  505. end;
  506. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  507. begin
  508. { reading of node references isn't implemented yet (FK) }
  509. internalerror(200506182);
  510. { avoid warning }
  511. result := nil;
  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. result.derefnode;
  520. nodeppuidxfree;
  521. end;
  522. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  523. begin
  524. nodeppuidx:=0;
  525. ppuwritenode(ppufile,n);
  526. ppufile.writeentry(ibnodetree);
  527. end;
  528. procedure printnodeindent;
  529. begin
  530. printnodeindention:=printnodeindention+printnodespacing;
  531. end;
  532. procedure printnodeunindent;
  533. begin
  534. delete(printnodeindention,1,length(printnodespacing));
  535. end;
  536. procedure printnode(var t:text;n:tnode);
  537. begin
  538. if assigned(n) then
  539. n.printnodetree(t)
  540. else
  541. writeln(t,printnodeindention,'nil');
  542. end;
  543. procedure printnode(n:tnode);
  544. begin
  545. printnode(output,n);
  546. end;
  547. function is_constnode(p : tnode) : boolean;
  548. begin
  549. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  550. end;
  551. function is_constintnode(p : tnode) : boolean;
  552. begin
  553. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resultdef);
  554. end;
  555. function is_constcharnode(p : tnode) : boolean;
  556. begin
  557. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resultdef);
  558. end;
  559. function is_constwidecharnode(p : tnode) : boolean;
  560. begin
  561. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resultdef);
  562. end;
  563. function is_constrealnode(p : tnode) : boolean;
  564. begin
  565. is_constrealnode:=(p.nodetype=realconstn);
  566. end;
  567. function is_constboolnode(p : tnode) : boolean;
  568. begin
  569. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resultdef);
  570. end;
  571. function is_constenumnode(p : tnode) : boolean;
  572. begin
  573. is_constenumnode:=(p.nodetype=ordconstn) and (p.resultdef.typ=enumdef);
  574. end;
  575. {****************************************************************************
  576. TNODE
  577. ****************************************************************************}
  578. constructor tnode.create(t:tnodetype);
  579. begin
  580. inherited create;
  581. nodetype:=t;
  582. blocktype:=block_type;
  583. { updated by firstpass }
  584. expectloc:=LOC_INVALID;
  585. { updated by secondpass }
  586. location.loc:=LOC_INVALID;
  587. { save local info }
  588. fileinfo:=current_filepos;
  589. localswitches:=current_settings.localswitches;
  590. resultdef:=nil;
  591. {$ifdef EXTDEBUG}
  592. maxfirstpasscount:=0;
  593. firstpasscount:=0;
  594. {$endif EXTDEBUG}
  595. flags:=[];
  596. ppuidx:=-1;
  597. end;
  598. constructor tnode.createforcopy;
  599. begin
  600. end;
  601. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  602. begin
  603. nodetype:=t;
  604. { tnode fields }
  605. blocktype:=tblock_type(ppufile.getbyte);
  606. ppufile.getposinfo(fileinfo);
  607. ppufile.getsmallset(localswitches);
  608. ppufile.getderef(resultdefderef);
  609. ppufile.getsmallset(flags);
  610. { updated by firstpass }
  611. expectloc:=LOC_INVALID;
  612. { updated by secondpass }
  613. location.loc:=LOC_INVALID;
  614. {$ifdef EXTDEBUG}
  615. maxfirstpasscount:=0;
  616. firstpasscount:=0;
  617. {$endif EXTDEBUG}
  618. ppuidx:=-1;
  619. end;
  620. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  621. begin
  622. ppufile.putbyte(byte(block_type));
  623. ppufile.putposinfo(fileinfo);
  624. ppufile.putsmallset(localswitches);
  625. ppufile.putderef(resultdefderef);
  626. ppufile.putsmallset(flags);
  627. end;
  628. procedure tnode.buildderefimpl;
  629. begin
  630. resultdefderef.build(resultdef);
  631. end;
  632. procedure tnode.derefimpl;
  633. begin
  634. resultdef:=tdef(resultdefderef.resolve);
  635. end;
  636. procedure tnode.derefnode;
  637. begin
  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. {$ifdef EXTDEBUG}
  653. if firstpasscount>maxfirstpasscount then
  654. maxfirstpasscount:=firstpasscount;
  655. {$endif EXTDEBUG}
  656. if assigned(optinfo) then
  657. dispose(optinfo);
  658. end;
  659. procedure tnode.concattolist(l : tlinkedlist);
  660. begin
  661. end;
  662. function tnode.ischild(p : tnode) : boolean;
  663. begin
  664. ischild:=false;
  665. end;
  666. procedure tnode.mark_write;
  667. begin
  668. {$ifdef EXTDEBUG}
  669. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  670. {$endif EXTDEBUG}
  671. end;
  672. procedure tnode.printnodeinfo(var t:text);
  673. begin
  674. write(t,nodetype2str[nodetype]);
  675. if assigned(resultdef) then
  676. write(t,', resultdef = "',resultdef.GetTypeName,'"')
  677. else
  678. write(t,', resultdef = <nil>');
  679. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  680. ', loc = ',tcgloc2str[location.loc],
  681. ', expectloc = ',tcgloc2str[expectloc]);
  682. end;
  683. procedure tnode.printnodedata(var t:text);
  684. begin
  685. end;
  686. procedure tnode.printnodetree(var t:text);
  687. begin
  688. write(t,printnodeindention,'(');
  689. printnodeinfo(t);
  690. writeln(t);
  691. printnodeindent;
  692. printnodedata(t);
  693. printnodeunindent;
  694. writeln(t,printnodeindention,')');
  695. end;
  696. function tnode.isequal(p : tnode) : boolean;
  697. begin
  698. isequal:=
  699. (not assigned(self) and not assigned(p)) or
  700. (assigned(self) and assigned(p) and
  701. { optimized subclasses have the same nodetype as their }
  702. { superclass (for compatibility), so also check the classtype (JM) }
  703. (p.classtype=classtype) and
  704. (p.nodetype=nodetype) and
  705. (flags*flagsequal=p.flags*flagsequal) and
  706. docompare(p));
  707. end;
  708. {$ifdef state_tracking}
  709. function Tnode.track_state_pass(exec_known:boolean):boolean;
  710. begin
  711. track_state_pass:=false;
  712. end;
  713. {$endif state_tracking}
  714. function tnode.docompare(p : tnode) : boolean;
  715. begin
  716. docompare:=true;
  717. end;
  718. function cleanupcopiedto(var n : tnode;arg : pointer) : foreachnoderesult;
  719. begin
  720. result:=fen_true;
  721. if n.nodetype=labeln then
  722. tlabelnode(n).copiedto:=nil;
  723. end;
  724. function tnode.getcopy : tnode;
  725. begin
  726. result:=dogetcopy;
  727. foreachnodestatic(pm_postprocess,self,@cleanupcopiedto,nil);
  728. end;
  729. function tnode.dogetcopy : tnode;
  730. var
  731. p : tnode;
  732. begin
  733. { this is quite tricky because we need a node of the current }
  734. { node type and not one of tnode! }
  735. p:=tnodeclass(classtype).createforcopy;
  736. p.nodetype:=nodetype;
  737. p.expectloc:=expectloc;
  738. p.location:=location;
  739. p.parent:=parent;
  740. p.flags:=flags;
  741. p.resultdef:=resultdef;
  742. p.fileinfo:=fileinfo;
  743. p.localswitches:=localswitches;
  744. {$ifdef extdebug}
  745. p.firstpasscount:=firstpasscount;
  746. {$endif extdebug}
  747. { p.list:=list; }
  748. result:=p;
  749. end;
  750. procedure tnode.insertintolist(l : tnodelist);
  751. begin
  752. end;
  753. { ensures that the optimizer info record is allocated }
  754. function tnode.allocoptinfo : poptinfo;inline;
  755. begin
  756. if not(assigned(optinfo)) then
  757. new(optinfo);
  758. result:=optinfo;
  759. end;
  760. {****************************************************************************
  761. TUNARYNODE
  762. ****************************************************************************}
  763. constructor tunarynode.create(t:tnodetype;l : tnode);
  764. begin
  765. inherited create(t);
  766. left:=l;
  767. end;
  768. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  769. begin
  770. inherited ppuload(t,ppufile);
  771. left:=ppuloadnode(ppufile);
  772. end;
  773. destructor tunarynode.destroy;
  774. begin
  775. left.free;
  776. inherited destroy;
  777. end;
  778. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  779. begin
  780. inherited ppuwrite(ppufile);
  781. ppuwritenode(ppufile,left);
  782. end;
  783. procedure tunarynode.buildderefimpl;
  784. begin
  785. inherited buildderefimpl;
  786. if assigned(left) then
  787. left.buildderefimpl;
  788. end;
  789. procedure tunarynode.derefimpl;
  790. begin
  791. inherited derefimpl;
  792. if assigned(left) then
  793. left.derefimpl;
  794. end;
  795. procedure tunarynode.derefnode;
  796. begin
  797. inherited derefnode;
  798. if assigned(left) then
  799. left.derefnode;
  800. end;
  801. function tunarynode.docompare(p : tnode) : boolean;
  802. begin
  803. docompare:=(inherited docompare(p) and
  804. ((left=nil) or left.isequal(tunarynode(p).left))
  805. );
  806. end;
  807. function tunarynode.dogetcopy : tnode;
  808. var
  809. p : tunarynode;
  810. begin
  811. p:=tunarynode(inherited dogetcopy);
  812. if assigned(left) then
  813. p.left:=left.dogetcopy
  814. else
  815. p.left:=nil;
  816. result:=p;
  817. end;
  818. procedure tunarynode.insertintolist(l : tnodelist);
  819. begin
  820. end;
  821. procedure tunarynode.printnodedata(var t:text);
  822. begin
  823. inherited printnodedata(t);
  824. printnode(t,left);
  825. end;
  826. procedure tunarynode.concattolist(l : tlinkedlist);
  827. begin
  828. left.parent:=self;
  829. left.concattolist(l);
  830. inherited concattolist(l);
  831. end;
  832. function tunarynode.ischild(p : tnode) : boolean;
  833. begin
  834. ischild:=p=left;
  835. end;
  836. {****************************************************************************
  837. TBINARYNODE
  838. ****************************************************************************}
  839. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  840. begin
  841. inherited create(t,l);
  842. right:=r
  843. end;
  844. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  845. begin
  846. inherited ppuload(t,ppufile);
  847. right:=ppuloadnode(ppufile);
  848. end;
  849. destructor tbinarynode.destroy;
  850. begin
  851. right.free;
  852. inherited destroy;
  853. end;
  854. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  855. begin
  856. inherited ppuwrite(ppufile);
  857. ppuwritenode(ppufile,right);
  858. end;
  859. procedure tbinarynode.buildderefimpl;
  860. begin
  861. inherited buildderefimpl;
  862. if assigned(right) then
  863. right.buildderefimpl;
  864. end;
  865. procedure tbinarynode.derefimpl;
  866. begin
  867. inherited derefimpl;
  868. if assigned(right) then
  869. right.derefimpl;
  870. end;
  871. procedure tbinarynode.derefnode;
  872. begin
  873. inherited derefnode;
  874. if assigned(right) then
  875. right.derefnode;
  876. end;
  877. procedure tbinarynode.concattolist(l : tlinkedlist);
  878. begin
  879. { we could change that depending on the number of }
  880. { required registers }
  881. left.parent:=self;
  882. left.concattolist(l);
  883. left.parent:=self;
  884. left.concattolist(l);
  885. inherited concattolist(l);
  886. end;
  887. function tbinarynode.ischild(p : tnode) : boolean;
  888. begin
  889. ischild:=(p=right);
  890. end;
  891. function tbinarynode.docompare(p : tnode) : boolean;
  892. begin
  893. docompare:=(inherited docompare(p) and
  894. ((right=nil) or right.isequal(tbinarynode(p).right))
  895. );
  896. end;
  897. function tbinarynode.dogetcopy : tnode;
  898. var
  899. p : tbinarynode;
  900. begin
  901. p:=tbinarynode(inherited dogetcopy);
  902. if assigned(right) then
  903. p.right:=right.dogetcopy
  904. else
  905. p.right:=nil;
  906. result:=p;
  907. end;
  908. procedure tbinarynode.insertintolist(l : tnodelist);
  909. begin
  910. end;
  911. procedure tbinarynode.swapleftright;
  912. var
  913. swapp : tnode;
  914. begin
  915. swapp:=right;
  916. right:=left;
  917. left:=swapp;
  918. if nf_swapped in flags then
  919. exclude(flags,nf_swapped)
  920. else
  921. include(flags,nf_swapped);
  922. end;
  923. procedure tbinarynode.printnodedata(var t:text);
  924. begin
  925. inherited printnodedata(t);
  926. printnode(t,right);
  927. end;
  928. procedure tbinarynode.printnodelist(var t:text);
  929. var
  930. hp : tbinarynode;
  931. begin
  932. hp:=self;
  933. while assigned(hp) do
  934. begin
  935. write(t,printnodeindention,'(');
  936. printnodeindent;
  937. hp.printnodeinfo(t);
  938. writeln(t);
  939. printnode(t,hp.left);
  940. writeln(t);
  941. printnodeunindent;
  942. writeln(t,printnodeindention,')');
  943. hp:=tbinarynode(hp.right);
  944. end;
  945. end;
  946. {****************************************************************************
  947. TTERTIARYNODE
  948. ****************************************************************************}
  949. constructor ttertiarynode.create(_t:tnodetype;l,r,t : tnode);
  950. begin
  951. inherited create(_t,l,r);
  952. third:=t;
  953. end;
  954. constructor ttertiarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  955. begin
  956. inherited ppuload(t,ppufile);
  957. third:=ppuloadnode(ppufile);
  958. end;
  959. destructor ttertiarynode.destroy;
  960. begin
  961. third.free;
  962. inherited destroy;
  963. end;
  964. procedure ttertiarynode.ppuwrite(ppufile:tcompilerppufile);
  965. begin
  966. inherited ppuwrite(ppufile);
  967. ppuwritenode(ppufile,third);
  968. end;
  969. procedure ttertiarynode.buildderefimpl;
  970. begin
  971. inherited buildderefimpl;
  972. if assigned(third) then
  973. third.buildderefimpl;
  974. end;
  975. procedure ttertiarynode.derefimpl;
  976. begin
  977. inherited derefimpl;
  978. if assigned(third) then
  979. third.derefimpl;
  980. end;
  981. procedure ttertiarynode.derefnode;
  982. begin
  983. inherited derefnode;
  984. if assigned(third) then
  985. third.derefnode;
  986. end;
  987. function ttertiarynode.docompare(p : tnode) : boolean;
  988. begin
  989. docompare:=(inherited docompare(p) and
  990. ((third=nil) or third.isequal(ttertiarynode(p).third))
  991. );
  992. end;
  993. function ttertiarynode.dogetcopy : tnode;
  994. var
  995. p : ttertiarynode;
  996. begin
  997. p:=ttertiarynode(inherited dogetcopy);
  998. if assigned(third) then
  999. p.third:=third.dogetcopy
  1000. else
  1001. p.third:=nil;
  1002. result:=p;
  1003. end;
  1004. procedure ttertiarynode.insertintolist(l : tnodelist);
  1005. begin
  1006. end;
  1007. procedure ttertiarynode.printnodedata(var t:text);
  1008. begin
  1009. inherited printnodedata(t);
  1010. printnode(t,third);
  1011. end;
  1012. procedure ttertiarynode.concattolist(l : tlinkedlist);
  1013. begin
  1014. third.parent:=self;
  1015. third.concattolist(l);
  1016. inherited concattolist(l);
  1017. end;
  1018. function ttertiarynode.ischild(p : tnode) : boolean;
  1019. begin
  1020. ischild:=p=third;
  1021. end;
  1022. {****************************************************************************
  1023. TBINOPNODE
  1024. ****************************************************************************}
  1025. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  1026. begin
  1027. inherited create(t,l,r);
  1028. end;
  1029. function tbinopnode.docompare(p : tnode) : boolean;
  1030. begin
  1031. docompare:=(inherited docompare(p)) or
  1032. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  1033. ((nf_swapable in flags) and
  1034. left.isequal(tbinopnode(p).right) and
  1035. right.isequal(tbinopnode(p).left));
  1036. end;
  1037. end.