node.pas 36 KB

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