nflw.pas 40 KB

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