node.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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,cgbase,
  25. aasmbase,
  26. symtype;
  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. loadparentfpn { Load the framepointer of the parent for nested procedures }
  106. );
  107. const
  108. nodetype2str : array[tnodetype] of string[24] = (
  109. '<emptynode>',
  110. 'addn',
  111. 'muln',
  112. 'subn',
  113. 'divn',
  114. 'symdifn',
  115. 'modn',
  116. 'assignn',
  117. 'loadn',
  118. 'rangen',
  119. 'ltn',
  120. 'lten',
  121. 'gtn',
  122. 'gten',
  123. 'equaln',
  124. 'unequaln',
  125. 'inn',
  126. 'orn',
  127. 'xorn',
  128. 'shrn',
  129. 'shln',
  130. 'slashn',
  131. 'andn',
  132. 'subscriptn',
  133. 'derefn',
  134. 'addrn',
  135. 'ordconstn',
  136. 'typeconvn',
  137. 'calln',
  138. 'callparan',
  139. 'realconstn',
  140. 'unaryminusn',
  141. 'asmn',
  142. 'vecn',
  143. 'pointerconstn',
  144. 'stringconstn',
  145. 'notn',
  146. 'inlinen',
  147. 'niln',
  148. 'errorn',
  149. 'typen',
  150. 'setelementn',
  151. 'setconstn',
  152. 'blockn',
  153. 'statementn',
  154. 'ifn',
  155. 'breakn',
  156. 'continuen',
  157. 'whilerepeatn',
  158. 'forn',
  159. 'exitn',
  160. 'withn',
  161. 'casen',
  162. 'labeln',
  163. 'goton',
  164. 'tryexceptn',
  165. 'raisen',
  166. 'tryfinallyn',
  167. 'onn',
  168. 'isn',
  169. 'asn',
  170. 'caretn',
  171. 'starstarn',
  172. 'arrayconstructn',
  173. 'arrayconstructrangen',
  174. 'tempcreaten',
  175. 'temprefn',
  176. 'tempdeleten',
  177. 'addoptn',
  178. 'nothingn',
  179. 'loadvmtaddrn',
  180. 'guidconstn',
  181. 'rttin',
  182. 'loadparentfpn');
  183. type
  184. { all boolean field of ttree are now collected in flags }
  185. tnodeflag = (
  186. nf_swapable, { tbinop operands can be swaped }
  187. nf_swaped, { tbinop operands are swaped }
  188. nf_error,
  189. { general }
  190. nf_write, { Node is written to }
  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. nf_typedaddr,
  204. { tvecnode }
  205. nf_memindex,
  206. nf_memseg,
  207. nf_callunique,
  208. { tloadnode }
  209. nf_absolute,
  210. nf_load_self_pointer,
  211. { taddnode }
  212. nf_is_currency,
  213. { tassignmentnode }
  214. nf_concat_string,
  215. nf_use_strconcat,
  216. { tarrayconstructnode }
  217. nf_forcevaria,
  218. nf_novariaallowed,
  219. { ttypeconvnode }
  220. nf_explicit,
  221. { tinlinenode }
  222. nf_inlineconst
  223. );
  224. tnodeflags = set of tnodeflag;
  225. const
  226. { contains the flags which must be equal for the equality }
  227. { of nodes }
  228. flagsequal : tnodeflags = [nf_error];
  229. type
  230. tnodelist = class
  231. end;
  232. { later (for the newcg) tnode will inherit from tlinkedlist_item }
  233. tnode = class
  234. public
  235. { type of this node }
  236. nodetype : tnodetype;
  237. { type of the current code block, general/const/type }
  238. blocktype : tblock_type;
  239. { expected location of the result of this node (pass1) }
  240. expectloc : tcgloc;
  241. { the location of the result of this node (pass2) }
  242. location : tlocation;
  243. { the parent node of this is node }
  244. { this field is set by concattolist }
  245. parent : tnode;
  246. { there are some properties about the node stored }
  247. flags : tnodeflags;
  248. ppuidx : longint;
  249. { the number of registers needed to evalute the node }
  250. registers32,registersfpu,registersmm : longint; { must be longint !!!! }
  251. {$ifdef SUPPORT_MMX}
  252. registersmmx : longint;
  253. {$endif SUPPORT_MMX}
  254. resulttype : ttype;
  255. fileinfo : tfileposinfo;
  256. localswitches : tlocalswitches;
  257. {$ifdef extdebug}
  258. maxfirstpasscount,
  259. firstpasscount : longint;
  260. {$endif extdebug}
  261. constructor create(t:tnodetype);
  262. { this constructor is only for creating copies of class }
  263. { the fields are copied by getcopy }
  264. constructor createforcopy;
  265. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  266. destructor destroy;override;
  267. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  268. procedure buildderefimpl;virtual;
  269. procedure derefimpl;virtual;
  270. procedure derefnode;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 buildderefimpl;override;
  323. procedure derefimpl;override;
  324. procedure derefnode;override;
  325. procedure concattolist(l : tlinkedlist);override;
  326. function ischild(p : tnode) : boolean;override;
  327. function docompare(p : tnode) : boolean;override;
  328. function getcopy : tnode;override;
  329. procedure insertintolist(l : tnodelist);override;
  330. procedure left_max;
  331. procedure printnodedata(var t:text);override;
  332. end;
  333. pbinarynode = ^tbinarynode;
  334. tbinarynode = class(tunarynode)
  335. right : tnode;
  336. constructor create(t:tnodetype;l,r : tnode);
  337. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  338. destructor destroy;override;
  339. procedure ppuwrite(ppufile:tcompilerppufile);override;
  340. procedure buildderefimpl;override;
  341. procedure derefimpl;override;
  342. procedure derefnode;override;
  343. procedure concattolist(l : tlinkedlist);override;
  344. function ischild(p : tnode) : boolean;override;
  345. function docompare(p : tnode) : boolean;override;
  346. procedure swapleftright;
  347. function getcopy : tnode;override;
  348. procedure insertintolist(l : tnodelist);override;
  349. procedure left_right_max;
  350. procedure printnodedata(var t:text);override;
  351. procedure printnodelist(var t:text);
  352. end;
  353. tbinopnode = class(tbinarynode)
  354. constructor create(t:tnodetype;l,r : tnode);virtual;
  355. function docompare(p : tnode) : boolean;override;
  356. end;
  357. var
  358. { array with all class types for tnodes }
  359. nodeclass : tnodeclassarray;
  360. function nodeppuidxget(i:longint):tnode;
  361. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  362. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  363. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  364. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  365. const
  366. printnodespacing = ' ';
  367. var
  368. { indention used when writing the tree to the screen }
  369. printnodeindention : string;
  370. procedure printnodeindent;
  371. procedure printnodeunindent;
  372. procedure printnode(var t:text;n:tnode);
  373. implementation
  374. uses
  375. cutils,verbose,ppu;
  376. const
  377. ppunodemarker = 255;
  378. {****************************************************************************
  379. Helpers
  380. ****************************************************************************}
  381. var
  382. nodeppudata : tdynamicarray;
  383. nodeppuidx : longint;
  384. procedure nodeppuidxcreate;
  385. begin
  386. nodeppudata:=tdynamicarray.create(1024);
  387. nodeppuidx:=0;
  388. end;
  389. procedure nodeppuidxfree;
  390. begin
  391. nodeppudata.free;
  392. nodeppudata:=nil;
  393. end;
  394. procedure nodeppuidxadd(n:tnode);
  395. begin
  396. if n.ppuidx<0 then
  397. internalerror(200311072);
  398. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  399. nodeppudata.write(n,sizeof(pointer));
  400. end;
  401. function nodeppuidxget(i:longint):tnode;
  402. var
  403. l : longint;
  404. begin
  405. if i<0 then
  406. internalerror(200311072);
  407. nodeppudata.seek(i*sizeof(pointer));
  408. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  409. internalerror(200311073);
  410. end;
  411. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  412. var
  413. b : byte;
  414. t : tnodetype;
  415. hppuidx : longint;
  416. begin
  417. { marker }
  418. b:=ppufile.getbyte;
  419. if b<>ppunodemarker then
  420. internalerror(200208151);
  421. { load nodetype }
  422. t:=tnodetype(ppufile.getbyte);
  423. if t>high(tnodetype) then
  424. internalerror(200208152);
  425. if t<>emptynode then
  426. begin
  427. if not assigned(nodeclass[t]) then
  428. internalerror(200208153);
  429. hppuidx:=ppufile.getlongint;
  430. //writeln('load: ',nodetype2str[t]);
  431. { generate node of the correct class }
  432. result:=nodeclass[t].ppuload(t,ppufile);
  433. result.ppuidx:=hppuidx;
  434. nodeppuidxadd(result);
  435. end
  436. else
  437. result:=nil;
  438. end;
  439. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  440. begin
  441. { marker, read by ppuloadnode }
  442. ppufile.putbyte(ppunodemarker);
  443. { type, read by ppuloadnode }
  444. if assigned(n) then
  445. begin
  446. if n.ppuidx=-1 then
  447. internalerror(200311071);
  448. n.ppuidx:=nodeppuidx;
  449. inc(nodeppuidx);
  450. ppufile.putbyte(byte(n.nodetype));
  451. ppufile.putlongint(n.ppuidx);
  452. //writeln('write: ',nodetype2str[n.nodetype]);
  453. n.ppuwrite(ppufile);
  454. end
  455. else
  456. ppufile.putbyte(byte(emptynode));
  457. end;
  458. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  459. begin
  460. if ppufile.readentry<>ibnodetree then
  461. Message(unit_f_ppu_read_error);
  462. nodeppuidxcreate;
  463. result:=ppuloadnode(ppufile);
  464. result.derefnode;
  465. nodeppuidxfree;
  466. end;
  467. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  468. begin
  469. nodeppuidx:=0;
  470. ppuwritenode(ppufile,n);
  471. ppufile.writeentry(ibnodetree);
  472. end;
  473. procedure printnodeindent;
  474. begin
  475. printnodeindention:=printnodeindention+printnodespacing;
  476. end;
  477. procedure printnodeunindent;
  478. begin
  479. delete(printnodeindention,1,length(printnodespacing));
  480. end;
  481. procedure printnode(var t:text;n:tnode);
  482. begin
  483. if assigned(n) then
  484. n.printnodetree(t)
  485. else
  486. writeln(t,printnodeindention,'nil');
  487. end;
  488. {****************************************************************************
  489. TNODE
  490. ****************************************************************************}
  491. constructor tnode.create(t:tnodetype);
  492. begin
  493. inherited create;
  494. nodetype:=t;
  495. blocktype:=block_type;
  496. { updated by firstpass }
  497. expectloc:=LOC_INVALID;
  498. { updated by secondpass }
  499. location.loc:=LOC_INVALID;
  500. { save local info }
  501. fileinfo:=aktfilepos;
  502. localswitches:=aktlocalswitches;
  503. resulttype.reset;
  504. registers32:=0;
  505. registersfpu:=0;
  506. {$ifdef SUPPORT_MMX}
  507. registersmmx:=0;
  508. {$endif SUPPORT_MMX}
  509. {$ifdef EXTDEBUG}
  510. maxfirstpasscount:=0;
  511. firstpasscount:=0;
  512. {$endif EXTDEBUG}
  513. flags:=[];
  514. ppuidx:=-1;
  515. end;
  516. constructor tnode.createforcopy;
  517. begin
  518. end;
  519. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  520. begin
  521. nodetype:=t;
  522. { tnode fields }
  523. blocktype:=tblock_type(ppufile.getbyte);
  524. ppufile.getposinfo(fileinfo);
  525. ppufile.getsmallset(localswitches);
  526. ppufile.gettype(resulttype);
  527. ppufile.getsmallset(flags);
  528. { updated by firstpass }
  529. expectloc:=LOC_INVALID;
  530. { updated by secondpass }
  531. location.loc:=LOC_INVALID;
  532. registers32:=0;
  533. registersfpu:=0;
  534. {$ifdef SUPPORT_MMX}
  535. registersmmx:=0;
  536. {$endif SUPPORT_MMX}
  537. {$ifdef EXTDEBUG}
  538. maxfirstpasscount:=0;
  539. firstpasscount:=0;
  540. {$endif EXTDEBUG}
  541. ppuidx:=-1;
  542. end;
  543. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  544. begin
  545. ppufile.putbyte(byte(block_type));
  546. ppufile.putposinfo(fileinfo);
  547. ppufile.putsmallset(localswitches);
  548. ppufile.puttype(resulttype);
  549. ppufile.putsmallset(flags);
  550. end;
  551. procedure tnode.buildderefimpl;
  552. begin
  553. resulttype.buildderef;
  554. end;
  555. procedure tnode.derefimpl;
  556. begin
  557. resulttype.resolve;
  558. end;
  559. procedure tnode.derefnode;
  560. begin
  561. end;
  562. procedure tnode.toggleflag(f : tnodeflag);
  563. begin
  564. if f in flags then
  565. exclude(flags,f)
  566. else
  567. include(flags,f);
  568. end;
  569. destructor tnode.destroy;
  570. begin
  571. {$ifdef EXTDEBUG}
  572. if firstpasscount>maxfirstpasscount then
  573. maxfirstpasscount:=firstpasscount;
  574. {$endif EXTDEBUG}
  575. end;
  576. procedure tnode.concattolist(l : tlinkedlist);
  577. begin
  578. end;
  579. function tnode.ischild(p : tnode) : boolean;
  580. begin
  581. ischild:=false;
  582. end;
  583. procedure tnode.mark_write;
  584. begin
  585. {$ifdef EXTDEBUG}
  586. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  587. {$endif EXTDEBUG}
  588. end;
  589. procedure tnode.printnodeinfo(var t:text);
  590. begin
  591. write(t,nodetype2str[nodetype]);
  592. if assigned(resulttype.def) then
  593. write(t,', resulttype = "',resulttype.def.gettypename,'"')
  594. else
  595. write(t,', resulttype = <nil>');
  596. writeln(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  597. ', loc = ',tcgloc2str[location.loc],
  598. ', expectloc = ',tcgloc2str[expectloc],
  599. ', intregs = ',registers32,
  600. ', fpuregs = ',registersfpu);
  601. end;
  602. procedure tnode.printnodedata(var t:text);
  603. begin
  604. end;
  605. procedure tnode.printnodetree(var t:text);
  606. begin
  607. write(t,printnodeindention,'(');
  608. printnodeinfo(t);
  609. printnodeindent;
  610. printnodedata(t);
  611. printnodeunindent;
  612. writeln(t,printnodeindention,')');
  613. end;
  614. function tnode.isequal(p : tnode) : boolean;
  615. begin
  616. isequal:=
  617. (not assigned(self) and not assigned(p)) or
  618. (assigned(self) and assigned(p) and
  619. { optimized subclasses have the same nodetype as their }
  620. { superclass (for compatibility), so also check the classtype (JM) }
  621. (p.classtype=classtype) and
  622. (p.nodetype=nodetype) and
  623. (flags*flagsequal=p.flags*flagsequal) and
  624. docompare(p));
  625. end;
  626. {$ifdef state_tracking}
  627. function Tnode.track_state_pass(exec_known:boolean):boolean;
  628. begin
  629. track_state_pass:=false;
  630. end;
  631. {$endif state_tracking}
  632. function tnode.docompare(p : tnode) : boolean;
  633. begin
  634. docompare:=true;
  635. end;
  636. function tnode.getcopy : tnode;
  637. var
  638. p : tnode;
  639. begin
  640. { this is quite tricky because we need a node of the current }
  641. { node type and not one of tnode! }
  642. p:=tnodeclass(classtype).createforcopy;
  643. p.nodetype:=nodetype;
  644. p.location:=location;
  645. p.parent:=parent;
  646. p.flags:=flags;
  647. p.registers32:=registers32;
  648. p.registersfpu:=registersfpu;
  649. {$ifdef SUPPORT_MMX}
  650. p.registersmmx:=registersmmx;
  651. p.registerskni:=registerskni;
  652. {$endif SUPPORT_MMX}
  653. p.resulttype:=resulttype;
  654. p.fileinfo:=fileinfo;
  655. p.localswitches:=localswitches;
  656. {$ifdef extdebug}
  657. p.firstpasscount:=firstpasscount;
  658. {$endif extdebug}
  659. { p.list:=list; }
  660. getcopy:=p;
  661. end;
  662. procedure tnode.insertintolist(l : tnodelist);
  663. begin
  664. end;
  665. procedure tnode.set_file_line(from : tnode);
  666. begin
  667. if assigned(from) then
  668. fileinfo:=from.fileinfo;
  669. end;
  670. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  671. begin
  672. fileinfo:=filepos;
  673. end;
  674. {****************************************************************************
  675. TUNARYNODE
  676. ****************************************************************************}
  677. constructor tunarynode.create(t:tnodetype;l : tnode);
  678. begin
  679. inherited create(t);
  680. left:=l;
  681. end;
  682. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  683. begin
  684. inherited ppuload(t,ppufile);
  685. left:=ppuloadnode(ppufile);
  686. end;
  687. destructor tunarynode.destroy;
  688. begin
  689. left.free;
  690. inherited destroy;
  691. end;
  692. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  693. begin
  694. inherited ppuwrite(ppufile);
  695. ppuwritenode(ppufile,left);
  696. end;
  697. procedure tunarynode.buildderefimpl;
  698. begin
  699. inherited buildderefimpl;
  700. if assigned(left) then
  701. left.buildderefimpl;
  702. end;
  703. procedure tunarynode.derefimpl;
  704. begin
  705. inherited derefimpl;
  706. if assigned(left) then
  707. left.derefimpl;
  708. end;
  709. procedure tunarynode.derefnode;
  710. begin
  711. inherited derefnode;
  712. if assigned(left) then
  713. left.derefnode;
  714. end;
  715. function tunarynode.docompare(p : tnode) : boolean;
  716. begin
  717. docompare:=(inherited docompare(p) and
  718. ((left=nil) or left.isequal(tunarynode(p).left))
  719. );
  720. end;
  721. function tunarynode.getcopy : tnode;
  722. var
  723. p : tunarynode;
  724. begin
  725. p:=tunarynode(inherited getcopy);
  726. if assigned(left) then
  727. p.left:=left.getcopy
  728. else
  729. p.left:=nil;
  730. getcopy:=p;
  731. end;
  732. procedure tunarynode.insertintolist(l : tnodelist);
  733. begin
  734. end;
  735. procedure tunarynode.printnodedata(var t:text);
  736. begin
  737. inherited printnodedata(t);
  738. printnode(t,left);
  739. end;
  740. procedure tunarynode.left_max;
  741. begin
  742. registers32:=left.registers32;
  743. registersfpu:=left.registersfpu;
  744. {$ifdef SUPPORT_MMX}
  745. registersmmx:=left.registersmmx;
  746. {$endif SUPPORT_MMX}
  747. end;
  748. procedure tunarynode.concattolist(l : tlinkedlist);
  749. begin
  750. left.parent:=self;
  751. left.concattolist(l);
  752. inherited concattolist(l);
  753. end;
  754. function tunarynode.ischild(p : tnode) : boolean;
  755. begin
  756. ischild:=p=left;
  757. end;
  758. {****************************************************************************
  759. TBINARYNODE
  760. ****************************************************************************}
  761. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  762. begin
  763. inherited create(t,l);
  764. right:=r
  765. end;
  766. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  767. begin
  768. inherited ppuload(t,ppufile);
  769. right:=ppuloadnode(ppufile);
  770. end;
  771. destructor tbinarynode.destroy;
  772. begin
  773. right.free;
  774. inherited destroy;
  775. end;
  776. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  777. begin
  778. inherited ppuwrite(ppufile);
  779. ppuwritenode(ppufile,right);
  780. end;
  781. procedure tbinarynode.buildderefimpl;
  782. begin
  783. inherited buildderefimpl;
  784. if assigned(right) then
  785. right.buildderefimpl;
  786. end;
  787. procedure tbinarynode.derefimpl;
  788. begin
  789. inherited derefimpl;
  790. if assigned(right) then
  791. right.derefimpl;
  792. end;
  793. procedure tbinarynode.derefnode;
  794. begin
  795. inherited derefnode;
  796. if assigned(right) then
  797. right.derefnode;
  798. end;
  799. procedure tbinarynode.concattolist(l : tlinkedlist);
  800. begin
  801. { we could change that depending on the number of }
  802. { required registers }
  803. left.parent:=self;
  804. left.concattolist(l);
  805. left.parent:=self;
  806. left.concattolist(l);
  807. inherited concattolist(l);
  808. end;
  809. function tbinarynode.ischild(p : tnode) : boolean;
  810. begin
  811. ischild:=(p=right);
  812. end;
  813. function tbinarynode.docompare(p : tnode) : boolean;
  814. begin
  815. docompare:=(inherited docompare(p) and
  816. ((right=nil) or right.isequal(tbinarynode(p).right))
  817. );
  818. end;
  819. function tbinarynode.getcopy : tnode;
  820. var
  821. p : tbinarynode;
  822. begin
  823. p:=tbinarynode(inherited getcopy);
  824. if assigned(right) then
  825. p.right:=right.getcopy
  826. else
  827. p.right:=nil;
  828. getcopy:=p;
  829. end;
  830. procedure tbinarynode.insertintolist(l : tnodelist);
  831. begin
  832. end;
  833. procedure tbinarynode.swapleftright;
  834. var
  835. swapp : tnode;
  836. begin
  837. swapp:=right;
  838. right:=left;
  839. left:=swapp;
  840. if nf_swaped in flags then
  841. exclude(flags,nf_swaped)
  842. else
  843. include(flags,nf_swaped);
  844. end;
  845. procedure tbinarynode.left_right_max;
  846. begin
  847. if assigned(left) then
  848. begin
  849. if assigned(right) then
  850. begin
  851. registers32:=max(left.registers32,right.registers32);
  852. registersfpu:=max(left.registersfpu,right.registersfpu);
  853. {$ifdef SUPPORT_MMX}
  854. registersmmx:=max(left.registersmmx,right.registersmmx);
  855. {$endif SUPPORT_MMX}
  856. end
  857. else
  858. begin
  859. registers32:=left.registers32;
  860. registersfpu:=left.registersfpu;
  861. {$ifdef SUPPORT_MMX}
  862. registersmmx:=left.registersmmx;
  863. {$endif SUPPORT_MMX}
  864. end;
  865. end;
  866. end;
  867. procedure tbinarynode.printnodedata(var t:text);
  868. begin
  869. inherited printnodedata(t);
  870. printnode(t,right);
  871. end;
  872. procedure tbinarynode.printnodelist(var t:text);
  873. var
  874. hp : tbinarynode;
  875. begin
  876. hp:=self;
  877. while assigned(hp) do
  878. begin
  879. write(t,printnodeindention,'(');
  880. printnodeindent;
  881. hp.printnodeinfo(t);
  882. printnode(t,hp.left);
  883. printnodeunindent;
  884. writeln(t,printnodeindention,')');
  885. hp:=tbinarynode(hp.right);
  886. end;
  887. end;
  888. {****************************************************************************
  889. TBINOPYNODE
  890. ****************************************************************************}
  891. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  892. begin
  893. inherited create(t,l,r);
  894. end;
  895. function tbinopnode.docompare(p : tnode) : boolean;
  896. begin
  897. docompare:=(inherited docompare(p)) or
  898. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  899. ((nf_swapable in flags) and
  900. left.isequal(tbinopnode(p).right) and
  901. right.isequal(tbinopnode(p).left));
  902. end;
  903. end.
  904. {
  905. $Log$
  906. Revision 1.80 2004-01-26 16:12:28 daniel
  907. * reginfo now also only allocated during register allocation
  908. * third round of gdb cleanups: kick out most of concatstabto
  909. Revision 1.79 2003/12/26 00:32:22 florian
  910. + fpu<->mm register conversion
  911. Revision 1.78 2003/12/01 18:44:15 peter
  912. * fixed some crashes
  913. * fixed varargs and register calling probs
  914. Revision 1.77 2003/11/29 14:33:13 peter
  915. * typed address only used for @ and addr() that are parsed
  916. Revision 1.76 2003/11/23 17:38:48 peter
  917. * mark nodes that are copies
  918. Revision 1.75 2003/11/12 15:48:27 peter
  919. * fix set_varstate in for loops
  920. * fix set_varstate from case statements
  921. Revision 1.74 2003/11/10 22:02:52 peter
  922. * cross unit inlining fixed
  923. Revision 1.73 2003/10/23 14:44:07 peter
  924. * splitted buildderef and buildderefimpl to fix interface crc
  925. calculation
  926. Revision 1.72 2003/10/22 20:40:00 peter
  927. * write derefdata in a separate ppu entry
  928. Revision 1.71 2003/10/18 15:41:26 peter
  929. * made worklists dynamic in size
  930. Revision 1.70 2003/10/17 01:22:08 florian
  931. * compilation of the powerpc compiler fixed
  932. Revision 1.69 2003/10/08 19:19:45 peter
  933. * set_varstate cleanup
  934. Revision 1.68 2003/10/01 20:34:49 peter
  935. * procinfo unit contains tprocinfo
  936. * cginfo renamed to cgbase
  937. * moved cgmessage to verbose
  938. * fixed ppc and sparc compiles
  939. Revision 1.67 2003/09/28 17:55:04 peter
  940. * parent framepointer changed to hidden parameter
  941. * tloadparentfpnode added
  942. Revision 1.66 2003/09/06 22:27:08 florian
  943. * fixed web bug 2669
  944. * cosmetic fix in printnode
  945. * tobjectdef.gettypename implemented
  946. Revision 1.65 2003/09/03 15:55:01 peter
  947. * NEWRA branch merged
  948. Revision 1.64 2003/09/03 11:18:37 florian
  949. * fixed arm concatcopy
  950. + arm support in the common compiler sources added
  951. * moved some generic cg code around
  952. + tfputype added
  953. * ...
  954. Revision 1.63 2003/08/10 17:25:23 peter
  955. * fixed some reported bugs
  956. Revision 1.62 2003/05/26 21:17:17 peter
  957. * procinlinenode removed
  958. * aktexit2label removed, fast exit removed
  959. + tcallnode.inlined_pass_2 added
  960. Revision 1.61 2003/05/13 19:14:41 peter
  961. * failn removed
  962. * inherited result code check moven to pexpr
  963. Revision 1.60 2003/05/11 21:37:03 peter
  964. * moved implicit exception frame from ncgutil to psub
  965. * constructor/destructor helpers moved from cobj/ncgutil to psub
  966. Revision 1.59 2003/05/09 17:47:02 peter
  967. * self moved to hidden parameter
  968. * removed hdisposen,hnewn,selfn
  969. Revision 1.58 2003/04/25 20:59:33 peter
  970. * removed funcretn,funcretsym, function result is now in varsym
  971. and aliases for result and function name are added using absolutesym
  972. * vs_hidden parameter for funcret passed in parameter
  973. * vs_hidden fixes
  974. * writenode changed to printnode and released from extdebug
  975. * -vp option added to generate a tree.log with the nodetree
  976. * nicer printnode for statements, callnode
  977. Revision 1.57 2002/04/25 20:15:39 florian
  978. * block nodes within expressions shouldn't release the used registers,
  979. fixed using a flag till the new rg is ready
  980. Revision 1.56 2003/04/24 22:29:58 florian
  981. * fixed a lot of PowerPC related stuff
  982. Revision 1.55 2003/04/23 10:12:14 peter
  983. * allow multi pass2 changed to global boolean instead of node flag
  984. Revision 1.54 2003/04/22 23:50:23 peter
  985. * firstpass uses expectloc
  986. * checks if there are differences between the expectloc and
  987. location.loc from secondpass in EXTDEBUG
  988. Revision 1.53 2003/04/22 09:52:00 peter
  989. * mark_write implemented for default with a warning in EXTDEBUG, this
  990. is required for error recovery where the left node can be also a non
  991. writable node
  992. Revision 1.52 2003/04/10 17:57:52 peter
  993. * vs_hidden released
  994. Revision 1.51 2003/03/28 19:16:56 peter
  995. * generic constructor working for i386
  996. * remove fixed self register
  997. * esi added as address register for i386
  998. Revision 1.50 2003/03/17 16:54:41 peter
  999. * support DefaultHandler and anonymous inheritance fixed
  1000. for message methods
  1001. Revision 1.49 2003/01/04 15:54:03 daniel
  1002. * Fixed mark_write for @ operator
  1003. (can happen when compiling @procvar:=nil (Delphi mode construction))
  1004. Revision 1.48 2003/01/03 21:03:02 peter
  1005. * made mark_write dummy instead of abstract
  1006. Revision 1.47 2003/01/03 12:15:56 daniel
  1007. * Removed ifdefs around notifications
  1008. ifdefs around for loop optimizations remain
  1009. Revision 1.46 2002/12/26 18:24:33 jonas
  1010. * fixed check for whether or not a high parameter was already generated
  1011. * no type checking/conversions for invisible parameters
  1012. Revision 1.45 2002/11/28 11:17:04 florian
  1013. * loop node flags from node flags splitted
  1014. Revision 1.44 2002/10/05 00:48:57 peter
  1015. * support inherited; support for overload as it is handled by
  1016. delphi. This is only for delphi mode as it is working is
  1017. undocumented and hard to predict what is done
  1018. Revision 1.43 2002/09/07 15:25:03 peter
  1019. * old logs removed and tabs fixed
  1020. Revision 1.42 2002/09/03 16:26:26 daniel
  1021. * Make Tprocdef.defs protected
  1022. Revision 1.41 2002/09/01 13:28:38 daniel
  1023. - write_access fields removed in favor of a flag
  1024. Revision 1.40 2002/09/01 08:01:16 daniel
  1025. * Removed sets from Tcallnode.det_resulttype
  1026. + Added read/write notifications of variables. These will be usefull
  1027. for providing information for several optimizations. For example
  1028. the value of the loop variable of a for loop does matter is the
  1029. variable is read after the for loop, but if it's no longer used
  1030. or written, it doesn't matter and this can be used to optimize
  1031. the loop code generation.
  1032. Revision 1.39 2002/08/22 11:21:45 florian
  1033. + register32 is now written by tnode.dowrite
  1034. * fixed write of value of tconstnode
  1035. Revision 1.38 2002/08/19 19:36:44 peter
  1036. * More fixes for cross unit inlining, all tnodes are now implemented
  1037. * Moved pocall_internconst to po_internconst because it is not a
  1038. calling type at all and it conflicted when inlining of these small
  1039. functions was requested
  1040. Revision 1.37 2002/08/18 20:06:24 peter
  1041. * inlining is now also allowed in interface
  1042. * renamed write/load to ppuwrite/ppuload
  1043. * tnode storing in ppu
  1044. * nld,ncon,nbas are already updated for storing in ppu
  1045. Revision 1.36 2002/08/17 22:09:46 florian
  1046. * result type handling in tcgcal.pass_2 overhauled
  1047. * better tnode.dowrite
  1048. * some ppc stuff fixed
  1049. Revision 1.35 2002/08/15 19:10:35 peter
  1050. * first things tai,tnode storing in ppu
  1051. Revision 1.34 2002/08/09 19:15:41 carl
  1052. - removed newcg define
  1053. Revision 1.33 2002/07/23 12:34:30 daniel
  1054. * Readded old set code. To use it define 'oldset'. Activated by default
  1055. for ppc.
  1056. Revision 1.32 2002/07/22 11:48:04 daniel
  1057. * Sets are now internally sets.
  1058. Revision 1.31 2002/07/21 06:58:49 daniel
  1059. * Changed booleans into flags
  1060. Revision 1.30 2002/07/19 11:41:36 daniel
  1061. * State tracker work
  1062. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1063. allows the state tracker to change while nodes automatically into
  1064. repeat nodes.
  1065. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1066. 'not(a>b)' is optimized into 'a<=b'.
  1067. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1068. by removing the notn and later switchting the true and falselabels. The
  1069. same is done with 'repeat until not a'.
  1070. Revision 1.29 2002/07/14 18:00:44 daniel
  1071. + Added the beginning of a state tracker. This will track the values of
  1072. variables through procedures and optimize things away.
  1073. Revision 1.28 2002/07/01 18:46:24 peter
  1074. * internal linker
  1075. * reorganized aasm layer
  1076. Revision 1.27 2002/05/18 13:34:10 peter
  1077. * readded missing revisions
  1078. Revision 1.26 2002/05/16 19:46:39 carl
  1079. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1080. + try to fix temp allocation (still in ifdef)
  1081. + generic constructor calls
  1082. + start of tassembler / tmodulebase class cleanup
  1083. Revision 1.24 2002/04/21 19:02:04 peter
  1084. * removed newn and disposen nodes, the code is now directly
  1085. inlined from pexpr
  1086. * -an option that will write the secondpass nodes to the .s file, this
  1087. requires EXTDEBUG define to actually write the info
  1088. * fixed various internal errors and crashes due recent code changes
  1089. Revision 1.23 2002/04/06 18:13:01 jonas
  1090. * several powerpc-related additions and fixes
  1091. Revision 1.22 2002/03/31 20:26:35 jonas
  1092. + a_loadfpu_* and a_loadmm_* methods in tcg
  1093. * register allocation is now handled by a class and is mostly processor
  1094. independent (+rgobj.pas and i386/rgcpu.pas)
  1095. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1096. * some small improvements and fixes to the optimizer
  1097. * some register allocation fixes
  1098. * some fpuvaroffset fixes in the unary minus node
  1099. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1100. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1101. also better optimizable)
  1102. * fixed and optimized register saving/restoring for new/dispose nodes
  1103. * LOC_FPU locations now also require their "register" field to be set to
  1104. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1105. - list field removed of the tnode class because it's not used currently
  1106. and can cause hard-to-find bugs
  1107. }