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