node.pas 36 KB

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