node.pas 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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. private
  246. fppuidx : longint;
  247. function getppuidx:longint;
  248. public
  249. { type of this node }
  250. nodetype : tnodetype;
  251. { type of the current code block, general/const/type }
  252. blocktype : tblock_type;
  253. { expected location of the result of this node (pass1) }
  254. expectloc : tcgloc;
  255. { the location of the result of this node (pass2) }
  256. location : tlocation;
  257. { the parent node of this is node }
  258. { this field is set by concattolist }
  259. parent : tnode;
  260. { next node in control flow on the same block level, i.e.
  261. for loop nodes, this is the next node after the end of the loop,
  262. same for if and case, if this field is nil, the next node is the procedure exit,
  263. for the last node in a loop this is set to the loop header
  264. this field is set only for control flow nodes }
  265. successor : tnode;
  266. { there are some properties about the node stored }
  267. flags : tnodeflags;
  268. resultdef : tdef;
  269. resultdefderef : tderef;
  270. fileinfo : tfileposinfo;
  271. localswitches : tlocalswitches;
  272. optinfo : poptinfo;
  273. constructor create(t:tnodetype);
  274. { this constructor is only for creating copies of class }
  275. { the fields are copied by getcopy }
  276. constructor createforcopy;
  277. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);virtual;
  278. destructor destroy;override;
  279. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  280. procedure buildderefimpl;virtual;
  281. procedure derefimpl;virtual;
  282. procedure resolveppuidx;virtual;
  283. { toggles the flag }
  284. procedure toggleflag(f : tnodeflag);
  285. { the 1.1 code generator may override pass_1 }
  286. { and it need not to implement det_* then }
  287. { 1.1: pass_1 returns a value<>0 if the node has been transformed }
  288. { 2.0: runs pass_typecheck and det_temp }
  289. function pass_1 : tnode;virtual;abstract;
  290. { dermines the resultdef of the node }
  291. function pass_typecheck : tnode;virtual;abstract;
  292. { tries to simplify the node, returns a value <>nil if a simplified
  293. node has been created }
  294. function simplify : tnode;virtual;
  295. {$ifdef state_tracking}
  296. { Does optimizations by keeping track of the variable states
  297. in a procedure }
  298. function track_state_pass(exec_known:boolean):boolean;virtual;
  299. {$endif}
  300. { For a t1:=t2 tree, mark the part of the tree t1 that gets
  301. written to (normally the loadnode) as write access. }
  302. procedure mark_write;virtual;
  303. { dermines the number of necessary temp. locations to evaluate
  304. the node }
  305. procedure det_temp;virtual;abstract;
  306. procedure pass_generate_code;virtual;abstract;
  307. { comparing of nodes }
  308. function isequal(p : tnode) : boolean;
  309. { to implement comparisation, override this method }
  310. function docompare(p : tnode) : boolean;virtual;
  311. { wrapper for getcopy }
  312. function getcopy : tnode;
  313. { does the real copying of a node }
  314. function dogetcopy : tnode;virtual;
  315. { returns the real loadn/temprefn a node refers to,
  316. skipping (absolute) equal type conversions }
  317. function actualtargetnode: tnode;virtual;
  318. procedure insertintolist(l : tnodelist);virtual;
  319. { writes a node for debugging purpose, shouldn't be called }
  320. { direct, because there is no test for nil, use printnode }
  321. { to write a complete tree }
  322. procedure printnodeinfo(var t:text);virtual;
  323. procedure printnodedata(var t:text);virtual;
  324. procedure printnodetree(var t:text);virtual;
  325. procedure concattolist(l : tlinkedlist);virtual;
  326. function ischild(p : tnode) : boolean;virtual;
  327. { ensures that the optimizer info record is allocated }
  328. function allocoptinfo : poptinfo;inline;
  329. property ppuidx:longint read getppuidx;
  330. end;
  331. tnodeclass = class of tnode;
  332. tnodeclassarray = array[tnodetype] of tnodeclass;
  333. { this node is the anchestor for all nodes with at least }
  334. { one child, you have to use it if you want to use }
  335. { true- and current_procinfo.CurrFalseLabel }
  336. punarynode = ^tunarynode;
  337. tunarynode = class(tnode)
  338. left : tnode;
  339. constructor create(t:tnodetype;l : tnode);
  340. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  341. destructor destroy;override;
  342. procedure ppuwrite(ppufile:tcompilerppufile);override;
  343. procedure buildderefimpl;override;
  344. procedure derefimpl;override;
  345. procedure concattolist(l : tlinkedlist);override;
  346. function ischild(p : tnode) : boolean;override;
  347. function docompare(p : tnode) : boolean;override;
  348. function dogetcopy : tnode;override;
  349. procedure insertintolist(l : tnodelist);override;
  350. procedure printnodedata(var t:text);override;
  351. end;
  352. pbinarynode = ^tbinarynode;
  353. tbinarynode = class(tunarynode)
  354. right : tnode;
  355. constructor create(t:tnodetype;l,r : tnode);
  356. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  357. destructor destroy;override;
  358. procedure ppuwrite(ppufile:tcompilerppufile);override;
  359. procedure buildderefimpl;override;
  360. procedure derefimpl;override;
  361. procedure concattolist(l : tlinkedlist);override;
  362. function ischild(p : tnode) : boolean;override;
  363. function docompare(p : tnode) : boolean;override;
  364. procedure swapleftright;
  365. function dogetcopy : tnode;override;
  366. procedure insertintolist(l : tnodelist);override;
  367. procedure printnodedata(var t:text);override;
  368. procedure printnodelist(var t:text);
  369. end;
  370. ptertiarynode = ^ttertiarynode;
  371. ttertiarynode = class(tbinarynode)
  372. third : tnode;
  373. constructor create(_t:tnodetype;l,r,t : tnode);
  374. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  375. destructor destroy;override;
  376. procedure ppuwrite(ppufile:tcompilerppufile);override;
  377. procedure buildderefimpl;override;
  378. procedure derefimpl;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. const
  399. printnodespacing = ' ';
  400. var
  401. { indention used when writing the tree to the screen }
  402. printnodeindention : string;
  403. procedure printnodeindent;
  404. procedure printnodeunindent;
  405. procedure printnode(var t:text;n:tnode);
  406. procedure printnode(n:tnode);
  407. function is_constnode(p : tnode) : boolean;
  408. function is_constintnode(p : tnode) : boolean;
  409. function is_constcharnode(p : tnode) : boolean;
  410. function is_constrealnode(p : tnode) : boolean;
  411. function is_constboolnode(p : tnode) : boolean;
  412. function is_constenumnode(p : tnode) : boolean;
  413. function is_constwidecharnode(p : tnode) : boolean;
  414. function is_constpointernode(p : tnode) : boolean;
  415. implementation
  416. uses
  417. cutils,verbose,ppu,
  418. symconst,
  419. nutils,nflw,
  420. defutil;
  421. const
  422. ppunodemarker = 255;
  423. {****************************************************************************
  424. Helpers
  425. ****************************************************************************}
  426. var
  427. nodeppulist : TFPObjectList;
  428. nodeppuidx : longint;
  429. procedure nodeppuidxcreate;
  430. begin
  431. nodeppulist:=TFPObjectList.Create(false);
  432. nodeppuidx:=0;
  433. end;
  434. procedure nodeppuidxresolve;
  435. var
  436. i : longint;
  437. n : tnode;
  438. begin
  439. for i:=0 to nodeppulist.count-1 do
  440. begin
  441. n:=tnode(nodeppulist[i]);
  442. if assigned(n) then
  443. n.resolveppuidx;
  444. end;
  445. end;
  446. procedure nodeppuidxfree;
  447. begin
  448. nodeppulist.free;
  449. nodeppulist:=nil;
  450. nodeppuidx:=0;
  451. end;
  452. procedure nodeppuidxadd(n:tnode);
  453. var
  454. i : longint;
  455. begin
  456. i:=n.ppuidx;
  457. if i<=0 then
  458. internalerror(200311072);
  459. if i>=nodeppulist.capacity then
  460. nodeppulist.capacity:=((i div 1024)+1)*1024;
  461. if i>=nodeppulist.count then
  462. nodeppulist.count:=i+1;
  463. nodeppulist[i]:=n;
  464. end;
  465. function nodeppuidxget(i:longint):tnode;
  466. begin
  467. if i<=0 then
  468. internalerror(200311073);
  469. result:=tnode(nodeppulist[i]);
  470. end;
  471. function ppuloadnode(ppufile:tcompilerppufile):tnode;
  472. var
  473. b : byte;
  474. t : tnodetype;
  475. hppuidx : longint;
  476. begin
  477. { marker }
  478. b:=ppufile.getbyte;
  479. if b<>ppunodemarker then
  480. internalerror(200208151);
  481. { load nodetype }
  482. t:=tnodetype(ppufile.getbyte);
  483. if t>high(tnodetype) then
  484. internalerror(200208152);
  485. if t<>emptynode then
  486. begin
  487. if not assigned(nodeclass[t]) then
  488. internalerror(200208153);
  489. hppuidx:=ppufile.getlongint;
  490. //writeln('load: ',nodetype2str[t]);
  491. { generate node of the correct class }
  492. result:=nodeclass[t].ppuload(t,ppufile);
  493. result.fppuidx:=hppuidx;
  494. nodeppuidxadd(result);
  495. end
  496. else
  497. result:=nil;
  498. end;
  499. procedure ppuwritenode(ppufile:tcompilerppufile;n:tnode);
  500. begin
  501. { marker, read by ppuloadnode }
  502. ppufile.putbyte(ppunodemarker);
  503. { type, read by ppuloadnode }
  504. if assigned(n) then
  505. begin
  506. ppufile.putbyte(byte(n.nodetype));
  507. ppufile.putlongint(n.ppuidx);
  508. //writeln('write: ',nodetype2str[n.nodetype]);
  509. n.ppuwrite(ppufile);
  510. end
  511. else
  512. ppufile.putbyte(byte(emptynode));
  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. nodeppuidxresolve;
  521. nodeppuidxfree;
  522. end;
  523. procedure ppuwritenodetree(ppufile:tcompilerppufile;n:tnode);
  524. begin
  525. nodeppuidxcreate;
  526. ppuwritenode(ppufile,n);
  527. ppufile.writeentry(ibnodetree);
  528. nodeppuidxfree;
  529. end;
  530. procedure printnodeindent;
  531. begin
  532. printnodeindention:=printnodeindention+printnodespacing;
  533. end;
  534. procedure printnodeunindent;
  535. begin
  536. delete(printnodeindention,1,length(printnodespacing));
  537. end;
  538. procedure printnode(var t:text;n:tnode);
  539. begin
  540. if assigned(n) then
  541. n.printnodetree(t)
  542. else
  543. writeln(t,printnodeindention,'nil');
  544. end;
  545. procedure printnode(n:tnode);
  546. begin
  547. printnode(output,n);
  548. end;
  549. function is_constnode(p : tnode) : boolean;
  550. begin
  551. is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,guidconstn]);
  552. end;
  553. function is_constintnode(p : tnode) : boolean;
  554. begin
  555. is_constintnode:=(p.nodetype=ordconstn) and is_integer(p.resultdef);
  556. end;
  557. function is_constcharnode(p : tnode) : boolean;
  558. begin
  559. is_constcharnode:=(p.nodetype=ordconstn) and is_char(p.resultdef);
  560. end;
  561. function is_constwidecharnode(p : tnode) : boolean;
  562. begin
  563. is_constwidecharnode:=(p.nodetype=ordconstn) and is_widechar(p.resultdef);
  564. end;
  565. function is_constrealnode(p : tnode) : boolean;
  566. begin
  567. is_constrealnode:=(p.nodetype=realconstn);
  568. end;
  569. function is_constboolnode(p : tnode) : boolean;
  570. begin
  571. is_constboolnode:=(p.nodetype=ordconstn) and is_boolean(p.resultdef);
  572. end;
  573. function is_constenumnode(p : tnode) : boolean;
  574. begin
  575. is_constenumnode:=(p.nodetype=ordconstn) and (p.resultdef.typ=enumdef);
  576. end;
  577. function is_constpointernode(p : tnode) : boolean;
  578. begin
  579. is_constpointernode:=(p.nodetype=pointerconstn);
  580. end;
  581. {****************************************************************************
  582. TNODE
  583. ****************************************************************************}
  584. constructor tnode.create(t:tnodetype);
  585. begin
  586. inherited create;
  587. nodetype:=t;
  588. blocktype:=block_type;
  589. { updated by firstpass }
  590. expectloc:=LOC_INVALID;
  591. { updated by secondpass }
  592. location.loc:=LOC_INVALID;
  593. { save local info }
  594. fileinfo:=current_filepos;
  595. localswitches:=current_settings.localswitches;
  596. resultdef:=nil;
  597. flags:=[];
  598. end;
  599. constructor tnode.createforcopy;
  600. begin
  601. end;
  602. constructor tnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  603. begin
  604. nodetype:=t;
  605. { tnode fields }
  606. blocktype:=tblock_type(ppufile.getbyte);
  607. ppufile.getposinfo(fileinfo);
  608. ppufile.getsmallset(localswitches);
  609. ppufile.getderef(resultdefderef);
  610. ppufile.getsmallset(flags);
  611. { updated by firstpass }
  612. expectloc:=LOC_INVALID;
  613. { updated by secondpass }
  614. location.loc:=LOC_INVALID;
  615. end;
  616. procedure tnode.ppuwrite(ppufile:tcompilerppufile);
  617. begin
  618. ppufile.putbyte(byte(block_type));
  619. ppufile.putposinfo(fileinfo);
  620. ppufile.putsmallset(localswitches);
  621. ppufile.putderef(resultdefderef);
  622. ppufile.putsmallset(flags);
  623. end;
  624. function tnode.getppuidx:longint;
  625. begin
  626. if fppuidx=0 then
  627. begin
  628. inc(nodeppuidx);
  629. fppuidx:=nodeppuidx;
  630. end;
  631. result:=fppuidx;
  632. end;
  633. procedure tnode.resolveppuidx;
  634. begin
  635. end;
  636. procedure tnode.buildderefimpl;
  637. begin
  638. resultdefderef.build(resultdef);
  639. end;
  640. procedure tnode.derefimpl;
  641. begin
  642. resultdef:=tdef(resultdefderef.resolve);
  643. end;
  644. procedure tnode.toggleflag(f : tnodeflag);
  645. begin
  646. if f in flags then
  647. exclude(flags,f)
  648. else
  649. include(flags,f);
  650. end;
  651. function tnode.simplify : tnode;
  652. begin
  653. result:=nil;
  654. end;
  655. destructor tnode.destroy;
  656. begin
  657. if assigned(optinfo) then
  658. dispose(optinfo);
  659. end;
  660. procedure tnode.concattolist(l : tlinkedlist);
  661. begin
  662. end;
  663. function tnode.ischild(p : tnode) : boolean;
  664. begin
  665. ischild:=false;
  666. end;
  667. procedure tnode.mark_write;
  668. begin
  669. {$ifdef EXTDEBUG}
  670. Comment(V_Warning,'mark_write not implemented for '+nodetype2str[nodetype]);
  671. {$endif EXTDEBUG}
  672. end;
  673. procedure tnode.printnodeinfo(var t:text);
  674. begin
  675. write(t,nodetype2str[nodetype]);
  676. if assigned(resultdef) then
  677. write(t,', resultdef = "',resultdef.GetTypeName,'"')
  678. else
  679. write(t,', resultdef = <nil>');
  680. write(t,', pos = (',fileinfo.line,',',fileinfo.column,')',
  681. ', loc = ',tcgloc2str[location.loc],
  682. ', expectloc = ',tcgloc2str[expectloc]);
  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.resultdef:=resultdef;
  743. p.fileinfo:=fileinfo;
  744. p.localswitches:=localswitches;
  745. { p.list:=list; }
  746. result:=p;
  747. end;
  748. function tnode.actualtargetnode: tnode;
  749. begin
  750. result:=self;
  751. end;
  752. procedure tnode.insertintolist(l : tnodelist);
  753. begin
  754. end;
  755. { ensures that the optimizer info record is allocated }
  756. function tnode.allocoptinfo : poptinfo;inline;
  757. begin
  758. if not(assigned(optinfo)) then
  759. new(optinfo);
  760. result:=optinfo;
  761. end;
  762. {****************************************************************************
  763. TUNARYNODE
  764. ****************************************************************************}
  765. constructor tunarynode.create(t:tnodetype;l : tnode);
  766. begin
  767. inherited create(t);
  768. left:=l;
  769. end;
  770. constructor tunarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  771. begin
  772. inherited ppuload(t,ppufile);
  773. left:=ppuloadnode(ppufile);
  774. end;
  775. destructor tunarynode.destroy;
  776. begin
  777. left.free;
  778. inherited destroy;
  779. end;
  780. procedure tunarynode.ppuwrite(ppufile:tcompilerppufile);
  781. begin
  782. inherited ppuwrite(ppufile);
  783. ppuwritenode(ppufile,left);
  784. end;
  785. procedure tunarynode.buildderefimpl;
  786. begin
  787. inherited buildderefimpl;
  788. if assigned(left) then
  789. left.buildderefimpl;
  790. end;
  791. procedure tunarynode.derefimpl;
  792. begin
  793. inherited derefimpl;
  794. if assigned(left) then
  795. left.derefimpl;
  796. end;
  797. function tunarynode.docompare(p : tnode) : boolean;
  798. begin
  799. docompare:=(inherited docompare(p) and
  800. ((left=nil) or left.isequal(tunarynode(p).left))
  801. );
  802. end;
  803. function tunarynode.dogetcopy : tnode;
  804. var
  805. p : tunarynode;
  806. begin
  807. p:=tunarynode(inherited dogetcopy);
  808. if assigned(left) then
  809. p.left:=left.dogetcopy
  810. else
  811. p.left:=nil;
  812. result:=p;
  813. end;
  814. procedure tunarynode.insertintolist(l : tnodelist);
  815. begin
  816. end;
  817. procedure tunarynode.printnodedata(var t:text);
  818. begin
  819. inherited printnodedata(t);
  820. printnode(t,left);
  821. end;
  822. procedure tunarynode.concattolist(l : tlinkedlist);
  823. begin
  824. left.parent:=self;
  825. left.concattolist(l);
  826. inherited concattolist(l);
  827. end;
  828. function tunarynode.ischild(p : tnode) : boolean;
  829. begin
  830. ischild:=p=left;
  831. end;
  832. {****************************************************************************
  833. TBINARYNODE
  834. ****************************************************************************}
  835. constructor tbinarynode.create(t:tnodetype;l,r : tnode);
  836. begin
  837. inherited create(t,l);
  838. right:=r
  839. end;
  840. constructor tbinarynode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  841. begin
  842. inherited ppuload(t,ppufile);
  843. right:=ppuloadnode(ppufile);
  844. end;
  845. destructor tbinarynode.destroy;
  846. begin
  847. right.free;
  848. inherited destroy;
  849. end;
  850. procedure tbinarynode.ppuwrite(ppufile:tcompilerppufile);
  851. begin
  852. inherited ppuwrite(ppufile);
  853. ppuwritenode(ppufile,right);
  854. end;
  855. procedure tbinarynode.buildderefimpl;
  856. begin
  857. inherited buildderefimpl;
  858. if assigned(right) then
  859. right.buildderefimpl;
  860. end;
  861. procedure tbinarynode.derefimpl;
  862. begin
  863. inherited derefimpl;
  864. if assigned(right) then
  865. right.derefimpl;
  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. function ttertiarynode.docompare(p : tnode) : boolean;
  972. begin
  973. docompare:=(inherited docompare(p) and
  974. ((third=nil) or third.isequal(ttertiarynode(p).third))
  975. );
  976. end;
  977. function ttertiarynode.dogetcopy : tnode;
  978. var
  979. p : ttertiarynode;
  980. begin
  981. p:=ttertiarynode(inherited dogetcopy);
  982. if assigned(third) then
  983. p.third:=third.dogetcopy
  984. else
  985. p.third:=nil;
  986. result:=p;
  987. end;
  988. procedure ttertiarynode.insertintolist(l : tnodelist);
  989. begin
  990. end;
  991. procedure ttertiarynode.printnodedata(var t:text);
  992. begin
  993. inherited printnodedata(t);
  994. printnode(t,third);
  995. end;
  996. procedure ttertiarynode.concattolist(l : tlinkedlist);
  997. begin
  998. third.parent:=self;
  999. third.concattolist(l);
  1000. inherited concattolist(l);
  1001. end;
  1002. function ttertiarynode.ischild(p : tnode) : boolean;
  1003. begin
  1004. ischild:=p=third;
  1005. end;
  1006. {****************************************************************************
  1007. TBINOPNODE
  1008. ****************************************************************************}
  1009. constructor tbinopnode.create(t:tnodetype;l,r : tnode);
  1010. begin
  1011. inherited create(t,l,r);
  1012. end;
  1013. function tbinopnode.docompare(p : tnode) : boolean;
  1014. begin
  1015. docompare:=(inherited docompare(p)) or
  1016. { if that's in the flags, is p then always a tbinopnode (?) (JM) }
  1017. ((nf_swapable in flags) and
  1018. left.isequal(tbinopnode(p).right) and
  1019. right.isequal(tbinopnode(p).left));
  1020. end;
  1021. end.