node.pas 37 KB

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