node.pas 32 KB

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