node.pas 33 KB

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