optdfa.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. {
  2. DFA
  3. Copyright (c) 2007 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { $define DEBUG_DFA}
  18. { $define EXTDEBUG_DFA}
  19. { this unit implements routines to perform dfa }
  20. unit optdfa;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. node,optutils;
  25. type
  26. TDFABuilder = class
  27. protected
  28. procedure CreateLifeInfo(node : tnode;map : TIndexedNodeSet);
  29. public
  30. resultnode : tnode;
  31. nodemap : TIndexedNodeSet;
  32. { reset all dfa info, this is required before creating dfa info
  33. if the tree has been changed without updating dfa }
  34. procedure resetdfainfo(node : tnode);
  35. procedure createdfainfo(node : tnode);
  36. procedure redodfainfo(node : tnode);
  37. destructor destroy;override;
  38. end;
  39. procedure CheckAndWarn(code : tnode;nodetosearch : tnode);
  40. implementation
  41. uses
  42. globtype,constexp,
  43. verbose,
  44. symconst,symdef,symsym,
  45. defutil,
  46. procinfo,
  47. nutils,htypechk,
  48. nbas,nflw,ncal,nset,nld,nadd,
  49. optbase;
  50. (*
  51. function initnodes(var n:tnode; arg: pointer) : foreachnoderesult;
  52. begin
  53. { node worth to add? }
  54. if (node_complexity(n)>1) and (tstoreddef(n.resultdef).is_intregable or tstoreddef(n.resultdef).is_fpuregable) then
  55. begin
  56. plists(arg)^.nodelist.Add(n);
  57. plists(arg)^.locationlist.Add(@n);
  58. result:=fen_false;
  59. end
  60. else
  61. result:=fen_norecurse_false;
  62. end;
  63. *)
  64. {
  65. x:=f; read: [f]
  66. while x do read: []
  67. a:=b; read: [a,b,d] def: [a] life: read*def=[a]
  68. c:=d; read: [a,d] def: [a,c] life: read*def=[a]
  69. e:=a; read: [a] def: [a,c,e] life: read*def=[a]
  70. function f(b,d,x : type) : type;
  71. begin
  72. while x do alive: b,d,x
  73. begin
  74. a:=b; alive: b,d,x
  75. c:=d; alive: a,d,x
  76. e:=a+c; alive: a,c,x
  77. dec(x); alive: c,e,x
  78. end;
  79. result:=c+e; alive: c,e
  80. end; alive: result
  81. }
  82. type
  83. tdfainfo = record
  84. use : PDFASet;
  85. def : PDFASet;
  86. map : TIndexedNodeSet
  87. end;
  88. pdfainfo = ^tdfainfo;
  89. function AddDefUse(var n: tnode; arg: pointer): foreachnoderesult;
  90. begin
  91. case n.nodetype of
  92. tempcreaten:
  93. begin
  94. if assigned(ttempcreatenode(n).tempinfo^.tempinitcode) then
  95. begin
  96. pdfainfo(arg)^.map.Add(n);
  97. DFASetInclude(pdfainfo(arg)^.def^,n.optinfo^.index);
  98. end;
  99. end;
  100. temprefn,
  101. loadn:
  102. begin
  103. pdfainfo(arg)^.map.Add(n);
  104. if nf_modify in n.flags then
  105. begin
  106. DFASetInclude(pdfainfo(arg)^.use^,n.optinfo^.index);
  107. DFASetInclude(pdfainfo(arg)^.def^,n.optinfo^.index)
  108. end
  109. else if nf_write in n.flags then
  110. DFASetInclude(pdfainfo(arg)^.def^,n.optinfo^.index)
  111. else
  112. DFASetInclude(pdfainfo(arg)^.use^,n.optinfo^.index);
  113. end;
  114. else
  115. ;
  116. end;
  117. result:=fen_false;
  118. end;
  119. function ResetProcessing(var n: tnode; arg: pointer): foreachnoderesult;
  120. begin
  121. exclude(n.flags,nf_processing);
  122. { dfa works only on normalized trees, so do not recurse into expressions, because
  123. ResetProcessing eats a signififcant amount of time of CheckAndWarn
  124. the following set contains (hopefully) most of the expression nodes }
  125. if n.nodetype in [calln,inlinen,assignn,callparan,andn,addn,orn,subn,muln,divn,slashn,notn,equaln,unequaln,gtn,ltn,lten,gten,loadn,
  126. typeconvn,vecn,subscriptn,addrn,derefn] then
  127. result:=fen_norecurse_false
  128. else
  129. result:=fen_false;
  130. end;
  131. function ResetDFA(var n: tnode; arg: pointer): foreachnoderesult;
  132. begin
  133. if assigned(n.optinfo) then
  134. begin
  135. with n.optinfo^ do
  136. begin
  137. life:=nil;
  138. def:=nil;
  139. use:=nil;
  140. defsum:=nil;
  141. end;
  142. end;
  143. result:=fen_false;
  144. end;
  145. procedure TDFABuilder.CreateLifeInfo(node : tnode;map : TIndexedNodeSet);
  146. var
  147. changed : boolean;
  148. procedure CreateInfo(node : tnode);
  149. { update life entry of a node with l, set changed if this changes
  150. life info for the node
  151. }
  152. procedure updatelifeinfo(n : tnode;const l : TDFASet);
  153. var
  154. b : boolean;
  155. begin
  156. b:=DFASetNotEqual(l,n.optinfo^.life);
  157. {$ifdef DEBUG_DFA}
  158. if not(changed) and b then
  159. begin
  160. writeln('Another DFA pass caused by: ',nodetype2str[n.nodetype],'(',n.fileinfo.line,',',n.fileinfo.column,')');
  161. write(' Life info set was: ');PrintDFASet(Output,n.optinfo^.life);writeln;
  162. write(' Life info set will be: ');PrintDFASet(Output,l);writeln;
  163. end;
  164. {$endif DEBUG_DFA}
  165. changed:=changed or b;
  166. n.optinfo^.life:=l;
  167. end;
  168. procedure calclife(n : tnode);
  169. var
  170. l : TDFASet;
  171. begin
  172. if assigned(n.successor) then
  173. begin
  174. { ensure we can access optinfo }
  175. DFASetDiff(l,n.successor.optinfo^.life,n.optinfo^.def);
  176. DFASetIncludeSet(l,n.optinfo^.use);
  177. DFASetIncludeSet(l,n.optinfo^.life);
  178. end
  179. else
  180. begin
  181. l:=n.optinfo^.use;
  182. DFASetIncludeSet(l,n.optinfo^.life);
  183. end;
  184. updatelifeinfo(n,l);
  185. end;
  186. var
  187. dfainfo : tdfainfo;
  188. l : TDFASet;
  189. save: TDFASet;
  190. lv, hv: TConstExprInt;
  191. i : longint;
  192. counteruse_after_loop : boolean;
  193. begin
  194. if node=nil then
  195. exit;
  196. { ensure we've already optinfo set }
  197. node.allocoptinfo;
  198. if nf_processing in node.flags then
  199. exit;
  200. include(node.flags,nf_processing);
  201. if assigned(node.successor) then
  202. CreateInfo(node.successor);
  203. {$ifdef EXTDEBUG_DFA}
  204. writeln('Handling: ',nodetype2str[node.nodetype],'(',node.fileinfo.line,',',node.fileinfo.column,')');
  205. {$endif EXTDEBUG_DFA}
  206. { life:=succesorlive-definition+use }
  207. case node.nodetype of
  208. whilerepeatn:
  209. begin
  210. { analyze the loop condition }
  211. if not(assigned(node.optinfo^.def)) and
  212. not(assigned(node.optinfo^.use)) then
  213. begin
  214. dfainfo.use:[email protected]^.use;
  215. dfainfo.def:[email protected]^.def;
  216. dfainfo.map:=map;
  217. foreachnodestatic(pm_postprocess,twhilerepeatnode(node).left,@AddDefUse,@dfainfo);
  218. end;
  219. { NB: this node should typically have empty def set }
  220. if assigned(node.successor) then
  221. DFASetDiff(l,node.successor.optinfo^.life,node.optinfo^.def)
  222. else if assigned(resultnode) then
  223. DFASetDiff(l,resultnode.optinfo^.life,node.optinfo^.def)
  224. else
  225. l:=nil;
  226. { for repeat..until, node use set in included at the end of loop }
  227. if not (lnf_testatbegin in twhilerepeatnode(node).loopflags) then
  228. DFASetIncludeSet(l,node.optinfo^.use);
  229. DFASetIncludeSet(l,node.optinfo^.life);
  230. save:=node.optinfo^.life;
  231. { to process body correctly, we need life info in place (because
  232. whilerepeatnode is successor of its body). }
  233. node.optinfo^.life:=l;
  234. { now process the body }
  235. CreateInfo(twhilerepeatnode(node).right);
  236. { restore, to prevent infinite recursion via changed flag }
  237. node.optinfo^.life:=save;
  238. { for while loops, node use set is included at the beginning of loop }
  239. l:=twhilerepeatnode(node).right.optinfo^.life;
  240. if lnf_testatbegin in twhilerepeatnode(node).loopflags then
  241. DFASetIncludeSet(l,node.optinfo^.use);
  242. UpdateLifeInfo(node,l);
  243. { ... and a second iteration for fast convergence }
  244. CreateInfo(twhilerepeatnode(node).right);
  245. end;
  246. forn:
  247. begin
  248. {
  249. left: loopvar
  250. right: from
  251. t1: to
  252. t2: body
  253. }
  254. node.allocoptinfo;
  255. tfornode(node).loopiteration.allocoptinfo;
  256. if not(assigned(node.optinfo^.def)) and
  257. not(assigned(node.optinfo^.use)) then
  258. begin
  259. dfainfo.use:[email protected]^.use;
  260. dfainfo.def:[email protected]^.def;
  261. dfainfo.map:=map;
  262. foreachnodestatic(pm_postprocess,tfornode(node).left,@AddDefUse,@dfainfo);
  263. foreachnodestatic(pm_postprocess,tfornode(node).right,@AddDefUse,@dfainfo);
  264. foreachnodestatic(pm_postprocess,tfornode(node).t1,@AddDefUse,@dfainfo);
  265. end;
  266. { create life for the body }
  267. CreateInfo(tfornode(node).t2);
  268. { is the counter living after the loop?
  269. if left is a record element, it might not be tracked by dfa, so
  270. optinfo might not be assigned
  271. }
  272. counteruse_after_loop:=assigned(tfornode(node).left.optinfo) and assigned(node.successor) and
  273. DFASetIn(node.successor.optinfo^.life,tfornode(node).left.optinfo^.index);
  274. if counteruse_after_loop then
  275. begin
  276. { if yes, then we should warn }
  277. { !!!!!! }
  278. end
  279. else
  280. Include(tfornode(node).loopflags,lnf_dont_mind_loopvar_on_exit);
  281. { first update the dummy node }
  282. { get the life of the loop block }
  283. l:=copy(tfornode(node).t2.optinfo^.life);
  284. { take care of the sucessor }
  285. if assigned(node.successor) then
  286. DFASetIncludeSet(l,node.successor.optinfo^.life);
  287. { the counter variable is living as well inside the for loop
  288. if left is a record element, it might not be tracked by dfa, so
  289. optinfo might not be assigned
  290. }
  291. if assigned(tfornode(node).left.optinfo) then
  292. DFASetInclude(l,tfornode(node).left.optinfo^.index);
  293. { force block node life info }
  294. UpdateLifeInfo(tfornode(node).loopiteration,l);
  295. { now update the for node itself }
  296. { get the life of the loop block }
  297. l:=copy(tfornode(node).t2.optinfo^.life);
  298. { take care of the sucessor as it's possible that we don't have one execution of the body }
  299. if (not(tfornode(node).right.nodetype=ordconstn) or not(tfornode(node).t1.nodetype=ordconstn)) and
  300. assigned(node.successor) then
  301. DFASetIncludeSet(l,node.successor.optinfo^.life);
  302. {
  303. the counter variable is not living at the entry of the for node
  304. if left is a record element, it might not be tracked by dfa, so
  305. optinfo might not be assigned
  306. }
  307. if assigned(tfornode(node).left.optinfo) then
  308. DFASetExclude(l,tfornode(node).left.optinfo^.index);
  309. { ... but it could be that left/right use it, so do this after
  310. removing the def of the counter variable }
  311. DFASetIncludeSet(l,node.optinfo^.use);
  312. UpdateLifeInfo(node,l);
  313. { ... and a second iteration for fast convergence }
  314. CreateInfo(tfornode(node).t2);
  315. end;
  316. temprefn,
  317. loadn,
  318. typeconvn,
  319. derefn,
  320. assignn:
  321. begin
  322. if not(assigned(node.optinfo^.def)) and
  323. not(assigned(node.optinfo^.use)) then
  324. begin
  325. dfainfo.use:[email protected]^.use;
  326. dfainfo.def:[email protected]^.def;
  327. dfainfo.map:=map;
  328. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  329. end;
  330. calclife(node);
  331. end;
  332. statementn:
  333. begin
  334. { nested statement }
  335. CreateInfo(tstatementnode(node).statement);
  336. { inherit info }
  337. node.optinfo^.life:=tstatementnode(node).statement.optinfo^.life;
  338. end;
  339. blockn:
  340. begin
  341. CreateInfo(tblocknode(node).statements);
  342. { ensure that we don't remove life info }
  343. l:=node.optinfo^.life;
  344. if assigned(node.successor) then
  345. DFASetIncludeSet(l,node.successor.optinfo^.life);
  346. UpdateLifeInfo(node,l);
  347. end;
  348. ifn:
  349. begin
  350. { get information from cond. expression }
  351. if not(assigned(node.optinfo^.def)) and
  352. not(assigned(node.optinfo^.use)) then
  353. begin
  354. dfainfo.use:[email protected]^.use;
  355. dfainfo.def:[email protected]^.def;
  356. dfainfo.map:=map;
  357. foreachnodestatic(pm_postprocess,tifnode(node).left,@AddDefUse,@dfainfo);
  358. end;
  359. { create life info for then and else node }
  360. CreateInfo(tifnode(node).right);
  361. CreateInfo(tifnode(node).t1);
  362. { ensure that we don't remove life info }
  363. l:=node.optinfo^.life;
  364. { get life info from then branch }
  365. if assigned(tifnode(node).right) then
  366. DFASetIncludeSet(l,tifnode(node).right.optinfo^.life);
  367. { get life info from else branch }
  368. if assigned(tifnode(node).t1) then
  369. DFASetIncludeSet(l,tifnode(node).t1.optinfo^.life)
  370. else if assigned(node.successor) then
  371. DFASetIncludeSet(l,node.successor.optinfo^.life);
  372. { remove def info from the cond. expression }
  373. DFASetExcludeSet(l,tifnode(node).optinfo^.def);
  374. { add use info from the cond. expression }
  375. DFASetIncludeSet(l,tifnode(node).optinfo^.use);
  376. { finally, update the life info of the node }
  377. UpdateLifeInfo(node,l);
  378. end;
  379. casen:
  380. begin
  381. { get information from "case" expression }
  382. if not(assigned(node.optinfo^.def)) and
  383. not(assigned(node.optinfo^.use)) then
  384. begin
  385. dfainfo.use:[email protected]^.use;
  386. dfainfo.def:[email protected]^.def;
  387. dfainfo.map:=map;
  388. foreachnodestatic(pm_postprocess,tcasenode(node).left,@AddDefUse,@dfainfo);
  389. end;
  390. { create life info for block and else nodes }
  391. for i:=0 to tcasenode(node).blocks.count-1 do
  392. CreateInfo(pcaseblock(tcasenode(node).blocks[i])^.statement);
  393. CreateInfo(tcasenode(node).elseblock);
  394. { ensure that we don't remove life info }
  395. l:=node.optinfo^.life;
  396. { get life info from case branches }
  397. for i:=0 to tcasenode(node).blocks.count-1 do
  398. DFASetIncludeSet(l,pcaseblock(tcasenode(node).blocks[i])^.statement.optinfo^.life);
  399. { get life info from else branch or the succesor }
  400. if assigned(tcasenode(node).elseblock) then
  401. DFASetIncludeSet(l,tcasenode(node).elseblock.optinfo^.life)
  402. else if assigned(node.successor) then
  403. begin
  404. if is_ordinal(tcasenode(node).left.resultdef) then
  405. begin
  406. getrange(tcasenode(node).left.resultdef,lv,hv);
  407. if tcasenode(node).labelcoverage<(hv-lv) then
  408. DFASetIncludeSet(l,node.successor.optinfo^.life);
  409. end
  410. else
  411. DFASetIncludeSet(l,node.successor.optinfo^.life);
  412. end;
  413. { add use info from the "case" expression }
  414. DFASetIncludeSet(l,tcasenode(node).optinfo^.use);
  415. { finally, update the life info of the node }
  416. UpdateLifeInfo(node,l);
  417. end;
  418. exitn:
  419. begin
  420. { in case of inlining, an exit node can have a successor, in this case, we do not have to
  421. use the faked resultnode }
  422. if assigned(node.successor) then
  423. begin
  424. l:=node.optinfo^.life;
  425. DFASetIncludeSet(l,node.successor.optinfo^.life);
  426. UpdateLifeInfo(node,l);
  427. end
  428. else if assigned(resultnode) and (resultnode.nodetype<>nothingn) then
  429. begin
  430. if not(assigned(node.optinfo^.def)) and
  431. not(assigned(node.optinfo^.use)) then
  432. begin
  433. if assigned(texitnode(node).left) then
  434. begin
  435. { this should never happen as
  436. texitnode.pass_typecheck converts the left node into a separate node already
  437. node.optinfo^.def:=resultnode.optinfo^.def;
  438. dfainfo.use:[email protected]^.use;
  439. dfainfo.def:[email protected]^.def;
  440. dfainfo.map:=map;
  441. foreachnodestatic(pm_postprocess,texitnode(node).left,@AddDefUse,@dfainfo);
  442. calclife(node); }
  443. Internalerror(2020122901);
  444. end
  445. else
  446. begin
  447. { get info from faked resultnode }
  448. node.optinfo^.use:=resultnode.optinfo^.use;
  449. node.optinfo^.life:=node.optinfo^.use;
  450. changed:=true;
  451. end;
  452. end;
  453. end;
  454. end;
  455. {$ifdef JVM}
  456. { all other platforms except jvm translate raise nodes into call nodes during pass_1 }
  457. raisen,
  458. {$endif JVM}
  459. asn,
  460. inlinen,
  461. calln:
  462. begin
  463. if not(assigned(node.optinfo^.def)) and
  464. not(assigned(node.optinfo^.use)) then
  465. begin
  466. dfainfo.use:[email protected]^.use;
  467. dfainfo.def:[email protected]^.def;
  468. dfainfo.map:=map;
  469. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  470. end;
  471. calclife(node);
  472. end;
  473. labeln:
  474. begin
  475. calclife(node);
  476. if assigned(tlabelnode(node).left) then
  477. begin
  478. l:=node.optinfo^.life;
  479. DFASetIncludeSet(l,tlabelnode(node).optinfo^.life);
  480. UpdateLifeInfo(node,l);
  481. end;
  482. end;
  483. tempcreaten,
  484. tempdeleten,
  485. nothingn,
  486. continuen,
  487. goton,
  488. breakn:
  489. begin
  490. calclife(node);
  491. end;
  492. else
  493. internalerror(2007050502);
  494. end;
  495. end;
  496. var
  497. runs : integer;
  498. begin
  499. runs:=0;
  500. repeat
  501. inc(runs);
  502. changed:=false;
  503. CreateInfo(node);
  504. foreachnodestatic(pm_postprocess,node,@ResetProcessing,nil);
  505. { the result node is not reached by foreachnodestatic }
  506. exclude(resultnode.flags,nf_processing);
  507. {$ifdef DEBUG_DFA}
  508. PrintIndexedNodeSet(output,map);
  509. PrintDFAInfo(output,node);
  510. {$endif DEBUG_DFA}
  511. until not(changed);
  512. {$ifdef DEBUG_DFA}
  513. writeln('DFA solver iterations: ',runs);
  514. {$endif DEBUG_DFA}
  515. end;
  516. { reset all dfa info, this is required before creating dfa info
  517. if the tree has been changed without updating dfa }
  518. procedure TDFABuilder.resetdfainfo(node : tnode);
  519. begin
  520. nodemap.Free;
  521. nodemap:=nil;
  522. resultnode.Free;
  523. resultnode:=nil;
  524. foreachnodestatic(pm_postprocess,node,@ResetDFA,nil);
  525. end;
  526. procedure TDFABuilder.createdfainfo(node : tnode);
  527. var
  528. dfarec : tdfainfo;
  529. begin
  530. if not(assigned(nodemap)) then
  531. nodemap:=TIndexedNodeSet.Create;
  532. { create a fake node using the result which will be the last node }
  533. if not(is_void(current_procinfo.procdef.returndef)) then
  534. begin
  535. if current_procinfo.procdef.proctypeoption=potype_constructor then
  536. resultnode:=load_self_node
  537. else
  538. resultnode:=load_result_node;
  539. resultnode.allocoptinfo;
  540. dfarec.use:[email protected]^.use;
  541. dfarec.def:[email protected]^.def;
  542. dfarec.map:=nodemap;
  543. AddDefUse(resultnode,@dfarec);
  544. resultnode.optinfo^.life:=resultnode.optinfo^.use;
  545. end
  546. else
  547. begin
  548. resultnode:=cnothingnode.create;
  549. resultnode.allocoptinfo;
  550. end;
  551. { add controll flow information }
  552. SetNodeSucessors(node,resultnode);
  553. { now, collect life information }
  554. CreateLifeInfo(node,nodemap);
  555. end;
  556. procedure TDFABuilder.redodfainfo(node: tnode);
  557. begin
  558. resetdfainfo(node);
  559. createdfainfo(node);
  560. include(current_procinfo.flags,pi_dfaavailable);
  561. end;
  562. destructor TDFABuilder.Destroy;
  563. begin
  564. Resultnode.free;
  565. nodemap.free;
  566. inherited destroy;
  567. end;
  568. type
  569. { helper structure to be able to pass more than one variable to the iterator function }
  570. TSearchNodeInfo = record
  571. nodetosearch : tnode;
  572. { this contains a list of all file locations where a warning was thrown already,
  573. the same location might appear multiple times because nodes might have been copied }
  574. warnedfilelocs : array of tfileposinfo;
  575. end;
  576. PSearchNodeInfo = ^TSearchNodeInfo;
  577. { searches for a given node n and warns if the node is found as being uninitialized. If a node is
  578. found, searching is stopped so each call issues only one warning/hint }
  579. function SearchNode(var n: tnode; arg: pointer): foreachnoderesult;
  580. function WarnedForLocation(f : tfileposinfo) : boolean;
  581. var
  582. i : longint;
  583. begin
  584. result:=true;
  585. for i:=0 to high(PSearchNodeInfo(arg)^.warnedfilelocs) do
  586. with PSearchNodeInfo(arg)^.warnedfilelocs[i] do
  587. begin
  588. if (f.column=column) and (f.fileindex=fileindex) and (f.line=line) and (f.moduleindex=moduleindex) then
  589. exit;
  590. end;
  591. result:=false;
  592. end;
  593. procedure AddFilepos(const f : tfileposinfo);
  594. begin
  595. Setlength(PSearchNodeInfo(arg)^.warnedfilelocs,length(PSearchNodeInfo(arg)^.warnedfilelocs)+1);
  596. PSearchNodeInfo(arg)^.warnedfilelocs[high(PSearchNodeInfo(arg)^.warnedfilelocs)]:=f;
  597. end;
  598. { Checks if the symbol is a candidate for a warning.
  599. Emit warning/note for living locals, result and parameters, but only about the current
  600. symtables }
  601. function SymbolCandidateForWarningOrHint(sym : tabstractnormalvarsym) : Boolean;
  602. begin
  603. Result:=(((sym.owner=current_procinfo.procdef.localst) and
  604. (current_procinfo.procdef.localst.symtablelevel=sym.owner.symtablelevel)
  605. ) or
  606. ((sym.owner=current_procinfo.procdef.parast) and
  607. (sym.typ=paravarsym) and
  608. (current_procinfo.procdef.parast.symtablelevel=sym.owner.symtablelevel) and
  609. { all parameters except out parameters are initialized by the caller }
  610. (tparavarsym(sym).varspez=vs_out)
  611. ) or
  612. ((vo_is_funcret in sym.varoptions) and
  613. (current_procinfo.procdef.parast.symtablelevel=sym.owner.symtablelevel)
  614. )
  615. ) and not(vo_is_external in sym.varoptions) and
  616. not sym.inparentfpstruct;
  617. end;
  618. var
  619. varsym : tabstractnormalvarsym;
  620. methodpointer,
  621. hpt : tnode;
  622. begin
  623. result:=fen_false;
  624. case n.nodetype of
  625. callparan:
  626. begin
  627. { do not warn about variables passed by var, just issue a hint, this
  628. is a workaround for old code e.g. using fillchar }
  629. if assigned(tcallparanode(n).parasym) and (tcallparanode(n).parasym.varspez in [vs_var,vs_out]) then
  630. begin
  631. hpt:=tcallparanode(n).left;
  632. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn,typeconvn]) do
  633. hpt:=tunarynode(hpt).left;
  634. if assigned(hpt) and (hpt.nodetype=loadn) and not(WarnedForLocation(hpt.fileinfo)) and
  635. SymbolCandidateForWarningOrHint(tabstractnormalvarsym(tloadnode(hpt).symtableentry)) and
  636. PSearchNodeInfo(arg)^.nodetosearch.isequal(hpt) then
  637. begin
  638. { issue only a hint for var, when encountering the node passed as out, we need only to stop searching }
  639. if tcallparanode(n).parasym.varspez=vs_var then
  640. UninitializedVariableMessage(hpt.fileinfo,false,
  641. tloadnode(hpt).symtable.symtabletype=localsymtable,
  642. is_managed_type(tloadnode(hpt).resultdef),
  643. tloadnode(hpt).symtableentry.RealName);
  644. AddFilepos(hpt.fileinfo);
  645. result:=fen_norecurse_true;
  646. end
  647. end;
  648. end;
  649. orn,
  650. andn:
  651. begin
  652. { take care of short boolean evaluation: if the expression to be search is found in left,
  653. we do not need to search right }
  654. if foreachnodestatic(pm_postprocess,taddnode(n).left,@optdfa.SearchNode,arg) or
  655. foreachnodestatic(pm_postprocess,taddnode(n).right,@optdfa.SearchNode,arg) then
  656. result:=fen_norecurse_true
  657. else
  658. result:=fen_norecurse_false;
  659. end;
  660. calln:
  661. begin
  662. methodpointer:=tcallnode(n).methodpointer;
  663. if assigned(methodpointer) and (methodpointer.nodetype<>typen) then
  664. begin
  665. { Remove all postfix operators }
  666. hpt:=methodpointer;
  667. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  668. hpt:=tunarynode(hpt).left;
  669. { skip (absolute and other simple) type conversions -- only now,
  670. because the checks above have to take type conversions into
  671. e.g. class reference types account }
  672. hpt:=actualtargetnode(@hpt)^;
  673. { R.Init then R will be initialized by the constructor,
  674. Also allow it for simple loads }
  675. if (tcallnode(n).procdefinition.proctypeoption=potype_constructor) or
  676. (PSearchNodeInfo(arg)^.nodetosearch.isequal(hpt) and
  677. (((methodpointer.resultdef.typ=objectdef) and
  678. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)) or
  679. (methodpointer.resultdef.typ=recorddef)
  680. )
  681. ) then
  682. begin
  683. { don't warn about the method pointer }
  684. AddFilepos(hpt.fileinfo);
  685. if not(foreachnodestatic(pm_postprocess,tcallnode(n).left,@optdfa.SearchNode,arg)) then
  686. foreachnodestatic(pm_postprocess,tcallnode(n).right,@optdfa.SearchNode,arg);
  687. result:=fen_norecurse_true
  688. end;
  689. end;
  690. end;
  691. loadn:
  692. begin
  693. if (tloadnode(n).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) and
  694. PSearchNodeInfo(arg)^.nodetosearch.isequal(n) and ((nf_modify in n.flags) or not(nf_write in n.flags)) then
  695. begin
  696. varsym:=tabstractnormalvarsym(tloadnode(n).symtableentry);
  697. if assigned(varsym.owner) and SymbolCandidateForWarningOrHint(varsym) then
  698. begin
  699. if (vo_is_funcret in varsym.varoptions) and not(WarnedForLocation(n.fileinfo)) then
  700. begin
  701. if is_managed_type(varsym.vardef) then
  702. MessagePos(n.fileinfo,sym_w_managed_function_result_uninitialized)
  703. else
  704. MessagePos(n.fileinfo,sym_w_function_result_uninitialized);
  705. AddFilepos(n.fileinfo);
  706. result:=fen_norecurse_true;
  707. end
  708. else
  709. begin
  710. { typed consts are initialized, further, warn only once per location }
  711. if not (vo_is_typed_const in varsym.varoptions) and not(WarnedForLocation(n.fileinfo)) then
  712. begin
  713. UninitializedVariableMessage(n.fileinfo,true,varsym.typ=localvarsym,is_managed_type(varsym.vardef),varsym.realname);
  714. AddFilepos(n.fileinfo);
  715. result:=fen_norecurse_true;
  716. end;
  717. end;
  718. end
  719. {$ifdef dummy}
  720. { if a the variable we are looking for is passed as a var parameter, we stop searching }
  721. else if assigned(varsym.owner) and
  722. (varsym.owner=current_procinfo.procdef.parast) and
  723. (varsym.typ=paravarsym) and
  724. (current_procinfo.procdef.parast.symtablelevel=varsym.owner.symtablelevel) and
  725. (tparavarsym(varsym).varspez=vs_var) then
  726. result:=fen_norecurse_true;
  727. {$endif dummy}
  728. end;
  729. end;
  730. else
  731. ;
  732. end;
  733. end;
  734. procedure CheckAndWarn(code : tnode;nodetosearch : tnode);
  735. var
  736. SearchNodeInfo : TSearchNodeInfo;
  737. function DoCheck(node : tnode) : boolean;
  738. var
  739. i : longint;
  740. touchesnode : Boolean;
  741. procedure MaybeDoCheck(n : tnode);inline;
  742. begin
  743. Result:=Result or DoCheck(n);
  744. end;
  745. procedure MaybeSearchIn(n : tnode);
  746. begin
  747. if touchesnode then
  748. Result:=Result or foreachnodestatic(pm_postprocess,n,@SearchNode,@SearchNodeInfo);
  749. end;
  750. begin
  751. result:=false;
  752. if node=nil then
  753. exit;
  754. if nf_processing in node.flags then
  755. exit;
  756. include(node.flags,nf_processing);
  757. if not(DFASetIn(node.optinfo^.life,nodetosearch.optinfo^.index)) then
  758. exit;
  759. { we do not need this info always, so try to safe some time here, CheckAndWarn
  760. takes a lot of time anyways }
  761. if not(node.nodetype in [statementn,blockn]) then
  762. touchesnode:=DFASetIn(node.optinfo^.use,nodetosearch.optinfo^.index) or
  763. DFASetIn(node.optinfo^.def,nodetosearch.optinfo^.index)
  764. else
  765. touchesnode:=false;
  766. case node.nodetype of
  767. whilerepeatn:
  768. begin
  769. MaybeSearchIn(twhilerepeatnode(node).left);
  770. MaybeDoCheck(twhilerepeatnode(node).right);
  771. end;
  772. forn:
  773. begin
  774. MaybeSearchIn(tfornode(node).right);
  775. MaybeSearchIn(tfornode(node).t1);
  776. MaybeDoCheck(tfornode(node).t2);
  777. end;
  778. statementn:
  779. MaybeDoCheck(tstatementnode(node).statement);
  780. blockn:
  781. MaybeDoCheck(tblocknode(node).statements);
  782. ifn:
  783. begin
  784. MaybeSearchIn(tifnode(node).left);
  785. MaybeDoCheck(tifnode(node).right);
  786. MaybeDoCheck(tifnode(node).t1);
  787. end;
  788. casen:
  789. begin
  790. MaybeSearchIn(tcasenode(node).left);
  791. for i:=0 to tcasenode(node).blocks.count-1 do
  792. MaybeDoCheck(pcaseblock(tcasenode(node).blocks[i])^.statement);
  793. MaybeDoCheck(tcasenode(node).elseblock);
  794. end;
  795. labeln:
  796. MaybeDoCheck(tlabelnode(node).left);
  797. { we are aware of the following nodes so if new node types are added to the compiler
  798. and pop up in the search, the ie below kicks in as a reminder }
  799. exitn:
  800. begin
  801. MaybeSearchIn(texitnode(node).left);
  802. { exit uses the resultnode implicitly, so searching for a matching node is
  803. useless, if we reach the exit node and found the living node not in left, then
  804. it can be only the resultnode
  805. successor might be assigned in case of an inlined exit node, in this case we do not warn about an unassigned
  806. result as this had happened already when the routine has been compiled }
  807. if not(assigned(node.successor)) and not(Result) and not(is_void(current_procinfo.procdef.returndef)) and
  808. not(assigned(texitnode(node).resultexpr)) and
  809. { don't warn about constructors }
  810. not(current_procinfo.procdef.proctypeoption in [potype_class_constructor,potype_constructor]) then
  811. begin
  812. if is_managed_type(current_procinfo.procdef.returndef) then
  813. MessagePos(node.fileinfo,sym_w_managed_function_result_uninitialized)
  814. else
  815. MessagePos(node.fileinfo,sym_w_function_result_uninitialized);
  816. Setlength(SearchNodeInfo.warnedfilelocs,length(SearchNodeInfo.warnedfilelocs)+1);
  817. SearchNodeInfo.warnedfilelocs[high(SearchNodeInfo.warnedfilelocs)]:=node.fileinfo;
  818. end
  819. end;
  820. { could be the implicitly generated load node for the result }
  821. {$ifdef JVM}
  822. { all other platforms except jvm translate raise nodes into call nodes during pass_1 }
  823. raisen,
  824. {$endif JVM}
  825. loadn,
  826. assignn,
  827. calln,
  828. temprefn,
  829. typeconvn,
  830. inlinen,
  831. tempcreaten,
  832. tempdeleten:
  833. MaybeSearchIn(node);
  834. nothingn,
  835. continuen,
  836. goton,
  837. breakn:
  838. ;
  839. else
  840. internalerror(2013111301);
  841. end;
  842. { if already a warning has been issued, then stop }
  843. if Result then
  844. exit;
  845. if assigned(node.successor) then
  846. MaybeDoCheck(node.successor);
  847. end;
  848. begin
  849. SearchNodeInfo.nodetosearch:=nodetosearch;
  850. DoCheck(code);
  851. foreachnodestatic(pm_postprocess,code,@ResetProcessing,nil);
  852. end;
  853. end.