2 // DataFlowAnalysisBase.cs
5 // Alexander Chebaturkin (chebaturkin@gmail.com)
7 // Copyright (C) 2011 Alexander Chebaturkin
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
32 using Mono.CodeContracts.Static.ControlFlow;
33 using Mono.CodeContracts.Static.DataStructures;
35 namespace Mono.CodeContracts.Static.DataFlowAnalysis {
36 abstract class DataFlowAnalysisBase<AState> :
37 IEqualityComparer<APC> {
39 protected Dictionary<APC, AState> JoinState;
40 protected PriorityQueue<APC> pending;
41 private IWidenStrategy widen_strategy;
43 protected DataFlowAnalysisBase (ICFG cfg)
46 this.pending = new PriorityQueue<APC> (WorkingListComparer);
47 this.JoinState = new Dictionary<APC, AState> (this);
48 this.widen_strategy = null;
51 #region IEqualityComparer<APC> Members
52 bool IEqualityComparer<APC>.Equals (APC x, APC y)
57 int IEqualityComparer<APC>.GetHashCode (APC obj)
59 return obj.GetHashCode ();
63 public void Initialize (APC entryPoint, AState state)
65 this.JoinState.Add (entryPoint, state);
66 this.pending.Enqueue (entryPoint);
69 public virtual void ComputeFixPoint ()
71 this.widen_strategy = new EdgeBasedWidening (20);
73 while (this.pending.Count > 0) {
74 APC next = this.pending.Dequeue ();
75 AState state = MutableVersion (this.JoinState [next], next);
78 bool repeatOuter = false;
81 if (!IsBottom (cur, state)) {
82 state = Transfer (cur, state);
87 } while (HasSingleSuccessor (cur, out next) && !RequiresJoining (next));
92 foreach (APC successorAPC in Successors (cur)) {
93 if (!IsBottom (successorAPC, state))
94 PushState (cur, successorAPC, state);
99 protected virtual void Dump (AState state)
103 public IEnumerable<KeyValuePair<APC, AState>> States ()
105 return this.JoinState;
108 protected abstract IEnumerable<APC> Successors (APC pc);
110 protected virtual void PushState (APC current, APC next, AState state)
112 state = ImmutableVersion (state, next);
113 if (RequiresJoining (next)) {
114 if (!JoinStateAtBlock (new Pair<APC, APC> (current, next), state))
116 this.pending.Enqueue (next);
118 this.JoinState [next] = state;
119 this.pending.Enqueue (next);
123 private bool JoinStateAtBlock (Pair<APC, APC> edge, AState state)
125 AState existingState;
126 if (this.JoinState.TryGetValue (edge.Value, out existingState)) {
127 bool widen = this.widen_strategy.WantToWiden (edge.Key, edge.Value, IsBackEdge (edge.Key, edge.Value));
129 bool result = Join (edge, state, existingState, out joinedState, widen);
131 this.JoinState [edge.Value] = ImmutableVersion (joinedState, edge.Value);
135 this.JoinState.Add (edge.Value, state);
139 protected abstract bool IsBackEdge (APC from, APC to);
141 protected abstract int WorkingListComparer (APC a, APC b);
143 protected abstract bool Join (Pair<APC, APC> edge, AState newState, AState existingState, out AState joinedState, bool widen);
145 protected abstract bool RequiresJoining (APC pc);
147 protected abstract bool HasSingleSuccessor (APC pc, out APC next);
149 protected abstract bool IsBottom (APC pc, AState state);
151 protected abstract AState Transfer (APC pc, AState state);
153 protected abstract AState MutableVersion (AState state, APC at);
154 protected abstract AState ImmutableVersion (AState state, APC at);
156 public void PrintStatesAtJoinPoints (TextWriter tw)
158 foreach (APC apc in this.JoinState.Keys) {
159 string str = this.JoinState [apc].ToString ().Replace (Environment.NewLine, Environment.NewLine + " ");
160 tw.WriteLine ("Block {0}, PC {1}: {2}", apc.Block, apc.Index, str);