nflw.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and register allocation for nodes that influence
  4. the flow
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nflw;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. node,cpubase,
  24. symnot,
  25. symtype,symbase,symdef,symsym,
  26. optunrol;
  27. type
  28. { flags used by loop nodes }
  29. tloopflag = (
  30. { set if it is a for ... downto ... do loop }
  31. lnf_backward,
  32. { Do we need to parse childs to set var state? }
  33. lnf_varstate,
  34. { Do a test at the begin of the loop?}
  35. lnf_testatbegin,
  36. { Negate the loop test? }
  37. lnf_checknegate,
  38. { Should the value of the loop variable on exit be correct. }
  39. lnf_dont_mind_loopvar_on_exit);
  40. tloopflags = set of tloopflag;
  41. const
  42. { loop flags which must match to consider loop nodes equal regarding the flags }
  43. loopflagsequal = [lnf_backward];
  44. type
  45. tlabelnode = class;
  46. tloopnode = class(tbinarynode)
  47. t1,t2 : tnode;
  48. loopflags : tloopflags;
  49. constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
  50. destructor destroy;override;
  51. function _getcopy : tnode;override;
  52. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  53. procedure ppuwrite(ppufile:tcompilerppufile);override;
  54. procedure buildderefimpl;override;
  55. procedure derefimpl;override;
  56. procedure insertintolist(l : tnodelist);override;
  57. procedure printnodetree(var t:text);override;
  58. function docompare(p: tnode): boolean; override;
  59. end;
  60. twhilerepeatnode = class(tloopnode)
  61. constructor create(l,r:Tnode;tab,cn:boolean);virtual;
  62. function det_resulttype:tnode;override;
  63. function pass_1 : tnode;override;
  64. {$ifdef state_tracking}
  65. function track_state_pass(exec_known:boolean):boolean;override;
  66. {$endif}
  67. end;
  68. twhilerepeatnodeclass = class of twhilerepeatnode;
  69. tifnode = class(tloopnode)
  70. constructor create(l,r,_t1 : tnode);virtual;
  71. function det_resulttype:tnode;override;
  72. function pass_1 : tnode;override;
  73. end;
  74. tifnodeclass = class of tifnode;
  75. tfornode = class(tloopnode)
  76. { if count isn divisable by unrolls then
  77. the for loop must jump to this label to get the correct
  78. number of executions }
  79. entrylabel : tnode;
  80. loopvar_notid:cardinal;
  81. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;
  82. procedure loop_var_access(not_type:Tnotification_flag;symbol:Tsym);
  83. function det_resulttype:tnode;override;
  84. function pass_1 : tnode;override;
  85. end;
  86. tfornodeclass = class of tfornode;
  87. texitnode = class(tunarynode)
  88. constructor create(l:tnode);virtual;
  89. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  90. procedure ppuwrite(ppufile:tcompilerppufile);override;
  91. function det_resulttype:tnode;override;
  92. function pass_1 : tnode;override;
  93. end;
  94. texitnodeclass = class of texitnode;
  95. tbreaknode = class(tnode)
  96. constructor create;virtual;
  97. function det_resulttype:tnode;override;
  98. function pass_1 : tnode;override;
  99. end;
  100. tbreaknodeclass = class of tbreaknode;
  101. tcontinuenode = class(tnode)
  102. constructor create;virtual;
  103. function det_resulttype:tnode;override;
  104. function pass_1 : tnode;override;
  105. end;
  106. tcontinuenodeclass = class of tcontinuenode;
  107. tgotonode = class(tnode)
  108. { we still need this for resolving forward gotos }
  109. labelsym : tlabelsym;
  110. labelnode : tlabelnode;
  111. exceptionblock : integer;
  112. { internlab : tinterngotolabel;}
  113. constructor create(p : tlabelnode);virtual;
  114. { as long as we don't know the label node we can't resolve it }
  115. constructor create_sym(p : tlabelsym);virtual;
  116. { constructor createintern(g:tinterngotolabel);}
  117. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  118. procedure ppuwrite(ppufile:tcompilerppufile);override;
  119. procedure buildderefimpl;override;
  120. procedure derefimpl;override;
  121. function _getcopy : tnode;override;
  122. function det_resulttype:tnode;override;
  123. function pass_1 : tnode;override;
  124. function docompare(p: tnode): boolean; override;
  125. end;
  126. tgotonodeclass = class of tgotonode;
  127. tlabelnode = class(tunarynode)
  128. exceptionblock : integer;
  129. { when copying trees, this points to the newly created copy of a label }
  130. copiedto : tlabelnode;
  131. { contains all goto nodesrefering to this label }
  132. referinggotonodes : tlist;
  133. constructor create(l:tnode);virtual;
  134. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  135. procedure ppuwrite(ppufile:tcompilerppufile);override;
  136. procedure buildderefimpl;override;
  137. procedure derefimpl;override;
  138. function _getcopy : tnode;override;
  139. function det_resulttype:tnode;override;
  140. function pass_1 : tnode;override;
  141. function docompare(p: tnode): boolean; override;
  142. end;
  143. tlabelnodeclass = class of tlabelnode;
  144. traisenode = class(tbinarynode)
  145. frametree : tnode;
  146. constructor create(l,taddr,tframe:tnode);virtual;
  147. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  148. procedure ppuwrite(ppufile:tcompilerppufile);override;
  149. procedure buildderefimpl;override;
  150. procedure derefimpl;override;
  151. function _getcopy : tnode;override;
  152. procedure insertintolist(l : tnodelist);override;
  153. function det_resulttype:tnode;override;
  154. function pass_1 : tnode;override;
  155. function docompare(p: tnode): boolean; override;
  156. end;
  157. traisenodeclass = class of traisenode;
  158. ttryexceptnode = class(tloopnode)
  159. constructor create(l,r,_t1 : tnode);virtual;
  160. function det_resulttype:tnode;override;
  161. function pass_1 : tnode;override;
  162. end;
  163. ttryexceptnodeclass = class of ttryexceptnode;
  164. ttryfinallynode = class(tloopnode)
  165. implicitframe : boolean;
  166. constructor create(l,r:tnode);virtual;
  167. constructor create_implicit(l,r,_t1:tnode);virtual;
  168. function det_resulttype:tnode;override;
  169. function pass_1 : tnode;override;
  170. end;
  171. ttryfinallynodeclass = class of ttryfinallynode;
  172. tonnode = class(tbinarynode)
  173. exceptsymtable : tsymtable;
  174. excepttype : tobjectdef;
  175. constructor create(l,r:tnode);virtual;
  176. destructor destroy;override;
  177. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  178. function det_resulttype:tnode;override;
  179. function pass_1 : tnode;override;
  180. function _getcopy : tnode;override;
  181. function docompare(p: tnode): boolean; override;
  182. end;
  183. tonnodeclass = class of tonnode;
  184. var
  185. cwhilerepeatnode : twhilerepeatnodeclass;
  186. cifnode : tifnodeclass;
  187. cfornode : tfornodeclass;
  188. cexitnode : texitnodeclass;
  189. cbreaknode : tbreaknodeclass;
  190. ccontinuenode : tcontinuenodeclass;
  191. cgotonode : tgotonodeclass;
  192. clabelnode : tlabelnodeclass;
  193. craisenode : traisenodeclass;
  194. ctryexceptnode : ttryexceptnodeclass;
  195. ctryfinallynode : ttryfinallynodeclass;
  196. connode : tonnodeclass;
  197. implementation
  198. uses
  199. globtype,systems,
  200. cutils,verbose,globals,
  201. symconst,paramgr,defcmp,defutil,htypechk,pass_1,
  202. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,
  203. {$ifdef state_tracking}
  204. nstate,
  205. {$endif}
  206. cgbase,procinfo
  207. ;
  208. {****************************************************************************
  209. TLOOPNODE
  210. *****************************************************************************}
  211. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  212. begin
  213. inherited create(tt,l,r);
  214. t1:=_t1;
  215. t2:=_t2;
  216. fileinfo:=l.fileinfo;
  217. end;
  218. destructor tloopnode.destroy;
  219. begin
  220. t1.free;
  221. t2.free;
  222. inherited destroy;
  223. end;
  224. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  225. begin
  226. inherited ppuload(t,ppufile);
  227. t1:=ppuloadnode(ppufile);
  228. t2:=ppuloadnode(ppufile);
  229. end;
  230. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  231. begin
  232. inherited ppuwrite(ppufile);
  233. ppuwritenode(ppufile,t1);
  234. ppuwritenode(ppufile,t2);
  235. end;
  236. procedure tloopnode.buildderefimpl;
  237. begin
  238. inherited buildderefimpl;
  239. if assigned(t1) then
  240. t1.buildderefimpl;
  241. if assigned(t2) then
  242. t2.buildderefimpl;
  243. end;
  244. procedure tloopnode.derefimpl;
  245. begin
  246. inherited derefimpl;
  247. if assigned(t1) then
  248. t1.derefimpl;
  249. if assigned(t2) then
  250. t2.derefimpl;
  251. end;
  252. function tloopnode._getcopy : tnode;
  253. var
  254. p : tloopnode;
  255. begin
  256. p:=tloopnode(inherited _getcopy);
  257. if assigned(t1) then
  258. p.t1:=t1._getcopy
  259. else
  260. p.t1:=nil;
  261. if assigned(t2) then
  262. p.t2:=t2._getcopy
  263. else
  264. p.t2:=nil;
  265. p.loopflags:=loopflags;
  266. _getcopy:=p;
  267. end;
  268. procedure tloopnode.insertintolist(l : tnodelist);
  269. begin
  270. end;
  271. procedure tloopnode.printnodetree(var t:text);
  272. begin
  273. write(t,printnodeindention,'(');
  274. printnodeindent;
  275. printnodeinfo(t);
  276. writeln(t);
  277. printnode(t,left);
  278. printnode(t,right);
  279. printnode(t,t1);
  280. printnode(t,t2);
  281. printnodeunindent;
  282. writeln(t,printnodeindention,')');
  283. end;
  284. function tloopnode.docompare(p: tnode): boolean;
  285. begin
  286. docompare :=
  287. inherited docompare(p) and
  288. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  289. t1.isequal(tloopnode(p).t1) and
  290. t2.isequal(tloopnode(p).t2);
  291. end;
  292. {****************************************************************************
  293. TWHILEREPEATNODE
  294. *****************************************************************************}
  295. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  296. begin
  297. inherited create(whilerepeatn,l,r,nil,nil);
  298. if tab then
  299. include(loopflags, lnf_testatbegin);
  300. if cn then
  301. include(loopflags,lnf_checknegate);
  302. end;
  303. function twhilerepeatnode.det_resulttype:tnode;
  304. var
  305. t:Tunarynode;
  306. begin
  307. result:=nil;
  308. resulttype:=voidtype;
  309. resulttypepass(left);
  310. {A not node can be removed.}
  311. if left.nodetype=notn then
  312. begin
  313. t:=Tunarynode(left);
  314. left:=Tunarynode(left).left;
  315. t.left:=nil;
  316. t.destroy;
  317. {Symdif operator, in case you are wondering:}
  318. loopflags:=loopflags >< [lnf_checknegate];
  319. end;
  320. { loop instruction }
  321. if assigned(right) then
  322. resulttypepass(right);
  323. set_varstate(left,vs_used,[vsf_must_be_valid]);
  324. if codegenerror then
  325. exit;
  326. if not is_boolean(left.resulttype.def) then
  327. begin
  328. if left.resulttype.def.deftype=variantdef then
  329. inserttypeconv(left,booltype)
  330. else
  331. CGMessage1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  332. end;
  333. { Give warnings for code that will never be executed for
  334. while false do }
  335. if (lnf_testatbegin in loopflags) and
  336. (left.nodetype=ordconstn) and
  337. (tordconstnode(left).value=0) and
  338. assigned(right) then
  339. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  340. end;
  341. function twhilerepeatnode.pass_1 : tnode;
  342. var
  343. old_t_times : longint;
  344. begin
  345. result:=nil;
  346. expectloc:=LOC_VOID;
  347. old_t_times:=cg.t_times;
  348. { calc register weight }
  349. if not(cs_littlesize in aktglobalswitches ) then
  350. cg.t_times:=cg.t_times*8;
  351. firstpass(left);
  352. if codegenerror then
  353. exit;
  354. registersint:=left.registersint;
  355. registersfpu:=left.registersfpu;
  356. {$ifdef SUPPORT_MMX}
  357. registersmmx:=left.registersmmx;
  358. {$endif SUPPORT_MMX}
  359. { loop instruction }
  360. if assigned(right) then
  361. begin
  362. firstpass(right);
  363. if codegenerror then
  364. exit;
  365. if registersint<right.registersint then
  366. registersint:=right.registersint;
  367. if registersfpu<right.registersfpu then
  368. registersfpu:=right.registersfpu;
  369. {$ifdef SUPPORT_MMX}
  370. if registersmmx<right.registersmmx then
  371. registersmmx:=right.registersmmx;
  372. {$endif SUPPORT_MMX}
  373. end;
  374. cg.t_times:=old_t_times;
  375. end;
  376. {$ifdef state_tracking}
  377. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  378. var condition:Tnode;
  379. code:Tnode;
  380. done:boolean;
  381. value:boolean;
  382. change:boolean;
  383. firsttest:boolean;
  384. factval:Tnode;
  385. begin
  386. track_state_pass:=false;
  387. done:=false;
  388. firsttest:=true;
  389. {For repeat until statements, first do a pass through the code.}
  390. if not(lnf_testatbegin in flags) then
  391. begin
  392. code:=right.getcopy;
  393. if code.track_state_pass(exec_known) then
  394. track_state_pass:=true;
  395. code.destroy;
  396. end;
  397. repeat
  398. condition:=left.getcopy;
  399. code:=right.getcopy;
  400. change:=condition.track_state_pass(exec_known);
  401. factval:=aktstate.find_fact(left);
  402. if factval<>nil then
  403. begin
  404. condition.destroy;
  405. condition:=factval.getcopy;
  406. change:=true;
  407. end;
  408. if change then
  409. begin
  410. track_state_pass:=true;
  411. {Force new resulttype pass.}
  412. condition.resulttype.def:=nil;
  413. do_resulttypepass(condition);
  414. end;
  415. if is_constboolnode(condition) then
  416. begin
  417. {Try to turn a while loop into a repeat loop.}
  418. if firsttest then
  419. exclude(flags,testatbegin);
  420. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  421. if value then
  422. begin
  423. if code.track_state_pass(exec_known) then
  424. track_state_pass:=true;
  425. end
  426. else
  427. done:=true;
  428. end
  429. else
  430. begin
  431. {Remove any modified variables from the state.}
  432. code.track_state_pass(false);
  433. done:=true;
  434. end;
  435. code.destroy;
  436. condition.destroy;
  437. firsttest:=false;
  438. until done;
  439. {The loop condition is also known, for example:
  440. while i<10 do
  441. begin
  442. ...
  443. end;
  444. When the loop is done, we do know that i<10 = false.
  445. }
  446. condition:=left.getcopy;
  447. if condition.track_state_pass(exec_known) then
  448. begin
  449. track_state_pass:=true;
  450. {Force new resulttype pass.}
  451. condition.resulttype.def:=nil;
  452. do_resulttypepass(condition);
  453. end;
  454. if not is_constboolnode(condition) then
  455. aktstate.store_fact(condition,
  456. cordconstnode.create(byte(checknegate),booltype,true))
  457. else
  458. condition.destroy;
  459. end;
  460. {$endif}
  461. {*****************************************************************************
  462. TIFNODE
  463. *****************************************************************************}
  464. constructor tifnode.create(l,r,_t1 : tnode);
  465. begin
  466. inherited create(ifn,l,r,_t1,nil);
  467. end;
  468. function tifnode.det_resulttype:tnode;
  469. begin
  470. result:=nil;
  471. resulttype:=voidtype;
  472. resulttypepass(left);
  473. { if path }
  474. if assigned(right) then
  475. resulttypepass(right);
  476. { else path }
  477. if assigned(t1) then
  478. resulttypepass(t1);
  479. set_varstate(left,vs_used,[vsf_must_be_valid]);
  480. if codegenerror then
  481. exit;
  482. if not is_boolean(left.resulttype.def) then
  483. begin
  484. if left.resulttype.def.deftype=variantdef then
  485. inserttypeconv(left,booltype)
  486. else
  487. Message1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  488. end;
  489. { optimize constant expressions }
  490. if left.nodetype=ordconstn then
  491. begin
  492. if tordconstnode(left).value=1 then
  493. begin
  494. if assigned(right) then
  495. result:=right
  496. else
  497. result:=cnothingnode.create;
  498. right:=nil;
  499. if assigned(t1) then
  500. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  501. end
  502. else
  503. begin
  504. if assigned(t1) then
  505. result:=t1
  506. else
  507. result:=cnothingnode.create;
  508. t1:=nil;
  509. if assigned(right) then
  510. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  511. end;
  512. end;
  513. end;
  514. function tifnode.pass_1 : tnode;
  515. var
  516. old_t_times : longint;
  517. begin
  518. result:=nil;
  519. expectloc:=LOC_VOID;
  520. old_t_times:=cg.t_times;
  521. firstpass(left);
  522. registersint:=left.registersint;
  523. registersfpu:=left.registersfpu;
  524. {$ifdef SUPPORT_MMX}
  525. registersmmx:=left.registersmmx;
  526. {$endif SUPPORT_MMX}
  527. { determines registers weigths }
  528. if not(cs_littlesize in aktglobalswitches) then
  529. cg.t_times:=cg.t_times div 2;
  530. if cg.t_times=0 then
  531. cg.t_times:=1;
  532. { if path }
  533. if assigned(right) then
  534. begin
  535. firstpass(right);
  536. if registersint<right.registersint then
  537. registersint:=right.registersint;
  538. if registersfpu<right.registersfpu then
  539. registersfpu:=right.registersfpu;
  540. {$ifdef SUPPORT_MMX}
  541. if registersmmx<right.registersmmx then
  542. registersmmx:=right.registersmmx;
  543. {$endif SUPPORT_MMX}
  544. end;
  545. { else path }
  546. if assigned(t1) then
  547. begin
  548. firstpass(t1);
  549. if registersint<t1.registersint then
  550. registersint:=t1.registersint;
  551. if registersfpu<t1.registersfpu then
  552. registersfpu:=t1.registersfpu;
  553. {$ifdef SUPPORT_MMX}
  554. if registersmmx<t1.registersmmx then
  555. registersmmx:=t1.registersmmx;
  556. {$endif SUPPORT_MMX}
  557. end;
  558. { leave if we've got an error in one of the paths }
  559. if codegenerror then
  560. exit;
  561. cg.t_times:=old_t_times;
  562. end;
  563. {*****************************************************************************
  564. TFORNODE
  565. *****************************************************************************}
  566. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  567. begin
  568. inherited create(forn,l,r,_t1,_t2);
  569. if back then
  570. include(loopflags,lnf_backward);
  571. include(loopflags,lnf_testatbegin);
  572. end;
  573. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  574. symbol:Tsym);
  575. begin
  576. {If there is a read access, the value of the loop counter is important;
  577. at the end of the loop the loop variable should contain the value it
  578. had in the last iteration.}
  579. if not_type=vn_onwrite then
  580. begin
  581. writeln('Loopvar does not matter on exit');
  582. end
  583. else
  584. begin
  585. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  586. writeln('Loopvar does matter on exit');
  587. end;
  588. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  589. end;
  590. function tfornode.det_resulttype:tnode;
  591. var
  592. unrollres : tnode;
  593. begin
  594. result:=nil;
  595. resulttype:=voidtype;
  596. { loop unrolling }
  597. if cs_loopunroll in aktglobalswitches then
  598. begin
  599. unrollres:=unroll_loop(self);
  600. if assigned(unrollres) then
  601. begin
  602. resulttypepass(unrollres);
  603. result:=unrollres;
  604. exit;
  605. end;
  606. end;
  607. { process the loopvar, from and to, varstates are already set }
  608. resulttypepass(left);
  609. resulttypepass(right);
  610. resulttypepass(t1);
  611. {Can we spare the first comparision?}
  612. if (t1.nodetype=ordconstn) and
  613. (right.nodetype=ordconstn) and
  614. (
  615. (
  616. (lnf_backward in loopflags) and
  617. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  618. ) or
  619. (
  620. not(lnf_backward in loopflags) and
  621. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  622. )
  623. ) then
  624. exclude(loopflags,lnf_testatbegin);
  625. { Make sure that the loop var and the
  626. from and to values are compatible types }
  627. inserttypeconv(right,left.resulttype);
  628. inserttypeconv(t1,left.resulttype);
  629. if assigned(t2) then
  630. resulttypepass(t2);
  631. end;
  632. function tfornode.pass_1 : tnode;
  633. var
  634. old_t_times : longint;
  635. begin
  636. result:=nil;
  637. expectloc:=LOC_VOID;
  638. firstpass(left);
  639. if left.registersint>registersint then
  640. registersint:=left.registersint;
  641. if left.registersfpu>registersfpu then
  642. registersfpu:=left.registersfpu;
  643. {$ifdef SUPPORT_MMX}
  644. if left.registersmmx>registersmmx then
  645. registersmmx:=left.registersmmx;
  646. {$endif SUPPORT_MMX}
  647. firstpass(right);
  648. if right.registersint>registersint then
  649. registersint:=right.registersint;
  650. if right.registersfpu>registersfpu then
  651. registersfpu:=right.registersfpu;
  652. {$ifdef SUPPORT_MMX}
  653. if right.registersmmx>registersmmx then
  654. registersmmx:=right.registersmmx;
  655. {$endif SUPPORT_MMX}
  656. firstpass(t1);
  657. if t1.registersint>registersint then
  658. registersint:=t1.registersint;
  659. if t1.registersfpu>registersfpu then
  660. registersfpu:=t1.registersfpu;
  661. {$ifdef SUPPORT_MMX}
  662. if t1.registersmmx>registersmmx then
  663. registersmmx:=t1.registersmmx;
  664. {$endif SUPPORT_MMX}
  665. if assigned(t2) then
  666. begin
  667. { Calc register weight }
  668. old_t_times:=cg.t_times;
  669. if not(cs_littlesize in aktglobalswitches) then
  670. cg.t_times:=cg.t_times*8;
  671. firstpass(t2);
  672. if codegenerror then
  673. exit;
  674. if t2.registersint>registersint then
  675. registersint:=t2.registersint;
  676. if t2.registersfpu>registersfpu then
  677. registersfpu:=t2.registersfpu;
  678. {$ifdef SUPPORT_MMX}
  679. if t2.registersmmx>registersmmx then
  680. registersmmx:=t2.registersmmx;
  681. {$endif SUPPORT_MMX}
  682. cg.t_times:=old_t_times;
  683. end;
  684. { we need at least one register for comparisons PM }
  685. if registersint=0 then
  686. inc(registersint);
  687. end;
  688. {*****************************************************************************
  689. TEXITNODE
  690. *****************************************************************************}
  691. constructor texitnode.create(l:tnode);
  692. begin
  693. inherited create(exitn,l);
  694. end;
  695. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  696. begin
  697. inherited ppuload(t,ppufile);
  698. end;
  699. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  700. begin
  701. inherited ppuwrite(ppufile);
  702. end;
  703. function texitnode.det_resulttype:tnode;
  704. begin
  705. result:=nil;
  706. if assigned(left) then
  707. begin
  708. { add assignment to funcretsym }
  709. inserttypeconv(left,current_procinfo.procdef.rettype);
  710. left:=cassignmentnode.create(
  711. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  712. left);
  713. resulttypepass(left);
  714. set_varstate(left,vs_used,[vsf_must_be_valid]);
  715. end;
  716. resulttype:=voidtype;
  717. end;
  718. function texitnode.pass_1 : tnode;
  719. begin
  720. result:=nil;
  721. expectloc:=LOC_VOID;
  722. if assigned(left) then
  723. begin
  724. firstpass(left);
  725. if codegenerror then
  726. exit;
  727. registersint:=left.registersint;
  728. registersfpu:=left.registersfpu;
  729. {$ifdef SUPPORT_MMX}
  730. registersmmx:=left.registersmmx;
  731. {$endif SUPPORT_MMX}
  732. end;
  733. end;
  734. {*****************************************************************************
  735. TBREAKNODE
  736. *****************************************************************************}
  737. constructor tbreaknode.create;
  738. begin
  739. inherited create(breakn);
  740. end;
  741. function tbreaknode.det_resulttype:tnode;
  742. begin
  743. result:=nil;
  744. resulttype:=voidtype;
  745. end;
  746. function tbreaknode.pass_1 : tnode;
  747. begin
  748. result:=nil;
  749. expectloc:=LOC_VOID;
  750. end;
  751. {*****************************************************************************
  752. TCONTINUENODE
  753. *****************************************************************************}
  754. constructor tcontinuenode.create;
  755. begin
  756. inherited create(continuen);
  757. end;
  758. function tcontinuenode.det_resulttype:tnode;
  759. begin
  760. result:=nil;
  761. resulttype:=voidtype;
  762. end;
  763. function tcontinuenode.pass_1 : tnode;
  764. begin
  765. result:=nil;
  766. expectloc:=LOC_VOID;
  767. end;
  768. {*****************************************************************************
  769. TGOTONODE
  770. *****************************************************************************}
  771. constructor tgotonode.create(p : tlabelnode);
  772. begin
  773. inherited create(goton);
  774. exceptionblock:=aktexceptblock;
  775. labelnode:=p;
  776. labelsym:=nil;
  777. end;
  778. constructor tgotonode.create_sym(p : tlabelsym);
  779. begin
  780. inherited create(goton);
  781. exceptionblock:=aktexceptblock;
  782. if assigned(p.code) then
  783. labelnode:=tlabelnode(p.code)
  784. else
  785. labelnode:=nil;
  786. labelsym:=p;
  787. end;
  788. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  789. begin
  790. inherited ppuload(t,ppufile);
  791. labelnode:=tlabelnode(ppuloadnoderef(ppufile));
  792. exceptionblock:=ppufile.getbyte;
  793. end;
  794. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  795. begin
  796. inherited ppuwrite(ppufile);
  797. ppuwritenoderef(ppufile,labelnode);
  798. ppufile.putbyte(exceptionblock);
  799. end;
  800. procedure tgotonode.buildderefimpl;
  801. begin
  802. inherited buildderefimpl;
  803. //!!! deref(labelnode);
  804. end;
  805. procedure tgotonode.derefimpl;
  806. begin
  807. inherited derefimpl;
  808. //!!! deref(labelnode);
  809. end;
  810. function tgotonode.det_resulttype:tnode;
  811. begin
  812. result:=nil;
  813. resulttype:=voidtype;
  814. end;
  815. function tgotonode.pass_1 : tnode;
  816. begin
  817. result:=nil;
  818. expectloc:=LOC_VOID;
  819. if not(assigned(labelnode)) then
  820. begin
  821. if assigned(labelsym.code) then
  822. labelnode:=tlabelnode(labelsym.code)
  823. else
  824. internalerror(200506183);
  825. end;
  826. { check if we don't mess with exception blocks }
  827. if assigned(labelnode) and
  828. (exceptionblock<>labelnode.exceptionblock) then
  829. CGMessage(cg_e_goto_inout_of_exception_block);
  830. end;
  831. function tgotonode._getcopy : tnode;
  832. var
  833. p : tgotonode;
  834. i : aint;
  835. begin
  836. p:=tgotonode(inherited _getcopy);
  837. {
  838. p.exceptionblock:=exceptionblock;
  839. { When we copying, we do an ugly trick to determine if the label used
  840. by the current goto node is already copied: if the referinggotonodes
  841. contains the current label, it isn't copied yet, so copy also the
  842. label node and set the copiedto field to the newly created node.
  843. If a label to copy is reached the copiedto field is checked. If it's non nil
  844. the copiedto field is returned and the copiedto field is reset to nil.
  845. }
  846. { assume no copying }
  847. newlabelnode:=labelnode;
  848. for i:=0 to labelnode.copiedto.referingotonodes.count-1 do
  849. begin
  850. { copy labelnode? }
  851. if labelnode.copiedto.referinggotonodes[i]=self then
  852. begin
  853. oldlabelnode.copiedto:=newlabelnode;
  854. end;
  855. end;
  856. p.labelnode:=newlabelnode;
  857. p.labelnode.referinggotonodes.add(self);
  858. }
  859. result:=p;
  860. end;
  861. function tgotonode.docompare(p: tnode): boolean;
  862. begin
  863. docompare := false;
  864. end;
  865. {*****************************************************************************
  866. TLABELNODE
  867. *****************************************************************************}
  868. constructor tlabelnode.create(l:tnode);
  869. begin
  870. inherited create(labeln,l);
  871. exceptionblock:=aktexceptblock;
  872. end;
  873. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  874. begin
  875. inherited ppuload(t,ppufile);
  876. exceptionblock:=ppufile.getbyte;
  877. end;
  878. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  879. begin
  880. inherited ppuwrite(ppufile);
  881. ppufile.putbyte(exceptionblock);
  882. end;
  883. procedure tlabelnode.buildderefimpl;
  884. begin
  885. inherited buildderefimpl;
  886. end;
  887. procedure tlabelnode.derefimpl;
  888. begin
  889. inherited derefimpl;
  890. end;
  891. function tlabelnode.det_resulttype:tnode;
  892. begin
  893. result:=nil;
  894. { left could still be unassigned }
  895. if assigned(left) then
  896. resulttypepass(left);
  897. resulttype:=voidtype;
  898. end;
  899. function tlabelnode.pass_1 : tnode;
  900. begin
  901. result:=nil;
  902. expectloc:=LOC_VOID;
  903. if assigned(left) then
  904. begin
  905. firstpass(left);
  906. registersint:=left.registersint;
  907. registersfpu:=left.registersfpu;
  908. {$ifdef SUPPORT_MMX}
  909. registersmmx:=left.registersmmx;
  910. {$endif SUPPORT_MMX}
  911. end;
  912. end;
  913. function tlabelnode._getcopy : tnode;
  914. var
  915. p : tlabelnode;
  916. begin
  917. p:=tlabelnode(inherited _getcopy);
  918. p.exceptionblock:=exceptionblock;
  919. result:=p;
  920. end;
  921. function tlabelnode.docompare(p: tnode): boolean;
  922. begin
  923. docompare := false;
  924. end;
  925. {*****************************************************************************
  926. TRAISENODE
  927. *****************************************************************************}
  928. constructor traisenode.create(l,taddr,tframe:tnode);
  929. begin
  930. inherited create(raisen,l,taddr);
  931. frametree:=tframe;
  932. end;
  933. constructor traisenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  934. begin
  935. inherited ppuload(t,ppufile);
  936. frametree:=ppuloadnode(ppufile);
  937. end;
  938. procedure traisenode.ppuwrite(ppufile:tcompilerppufile);
  939. begin
  940. inherited ppuwrite(ppufile);
  941. ppuwritenode(ppufile,frametree);
  942. end;
  943. procedure traisenode.buildderefimpl;
  944. begin
  945. inherited buildderefimpl;
  946. if assigned(frametree) then
  947. frametree.buildderefimpl;
  948. end;
  949. procedure traisenode.derefimpl;
  950. begin
  951. inherited derefimpl;
  952. if assigned(frametree) then
  953. frametree.derefimpl;
  954. end;
  955. function traisenode._getcopy : tnode;
  956. var
  957. n : traisenode;
  958. begin
  959. n:=traisenode(inherited _getcopy);
  960. if assigned(frametree) then
  961. n.frametree:=frametree._getcopy
  962. else
  963. n.frametree:=nil;
  964. _getcopy:=n;
  965. end;
  966. procedure traisenode.insertintolist(l : tnodelist);
  967. begin
  968. end;
  969. function traisenode.det_resulttype:tnode;
  970. begin
  971. result:=nil;
  972. resulttype:=voidtype;
  973. if assigned(left) then
  974. begin
  975. { first para must be a _class_ }
  976. resulttypepass(left);
  977. set_varstate(left,vs_used,[vsf_must_be_valid]);
  978. if codegenerror then
  979. exit;
  980. if not(is_class(left.resulttype.def)) then
  981. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  982. { insert needed typeconvs for addr,frame }
  983. if assigned(right) then
  984. begin
  985. { addr }
  986. resulttypepass(right);
  987. inserttypeconv(right,voidpointertype);
  988. { frame }
  989. if assigned(frametree) then
  990. begin
  991. resulttypepass(frametree);
  992. inserttypeconv(frametree,voidpointertype);
  993. end;
  994. end;
  995. end;
  996. end;
  997. function traisenode.pass_1 : tnode;
  998. begin
  999. result:=nil;
  1000. include(current_procinfo.flags,pi_do_call);
  1001. expectloc:=LOC_VOID;
  1002. if assigned(left) then
  1003. begin
  1004. { first para must be a _class_ }
  1005. firstpass(left);
  1006. { insert needed typeconvs for addr,frame }
  1007. if assigned(right) then
  1008. begin
  1009. { addr }
  1010. firstpass(right);
  1011. { frame }
  1012. if assigned(frametree) then
  1013. firstpass(frametree);
  1014. end;
  1015. left_right_max;
  1016. end;
  1017. end;
  1018. function traisenode.docompare(p: tnode): boolean;
  1019. begin
  1020. docompare := false;
  1021. end;
  1022. {*****************************************************************************
  1023. TTRYEXCEPTNODE
  1024. *****************************************************************************}
  1025. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  1026. begin
  1027. inherited create(tryexceptn,l,r,_t1,nil);
  1028. end;
  1029. function ttryexceptnode.det_resulttype:tnode;
  1030. begin
  1031. result:=nil;
  1032. resulttypepass(left);
  1033. { on statements }
  1034. if assigned(right) then
  1035. resulttypepass(right);
  1036. { else block }
  1037. if assigned(t1) then
  1038. resulttypepass(t1);
  1039. resulttype:=voidtype;
  1040. end;
  1041. function ttryexceptnode.pass_1 : tnode;
  1042. begin
  1043. result:=nil;
  1044. include(current_procinfo.flags,pi_do_call);
  1045. expectloc:=LOC_VOID;
  1046. firstpass(left);
  1047. { on statements }
  1048. if assigned(right) then
  1049. begin
  1050. firstpass(right);
  1051. registersint:=max(registersint,right.registersint);
  1052. registersfpu:=max(registersfpu,right.registersfpu);
  1053. {$ifdef SUPPORT_MMX}
  1054. registersmmx:=max(registersmmx,right.registersmmx);
  1055. {$endif SUPPORT_MMX}
  1056. end;
  1057. { else block }
  1058. if assigned(t1) then
  1059. begin
  1060. firstpass(t1);
  1061. registersint:=max(registersint,t1.registersint);
  1062. registersfpu:=max(registersfpu,t1.registersfpu);
  1063. {$ifdef SUPPORT_MMX}
  1064. registersmmx:=max(registersmmx,t1.registersmmx);
  1065. {$endif SUPPORT_MMX}
  1066. end;
  1067. end;
  1068. {*****************************************************************************
  1069. TTRYFINALLYNODE
  1070. *****************************************************************************}
  1071. constructor ttryfinallynode.create(l,r:tnode);
  1072. begin
  1073. inherited create(tryfinallyn,l,r,nil,nil);
  1074. implicitframe:=false;
  1075. end;
  1076. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1077. begin
  1078. inherited create(tryfinallyn,l,r,_t1,nil);
  1079. implicitframe:=true;
  1080. end;
  1081. function ttryfinallynode.det_resulttype:tnode;
  1082. begin
  1083. result:=nil;
  1084. include(current_procinfo.flags,pi_do_call);
  1085. resulttype:=voidtype;
  1086. resulttypepass(left);
  1087. set_varstate(left,vs_used,[vsf_must_be_valid]);
  1088. resulttypepass(right);
  1089. set_varstate(right,vs_used,[vsf_must_be_valid]);
  1090. { special finally block only executed when there was an exception }
  1091. if assigned(t1) then
  1092. begin
  1093. resulttypepass(t1);
  1094. set_varstate(t1,vs_used,[vsf_must_be_valid]);
  1095. end;
  1096. end;
  1097. function ttryfinallynode.pass_1 : tnode;
  1098. begin
  1099. result:=nil;
  1100. expectloc:=LOC_VOID;
  1101. firstpass(left);
  1102. firstpass(right);
  1103. left_right_max;
  1104. if assigned(t1) then
  1105. begin
  1106. firstpass(t1);
  1107. registersint:=max(registersint,t1.registersint);
  1108. registersfpu:=max(registersfpu,t1.registersfpu);
  1109. {$ifdef SUPPORT_MMX}
  1110. registersmmx:=max(registersmmx,t1.registersmmx);
  1111. {$endif SUPPORT_MMX}
  1112. end;
  1113. end;
  1114. {*****************************************************************************
  1115. TONNODE
  1116. *****************************************************************************}
  1117. constructor tonnode.create(l,r:tnode);
  1118. begin
  1119. inherited create(onn,l,r);
  1120. exceptsymtable:=nil;
  1121. excepttype:=nil;
  1122. end;
  1123. destructor tonnode.destroy;
  1124. begin
  1125. { copied nodes don't need to release the symtable }
  1126. if assigned(exceptsymtable) then
  1127. exceptsymtable.free;
  1128. inherited destroy;
  1129. end;
  1130. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1131. begin
  1132. inherited ppuload(t,ppufile);
  1133. exceptsymtable:=nil;
  1134. excepttype:=nil;
  1135. end;
  1136. function tonnode._getcopy : tnode;
  1137. var
  1138. n : tonnode;
  1139. begin
  1140. n:=tonnode(inherited _getcopy);
  1141. n.exceptsymtable:=exceptsymtable.getcopy;
  1142. n.excepttype:=excepttype;
  1143. result:=n;
  1144. end;
  1145. function tonnode.det_resulttype:tnode;
  1146. begin
  1147. result:=nil;
  1148. resulttype:=voidtype;
  1149. if not(is_class(excepttype)) then
  1150. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1151. if assigned(left) then
  1152. resulttypepass(left);
  1153. if assigned(right) then
  1154. resulttypepass(right);
  1155. end;
  1156. function tonnode.pass_1 : tnode;
  1157. begin
  1158. result:=nil;
  1159. include(current_procinfo.flags,pi_do_call);
  1160. expectloc:=LOC_VOID;
  1161. registersint:=0;
  1162. registersfpu:=0;
  1163. {$ifdef SUPPORT_MMX}
  1164. registersmmx:=0;
  1165. {$endif SUPPORT_MMX}
  1166. if assigned(left) then
  1167. begin
  1168. firstpass(left);
  1169. registersint:=left.registersint;
  1170. registersfpu:=left.registersfpu;
  1171. {$ifdef SUPPORT_MMX}
  1172. registersmmx:=left.registersmmx;
  1173. {$endif SUPPORT_MMX}
  1174. end;
  1175. if assigned(right) then
  1176. begin
  1177. firstpass(right);
  1178. registersint:=max(registersint,right.registersint);
  1179. registersfpu:=max(registersfpu,right.registersfpu);
  1180. {$ifdef SUPPORT_MMX}
  1181. registersmmx:=max(registersmmx,right.registersmmx);
  1182. {$endif SUPPORT_MMX}
  1183. end;
  1184. end;
  1185. function tonnode.docompare(p: tnode): boolean;
  1186. begin
  1187. docompare := false;
  1188. end;
  1189. begin
  1190. cwhilerepeatnode:=twhilerepeatnode;
  1191. cifnode:=tifnode;
  1192. cfornode:=tfornode;
  1193. cexitnode:=texitnode;
  1194. cgotonode:=tgotonode;
  1195. clabelnode:=tlabelnode;
  1196. craisenode:=traisenode;
  1197. ctryexceptnode:=ttryexceptnode;
  1198. ctryfinallynode:=ttryfinallynode;
  1199. connode:=tonnode;
  1200. end.