node.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Basic node handling
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit node;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. globtype,globals,
  24. cpubase,
  25. aasmbase,
  26. symtype,symppu;
  27. type
  28. pconstset = ^tconstset;
  29. {$ifdef oldset}
  30. tconstset = array[0..31] of byte;
  31. pconst32bitset = ^tconst32bitset;
  32. tconst32bitset = array[0..7] of longint;
  33. {$else}
  34. tconstset = set of 0..255;
  35. {$endif}
  36. tnodetype = (
  37. emptynode, {No node (returns nil when loading from ppu)}
  38. addn, {Represents the + operator}
  39. muln, {Represents the * operator}
  40. subn, {Represents the - operator}
  41. divn, {Represents the div operator}
  42. symdifn, {Represents the >< operator}
  43. modn, {Represents the mod operator}
  44. assignn, {Represents an assignment}
  45. loadn, {Represents the use of a variabele}
  46. rangen, {Represents a range (i.e. 0..9)}
  47. ltn, {Represents the < operator}
  48. lten, {Represents the <= operator}
  49. gtn, {Represents the > operator}
  50. gten, {Represents the >= operator}
  51. equaln, {Represents the = operator}
  52. unequaln, {Represents the <> operator}
  53. inn, {Represents the in operator}
  54. orn, {Represents the or operator}
  55. xorn, {Represents the xor operator}
  56. shrn, {Represents the shr operator}
  57. shln, {Represents the shl operator}
  58. slashn, {Represents the / operator}
  59. andn, {Represents the and operator}
  60. subscriptn, {Field in a record/object}
  61. derefn, {Dereferences a pointer}
  62. addrn, {Represents the @ operator}
  63. doubleaddrn, {Represents the @@ operator}
  64. ordconstn, {Represents an ordinal value}
  65. typeconvn, {Represents type-conversion/typecast}
  66. calln, {Represents a call node}
  67. callparan, {Represents a parameter}
  68. realconstn, {Represents a real value}
  69. unaryminusn, {Represents a sign change (i.e. -2)}
  70. asmn, {Represents an assembler node }
  71. vecn, {Represents array indexing}
  72. pointerconstn, {Represents a pointer constant}
  73. stringconstn, {Represents a string constant}
  74. funcretn, {Represents the function result var}
  75. selfn, {Represents the self parameter}
  76. notn, {Represents the not operator}
  77. inlinen, {Internal procedures (i.e. writeln)}
  78. niln, {Represents the nil pointer}
  79. errorn, {This part of the tree could not be
  80. parsed because of a compiler error}
  81. typen, {A type name. Used for i.e. typeof(obj)}
  82. hnewn, {The new operation, constructor call}
  83. hdisposen, {The dispose operation with destructor call}
  84. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  85. setconstn, {A set constant (i.e. [1,2])}
  86. blockn, {A block of statements}
  87. statementn, {One statement in a block of nodes}
  88. ifn, {An if statement}
  89. breakn, {A break statement}
  90. continuen, {A continue statement}
  91. whilerepeatn, {A while or repeat statement}
  92. forn, {A for loop}
  93. exitn, {An exit statement}
  94. withn, {A with statement}
  95. casen, {A case statement}
  96. labeln, {A label}
  97. goton, {A goto statement}
  98. tryexceptn, {A try except block}
  99. raisen, {A raise statement}
  100. tryfinallyn, {A try finally statement}
  101. onn, {For an on statement in exception code}
  102. isn, {Represents the is operator}
  103. asn, {Represents the as typecast}
  104. caretn, {Represents the ^ operator}
  105. failn, {Represents the fail statement}
  106. starstarn, {Represents the ** operator exponentiation }
  107. procinlinen, {Procedures that can be inlined }
  108. arrayconstructorn, {Construction node for [...] parsing}
  109. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  110. tempcreaten, { for temps in the result/firstpass }
  111. temprefn, { references to temps }
  112. tempdeleten, { for temps in the result/firstpass }
  113. addoptn, { added for optimizations where we cannot suppress }
  114. nothingn, {NOP, Do nothing}
  115. loadvmtn, {Load the address of the VMT of a class/object}
  116. guidconstn, {A GUID COM Interface constant }
  117. rttin {Rtti information so they can be accessed in result/firstpass}
  118. );
  119. const
  120. nodetype2str : array[tnodetype] of string[20] = (
  121. '<emptynode>',
  122. 'addn',
  123. 'muln',
  124. 'subn',
  125. 'divn',
  126. 'symdifn',
  127. 'modn',
  128. 'assignn',
  129. 'loadn',
  130. 'rangen',
  131. 'ltn',
  132. 'lten',
  133. 'gtn',
  134. 'gten',
  135. 'equaln',
  136. 'unequaln',
  137. 'inn',
  138. 'orn',
  139. 'xorn',
  140. 'shrn',
  141. 'shln',
  142. 'slashn',
  143. 'andn',
  144. 'subscriptn',
  145. 'derefn',
  146. 'addrn',
  147. 'doubleaddrn',
  148. 'ordconstn',
  149. 'typeconvn',
  150. 'calln',
  151. 'callparan',
  152. 'realconstn',
  153. 'unaryminusn',
  154. 'asmn',
  155. 'vecn',
  156. 'pointerconstn',
  157. 'stringconstn',
  158. 'funcretn',
  159. 'selfn',
  160. 'notn',
  161. 'inlinen',
  162. 'niln',
  163. 'errorn',
  164. 'typen',
  165. 'hnewn',
  166. 'hdisposen',
  167. 'setelementn',
  168. 'setconstn',
  169. 'blockn',
  170. 'statementn',
  171. 'ifn',
  172. 'breakn',
  173. 'continuen',
  174. 'whilerepeatn',
  175. 'forn',
  176. 'exitn',
  177. 'withn',
  178. 'casen',
  179. 'labeln',
  180. 'goton',
  181. 'tryexceptn',
  182. 'raisen',
  183. 'tryfinallyn',
  184. 'onn',
  185. 'isn',
  186. 'asn',
  187. 'caretn',
  188. 'failn',
  189. 'starstarn',
  190. 'procinlinen',
  191. 'arrayconstructn',
  192. 'arrayconstructrangen',
  193. 'tempcreaten',
  194. 'temprefn',
  195. 'tempdeleten',
  196. 'addoptn',
  197. 'nothingn',
  198. 'loadvmtn',
  199. 'guidconstn',
  200. 'rttin');
  201. type
  202. { all boolean field of ttree are now collected in flags }
  203. tnodeflags = (
  204. nf_needs_truefalselabel,
  205. nf_swapable, { tbinop operands can be swaped }
  206. nf_swaped, { tbinop operands are swaped }
  207. nf_error,
  208. nf_write, { Node is written to }
  209. { flags used by tcallnode }
  210. nf_return_value_used,
  211. nf_static_call,
  212. nf_auto_inherited,
  213. { flags used by tcallparanode }
  214. nf_varargs_para, { belongs this para to varargs }
  215. nf_hightree_generated, { has the hightree for thispara been generated }
  216. { taddrnode }
  217. nf_procvarload,
  218. { tvecnode }
  219. nf_memindex,
  220. nf_memseg,
  221. nf_callunique,
  222. { twithnode }
  223. nf_islocal,
  224. { tloadnode }
  225. nf_absolute,
  226. nf_first,
  227. { tassignmentnode }
  228. nf_concat_string,
  229. { tfuncretnode }
  230. nf_is_first_funcret,
  231. { tarrayconstructnode }
  232. nf_cargs, { 20th }
  233. nf_cargswap,
  234. nf_forcevaria,
  235. nf_novariaallowed,
  236. { ttypeconvnode }
  237. nf_explizit,
  238. { tinlinenode }
  239. nf_inlineconst,
  240. { general }
  241. nf_isproperty,
  242. nf_varstateset,
  243. { tasmnode }
  244. nf_object_preserved, { 30th }
  245. { taddnode }
  246. nf_use_strconcat
  247. );
  248. tnodeflagset = set of tnodeflags;
  249. const
  250. { contains the flags which must be equal for the equality }
  251. { of nodes }
  252. flagsequal : tnodeflagset = [nf_error,nf_static_call];
  253. type
  254. tnodelist = class
  255. end;
  256. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  257. tnode = class
  258. public
  259. { type of this node }
  260. nodetype : tnodetype;
  261. { type of the current code block, general/const/type }
  262. blocktype : tblock_type;
  263. { the location of the result of this node }
  264. location : tlocation;
  265. { the parent node of this is node }
  266. { this field is set by concattolist }
  267. parent : tnode;
  268. { there are some properties about the node stored }
  269. flags : tnodeflagset;
  270. { the number of registers needed to evalute the node }
  271. registers32,registersfpu : longint; { must be longint !!!! }
  272. {$ifdef SUPPORT_MMX}
  273. registersmmx,registerskni : longint;
  274. {$endif SUPPORT_MMX}
  275. resulttype : ttype;
  276. fileinfo : tfileposinfo;
  277. localswitches : tlocalswitches;
  278. {$ifdef extdebug}
  279. maxfirstpasscount,
  280. firstpasscount : longint;
  281. {$endif extdebug}
  282. constructor create(t:tnodetype);
  283. { this constructor is only for creating copies of class }
  284. { the fields are copied by getcopy }
  285. constructor createforcopy;
  286. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  287. destructor destroy;override;
  288. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  289. procedure derefimpl;virtual;
  290. { toggles the flag }
  291. procedure toggleflag(f : tnodeflags);
  292. { the 1.1 code generator may override pass_1 }
  293. { and it need not to implement det_* then }
  294. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  295. { 2.0: runs det_resulttype and det_temp }
  296. function pass_1 : tnode;virtual;abstract;
  297. { dermines the resulttype of the node }
  298. function det_resulttype : tnode;virtual;abstract;
  299. { dermines the number of necessary temp. locations to evaluate
  300. the node }
  301. {$ifdef state_tracking}
  302. { Does optimizations by keeping track of the variable states
  303. in a procedure }
  304. function track_state_pass(exec_known:boolean):boolean;virtual;
  305. {$endif}
  306. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  307. written to (normally the loadnode) as write access. }
  308. procedure mark_write;virtual;abstract;
  309. procedure det_temp;virtual;abstract;
  310. procedure pass_2;virtual;abstract;
  311. { comparing of nodes }
  312. function isequal(p : tnode) : boolean;
  313. { to implement comparisation, override this method }
  314. function docompare(p : tnode) : boolean;virtual;
  315. { gets a copy of the node }
  316. function getcopy : tnode;virtual;
  317. procedure insertintolist(l : tnodelist);virtual;
  318. {$ifdef EXTDEBUG}
  319. { writes a node for debugging purpose, shouldn't be called }
  320. { direct, because there is no test for nil, use writenode }
  321. { to write a complete tree }
  322. procedure dowrite;
  323. procedure dowritenodetype;virtual;
  324. procedure _dowrite;virtual;
  325. {$endif EXTDEBUG}
  326. procedure concattolist(l : tlinkedlist);virtual;
  327. function ischild(p : tnode) : boolean;virtual;
  328. procedure set_file_line(from : tnode);
  329. procedure set_tree_filepos(const filepos : tfileposinfo);
  330. end;
  331. tnodeclass = class of tnode;
  332. tnodeclassarray = array[tnodetype] of tnodeclass;
  333. { this node is the anchestor for all nodes with at least }
  334. { one child, you have to use it if you want to use }
  335. { true- and falselabel }
  336. punarynode = ^tunarynode;
  337. tunarynode = class(tnode)
  338. left : tnode;
  339. constructor create(t:tnodetype;l : tnode);
  340. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  341. destructor destroy;override;
  342. procedure ppuwrite(ppufile:tcompilerppufile);override;
  343. procedure derefimpl;override;
  344. procedure concattolist(l : tlinkedlist);override;
  345. function ischild(p : tnode) : boolean;override;
  346. function docompare(p : tnode) : boolean;override;
  347. function getcopy : tnode;override;
  348. procedure insertintolist(l : tnodelist);override;
  349. procedure left_max;
  350. {$ifdef extdebug}
  351. procedure _dowrite;override;
  352. {$endif extdebug}
  353. end;
  354. pbinarynode = ^tbinarynode;
  355. tbinarynode = class(tunarynode)
  356. right : tnode;
  357. constructor create(t:tnodetype;l,r : tnode);
  358. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  359. destructor destroy;override;
  360. procedure ppuwrite(ppufile:tcompilerppufile);override;
  361. procedure derefimpl;override;
  362. procedure concattolist(l : tlinkedlist);override;
  363. function ischild(p : tnode) : boolean;override;
  364. function docompare(p : tnode) : boolean;override;
  365. procedure swapleftright;
  366. function getcopy : tnode;override;
  367. procedure insertintolist(l : tnodelist);override;
  368. procedure left_right_max;
  369. {$ifdef extdebug}
  370. procedure _dowrite;override;
  371. {$endif extdebug}
  372. end;
  373. tbinopnode = class(tbinarynode)
  374. constructor create(t:tnodetype;l,r : tnode);virtual;
  375. function docompare(p : tnode) : boolean;override;
  376. end;
  377. {$ifdef tempregdebug}
  378. type
  379. pptree = ^tnode;
  380. var
  381. curptree: pptree;
  382. {$endif tempregdebug}
  383. var
  384. { array with all class types for tnodes }
  385. nodeclass : tnodeclassarray;
  386. {$ifdef EXTDEBUG}
  387. { indention used when writing the tree to the screen }
  388. writenodeindention : string;
  389. {$endif EXTDEBUG}
  390. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  391. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  392. {$ifdef EXTDEBUG}
  393. procedure writenode(t:tnode);
  394. {$endif EXTDEBUG}
  395. implementation
  396. uses
  397. cutils,verbose;
  398. const
  399. ppunodemarker = 255;
  400. {****************************************************************************
  401. Helpers
  402. ****************************************************************************}
  403. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  404. var
  405. b : byte;
  406. t : tnodetype;
  407. begin
  408. { marker }
  409. b:=ppufile.getbyte;
  410. if b<>ppunodemarker then
  411. internalerror(200208151);
  412. { load nodetype }
  413. t:=tnodetype(ppufile.getbyte);
  414. if t>high(tnodetype) then
  415. internalerror(200208152);
  416. if t<>emptynode then
  417. begin
  418. if not assigned(nodeclass[t]) then
  419. internalerror(200208153);
  420. //writeln('load: ',nodetype2str[t]);
  421. { generate node of the correct class }
  422. ppuloadnode:=nodeclass[t].ppuload(t,ppufile);
  423. end
  424. else
  425. ppuloadnode:=nil;
  426. end;
  427. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  428. begin
  429. { marker, read by ppuloadnode }
  430. ppufile.putbyte(ppunodemarker);
  431. { type, read by ppuloadnode }
  432. if assigned(n) then
  433. begin
  434. ppufile.putbyte(byte(n.nodetype));
  435. //writeln('write: ',nodetype2str[n.nodetype]);
  436. n.ppuwrite(ppufile);
  437. end
  438. else
  439. ppufile.putbyte(byte(emptynode));
  440. end;
  441. {$ifdef EXTDEBUG}
  442. procedure writenode(t:tnode);
  443. begin
  444. if assigned(t) then
  445. t.dowrite
  446. else
  447. write(writenodeindention,'nil');
  448. if writenodeindention='' then
  449. writeln;
  450. end;
  451. {$endif EXTDEBUG}
  452. {****************************************************************************
  453. TNODE
  454. ****************************************************************************}
  455. constructor tnode.create(t:tnodetype);
  456. begin
  457. inherited create;
  458. nodetype:=t;
  459. blocktype:=block_type;
  460. { this allows easier error tracing }
  461. location.loc:=LOC_INVALID;
  462. { save local info }
  463. fileinfo:=aktfilepos;
  464. localswitches:=aktlocalswitches;
  465. resulttype.reset;
  466. registers32:=0;
  467. registersfpu:=0;
  468. {$ifdef SUPPORT_MMX}
  469. registersmmx:=0;
  470. {$endif SUPPORT_MMX}
  471. {$ifdef EXTDEBUG}
  472. maxfirstpasscount:=0;
  473. firstpasscount:=0;
  474. {$endif EXTDEBUG}
  475. flags:=[];
  476. end;
  477. constructor tnode.createforcopy;
  478. begin
  479. end;
  480. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  481. begin
  482. nodetype:=t;
  483. { tnode fields }
  484. blocktype:=tblock_type(ppufile.getbyte);
  485. ppufile.getposinfo(fileinfo);
  486. ppufile.getsmallset(localswitches);
  487. ppufile.gettype(resulttype);
  488. ppufile.getsmallset(flags);
  489. { updated by firstpass }
  490. location.loc:=LOC_INVALID;
  491. registers32:=0;
  492. registersfpu:=0;
  493. {$ifdef SUPPORT_MMX}
  494. registersmmx:=0;
  495. {$endif SUPPORT_MMX}
  496. {$ifdef EXTDEBUG}
  497. maxfirstpasscount:=0;
  498. firstpasscount:=0;
  499. {$endif EXTDEBUG}
  500. end;
  501. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  502. begin
  503. ppufile.putbyte(byte(block_type));
  504. ppufile.putposinfo(fileinfo);
  505. ppufile.putsmallset(localswitches);
  506. ppufile.puttype(resulttype);
  507. ppufile.putsmallset(flags);
  508. end;
  509. procedure tnode.derefimpl;
  510. begin
  511. resulttype.resolve;
  512. end;
  513. procedure tnode.toggleflag(f : tnodeflags);
  514. begin
  515. if f in flags then
  516. exclude(flags,f)
  517. else
  518. include(flags,f);
  519. end;
  520. destructor tnode.destroy;
  521. begin
  522. {$ifdef EXTDEBUG}
  523. if firstpasscount>maxfirstpasscount then
  524. maxfirstpasscount:=firstpasscount;
  525. {$endif EXTDEBUG}
  526. end;
  527. procedure tnode.concattolist(l : tlinkedlist);
  528. begin
  529. end;
  530. function tnode.ischild(p : tnode) : boolean;
  531. begin
  532. ischild:=false;
  533. end;
  534. {$ifdef EXTDEBUG}
  535. procedure tnode._dowrite;
  536. begin
  537. dowritenodetype;
  538. if assigned(resulttype.def) then
  539. write(',resulttype = "',resulttype.def.gettypename,'"')
  540. else
  541. write(',resulttype = <nil>');
  542. write(',location.loc = ',ord(location.loc));
  543. write(',registersint = ',registers32);
  544. write(',registersfpu = ',registersfpu);
  545. end;
  546. procedure tnode.dowritenodetype;
  547. begin
  548. write(nodetype2str[nodetype]);
  549. end;
  550. procedure tnode.dowrite;
  551. begin
  552. write(writenodeindention,'(');
  553. writenodeindention:=writenodeindention+' ';
  554. _dowrite;
  555. writeln;
  556. delete(writenodeindention,1,4);
  557. write(writenodeindention,')');
  558. end;
  559. {$endif EXTDEBUG}
  560. function tnode.isequal(p : tnode) : boolean;
  561. begin
  562. isequal:=
  563. (not assigned(self) and not assigned(p)) or
  564. (assigned(self) and assigned(p) and
  565. { optimized subclasses have the same nodetype as their }
  566. { superclass (for compatibility), so also check the classtype (JM) }
  567. (p.classtype=classtype) and
  568. (p.nodetype=nodetype) and
  569. (flags*flagsequal=p.flags*flagsequal) and
  570. docompare(p));
  571. end;
  572. {$ifdef state_tracking}
  573. function Tnode.track_state_pass(exec_known:boolean):boolean;
  574. begin
  575. track_state_pass:=false;
  576. end;
  577. {$endif state_tracking}
  578. function tnode.docompare(p : tnode) : boolean;
  579. begin
  580. docompare:=true;
  581. end;
  582. function tnode.getcopy : tnode;
  583. var
  584. p : tnode;
  585. begin
  586. { this is quite tricky because we need a node of the current }
  587. { node type and not one of tnode! }
  588. p:=tnodeclass(classtype).createforcopy;
  589. p.nodetype:=nodetype;
  590. p.location:=location;
  591. p.parent:=parent;
  592. p.flags:=flags;
  593. p.registers32:=registers32;
  594. p.registersfpu:=registersfpu;
  595. {$ifdef SUPPORT_MMX}
  596. p.registersmmx:=registersmmx;
  597. p.registerskni:=registerskni;
  598. {$endif SUPPORT_MMX}
  599. p.resulttype:=resulttype;
  600. p.fileinfo:=fileinfo;
  601. p.localswitches:=localswitches;
  602. {$ifdef extdebug}
  603. p.firstpasscount:=firstpasscount;
  604. {$endif extdebug}
  605. { p.list:=list; }
  606. getcopy:=p;
  607. end;
  608. procedure tnode.insertintolist(l : tnodelist);
  609. begin
  610. end;
  611. procedure tnode.set_file_line(from : tnode);
  612. begin
  613. if assigned(from) then
  614. fileinfo:=from.fileinfo;
  615. end;
  616. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  617. begin
  618. fileinfo:=filepos;
  619. end;
  620. {****************************************************************************
  621. TUNARYNODE
  622. ****************************************************************************}
  623. constructor tunarynode.create(t:tnodetype;l : tnode);
  624. begin
  625. inherited create(t);
  626. left:=l;
  627. end;
  628. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  629. begin
  630. inherited ppuload(t,ppufile);
  631. left:=ppuloadnode(ppufile);
  632. end;
  633. destructor tunarynode.destroy;
  634. begin
  635. left.free;
  636. inherited destroy;
  637. end;
  638. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  639. begin
  640. inherited ppuwrite(ppufile);
  641. ppuwritenode(ppufile,left);
  642. end;
  643. procedure tunarynode.derefimpl;
  644. begin
  645. inherited derefimpl;
  646. if assigned(left) then
  647. left.derefimpl;
  648. end;
  649. function tunarynode.docompare(p : tnode) : boolean;
  650. begin
  651. docompare:=(inherited docompare(p) and
  652. ((left=nil) or left.isequal(tunarynode(p).left))
  653. );
  654. end;
  655. function tunarynode.getcopy : tnode;
  656. var
  657. p : tunarynode;
  658. begin
  659. p:=tunarynode(inherited getcopy);
  660. if assigned(left) then
  661. p.left:=left.getcopy
  662. else
  663. p.left:=nil;
  664. getcopy:=p;
  665. end;
  666. procedure tunarynode.insertintolist(l : tnodelist);
  667. begin
  668. end;
  669. {$ifdef extdebug}
  670. procedure tunarynode._dowrite;
  671. begin
  672. inherited _dowrite;
  673. writeln(',');
  674. writenode(left);
  675. end;
  676. {$endif}
  677. procedure tunarynode.left_max;
  678. begin
  679. registers32:=left.registers32;
  680. registersfpu:=left.registersfpu;
  681. {$ifdef SUPPORT_MMX}
  682. registersmmx:=left.registersmmx;
  683. {$endif SUPPORT_MMX}
  684. end;
  685. procedure tunarynode.concattolist(l : tlinkedlist);
  686. begin
  687. left.parent:=self;
  688. left.concattolist(l);
  689. inherited concattolist(l);
  690. end;
  691. function tunarynode.ischild(p : tnode) : boolean;
  692. begin
  693. ischild:=p=left;
  694. end;
  695. {****************************************************************************
  696. TBINARYNODE
  697. ****************************************************************************}
  698. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  699. begin
  700. inherited create(t,l);
  701. right:=r
  702. end;
  703. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  704. begin
  705. inherited ppuload(t,ppufile);
  706. right:=ppuloadnode(ppufile);
  707. end;
  708. destructor tbinarynode.destroy;
  709. begin
  710. right.free;
  711. inherited destroy;
  712. end;
  713. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  714. begin
  715. inherited ppuwrite(ppufile);
  716. ppuwritenode(ppufile,right);
  717. end;
  718. procedure tbinarynode.derefimpl;
  719. begin
  720. inherited derefimpl;
  721. if assigned(right) then
  722. right.derefimpl;
  723. end;
  724. procedure tbinarynode.concattolist(l : tlinkedlist);
  725. begin
  726. { we could change that depending on the number of }
  727. { required registers }
  728. left.parent:=self;
  729. left.concattolist(l);
  730. left.parent:=self;
  731. left.concattolist(l);
  732. inherited concattolist(l);
  733. end;
  734. function tbinarynode.ischild(p : tnode) : boolean;
  735. begin
  736. ischild:=(p=right);
  737. end;
  738. function tbinarynode.docompare(p : tnode) : boolean;
  739. begin
  740. docompare:=(inherited docompare(p) and
  741. ((right=nil) or right.isequal(tbinarynode(p).right))
  742. );
  743. end;
  744. function tbinarynode.getcopy : tnode;
  745. var
  746. p : tbinarynode;
  747. begin
  748. p:=tbinarynode(inherited getcopy);
  749. if assigned(right) then
  750. p.right:=right.getcopy
  751. else
  752. p.right:=nil;
  753. getcopy:=p;
  754. end;
  755. procedure tbinarynode.insertintolist(l : tnodelist);
  756. begin
  757. end;
  758. procedure tbinarynode.swapleftright;
  759. var
  760. swapp : tnode;
  761. begin
  762. swapp:=right;
  763. right:=left;
  764. left:=swapp;
  765. if nf_swaped in flags then
  766. exclude(flags,nf_swaped)
  767. else
  768. include(flags,nf_swaped);
  769. end;
  770. procedure tbinarynode.left_right_max;
  771. begin
  772. if assigned(left) then
  773. begin
  774. if assigned(right) then
  775. begin
  776. registers32:=max(left.registers32,right.registers32);
  777. registersfpu:=max(left.registersfpu,right.registersfpu);
  778. {$ifdef SUPPORT_MMX}
  779. registersmmx:=max(left.registersmmx,right.registersmmx);
  780. {$endif SUPPORT_MMX}
  781. end
  782. else
  783. begin
  784. registers32:=left.registers32;
  785. registersfpu:=left.registersfpu;
  786. {$ifdef SUPPORT_MMX}
  787. registersmmx:=left.registersmmx;
  788. {$endif SUPPORT_MMX}
  789. end;
  790. end;
  791. end;
  792. {$ifdef extdebug}
  793. procedure tbinarynode._dowrite;
  794. begin
  795. inherited _dowrite;
  796. writeln(',');
  797. writenode(right);
  798. end;
  799. {$endif}
  800. {****************************************************************************
  801. TBINOPYNODE
  802. ****************************************************************************}
  803. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  804. begin
  805. inherited create(t,l,r);
  806. end;
  807. function tbinopnode.docompare(p : tnode) : boolean;
  808. begin
  809. docompare:=(inherited docompare(p)) or
  810. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  811. ((nf_swapable in flags) and
  812. left.isequal(tbinopnode(p).right) and
  813. right.isequal(tbinopnode(p).left));
  814. end;
  815. end.
  816. {
  817. $Log$
  818. Revision 1.47 2003-01-03 12:15:56 daniel
  819. * Removed ifdefs around notifications
  820. ifdefs around for loop optimizations remain
  821. Revision 1.46 2002/12/26 18:24:33 jonas
  822. * fixed check for whether or not a high parameter was already generated
  823. * no type checking/conversions for invisible parameters
  824. Revision 1.45 2002/11/28 11:17:04 florian
  825. * loop node flags from node flags splitted
  826. Revision 1.44 2002/10/05 00:48:57 peter
  827. * support inherited; support for overload as it is handled by
  828. delphi. This is only for delphi mode as it is working is
  829. undocumented and hard to predict what is done
  830. Revision 1.43 2002/09/07 15:25:03 peter
  831. * old logs removed and tabs fixed
  832. Revision 1.42 2002/09/03 16:26:26 daniel
  833. * Make Tprocdef.defs protected
  834. Revision 1.41 2002/09/01 13:28:38 daniel
  835. - write_access fields removed in favor of a flag
  836. Revision 1.40 2002/09/01 08:01:16 daniel
  837. * Removed sets from Tcallnode.det_resulttype
  838. + Added read/write notifications of variables. These will be usefull
  839. for providing information for several optimizations. For example
  840. the value of the loop variable of a for loop does matter is the
  841. variable is read after the for loop, but if it's no longer used
  842. or written, it doesn't matter and this can be used to optimize
  843. the loop code generation.
  844. Revision 1.39 2002/08/22 11:21:45 florian
  845. + register32 is now written by tnode.dowrite
  846. * fixed write of value of tconstnode
  847. Revision 1.38 2002/08/19 19:36:44 peter
  848. * More fixes for cross unit inlining, all tnodes are now implemented
  849. * Moved pocall_internconst to po_internconst because it is not a
  850. calling type at all and it conflicted when inlining of these small
  851. functions was requested
  852. Revision 1.37 2002/08/18 20:06:24 peter
  853. * inlining is now also allowed in interface
  854. * renamed write/load to ppuwrite/ppuload
  855. * tnode storing in ppu
  856. * nld,ncon,nbas are already updated for storing in ppu
  857. Revision 1.36 2002/08/17 22:09:46 florian
  858. * result type handling in tcgcal.pass_2 overhauled
  859. * better tnode.dowrite
  860. * some ppc stuff fixed
  861. Revision 1.35 2002/08/15 19:10:35 peter
  862. * first things tai,tnode storing in ppu
  863. Revision 1.34 2002/08/09 19:15:41 carl
  864. - removed newcg define
  865. Revision 1.33 2002/07/23 12:34:30 daniel
  866. * Readded old set code. To use it define 'oldset'. Activated by default
  867. for ppc.
  868. Revision 1.32 2002/07/22 11:48:04 daniel
  869. * Sets are now internally sets.
  870. Revision 1.31 2002/07/21 06:58:49 daniel
  871. * Changed booleans into flags
  872. Revision 1.30 2002/07/19 11:41:36 daniel
  873. * State tracker work
  874. * The whilen and repeatn are now completely unified into whilerepeatn. This
  875. allows the state tracker to change while nodes automatically into
  876. repeat nodes.
  877. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  878. 'not(a>b)' is optimized into 'a<=b'.
  879. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  880. by removing the notn and later switchting the true and falselabels. The
  881. same is done with 'repeat until not a'.
  882. Revision 1.29 2002/07/14 18:00:44 daniel
  883. + Added the beginning of a state tracker. This will track the values of
  884. variables through procedures and optimize things away.
  885. Revision 1.28 2002/07/01 18:46:24 peter
  886. * internal linker
  887. * reorganized aasm layer
  888. Revision 1.27 2002/05/18 13:34:10 peter
  889. * readded missing revisions
  890. Revision 1.26 2002/05/16 19:46:39 carl
  891. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  892. + try to fix temp allocation (still in ifdef)
  893. + generic constructor calls
  894. + start of tassembler / tmodulebase class cleanup
  895. Revision 1.24 2002/04/21 19:02:04 peter
  896. * removed newn and disposen nodes, the code is now directly
  897. inlined from pexpr
  898. * -an option that will write the secondpass nodes to the .s file, this
  899. requires EXTDEBUG define to actually write the info
  900. * fixed various internal errors and crashes due recent code changes
  901. Revision 1.23 2002/04/06 18:13:01 jonas
  902. * several powerpc-related additions and fixes
  903. Revision 1.22 2002/03/31 20:26:35 jonas
  904. + a_loadfpu_* and a_loadmm_* methods in tcg
  905. * register allocation is now handled by a class and is mostly processor
  906. independent (+rgobj.pas and i386/rgcpu.pas)
  907. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  908. * some small improvements and fixes to the optimizer
  909. * some register allocation fixes
  910. * some fpuvaroffset fixes in the unary minus node
  911. * push/popusedregisters is now called rg.save/restoreusedregisters and
  912. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  913. also better optimizable)
  914. * fixed and optimized register saving/restoring for new/dispose nodes
  915. * LOC_FPU locations now also require their "register" field to be set to
  916. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  917. - list field removed of the tnode class because it's not used currently
  918. and can cause hard-to-find bugs
  919. }