node.pas 36 KB

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