nflw.pas 40 KB

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