node.pas 33 KB

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