optdfa.pas 37 KB

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