node.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Basic node handling
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit node;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. globtype,globals,
  24. cpubase,cgbase,cgutils,
  25. aasmbase,
  26. symtype;
  27. type
  28. pconstset = ^tconstset;
  29. tconstset = set of 0..255;
  30. tnodetype = (
  31. emptynode, {No node (returns nil when loading from ppu)}
  32. addn, {Represents the + operator}
  33. muln, {Represents the * operator}
  34. subn, {Represents the - operator}
  35. divn, {Represents the div operator}
  36. symdifn, {Represents the >< operator}
  37. modn, {Represents the mod operator}
  38. assignn, {Represents an assignment}
  39. loadn, {Represents the use of a variabele}
  40. rangen, {Represents a range (i.e. 0..9)}
  41. ltn, {Represents the < operator}
  42. lten, {Represents the <= operator}
  43. gtn, {Represents the > operator}
  44. gten, {Represents the >= operator}
  45. equaln, {Represents the = operator}
  46. unequaln, {Represents the <> operator}
  47. inn, {Represents the in operator}
  48. orn, {Represents the or operator}
  49. xorn, {Represents the xor operator}
  50. shrn, {Represents the shr operator}
  51. shln, {Represents the shl operator}
  52. slashn, {Represents the / operator}
  53. andn, {Represents the and operator}
  54. subscriptn, {Field in a record/object}
  55. derefn, {Dereferences a pointer}
  56. addrn, {Represents the @ operator}
  57. ordconstn, {Represents an ordinal value}
  58. typeconvn, {Represents type-conversion/typecast}
  59. calln, {Represents a call node}
  60. callparan, {Represents a parameter}
  61. realconstn, {Represents a real value}
  62. unaryminusn, {Represents a sign change (i.e. -2)}
  63. asmn, {Represents an assembler node }
  64. vecn, {Represents array indexing}
  65. pointerconstn, {Represents a pointer constant}
  66. stringconstn, {Represents a string constant}
  67. notn, {Represents the not operator}
  68. inlinen, {Internal procedures (i.e. writeln)}
  69. niln, {Represents the nil pointer}
  70. errorn, {This part of the tree could not be
  71. parsed because of a compiler error}
  72. typen, {A type name. Used for i.e. typeof(obj)}
  73. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  74. setconstn, {A set constant (i.e. [1,2])}
  75. blockn, {A block of statements}
  76. statementn, {One statement in a block of nodes}
  77. ifn, {An if statement}
  78. breakn, {A break statement}
  79. continuen, {A continue statement}
  80. whilerepeatn, {A while or repeat statement}
  81. forn, {A for loop}
  82. exitn, {An exit statement}
  83. withn, {A with statement}
  84. casen, {A case statement}
  85. labeln, {A label}
  86. goton, {A goto statement}
  87. tryexceptn, {A try except block}
  88. raisen, {A raise statement}
  89. tryfinallyn, {A try finally statement}
  90. onn, {For an on statement in exception code}
  91. isn, {Represents the is operator}
  92. asn, {Represents the as typecast}
  93. caretn, {Represents the ^ operator}
  94. starstarn, {Represents the ** operator exponentiation }
  95. arrayconstructorn, {Construction node for [...] parsing}
  96. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  97. tempcreaten, { for temps in the result/firstpass }
  98. temprefn, { references to temps }
  99. tempdeleten, { for temps in the result/firstpass }
  100. addoptn, { added for optimizations where we cannot suppress }
  101. nothingn, {NOP, Do nothing}
  102. loadvmtaddrn, {Load the address of the VMT of a class/object}
  103. guidconstn, {A GUID COM Interface constant }
  104. rttin, {Rtti information so they can be accessed in result/firstpass}
  105. loadparentfpn { Load the framepointer of the parent for nested procedures }
  106. );
  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. 'caretn',
  171. 'starstarn',
  172. 'arrayconstructn',
  173. 'arrayconstructrangen',
  174. 'tempcreaten',
  175. 'temprefn',
  176. 'tempdeleten',
  177. 'addoptn',
  178. 'nothingn',
  179. 'loadvmtaddrn',
  180. 'guidconstn',
  181. 'rttin',
  182. 'loadparentfpn');
  183. type
  184. { all boolean field of ttree are now collected in flags }
  185. tnodeflag = (
  186. nf_swapable, { tbinop operands can be swaped }
  187. nf_swaped, { 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. { taddnode }
  206. nf_is_currency,
  207. nf_has_pointerdiv,
  208. { tassignmentnode }
  209. nf_concat_string,
  210. nf_use_strconcat,
  211. { tarrayconstructnode }
  212. nf_forcevaria,
  213. nf_novariaallowed,
  214. { ttypeconvnode }
  215. nf_explicit,
  216. nf_internal, { no warnings/hints generated }
  217. nf_load_procvar,
  218. { tinlinenode }
  219. nf_inlineconst,
  220. { tasmnode }
  221. nf_get_asm_position,
  222. { tblocknode }
  223. nf_block_with_exit
  224. );
  225. tnodeflags = set of tnodeflag;
  226. const
  227. { contains the flags which must be equal for the equality }
  228. { of nodes }
  229. flagsequal : tnodeflags = [nf_error];
  230. type
  231. tnodelist = class
  232. end;
  233. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  234. tnode = class
  235. public
  236. { type of this node }
  237. nodetype : tnodetype;
  238. { type of the current code block, general/const/type }
  239. blocktype : tblock_type;
  240. { expected location of the result of this node (pass1) }
  241. expectloc : tcgloc;
  242. { the location of the result of this node (pass2) }
  243. location : tlocation;
  244. { the parent node of this is node }
  245. { this field is set by concattolist }
  246. parent : tnode;
  247. { there are some properties about the node stored }
  248. flags : tnodeflags;
  249. ppuidx : longint;
  250. { the number of registers needed to evalute the node }
  251. registersint,registersfpu,registersmm : longint; { must be longint !!!! }
  252. {$ifdef SUPPORT_MMX}
  253. registersmmx : longint;
  254. {$endif SUPPORT_MMX}
  255. resulttype : ttype;
  256. fileinfo : tfileposinfo;
  257. localswitches : tlocalswitches;
  258. {$ifdef extdebug}
  259. maxfirstpasscount,
  260. firstpasscount : longint;
  261. {$endif extdebug}
  262. constructor create(t:tnodetype);
  263. { this constructor is only for creating copies of class }
  264. { the fields are copied by getcopy }
  265. constructor createforcopy;
  266. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  267. destructor destroy;override;
  268. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  269. procedure buildderefimpl;virtual;
  270. procedure derefimpl;virtual;
  271. procedure derefnode;virtual;
  272. { toggles the flag }
  273. procedure toggleflag(f : tnodeflag);
  274. { the 1.1 code generator may override pass_1 }
  275. { and it need not to implement det_* then }
  276. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  277. { 2.0: runs det_resulttype and det_temp }
  278. function pass_1 : tnode;virtual;abstract;
  279. { dermines the resulttype of the node }
  280. function det_resulttype : tnode;virtual;abstract;
  281. { dermines the number of necessary temp. locations to evaluate
  282. the node }
  283. {$ifdef state_tracking}
  284. { Does optimizations by keeping track of the variable states
  285. in a procedure }
  286. function track_state_pass(exec_known:boolean):boolean;virtual;
  287. {$endif}
  288. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  289. written to (normally the loadnode) as write access. }
  290. procedure mark_write;virtual;
  291. procedure det_temp;virtual;abstract;
  292. procedure pass_2;virtual;abstract;
  293. { comparing of nodes }
  294. function isequal(p : tnode) : boolean;
  295. { to implement comparisation, override this method }
  296. function docompare(p : tnode) : boolean;virtual;
  297. { gets a copy of the node }
  298. function getcopy : tnode;virtual;
  299. procedure insertintolist(l : tnodelist);virtual;
  300. { writes a node for debugging purpose, shouldn't be called }
  301. { direct, because there is no test for nil, use printnode }
  302. { to write a complete tree }
  303. procedure printnodeinfo(var t:text);virtual;
  304. procedure printnodedata(var t:text);virtual;
  305. procedure printnodetree(var t:text);virtual;
  306. procedure concattolist(l : tlinkedlist);virtual;
  307. function ischild(p : tnode) : boolean;virtual;
  308. end;
  309. tnodeclass = class of tnode;
  310. tnodeclassarray = array[tnodetype] of tnodeclass;
  311. { this node is the anchestor for all nodes with at least }
  312. { one child, you have to use it if you want to use }
  313. { true- and falselabel }
  314. punarynode = ^tunarynode;
  315. tunarynode = class(tnode)
  316. left : tnode;
  317. constructor create(t:tnodetype;l : tnode);
  318. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  319. destructor destroy;override;
  320. procedure ppuwrite(ppufile:tcompilerppufile);override;
  321. procedure buildderefimpl;override;
  322. procedure derefimpl;override;
  323. procedure derefnode;override;
  324. procedure concattolist(l : tlinkedlist);override;
  325. function ischild(p : tnode) : boolean;override;
  326. function docompare(p : tnode) : boolean;override;
  327. function getcopy : tnode;override;
  328. procedure insertintolist(l : tnodelist);override;
  329. procedure left_max;
  330. procedure printnodedata(var t:text);override;
  331. end;
  332. pbinarynode = ^tbinarynode;
  333. tbinarynode = class(tunarynode)
  334. right : tnode;
  335. constructor create(t:tnodetype;l,r : tnode);
  336. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  337. destructor destroy;override;
  338. procedure ppuwrite(ppufile:tcompilerppufile);override;
  339. procedure buildderefimpl;override;
  340. procedure derefimpl;override;
  341. procedure derefnode;override;
  342. procedure concattolist(l : tlinkedlist);override;
  343. function ischild(p : tnode) : boolean;override;
  344. function docompare(p : tnode) : boolean;override;
  345. procedure swapleftright;
  346. function getcopy : tnode;override;
  347. procedure insertintolist(l : tnodelist);override;
  348. procedure left_right_max;
  349. procedure printnodedata(var t:text);override;
  350. procedure printnodelist(var t:text);
  351. end;
  352. tbinopnode = class(tbinarynode)
  353. constructor create(t:tnodetype;l,r : tnode);virtual;
  354. function docompare(p : tnode) : boolean;override;
  355. end;
  356. var
  357. { array with all class types for tnodes }
  358. nodeclass : tnodeclassarray;
  359. function nodeppuidxget(i:longint):tnode;
  360. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  361. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  362. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  363. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  364. const
  365. printnodespacing = ' ';
  366. var
  367. { indention used when writing the tree to the screen }
  368. printnodeindention : string;
  369. procedure printnodeindent;
  370. procedure printnodeunindent;
  371. procedure printnode(var t:text;n:tnode);
  372. function is_constnode(p : tnode) : boolean;
  373. function is_constintnode(p : tnode) : boolean;
  374. function is_constcharnode(p : tnode) : boolean;
  375. function is_constrealnode(p : tnode) : boolean;
  376. function is_constboolnode(p : tnode) : boolean;
  377. function is_constenumnode(p : tnode) : boolean;
  378. function is_constwidecharnode(p : tnode) : boolean;
  379. implementation
  380. uses
  381. cutils,verbose,ppu,
  382. symconst,
  383. defutil;
  384. const
  385. ppunodemarker = 255;
  386. {****************************************************************************
  387. Helpers
  388. ****************************************************************************}
  389. var
  390. nodeppudata : tdynamicarray;
  391. nodeppuidx : longint;
  392. procedure nodeppuidxcreate;
  393. begin
  394. nodeppudata:=tdynamicarray.create(1024);
  395. nodeppuidx:=0;
  396. end;
  397. procedure nodeppuidxfree;
  398. begin
  399. nodeppudata.free;
  400. nodeppudata:=nil;
  401. end;
  402. procedure nodeppuidxadd(n:tnode);
  403. begin
  404. if n.ppuidx<0 then
  405. internalerror(200311072);
  406. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  407. nodeppudata.write(n,sizeof(pointer));
  408. end;
  409. function nodeppuidxget(i:longint):tnode;
  410. var
  411. l : longint;
  412. begin
  413. if i<0 then
  414. internalerror(200311072);
  415. nodeppudata.seek(i*sizeof(pointer));
  416. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  417. internalerror(200311073);
  418. end;
  419. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  420. var
  421. b : byte;
  422. t : tnodetype;
  423. hppuidx : longint;
  424. begin
  425. { marker }
  426. b:=ppufile.getbyte;
  427. if b<>ppunodemarker then
  428. internalerror(200208151);
  429. { load nodetype }
  430. t:=tnodetype(ppufile.getbyte);
  431. if t>high(tnodetype) then
  432. internalerror(200208152);
  433. if t<>emptynode then
  434. begin
  435. if not assigned(nodeclass[t]) then
  436. internalerror(200208153);
  437. hppuidx:=ppufile.getlongint;
  438. //writeln('load: ',nodetype2str[t]);
  439. { generate node of the correct class }
  440. result:=nodeclass[t].ppuload(t,ppufile);
  441. result.ppuidx:=hppuidx;
  442. nodeppuidxadd(result);
  443. end
  444. else
  445. result:=nil;
  446. end;
  447. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  448. begin
  449. { marker, read by ppuloadnode }
  450. ppufile.putbyte(ppunodemarker);
  451. { type, read by ppuloadnode }
  452. if assigned(n) then
  453. begin
  454. if n.ppuidx=-1 then
  455. internalerror(200311071);
  456. n.ppuidx:=nodeppuidx;
  457. inc(nodeppuidx);
  458. ppufile.putbyte(byte(n.nodetype));
  459. ppufile.putlongint(n.ppuidx);
  460. //writeln('write: ',nodetype2str[n.nodetype]);
  461. n.ppuwrite(ppufile);
  462. end
  463. else
  464. ppufile.putbyte(byte(emptynode));
  465. end;
  466. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  467. begin
  468. if ppufile.readentry<>ibnodetree then
  469. Message(unit_f_ppu_read_error);
  470. nodeppuidxcreate;
  471. result:=ppuloadnode(ppufile);
  472. result.derefnode;
  473. nodeppuidxfree;
  474. end;
  475. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  476. begin
  477. nodeppuidx:=0;
  478. ppuwritenode(ppufile,n);
  479. ppufile.writeentry(ibnodetree);
  480. end;
  481. procedure printnodeindent;
  482. begin
  483. printnodeindention:=printnodeindention+printnodespacing;
  484. end;
  485. procedure printnodeunindent;
  486. begin
  487. delete(printnodeindention,1,length(printnodespacing));
  488. end;
  489. procedure printnode(var t:text;n:tnode);
  490. begin
  491. if assigned(n) then
  492. n.printnodetree(t)
  493. else
  494. writeln(t,printnodeindention,'nil');
  495. end;
  496. function is_constnode(p : tnode) : boolean;
  497. begin
  498. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  499. end;
  500. function is_constintnode(p : tnode) : boolean;
  501. begin
  502. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resulttype.def);
  503. end;
  504. function is_constcharnode(p : tnode) : boolean;
  505. begin
  506. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resulttype.def);
  507. end;
  508. function is_constwidecharnode(p : tnode) : boolean;
  509. begin
  510. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resulttype.def);
  511. end;
  512. function is_constrealnode(p : tnode) : boolean;
  513. begin
  514. is_constrealnode:=(p.nodetype=realconstn);
  515. end;
  516. function is_constboolnode(p : tnode) : boolean;
  517. begin
  518. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resulttype.def);
  519. end;
  520. function is_constenumnode(p : tnode) : boolean;
  521. begin
  522. is_constenumnode:=(p.nodetype=ordconstn) and (p.resulttype.def.deftype=enumdef);
  523. end;
  524. {****************************************************************************
  525. TNODE
  526. ****************************************************************************}
  527. constructor tnode.create(t:tnodetype);
  528. begin
  529. inherited create;
  530. nodetype:=t;
  531. blocktype:=block_type;
  532. { updated by firstpass }
  533. expectloc:=LOC_INVALID;
  534. { updated by secondpass }
  535. location.loc:=LOC_INVALID;
  536. { save local info }
  537. fileinfo:=aktfilepos;
  538. localswitches:=aktlocalswitches;
  539. resulttype.reset;
  540. registersint:=0;
  541. registersfpu:=0;
  542. {$ifdef SUPPORT_MMX}
  543. registersmmx:=0;
  544. {$endif SUPPORT_MMX}
  545. {$ifdef EXTDEBUG}
  546. maxfirstpasscount:=0;
  547. firstpasscount:=0;
  548. {$endif EXTDEBUG}
  549. flags:=[];
  550. ppuidx:=-1;
  551. end;
  552. constructor tnode.createforcopy;
  553. begin
  554. end;
  555. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  556. begin
  557. nodetype:=t;
  558. { tnode fields }
  559. blocktype:=tblock_type(ppufile.getbyte);
  560. ppufile.getposinfo(fileinfo);
  561. ppufile.getsmallset(localswitches);
  562. ppufile.gettype(resulttype);
  563. ppufile.getsmallset(flags);
  564. { updated by firstpass }
  565. expectloc:=LOC_INVALID;
  566. { updated by secondpass }
  567. location.loc:=LOC_INVALID;
  568. registersint:=0;
  569. registersfpu:=0;
  570. {$ifdef SUPPORT_MMX}
  571. registersmmx:=0;
  572. {$endif SUPPORT_MMX}
  573. {$ifdef EXTDEBUG}
  574. maxfirstpasscount:=0;
  575. firstpasscount:=0;
  576. {$endif EXTDEBUG}
  577. ppuidx:=-1;
  578. end;
  579. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  580. begin
  581. ppufile.putbyte(byte(block_type));
  582. ppufile.putposinfo(fileinfo);
  583. ppufile.putsmallset(localswitches);
  584. ppufile.puttype(resulttype);
  585. ppufile.putsmallset(flags);
  586. end;
  587. procedure tnode.buildderefimpl;
  588. begin
  589. resulttype.buildderef;
  590. end;
  591. procedure tnode.derefimpl;
  592. begin
  593. resulttype.resolve;
  594. end;
  595. procedure tnode.derefnode;
  596. begin
  597. end;
  598. procedure tnode.toggleflag(f : tnodeflag);
  599. begin
  600. if f in flags then
  601. exclude(flags,f)
  602. else
  603. include(flags,f);
  604. end;
  605. destructor tnode.destroy;
  606. begin
  607. {$ifdef EXTDEBUG}
  608. if firstpasscount>maxfirstpasscount then
  609. maxfirstpasscount:=firstpasscount;
  610. {$endif EXTDEBUG}
  611. end;
  612. procedure tnode.concattolist(l : tlinkedlist);
  613. begin
  614. end;
  615. function tnode.ischild(p : tnode) : boolean;
  616. begin
  617. ischild:=false;
  618. end;
  619. procedure tnode.mark_write;
  620. begin
  621. {$ifdef EXTDEBUG}
  622. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  623. {$endif EXTDEBUG}
  624. end;
  625. procedure tnode.printnodeinfo(var t:text);
  626. begin
  627. write(t,nodetype2str[nodetype]);
  628. if assigned(resulttype.def) then
  629. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  630. else
  631. write(t,', resulttype = <nil>');
  632. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  633. ', loc = ',tcgloc2str[location.loc],
  634. ', expectloc = ',tcgloc2str[expectloc],
  635. ', intregs = ',registersint,
  636. ', fpuregs = ',registersfpu);
  637. end;
  638. procedure tnode.printnodedata(var t:text);
  639. begin
  640. end;
  641. procedure tnode.printnodetree(var t:text);
  642. begin
  643. write(t,printnodeindention,'(');
  644. printnodeinfo(t);
  645. writeln(t);
  646. printnodeindent;
  647. printnodedata(t);
  648. printnodeunindent;
  649. writeln(t,printnodeindention,')');
  650. end;
  651. function tnode.isequal(p : tnode) : boolean;
  652. begin
  653. isequal:=
  654. (not assigned(self) and not assigned(p)) or
  655. (assigned(self) and assigned(p) and
  656. { optimized subclasses have the same nodetype as their }
  657. { superclass (for compatibility), so also check the classtype (JM) }
  658. (p.classtype=classtype) and
  659. (p.nodetype=nodetype) and
  660. (flags*flagsequal=p.flags*flagsequal) and
  661. docompare(p));
  662. end;
  663. {$ifdef state_tracking}
  664. function Tnode.track_state_pass(exec_known:boolean):boolean;
  665. begin
  666. track_state_pass:=false;
  667. end;
  668. {$endif state_tracking}
  669. function tnode.docompare(p : tnode) : boolean;
  670. begin
  671. docompare:=true;
  672. end;
  673. function tnode.getcopy : tnode;
  674. var
  675. p : tnode;
  676. begin
  677. { this is quite tricky because we need a node of the current }
  678. { node type and not one of tnode! }
  679. p:=tnodeclass(classtype).createforcopy;
  680. p.nodetype:=nodetype;
  681. p.expectloc:=expectloc;
  682. p.location:=location;
  683. p.parent:=parent;
  684. p.flags:=flags;
  685. p.registersint:=registersint;
  686. p.registersfpu:=registersfpu;
  687. {$ifdef SUPPORT_MMX}
  688. p.registersmmx:=registersmmx;
  689. p.registerskni:=registerskni;
  690. {$endif SUPPORT_MMX}
  691. p.resulttype:=resulttype;
  692. p.fileinfo:=fileinfo;
  693. p.localswitches:=localswitches;
  694. {$ifdef extdebug}
  695. p.firstpasscount:=firstpasscount;
  696. {$endif extdebug}
  697. { p.list:=list; }
  698. getcopy:=p;
  699. end;
  700. procedure tnode.insertintolist(l : tnodelist);
  701. begin
  702. end;
  703. {****************************************************************************
  704. TUNARYNODE
  705. ****************************************************************************}
  706. constructor tunarynode.create(t:tnodetype;l : tnode);
  707. begin
  708. inherited create(t);
  709. left:=l;
  710. end;
  711. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  712. begin
  713. inherited ppuload(t,ppufile);
  714. left:=ppuloadnode(ppufile);
  715. end;
  716. destructor tunarynode.destroy;
  717. begin
  718. left.free;
  719. inherited destroy;
  720. end;
  721. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  722. begin
  723. inherited ppuwrite(ppufile);
  724. ppuwritenode(ppufile,left);
  725. end;
  726. procedure tunarynode.buildderefimpl;
  727. begin
  728. inherited buildderefimpl;
  729. if assigned(left) then
  730. left.buildderefimpl;
  731. end;
  732. procedure tunarynode.derefimpl;
  733. begin
  734. inherited derefimpl;
  735. if assigned(left) then
  736. left.derefimpl;
  737. end;
  738. procedure tunarynode.derefnode;
  739. begin
  740. inherited derefnode;
  741. if assigned(left) then
  742. left.derefnode;
  743. end;
  744. function tunarynode.docompare(p : tnode) : boolean;
  745. begin
  746. docompare:=(inherited docompare(p) and
  747. ((left=nil) or left.isequal(tunarynode(p).left))
  748. );
  749. end;
  750. function tunarynode.getcopy : tnode;
  751. var
  752. p : tunarynode;
  753. begin
  754. p:=tunarynode(inherited getcopy);
  755. if assigned(left) then
  756. p.left:=left.getcopy
  757. else
  758. p.left:=nil;
  759. getcopy:=p;
  760. end;
  761. procedure tunarynode.insertintolist(l : tnodelist);
  762. begin
  763. end;
  764. procedure tunarynode.printnodedata(var t:text);
  765. begin
  766. inherited printnodedata(t);
  767. printnode(t,left);
  768. end;
  769. procedure tunarynode.left_max;
  770. begin
  771. registersint:=left.registersint;
  772. registersfpu:=left.registersfpu;
  773. {$ifdef SUPPORT_MMX}
  774. registersmmx:=left.registersmmx;
  775. {$endif SUPPORT_MMX}
  776. end;
  777. procedure tunarynode.concattolist(l : tlinkedlist);
  778. begin
  779. left.parent:=self;
  780. left.concattolist(l);
  781. inherited concattolist(l);
  782. end;
  783. function tunarynode.ischild(p : tnode) : boolean;
  784. begin
  785. ischild:=p=left;
  786. end;
  787. {****************************************************************************
  788. TBINARYNODE
  789. ****************************************************************************}
  790. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  791. begin
  792. inherited create(t,l);
  793. right:=r
  794. end;
  795. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  796. begin
  797. inherited ppuload(t,ppufile);
  798. right:=ppuloadnode(ppufile);
  799. end;
  800. destructor tbinarynode.destroy;
  801. begin
  802. right.free;
  803. inherited destroy;
  804. end;
  805. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  806. begin
  807. inherited ppuwrite(ppufile);
  808. ppuwritenode(ppufile,right);
  809. end;
  810. procedure tbinarynode.buildderefimpl;
  811. begin
  812. inherited buildderefimpl;
  813. if assigned(right) then
  814. right.buildderefimpl;
  815. end;
  816. procedure tbinarynode.derefimpl;
  817. begin
  818. inherited derefimpl;
  819. if assigned(right) then
  820. right.derefimpl;
  821. end;
  822. procedure tbinarynode.derefnode;
  823. begin
  824. inherited derefnode;
  825. if assigned(right) then
  826. right.derefnode;
  827. end;
  828. procedure tbinarynode.concattolist(l : tlinkedlist);
  829. begin
  830. { we could change that depending on the number of }
  831. { required registers }
  832. left.parent:=self;
  833. left.concattolist(l);
  834. left.parent:=self;
  835. left.concattolist(l);
  836. inherited concattolist(l);
  837. end;
  838. function tbinarynode.ischild(p : tnode) : boolean;
  839. begin
  840. ischild:=(p=right);
  841. end;
  842. function tbinarynode.docompare(p : tnode) : boolean;
  843. begin
  844. docompare:=(inherited docompare(p) and
  845. ((right=nil) or right.isequal(tbinarynode(p).right))
  846. );
  847. end;
  848. function tbinarynode.getcopy : tnode;
  849. var
  850. p : tbinarynode;
  851. begin
  852. p:=tbinarynode(inherited getcopy);
  853. if assigned(right) then
  854. p.right:=right.getcopy
  855. else
  856. p.right:=nil;
  857. getcopy:=p;
  858. end;
  859. procedure tbinarynode.insertintolist(l : tnodelist);
  860. begin
  861. end;
  862. procedure tbinarynode.swapleftright;
  863. var
  864. swapp : tnode;
  865. begin
  866. swapp:=right;
  867. right:=left;
  868. left:=swapp;
  869. if nf_swaped in flags then
  870. exclude(flags,nf_swaped)
  871. else
  872. include(flags,nf_swaped);
  873. end;
  874. procedure tbinarynode.left_right_max;
  875. begin
  876. if assigned(left) then
  877. begin
  878. if assigned(right) then
  879. begin
  880. registersint:=max(left.registersint,right.registersint);
  881. registersfpu:=max(left.registersfpu,right.registersfpu);
  882. {$ifdef SUPPORT_MMX}
  883. registersmmx:=max(left.registersmmx,right.registersmmx);
  884. {$endif SUPPORT_MMX}
  885. end
  886. else
  887. begin
  888. registersint:=left.registersint;
  889. registersfpu:=left.registersfpu;
  890. {$ifdef SUPPORT_MMX}
  891. registersmmx:=left.registersmmx;
  892. {$endif SUPPORT_MMX}
  893. end;
  894. end;
  895. end;
  896. procedure tbinarynode.printnodedata(var t:text);
  897. begin
  898. inherited printnodedata(t);
  899. printnode(t,right);
  900. end;
  901. procedure tbinarynode.printnodelist(var t:text);
  902. var
  903. hp : tbinarynode;
  904. begin
  905. hp:=self;
  906. while assigned(hp) do
  907. begin
  908. write(t,printnodeindention,'(');
  909. printnodeindent;
  910. hp.printnodeinfo(t);
  911. printnode(t,hp.left);
  912. writeln(t);
  913. printnodeunindent;
  914. writeln(t,printnodeindention,')');
  915. hp:=tbinarynode(hp.right);
  916. end;
  917. end;
  918. {****************************************************************************
  919. TBINOPYNODE
  920. ****************************************************************************}
  921. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  922. begin
  923. inherited create(t,l,r);
  924. end;
  925. function tbinopnode.docompare(p : tnode) : boolean;
  926. begin
  927. docompare:=(inherited docompare(p)) or
  928. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  929. ((nf_swapable in flags) and
  930. left.isequal(tbinopnode(p).right) and
  931. right.isequal(tbinopnode(p).left));
  932. end;
  933. end.
  934. {
  935. $Log$
  936. Revision 1.96 2005-02-14 17:13:06 peter
  937. * truncate log
  938. Revision 1.95 2005/01/04 16:39:46 peter
  939. * set nf_is_self node flag when self is loaded
  940. Revision 1.94 2005/01/03 17:55:57 florian
  941. + first batch of patches to support tdef.getcopy fully
  942. }