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