node.pas 32 KB

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