node.pas 36 KB

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