node.pas 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Basic node handling
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit node;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. globtype,globals,
  24. cpubase,cginfo,
  25. aasmbase,
  26. symtype,symppu;
  27. type
  28. pconstset = ^tconstset;
  29. {$ifdef oldset}
  30. tconstset = array[0..31] of byte;
  31. pconst32bitset = ^tconst32bitset;
  32. tconst32bitset = array[0..7] of longint;
  33. {$else}
  34. tconstset = set of 0..255;
  35. {$endif}
  36. tnodetype = (
  37. emptynode, {No node (returns nil when loading from ppu)}
  38. addn, {Represents the + operator}
  39. muln, {Represents the * operator}
  40. subn, {Represents the - operator}
  41. divn, {Represents the div operator}
  42. symdifn, {Represents the >< operator}
  43. modn, {Represents the mod operator}
  44. assignn, {Represents an assignment}
  45. loadn, {Represents the use of a variabele}
  46. rangen, {Represents a range (i.e. 0..9)}
  47. ltn, {Represents the < operator}
  48. lten, {Represents the <= operator}
  49. gtn, {Represents the > operator}
  50. gten, {Represents the >= operator}
  51. equaln, {Represents the = operator}
  52. unequaln, {Represents the <> operator}
  53. inn, {Represents the in operator}
  54. orn, {Represents the or operator}
  55. xorn, {Represents the xor operator}
  56. shrn, {Represents the shr operator}
  57. shln, {Represents the shl operator}
  58. slashn, {Represents the / operator}
  59. andn, {Represents the and operator}
  60. subscriptn, {Field in a record/object}
  61. derefn, {Dereferences a pointer}
  62. addrn, {Represents the @ operator}
  63. doubleaddrn, {Represents the @@ operator}
  64. ordconstn, {Represents an ordinal value}
  65. typeconvn, {Represents type-conversion/typecast}
  66. calln, {Represents a call node}
  67. callparan, {Represents a parameter}
  68. realconstn, {Represents a real value}
  69. unaryminusn, {Represents a sign change (i.e. -2)}
  70. asmn, {Represents an assembler node }
  71. vecn, {Represents array indexing}
  72. pointerconstn, {Represents a pointer constant}
  73. stringconstn, {Represents a string constant}
  74. notn, {Represents the not operator}
  75. inlinen, {Internal procedures (i.e. writeln)}
  76. niln, {Represents the nil pointer}
  77. errorn, {This part of the tree could not be
  78. parsed because of a compiler error}
  79. typen, {A type name. Used for i.e. typeof(obj)}
  80. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  81. setconstn, {A set constant (i.e. [1,2])}
  82. blockn, {A block of statements}
  83. statementn, {One statement in a block of nodes}
  84. ifn, {An if statement}
  85. breakn, {A break statement}
  86. continuen, {A continue statement}
  87. whilerepeatn, {A while or repeat statement}
  88. forn, {A for loop}
  89. exitn, {An exit statement}
  90. withn, {A with statement}
  91. casen, {A case statement}
  92. labeln, {A label}
  93. goton, {A goto statement}
  94. tryexceptn, {A try except block}
  95. raisen, {A raise statement}
  96. tryfinallyn, {A try finally statement}
  97. onn, {For an on statement in exception code}
  98. isn, {Represents the is operator}
  99. asn, {Represents the as typecast}
  100. caretn, {Represents the ^ operator}
  101. failn, {Represents the fail statement}
  102. starstarn, {Represents the ** operator exponentiation }
  103. procinlinen, {Procedures that can be inlined }
  104. arrayconstructorn, {Construction node for [...] parsing}
  105. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  106. tempcreaten, { for temps in the result/firstpass }
  107. temprefn, { references to temps }
  108. tempdeleten, { for temps in the result/firstpass }
  109. addoptn, { added for optimizations where we cannot suppress }
  110. nothingn, {NOP, Do nothing}
  111. loadvmtaddrn, {Load the address of the VMT of a class/object}
  112. guidconstn, {A GUID COM Interface constant }
  113. rttin {Rtti information so they can be accessed in result/firstpass}
  114. );
  115. const
  116. nodetype2str : array[tnodetype] of string[20] = (
  117. '<emptynode>',
  118. 'addn',
  119. 'muln',
  120. 'subn',
  121. 'divn',
  122. 'symdifn',
  123. 'modn',
  124. 'assignn',
  125. 'loadn',
  126. 'rangen',
  127. 'ltn',
  128. 'lten',
  129. 'gtn',
  130. 'gten',
  131. 'equaln',
  132. 'unequaln',
  133. 'inn',
  134. 'orn',
  135. 'xorn',
  136. 'shrn',
  137. 'shln',
  138. 'slashn',
  139. 'andn',
  140. 'subscriptn',
  141. 'derefn',
  142. 'addrn',
  143. 'doubleaddrn',
  144. 'ordconstn',
  145. 'typeconvn',
  146. 'calln',
  147. 'callparan',
  148. 'realconstn',
  149. 'unaryminusn',
  150. 'asmn',
  151. 'vecn',
  152. 'pointerconstn',
  153. 'stringconstn',
  154. 'notn',
  155. 'inlinen',
  156. 'niln',
  157. 'errorn',
  158. 'typen',
  159. 'setelementn',
  160. 'setconstn',
  161. 'blockn',
  162. 'statementn',
  163. 'ifn',
  164. 'breakn',
  165. 'continuen',
  166. 'whilerepeatn',
  167. 'forn',
  168. 'exitn',
  169. 'withn',
  170. 'casen',
  171. 'labeln',
  172. 'goton',
  173. 'tryexceptn',
  174. 'raisen',
  175. 'tryfinallyn',
  176. 'onn',
  177. 'isn',
  178. 'asn',
  179. 'caretn',
  180. 'failn',
  181. 'starstarn',
  182. 'procinlinen',
  183. 'arrayconstructn',
  184. 'arrayconstructrangen',
  185. 'tempcreaten',
  186. 'temprefn',
  187. 'tempdeleten',
  188. 'addoptn',
  189. 'nothingn',
  190. 'loadvmtaddrn',
  191. 'guidconstn',
  192. 'rttin');
  193. type
  194. { all boolean field of ttree are now collected in flags }
  195. tnodeflag = (
  196. nf_swapable, { tbinop operands can be swaped }
  197. nf_swaped, { tbinop operands are swaped }
  198. nf_error,
  199. { general }
  200. nf_write, { Node is written to }
  201. nf_first_use, { First node that uses a variable after declared }
  202. nf_varstateset,
  203. nf_isproperty,
  204. { flags used by tcallnode }
  205. nf_return_value_used,
  206. nf_inherited,
  207. nf_anon_inherited,
  208. nf_new_call,
  209. nf_dispose_call,
  210. nf_member_call, { called with implicit methodpointer tree }
  211. { flags used by tcallparanode }
  212. nf_varargs_para, { belongs this para to varargs }
  213. { taddrnode }
  214. nf_procvarload,
  215. { tvecnode }
  216. nf_memindex,
  217. nf_memseg,
  218. nf_callunique,
  219. { twithnode }
  220. nf_islocal,
  221. { tloadnode }
  222. nf_absolute,
  223. { taddnode }
  224. nf_is_currency,
  225. { tassignmentnode }
  226. nf_concat_string,
  227. nf_use_strconcat,
  228. { tarrayconstructnode }
  229. nf_cargs,
  230. nf_cargswap,
  231. nf_forcevaria,
  232. nf_novariaallowed,
  233. { ttypeconvnode }
  234. nf_explicit,
  235. { tinlinenode }
  236. nf_inlineconst,
  237. { tblocknode }
  238. nf_releasetemps
  239. );
  240. tnodeflags = set of tnodeflag;
  241. const
  242. { contains the flags which must be equal for the equality }
  243. { of nodes }
  244. flagsequal : tnodeflags = [nf_error];
  245. type
  246. tnodelist = class
  247. end;
  248. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  249. tnode = class
  250. public
  251. { type of this node }
  252. nodetype : tnodetype;
  253. { type of the current code block, general/const/type }
  254. blocktype : tblock_type;
  255. { expected location of the result of this node (pass1) }
  256. expectloc : tcgloc;
  257. { the location of the result of this node (pass2) }
  258. location : tlocation;
  259. { the parent node of this is node }
  260. { this field is set by concattolist }
  261. parent : tnode;
  262. { there are some properties about the node stored }
  263. flags : tnodeflags;
  264. { the number of registers needed to evalute the node }
  265. registers32,registersfpu : longint; { must be longint !!!! }
  266. {$ifdef SUPPORT_MMX}
  267. registersmmx,registerskni : longint;
  268. {$endif SUPPORT_MMX}
  269. resulttype : ttype;
  270. fileinfo : tfileposinfo;
  271. localswitches : tlocalswitches;
  272. {$ifdef extdebug}
  273. maxfirstpasscount,
  274. firstpasscount : longint;
  275. {$endif extdebug}
  276. constructor create(t:tnodetype);
  277. { this constructor is only for creating copies of class }
  278. { the fields are copied by getcopy }
  279. constructor createforcopy;
  280. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  281. destructor destroy;override;
  282. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  283. procedure derefimpl;virtual;
  284. { toggles the flag }
  285. procedure toggleflag(f : tnodeflag);
  286. { the 1.1 code generator may override pass_1 }
  287. { and it need not to implement det_* then }
  288. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  289. { 2.0: runs det_resulttype and det_temp }
  290. function pass_1 : tnode;virtual;abstract;
  291. { dermines the resulttype of the node }
  292. function det_resulttype : tnode;virtual;abstract;
  293. { dermines the number of necessary temp. locations to evaluate
  294. the node }
  295. {$ifdef state_tracking}
  296. { Does optimizations by keeping track of the variable states
  297. in a procedure }
  298. function track_state_pass(exec_known:boolean):boolean;virtual;
  299. {$endif}
  300. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  301. written to (normally the loadnode) as write access. }
  302. procedure mark_write;virtual;
  303. procedure det_temp;virtual;abstract;
  304. procedure pass_2;virtual;abstract;
  305. { comparing of nodes }
  306. function isequal(p : tnode) : boolean;
  307. { to implement comparisation, override this method }
  308. function docompare(p : tnode) : boolean;virtual;
  309. { gets a copy of the node }
  310. function getcopy : tnode;virtual;
  311. procedure insertintolist(l : tnodelist);virtual;
  312. { writes a node for debugging purpose, shouldn't be called }
  313. { direct, because there is no test for nil, use printnode }
  314. { to write a complete tree }
  315. procedure printnodeinfo(var t:text);
  316. procedure printnodedata(var t:text);virtual;
  317. procedure printnodetree(var t:text);virtual;
  318. procedure concattolist(l : tlinkedlist);virtual;
  319. function ischild(p : tnode) : boolean;virtual;
  320. procedure set_file_line(from : tnode);
  321. procedure set_tree_filepos(const filepos : tfileposinfo);
  322. end;
  323. tnodeclass = class of tnode;
  324. tnodeclassarray = array[tnodetype] of tnodeclass;
  325. { this node is the anchestor for all nodes with at least }
  326. { one child, you have to use it if you want to use }
  327. { true- and falselabel }
  328. punarynode = ^tunarynode;
  329. tunarynode = class(tnode)
  330. left : tnode;
  331. constructor create(t:tnodetype;l : tnode);
  332. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  333. destructor destroy;override;
  334. procedure ppuwrite(ppufile:tcompilerppufile);override;
  335. procedure derefimpl;override;
  336. procedure concattolist(l : tlinkedlist);override;
  337. function ischild(p : tnode) : boolean;override;
  338. function docompare(p : tnode) : boolean;override;
  339. function getcopy : tnode;override;
  340. procedure insertintolist(l : tnodelist);override;
  341. procedure left_max;
  342. procedure printnodedata(var t:text);override;
  343. end;
  344. pbinarynode = ^tbinarynode;
  345. tbinarynode = class(tunarynode)
  346. right : tnode;
  347. constructor create(t:tnodetype;l,r : tnode);
  348. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  349. destructor destroy;override;
  350. procedure ppuwrite(ppufile:tcompilerppufile);override;
  351. procedure derefimpl;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 getcopy : 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. {$ifdef tempregdebug}
  367. type
  368. pptree = ^tnode;
  369. var
  370. curptree: pptree;
  371. {$endif tempregdebug}
  372. var
  373. { array with all class types for tnodes }
  374. nodeclass : tnodeclassarray;
  375. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  376. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  377. const
  378. printnodespacing = ' ';
  379. var
  380. { indention used when writing the tree to the screen }
  381. printnodeindention : string;
  382. procedure printnodeindent;
  383. procedure printnodeunindent;
  384. procedure printnode(var t:text;n:tnode);
  385. implementation
  386. uses
  387. cutils,verbose;
  388. const
  389. ppunodemarker = 255;
  390. {****************************************************************************
  391. Helpers
  392. ****************************************************************************}
  393. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  394. var
  395. b : byte;
  396. t : tnodetype;
  397. begin
  398. { marker }
  399. b:=ppufile.getbyte;
  400. if b<>ppunodemarker then
  401. internalerror(200208151);
  402. { load nodetype }
  403. t:=tnodetype(ppufile.getbyte);
  404. if t>high(tnodetype) then
  405. internalerror(200208152);
  406. if t<>emptynode then
  407. begin
  408. if not assigned(nodeclass[t]) then
  409. internalerror(200208153);
  410. //writeln('load: ',nodetype2str[t]);
  411. { generate node of the correct class }
  412. ppuloadnode:=nodeclass[t].ppuload(t,ppufile);
  413. end
  414. else
  415. ppuloadnode:=nil;
  416. end;
  417. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  418. begin
  419. { marker, read by ppuloadnode }
  420. ppufile.putbyte(ppunodemarker);
  421. { type, read by ppuloadnode }
  422. if assigned(n) then
  423. begin
  424. ppufile.putbyte(byte(n.nodetype));
  425. //writeln('write: ',nodetype2str[n.nodetype]);
  426. n.ppuwrite(ppufile);
  427. end
  428. else
  429. ppufile.putbyte(byte(emptynode));
  430. end;
  431. procedure printnodeindent;
  432. begin
  433. printnodeindention:=printnodeindention+printnodespacing;
  434. end;
  435. procedure printnodeunindent;
  436. begin
  437. delete(printnodeindention,1,length(printnodespacing));
  438. end;
  439. procedure printnode(var t:text;n:tnode);
  440. begin
  441. if assigned(n) then
  442. n.printnodetree(t)
  443. else
  444. writeln(t,printnodeindention,'nil');
  445. end;
  446. {****************************************************************************
  447. TNODE
  448. ****************************************************************************}
  449. constructor tnode.create(t:tnodetype);
  450. begin
  451. inherited create;
  452. nodetype:=t;
  453. blocktype:=block_type;
  454. { updated by firstpass }
  455. expectloc:=LOC_INVALID;
  456. { updated by secondpass }
  457. location.loc:=LOC_INVALID;
  458. { save local info }
  459. fileinfo:=aktfilepos;
  460. localswitches:=aktlocalswitches;
  461. resulttype.reset;
  462. registers32:=0;
  463. registersfpu:=0;
  464. {$ifdef SUPPORT_MMX}
  465. registersmmx:=0;
  466. {$endif SUPPORT_MMX}
  467. {$ifdef EXTDEBUG}
  468. maxfirstpasscount:=0;
  469. firstpasscount:=0;
  470. {$endif EXTDEBUG}
  471. flags:=[];
  472. end;
  473. constructor tnode.createforcopy;
  474. begin
  475. end;
  476. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  477. begin
  478. nodetype:=t;
  479. { tnode fields }
  480. blocktype:=tblock_type(ppufile.getbyte);
  481. ppufile.getposinfo(fileinfo);
  482. ppufile.getsmallset(localswitches);
  483. ppufile.gettype(resulttype);
  484. ppufile.getsmallset(flags);
  485. { updated by firstpass }
  486. expectloc:=LOC_INVALID;
  487. { updated by secondpass }
  488. location.loc:=LOC_INVALID;
  489. registers32:=0;
  490. registersfpu:=0;
  491. {$ifdef SUPPORT_MMX}
  492. registersmmx:=0;
  493. {$endif SUPPORT_MMX}
  494. {$ifdef EXTDEBUG}
  495. maxfirstpasscount:=0;
  496. firstpasscount:=0;
  497. {$endif EXTDEBUG}
  498. end;
  499. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  500. begin
  501. ppufile.putbyte(byte(block_type));
  502. ppufile.putposinfo(fileinfo);
  503. ppufile.putsmallset(localswitches);
  504. ppufile.puttype(resulttype);
  505. ppufile.putsmallset(flags);
  506. end;
  507. procedure tnode.derefimpl;
  508. begin
  509. resulttype.resolve;
  510. end;
  511. procedure tnode.toggleflag(f : tnodeflag);
  512. begin
  513. if f in flags then
  514. exclude(flags,f)
  515. else
  516. include(flags,f);
  517. end;
  518. destructor tnode.destroy;
  519. begin
  520. {$ifdef EXTDEBUG}
  521. if firstpasscount>maxfirstpasscount then
  522. maxfirstpasscount:=firstpasscount;
  523. {$endif EXTDEBUG}
  524. end;
  525. procedure tnode.concattolist(l : tlinkedlist);
  526. begin
  527. end;
  528. function tnode.ischild(p : tnode) : boolean;
  529. begin
  530. ischild:=false;
  531. end;
  532. procedure tnode.mark_write;
  533. begin
  534. {$ifdef EXTDEBUG}
  535. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  536. {$endif EXTDEBUG}
  537. end;
  538. procedure tnode.printnodeinfo(var t:text);
  539. begin
  540. write(t,nodetype2str[nodetype]);
  541. if assigned(resulttype.def) then
  542. write(t,' ,resulttype = "',resulttype.def.gettypename,'"')
  543. else
  544. write(t,' ,resulttype = <nil>');
  545. writeln(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  546. ', loc = ',tcgloc2str[location.loc],
  547. ', intregs = ',registers32,
  548. ', fpuregs = ',registersfpu);
  549. end;
  550. procedure tnode.printnodedata(var t:text);
  551. begin
  552. end;
  553. procedure tnode.printnodetree(var t:text);
  554. begin
  555. write(t,printnodeindention,'(');
  556. printnodeinfo(t);
  557. printnodeindent;
  558. printnodedata(t);
  559. printnodeunindent;
  560. writeln(t,printnodeindention,')');
  561. end;
  562. function tnode.isequal(p : tnode) : boolean;
  563. begin
  564. isequal:=
  565. (not assigned(self) and not assigned(p)) or
  566. (assigned(self) and assigned(p) and
  567. { optimized subclasses have the same nodetype as their }
  568. { superclass (for compatibility), so also check the classtype (JM) }
  569. (p.classtype=classtype) and
  570. (p.nodetype=nodetype) and
  571. (flags*flagsequal=p.flags*flagsequal) and
  572. docompare(p));
  573. end;
  574. {$ifdef state_tracking}
  575. function Tnode.track_state_pass(exec_known:boolean):boolean;
  576. begin
  577. track_state_pass:=false;
  578. end;
  579. {$endif state_tracking}
  580. function tnode.docompare(p : tnode) : boolean;
  581. begin
  582. docompare:=true;
  583. end;
  584. function tnode.getcopy : tnode;
  585. var
  586. p : tnode;
  587. begin
  588. { this is quite tricky because we need a node of the current }
  589. { node type and not one of tnode! }
  590. p:=tnodeclass(classtype).createforcopy;
  591. p.nodetype:=nodetype;
  592. p.location:=location;
  593. p.parent:=parent;
  594. p.flags:=flags;
  595. p.registers32:=registers32;
  596. p.registersfpu:=registersfpu;
  597. {$ifdef SUPPORT_MMX}
  598. p.registersmmx:=registersmmx;
  599. p.registerskni:=registerskni;
  600. {$endif SUPPORT_MMX}
  601. p.resulttype:=resulttype;
  602. p.fileinfo:=fileinfo;
  603. p.localswitches:=localswitches;
  604. {$ifdef extdebug}
  605. p.firstpasscount:=firstpasscount;
  606. {$endif extdebug}
  607. { p.list:=list; }
  608. getcopy:=p;
  609. end;
  610. procedure tnode.insertintolist(l : tnodelist);
  611. begin
  612. end;
  613. procedure tnode.set_file_line(from : tnode);
  614. begin
  615. if assigned(from) then
  616. fileinfo:=from.fileinfo;
  617. end;
  618. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  619. begin
  620. fileinfo:=filepos;
  621. end;
  622. {****************************************************************************
  623. TUNARYNODE
  624. ****************************************************************************}
  625. constructor tunarynode.create(t:tnodetype;l : tnode);
  626. begin
  627. inherited create(t);
  628. left:=l;
  629. end;
  630. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  631. begin
  632. inherited ppuload(t,ppufile);
  633. left:=ppuloadnode(ppufile);
  634. end;
  635. destructor tunarynode.destroy;
  636. begin
  637. left.free;
  638. inherited destroy;
  639. end;
  640. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  641. begin
  642. inherited ppuwrite(ppufile);
  643. ppuwritenode(ppufile,left);
  644. end;
  645. procedure tunarynode.derefimpl;
  646. begin
  647. inherited derefimpl;
  648. if assigned(left) then
  649. left.derefimpl;
  650. end;
  651. function tunarynode.docompare(p : tnode) : boolean;
  652. begin
  653. docompare:=(inherited docompare(p) and
  654. ((left=nil) or left.isequal(tunarynode(p).left))
  655. );
  656. end;
  657. function tunarynode.getcopy : tnode;
  658. var
  659. p : tunarynode;
  660. begin
  661. p:=tunarynode(inherited getcopy);
  662. if assigned(left) then
  663. p.left:=left.getcopy
  664. else
  665. p.left:=nil;
  666. getcopy:=p;
  667. end;
  668. procedure tunarynode.insertintolist(l : tnodelist);
  669. begin
  670. end;
  671. procedure tunarynode.printnodedata(var t:text);
  672. begin
  673. inherited printnodedata(t);
  674. printnode(t,left);
  675. end;
  676. procedure tunarynode.left_max;
  677. begin
  678. registers32:=left.registers32;
  679. registersfpu:=left.registersfpu;
  680. {$ifdef SUPPORT_MMX}
  681. registersmmx:=left.registersmmx;
  682. {$endif SUPPORT_MMX}
  683. end;
  684. procedure tunarynode.concattolist(l : tlinkedlist);
  685. begin
  686. left.parent:=self;
  687. left.concattolist(l);
  688. inherited concattolist(l);
  689. end;
  690. function tunarynode.ischild(p : tnode) : boolean;
  691. begin
  692. ischild:=p=left;
  693. end;
  694. {****************************************************************************
  695. TBINARYNODE
  696. ****************************************************************************}
  697. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  698. begin
  699. inherited create(t,l);
  700. right:=r
  701. end;
  702. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  703. begin
  704. inherited ppuload(t,ppufile);
  705. right:=ppuloadnode(ppufile);
  706. end;
  707. destructor tbinarynode.destroy;
  708. begin
  709. right.free;
  710. inherited destroy;
  711. end;
  712. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  713. begin
  714. inherited ppuwrite(ppufile);
  715. ppuwritenode(ppufile,right);
  716. end;
  717. procedure tbinarynode.derefimpl;
  718. begin
  719. inherited derefimpl;
  720. if assigned(right) then
  721. right.derefimpl;
  722. end;
  723. procedure tbinarynode.concattolist(l : tlinkedlist);
  724. begin
  725. { we could change that depending on the number of }
  726. { required registers }
  727. left.parent:=self;
  728. left.concattolist(l);
  729. left.parent:=self;
  730. left.concattolist(l);
  731. inherited concattolist(l);
  732. end;
  733. function tbinarynode.ischild(p : tnode) : boolean;
  734. begin
  735. ischild:=(p=right);
  736. end;
  737. function tbinarynode.docompare(p : tnode) : boolean;
  738. begin
  739. docompare:=(inherited docompare(p) and
  740. ((right=nil) or right.isequal(tbinarynode(p).right))
  741. );
  742. end;
  743. function tbinarynode.getcopy : tnode;
  744. var
  745. p : tbinarynode;
  746. begin
  747. p:=tbinarynode(inherited getcopy);
  748. if assigned(right) then
  749. p.right:=right.getcopy
  750. else
  751. p.right:=nil;
  752. getcopy:=p;
  753. end;
  754. procedure tbinarynode.insertintolist(l : tnodelist);
  755. begin
  756. end;
  757. procedure tbinarynode.swapleftright;
  758. var
  759. swapp : tnode;
  760. begin
  761. swapp:=right;
  762. right:=left;
  763. left:=swapp;
  764. if nf_swaped in flags then
  765. exclude(flags,nf_swaped)
  766. else
  767. include(flags,nf_swaped);
  768. end;
  769. procedure tbinarynode.left_right_max;
  770. begin
  771. if assigned(left) then
  772. begin
  773. if assigned(right) then
  774. begin
  775. registers32:=max(left.registers32,right.registers32);
  776. registersfpu:=max(left.registersfpu,right.registersfpu);
  777. {$ifdef SUPPORT_MMX}
  778. registersmmx:=max(left.registersmmx,right.registersmmx);
  779. {$endif SUPPORT_MMX}
  780. end
  781. else
  782. begin
  783. registers32:=left.registers32;
  784. registersfpu:=left.registersfpu;
  785. {$ifdef SUPPORT_MMX}
  786. registersmmx:=left.registersmmx;
  787. {$endif SUPPORT_MMX}
  788. end;
  789. end;
  790. end;
  791. procedure tbinarynode.printnodedata(var t:text);
  792. begin
  793. inherited printnodedata(t);
  794. printnode(t,right);
  795. end;
  796. procedure tbinarynode.printnodelist(var t:text);
  797. var
  798. hp : tbinarynode;
  799. begin
  800. hp:=self;
  801. while assigned(hp) do
  802. begin
  803. write(t,printnodeindention,'(');
  804. printnodeindent;
  805. hp.printnodeinfo(t);
  806. printnode(t,hp.left);
  807. printnodeunindent;
  808. writeln(t,printnodeindention,')');
  809. hp:=tbinarynode(hp.right);
  810. end;
  811. end;
  812. {****************************************************************************
  813. TBINOPYNODE
  814. ****************************************************************************}
  815. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  816. begin
  817. inherited create(t,l,r);
  818. end;
  819. function tbinopnode.docompare(p : tnode) : boolean;
  820. begin
  821. docompare:=(inherited docompare(p)) or
  822. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  823. ((nf_swapable in flags) and
  824. left.isequal(tbinopnode(p).right) and
  825. right.isequal(tbinopnode(p).left));
  826. end;
  827. end.
  828. {
  829. $Log$
  830. Revision 1.59 2003-05-09 17:47:02 peter
  831. * self moved to hidden parameter
  832. * removed hdisposen,hnewn,selfn
  833. Revision 1.58 2003/04/25 20:59:33 peter
  834. * removed funcretn,funcretsym, function result is now in varsym
  835. and aliases for result and function name are added using absolutesym
  836. * vs_hidden parameter for funcret passed in parameter
  837. * vs_hidden fixes
  838. * writenode changed to printnode and released from extdebug
  839. * -vp option added to generate a tree.log with the nodetree
  840. * nicer printnode for statements, callnode
  841. Revision 1.57 2002/04/25 20:15:39 florian
  842. * block nodes within expressions shouldn't release the used registers,
  843. fixed using a flag till the new rg is ready
  844. Revision 1.56 2003/04/24 22:29:58 florian
  845. * fixed a lot of PowerPC related stuff
  846. Revision 1.55 2003/04/23 10:12:14 peter
  847. * allow multi pass2 changed to global boolean instead of node flag
  848. Revision 1.54 2003/04/22 23:50:23 peter
  849. * firstpass uses expectloc
  850. * checks if there are differences between the expectloc and
  851. location.loc from secondpass in EXTDEBUG
  852. Revision 1.53 2003/04/22 09:52:00 peter
  853. * mark_write implemented for default with a warning in EXTDEBUG, this
  854. is required for error recovery where the left node can be also a non
  855. writable node
  856. Revision 1.52 2003/04/10 17:57:52 peter
  857. * vs_hidden released
  858. Revision 1.51 2003/03/28 19:16:56 peter
  859. * generic constructor working for i386
  860. * remove fixed self register
  861. * esi added as address register for i386
  862. Revision 1.50 2003/03/17 16:54:41 peter
  863. * support DefaultHandler and anonymous inheritance fixed
  864. for message methods
  865. Revision 1.49 2003/01/04 15:54:03 daniel
  866. * Fixed mark_write for @ operator
  867. (can happen when compiling @procvar:=nil (Delphi mode construction))
  868. Revision 1.48 2003/01/03 21:03:02 peter
  869. * made mark_write dummy instead of abstract
  870. Revision 1.47 2003/01/03 12:15:56 daniel
  871. * Removed ifdefs around notifications
  872. ifdefs around for loop optimizations remain
  873. Revision 1.46 2002/12/26 18:24:33 jonas
  874. * fixed check for whether or not a high parameter was already generated
  875. * no type checking/conversions for invisible parameters
  876. Revision 1.45 2002/11/28 11:17:04 florian
  877. * loop node flags from node flags splitted
  878. Revision 1.44 2002/10/05 00:48:57 peter
  879. * support inherited; support for overload as it is handled by
  880. delphi. This is only for delphi mode as it is working is
  881. undocumented and hard to predict what is done
  882. Revision 1.43 2002/09/07 15:25:03 peter
  883. * old logs removed and tabs fixed
  884. Revision 1.42 2002/09/03 16:26:26 daniel
  885. * Make Tprocdef.defs protected
  886. Revision 1.41 2002/09/01 13:28:38 daniel
  887. - write_access fields removed in favor of a flag
  888. Revision 1.40 2002/09/01 08:01:16 daniel
  889. * Removed sets from Tcallnode.det_resulttype
  890. + Added read/write notifications of variables. These will be usefull
  891. for providing information for several optimizations. For example
  892. the value of the loop variable of a for loop does matter is the
  893. variable is read after the for loop, but if it's no longer used
  894. or written, it doesn't matter and this can be used to optimize
  895. the loop code generation.
  896. Revision 1.39 2002/08/22 11:21:45 florian
  897. + register32 is now written by tnode.dowrite
  898. * fixed write of value of tconstnode
  899. Revision 1.38 2002/08/19 19:36:44 peter
  900. * More fixes for cross unit inlining, all tnodes are now implemented
  901. * Moved pocall_internconst to po_internconst because it is not a
  902. calling type at all and it conflicted when inlining of these small
  903. functions was requested
  904. Revision 1.37 2002/08/18 20:06:24 peter
  905. * inlining is now also allowed in interface
  906. * renamed write/load to ppuwrite/ppuload
  907. * tnode storing in ppu
  908. * nld,ncon,nbas are already updated for storing in ppu
  909. Revision 1.36 2002/08/17 22:09:46 florian
  910. * result type handling in tcgcal.pass_2 overhauled
  911. * better tnode.dowrite
  912. * some ppc stuff fixed
  913. Revision 1.35 2002/08/15 19:10:35 peter
  914. * first things tai,tnode storing in ppu
  915. Revision 1.34 2002/08/09 19:15:41 carl
  916. - removed newcg define
  917. Revision 1.33 2002/07/23 12:34:30 daniel
  918. * Readded old set code. To use it define 'oldset'. Activated by default
  919. for ppc.
  920. Revision 1.32 2002/07/22 11:48:04 daniel
  921. * Sets are now internally sets.
  922. Revision 1.31 2002/07/21 06:58:49 daniel
  923. * Changed booleans into flags
  924. Revision 1.30 2002/07/19 11:41:36 daniel
  925. * State tracker work
  926. * The whilen and repeatn are now completely unified into whilerepeatn. This
  927. allows the state tracker to change while nodes automatically into
  928. repeat nodes.
  929. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  930. 'not(a>b)' is optimized into 'a<=b'.
  931. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  932. by removing the notn and later switchting the true and falselabels. The
  933. same is done with 'repeat until not a'.
  934. Revision 1.29 2002/07/14 18:00:44 daniel
  935. + Added the beginning of a state tracker. This will track the values of
  936. variables through procedures and optimize things away.
  937. Revision 1.28 2002/07/01 18:46:24 peter
  938. * internal linker
  939. * reorganized aasm layer
  940. Revision 1.27 2002/05/18 13:34:10 peter
  941. * readded missing revisions
  942. Revision 1.26 2002/05/16 19:46:39 carl
  943. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  944. + try to fix temp allocation (still in ifdef)
  945. + generic constructor calls
  946. + start of tassembler / tmodulebase class cleanup
  947. Revision 1.24 2002/04/21 19:02:04 peter
  948. * removed newn and disposen nodes, the code is now directly
  949. inlined from pexpr
  950. * -an option that will write the secondpass nodes to the .s file, this
  951. requires EXTDEBUG define to actually write the info
  952. * fixed various internal errors and crashes due recent code changes
  953. Revision 1.23 2002/04/06 18:13:01 jonas
  954. * several powerpc-related additions and fixes
  955. Revision 1.22 2002/03/31 20:26:35 jonas
  956. + a_loadfpu_* and a_loadmm_* methods in tcg
  957. * register allocation is now handled by a class and is mostly processor
  958. independent (+rgobj.pas and i386/rgcpu.pas)
  959. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  960. * some small improvements and fixes to the optimizer
  961. * some register allocation fixes
  962. * some fpuvaroffset fixes in the unary minus node
  963. * push/popusedregisters is now called rg.save/restoreusedregisters and
  964. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  965. also better optimizable)
  966. * fixed and optimized register saving/restoring for new/dispose nodes
  967. * LOC_FPU locations now also require their "register" field to be set to
  968. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  969. - list field removed of the tnode class because it's not used currently
  970. and can cause hard-to-find bugs
  971. }