node.pas 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Basic node handling
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit node;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,
  23. cpubase,cgbase,cgutils,
  24. aasmbase,
  25. symtype,
  26. optbase;
  27. type
  28. tnodetype = (
  29. emptynode, {No node (returns nil when loading from ppu)}
  30. addn, {Represents the + operator}
  31. muln, {Represents the * operator}
  32. subn, {Represents the - operator}
  33. divn, {Represents the div operator}
  34. symdifn, {Represents the >< operator}
  35. modn, {Represents the mod operator}
  36. assignn, {Represents an assignment}
  37. loadn, {Represents the use of a variabele}
  38. rangen, {Represents a range (i.e. 0..9)}
  39. ltn, {Represents the < operator}
  40. lten, {Represents the <= operator}
  41. gtn, {Represents the > operator}
  42. gten, {Represents the >= operator}
  43. equaln, {Represents the = operator}
  44. unequaln, {Represents the <> operator}
  45. inn, {Represents the in operator}
  46. orn, {Represents the or operator}
  47. xorn, {Represents the xor operator}
  48. shrn, {Represents the shr operator}
  49. shln, {Represents the shl operator}
  50. slashn, {Represents the / operator}
  51. andn, {Represents the and operator}
  52. subscriptn, {Field in a record/object}
  53. derefn, {Dereferences a pointer}
  54. addrn, {Represents the @ operator}
  55. ordconstn, {Represents an ordinal value}
  56. typeconvn, {Represents type-conversion/typecast}
  57. calln, {Represents a call node}
  58. callparan, {Represents a parameter}
  59. realconstn, {Represents a real value}
  60. unaryminusn, {Represents a sign change (i.e. -2)}
  61. asmn, {Represents an assembler node }
  62. vecn, {Represents array indexing}
  63. pointerconstn, {Represents a pointer constant}
  64. stringconstn, {Represents a string constant}
  65. notn, {Represents the not operator}
  66. inlinen, {Internal procedures (i.e. writeln)}
  67. niln, {Represents the nil pointer}
  68. errorn, {This part of the tree could not be
  69. parsed because of a compiler error}
  70. typen, {A type name. Used for i.e. typeof(obj)}
  71. setelementn, {A set element(s) (i.e. [a,b] and also [a..b])}
  72. setconstn, {A set constant (i.e. [1,2])}
  73. blockn, {A block of statements}
  74. statementn, {One statement in a block of nodes}
  75. ifn, {An if statement}
  76. breakn, {A break statement}
  77. continuen, {A continue statement}
  78. whilerepeatn, {A while or repeat statement}
  79. forn, {A for loop}
  80. exitn, {An exit statement}
  81. withn, {A with statement}
  82. casen, {A case statement}
  83. labeln, {A label}
  84. goton, {A goto statement}
  85. tryexceptn, {A try except block}
  86. raisen, {A raise statement}
  87. tryfinallyn, {A try finally statement}
  88. onn, {For an on statement in exception code}
  89. isn, {Represents the is operator}
  90. asn, {Represents the as typecast}
  91. starstarn, {Represents the ** operator exponentiation }
  92. arrayconstructorn, {Construction node for [...] parsing}
  93. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  94. tempcreaten, { for temps in the result/firstpass }
  95. temprefn, { references to temps }
  96. tempdeleten, { for temps in the result/firstpass }
  97. addoptn, { added for optimizations where we cannot suppress }
  98. nothingn, { NOP, Do nothing}
  99. loadvmtaddrn, { Load the address of the VMT of a class/object}
  100. guidconstn, { A GUID COM Interface constant }
  101. rttin, { Rtti information so they can be accessed in result/firstpass}
  102. loadparentfpn, { Load the framepointer of the parent for nested procedures }
  103. dataconstn { node storing some binary data }
  104. );
  105. tnodetypeset = set of tnodetype;
  106. pnodetypeset = ^tnodetypeset;
  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. 'starstarn',
  171. 'arrayconstructn',
  172. 'arrayconstructrangen',
  173. 'tempcreaten',
  174. 'temprefn',
  175. 'tempdeleten',
  176. 'addoptn',
  177. 'nothingn',
  178. 'loadvmtaddrn',
  179. 'guidconstn',
  180. 'rttin',
  181. 'loadparentfpn',
  182. 'dataconstn');
  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_swapped, { tbinop operands are swaped }
  188. nf_error,
  189. { general }
  190. nf_pass1_done,
  191. nf_write, { Node is written to }
  192. nf_is_funcret,
  193. nf_isproperty,
  194. nf_processing,
  195. { taddrnode }
  196. nf_typedaddr,
  197. { tderefnode }
  198. nf_no_checkpointer,
  199. { tvecnode }
  200. nf_memindex,
  201. nf_memseg,
  202. nf_callunique,
  203. { tloadnode }
  204. nf_absolute,
  205. nf_is_self,
  206. nf_load_self_pointer,
  207. nf_inherited,
  208. { the loadnode is generated internally and a varspez=vs_const should be ignore,
  209. this requires that the parameter is actually passed by value
  210. Be really carefull when using this flag! }
  211. nf_isinternal_ignoreconst,
  212. { taddnode }
  213. nf_is_currency,
  214. nf_has_pointerdiv,
  215. nf_short_bool,
  216. { tassignmentnode }
  217. nf_assign_done_in_right,
  218. { tarrayconstructnode }
  219. nf_forcevaria,
  220. nf_novariaallowed,
  221. { ttypeconvnode, and the first one also treal/ord/pointerconstn }
  222. nf_explicit,
  223. nf_internal, { no warnings/hints generated }
  224. nf_load_procvar,
  225. { tinlinenode }
  226. nf_inlineconst,
  227. { tasmnode }
  228. nf_get_asm_position,
  229. { tblocknode }
  230. nf_block_with_exit
  231. );
  232. tnodeflags = set of tnodeflag;
  233. const
  234. { contains the flags which must be equal for the equality }
  235. { of nodes }
  236. flagsequal : tnodeflags = [nf_error];
  237. type
  238. tnodelist = class
  239. end;
  240. pnode = ^tnode;
  241. { basic class for the intermediated representation fpc uses }
  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. { next node in control flow on the same block level, i.e.
  256. for loop nodes, this is the next node after the end of the loop,
  257. same for if and case, if this field is nil, the next node is the procedure exit,
  258. for the last node in a loop this is set to the loop header
  259. this field is set only for control flow nodes }
  260. successor : tnode;
  261. { there are some properties about the node stored }
  262. flags : tnodeflags;
  263. ppuidx : longint;
  264. { the number of registers needed to evalute the node }
  265. registersint,registersfpu,registersmm : longint; { must be longint !!!! }
  266. {$ifdef SUPPORT_MMX}
  267. registersmmx : longint;
  268. {$endif SUPPORT_MMX}
  269. resultdef : tdef;
  270. resultdefderef : tderef;
  271. fileinfo : tfileposinfo;
  272. localswitches : tlocalswitches;
  273. optinfo : poptinfo;
  274. {$ifdef extdebug}
  275. maxfirstpasscount,
  276. firstpasscount : longint;
  277. {$endif extdebug}
  278. constructor create(t:tnodetype);
  279. { this constructor is only for creating copies of class }
  280. { the fields are copied by getcopy }
  281. constructor createforcopy;
  282. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  283. destructor destroy;override;
  284. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  285. procedure buildderefimpl;virtual;
  286. procedure derefimpl;virtual;
  287. procedure derefnode;virtual;
  288. { toggles the flag }
  289. procedure toggleflag(f : tnodeflag);
  290. { the 1.1 code generator may override pass_1 }
  291. { and it need not to implement det_* then }
  292. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  293. { 2.0: runs pass_typecheck and det_temp }
  294. function pass_1 : tnode;virtual;abstract;
  295. { dermines the resultdef of the node }
  296. function pass_typecheck : tnode;virtual;abstract;
  297. { tries to simplify the node, returns a value <>nil if a simplified
  298. node has been created }
  299. function simplify : tnode;virtual;
  300. {$ifdef state_tracking}
  301. { Does optimizations by keeping track of the variable states
  302. in a procedure }
  303. function track_state_pass(exec_known:boolean):boolean;virtual;
  304. {$endif}
  305. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  306. written to (normally the loadnode) as write access. }
  307. procedure mark_write;virtual;
  308. { dermines the number of necessary temp. locations to evaluate
  309. the node }
  310. procedure det_temp;virtual;abstract;
  311. procedure pass_generate_code;virtual;abstract;
  312. { comparing of nodes }
  313. function isequal(p : tnode) : boolean;
  314. { to implement comparisation, override this method }
  315. function docompare(p : tnode) : boolean;virtual;
  316. { wrapper for getcopy }
  317. function getcopy : tnode;
  318. { does the real copying of a node }
  319. function dogetcopy : tnode;virtual;
  320. procedure insertintolist(l : tnodelist);virtual;
  321. { writes a node for debugging purpose, shouldn't be called }
  322. { direct, because there is no test for nil, use printnode }
  323. { to write a complete tree }
  324. procedure printnodeinfo(var t:text);virtual;
  325. procedure printnodedata(var t:text);virtual;
  326. procedure printnodetree(var t:text);virtual;
  327. procedure concattolist(l : tlinkedlist);virtual;
  328. function ischild(p : tnode) : boolean;virtual;
  329. { ensures that the optimizer info record is allocated }
  330. function allocoptinfo : poptinfo;inline;
  331. end;
  332. tnodeclass = class of tnode;
  333. tnodeclassarray = array[tnodetype] of tnodeclass;
  334. { this node is the anchestor for all nodes with at least }
  335. { one child, you have to use it if you want to use }
  336. { true- and current_procinfo.CurrFalseLabel }
  337. punarynode = ^tunarynode;
  338. tunarynode = class(tnode)
  339. left : tnode;
  340. constructor create(t:tnodetype;l : tnode);
  341. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  342. destructor destroy;override;
  343. procedure ppuwrite(ppufile:tcompilerppufile);override;
  344. procedure buildderefimpl;override;
  345. procedure derefimpl;override;
  346. procedure derefnode;override;
  347. procedure concattolist(l : tlinkedlist);override;
  348. function ischild(p : tnode) : boolean;override;
  349. function docompare(p : tnode) : boolean;override;
  350. function dogetcopy : tnode;override;
  351. procedure insertintolist(l : tnodelist);override;
  352. procedure left_max;
  353. procedure printnodedata(var t:text);override;
  354. end;
  355. pbinarynode = ^tbinarynode;
  356. tbinarynode = class(tunarynode)
  357. right : tnode;
  358. constructor create(t:tnodetype;l,r : tnode);
  359. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  360. destructor destroy;override;
  361. procedure ppuwrite(ppufile:tcompilerppufile);override;
  362. procedure buildderefimpl;override;
  363. procedure derefimpl;override;
  364. procedure derefnode;override;
  365. procedure concattolist(l : tlinkedlist);override;
  366. function ischild(p : tnode) : boolean;override;
  367. function docompare(p : tnode) : boolean;override;
  368. procedure swapleftright;
  369. function dogetcopy : tnode;override;
  370. procedure insertintolist(l : tnodelist);override;
  371. procedure left_right_max;
  372. procedure printnodedata(var t:text);override;
  373. procedure printnodelist(var t:text);
  374. end;
  375. ptertiarynode = ^ttertiarynode;
  376. ttertiarynode = class(tbinarynode)
  377. third : tnode;
  378. constructor create(_t:tnodetype;l,r,t : tnode);
  379. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  380. destructor destroy;override;
  381. procedure ppuwrite(ppufile:tcompilerppufile);override;
  382. procedure buildderefimpl;override;
  383. procedure derefimpl;override;
  384. procedure derefnode;override;
  385. procedure concattolist(l : tlinkedlist);override;
  386. function ischild(p : tnode) : boolean;override;
  387. function docompare(p : tnode) : boolean;override;
  388. function dogetcopy : tnode;override;
  389. procedure insertintolist(l : tnodelist);override;
  390. procedure printnodedata(var t:text);override;
  391. end;
  392. tbinopnode = class(tbinarynode)
  393. constructor create(t:tnodetype;l,r : tnode);virtual;
  394. function docompare(p : tnode) : boolean;override;
  395. end;
  396. var
  397. { array with all class types for tnodes }
  398. nodeclass : tnodeclassarray;
  399. function nodeppuidxget(i:longint):tnode;
  400. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  401. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  402. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  403. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  404. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  405. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  406. const
  407. printnodespacing = ' ';
  408. var
  409. { indention used when writing the tree to the screen }
  410. printnodeindention : string;
  411. procedure printnodeindent;
  412. procedure printnodeunindent;
  413. procedure printnode(var t:text;n:tnode);
  414. procedure printnode(n:tnode);
  415. function is_constnode(p : tnode) : boolean;
  416. function is_constintnode(p : tnode) : boolean;
  417. function is_constcharnode(p : tnode) : boolean;
  418. function is_constrealnode(p : tnode) : boolean;
  419. function is_constboolnode(p : tnode) : boolean;
  420. function is_constenumnode(p : tnode) : boolean;
  421. function is_constwidecharnode(p : tnode) : boolean;
  422. implementation
  423. uses
  424. cutils,verbose,ppu,
  425. symconst,
  426. nutils,nflw,
  427. defutil;
  428. const
  429. ppunodemarker = 255;
  430. {****************************************************************************
  431. Helpers
  432. ****************************************************************************}
  433. var
  434. nodeppudata : tdynamicarray;
  435. nodeppuidx : longint;
  436. procedure nodeppuidxcreate;
  437. begin
  438. nodeppudata:=tdynamicarray.create(1024);
  439. nodeppuidx:=0;
  440. end;
  441. procedure nodeppuidxfree;
  442. begin
  443. nodeppudata.free;
  444. nodeppudata:=nil;
  445. end;
  446. procedure nodeppuidxadd(n:tnode);
  447. begin
  448. if n.ppuidx<0 then
  449. internalerror(200311072);
  450. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  451. nodeppudata.write(n,sizeof(pointer));
  452. end;
  453. function nodeppuidxget(i:longint):tnode;
  454. begin
  455. if i<0 then
  456. internalerror(200311072);
  457. nodeppudata.seek(i*sizeof(pointer));
  458. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  459. internalerror(200311073);
  460. end;
  461. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  462. var
  463. b : byte;
  464. t : tnodetype;
  465. hppuidx : longint;
  466. begin
  467. { marker }
  468. b:=ppufile.getbyte;
  469. if b<>ppunodemarker then
  470. internalerror(200208151);
  471. { load nodetype }
  472. t:=tnodetype(ppufile.getbyte);
  473. if t>high(tnodetype) then
  474. internalerror(200208152);
  475. if t<>emptynode then
  476. begin
  477. if not assigned(nodeclass[t]) then
  478. internalerror(200208153);
  479. hppuidx:=ppufile.getlongint;
  480. //writeln('load: ',nodetype2str[t]);
  481. { generate node of the correct class }
  482. result:=nodeclass[t].ppuload(t,ppufile);
  483. result.ppuidx:=hppuidx;
  484. nodeppuidxadd(result);
  485. end
  486. else
  487. result:=nil;
  488. end;
  489. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  490. begin
  491. { marker, read by ppuloadnode }
  492. ppufile.putbyte(ppunodemarker);
  493. { type, read by ppuloadnode }
  494. if assigned(n) then
  495. begin
  496. if n.ppuidx=-1 then
  497. internalerror(200311071);
  498. n.ppuidx:=nodeppuidx;
  499. inc(nodeppuidx);
  500. ppufile.putbyte(byte(n.nodetype));
  501. ppufile.putlongint(n.ppuidx);
  502. //writeln('write: ',nodetype2str[n.nodetype]);
  503. n.ppuwrite(ppufile);
  504. end
  505. else
  506. ppufile.putbyte(byte(emptynode));
  507. end;
  508. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  509. begin
  510. { writing of node references isn't implemented yet (FK) }
  511. internalerror(200506181);
  512. end;
  513. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  514. begin
  515. { reading of node references isn't implemented yet (FK) }
  516. internalerror(200506182);
  517. { avoid warning }
  518. result := nil;
  519. end;
  520. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  521. begin
  522. if ppufile.readentry<>ibnodetree then
  523. Message(unit_f_ppu_read_error);
  524. nodeppuidxcreate;
  525. result:=ppuloadnode(ppufile);
  526. result.derefnode;
  527. nodeppuidxfree;
  528. end;
  529. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  530. begin
  531. nodeppuidx:=0;
  532. ppuwritenode(ppufile,n);
  533. ppufile.writeentry(ibnodetree);
  534. end;
  535. procedure printnodeindent;
  536. begin
  537. printnodeindention:=printnodeindention+printnodespacing;
  538. end;
  539. procedure printnodeunindent;
  540. begin
  541. delete(printnodeindention,1,length(printnodespacing));
  542. end;
  543. procedure printnode(var t:text;n:tnode);
  544. begin
  545. if assigned(n) then
  546. n.printnodetree(t)
  547. else
  548. writeln(t,printnodeindention,'nil');
  549. end;
  550. procedure printnode(n:tnode);
  551. begin
  552. printnode(output,n);
  553. end;
  554. function is_constnode(p : tnode) : boolean;
  555. begin
  556. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  557. end;
  558. function is_constintnode(p : tnode) : boolean;
  559. begin
  560. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resultdef);
  561. end;
  562. function is_constcharnode(p : tnode) : boolean;
  563. begin
  564. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resultdef);
  565. end;
  566. function is_constwidecharnode(p : tnode) : boolean;
  567. begin
  568. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resultdef);
  569. end;
  570. function is_constrealnode(p : tnode) : boolean;
  571. begin
  572. is_constrealnode:=(p.nodetype=realconstn);
  573. end;
  574. function is_constboolnode(p : tnode) : boolean;
  575. begin
  576. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resultdef);
  577. end;
  578. function is_constenumnode(p : tnode) : boolean;
  579. begin
  580. is_constenumnode:=(p.nodetype=ordconstn) and (p.resultdef.typ=enumdef);
  581. end;
  582. {****************************************************************************
  583. TNODE
  584. ****************************************************************************}
  585. constructor tnode.create(t:tnodetype);
  586. begin
  587. inherited create;
  588. nodetype:=t;
  589. blocktype:=block_type;
  590. { updated by firstpass }
  591. expectloc:=LOC_INVALID;
  592. { updated by secondpass }
  593. location.loc:=LOC_INVALID;
  594. { save local info }
  595. fileinfo:=current_filepos;
  596. localswitches:=current_settings.localswitches;
  597. resultdef:=nil;
  598. registersint:=0;
  599. registersfpu:=0;
  600. {$ifdef SUPPORT_MMX}
  601. registersmmx:=0;
  602. {$endif SUPPORT_MMX}
  603. {$ifdef EXTDEBUG}
  604. maxfirstpasscount:=0;
  605. firstpasscount:=0;
  606. {$endif EXTDEBUG}
  607. flags:=[];
  608. ppuidx:=-1;
  609. end;
  610. constructor tnode.createforcopy;
  611. begin
  612. end;
  613. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  614. begin
  615. nodetype:=t;
  616. { tnode fields }
  617. blocktype:=tblock_type(ppufile.getbyte);
  618. ppufile.getposinfo(fileinfo);
  619. ppufile.getsmallset(localswitches);
  620. ppufile.getderef(resultdefderef);
  621. ppufile.getsmallset(flags);
  622. { updated by firstpass }
  623. expectloc:=LOC_INVALID;
  624. { updated by secondpass }
  625. location.loc:=LOC_INVALID;
  626. registersint:=0;
  627. registersfpu:=0;
  628. {$ifdef SUPPORT_MMX}
  629. registersmmx:=0;
  630. {$endif SUPPORT_MMX}
  631. {$ifdef EXTDEBUG}
  632. maxfirstpasscount:=0;
  633. firstpasscount:=0;
  634. {$endif EXTDEBUG}
  635. ppuidx:=-1;
  636. end;
  637. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  638. begin
  639. ppufile.putbyte(byte(block_type));
  640. ppufile.putposinfo(fileinfo);
  641. ppufile.putsmallset(localswitches);
  642. ppufile.putderef(resultdefderef);
  643. ppufile.putsmallset(flags);
  644. end;
  645. procedure tnode.buildderefimpl;
  646. begin
  647. resultdefderef.build(resultdef);
  648. end;
  649. procedure tnode.derefimpl;
  650. begin
  651. resultdef:=tdef(resultdefderef.resolve);
  652. end;
  653. procedure tnode.derefnode;
  654. begin
  655. end;
  656. procedure tnode.toggleflag(f : tnodeflag);
  657. begin
  658. if f in flags then
  659. exclude(flags,f)
  660. else
  661. include(flags,f);
  662. end;
  663. function tnode.simplify : tnode;
  664. begin
  665. result:=nil;
  666. end;
  667. destructor tnode.destroy;
  668. begin
  669. {$ifdef EXTDEBUG}
  670. if firstpasscount>maxfirstpasscount then
  671. maxfirstpasscount:=firstpasscount;
  672. {$endif EXTDEBUG}
  673. if assigned(optinfo) then
  674. dispose(optinfo);
  675. end;
  676. procedure tnode.concattolist(l : tlinkedlist);
  677. begin
  678. end;
  679. function tnode.ischild(p : tnode) : boolean;
  680. begin
  681. ischild:=false;
  682. end;
  683. procedure tnode.mark_write;
  684. begin
  685. {$ifdef EXTDEBUG}
  686. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  687. {$endif EXTDEBUG}
  688. end;
  689. procedure tnode.printnodeinfo(var t:text);
  690. begin
  691. write(t,nodetype2str[nodetype]);
  692. if assigned(resultdef) then
  693. write(t,', resultdef = "',resultdef.GetTypeName,'"')
  694. else
  695. write(t,', resultdef = <nil>');
  696. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  697. ', loc = ',tcgloc2str[location.loc],
  698. ', expectloc = ',tcgloc2str[expectloc],
  699. ', intregs = ',registersint,
  700. ', fpuregs = ',registersfpu);
  701. end;
  702. procedure tnode.printnodedata(var t:text);
  703. begin
  704. end;
  705. procedure tnode.printnodetree(var t:text);
  706. begin
  707. write(t,printnodeindention,'(');
  708. printnodeinfo(t);
  709. writeln(t);
  710. printnodeindent;
  711. printnodedata(t);
  712. printnodeunindent;
  713. writeln(t,printnodeindention,')');
  714. end;
  715. function tnode.isequal(p : tnode) : boolean;
  716. begin
  717. isequal:=
  718. (not assigned(self) and not assigned(p)) or
  719. (assigned(self) and assigned(p) and
  720. { optimized subclasses have the same nodetype as their }
  721. { superclass (for compatibility), so also check the classtype (JM) }
  722. (p.classtype=classtype) and
  723. (p.nodetype=nodetype) and
  724. (flags*flagsequal=p.flags*flagsequal) and
  725. docompare(p));
  726. end;
  727. {$ifdef state_tracking}
  728. function Tnode.track_state_pass(exec_known:boolean):boolean;
  729. begin
  730. track_state_pass:=false;
  731. end;
  732. {$endif state_tracking}
  733. function tnode.docompare(p : tnode) : boolean;
  734. begin
  735. docompare:=true;
  736. end;
  737. function cleanupcopiedto(var n : tnode;arg : pointer) : foreachnoderesult;
  738. begin
  739. result:=fen_true;
  740. if n.nodetype=labeln then
  741. tlabelnode(n).copiedto:=nil;
  742. end;
  743. function tnode.getcopy : tnode;
  744. begin
  745. result:=dogetcopy;
  746. foreachnodestatic(pm_postprocess,self,@cleanupcopiedto,nil);
  747. end;
  748. function tnode.dogetcopy : tnode;
  749. var
  750. p : tnode;
  751. begin
  752. { this is quite tricky because we need a node of the current }
  753. { node type and not one of tnode! }
  754. p:=tnodeclass(classtype).createforcopy;
  755. p.nodetype:=nodetype;
  756. p.expectloc:=expectloc;
  757. p.location:=location;
  758. p.parent:=parent;
  759. p.flags:=flags;
  760. p.registersint:=registersint;
  761. p.registersfpu:=registersfpu;
  762. {$ifdef SUPPORT_MMX}
  763. p.registersmmx:=registersmmx;
  764. p.registersmm:=registersmm;
  765. {$endif SUPPORT_MMX}
  766. p.resultdef:=resultdef;
  767. p.fileinfo:=fileinfo;
  768. p.localswitches:=localswitches;
  769. {$ifdef extdebug}
  770. p.firstpasscount:=firstpasscount;
  771. {$endif extdebug}
  772. { p.list:=list; }
  773. result:=p;
  774. end;
  775. procedure tnode.insertintolist(l : tnodelist);
  776. begin
  777. end;
  778. { ensures that the optimizer info record is allocated }
  779. function tnode.allocoptinfo : poptinfo;inline;
  780. begin
  781. if not(assigned(optinfo)) then
  782. new(optinfo);
  783. result:=optinfo;
  784. end;
  785. {****************************************************************************
  786. TUNARYNODE
  787. ****************************************************************************}
  788. constructor tunarynode.create(t:tnodetype;l : tnode);
  789. begin
  790. inherited create(t);
  791. left:=l;
  792. end;
  793. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  794. begin
  795. inherited ppuload(t,ppufile);
  796. left:=ppuloadnode(ppufile);
  797. end;
  798. destructor tunarynode.destroy;
  799. begin
  800. left.free;
  801. inherited destroy;
  802. end;
  803. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  804. begin
  805. inherited ppuwrite(ppufile);
  806. ppuwritenode(ppufile,left);
  807. end;
  808. procedure tunarynode.buildderefimpl;
  809. begin
  810. inherited buildderefimpl;
  811. if assigned(left) then
  812. left.buildderefimpl;
  813. end;
  814. procedure tunarynode.derefimpl;
  815. begin
  816. inherited derefimpl;
  817. if assigned(left) then
  818. left.derefimpl;
  819. end;
  820. procedure tunarynode.derefnode;
  821. begin
  822. inherited derefnode;
  823. if assigned(left) then
  824. left.derefnode;
  825. end;
  826. function tunarynode.docompare(p : tnode) : boolean;
  827. begin
  828. docompare:=(inherited docompare(p) and
  829. ((left=nil) or left.isequal(tunarynode(p).left))
  830. );
  831. end;
  832. function tunarynode.dogetcopy : tnode;
  833. var
  834. p : tunarynode;
  835. begin
  836. p:=tunarynode(inherited dogetcopy);
  837. if assigned(left) then
  838. p.left:=left.dogetcopy
  839. else
  840. p.left:=nil;
  841. result:=p;
  842. end;
  843. procedure tunarynode.insertintolist(l : tnodelist);
  844. begin
  845. end;
  846. procedure tunarynode.printnodedata(var t:text);
  847. begin
  848. inherited printnodedata(t);
  849. printnode(t,left);
  850. end;
  851. procedure tunarynode.left_max;
  852. begin
  853. registersint:=left.registersint;
  854. registersfpu:=left.registersfpu;
  855. {$ifdef SUPPORT_MMX}
  856. registersmmx:=left.registersmmx;
  857. {$endif SUPPORT_MMX}
  858. end;
  859. procedure tunarynode.concattolist(l : tlinkedlist);
  860. begin
  861. left.parent:=self;
  862. left.concattolist(l);
  863. inherited concattolist(l);
  864. end;
  865. function tunarynode.ischild(p : tnode) : boolean;
  866. begin
  867. ischild:=p=left;
  868. end;
  869. {****************************************************************************
  870. TBINARYNODE
  871. ****************************************************************************}
  872. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  873. begin
  874. inherited create(t,l);
  875. right:=r
  876. end;
  877. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  878. begin
  879. inherited ppuload(t,ppufile);
  880. right:=ppuloadnode(ppufile);
  881. end;
  882. destructor tbinarynode.destroy;
  883. begin
  884. right.free;
  885. inherited destroy;
  886. end;
  887. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  888. begin
  889. inherited ppuwrite(ppufile);
  890. ppuwritenode(ppufile,right);
  891. end;
  892. procedure tbinarynode.buildderefimpl;
  893. begin
  894. inherited buildderefimpl;
  895. if assigned(right) then
  896. right.buildderefimpl;
  897. end;
  898. procedure tbinarynode.derefimpl;
  899. begin
  900. inherited derefimpl;
  901. if assigned(right) then
  902. right.derefimpl;
  903. end;
  904. procedure tbinarynode.derefnode;
  905. begin
  906. inherited derefnode;
  907. if assigned(right) then
  908. right.derefnode;
  909. end;
  910. procedure tbinarynode.concattolist(l : tlinkedlist);
  911. begin
  912. { we could change that depending on the number of }
  913. { required registers }
  914. left.parent:=self;
  915. left.concattolist(l);
  916. left.parent:=self;
  917. left.concattolist(l);
  918. inherited concattolist(l);
  919. end;
  920. function tbinarynode.ischild(p : tnode) : boolean;
  921. begin
  922. ischild:=(p=right);
  923. end;
  924. function tbinarynode.docompare(p : tnode) : boolean;
  925. begin
  926. docompare:=(inherited docompare(p) and
  927. ((right=nil) or right.isequal(tbinarynode(p).right))
  928. );
  929. end;
  930. function tbinarynode.dogetcopy : tnode;
  931. var
  932. p : tbinarynode;
  933. begin
  934. p:=tbinarynode(inherited dogetcopy);
  935. if assigned(right) then
  936. p.right:=right.dogetcopy
  937. else
  938. p.right:=nil;
  939. result:=p;
  940. end;
  941. procedure tbinarynode.insertintolist(l : tnodelist);
  942. begin
  943. end;
  944. procedure tbinarynode.swapleftright;
  945. var
  946. swapp : tnode;
  947. begin
  948. swapp:=right;
  949. right:=left;
  950. left:=swapp;
  951. if nf_swapped in flags then
  952. exclude(flags,nf_swapped)
  953. else
  954. include(flags,nf_swapped);
  955. end;
  956. procedure tbinarynode.left_right_max;
  957. begin
  958. if assigned(left) then
  959. begin
  960. if assigned(right) then
  961. begin
  962. registersint:=max(left.registersint,right.registersint);
  963. registersfpu:=max(left.registersfpu,right.registersfpu);
  964. {$ifdef SUPPORT_MMX}
  965. registersmmx:=max(left.registersmmx,right.registersmmx);
  966. {$endif SUPPORT_MMX}
  967. end
  968. else
  969. begin
  970. registersint:=left.registersint;
  971. registersfpu:=left.registersfpu;
  972. {$ifdef SUPPORT_MMX}
  973. registersmmx:=left.registersmmx;
  974. {$endif SUPPORT_MMX}
  975. end;
  976. end;
  977. end;
  978. procedure tbinarynode.printnodedata(var t:text);
  979. begin
  980. inherited printnodedata(t);
  981. printnode(t,right);
  982. end;
  983. procedure tbinarynode.printnodelist(var t:text);
  984. var
  985. hp : tbinarynode;
  986. begin
  987. hp:=self;
  988. while assigned(hp) do
  989. begin
  990. write(t,printnodeindention,'(');
  991. printnodeindent;
  992. hp.printnodeinfo(t);
  993. writeln(t);
  994. printnode(t,hp.left);
  995. writeln(t);
  996. printnodeunindent;
  997. writeln(t,printnodeindention,')');
  998. hp:=tbinarynode(hp.right);
  999. end;
  1000. end;
  1001. {****************************************************************************
  1002. TTERTIARYNODE
  1003. ****************************************************************************}
  1004. constructor ttertiarynode.create(_t:tnodetype;l,r,t : tnode);
  1005. begin
  1006. inherited create(_t,l,r);
  1007. third:=t;
  1008. end;
  1009. constructor ttertiarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1010. begin
  1011. inherited ppuload(t,ppufile);
  1012. third:=ppuloadnode(ppufile);
  1013. end;
  1014. destructor ttertiarynode.destroy;
  1015. begin
  1016. third.free;
  1017. inherited destroy;
  1018. end;
  1019. procedure ttertiarynode.ppuwrite(ppufile:tcompilerppufile);
  1020. begin
  1021. inherited ppuwrite(ppufile);
  1022. ppuwritenode(ppufile,third);
  1023. end;
  1024. procedure ttertiarynode.buildderefimpl;
  1025. begin
  1026. inherited buildderefimpl;
  1027. if assigned(third) then
  1028. third.buildderefimpl;
  1029. end;
  1030. procedure ttertiarynode.derefimpl;
  1031. begin
  1032. inherited derefimpl;
  1033. if assigned(third) then
  1034. third.derefimpl;
  1035. end;
  1036. procedure ttertiarynode.derefnode;
  1037. begin
  1038. inherited derefnode;
  1039. if assigned(third) then
  1040. third.derefnode;
  1041. end;
  1042. function ttertiarynode.docompare(p : tnode) : boolean;
  1043. begin
  1044. docompare:=(inherited docompare(p) and
  1045. ((third=nil) or third.isequal(ttertiarynode(p).third))
  1046. );
  1047. end;
  1048. function ttertiarynode.dogetcopy : tnode;
  1049. var
  1050. p : ttertiarynode;
  1051. begin
  1052. p:=ttertiarynode(inherited dogetcopy);
  1053. if assigned(third) then
  1054. p.third:=third.dogetcopy
  1055. else
  1056. p.third:=nil;
  1057. result:=p;
  1058. end;
  1059. procedure ttertiarynode.insertintolist(l : tnodelist);
  1060. begin
  1061. end;
  1062. procedure ttertiarynode.printnodedata(var t:text);
  1063. begin
  1064. inherited printnodedata(t);
  1065. printnode(t,third);
  1066. end;
  1067. procedure ttertiarynode.concattolist(l : tlinkedlist);
  1068. begin
  1069. third.parent:=self;
  1070. third.concattolist(l);
  1071. inherited concattolist(l);
  1072. end;
  1073. function ttertiarynode.ischild(p : tnode) : boolean;
  1074. begin
  1075. ischild:=p=third;
  1076. end;
  1077. {****************************************************************************
  1078. TBINOPNODE
  1079. ****************************************************************************}
  1080. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  1081. begin
  1082. inherited create(t,l,r);
  1083. end;
  1084. function tbinopnode.docompare(p : tnode) : boolean;
  1085. begin
  1086. docompare:=(inherited docompare(p)) or
  1087. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  1088. ((nf_swapable in flags) and
  1089. left.isequal(tbinopnode(p).right) and
  1090. right.isequal(tbinopnode(p).left));
  1091. end;
  1092. end.