node.pas 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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. function is_untyped_addrnode(p: tnode): boolean;
  385. implementation
  386. uses
  387. cutils,verbose,ppu,
  388. symconst,
  389. defutil;
  390. const
  391. ppunodemarker = 255;
  392. {****************************************************************************
  393. Helpers
  394. ****************************************************************************}
  395. var
  396. nodeppudata : tdynamicarray;
  397. nodeppuidx : longint;
  398. procedure nodeppuidxcreate;
  399. begin
  400. nodeppudata:=tdynamicarray.create(1024);
  401. nodeppuidx:=0;
  402. end;
  403. procedure nodeppuidxfree;
  404. begin
  405. nodeppudata.free;
  406. nodeppudata:=nil;
  407. end;
  408. procedure nodeppuidxadd(n:tnode);
  409. begin
  410. if n.ppuidx<0 then
  411. internalerror(200311072);
  412. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  413. nodeppudata.write(n,sizeof(pointer));
  414. end;
  415. function nodeppuidxget(i:longint):tnode;
  416. begin
  417. if i<0 then
  418. internalerror(200311072);
  419. nodeppudata.seek(i*sizeof(pointer));
  420. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  421. internalerror(200311073);
  422. end;
  423. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  424. var
  425. b : byte;
  426. t : tnodetype;
  427. hppuidx : longint;
  428. begin
  429. { marker }
  430. b:=ppufile.getbyte;
  431. if b<>ppunodemarker then
  432. internalerror(200208151);
  433. { load nodetype }
  434. t:=tnodetype(ppufile.getbyte);
  435. if t>high(tnodetype) then
  436. internalerror(200208152);
  437. if t<>emptynode then
  438. begin
  439. if not assigned(nodeclass[t]) then
  440. internalerror(200208153);
  441. hppuidx:=ppufile.getlongint;
  442. //writeln('load: ',nodetype2str[t]);
  443. { generate node of the correct class }
  444. result:=nodeclass[t].ppuload(t,ppufile);
  445. result.ppuidx:=hppuidx;
  446. nodeppuidxadd(result);
  447. end
  448. else
  449. result:=nil;
  450. end;
  451. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  452. begin
  453. { marker, read by ppuloadnode }
  454. ppufile.putbyte(ppunodemarker);
  455. { type, read by ppuloadnode }
  456. if assigned(n) then
  457. begin
  458. if n.ppuidx=-1 then
  459. internalerror(200311071);
  460. n.ppuidx:=nodeppuidx;
  461. inc(nodeppuidx);
  462. ppufile.putbyte(byte(n.nodetype));
  463. ppufile.putlongint(n.ppuidx);
  464. //writeln('write: ',nodetype2str[n.nodetype]);
  465. n.ppuwrite(ppufile);
  466. end
  467. else
  468. ppufile.putbyte(byte(emptynode));
  469. end;
  470. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  471. begin
  472. { writing of node references isn't implemented yet (FK) }
  473. internalerror(200506181);
  474. end;
  475. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  476. begin
  477. { reading of node references isn't implemented yet (FK) }
  478. internalerror(200506182);
  479. { avoid warning }
  480. result := nil;
  481. end;
  482. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  483. begin
  484. if ppufile.readentry<>ibnodetree then
  485. Message(unit_f_ppu_read_error);
  486. nodeppuidxcreate;
  487. result:=ppuloadnode(ppufile);
  488. result.derefnode;
  489. nodeppuidxfree;
  490. end;
  491. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  492. begin
  493. nodeppuidx:=0;
  494. ppuwritenode(ppufile,n);
  495. ppufile.writeentry(ibnodetree);
  496. end;
  497. procedure printnodeindent;
  498. begin
  499. printnodeindention:=printnodeindention+printnodespacing;
  500. end;
  501. procedure printnodeunindent;
  502. begin
  503. delete(printnodeindention,1,length(printnodespacing));
  504. end;
  505. procedure printnode(var t:text;n:tnode);
  506. begin
  507. if assigned(n) then
  508. n.printnodetree(t)
  509. else
  510. writeln(t,printnodeindention,'nil');
  511. end;
  512. function is_constnode(p : tnode) : boolean;
  513. begin
  514. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  515. end;
  516. function is_constintnode(p : tnode) : boolean;
  517. begin
  518. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resulttype.def);
  519. end;
  520. function is_constcharnode(p : tnode) : boolean;
  521. begin
  522. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resulttype.def);
  523. end;
  524. function is_constwidecharnode(p : tnode) : boolean;
  525. begin
  526. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resulttype.def);
  527. end;
  528. function is_constrealnode(p : tnode) : boolean;
  529. begin
  530. is_constrealnode:=(p.nodetype=realconstn);
  531. end;
  532. function is_constboolnode(p : tnode) : boolean;
  533. begin
  534. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resulttype.def);
  535. end;
  536. function is_constenumnode(p : tnode) : boolean;
  537. begin
  538. is_constenumnode:=(p.nodetype=ordconstn) and (p.resulttype.def.deftype=enumdef);
  539. end;
  540. function is_untyped_addrnode(p: tnode): boolean;
  541. begin
  542. is_untyped_addrnode:=(p.nodetype=addrn) and not (nf_typedaddr in p.flags);
  543. end;
  544. {****************************************************************************
  545. TNODE
  546. ****************************************************************************}
  547. constructor tnode.create(t:tnodetype);
  548. begin
  549. inherited create;
  550. nodetype:=t;
  551. blocktype:=block_type;
  552. { updated by firstpass }
  553. expectloc:=LOC_INVALID;
  554. { updated by secondpass }
  555. location.loc:=LOC_INVALID;
  556. { save local info }
  557. fileinfo:=aktfilepos;
  558. localswitches:=aktlocalswitches;
  559. resulttype.reset;
  560. registersint:=0;
  561. registersfpu:=0;
  562. {$ifdef SUPPORT_MMX}
  563. registersmmx:=0;
  564. {$endif SUPPORT_MMX}
  565. {$ifdef EXTDEBUG}
  566. maxfirstpasscount:=0;
  567. firstpasscount:=0;
  568. {$endif EXTDEBUG}
  569. flags:=[];
  570. ppuidx:=-1;
  571. end;
  572. constructor tnode.createforcopy;
  573. begin
  574. end;
  575. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  576. begin
  577. nodetype:=t;
  578. { tnode fields }
  579. blocktype:=tblock_type(ppufile.getbyte);
  580. ppufile.getposinfo(fileinfo);
  581. ppufile.getsmallset(localswitches);
  582. ppufile.gettype(resulttype);
  583. ppufile.getsmallset(flags);
  584. { updated by firstpass }
  585. expectloc:=LOC_INVALID;
  586. { updated by secondpass }
  587. location.loc:=LOC_INVALID;
  588. registersint:=0;
  589. registersfpu:=0;
  590. {$ifdef SUPPORT_MMX}
  591. registersmmx:=0;
  592. {$endif SUPPORT_MMX}
  593. {$ifdef EXTDEBUG}
  594. maxfirstpasscount:=0;
  595. firstpasscount:=0;
  596. {$endif EXTDEBUG}
  597. ppuidx:=-1;
  598. end;
  599. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  600. begin
  601. ppufile.putbyte(byte(block_type));
  602. ppufile.putposinfo(fileinfo);
  603. ppufile.putsmallset(localswitches);
  604. ppufile.puttype(resulttype);
  605. ppufile.putsmallset(flags);
  606. end;
  607. procedure tnode.buildderefimpl;
  608. begin
  609. resulttype.buildderef;
  610. end;
  611. procedure tnode.derefimpl;
  612. begin
  613. resulttype.resolve;
  614. end;
  615. procedure tnode.derefnode;
  616. begin
  617. end;
  618. procedure tnode.toggleflag(f : tnodeflag);
  619. begin
  620. if f in flags then
  621. exclude(flags,f)
  622. else
  623. include(flags,f);
  624. end;
  625. function tnode.simplify : tnode;
  626. begin
  627. result:=nil;
  628. end;
  629. destructor tnode.destroy;
  630. begin
  631. {$ifdef EXTDEBUG}
  632. if firstpasscount>maxfirstpasscount then
  633. maxfirstpasscount:=firstpasscount;
  634. {$endif EXTDEBUG}
  635. end;
  636. procedure tnode.concattolist(l : tlinkedlist);
  637. begin
  638. end;
  639. function tnode.ischild(p : tnode) : boolean;
  640. begin
  641. ischild:=false;
  642. end;
  643. procedure tnode.mark_write;
  644. begin
  645. {$ifdef EXTDEBUG}
  646. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  647. {$endif EXTDEBUG}
  648. end;
  649. procedure tnode.printnodeinfo(var t:text);
  650. begin
  651. write(t,nodetype2str[nodetype]);
  652. if assigned(resulttype.def) then
  653. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  654. else
  655. write(t,', resulttype = <nil>');
  656. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  657. ', loc = ',tcgloc2str[location.loc],
  658. ', expectloc = ',tcgloc2str[expectloc],
  659. ', intregs = ',registersint,
  660. ', fpuregs = ',registersfpu);
  661. end;
  662. procedure tnode.printnodedata(var t:text);
  663. begin
  664. end;
  665. procedure tnode.printnodetree(var t:text);
  666. begin
  667. write(t,printnodeindention,'(');
  668. printnodeinfo(t);
  669. writeln(t);
  670. printnodeindent;
  671. printnodedata(t);
  672. printnodeunindent;
  673. writeln(t,printnodeindention,')');
  674. end;
  675. function tnode.isequal(p : tnode) : boolean;
  676. begin
  677. isequal:=
  678. (not assigned(self) and not assigned(p)) or
  679. (assigned(self) and assigned(p) and
  680. { optimized subclasses have the same nodetype as their }
  681. { superclass (for compatibility), so also check the classtype (JM) }
  682. (p.classtype=classtype) and
  683. (p.nodetype=nodetype) and
  684. (flags*flagsequal=p.flags*flagsequal) and
  685. docompare(p));
  686. end;
  687. {$ifdef state_tracking}
  688. function Tnode.track_state_pass(exec_known:boolean):boolean;
  689. begin
  690. track_state_pass:=false;
  691. end;
  692. {$endif state_tracking}
  693. function tnode.docompare(p : tnode) : boolean;
  694. begin
  695. docompare:=true;
  696. end;
  697. function tnode.getcopy : tnode;
  698. begin
  699. result:=_getcopy;
  700. end;
  701. function tnode._getcopy : tnode;
  702. var
  703. p : tnode;
  704. begin
  705. { this is quite tricky because we need a node of the current }
  706. { node type and not one of tnode! }
  707. p:=tnodeclass(classtype).createforcopy;
  708. p.nodetype:=nodetype;
  709. p.expectloc:=expectloc;
  710. p.location:=location;
  711. p.parent:=parent;
  712. p.flags:=flags;
  713. p.registersint:=registersint;
  714. p.registersfpu:=registersfpu;
  715. {$ifdef SUPPORT_MMX}
  716. p.registersmmx:=registersmmx;
  717. p.registersmm:=registersmm;
  718. {$endif SUPPORT_MMX}
  719. p.resulttype:=resulttype;
  720. p.fileinfo:=fileinfo;
  721. p.localswitches:=localswitches;
  722. {$ifdef extdebug}
  723. p.firstpasscount:=firstpasscount;
  724. {$endif extdebug}
  725. { p.list:=list; }
  726. result:=p;
  727. end;
  728. procedure tnode.insertintolist(l : tnodelist);
  729. begin
  730. end;
  731. {****************************************************************************
  732. TUNARYNODE
  733. ****************************************************************************}
  734. constructor tunarynode.create(t:tnodetype;l : tnode);
  735. begin
  736. inherited create(t);
  737. left:=l;
  738. end;
  739. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  740. begin
  741. inherited ppuload(t,ppufile);
  742. left:=ppuloadnode(ppufile);
  743. end;
  744. destructor tunarynode.destroy;
  745. begin
  746. left.free;
  747. inherited destroy;
  748. end;
  749. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  750. begin
  751. inherited ppuwrite(ppufile);
  752. ppuwritenode(ppufile,left);
  753. end;
  754. procedure tunarynode.buildderefimpl;
  755. begin
  756. inherited buildderefimpl;
  757. if assigned(left) then
  758. left.buildderefimpl;
  759. end;
  760. procedure tunarynode.derefimpl;
  761. begin
  762. inherited derefimpl;
  763. if assigned(left) then
  764. left.derefimpl;
  765. end;
  766. procedure tunarynode.derefnode;
  767. begin
  768. inherited derefnode;
  769. if assigned(left) then
  770. left.derefnode;
  771. end;
  772. function tunarynode.docompare(p : tnode) : boolean;
  773. begin
  774. docompare:=(inherited docompare(p) and
  775. ((left=nil) or left.isequal(tunarynode(p).left))
  776. );
  777. end;
  778. function tunarynode._getcopy : tnode;
  779. var
  780. p : tunarynode;
  781. begin
  782. p:=tunarynode(inherited _getcopy);
  783. if assigned(left) then
  784. p.left:=left._getcopy
  785. else
  786. p.left:=nil;
  787. result:=p;
  788. end;
  789. procedure tunarynode.insertintolist(l : tnodelist);
  790. begin
  791. end;
  792. procedure tunarynode.printnodedata(var t:text);
  793. begin
  794. inherited printnodedata(t);
  795. printnode(t,left);
  796. end;
  797. procedure tunarynode.left_max;
  798. begin
  799. registersint:=left.registersint;
  800. registersfpu:=left.registersfpu;
  801. {$ifdef SUPPORT_MMX}
  802. registersmmx:=left.registersmmx;
  803. {$endif SUPPORT_MMX}
  804. end;
  805. procedure tunarynode.concattolist(l : tlinkedlist);
  806. begin
  807. left.parent:=self;
  808. left.concattolist(l);
  809. inherited concattolist(l);
  810. end;
  811. function tunarynode.ischild(p : tnode) : boolean;
  812. begin
  813. ischild:=p=left;
  814. end;
  815. {****************************************************************************
  816. TBINARYNODE
  817. ****************************************************************************}
  818. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  819. begin
  820. inherited create(t,l);
  821. right:=r
  822. end;
  823. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  824. begin
  825. inherited ppuload(t,ppufile);
  826. right:=ppuloadnode(ppufile);
  827. end;
  828. destructor tbinarynode.destroy;
  829. begin
  830. right.free;
  831. inherited destroy;
  832. end;
  833. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  834. begin
  835. inherited ppuwrite(ppufile);
  836. ppuwritenode(ppufile,right);
  837. end;
  838. procedure tbinarynode.buildderefimpl;
  839. begin
  840. inherited buildderefimpl;
  841. if assigned(right) then
  842. right.buildderefimpl;
  843. end;
  844. procedure tbinarynode.derefimpl;
  845. begin
  846. inherited derefimpl;
  847. if assigned(right) then
  848. right.derefimpl;
  849. end;
  850. procedure tbinarynode.derefnode;
  851. begin
  852. inherited derefnode;
  853. if assigned(right) then
  854. right.derefnode;
  855. end;
  856. procedure tbinarynode.concattolist(l : tlinkedlist);
  857. begin
  858. { we could change that depending on the number of }
  859. { required registers }
  860. left.parent:=self;
  861. left.concattolist(l);
  862. left.parent:=self;
  863. left.concattolist(l);
  864. inherited concattolist(l);
  865. end;
  866. function tbinarynode.ischild(p : tnode) : boolean;
  867. begin
  868. ischild:=(p=right);
  869. end;
  870. function tbinarynode.docompare(p : tnode) : boolean;
  871. begin
  872. docompare:=(inherited docompare(p) and
  873. ((right=nil) or right.isequal(tbinarynode(p).right))
  874. );
  875. end;
  876. function tbinarynode._getcopy : tnode;
  877. var
  878. p : tbinarynode;
  879. begin
  880. p:=tbinarynode(inherited _getcopy);
  881. if assigned(right) then
  882. p.right:=right._getcopy
  883. else
  884. p.right:=nil;
  885. result:=p;
  886. end;
  887. procedure tbinarynode.insertintolist(l : tnodelist);
  888. begin
  889. end;
  890. procedure tbinarynode.swapleftright;
  891. var
  892. swapp : tnode;
  893. begin
  894. swapp:=right;
  895. right:=left;
  896. left:=swapp;
  897. if nf_swaped in flags then
  898. exclude(flags,nf_swaped)
  899. else
  900. include(flags,nf_swaped);
  901. end;
  902. procedure tbinarynode.left_right_max;
  903. begin
  904. if assigned(left) then
  905. begin
  906. if assigned(right) then
  907. begin
  908. registersint:=max(left.registersint,right.registersint);
  909. registersfpu:=max(left.registersfpu,right.registersfpu);
  910. {$ifdef SUPPORT_MMX}
  911. registersmmx:=max(left.registersmmx,right.registersmmx);
  912. {$endif SUPPORT_MMX}
  913. end
  914. else
  915. begin
  916. registersint:=left.registersint;
  917. registersfpu:=left.registersfpu;
  918. {$ifdef SUPPORT_MMX}
  919. registersmmx:=left.registersmmx;
  920. {$endif SUPPORT_MMX}
  921. end;
  922. end;
  923. end;
  924. procedure tbinarynode.printnodedata(var t:text);
  925. begin
  926. inherited printnodedata(t);
  927. printnode(t,right);
  928. end;
  929. procedure tbinarynode.printnodelist(var t:text);
  930. var
  931. hp : tbinarynode;
  932. begin
  933. hp:=self;
  934. while assigned(hp) do
  935. begin
  936. write(t,printnodeindention,'(');
  937. printnodeindent;
  938. hp.printnodeinfo(t);
  939. writeln(t);
  940. printnode(t,hp.left);
  941. writeln(t);
  942. printnodeunindent;
  943. writeln(t,printnodeindention,')');
  944. hp:=tbinarynode(hp.right);
  945. end;
  946. end;
  947. {****************************************************************************
  948. TBINOPYNODE
  949. ****************************************************************************}
  950. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  951. begin
  952. inherited create(t,l,r);
  953. end;
  954. function tbinopnode.docompare(p : tnode) : boolean;
  955. begin
  956. docompare:=(inherited docompare(p)) or
  957. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  958. ((nf_swapable in flags) and
  959. left.isequal(tbinopnode(p).right) and
  960. right.isequal(tbinopnode(p).left));
  961. end;
  962. end.