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