node.pas 36 KB

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