node.pas 36 KB

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