node.pas 36 KB

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