node.pas 33 KB

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