node.pas 32 KB

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