nflw.pas 40 KB

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