2
0

node.pas 34 KB

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