node.pas 35 KB

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