nflw.pas 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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 dogetcopy : 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;reintroduce;
  62. function pass_typecheck: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;reintroduce;
  71. function pass_typecheck:tnode;override;
  72. function pass_1 : tnode;override;
  73. function simplify : tnode;override;
  74. private
  75. function internalsimplify(warn: boolean) : tnode;
  76. end;
  77. tifnodeclass = class of tifnode;
  78. tfornode = class(tloopnode)
  79. { if count isn divisable by unrolls then
  80. the for loop must jump to this label to get the correct
  81. number of executions }
  82. entrylabel : tnode;
  83. loopvar_notid:cardinal;
  84. constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;reintroduce;
  85. procedure loop_var_access(not_type:Tnotification_flag;symbol:Tsym);
  86. function pass_typecheck:tnode;override;
  87. function pass_1 : tnode;override;
  88. end;
  89. tfornodeclass = class of tfornode;
  90. texitnode = class(tunarynode)
  91. constructor create(l:tnode);virtual;
  92. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  93. procedure ppuwrite(ppufile:tcompilerppufile);override;
  94. function pass_typecheck:tnode;override;
  95. function pass_1 : tnode;override;
  96. end;
  97. texitnodeclass = class of texitnode;
  98. tbreaknode = class(tnode)
  99. constructor create;virtual;
  100. function pass_typecheck:tnode;override;
  101. function pass_1 : tnode;override;
  102. end;
  103. tbreaknodeclass = class of tbreaknode;
  104. tcontinuenode = class(tnode)
  105. constructor create;virtual;
  106. function pass_typecheck:tnode;override;
  107. function pass_1 : tnode;override;
  108. end;
  109. tcontinuenodeclass = class of tcontinuenode;
  110. tgotonode = class(tnode)
  111. labelsym : tlabelsym;
  112. labelnode : tlabelnode;
  113. exceptionblock : integer;
  114. constructor create(p : tlabelsym);virtual;
  115. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  116. procedure ppuwrite(ppufile:tcompilerppufile);override;
  117. procedure buildderefimpl;override;
  118. procedure derefimpl;override;
  119. function dogetcopy : tnode;override;
  120. function pass_typecheck:tnode;override;
  121. function pass_1 : tnode;override;
  122. function docompare(p: tnode): boolean; override;
  123. end;
  124. tgotonodeclass = class of tgotonode;
  125. tlabelnode = class(tunarynode)
  126. exceptionblock : integer;
  127. { when copying trees, this points to the newly created copy of a label }
  128. copiedto : tlabelnode;
  129. labsym : tlabelsym;
  130. constructor create(l:tnode;alabsym:tlabelsym);virtual;
  131. destructor destroy;override;
  132. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  133. procedure ppuwrite(ppufile:tcompilerppufile);override;
  134. procedure buildderefimpl;override;
  135. procedure derefimpl;override;
  136. function dogetcopy : tnode;override;
  137. function pass_typecheck:tnode;override;
  138. function pass_1 : tnode;override;
  139. function docompare(p: tnode): boolean; override;
  140. end;
  141. tlabelnodeclass = class of tlabelnode;
  142. traisenode = class(ttertiarynode)
  143. constructor create(l,taddr,tframe:tnode);virtual;
  144. function pass_typecheck:tnode;override;
  145. function pass_1 : tnode;override;
  146. end;
  147. traisenodeclass = class of traisenode;
  148. ttryexceptnode = class(tloopnode)
  149. constructor create(l,r,_t1 : tnode);virtual;reintroduce;
  150. function pass_typecheck:tnode;override;
  151. function pass_1 : tnode;override;
  152. end;
  153. ttryexceptnodeclass = class of ttryexceptnode;
  154. ttryfinallynode = class(tloopnode)
  155. implicitframe : boolean;
  156. constructor create(l,r:tnode);virtual;reintroduce;
  157. constructor create_implicit(l,r,_t1:tnode);virtual;
  158. function pass_typecheck:tnode;override;
  159. function pass_1 : tnode;override;
  160. end;
  161. ttryfinallynodeclass = class of ttryfinallynode;
  162. tonnode = class(tbinarynode)
  163. excepTSymtable : TSymtable;
  164. excepttype : tobjectdef;
  165. constructor create(l,r:tnode);virtual;
  166. destructor destroy;override;
  167. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  168. function pass_typecheck:tnode;override;
  169. function pass_1 : tnode;override;
  170. function dogetcopy : tnode;override;
  171. function docompare(p: tnode): boolean; override;
  172. end;
  173. tonnodeclass = class of tonnode;
  174. var
  175. cwhilerepeatnode : twhilerepeatnodeclass;
  176. cifnode : tifnodeclass;
  177. cfornode : tfornodeclass;
  178. cexitnode : texitnodeclass;
  179. cbreaknode : tbreaknodeclass;
  180. ccontinuenode : tcontinuenodeclass;
  181. cgotonode : tgotonodeclass;
  182. clabelnode : tlabelnodeclass;
  183. craisenode : traisenodeclass;
  184. ctryexceptnode : ttryexceptnodeclass;
  185. ctryfinallynode : ttryfinallynodeclass;
  186. connode : tonnodeclass;
  187. implementation
  188. uses
  189. globtype,systems,constexp,
  190. cutils,verbose,globals,
  191. symconst,paramgr,defcmp,defutil,htypechk,pass_1,
  192. ncal,nadd,ncon,nmem,nld,ncnv,nbas,cgobj,nutils,
  193. {$ifdef prefetchnext}
  194. ninl,
  195. {$endif prefetchnext}
  196. {$ifdef state_tracking}
  197. nstate,
  198. {$endif}
  199. cgbase,procinfo
  200. ;
  201. {****************************************************************************
  202. TLOOPNODE
  203. *****************************************************************************}
  204. constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
  205. begin
  206. inherited create(tt,l,r);
  207. t1:=_t1;
  208. t2:=_t2;
  209. fileinfo:=l.fileinfo;
  210. end;
  211. destructor tloopnode.destroy;
  212. begin
  213. t1.free;
  214. t2.free;
  215. inherited destroy;
  216. end;
  217. constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  218. begin
  219. inherited ppuload(t,ppufile);
  220. t1:=ppuloadnode(ppufile);
  221. t2:=ppuloadnode(ppufile);
  222. end;
  223. procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
  224. begin
  225. inherited ppuwrite(ppufile);
  226. ppuwritenode(ppufile,t1);
  227. ppuwritenode(ppufile,t2);
  228. end;
  229. procedure tloopnode.buildderefimpl;
  230. begin
  231. inherited buildderefimpl;
  232. if assigned(t1) then
  233. t1.buildderefimpl;
  234. if assigned(t2) then
  235. t2.buildderefimpl;
  236. end;
  237. procedure tloopnode.derefimpl;
  238. begin
  239. inherited derefimpl;
  240. if assigned(t1) then
  241. t1.derefimpl;
  242. if assigned(t2) then
  243. t2.derefimpl;
  244. end;
  245. function tloopnode.dogetcopy : tnode;
  246. var
  247. p : tloopnode;
  248. begin
  249. p:=tloopnode(inherited dogetcopy);
  250. if assigned(t1) then
  251. p.t1:=t1.dogetcopy
  252. else
  253. p.t1:=nil;
  254. if assigned(t2) then
  255. p.t2:=t2.dogetcopy
  256. else
  257. p.t2:=nil;
  258. p.loopflags:=loopflags;
  259. dogetcopy:=p;
  260. end;
  261. procedure tloopnode.insertintolist(l : tnodelist);
  262. begin
  263. end;
  264. procedure tloopnode.printnodetree(var t:text);
  265. begin
  266. write(t,printnodeindention,'(');
  267. printnodeindent;
  268. printnodeinfo(t);
  269. writeln(t);
  270. printnode(t,left);
  271. printnode(t,right);
  272. printnode(t,t1);
  273. printnode(t,t2);
  274. printnodeunindent;
  275. writeln(t,printnodeindention,')');
  276. end;
  277. function tloopnode.docompare(p: tnode): boolean;
  278. begin
  279. docompare :=
  280. inherited docompare(p) and
  281. (loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
  282. t1.isequal(tloopnode(p).t1) and
  283. t2.isequal(tloopnode(p).t2);
  284. end;
  285. {****************************************************************************
  286. TWHILEREPEATNODE
  287. *****************************************************************************}
  288. constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
  289. begin
  290. inherited create(whilerepeatn,l,r,nil,nil);
  291. if tab then
  292. include(loopflags, lnf_testatbegin);
  293. if cn then
  294. include(loopflags,lnf_checknegate);
  295. end;
  296. function twhilerepeatnode.pass_typecheck:tnode;
  297. var
  298. t:Tunarynode;
  299. begin
  300. result:=nil;
  301. resultdef:=voidtype;
  302. typecheckpass(left);
  303. { tp procvar support }
  304. maybe_call_procvar(left,true);
  305. {A not node can be removed.}
  306. if left.nodetype=notn then
  307. begin
  308. t:=Tunarynode(left);
  309. left:=Tunarynode(left).left;
  310. t.left:=nil;
  311. t.destroy;
  312. {Symdif operator, in case you are wondering:}
  313. loopflags:=loopflags >< [lnf_checknegate];
  314. end;
  315. { loop instruction }
  316. if assigned(right) then
  317. typecheckpass(right);
  318. set_varstate(left,vs_read,[vsf_must_be_valid]);
  319. if codegenerror then
  320. exit;
  321. if not is_boolean(left.resultdef) then
  322. begin
  323. if left.resultdef.typ=variantdef then
  324. inserttypeconv(left,booltype)
  325. else
  326. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  327. end;
  328. { Give warnings for code that will never be executed for
  329. while false do }
  330. if (lnf_testatbegin in loopflags) and
  331. (left.nodetype=ordconstn) and
  332. (tordconstnode(left).value.uvalue=0) and
  333. assigned(right) then
  334. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  335. end;
  336. {$ifdef prefetchnext}
  337. type
  338. passignmentquery = ^tassignmentquery;
  339. tassignmentquery = record
  340. towhat: tnode;
  341. source: tassignmentnode;
  342. statementcount: cardinal;
  343. end;
  344. function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
  345. var
  346. query: passignmentquery absolute arg;
  347. temp, prederef: tnode;
  348. begin
  349. result := fen_norecurse_false;
  350. if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
  351. inc(query^.statementcount);
  352. { make sure there's something else in the loop besides going to the }
  353. { next item }
  354. if (query^.statementcount > 1) and
  355. (n.nodetype = assignn) then
  356. begin
  357. { skip type conversions of assignment target }
  358. temp := tassignmentnode(n).left;
  359. while (temp.nodetype = typeconvn) do
  360. temp := ttypeconvnode(temp).left;
  361. { assignment to x of the while assigned(x) check? }
  362. if not(temp.isequal(query^.towhat)) then
  363. exit;
  364. { right hand side of assignment dereferenced field of }
  365. { x? (no derefn in case of class) }
  366. temp := tassignmentnode(n).right;
  367. while (temp.nodetype = typeconvn) do
  368. temp := ttypeconvnode(temp).left;
  369. if (temp.nodetype <> subscriptn) then
  370. exit;
  371. prederef := tsubscriptnode(temp).left;
  372. temp := prederef;
  373. while (temp.nodetype = typeconvn) do
  374. temp := ttypeconvnode(temp).left;
  375. { see tests/test/prefetch1.pp }
  376. if (temp.nodetype = derefn) then
  377. temp := tderefnode(temp).left
  378. else
  379. temp := prederef;
  380. if temp.isequal(query^.towhat) then
  381. begin
  382. query^.source := tassignmentnode(n);
  383. result := fen_norecurse_true;
  384. end
  385. end
  386. { don't check nodes which can't contain an assignment or whose }
  387. { final assignment can vary a lot }
  388. else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
  389. result := fen_false;
  390. end;
  391. function findassignment(where: tnode; towhat: tnode): tassignmentnode;
  392. var
  393. query: tassignmentquery;
  394. begin
  395. query.towhat := towhat;
  396. query.source := nil;
  397. query.statementcount := 0;
  398. if foreachnodestatic(where,@checkassignment,@query) then
  399. result := query.source
  400. else
  401. result := nil;
  402. end;
  403. {$endif prefetchnext}
  404. function twhilerepeatnode.pass_1 : tnode;
  405. {$ifdef prefetchnext}
  406. var
  407. runnernode, prefetchcode: tnode;
  408. assignmentnode: tassignmentnode;
  409. prefetchstatements: tstatementnode;
  410. {$endif prefetchnext}
  411. begin
  412. result:=nil;
  413. expectloc:=LOC_VOID;
  414. firstpass(left);
  415. if codegenerror then
  416. exit;
  417. { loop instruction }
  418. if assigned(right) then
  419. begin
  420. firstpass(right);
  421. if codegenerror then
  422. exit;
  423. end;
  424. {$ifdef prefetchnext}
  425. { do at the end so all complex typeconversions are already }
  426. { converted to calln's }
  427. if (cs_opt_level1 in current_settings.optimizerswitches) and
  428. (lnf_testatbegin in loopflags) then
  429. begin
  430. { get first component of the while check }
  431. runnernode := left;
  432. while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
  433. runnernode := tunarynode(runnernode).left;
  434. { is it an assigned(x) check? }
  435. if ((runnernode.nodetype = inlinen) and
  436. (tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
  437. ((runnernode.nodetype = unequaln) and
  438. (taddnode(runnernode).right.nodetype = niln)) then
  439. begin
  440. runnernode := tunarynode(runnernode).left;
  441. { in case of in_assigned_x, there's a callparan in between }
  442. if (runnernode.nodetype = callparan) then
  443. runnernode := tcallparanode(runnernode).left;
  444. while (runnernode.nodetype = typeconvn) do
  445. runnernode := ttypeconvnode(runnernode).left;
  446. { is there an "x := x(^).somefield"? }
  447. assignmentnode := findassignment(right,runnernode);
  448. if assigned(assignmentnode) then
  449. begin
  450. prefetchcode := internalstatements(prefetchstatements);
  451. addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
  452. cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
  453. addstatement(prefetchstatements,right);
  454. right := prefetchcode;
  455. typecheckpass(right);
  456. end;
  457. end;
  458. end;
  459. {$endif prefetchnext}
  460. end;
  461. {$ifdef state_tracking}
  462. function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
  463. var condition:Tnode;
  464. code:Tnode;
  465. done:boolean;
  466. value:boolean;
  467. change:boolean;
  468. firsttest:boolean;
  469. factval:Tnode;
  470. begin
  471. track_state_pass:=false;
  472. done:=false;
  473. firsttest:=true;
  474. {For repeat until statements, first do a pass through the code.}
  475. if not(lnf_testatbegin in flags) then
  476. begin
  477. code:=right.getcopy;
  478. if code.track_state_pass(exec_known) then
  479. track_state_pass:=true;
  480. code.destroy;
  481. end;
  482. repeat
  483. condition:=left.getcopy;
  484. code:=right.getcopy;
  485. change:=condition.track_state_pass(exec_known);
  486. factval:=aktstate.find_fact(left);
  487. if factval<>nil then
  488. begin
  489. condition.destroy;
  490. condition:=factval.getcopy;
  491. change:=true;
  492. end;
  493. if change then
  494. begin
  495. track_state_pass:=true;
  496. {Force new resultdef pass.}
  497. condition.resultdef:=nil;
  498. do_typecheckpass(condition);
  499. end;
  500. if is_constboolnode(condition) then
  501. begin
  502. {Try to turn a while loop into a repeat loop.}
  503. if firsttest then
  504. exclude(flags,testatbegin);
  505. value:=(Tordconstnode(condition).value<>0) xor checknegate;
  506. if value then
  507. begin
  508. if code.track_state_pass(exec_known) then
  509. track_state_pass:=true;
  510. end
  511. else
  512. done:=true;
  513. end
  514. else
  515. begin
  516. {Remove any modified variables from the state.}
  517. code.track_state_pass(false);
  518. done:=true;
  519. end;
  520. code.destroy;
  521. condition.destroy;
  522. firsttest:=false;
  523. until done;
  524. {The loop condition is also known, for example:
  525. while i<10 do
  526. begin
  527. ...
  528. end;
  529. When the loop is done, we do know that i<10 = false.
  530. }
  531. condition:=left.getcopy;
  532. if condition.track_state_pass(exec_known) then
  533. begin
  534. track_state_pass:=true;
  535. {Force new resultdef pass.}
  536. condition.resultdef:=nil;
  537. do_typecheckpass(condition);
  538. end;
  539. if not is_constboolnode(condition) then
  540. aktstate.store_fact(condition,
  541. cordconstnode.create(byte(checknegate),booltype,true))
  542. else
  543. condition.destroy;
  544. end;
  545. {$endif}
  546. {*****************************************************************************
  547. TIFNODE
  548. *****************************************************************************}
  549. constructor tifnode.create(l,r,_t1 : tnode);
  550. begin
  551. inherited create(ifn,l,r,_t1,nil);
  552. end;
  553. function tifnode.internalsimplify(warn: boolean) : tnode;
  554. begin
  555. result:=nil;
  556. { optimize constant expressions }
  557. if (left.nodetype=ordconstn) then
  558. begin
  559. if tordconstnode(left).value.uvalue=1 then
  560. begin
  561. if assigned(right) then
  562. result:=right
  563. else
  564. result:=cnothingnode.create;
  565. right:=nil;
  566. if warn and assigned(t1) then
  567. CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
  568. end
  569. else
  570. begin
  571. if assigned(t1) then
  572. result:=t1
  573. else
  574. result:=cnothingnode.create;
  575. t1:=nil;
  576. if warn and assigned(right) then
  577. CGMessagePos(right.fileinfo,cg_w_unreachable_code);
  578. end;
  579. end;
  580. end;
  581. function tifnode.simplify : tnode;
  582. begin
  583. result:=internalsimplify(false);
  584. end;
  585. function tifnode.pass_typecheck:tnode;
  586. begin
  587. result:=nil;
  588. resultdef:=voidtype;
  589. typecheckpass(left);
  590. { tp procvar support }
  591. maybe_call_procvar(left,true);
  592. { if path }
  593. if assigned(right) then
  594. typecheckpass(right);
  595. { else path }
  596. if assigned(t1) then
  597. typecheckpass(t1);
  598. set_varstate(left,vs_read,[vsf_must_be_valid]);
  599. if codegenerror then
  600. exit;
  601. if not is_boolean(left.resultdef) then
  602. begin
  603. if left.resultdef.typ=variantdef then
  604. inserttypeconv(left,booltype)
  605. else
  606. Message1(type_e_boolean_expr_expected,left.resultdef.typename);
  607. end;
  608. result:=internalsimplify(true);
  609. end;
  610. function tifnode.pass_1 : tnode;
  611. begin
  612. result:=nil;
  613. expectloc:=LOC_VOID;
  614. firstpass(left);
  615. { if path }
  616. if assigned(right) then
  617. firstpass(right);
  618. { else path }
  619. if assigned(t1) then
  620. firstpass(t1);
  621. { leave if we've got an error in one of the paths }
  622. if codegenerror then
  623. exit;
  624. end;
  625. {*****************************************************************************
  626. TFORNODE
  627. *****************************************************************************}
  628. constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
  629. begin
  630. inherited create(forn,l,r,_t1,_t2);
  631. if back then
  632. include(loopflags,lnf_backward);
  633. include(loopflags,lnf_testatbegin);
  634. end;
  635. procedure Tfornode.loop_var_access(not_type:Tnotification_flag;
  636. symbol:Tsym);
  637. begin
  638. {If there is a read access, the value of the loop counter is important;
  639. at the end of the loop the loop variable should contain the value it
  640. had in the last iteration.}
  641. if not_type=vn_onwrite then
  642. begin
  643. writeln('Loopvar does not matter on exit');
  644. end
  645. else
  646. begin
  647. exclude(loopflags,lnf_dont_mind_loopvar_on_exit);
  648. writeln('Loopvar does matter on exit');
  649. end;
  650. Tabstractvarsym(symbol).unregister_notification(loopvar_notid);
  651. end;
  652. function tfornode.pass_typecheck:tnode;
  653. var
  654. unrollres : tnode;
  655. begin
  656. result:=nil;
  657. resultdef:=voidtype;
  658. { loop unrolling }
  659. if cs_opt_loopunroll in current_settings.optimizerswitches then
  660. begin
  661. unrollres:=unroll_loop(self);
  662. if assigned(unrollres) then
  663. begin
  664. typecheckpass(unrollres);
  665. result:=unrollres;
  666. exit;
  667. end;
  668. end;
  669. { process the loopvar, from and to, varstates are already set }
  670. typecheckpass(left);
  671. typecheckpass(right);
  672. typecheckpass(t1);
  673. {Can we spare the first comparision?}
  674. if (t1.nodetype=ordconstn) and
  675. (right.nodetype=ordconstn) and
  676. (
  677. (
  678. (lnf_backward in loopflags) and
  679. (Tordconstnode(right).value>=Tordconstnode(t1).value)
  680. ) or
  681. (
  682. not(lnf_backward in loopflags) and
  683. (Tordconstnode(right).value<=Tordconstnode(t1).value)
  684. )
  685. ) then
  686. exclude(loopflags,lnf_testatbegin);
  687. { Make sure that the loop var and the
  688. from and to values are compatible types }
  689. check_ranges(right.fileinfo,right,left.resultdef);
  690. inserttypeconv(right,left.resultdef);
  691. check_ranges(t1.fileinfo,t1,left.resultdef);
  692. inserttypeconv(t1,left.resultdef);
  693. if assigned(t2) then
  694. typecheckpass(t2);
  695. end;
  696. function tfornode.pass_1 : tnode;
  697. begin
  698. result:=nil;
  699. expectloc:=LOC_VOID;
  700. firstpass(left);
  701. firstpass(right);
  702. firstpass(t1);
  703. if assigned(t2) then
  704. begin
  705. firstpass(t2);
  706. if codegenerror then
  707. exit;
  708. end;
  709. end;
  710. {*****************************************************************************
  711. TEXITNODE
  712. *****************************************************************************}
  713. constructor texitnode.create(l:tnode);
  714. begin
  715. inherited create(exitn,l);
  716. end;
  717. constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  718. begin
  719. inherited ppuload(t,ppufile);
  720. end;
  721. procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
  722. begin
  723. inherited ppuwrite(ppufile);
  724. end;
  725. function texitnode.pass_typecheck:tnode;
  726. begin
  727. result:=nil;
  728. if assigned(left) then
  729. begin
  730. { add assignment to funcretsym }
  731. inserttypeconv(left,current_procinfo.procdef.returndef);
  732. left:=cassignmentnode.create(
  733. cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
  734. left);
  735. typecheckpass(left);
  736. set_varstate(left,vs_read,[vsf_must_be_valid]);
  737. end;
  738. resultdef:=voidtype;
  739. end;
  740. function texitnode.pass_1 : tnode;
  741. begin
  742. result:=nil;
  743. expectloc:=LOC_VOID;
  744. if assigned(left) then
  745. begin
  746. firstpass(left);
  747. if codegenerror then
  748. exit;
  749. end;
  750. end;
  751. {*****************************************************************************
  752. TBREAKNODE
  753. *****************************************************************************}
  754. constructor tbreaknode.create;
  755. begin
  756. inherited create(breakn);
  757. end;
  758. function tbreaknode.pass_typecheck:tnode;
  759. begin
  760. result:=nil;
  761. resultdef:=voidtype;
  762. end;
  763. function tbreaknode.pass_1 : tnode;
  764. begin
  765. result:=nil;
  766. expectloc:=LOC_VOID;
  767. end;
  768. {*****************************************************************************
  769. TCONTINUENODE
  770. *****************************************************************************}
  771. constructor tcontinuenode.create;
  772. begin
  773. inherited create(continuen);
  774. end;
  775. function tcontinuenode.pass_typecheck:tnode;
  776. begin
  777. result:=nil;
  778. resultdef:=voidtype;
  779. end;
  780. function tcontinuenode.pass_1 : tnode;
  781. begin
  782. result:=nil;
  783. expectloc:=LOC_VOID;
  784. end;
  785. {*****************************************************************************
  786. TGOTONODE
  787. *****************************************************************************}
  788. constructor tgotonode.create(p : tlabelsym);
  789. begin
  790. inherited create(goton);
  791. exceptionblock:=aktexceptblock;
  792. labelnode:=nil;
  793. labelsym:=p;
  794. end;
  795. constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  796. begin
  797. inherited ppuload(t,ppufile);
  798. labelnode:=tlabelnode(ppuloadnoderef(ppufile));
  799. exceptionblock:=ppufile.getbyte;
  800. end;
  801. procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
  802. begin
  803. inherited ppuwrite(ppufile);
  804. ppuwritenoderef(ppufile,labelnode);
  805. ppufile.putbyte(exceptionblock);
  806. end;
  807. procedure tgotonode.buildderefimpl;
  808. begin
  809. inherited buildderefimpl;
  810. //!!! deref(labelnode);
  811. end;
  812. procedure tgotonode.derefimpl;
  813. begin
  814. inherited derefimpl;
  815. //!!! deref(labelnode);
  816. end;
  817. function tgotonode.pass_typecheck:tnode;
  818. begin
  819. result:=nil;
  820. resultdef:=voidtype;
  821. end;
  822. function tgotonode.pass_1 : tnode;
  823. begin
  824. result:=nil;
  825. expectloc:=LOC_VOID;
  826. include(current_procinfo.flags,pi_has_goto);
  827. { The labelnode can already be set when
  828. this node was copied }
  829. if not assigned(labelnode) then
  830. begin
  831. if assigned(labelsym.code) then
  832. labelnode:=tlabelnode(labelsym.code)
  833. else
  834. CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
  835. end;
  836. { check if we don't mess with exception blocks }
  837. if assigned(labelnode) and
  838. (exceptionblock<>labelnode.exceptionblock) then
  839. CGMessage(cg_e_goto_inout_of_exception_block);
  840. end;
  841. function tgotonode.dogetcopy : tnode;
  842. var
  843. p : tgotonode;
  844. begin
  845. p:=tgotonode(inherited dogetcopy);
  846. p.exceptionblock:=exceptionblock;
  847. { force a valid labelnode }
  848. if not(assigned(labelnode)) then
  849. begin
  850. if assigned(labelsym) and assigned(labelsym.code) then
  851. labelnode:=tlabelnode(labelsym.code)
  852. else
  853. internalerror(200610291);
  854. end;
  855. p.labelsym:=labelsym;
  856. p.labelnode:=tlabelnode(labelnode.dogetcopy);
  857. result:=p;
  858. end;
  859. function tgotonode.docompare(p: tnode): boolean;
  860. begin
  861. docompare := false;
  862. end;
  863. {*****************************************************************************
  864. TLABELNODE
  865. *****************************************************************************}
  866. constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
  867. begin
  868. inherited create(labeln,l);
  869. exceptionblock:=aktexceptblock;
  870. labsym:=alabsym;
  871. { Register labelnode in labelsym }
  872. labsym.code:=self;
  873. end;
  874. constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  875. begin
  876. inherited ppuload(t,ppufile);
  877. exceptionblock:=ppufile.getbyte;
  878. end;
  879. destructor tlabelnode.destroy;
  880. begin
  881. { Remove reference in labelsym, this is to prevent
  882. goto's to this label }
  883. if assigned(labsym) and (labsym.code=pointer(self)) then
  884. labsym.code:=nil;
  885. inherited destroy;
  886. end;
  887. procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
  888. begin
  889. inherited ppuwrite(ppufile);
  890. ppufile.putbyte(exceptionblock);
  891. end;
  892. procedure tlabelnode.buildderefimpl;
  893. begin
  894. inherited buildderefimpl;
  895. end;
  896. procedure tlabelnode.derefimpl;
  897. begin
  898. inherited derefimpl;
  899. end;
  900. function tlabelnode.pass_typecheck:tnode;
  901. begin
  902. result:=nil;
  903. { left could still be unassigned }
  904. if assigned(left) then
  905. typecheckpass(left);
  906. resultdef:=voidtype;
  907. end;
  908. function tlabelnode.pass_1 : tnode;
  909. begin
  910. result:=nil;
  911. expectloc:=LOC_VOID;
  912. if assigned(left) then
  913. firstpass(left);
  914. end;
  915. function tlabelnode.dogetcopy : tnode;
  916. begin
  917. if not(assigned(copiedto)) then
  918. copiedto:=tlabelnode(inherited dogetcopy);
  919. copiedto.exceptionblock:=exceptionblock;
  920. result:=copiedto;
  921. end;
  922. function tlabelnode.docompare(p: tnode): boolean;
  923. begin
  924. docompare := false;
  925. end;
  926. {*****************************************************************************
  927. TRAISENODE
  928. *****************************************************************************}
  929. constructor traisenode.create(l,taddr,tframe:tnode);
  930. begin
  931. inherited create(raisen,l,taddr,tframe);
  932. end;
  933. function traisenode.pass_typecheck:tnode;
  934. begin
  935. result:=nil;
  936. resultdef:=voidtype;
  937. if assigned(left) then
  938. begin
  939. { first para must be a _class_ }
  940. typecheckpass(left);
  941. set_varstate(left,vs_read,[vsf_must_be_valid]);
  942. if codegenerror then
  943. exit;
  944. if not(is_class(left.resultdef)) then
  945. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  946. { insert needed typeconvs for addr,frame }
  947. if assigned(right) then
  948. begin
  949. { addr }
  950. typecheckpass(right);
  951. inserttypeconv(right,voidpointertype);
  952. { frame }
  953. if assigned(third) then
  954. begin
  955. typecheckpass(third);
  956. inserttypeconv(third,voidpointertype);
  957. end;
  958. end;
  959. end;
  960. end;
  961. function traisenode.pass_1 : tnode;
  962. begin
  963. result:=nil;
  964. include(current_procinfo.flags,pi_do_call);
  965. expectloc:=LOC_VOID;
  966. if assigned(left) then
  967. begin
  968. { first para must be a _class_ }
  969. firstpass(left);
  970. { insert needed typeconvs for addr,frame }
  971. if assigned(right) then
  972. begin
  973. { addr }
  974. firstpass(right);
  975. { frame }
  976. if assigned(third) then
  977. firstpass(third);
  978. end;
  979. end;
  980. end;
  981. {*****************************************************************************
  982. TTRYEXCEPTNODE
  983. *****************************************************************************}
  984. constructor ttryexceptnode.create(l,r,_t1 : tnode);
  985. begin
  986. inherited create(tryexceptn,l,r,_t1,nil);
  987. end;
  988. function ttryexceptnode.pass_typecheck:tnode;
  989. begin
  990. result:=nil;
  991. typecheckpass(left);
  992. { on statements }
  993. if assigned(right) then
  994. typecheckpass(right);
  995. { else block }
  996. if assigned(t1) then
  997. typecheckpass(t1);
  998. resultdef:=voidtype;
  999. end;
  1000. function ttryexceptnode.pass_1 : tnode;
  1001. begin
  1002. result:=nil;
  1003. include(current_procinfo.flags,pi_do_call);
  1004. expectloc:=LOC_VOID;
  1005. firstpass(left);
  1006. { on statements }
  1007. if assigned(right) then
  1008. firstpass(right);
  1009. { else block }
  1010. if assigned(t1) then
  1011. firstpass(t1);
  1012. end;
  1013. {*****************************************************************************
  1014. TTRYFINALLYNODE
  1015. *****************************************************************************}
  1016. constructor ttryfinallynode.create(l,r:tnode);
  1017. begin
  1018. inherited create(tryfinallyn,l,r,nil,nil);
  1019. implicitframe:=false;
  1020. end;
  1021. constructor ttryfinallynode.create_implicit(l,r,_t1:tnode);
  1022. begin
  1023. inherited create(tryfinallyn,l,r,_t1,nil);
  1024. implicitframe:=true;
  1025. end;
  1026. function ttryfinallynode.pass_typecheck:tnode;
  1027. begin
  1028. result:=nil;
  1029. include(current_procinfo.flags,pi_do_call);
  1030. resultdef:=voidtype;
  1031. typecheckpass(left);
  1032. // "try block" is "used"? (JM)
  1033. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  1034. typecheckpass(right);
  1035. // "except block" is "used"? (JM)
  1036. set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
  1037. { special finally block only executed when there was an exception }
  1038. if assigned(t1) then
  1039. begin
  1040. typecheckpass(t1);
  1041. // "finally block" is "used"? (JM)
  1042. set_varstate(t1,vs_readwritten,[vsf_must_be_valid]);
  1043. end;
  1044. end;
  1045. function ttryfinallynode.pass_1 : tnode;
  1046. begin
  1047. result:=nil;
  1048. expectloc:=LOC_VOID;
  1049. firstpass(left);
  1050. firstpass(right);
  1051. if assigned(t1) then
  1052. firstpass(t1);
  1053. end;
  1054. {*****************************************************************************
  1055. TONNODE
  1056. *****************************************************************************}
  1057. constructor tonnode.create(l,r:tnode);
  1058. begin
  1059. inherited create(onn,l,r);
  1060. excepTSymtable:=nil;
  1061. excepttype:=nil;
  1062. end;
  1063. destructor tonnode.destroy;
  1064. begin
  1065. { copied nodes don't need to release the symtable }
  1066. if assigned(excepTSymtable) then
  1067. excepTSymtable.free;
  1068. inherited destroy;
  1069. end;
  1070. constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1071. begin
  1072. inherited ppuload(t,ppufile);
  1073. excepTSymtable:=nil;
  1074. excepttype:=nil;
  1075. end;
  1076. function tonnode.dogetcopy : tnode;
  1077. var
  1078. n : tonnode;
  1079. begin
  1080. n:=tonnode(inherited dogetcopy);
  1081. if assigned(exceptsymtable) then
  1082. n.exceptsymtable:=exceptsymtable.getcopy
  1083. else
  1084. n.exceptsymtable:=nil;
  1085. n.excepttype:=excepttype;
  1086. result:=n;
  1087. end;
  1088. function tonnode.pass_typecheck:tnode;
  1089. begin
  1090. result:=nil;
  1091. resultdef:=voidtype;
  1092. if not(is_class(excepttype)) then
  1093. CGMessage1(type_e_class_type_expected,excepttype.typename);
  1094. if assigned(left) then
  1095. typecheckpass(left);
  1096. if assigned(right) then
  1097. typecheckpass(right);
  1098. end;
  1099. function tonnode.pass_1 : tnode;
  1100. begin
  1101. result:=nil;
  1102. include(current_procinfo.flags,pi_do_call);
  1103. expectloc:=LOC_VOID;
  1104. if assigned(left) then
  1105. firstpass(left);
  1106. if assigned(right) then
  1107. firstpass(right);
  1108. end;
  1109. function tonnode.docompare(p: tnode): boolean;
  1110. begin
  1111. docompare := false;
  1112. end;
  1113. begin
  1114. cwhilerepeatnode:=twhilerepeatnode;
  1115. cifnode:=tifnode;
  1116. cfornode:=tfornode;
  1117. cexitnode:=texitnode;
  1118. cgotonode:=tgotonode;
  1119. clabelnode:=tlabelnode;
  1120. craisenode:=traisenode;
  1121. ctryexceptnode:=ttryexceptnode;
  1122. ctryfinallynode:=ttryfinallynode;
  1123. connode:=tonnode;
  1124. end.