optdfa.pas 37 KB

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