The base architecture for code-contracts analysis
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static.DataFlowAnalysis / DataFlowAnalysisBase.cs
1 // 
2 // DataFlowAnalysisBase.cs
3 // 
4 // Authors:
5 //      Alexander Chebaturkin (chebaturkin@gmail.com)
6 // 
7 // Copyright (C) 2011 Alexander Chebaturkin
8 // 
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //  
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.
27 // 
28
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using Mono.CodeContracts.Static.ControlFlow;
33 using Mono.CodeContracts.Static.DataStructures;
34
35 namespace Mono.CodeContracts.Static.DataFlowAnalysis {
36         abstract class DataFlowAnalysisBase<AState> :
37                 IEqualityComparer<APC> {
38                 protected ICFG CFG;
39                 protected Dictionary<APC, AState> JoinState;
40                 protected PriorityQueue<APC> pending;
41                 private IWidenStrategy widen_strategy;
42
43                 protected DataFlowAnalysisBase (ICFG cfg)
44                 {
45                         this.CFG = cfg;
46                         this.pending = new PriorityQueue<APC> (WorkingListComparer);
47                         this.JoinState = new Dictionary<APC, AState> (this);
48                         this.widen_strategy = null;
49                 }
50
51                 #region IEqualityComparer<APC> Members
52                 bool IEqualityComparer<APC>.Equals (APC x, APC y)
53                 {
54                         return x.Equals (y);
55                 }
56
57                 int IEqualityComparer<APC>.GetHashCode (APC obj)
58                 {
59                         return obj.GetHashCode ();
60                 }
61                 #endregion
62
63                 public void Initialize (APC entryPoint, AState state)
64                 {
65                         this.JoinState.Add (entryPoint, state);
66                         this.pending.Enqueue (entryPoint);
67                 }
68
69                 public virtual void ComputeFixPoint ()
70                 {
71                         this.widen_strategy = new EdgeBasedWidening (20);
72
73                         while (this.pending.Count > 0) {
74                                 APC next = this.pending.Dequeue ();
75                                 AState state = MutableVersion (this.JoinState [next], next);
76
77                                 APC cur;
78                                 bool repeatOuter = false;
79                                 do {
80                                         cur = next;
81                                         if (!IsBottom (cur, state)) {
82                                                 state = Transfer (cur, state);
83                                         } else {
84                                                 repeatOuter = true;
85                                                 break;
86                                         }
87                                 } while (HasSingleSuccessor (cur, out next) && !RequiresJoining (next));
88
89                                 if (repeatOuter)
90                                         continue;
91
92                                 foreach (APC successorAPC in Successors (cur)) {
93                                         if (!IsBottom (successorAPC, state))
94                                                 PushState (cur, successorAPC, state);
95                                 }
96                         }
97                 }
98
99                 protected virtual void Dump (AState state)
100                 {
101                 }
102
103                 public IEnumerable<KeyValuePair<APC, AState>> States ()
104                 {
105                         return this.JoinState;
106                 }
107
108                 protected abstract IEnumerable<APC> Successors (APC pc);
109
110                 protected virtual void PushState (APC current, APC next, AState state)
111                 {
112                         state = ImmutableVersion (state, next);
113                         if (RequiresJoining (next)) {
114                                 if (!JoinStateAtBlock (new Pair<APC, APC> (current, next), state))
115                                         return;
116                                 this.pending.Enqueue (next);
117                         } else {
118                                 this.JoinState [next] = state;
119                                 this.pending.Enqueue (next);
120                         }
121                 }
122
123                 private bool JoinStateAtBlock (Pair<APC, APC> edge, AState state)
124                 {
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));
128                                 AState joinedState;
129                                 bool result = Join (edge, state, existingState, out joinedState, widen);
130                                 if (result)
131                                         this.JoinState [edge.Value] = ImmutableVersion (joinedState, edge.Value);
132                                 return result;
133                         }
134
135                         this.JoinState.Add (edge.Value, state);
136                         return true;
137                 }
138
139                 protected abstract bool IsBackEdge (APC from, APC to);
140
141                 protected abstract int WorkingListComparer (APC a, APC b);
142
143                 protected abstract bool Join (Pair<APC, APC> edge, AState newState, AState existingState, out AState joinedState, bool widen);
144
145                 protected abstract bool RequiresJoining (APC pc);
146
147                 protected abstract bool HasSingleSuccessor (APC pc, out APC next);
148
149                 protected abstract bool IsBottom (APC pc, AState state);
150
151                 protected abstract AState Transfer (APC pc, AState state);
152
153                 protected abstract AState MutableVersion (AState state, APC at);
154                 protected abstract AState ImmutableVersion (AState state, APC at);
155
156                 public void PrintStatesAtJoinPoints (TextWriter tw)
157                 {
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);
161                         }
162                 }
163         }
164 }