node.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. type
  27. tnodetype = (
  28. emptynode, {No node (returns nil when loading from ppu)}
  29. addn, {Represents the + operator}
  30. muln, {Represents the * operator}
  31. subn, {Represents the - operator}
  32. divn, {Represents the div operator}
  33. symdifn, {Represents the >< operator}
  34. modn, {Represents the mod operator}
  35. assignn, {Represents an assignment}
  36. loadn, {Represents the use of a variabele}
  37. rangen, {Represents a range (i.e. 0..9)}
  38. ltn, {Represents the < operator}
  39. lten, {Represents the <= operator}
  40. gtn, {Represents the > operator}
  41. gten, {Represents the >= operator}
  42. equaln, {Represents the = operator}
  43. unequaln, {Represents the <> operator}
  44. inn, {Represents the in operator}
  45. orn, {Represents the or operator}
  46. xorn, {Represents the xor operator}
  47. shrn, {Represents the shr operator}
  48. shln, {Represents the shl operator}
  49. slashn, {Represents the / operator}
  50. andn, {Represents the and operator}
  51. subscriptn, {Field in a record/object}
  52. derefn, {Dereferences a pointer}
  53. addrn, {Represents the @ operator}
  54. ordconstn, {Represents an ordinal value}
  55. typeconvn, {Represents type-conversion/typecast}
  56. calln, {Represents a call node}
  57. callparan, {Represents a parameter}
  58. realconstn, {Represents a real value}
  59. unaryminusn, {Represents a sign change (i.e. -2)}
  60. asmn, {Represents an assembler node }
  61. vecn, {Represents array indexing}
  62. pointerconstn, {Represents a pointer constant}
  63. stringconstn, {Represents a string constant}
  64. notn, {Represents the not operator}
  65. inlinen, {Internal procedures (i.e. writeln)}
  66. niln, {Represents the nil pointer}
  67. errorn, {This part of the tree could not be
  68. parsed because of a compiler error}
  69. typen, {A type name. Used for i.e. typeof(obj)}
  70. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  71. setconstn, {A set constant (i.e. [1,2])}
  72. blockn, {A block of statements}
  73. statementn, {One statement in a block of nodes}
  74. ifn, {An if statement}
  75. breakn, {A break statement}
  76. continuen, {A continue statement}
  77. whilerepeatn, {A while or repeat statement}
  78. forn, {A for loop}
  79. exitn, {An exit statement}
  80. withn, {A with statement}
  81. casen, {A case statement}
  82. labeln, {A label}
  83. goton, {A goto statement}
  84. tryexceptn, {A try except block}
  85. raisen, {A raise statement}
  86. tryfinallyn, {A try finally statement}
  87. onn, {For an on statement in exception code}
  88. isn, {Represents the is operator}
  89. asn, {Represents the as typecast}
  90. starstarn, {Represents the ** operator exponentiation }
  91. arrayconstructorn, {Construction node for [...] parsing}
  92. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  93. tempcreaten, { for temps in the result/firstpass }
  94. temprefn, { references to temps }
  95. tempdeleten, { for temps in the result/firstpass }
  96. addoptn, { added for optimizations where we cannot suppress }
  97. nothingn, { NOP, Do nothing}
  98. loadvmtaddrn, { Load the address of the VMT of a class/object}
  99. guidconstn, { A GUID COM Interface constant }
  100. rttin, { Rtti information so they can be accessed in result/firstpass}
  101. loadparentfpn, { Load the framepointer of the parent for nested procedures }
  102. dataconstn { node storing some binary data }
  103. );
  104. tnodetypeset = set of tnodetype;
  105. pnodetypeset = ^tnodetypeset;
  106. const
  107. nodetype2str : array[tnodetype] of string[24] = (
  108. '<emptynode>',
  109. 'addn',
  110. 'muln',
  111. 'subn',
  112. 'divn',
  113. 'symdifn',
  114. 'modn',
  115. 'assignn',
  116. 'loadn',
  117. 'rangen',
  118. 'ltn',
  119. 'lten',
  120. 'gtn',
  121. 'gten',
  122. 'equaln',
  123. 'unequaln',
  124. 'inn',
  125. 'orn',
  126. 'xorn',
  127. 'shrn',
  128. 'shln',
  129. 'slashn',
  130. 'andn',
  131. 'subscriptn',
  132. 'derefn',
  133. 'addrn',
  134. 'ordconstn',
  135. 'typeconvn',
  136. 'calln',
  137. 'callparan',
  138. 'realconstn',
  139. 'unaryminusn',
  140. 'asmn',
  141. 'vecn',
  142. 'pointerconstn',
  143. 'stringconstn',
  144. 'notn',
  145. 'inlinen',
  146. 'niln',
  147. 'errorn',
  148. 'typen',
  149. 'setelementn',
  150. 'setconstn',
  151. 'blockn',
  152. 'statementn',
  153. 'ifn',
  154. 'breakn',
  155. 'continuen',
  156. 'whilerepeatn',
  157. 'forn',
  158. 'exitn',
  159. 'withn',
  160. 'casen',
  161. 'labeln',
  162. 'goton',
  163. 'tryexceptn',
  164. 'raisen',
  165. 'tryfinallyn',
  166. 'onn',
  167. 'isn',
  168. 'asn',
  169. 'starstarn',
  170. 'arrayconstructn',
  171. 'arrayconstructrangen',
  172. 'tempcreaten',
  173. 'temprefn',
  174. 'tempdeleten',
  175. 'addoptn',
  176. 'nothingn',
  177. 'loadvmtaddrn',
  178. 'guidconstn',
  179. 'rttin',
  180. 'loadparentfpn',
  181. 'dataconstn');
  182. type
  183. { all boolean field of ttree are now collected in flags }
  184. tnodeflag = (
  185. nf_swapable, { tbinop operands can be swaped }
  186. nf_swapped, { tbinop operands are swaped }
  187. nf_error,
  188. { general }
  189. nf_pass1_done,
  190. nf_write, { Node is written to }
  191. nf_isproperty,
  192. { taddrnode }
  193. nf_typedaddr,
  194. { tderefnode }
  195. nf_no_checkpointer,
  196. { tvecnode }
  197. nf_memindex,
  198. nf_memseg,
  199. nf_callunique,
  200. { tloadnode }
  201. nf_absolute,
  202. nf_is_self,
  203. nf_load_self_pointer,
  204. nf_inherited,
  205. { the loadnode is generated internally and a varspez=vs_const should be ignore,
  206. this requires that the parameter is actually passed by value
  207. Be really carefull when using this flag! }
  208. nf_isinternal_ignoreconst,
  209. { taddnode }
  210. nf_is_currency,
  211. nf_has_pointerdiv,
  212. nf_short_bool,
  213. { tassignmentnode }
  214. nf_assign_done_in_right,
  215. { tarrayconstructnode }
  216. nf_forcevaria,
  217. nf_novariaallowed,
  218. { ttypeconvnode, and the first one also treal/ord/pointerconstn }
  219. nf_explicit,
  220. nf_internal, { no warnings/hints generated }
  221. nf_load_procvar,
  222. { tinlinenode }
  223. nf_inlineconst,
  224. { tasmnode }
  225. nf_get_asm_position,
  226. { tblocknode }
  227. nf_block_with_exit
  228. );
  229. tnodeflags = set of tnodeflag;
  230. const
  231. { contains the flags which must be equal for the equality }
  232. { of nodes }
  233. flagsequal : tnodeflags = [nf_error];
  234. type
  235. tnodelist = class
  236. end;
  237. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  238. tnode = class
  239. public
  240. { type of this node }
  241. nodetype : tnodetype;
  242. { type of the current code block, general/const/type }
  243. blocktype : tblock_type;
  244. { expected location of the result of this node (pass1) }
  245. expectloc : tcgloc;
  246. { the location of the result of this node (pass2) }
  247. location : tlocation;
  248. { the parent node of this is node }
  249. { this field is set by concattolist }
  250. parent : tnode;
  251. { there are some properties about the node stored }
  252. flags : tnodeflags;
  253. ppuidx : longint;
  254. { the number of registers needed to evalute the node }
  255. registersint,registersfpu,registersmm : longint; { must be longint !!!! }
  256. {$ifdef SUPPORT_MMX}
  257. registersmmx : longint;
  258. {$endif SUPPORT_MMX}
  259. resultdef : tdef;
  260. resultdefderef : tderef;
  261. fileinfo : tfileposinfo;
  262. localswitches : tlocalswitches;
  263. {$ifdef extdebug}
  264. maxfirstpasscount,
  265. firstpasscount : longint;
  266. {$endif extdebug}
  267. constructor create(t:tnodetype);
  268. { this constructor is only for creating copies of class }
  269. { the fields are copied by getcopy }
  270. constructor createforcopy;
  271. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  272. destructor destroy;override;
  273. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  274. procedure buildderefimpl;virtual;
  275. procedure derefimpl;virtual;
  276. procedure derefnode;virtual;
  277. { toggles the flag }
  278. procedure toggleflag(f : tnodeflag);
  279. { the 1.1 code generator may override pass_1 }
  280. { and it need not to implement det_* then }
  281. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  282. { 2.0: runs pass_typecheck and det_temp }
  283. function pass_1 : tnode;virtual;abstract;
  284. { dermines the resultdef of the node }
  285. function pass_typecheck : tnode;virtual;abstract;
  286. { tries to simplify the node, returns a value <>nil if a simplified
  287. node has been created }
  288. function simplify : tnode;virtual;
  289. {$ifdef state_tracking}
  290. { Does optimizations by keeping track of the variable states
  291. in a procedure }
  292. function track_state_pass(exec_known:boolean):boolean;virtual;
  293. {$endif}
  294. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  295. written to (normally the loadnode) as write access. }
  296. procedure mark_write;virtual;
  297. { dermines the number of necessary temp. locations to evaluate
  298. the node }
  299. procedure det_temp;virtual;abstract;
  300. procedure pass_generate_code;virtual;abstract;
  301. { comparing of nodes }
  302. function isequal(p : tnode) : boolean;
  303. { to implement comparisation, override this method }
  304. function docompare(p : tnode) : boolean;virtual;
  305. { wrapper for getcopy }
  306. function getcopy : tnode;
  307. { does the real copying of a node }
  308. function dogetcopy : tnode;virtual;
  309. procedure insertintolist(l : tnodelist);virtual;
  310. { writes a node for debugging purpose, shouldn't be called }
  311. { direct, because there is no test for nil, use printnode }
  312. { to write a complete tree }
  313. procedure printnodeinfo(var t:text);virtual;
  314. procedure printnodedata(var t:text);virtual;
  315. procedure printnodetree(var t:text);virtual;
  316. procedure concattolist(l : tlinkedlist);virtual;
  317. function ischild(p : tnode) : boolean;virtual;
  318. end;
  319. tnodeclass = class of tnode;
  320. tnodeclassarray = array[tnodetype] of tnodeclass;
  321. { this node is the anchestor for all nodes with at least }
  322. { one child, you have to use it if you want to use }
  323. { true- and current_procinfo.CurrFalseLabel }
  324. punarynode = ^tunarynode;
  325. tunarynode = class(tnode)
  326. left : tnode;
  327. constructor create(t:tnodetype;l : tnode);
  328. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  329. destructor destroy;override;
  330. procedure ppuwrite(ppufile:tcompilerppufile);override;
  331. procedure buildderefimpl;override;
  332. procedure derefimpl;override;
  333. procedure derefnode;override;
  334. procedure concattolist(l : tlinkedlist);override;
  335. function ischild(p : tnode) : boolean;override;
  336. function docompare(p : tnode) : boolean;override;
  337. function dogetcopy : tnode;override;
  338. procedure insertintolist(l : tnodelist);override;
  339. procedure left_max;
  340. procedure printnodedata(var t:text);override;
  341. end;
  342. pbinarynode = ^tbinarynode;
  343. tbinarynode = class(tunarynode)
  344. right : tnode;
  345. constructor create(t:tnodetype;l,r : tnode);
  346. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  347. destructor destroy;override;
  348. procedure ppuwrite(ppufile:tcompilerppufile);override;
  349. procedure buildderefimpl;override;
  350. procedure derefimpl;override;
  351. procedure derefnode;override;
  352. procedure concattolist(l : tlinkedlist);override;
  353. function ischild(p : tnode) : boolean;override;
  354. function docompare(p : tnode) : boolean;override;
  355. procedure swapleftright;
  356. function dogetcopy : tnode;override;
  357. procedure insertintolist(l : tnodelist);override;
  358. procedure left_right_max;
  359. procedure printnodedata(var t:text);override;
  360. procedure printnodelist(var t:text);
  361. end;
  362. ptertiarynode = ^ttertiarynode;
  363. ttertiarynode = class(tbinarynode)
  364. third : tnode;
  365. constructor create(_t:tnodetype;l,r,t : 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 derefnode;override;
  372. procedure concattolist(l : tlinkedlist);override;
  373. function ischild(p : tnode) : boolean;override;
  374. function docompare(p : tnode) : boolean;override;
  375. function dogetcopy : tnode;override;
  376. procedure insertintolist(l : tnodelist);override;
  377. procedure printnodedata(var t:text);override;
  378. end;
  379. tbinopnode = class(tbinarynode)
  380. constructor create(t:tnodetype;l,r : tnode);virtual;
  381. function docompare(p : tnode) : boolean;override;
  382. end;
  383. var
  384. { array with all class types for tnodes }
  385. nodeclass : tnodeclassarray;
  386. function nodeppuidxget(i:longint):tnode;
  387. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  388. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  389. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  390. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  391. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  392. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  393. const
  394. printnodespacing = ' ';
  395. var
  396. { indention used when writing the tree to the screen }
  397. printnodeindention : string;
  398. procedure printnodeindent;
  399. procedure printnodeunindent;
  400. procedure printnode(var t:text;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. function is_constnode(p : tnode) : boolean;
  537. begin
  538. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  539. end;
  540. function is_constintnode(p : tnode) : boolean;
  541. begin
  542. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resultdef);
  543. end;
  544. function is_constcharnode(p : tnode) : boolean;
  545. begin
  546. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resultdef);
  547. end;
  548. function is_constwidecharnode(p : tnode) : boolean;
  549. begin
  550. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resultdef);
  551. end;
  552. function is_constrealnode(p : tnode) : boolean;
  553. begin
  554. is_constrealnode:=(p.nodetype=realconstn);
  555. end;
  556. function is_constboolnode(p : tnode) : boolean;
  557. begin
  558. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resultdef);
  559. end;
  560. function is_constenumnode(p : tnode) : boolean;
  561. begin
  562. is_constenumnode:=(p.nodetype=ordconstn) and (p.resultdef.typ=enumdef);
  563. end;
  564. {****************************************************************************
  565. TNODE
  566. ****************************************************************************}
  567. constructor tnode.create(t:tnodetype);
  568. begin
  569. inherited create;
  570. nodetype:=t;
  571. blocktype:=block_type;
  572. { updated by firstpass }
  573. expectloc:=LOC_INVALID;
  574. { updated by secondpass }
  575. location.loc:=LOC_INVALID;
  576. { save local info }
  577. fileinfo:=current_filepos;
  578. localswitches:=current_settings.localswitches;
  579. resultdef:=nil;
  580. registersint:=0;
  581. registersfpu:=0;
  582. {$ifdef SUPPORT_MMX}
  583. registersmmx:=0;
  584. {$endif SUPPORT_MMX}
  585. {$ifdef EXTDEBUG}
  586. maxfirstpasscount:=0;
  587. firstpasscount:=0;
  588. {$endif EXTDEBUG}
  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. {$ifdef EXTDEBUG}
  614. maxfirstpasscount:=0;
  615. firstpasscount:=0;
  616. {$endif EXTDEBUG}
  617. ppuidx:=-1;
  618. end;
  619. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  620. begin
  621. ppufile.putbyte(byte(block_type));
  622. ppufile.putposinfo(fileinfo);
  623. ppufile.putsmallset(localswitches);
  624. ppufile.putderef(resultdefderef);
  625. ppufile.putsmallset(flags);
  626. end;
  627. procedure tnode.buildderefimpl;
  628. begin
  629. resultdefderef.build(resultdef);
  630. end;
  631. procedure tnode.derefimpl;
  632. begin
  633. resultdef:=tdef(resultdefderef.resolve);
  634. end;
  635. procedure tnode.derefnode;
  636. begin
  637. end;
  638. procedure tnode.toggleflag(f : tnodeflag);
  639. begin
  640. if f in flags then
  641. exclude(flags,f)
  642. else
  643. include(flags,f);
  644. end;
  645. function tnode.simplify : tnode;
  646. begin
  647. result:=nil;
  648. end;
  649. destructor tnode.destroy;
  650. begin
  651. {$ifdef EXTDEBUG}
  652. if firstpasscount>maxfirstpasscount then
  653. maxfirstpasscount:=firstpasscount;
  654. {$endif EXTDEBUG}
  655. end;
  656. procedure tnode.concattolist(l : tlinkedlist);
  657. begin
  658. end;
  659. function tnode.ischild(p : tnode) : boolean;
  660. begin
  661. ischild:=false;
  662. end;
  663. procedure tnode.mark_write;
  664. begin
  665. {$ifdef EXTDEBUG}
  666. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  667. {$endif EXTDEBUG}
  668. end;
  669. procedure tnode.printnodeinfo(var t:text);
  670. begin
  671. write(t,nodetype2str[nodetype]);
  672. if assigned(resultdef) then
  673. write(t,', resultdef = "',resultdef.GetTypeName,'"')
  674. else
  675. write(t,', resultdef = <nil>');
  676. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  677. ', loc = ',tcgloc2str[location.loc],
  678. ', expectloc = ',tcgloc2str[expectloc],
  679. ', intregs = ',registersint,
  680. ', fpuregs = ',registersfpu);
  681. end;
  682. procedure tnode.printnodedata(var t:text);
  683. begin
  684. end;
  685. procedure tnode.printnodetree(var t:text);
  686. begin
  687. write(t,printnodeindention,'(');
  688. printnodeinfo(t);
  689. writeln(t);
  690. printnodeindent;
  691. printnodedata(t);
  692. printnodeunindent;
  693. writeln(t,printnodeindention,')');
  694. end;
  695. function tnode.isequal(p : tnode) : boolean;
  696. begin
  697. isequal:=
  698. (not assigned(self) and not assigned(p)) or
  699. (assigned(self) and assigned(p) and
  700. { optimized subclasses have the same nodetype as their }
  701. { superclass (for compatibility), so also check the classtype (JM) }
  702. (p.classtype=classtype) and
  703. (p.nodetype=nodetype) and
  704. (flags*flagsequal=p.flags*flagsequal) and
  705. docompare(p));
  706. end;
  707. {$ifdef state_tracking}
  708. function Tnode.track_state_pass(exec_known:boolean):boolean;
  709. begin
  710. track_state_pass:=false;
  711. end;
  712. {$endif state_tracking}
  713. function tnode.docompare(p : tnode) : boolean;
  714. begin
  715. docompare:=true;
  716. end;
  717. function cleanupcopiedto(var n : tnode;arg : pointer) : foreachnoderesult;
  718. begin
  719. result:=fen_true;
  720. if n.nodetype=labeln then
  721. tlabelnode(n).copiedto:=nil;
  722. end;
  723. function tnode.getcopy : tnode;
  724. begin
  725. result:=dogetcopy;
  726. foreachnodestatic(pm_postprocess,self,@cleanupcopiedto,nil);
  727. end;
  728. function tnode.dogetcopy : tnode;
  729. var
  730. p : tnode;
  731. begin
  732. { this is quite tricky because we need a node of the current }
  733. { node type and not one of tnode! }
  734. p:=tnodeclass(classtype).createforcopy;
  735. p.nodetype:=nodetype;
  736. p.expectloc:=expectloc;
  737. p.location:=location;
  738. p.parent:=parent;
  739. p.flags:=flags;
  740. p.registersint:=registersint;
  741. p.registersfpu:=registersfpu;
  742. {$ifdef SUPPORT_MMX}
  743. p.registersmmx:=registersmmx;
  744. p.registersmm:=registersmm;
  745. {$endif SUPPORT_MMX}
  746. p.resultdef:=resultdef;
  747. p.fileinfo:=fileinfo;
  748. p.localswitches:=localswitches;
  749. {$ifdef extdebug}
  750. p.firstpasscount:=firstpasscount;
  751. {$endif extdebug}
  752. { p.list:=list; }
  753. result:=p;
  754. end;
  755. procedure tnode.insertintolist(l : tnodelist);
  756. begin
  757. end;
  758. {****************************************************************************
  759. TUNARYNODE
  760. ****************************************************************************}
  761. constructor tunarynode.create(t:tnodetype;l : tnode);
  762. begin
  763. inherited create(t);
  764. left:=l;
  765. end;
  766. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  767. begin
  768. inherited ppuload(t,ppufile);
  769. left:=ppuloadnode(ppufile);
  770. end;
  771. destructor tunarynode.destroy;
  772. begin
  773. left.free;
  774. inherited destroy;
  775. end;
  776. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  777. begin
  778. inherited ppuwrite(ppufile);
  779. ppuwritenode(ppufile,left);
  780. end;
  781. procedure tunarynode.buildderefimpl;
  782. begin
  783. inherited buildderefimpl;
  784. if assigned(left) then
  785. left.buildderefimpl;
  786. end;
  787. procedure tunarynode.derefimpl;
  788. begin
  789. inherited derefimpl;
  790. if assigned(left) then
  791. left.derefimpl;
  792. end;
  793. procedure tunarynode.derefnode;
  794. begin
  795. inherited derefnode;
  796. if assigned(left) then
  797. left.derefnode;
  798. end;
  799. function tunarynode.docompare(p : tnode) : boolean;
  800. begin
  801. docompare:=(inherited docompare(p) and
  802. ((left=nil) or left.isequal(tunarynode(p).left))
  803. );
  804. end;
  805. function tunarynode.dogetcopy : tnode;
  806. var
  807. p : tunarynode;
  808. begin
  809. p:=tunarynode(inherited dogetcopy);
  810. if assigned(left) then
  811. p.left:=left.dogetcopy
  812. else
  813. p.left:=nil;
  814. result:=p;
  815. end;
  816. procedure tunarynode.insertintolist(l : tnodelist);
  817. begin
  818. end;
  819. procedure tunarynode.printnodedata(var t:text);
  820. begin
  821. inherited printnodedata(t);
  822. printnode(t,left);
  823. end;
  824. procedure tunarynode.left_max;
  825. begin
  826. registersint:=left.registersint;
  827. registersfpu:=left.registersfpu;
  828. {$ifdef SUPPORT_MMX}
  829. registersmmx:=left.registersmmx;
  830. {$endif SUPPORT_MMX}
  831. end;
  832. procedure tunarynode.concattolist(l : tlinkedlist);
  833. begin
  834. left.parent:=self;
  835. left.concattolist(l);
  836. inherited concattolist(l);
  837. end;
  838. function tunarynode.ischild(p : tnode) : boolean;
  839. begin
  840. ischild:=p=left;
  841. end;
  842. {****************************************************************************
  843. TBINARYNODE
  844. ****************************************************************************}
  845. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  846. begin
  847. inherited create(t,l);
  848. right:=r
  849. end;
  850. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  851. begin
  852. inherited ppuload(t,ppufile);
  853. right:=ppuloadnode(ppufile);
  854. end;
  855. destructor tbinarynode.destroy;
  856. begin
  857. right.free;
  858. inherited destroy;
  859. end;
  860. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  861. begin
  862. inherited ppuwrite(ppufile);
  863. ppuwritenode(ppufile,right);
  864. end;
  865. procedure tbinarynode.buildderefimpl;
  866. begin
  867. inherited buildderefimpl;
  868. if assigned(right) then
  869. right.buildderefimpl;
  870. end;
  871. procedure tbinarynode.derefimpl;
  872. begin
  873. inherited derefimpl;
  874. if assigned(right) then
  875. right.derefimpl;
  876. end;
  877. procedure tbinarynode.derefnode;
  878. begin
  879. inherited derefnode;
  880. if assigned(right) then
  881. right.derefnode;
  882. end;
  883. procedure tbinarynode.concattolist(l : tlinkedlist);
  884. begin
  885. { we could change that depending on the number of }
  886. { required registers }
  887. left.parent:=self;
  888. left.concattolist(l);
  889. left.parent:=self;
  890. left.concattolist(l);
  891. inherited concattolist(l);
  892. end;
  893. function tbinarynode.ischild(p : tnode) : boolean;
  894. begin
  895. ischild:=(p=right);
  896. end;
  897. function tbinarynode.docompare(p : tnode) : boolean;
  898. begin
  899. docompare:=(inherited docompare(p) and
  900. ((right=nil) or right.isequal(tbinarynode(p).right))
  901. );
  902. end;
  903. function tbinarynode.dogetcopy : tnode;
  904. var
  905. p : tbinarynode;
  906. begin
  907. p:=tbinarynode(inherited dogetcopy);
  908. if assigned(right) then
  909. p.right:=right.dogetcopy
  910. else
  911. p.right:=nil;
  912. result:=p;
  913. end;
  914. procedure tbinarynode.insertintolist(l : tnodelist);
  915. begin
  916. end;
  917. procedure tbinarynode.swapleftright;
  918. var
  919. swapp : tnode;
  920. begin
  921. swapp:=right;
  922. right:=left;
  923. left:=swapp;
  924. if nf_swapped in flags then
  925. exclude(flags,nf_swapped)
  926. else
  927. include(flags,nf_swapped);
  928. end;
  929. procedure tbinarynode.left_right_max;
  930. begin
  931. if assigned(left) then
  932. begin
  933. if assigned(right) then
  934. begin
  935. registersint:=max(left.registersint,right.registersint);
  936. registersfpu:=max(left.registersfpu,right.registersfpu);
  937. {$ifdef SUPPORT_MMX}
  938. registersmmx:=max(left.registersmmx,right.registersmmx);
  939. {$endif SUPPORT_MMX}
  940. end
  941. else
  942. begin
  943. registersint:=left.registersint;
  944. registersfpu:=left.registersfpu;
  945. {$ifdef SUPPORT_MMX}
  946. registersmmx:=left.registersmmx;
  947. {$endif SUPPORT_MMX}
  948. end;
  949. end;
  950. end;
  951. procedure tbinarynode.printnodedata(var t:text);
  952. begin
  953. inherited printnodedata(t);
  954. printnode(t,right);
  955. end;
  956. procedure tbinarynode.printnodelist(var t:text);
  957. var
  958. hp : tbinarynode;
  959. begin
  960. hp:=self;
  961. while assigned(hp) do
  962. begin
  963. write(t,printnodeindention,'(');
  964. printnodeindent;
  965. hp.printnodeinfo(t);
  966. writeln(t);
  967. printnode(t,hp.left);
  968. writeln(t);
  969. printnodeunindent;
  970. writeln(t,printnodeindention,')');
  971. hp:=tbinarynode(hp.right);
  972. end;
  973. end;
  974. {****************************************************************************
  975. TTERTIARYNODE
  976. ****************************************************************************}
  977. constructor ttertiarynode.create(_t:tnodetype;l,r,t : tnode);
  978. begin
  979. inherited create(_t,l,r);
  980. third:=t;
  981. end;
  982. constructor ttertiarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  983. begin
  984. inherited ppuload(t,ppufile);
  985. third:=ppuloadnode(ppufile);
  986. end;
  987. destructor ttertiarynode.destroy;
  988. begin
  989. third.free;
  990. inherited destroy;
  991. end;
  992. procedure ttertiarynode.ppuwrite(ppufile:tcompilerppufile);
  993. begin
  994. inherited ppuwrite(ppufile);
  995. ppuwritenode(ppufile,third);
  996. end;
  997. procedure ttertiarynode.buildderefimpl;
  998. begin
  999. inherited buildderefimpl;
  1000. if assigned(third) then
  1001. third.buildderefimpl;
  1002. end;
  1003. procedure ttertiarynode.derefimpl;
  1004. begin
  1005. inherited derefimpl;
  1006. if assigned(third) then
  1007. third.derefimpl;
  1008. end;
  1009. procedure ttertiarynode.derefnode;
  1010. begin
  1011. inherited derefnode;
  1012. if assigned(third) then
  1013. third.derefnode;
  1014. end;
  1015. function ttertiarynode.docompare(p : tnode) : boolean;
  1016. begin
  1017. docompare:=(inherited docompare(p) and
  1018. ((third=nil) or third.isequal(ttertiarynode(p).third))
  1019. );
  1020. end;
  1021. function ttertiarynode.dogetcopy : tnode;
  1022. var
  1023. p : ttertiarynode;
  1024. begin
  1025. p:=ttertiarynode(inherited dogetcopy);
  1026. if assigned(third) then
  1027. p.third:=third.dogetcopy
  1028. else
  1029. p.third:=nil;
  1030. result:=p;
  1031. end;
  1032. procedure ttertiarynode.insertintolist(l : tnodelist);
  1033. begin
  1034. end;
  1035. procedure ttertiarynode.printnodedata(var t:text);
  1036. begin
  1037. inherited printnodedata(t);
  1038. printnode(t,third);
  1039. end;
  1040. procedure ttertiarynode.concattolist(l : tlinkedlist);
  1041. begin
  1042. third.parent:=self;
  1043. third.concattolist(l);
  1044. inherited concattolist(l);
  1045. end;
  1046. function ttertiarynode.ischild(p : tnode) : boolean;
  1047. begin
  1048. ischild:=p=third;
  1049. end;
  1050. {****************************************************************************
  1051. TBINOPNODE
  1052. ****************************************************************************}
  1053. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  1054. begin
  1055. inherited create(t,l,r);
  1056. end;
  1057. function tbinopnode.docompare(p : tnode) : boolean;
  1058. begin
  1059. docompare:=(inherited docompare(p)) or
  1060. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  1061. ((nf_swapable in flags) and
  1062. left.isequal(tbinopnode(p).right) and
  1063. right.isequal(tbinopnode(p).left));
  1064. end;
  1065. end.