node.pas 33 KB

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