nflw.pas 41 KB

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