node.pas 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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. caretn, {Represents the ^ operator}
  92. starstarn, {Represents the ** operator exponentiation }
  93. arrayconstructorn, {Construction node for [...] parsing}
  94. arrayconstructorrangen, {Range element to allow sets in array construction tree}
  95. tempcreaten, { for temps in the result/firstpass }
  96. temprefn, { references to temps }
  97. tempdeleten, { for temps in the result/firstpass }
  98. addoptn, { added for optimizations where we cannot suppress }
  99. nothingn, { NOP, Do nothing}
  100. loadvmtaddrn, { Load the address of the VMT of a class/object}
  101. guidconstn, { A GUID COM Interface constant }
  102. rttin, { Rtti information so they can be accessed in result/firstpass}
  103. loadparentfpn, { Load the framepointer of the parent for nested procedures }
  104. dataconstn { node storing some binary data }
  105. );
  106. tnodetypeset = set of tnodetype;
  107. pnodetypeset = ^tnodetypeset;
  108. const
  109. nodetype2str : array[tnodetype] of string[24] = (
  110. '<emptynode>',
  111. 'addn',
  112. 'muln',
  113. 'subn',
  114. 'divn',
  115. 'symdifn',
  116. 'modn',
  117. 'assignn',
  118. 'loadn',
  119. 'rangen',
  120. 'ltn',
  121. 'lten',
  122. 'gtn',
  123. 'gten',
  124. 'equaln',
  125. 'unequaln',
  126. 'inn',
  127. 'orn',
  128. 'xorn',
  129. 'shrn',
  130. 'shln',
  131. 'slashn',
  132. 'andn',
  133. 'subscriptn',
  134. 'derefn',
  135. 'addrn',
  136. 'ordconstn',
  137. 'typeconvn',
  138. 'calln',
  139. 'callparan',
  140. 'realconstn',
  141. 'unaryminusn',
  142. 'asmn',
  143. 'vecn',
  144. 'pointerconstn',
  145. 'stringconstn',
  146. 'notn',
  147. 'inlinen',
  148. 'niln',
  149. 'errorn',
  150. 'typen',
  151. 'setelementn',
  152. 'setconstn',
  153. 'blockn',
  154. 'statementn',
  155. 'ifn',
  156. 'breakn',
  157. 'continuen',
  158. 'whilerepeatn',
  159. 'forn',
  160. 'exitn',
  161. 'withn',
  162. 'casen',
  163. 'labeln',
  164. 'goton',
  165. 'tryexceptn',
  166. 'raisen',
  167. 'tryfinallyn',
  168. 'onn',
  169. 'isn',
  170. 'asn',
  171. 'caretn',
  172. 'starstarn',
  173. 'arrayconstructn',
  174. 'arrayconstructrangen',
  175. 'tempcreaten',
  176. 'temprefn',
  177. 'tempdeleten',
  178. 'addoptn',
  179. 'nothingn',
  180. 'loadvmtaddrn',
  181. 'guidconstn',
  182. 'rttin',
  183. 'loadparentfpn',
  184. 'dataconstn');
  185. type
  186. { all boolean field of ttree are now collected in flags }
  187. tnodeflag = (
  188. nf_swapable, { tbinop operands can be swaped }
  189. nf_swapped, { tbinop operands are swaped }
  190. nf_error,
  191. { general }
  192. nf_pass1_done,
  193. nf_write, { Node is written to }
  194. nf_isproperty,
  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. { there are some properties about the node stored }
  256. flags : tnodeflags;
  257. ppuidx : longint;
  258. { the number of registers needed to evalute the node }
  259. registersint,registersfpu,registersmm : longint; { must be longint !!!! }
  260. {$ifdef SUPPORT_MMX}
  261. registersmmx : longint;
  262. {$endif SUPPORT_MMX}
  263. resultdef : tdef;
  264. resultdefderef : tderef;
  265. fileinfo : tfileposinfo;
  266. localswitches : tlocalswitches;
  267. optinfo : poptinfo;
  268. {$ifdef extdebug}
  269. maxfirstpasscount,
  270. firstpasscount : longint;
  271. {$endif extdebug}
  272. constructor create(t:tnodetype);
  273. { this constructor is only for creating copies of class }
  274. { the fields are copied by getcopy }
  275. constructor createforcopy;
  276. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  277. destructor destroy;override;
  278. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  279. procedure buildderefimpl;virtual;
  280. procedure derefimpl;virtual;
  281. procedure derefnode;virtual;
  282. { toggles the flag }
  283. procedure toggleflag(f : tnodeflag);
  284. { the 1.1 code generator may override pass_1 }
  285. { and it need not to implement det_* then }
  286. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  287. { 2.0: runs pass_typecheck and det_temp }
  288. function pass_1 : tnode;virtual;abstract;
  289. { dermines the resultdef of the node }
  290. function pass_typecheck : tnode;virtual;abstract;
  291. { tries to simplify the node, returns a value <>nil if a simplified
  292. node has been created }
  293. function simplify : tnode;virtual;
  294. {$ifdef state_tracking}
  295. { Does optimizations by keeping track of the variable states
  296. in a procedure }
  297. function track_state_pass(exec_known:boolean):boolean;virtual;
  298. {$endif}
  299. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  300. written to (normally the loadnode) as write access. }
  301. procedure mark_write;virtual;
  302. { dermines the number of necessary temp. locations to evaluate
  303. the node }
  304. procedure det_temp;virtual;abstract;
  305. procedure pass_generate_code;virtual;abstract;
  306. { comparing of nodes }
  307. function isequal(p : tnode) : boolean;
  308. { to implement comparisation, override this method }
  309. function docompare(p : tnode) : boolean;virtual;
  310. { wrapper for getcopy }
  311. function getcopy : tnode;
  312. { does the real copying of a node }
  313. function dogetcopy : tnode;virtual;
  314. procedure insertintolist(l : tnodelist);virtual;
  315. { writes a node for debugging purpose, shouldn't be called }
  316. { direct, because there is no test for nil, use printnode }
  317. { to write a complete tree }
  318. procedure printnodeinfo(var t:text);virtual;
  319. procedure printnodedata(var t:text);virtual;
  320. procedure printnodetree(var t:text);virtual;
  321. procedure concattolist(l : tlinkedlist);virtual;
  322. function ischild(p : tnode) : boolean;virtual;
  323. end;
  324. tnodeclass = class of tnode;
  325. tnodeclassarray = array[tnodetype] of tnodeclass;
  326. { this node is the anchestor for all nodes with at least }
  327. { one child, you have to use it if you want to use }
  328. { true- and current_procinfo.CurrFalseLabel }
  329. punarynode = ^tunarynode;
  330. tunarynode = class(tnode)
  331. left : tnode;
  332. constructor create(t:tnodetype;l : tnode);
  333. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  334. destructor destroy;override;
  335. procedure ppuwrite(ppufile:tcompilerppufile);override;
  336. procedure buildderefimpl;override;
  337. procedure derefimpl;override;
  338. procedure derefnode;override;
  339. procedure concattolist(l : tlinkedlist);override;
  340. function ischild(p : tnode) : boolean;override;
  341. function docompare(p : tnode) : boolean;override;
  342. function dogetcopy : tnode;override;
  343. procedure insertintolist(l : tnodelist);override;
  344. procedure left_max;
  345. procedure printnodedata(var t:text);override;
  346. end;
  347. pbinarynode = ^tbinarynode;
  348. tbinarynode = class(tunarynode)
  349. right : tnode;
  350. constructor create(t:tnodetype;l,r : tnode);
  351. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  352. destructor destroy;override;
  353. procedure ppuwrite(ppufile:tcompilerppufile);override;
  354. procedure buildderefimpl;override;
  355. procedure derefimpl;override;
  356. procedure derefnode;override;
  357. procedure concattolist(l : tlinkedlist);override;
  358. function ischild(p : tnode) : boolean;override;
  359. function docompare(p : tnode) : boolean;override;
  360. procedure swapleftright;
  361. function dogetcopy : tnode;override;
  362. procedure insertintolist(l : tnodelist);override;
  363. procedure left_right_max;
  364. procedure printnodedata(var t:text);override;
  365. procedure printnodelist(var t:text);
  366. end;
  367. ptertiarynode = ^ttertiarynode;
  368. ttertiarynode = class(tbinarynode)
  369. third : tnode;
  370. constructor create(_t:tnodetype;l,r,t : tnode);
  371. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  372. destructor destroy;override;
  373. procedure ppuwrite(ppufile:tcompilerppufile);override;
  374. procedure buildderefimpl;override;
  375. procedure derefimpl;override;
  376. procedure derefnode;override;
  377. procedure concattolist(l : tlinkedlist);override;
  378. function ischild(p : tnode) : boolean;override;
  379. function docompare(p : tnode) : boolean;override;
  380. function dogetcopy : tnode;override;
  381. procedure insertintolist(l : tnodelist);override;
  382. procedure printnodedata(var t:text);override;
  383. end;
  384. tbinopnode = class(tbinarynode)
  385. constructor create(t:tnodetype;l,r : tnode);virtual;
  386. function docompare(p : tnode) : boolean;override;
  387. end;
  388. var
  389. { array with all class types for tnodes }
  390. nodeclass : tnodeclassarray;
  391. function nodeppuidxget(i:longint):tnode;
  392. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  393. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  394. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  395. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  396. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  397. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  398. const
  399. printnodespacing = ' ';
  400. var
  401. { indention used when writing the tree to the screen }
  402. printnodeindention : string;
  403. procedure printnodeindent;
  404. procedure printnodeunindent;
  405. procedure printnode(var t:text;n:tnode);
  406. function is_constnode(p : tnode) : boolean;
  407. function is_constintnode(p : tnode) : boolean;
  408. function is_constcharnode(p : tnode) : boolean;
  409. function is_constrealnode(p : tnode) : boolean;
  410. function is_constboolnode(p : tnode) : boolean;
  411. function is_constenumnode(p : tnode) : boolean;
  412. function is_constwidecharnode(p : tnode) : boolean;
  413. implementation
  414. uses
  415. cutils,verbose,ppu,
  416. symconst,
  417. nutils,nflw,
  418. defutil;
  419. const
  420. ppunodemarker = 255;
  421. {****************************************************************************
  422. Helpers
  423. ****************************************************************************}
  424. var
  425. nodeppudata : tdynamicarray;
  426. nodeppuidx : longint;
  427. procedure nodeppuidxcreate;
  428. begin
  429. nodeppudata:=tdynamicarray.create(1024);
  430. nodeppuidx:=0;
  431. end;
  432. procedure nodeppuidxfree;
  433. begin
  434. nodeppudata.free;
  435. nodeppudata:=nil;
  436. end;
  437. procedure nodeppuidxadd(n:tnode);
  438. begin
  439. if n.ppuidx<0 then
  440. internalerror(200311072);
  441. nodeppudata.seek(n.ppuidx*sizeof(pointer));
  442. nodeppudata.write(n,sizeof(pointer));
  443. end;
  444. function nodeppuidxget(i:longint):tnode;
  445. begin
  446. if i<0 then
  447. internalerror(200311072);
  448. nodeppudata.seek(i*sizeof(pointer));
  449. if nodeppudata.read(result,sizeof(pointer))<>sizeof(pointer) then
  450. internalerror(200311073);
  451. end;
  452. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  453. var
  454. b : byte;
  455. t : tnodetype;
  456. hppuidx : longint;
  457. begin
  458. { marker }
  459. b:=ppufile.getbyte;
  460. if b<>ppunodemarker then
  461. internalerror(200208151);
  462. { load nodetype }
  463. t:=tnodetype(ppufile.getbyte);
  464. if t>high(tnodetype) then
  465. internalerror(200208152);
  466. if t<>emptynode then
  467. begin
  468. if not assigned(nodeclass[t]) then
  469. internalerror(200208153);
  470. hppuidx:=ppufile.getlongint;
  471. //writeln('load: ',nodetype2str[t]);
  472. { generate node of the correct class }
  473. result:=nodeclass[t].ppuload(t,ppufile);
  474. result.ppuidx:=hppuidx;
  475. nodeppuidxadd(result);
  476. end
  477. else
  478. result:=nil;
  479. end;
  480. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  481. begin
  482. { marker, read by ppuloadnode }
  483. ppufile.putbyte(ppunodemarker);
  484. { type, read by ppuloadnode }
  485. if assigned(n) then
  486. begin
  487. if n.ppuidx=-1 then
  488. internalerror(200311071);
  489. n.ppuidx:=nodeppuidx;
  490. inc(nodeppuidx);
  491. ppufile.putbyte(byte(n.nodetype));
  492. ppufile.putlongint(n.ppuidx);
  493. //writeln('write: ',nodetype2str[n.nodetype]);
  494. n.ppuwrite(ppufile);
  495. end
  496. else
  497. ppufile.putbyte(byte(emptynode));
  498. end;
  499. procedure ppuwritenoderef(ppufile:tcompilerppufile;n:tnode);
  500. begin
  501. { writing of node references isn't implemented yet (FK) }
  502. internalerror(200506181);
  503. end;
  504. function ppuloadnoderef(ppufile:tcompilerppufile) : tnode;
  505. begin
  506. { reading of node references isn't implemented yet (FK) }
  507. internalerror(200506182);
  508. { avoid warning }
  509. result := nil;
  510. end;
  511. function ppuloadnodetree(ppufile:tcompilerppufile):tnode;
  512. begin
  513. if ppufile.readentry<>ibnodetree then
  514. Message(unit_f_ppu_read_error);
  515. nodeppuidxcreate;
  516. result:=ppuloadnode(ppufile);
  517. result.derefnode;
  518. nodeppuidxfree;
  519. end;
  520. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  521. begin
  522. nodeppuidx:=0;
  523. ppuwritenode(ppufile,n);
  524. ppufile.writeentry(ibnodetree);
  525. end;
  526. procedure printnodeindent;
  527. begin
  528. printnodeindention:=printnodeindention+printnodespacing;
  529. end;
  530. procedure printnodeunindent;
  531. begin
  532. delete(printnodeindention,1,length(printnodespacing));
  533. end;
  534. procedure printnode(var t:text;n:tnode);
  535. begin
  536. if assigned(n) then
  537. n.printnodetree(t)
  538. else
  539. writeln(t,printnodeindention,'nil');
  540. end;
  541. function is_constnode(p : tnode) : boolean;
  542. begin
  543. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  544. end;
  545. function is_constintnode(p : tnode) : boolean;
  546. begin
  547. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resultdef);
  548. end;
  549. function is_constcharnode(p : tnode) : boolean;
  550. begin
  551. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resultdef);
  552. end;
  553. function is_constwidecharnode(p : tnode) : boolean;
  554. begin
  555. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resultdef);
  556. end;
  557. function is_constrealnode(p : tnode) : boolean;
  558. begin
  559. is_constrealnode:=(p.nodetype=realconstn);
  560. end;
  561. function is_constboolnode(p : tnode) : boolean;
  562. begin
  563. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resultdef);
  564. end;
  565. function is_constenumnode(p : tnode) : boolean;
  566. begin
  567. is_constenumnode:=(p.nodetype=ordconstn) and (p.resultdef.typ=enumdef);
  568. end;
  569. {****************************************************************************
  570. TNODE
  571. ****************************************************************************}
  572. constructor tnode.create(t:tnodetype);
  573. begin
  574. inherited create;
  575. nodetype:=t;
  576. blocktype:=block_type;
  577. { updated by firstpass }
  578. expectloc:=LOC_INVALID;
  579. { updated by secondpass }
  580. location.loc:=LOC_INVALID;
  581. { save local info }
  582. fileinfo:=current_filepos;
  583. localswitches:=current_settings.localswitches;
  584. resultdef:=nil;
  585. registersint:=0;
  586. registersfpu:=0;
  587. {$ifdef SUPPORT_MMX}
  588. registersmmx:=0;
  589. {$endif SUPPORT_MMX}
  590. {$ifdef EXTDEBUG}
  591. maxfirstpasscount:=0;
  592. firstpasscount:=0;
  593. {$endif EXTDEBUG}
  594. flags:=[];
  595. ppuidx:=-1;
  596. end;
  597. constructor tnode.createforcopy;
  598. begin
  599. end;
  600. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  601. begin
  602. nodetype:=t;
  603. { tnode fields }
  604. blocktype:=tblock_type(ppufile.getbyte);
  605. ppufile.getposinfo(fileinfo);
  606. ppufile.getsmallset(localswitches);
  607. ppufile.getderef(resultdefderef);
  608. ppufile.getsmallset(flags);
  609. { updated by firstpass }
  610. expectloc:=LOC_INVALID;
  611. { updated by secondpass }
  612. location.loc:=LOC_INVALID;
  613. registersint:=0;
  614. registersfpu:=0;
  615. {$ifdef SUPPORT_MMX}
  616. registersmmx:=0;
  617. {$endif SUPPORT_MMX}
  618. {$ifdef EXTDEBUG}
  619. maxfirstpasscount:=0;
  620. firstpasscount:=0;
  621. {$endif EXTDEBUG}
  622. ppuidx:=-1;
  623. end;
  624. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  625. begin
  626. ppufile.putbyte(byte(block_type));
  627. ppufile.putposinfo(fileinfo);
  628. ppufile.putsmallset(localswitches);
  629. ppufile.putderef(resultdefderef);
  630. ppufile.putsmallset(flags);
  631. end;
  632. procedure tnode.buildderefimpl;
  633. begin
  634. resultdefderef.build(resultdef);
  635. end;
  636. procedure tnode.derefimpl;
  637. begin
  638. resultdef:=tdef(resultdefderef.resolve);
  639. end;
  640. procedure tnode.derefnode;
  641. begin
  642. end;
  643. procedure tnode.toggleflag(f : tnodeflag);
  644. begin
  645. if f in flags then
  646. exclude(flags,f)
  647. else
  648. include(flags,f);
  649. end;
  650. function tnode.simplify : tnode;
  651. begin
  652. result:=nil;
  653. end;
  654. destructor tnode.destroy;
  655. begin
  656. {$ifdef EXTDEBUG}
  657. if firstpasscount>maxfirstpasscount then
  658. maxfirstpasscount:=firstpasscount;
  659. {$endif EXTDEBUG}
  660. end;
  661. procedure tnode.concattolist(l : tlinkedlist);
  662. begin
  663. end;
  664. function tnode.ischild(p : tnode) : boolean;
  665. begin
  666. ischild:=false;
  667. end;
  668. procedure tnode.mark_write;
  669. begin
  670. {$ifdef EXTDEBUG}
  671. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  672. {$endif EXTDEBUG}
  673. end;
  674. procedure tnode.printnodeinfo(var t:text);
  675. begin
  676. write(t,nodetype2str[nodetype]);
  677. if assigned(resultdef) then
  678. write(t,', resultdef = "',resultdef.GetTypeName,'"')
  679. else
  680. write(t,', resultdef = <nil>');
  681. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  682. ', loc = ',tcgloc2str[location.loc],
  683. ', expectloc = ',tcgloc2str[expectloc],
  684. ', intregs = ',registersint,
  685. ', fpuregs = ',registersfpu);
  686. end;
  687. procedure tnode.printnodedata(var t:text);
  688. begin
  689. end;
  690. procedure tnode.printnodetree(var t:text);
  691. begin
  692. write(t,printnodeindention,'(');
  693. printnodeinfo(t);
  694. writeln(t);
  695. printnodeindent;
  696. printnodedata(t);
  697. printnodeunindent;
  698. writeln(t,printnodeindention,')');
  699. end;
  700. function tnode.isequal(p : tnode) : boolean;
  701. begin
  702. isequal:=
  703. (not assigned(self) and not assigned(p)) or
  704. (assigned(self) and assigned(p) and
  705. { optimized subclasses have the same nodetype as their }
  706. { superclass (for compatibility), so also check the classtype (JM) }
  707. (p.classtype=classtype) and
  708. (p.nodetype=nodetype) and
  709. (flags*flagsequal=p.flags*flagsequal) and
  710. docompare(p));
  711. end;
  712. {$ifdef state_tracking}
  713. function Tnode.track_state_pass(exec_known:boolean):boolean;
  714. begin
  715. track_state_pass:=false;
  716. end;
  717. {$endif state_tracking}
  718. function tnode.docompare(p : tnode) : boolean;
  719. begin
  720. docompare:=true;
  721. end;
  722. function cleanupcopiedto(var n : tnode;arg : pointer) : foreachnoderesult;
  723. begin
  724. result:=fen_true;
  725. if n.nodetype=labeln then
  726. tlabelnode(n).copiedto:=nil;
  727. end;
  728. function tnode.getcopy : tnode;
  729. begin
  730. result:=dogetcopy;
  731. foreachnodestatic(pm_postprocess,self,@cleanupcopiedto,nil);
  732. end;
  733. function tnode.dogetcopy : tnode;
  734. var
  735. p : tnode;
  736. begin
  737. { this is quite tricky because we need a node of the current }
  738. { node type and not one of tnode! }
  739. p:=tnodeclass(classtype).createforcopy;
  740. p.nodetype:=nodetype;
  741. p.expectloc:=expectloc;
  742. p.location:=location;
  743. p.parent:=parent;
  744. p.flags:=flags;
  745. p.registersint:=registersint;
  746. p.registersfpu:=registersfpu;
  747. {$ifdef SUPPORT_MMX}
  748. p.registersmmx:=registersmmx;
  749. p.registersmm:=registersmm;
  750. {$endif SUPPORT_MMX}
  751. p.resultdef:=resultdef;
  752. p.fileinfo:=fileinfo;
  753. p.localswitches:=localswitches;
  754. {$ifdef extdebug}
  755. p.firstpasscount:=firstpasscount;
  756. {$endif extdebug}
  757. { p.list:=list; }
  758. result:=p;
  759. end;
  760. procedure tnode.insertintolist(l : tnodelist);
  761. begin
  762. end;
  763. {****************************************************************************
  764. TUNARYNODE
  765. ****************************************************************************}
  766. constructor tunarynode.create(t:tnodetype;l : tnode);
  767. begin
  768. inherited create(t);
  769. left:=l;
  770. end;
  771. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  772. begin
  773. inherited ppuload(t,ppufile);
  774. left:=ppuloadnode(ppufile);
  775. end;
  776. destructor tunarynode.destroy;
  777. begin
  778. left.free;
  779. inherited destroy;
  780. end;
  781. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  782. begin
  783. inherited ppuwrite(ppufile);
  784. ppuwritenode(ppufile,left);
  785. end;
  786. procedure tunarynode.buildderefimpl;
  787. begin
  788. inherited buildderefimpl;
  789. if assigned(left) then
  790. left.buildderefimpl;
  791. end;
  792. procedure tunarynode.derefimpl;
  793. begin
  794. inherited derefimpl;
  795. if assigned(left) then
  796. left.derefimpl;
  797. end;
  798. procedure tunarynode.derefnode;
  799. begin
  800. inherited derefnode;
  801. if assigned(left) then
  802. left.derefnode;
  803. end;
  804. function tunarynode.docompare(p : tnode) : boolean;
  805. begin
  806. docompare:=(inherited docompare(p) and
  807. ((left=nil) or left.isequal(tunarynode(p).left))
  808. );
  809. end;
  810. function tunarynode.dogetcopy : tnode;
  811. var
  812. p : tunarynode;
  813. begin
  814. p:=tunarynode(inherited dogetcopy);
  815. if assigned(left) then
  816. p.left:=left.dogetcopy
  817. else
  818. p.left:=nil;
  819. result:=p;
  820. end;
  821. procedure tunarynode.insertintolist(l : tnodelist);
  822. begin
  823. end;
  824. procedure tunarynode.printnodedata(var t:text);
  825. begin
  826. inherited printnodedata(t);
  827. printnode(t,left);
  828. end;
  829. procedure tunarynode.left_max;
  830. begin
  831. registersint:=left.registersint;
  832. registersfpu:=left.registersfpu;
  833. {$ifdef SUPPORT_MMX}
  834. registersmmx:=left.registersmmx;
  835. {$endif SUPPORT_MMX}
  836. end;
  837. procedure tunarynode.concattolist(l : tlinkedlist);
  838. begin
  839. left.parent:=self;
  840. left.concattolist(l);
  841. inherited concattolist(l);
  842. end;
  843. function tunarynode.ischild(p : tnode) : boolean;
  844. begin
  845. ischild:=p=left;
  846. end;
  847. {****************************************************************************
  848. TBINARYNODE
  849. ****************************************************************************}
  850. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  851. begin
  852. inherited create(t,l);
  853. right:=r
  854. end;
  855. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  856. begin
  857. inherited ppuload(t,ppufile);
  858. right:=ppuloadnode(ppufile);
  859. end;
  860. destructor tbinarynode.destroy;
  861. begin
  862. right.free;
  863. inherited destroy;
  864. end;
  865. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  866. begin
  867. inherited ppuwrite(ppufile);
  868. ppuwritenode(ppufile,right);
  869. end;
  870. procedure tbinarynode.buildderefimpl;
  871. begin
  872. inherited buildderefimpl;
  873. if assigned(right) then
  874. right.buildderefimpl;
  875. end;
  876. procedure tbinarynode.derefimpl;
  877. begin
  878. inherited derefimpl;
  879. if assigned(right) then
  880. right.derefimpl;
  881. end;
  882. procedure tbinarynode.derefnode;
  883. begin
  884. inherited derefnode;
  885. if assigned(right) then
  886. right.derefnode;
  887. end;
  888. procedure tbinarynode.concattolist(l : tlinkedlist);
  889. begin
  890. { we could change that depending on the number of }
  891. { required registers }
  892. left.parent:=self;
  893. left.concattolist(l);
  894. left.parent:=self;
  895. left.concattolist(l);
  896. inherited concattolist(l);
  897. end;
  898. function tbinarynode.ischild(p : tnode) : boolean;
  899. begin
  900. ischild:=(p=right);
  901. end;
  902. function tbinarynode.docompare(p : tnode) : boolean;
  903. begin
  904. docompare:=(inherited docompare(p) and
  905. ((right=nil) or right.isequal(tbinarynode(p).right))
  906. );
  907. end;
  908. function tbinarynode.dogetcopy : tnode;
  909. var
  910. p : tbinarynode;
  911. begin
  912. p:=tbinarynode(inherited dogetcopy);
  913. if assigned(right) then
  914. p.right:=right.dogetcopy
  915. else
  916. p.right:=nil;
  917. result:=p;
  918. end;
  919. procedure tbinarynode.insertintolist(l : tnodelist);
  920. begin
  921. end;
  922. procedure tbinarynode.swapleftright;
  923. var
  924. swapp : tnode;
  925. begin
  926. swapp:=right;
  927. right:=left;
  928. left:=swapp;
  929. if nf_swapped in flags then
  930. exclude(flags,nf_swapped)
  931. else
  932. include(flags,nf_swapped);
  933. end;
  934. procedure tbinarynode.left_right_max;
  935. begin
  936. if assigned(left) then
  937. begin
  938. if assigned(right) then
  939. begin
  940. registersint:=max(left.registersint,right.registersint);
  941. registersfpu:=max(left.registersfpu,right.registersfpu);
  942. {$ifdef SUPPORT_MMX}
  943. registersmmx:=max(left.registersmmx,right.registersmmx);
  944. {$endif SUPPORT_MMX}
  945. end
  946. else
  947. begin
  948. registersint:=left.registersint;
  949. registersfpu:=left.registersfpu;
  950. {$ifdef SUPPORT_MMX}
  951. registersmmx:=left.registersmmx;
  952. {$endif SUPPORT_MMX}
  953. end;
  954. end;
  955. end;
  956. procedure tbinarynode.printnodedata(var t:text);
  957. begin
  958. inherited printnodedata(t);
  959. printnode(t,right);
  960. end;
  961. procedure tbinarynode.printnodelist(var t:text);
  962. var
  963. hp : tbinarynode;
  964. begin
  965. hp:=self;
  966. while assigned(hp) do
  967. begin
  968. write(t,printnodeindention,'(');
  969. printnodeindent;
  970. hp.printnodeinfo(t);
  971. writeln(t);
  972. printnode(t,hp.left);
  973. writeln(t);
  974. printnodeunindent;
  975. writeln(t,printnodeindention,')');
  976. hp:=tbinarynode(hp.right);
  977. end;
  978. end;
  979. {****************************************************************************
  980. TTERTIARYNODE
  981. ****************************************************************************}
  982. constructor ttertiarynode.create(_t:tnodetype;l,r,t : tnode);
  983. begin
  984. inherited create(_t,l,r);
  985. third:=t;
  986. end;
  987. constructor ttertiarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  988. begin
  989. inherited ppuload(t,ppufile);
  990. third:=ppuloadnode(ppufile);
  991. end;
  992. destructor ttertiarynode.destroy;
  993. begin
  994. third.free;
  995. inherited destroy;
  996. end;
  997. procedure ttertiarynode.ppuwrite(ppufile:tcompilerppufile);
  998. begin
  999. inherited ppuwrite(ppufile);
  1000. ppuwritenode(ppufile,third);
  1001. end;
  1002. procedure ttertiarynode.buildderefimpl;
  1003. begin
  1004. inherited buildderefimpl;
  1005. if assigned(third) then
  1006. third.buildderefimpl;
  1007. end;
  1008. procedure ttertiarynode.derefimpl;
  1009. begin
  1010. inherited derefimpl;
  1011. if assigned(third) then
  1012. third.derefimpl;
  1013. end;
  1014. procedure ttertiarynode.derefnode;
  1015. begin
  1016. inherited derefnode;
  1017. if assigned(third) then
  1018. third.derefnode;
  1019. end;
  1020. function ttertiarynode.docompare(p : tnode) : boolean;
  1021. begin
  1022. docompare:=(inherited docompare(p) and
  1023. ((third=nil) or third.isequal(ttertiarynode(p).third))
  1024. );
  1025. end;
  1026. function ttertiarynode.dogetcopy : tnode;
  1027. var
  1028. p : ttertiarynode;
  1029. begin
  1030. p:=ttertiarynode(inherited dogetcopy);
  1031. if assigned(third) then
  1032. p.third:=third.dogetcopy
  1033. else
  1034. p.third:=nil;
  1035. result:=p;
  1036. end;
  1037. procedure ttertiarynode.insertintolist(l : tnodelist);
  1038. begin
  1039. end;
  1040. procedure ttertiarynode.printnodedata(var t:text);
  1041. begin
  1042. inherited printnodedata(t);
  1043. printnode(t,third);
  1044. end;
  1045. procedure ttertiarynode.concattolist(l : tlinkedlist);
  1046. begin
  1047. third.parent:=self;
  1048. third.concattolist(l);
  1049. inherited concattolist(l);
  1050. end;
  1051. function ttertiarynode.ischild(p : tnode) : boolean;
  1052. begin
  1053. ischild:=p=third;
  1054. end;
  1055. {****************************************************************************
  1056. TBINOPNODE
  1057. ****************************************************************************}
  1058. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  1059. begin
  1060. inherited create(t,l,r);
  1061. end;
  1062. function tbinopnode.docompare(p : tnode) : boolean;
  1063. begin
  1064. docompare:=(inherited docompare(p)) or
  1065. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  1066. ((nf_swapable in flags) and
  1067. left.isequal(tbinopnode(p).right) and
  1068. right.isequal(tbinopnode(p).left));
  1069. end;
  1070. end.