nflw.pas 39 KB

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