node.pas 31 KB

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