node.pas 34 KB

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