node.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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. { tassignmentnode }
  207. nf_concat_string,
  208. nf_use_strconcat,
  209. { tarrayconstructnode }
  210. nf_forcevaria,
  211. nf_novariaallowed,
  212. { ttypeconvnode }
  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. { dermines the number of necessary temp. locations to evaluate
  280. the node }
  281. {$ifdef state_tracking}
  282. { Does optimizations by keeping track of the variable states
  283. in a procedure }
  284. function track_state_pass(exec_known:boolean):boolean;virtual;
  285. {$endif}
  286. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  287. written to (normally the loadnode) as write access. }
  288. procedure mark_write;virtual;
  289. procedure det_temp;virtual;abstract;
  290. procedure pass_2;virtual;abstract;
  291. { comparing of nodes }
  292. function isequal(p : tnode) : boolean;
  293. { to implement comparisation, override this method }
  294. function docompare(p : tnode) : boolean;virtual;
  295. { gets a copy of the node }
  296. function getcopy : tnode;virtual;
  297. procedure insertintolist(l : tnodelist);virtual;
  298. { writes a node for debugging purpose, shouldn't be called }
  299. { direct, because there is no test for nil, use printnode }
  300. { to write a complete tree }
  301. procedure printnodeinfo(var t:text);virtual;
  302. procedure printnodedata(var t:text);virtual;
  303. procedure printnodetree(var t:text);virtual;
  304. procedure concattolist(l : tlinkedlist);virtual;
  305. function ischild(p : tnode) : boolean;virtual;
  306. end;
  307. tnodeclass = class of tnode;
  308. tnodeclassarray = array[tnodetype] of tnodeclass;
  309. { this node is the anchestor for all nodes with at least }
  310. { one child, you have to use it if you want to use }
  311. { true- and falselabel }
  312. punarynode = ^tunarynode;
  313. tunarynode = class(tnode)
  314. left : tnode;
  315. constructor create(t:tnodetype;l : tnode);
  316. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  317. destructor destroy;override;
  318. procedure ppuwrite(ppufile:tcompilerppufile);override;
  319. procedure buildderefimpl;override;
  320. procedure derefimpl;override;
  321. procedure derefnode;override;
  322. procedure concattolist(l : tlinkedlist);override;
  323. function ischild(p : tnode) : boolean;override;
  324. function docompare(p : tnode) : boolean;override;
  325. function getcopy : tnode;override;
  326. procedure insertintolist(l : tnodelist);override;
  327. procedure left_max;
  328. procedure printnodedata(var t:text);override;
  329. end;
  330. pbinarynode = ^tbinarynode;
  331. tbinarynode = class(tunarynode)
  332. right : tnode;
  333. constructor create(t:tnodetype;l,r : tnode);
  334. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  335. destructor destroy;override;
  336. procedure ppuwrite(ppufile:tcompilerppufile);override;
  337. procedure buildderefimpl;override;
  338. procedure derefimpl;override;
  339. procedure derefnode;override;
  340. procedure concattolist(l : tlinkedlist);override;
  341. function ischild(p : tnode) : boolean;override;
  342. function docompare(p : tnode) : boolean;override;
  343. procedure swapleftright;
  344. function getcopy : tnode;override;
  345. procedure insertintolist(l : tnodelist);override;
  346. procedure left_right_max;
  347. procedure printnodedata(var t:text);override;
  348. procedure printnodelist(var t:text);
  349. end;
  350. tbinopnode = class(tbinarynode)
  351. constructor create(t:tnodetype;l,r : tnode);virtual;
  352. function docompare(p : tnode) : boolean;override;
  353. end;
  354. var
  355. { array with all class types for tnodes }
  356. nodeclass : tnodeclassarray;
  357. function nodeppuidxget(i:longint):tnode;
  358. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  359. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  360. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  361. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  362. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  363. function ppuloadnoderef(ppufile:tcompilerppufile) : 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. begin
  411. if i<0 then
  412. internalerror(200311072);
  413. nodeppudata.seek(i*sizeof(pointer));
  414. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  415. internalerror(200311073);
  416. end;
  417. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  418. var
  419. b : byte;
  420. t : tnodetype;
  421. hppuidx : longint;
  422. begin
  423. { marker }
  424. b:=ppufile.getbyte;
  425. if b<>ppunodemarker then
  426. internalerror(200208151);
  427. { load nodetype }
  428. t:=tnodetype(ppufile.getbyte);
  429. if t>high(tnodetype) then
  430. internalerror(200208152);
  431. if t<>emptynode then
  432. begin
  433. if not assigned(nodeclass[t]) then
  434. internalerror(200208153);
  435. hppuidx:=ppufile.getlongint;
  436. //writeln('load: ',nodetype2str[t]);
  437. { generate node of the correct class }
  438. result:=nodeclass[t].ppuload(t,ppufile);
  439. result.ppuidx:=hppuidx;
  440. nodeppuidxadd(result);
  441. end
  442. else
  443. result:=nil;
  444. end;
  445. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  446. begin
  447. { marker, read by ppuloadnode }
  448. ppufile.putbyte(ppunodemarker);
  449. { type, read by ppuloadnode }
  450. if assigned(n) then
  451. begin
  452. if n.ppuidx=-1 then
  453. internalerror(200311071);
  454. n.ppuidx:=nodeppuidx;
  455. inc(nodeppuidx);
  456. ppufile.putbyte(byte(n.nodetype));
  457. ppufile.putlongint(n.ppuidx);
  458. //writeln('write: ',nodetype2str[n.nodetype]);
  459. n.ppuwrite(ppufile);
  460. end
  461. else
  462. ppufile.putbyte(byte(emptynode));
  463. end;
  464. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  465. begin
  466. { writing of node references isn't implemented yet (FK) }
  467. internalerror(200506181);
  468. end;
  469. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  470. begin
  471. { reading of node references isn't implemented yet (FK) }
  472. internalerror(200506182);
  473. end;
  474. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  475. begin
  476. if ppufile.readentry<>ibnodetree then
  477. Message(unit_f_ppu_read_error);
  478. nodeppuidxcreate;
  479. result:=ppuloadnode(ppufile);
  480. result.derefnode;
  481. nodeppuidxfree;
  482. end;
  483. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  484. begin
  485. nodeppuidx:=0;
  486. ppuwritenode(ppufile,n);
  487. ppufile.writeentry(ibnodetree);
  488. end;
  489. procedure printnodeindent;
  490. begin
  491. printnodeindention:=printnodeindention+printnodespacing;
  492. end;
  493. procedure printnodeunindent;
  494. begin
  495. delete(printnodeindention,1,length(printnodespacing));
  496. end;
  497. procedure printnode(var t:text;n:tnode);
  498. begin
  499. if assigned(n) then
  500. n.printnodetree(t)
  501. else
  502. writeln(t,printnodeindention,'nil');
  503. end;
  504. function is_constnode(p : tnode) : boolean;
  505. begin
  506. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  507. end;
  508. function is_constintnode(p : tnode) : boolean;
  509. begin
  510. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resulttype.def);
  511. end;
  512. function is_constcharnode(p : tnode) : boolean;
  513. begin
  514. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resulttype.def);
  515. end;
  516. function is_constwidecharnode(p : tnode) : boolean;
  517. begin
  518. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resulttype.def);
  519. end;
  520. function is_constrealnode(p : tnode) : boolean;
  521. begin
  522. is_constrealnode:=(p.nodetype=realconstn);
  523. end;
  524. function is_constboolnode(p : tnode) : boolean;
  525. begin
  526. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resulttype.def);
  527. end;
  528. function is_constenumnode(p : tnode) : boolean;
  529. begin
  530. is_constenumnode:=(p.nodetype=ordconstn) and (p.resulttype.def.deftype=enumdef);
  531. end;
  532. {****************************************************************************
  533. TNODE
  534. ****************************************************************************}
  535. constructor tnode.create(t:tnodetype);
  536. begin
  537. inherited create;
  538. nodetype:=t;
  539. blocktype:=block_type;
  540. { updated by firstpass }
  541. expectloc:=LOC_INVALID;
  542. { updated by secondpass }
  543. location.loc:=LOC_INVALID;
  544. { save local info }
  545. fileinfo:=aktfilepos;
  546. localswitches:=aktlocalswitches;
  547. resulttype.reset;
  548. registersint:=0;
  549. registersfpu:=0;
  550. {$ifdef SUPPORT_MMX}
  551. registersmmx:=0;
  552. {$endif SUPPORT_MMX}
  553. {$ifdef EXTDEBUG}
  554. maxfirstpasscount:=0;
  555. firstpasscount:=0;
  556. {$endif EXTDEBUG}
  557. flags:=[];
  558. ppuidx:=-1;
  559. end;
  560. constructor tnode.createforcopy;
  561. begin
  562. end;
  563. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  564. begin
  565. nodetype:=t;
  566. { tnode fields }
  567. blocktype:=tblock_type(ppufile.getbyte);
  568. ppufile.getposinfo(fileinfo);
  569. ppufile.getsmallset(localswitches);
  570. ppufile.gettype(resulttype);
  571. ppufile.getsmallset(flags);
  572. { updated by firstpass }
  573. expectloc:=LOC_INVALID;
  574. { updated by secondpass }
  575. location.loc:=LOC_INVALID;
  576. registersint:=0;
  577. registersfpu:=0;
  578. {$ifdef SUPPORT_MMX}
  579. registersmmx:=0;
  580. {$endif SUPPORT_MMX}
  581. {$ifdef EXTDEBUG}
  582. maxfirstpasscount:=0;
  583. firstpasscount:=0;
  584. {$endif EXTDEBUG}
  585. ppuidx:=-1;
  586. end;
  587. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  588. begin
  589. ppufile.putbyte(byte(block_type));
  590. ppufile.putposinfo(fileinfo);
  591. ppufile.putsmallset(localswitches);
  592. ppufile.puttype(resulttype);
  593. ppufile.putsmallset(flags);
  594. end;
  595. procedure tnode.buildderefimpl;
  596. begin
  597. resulttype.buildderef;
  598. end;
  599. procedure tnode.derefimpl;
  600. begin
  601. resulttype.resolve;
  602. end;
  603. procedure tnode.derefnode;
  604. begin
  605. end;
  606. procedure tnode.toggleflag(f : tnodeflag);
  607. begin
  608. if f in flags then
  609. exclude(flags,f)
  610. else
  611. include(flags,f);
  612. end;
  613. destructor tnode.destroy;
  614. begin
  615. {$ifdef EXTDEBUG}
  616. if firstpasscount>maxfirstpasscount then
  617. maxfirstpasscount:=firstpasscount;
  618. {$endif EXTDEBUG}
  619. end;
  620. procedure tnode.concattolist(l : tlinkedlist);
  621. begin
  622. end;
  623. function tnode.ischild(p : tnode) : boolean;
  624. begin
  625. ischild:=false;
  626. end;
  627. procedure tnode.mark_write;
  628. begin
  629. {$ifdef EXTDEBUG}
  630. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  631. {$endif EXTDEBUG}
  632. end;
  633. procedure tnode.printnodeinfo(var t:text);
  634. begin
  635. write(t,nodetype2str[nodetype]);
  636. if assigned(resulttype.def) then
  637. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  638. else
  639. write(t,', resulttype = <nil>');
  640. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  641. ', loc = ',tcgloc2str[location.loc],
  642. ', expectloc = ',tcgloc2str[expectloc],
  643. ', intregs = ',registersint,
  644. ', fpuregs = ',registersfpu);
  645. end;
  646. procedure tnode.printnodedata(var t:text);
  647. begin
  648. end;
  649. procedure tnode.printnodetree(var t:text);
  650. begin
  651. write(t,printnodeindention,'(');
  652. printnodeinfo(t);
  653. writeln(t);
  654. printnodeindent;
  655. printnodedata(t);
  656. printnodeunindent;
  657. writeln(t,printnodeindention,')');
  658. end;
  659. function tnode.isequal(p : tnode) : boolean;
  660. begin
  661. isequal:=
  662. (not assigned(self) and not assigned(p)) or
  663. (assigned(self) and assigned(p) and
  664. { optimized subclasses have the same nodetype as their }
  665. { superclass (for compatibility), so also check the classtype (JM) }
  666. (p.classtype=classtype) and
  667. (p.nodetype=nodetype) and
  668. (flags*flagsequal=p.flags*flagsequal) and
  669. docompare(p));
  670. end;
  671. {$ifdef state_tracking}
  672. function Tnode.track_state_pass(exec_known:boolean):boolean;
  673. begin
  674. track_state_pass:=false;
  675. end;
  676. {$endif state_tracking}
  677. function tnode.docompare(p : tnode) : boolean;
  678. begin
  679. docompare:=true;
  680. end;
  681. function tnode.getcopy : tnode;
  682. var
  683. p : tnode;
  684. begin
  685. { this is quite tricky because we need a node of the current }
  686. { node type and not one of tnode! }
  687. p:=tnodeclass(classtype).createforcopy;
  688. p.nodetype:=nodetype;
  689. p.expectloc:=expectloc;
  690. p.location:=location;
  691. p.parent:=parent;
  692. p.flags:=flags;
  693. p.registersint:=registersint;
  694. p.registersfpu:=registersfpu;
  695. {$ifdef SUPPORT_MMX}
  696. p.registersmmx:=registersmmx;
  697. p.registersmm:=registersmm;
  698. {$endif SUPPORT_MMX}
  699. p.resulttype:=resulttype;
  700. p.fileinfo:=fileinfo;
  701. p.localswitches:=localswitches;
  702. {$ifdef extdebug}
  703. p.firstpasscount:=firstpasscount;
  704. {$endif extdebug}
  705. { p.list:=list; }
  706. getcopy:=p;
  707. end;
  708. procedure tnode.insertintolist(l : tnodelist);
  709. begin
  710. end;
  711. {****************************************************************************
  712. TUNARYNODE
  713. ****************************************************************************}
  714. constructor tunarynode.create(t:tnodetype;l : tnode);
  715. begin
  716. inherited create(t);
  717. left:=l;
  718. end;
  719. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  720. begin
  721. inherited ppuload(t,ppufile);
  722. left:=ppuloadnode(ppufile);
  723. end;
  724. destructor tunarynode.destroy;
  725. begin
  726. left.free;
  727. inherited destroy;
  728. end;
  729. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  730. begin
  731. inherited ppuwrite(ppufile);
  732. ppuwritenode(ppufile,left);
  733. end;
  734. procedure tunarynode.buildderefimpl;
  735. begin
  736. inherited buildderefimpl;
  737. if assigned(left) then
  738. left.buildderefimpl;
  739. end;
  740. procedure tunarynode.derefimpl;
  741. begin
  742. inherited derefimpl;
  743. if assigned(left) then
  744. left.derefimpl;
  745. end;
  746. procedure tunarynode.derefnode;
  747. begin
  748. inherited derefnode;
  749. if assigned(left) then
  750. left.derefnode;
  751. end;
  752. function tunarynode.docompare(p : tnode) : boolean;
  753. begin
  754. docompare:=(inherited docompare(p) and
  755. ((left=nil) or left.isequal(tunarynode(p).left))
  756. );
  757. end;
  758. function tunarynode.getcopy : tnode;
  759. var
  760. p : tunarynode;
  761. begin
  762. p:=tunarynode(inherited getcopy);
  763. if assigned(left) then
  764. p.left:=left.getcopy
  765. else
  766. p.left:=nil;
  767. getcopy:=p;
  768. end;
  769. procedure tunarynode.insertintolist(l : tnodelist);
  770. begin
  771. end;
  772. procedure tunarynode.printnodedata(var t:text);
  773. begin
  774. inherited printnodedata(t);
  775. printnode(t,left);
  776. end;
  777. procedure tunarynode.left_max;
  778. begin
  779. registersint:=left.registersint;
  780. registersfpu:=left.registersfpu;
  781. {$ifdef SUPPORT_MMX}
  782. registersmmx:=left.registersmmx;
  783. {$endif SUPPORT_MMX}
  784. end;
  785. procedure tunarynode.concattolist(l : tlinkedlist);
  786. begin
  787. left.parent:=self;
  788. left.concattolist(l);
  789. inherited concattolist(l);
  790. end;
  791. function tunarynode.ischild(p : tnode) : boolean;
  792. begin
  793. ischild:=p=left;
  794. end;
  795. {****************************************************************************
  796. TBINARYNODE
  797. ****************************************************************************}
  798. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  799. begin
  800. inherited create(t,l);
  801. right:=r
  802. end;
  803. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  804. begin
  805. inherited ppuload(t,ppufile);
  806. right:=ppuloadnode(ppufile);
  807. end;
  808. destructor tbinarynode.destroy;
  809. begin
  810. right.free;
  811. inherited destroy;
  812. end;
  813. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  814. begin
  815. inherited ppuwrite(ppufile);
  816. ppuwritenode(ppufile,right);
  817. end;
  818. procedure tbinarynode.buildderefimpl;
  819. begin
  820. inherited buildderefimpl;
  821. if assigned(right) then
  822. right.buildderefimpl;
  823. end;
  824. procedure tbinarynode.derefimpl;
  825. begin
  826. inherited derefimpl;
  827. if assigned(right) then
  828. right.derefimpl;
  829. end;
  830. procedure tbinarynode.derefnode;
  831. begin
  832. inherited derefnode;
  833. if assigned(right) then
  834. right.derefnode;
  835. end;
  836. procedure tbinarynode.concattolist(l : tlinkedlist);
  837. begin
  838. { we could change that depending on the number of }
  839. { required registers }
  840. left.parent:=self;
  841. left.concattolist(l);
  842. left.parent:=self;
  843. left.concattolist(l);
  844. inherited concattolist(l);
  845. end;
  846. function tbinarynode.ischild(p : tnode) : boolean;
  847. begin
  848. ischild:=(p=right);
  849. end;
  850. function tbinarynode.docompare(p : tnode) : boolean;
  851. begin
  852. docompare:=(inherited docompare(p) and
  853. ((right=nil) or right.isequal(tbinarynode(p).right))
  854. );
  855. end;
  856. function tbinarynode.getcopy : tnode;
  857. var
  858. p : tbinarynode;
  859. begin
  860. p:=tbinarynode(inherited getcopy);
  861. if assigned(right) then
  862. p.right:=right.getcopy
  863. else
  864. p.right:=nil;
  865. getcopy:=p;
  866. end;
  867. procedure tbinarynode.insertintolist(l : tnodelist);
  868. begin
  869. end;
  870. procedure tbinarynode.swapleftright;
  871. var
  872. swapp : tnode;
  873. begin
  874. swapp:=right;
  875. right:=left;
  876. left:=swapp;
  877. if nf_swaped in flags then
  878. exclude(flags,nf_swaped)
  879. else
  880. include(flags,nf_swaped);
  881. end;
  882. procedure tbinarynode.left_right_max;
  883. begin
  884. if assigned(left) then
  885. begin
  886. if assigned(right) then
  887. begin
  888. registersint:=max(left.registersint,right.registersint);
  889. registersfpu:=max(left.registersfpu,right.registersfpu);
  890. {$ifdef SUPPORT_MMX}
  891. registersmmx:=max(left.registersmmx,right.registersmmx);
  892. {$endif SUPPORT_MMX}
  893. end
  894. else
  895. begin
  896. registersint:=left.registersint;
  897. registersfpu:=left.registersfpu;
  898. {$ifdef SUPPORT_MMX}
  899. registersmmx:=left.registersmmx;
  900. {$endif SUPPORT_MMX}
  901. end;
  902. end;
  903. end;
  904. procedure tbinarynode.printnodedata(var t:text);
  905. begin
  906. inherited printnodedata(t);
  907. printnode(t,right);
  908. end;
  909. procedure tbinarynode.printnodelist(var t:text);
  910. var
  911. hp : tbinarynode;
  912. begin
  913. hp:=self;
  914. while assigned(hp) do
  915. begin
  916. write(t,printnodeindention,'(');
  917. printnodeindent;
  918. hp.printnodeinfo(t);
  919. writeln(t);
  920. printnode(t,hp.left);
  921. writeln(t);
  922. printnodeunindent;
  923. writeln(t,printnodeindention,')');
  924. hp:=tbinarynode(hp.right);
  925. end;
  926. end;
  927. {****************************************************************************
  928. TBINOPYNODE
  929. ****************************************************************************}
  930. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  931. begin
  932. inherited create(t,l,r);
  933. end;
  934. function tbinopnode.docompare(p : tnode) : boolean;
  935. begin
  936. docompare:=(inherited docompare(p)) or
  937. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  938. ((nf_swapable in flags) and
  939. left.isequal(tbinopnode(p).right) and
  940. right.isequal(tbinopnode(p).left));
  941. end;
  942. end.