summaryrefslogtreecommitdiff
path: root/ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs
blob: 83294fd91975b09ea85502b3f606e13c591aebd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

using ICSharpCode.Decompiler.Disassembler;
using Mono.Cecil.Cil;

namespace ICSharpCode.Decompiler.FlowAnalysis
{
	/// <summary>
	/// Type of the control flow node
	/// </summary>
	public enum ControlFlowNodeType
	{
		/// <summary>
		/// A normal node represents a basic block.
		/// </summary>
		Normal,
		/// <summary>
		/// The entry point of the method.
		/// </summary>
		EntryPoint,
		/// <summary>
		/// The exit point of the method (every ret instruction branches to this node)
		/// </summary>
		RegularExit,
		/// <summary>
		/// This node represents leaving a method irregularly by throwing an exception.
		/// </summary>
		ExceptionalExit,
		/// <summary>
		/// This node is used as a header for exception handler blocks.
		/// </summary>
		CatchHandler,
		/// <summary>
		/// This node is used as a header for finally blocks and fault blocks.
		/// Every leave instruction in the try block leads to the handler of the containing finally block;
		/// and exceptional control flow also leads to this handler.
		/// </summary>
		FinallyOrFaultHandler,
		/// <summary>
		/// This node is used as footer for finally blocks and fault blocks.
		/// Depending on the "copyFinallyBlocks" option used when creating the graph, it is connected with all leave targets using
		/// EndFinally edges (when not copying); or with a specific leave target using a normal edge (when copying).
		/// For fault blocks, an exception edge is used to represent the "re-throwing" of the exception.
		/// </summary>
		EndFinallyOrFault
	}
	
	/// <summary>
	/// Represents a block in the control flow graph.
	/// </summary>
	public sealed class ControlFlowNode
	{
		/// <summary>
		/// Index of this node in the ControlFlowGraph.Nodes collection.
		/// </summary>
		public readonly int BlockIndex;
		
		/// <summary>
		/// Gets the IL offset of this node.
		/// </summary>
		public readonly int Offset;
		
		/// <summary>
		/// Type of the node.
		/// </summary>
		public readonly ControlFlowNodeType NodeType;
		
		/// <summary>
		/// If this node is a FinallyOrFaultHandler node, this field points to the corresponding EndFinallyOrFault node.
		/// Otherwise, this field is null.
		/// </summary>
		public readonly ControlFlowNode EndFinallyOrFaultNode;
		
		/// <summary>
		/// Visited flag, used in various algorithms.
		/// Before using it in your algorithm, reset it to false by calling ControlFlowGraph.ResetVisited();
		/// </summary>
		public bool Visited;
		
		/// <summary>
		/// Gets whether this node is reachable. Requires that dominance is computed!
		/// </summary>
		public bool IsReachable {
			get { return ImmediateDominator != null || NodeType == ControlFlowNodeType.EntryPoint; }
		}
		
		/// <summary>
		/// Signalizes that this node is a copy of another node.
		/// </summary>
		public ControlFlowNode CopyFrom { get; internal set; }
		
		/// <summary>
		/// Gets the immediate dominator (the parent in the dominator tree).
		/// Null if dominance has not been calculated; or if the node is unreachable.
		/// </summary>
		public ControlFlowNode ImmediateDominator { get; internal set; }
		
		/// <summary>
		/// List of children in the dominator tree.
		/// </summary>
		public readonly List<ControlFlowNode> DominatorTreeChildren = new List<ControlFlowNode>();
		
		/// <summary>
		/// The dominance frontier of this node.
		/// This is the set of nodes for which this node dominates a predecessor, but which are not strictly dominated by this node.
		/// </summary>
		/// <remarks>
		/// b.DominanceFrontier = { y in CFG; (exists p in predecessors(y): b dominates p) and not (b strictly dominates y)}
		/// </remarks>
		public HashSet<ControlFlowNode> DominanceFrontier;
		
		/// <summary>
		/// Start of code block represented by this node. Only set for nodetype == Normal.
		/// </summary>
		public readonly Instruction Start;
		
		/// <summary>
		/// End of the code block represented by this node. Only set for nodetype == Normal.
		/// The end is exclusive, the end instruction itself does not belong to this block.
		/// </summary>
		public readonly Instruction End;
		
		/// <summary>
		/// Gets the exception handler associated with this node.
		/// Only set for nodetype == CatchHandler or nodetype == FinallyOrFaultHandler.
		/// </summary>
		public readonly ExceptionHandler ExceptionHandler;
		
		/// <summary>
		/// List of incoming control flow edges.
		/// </summary>
		public readonly List<ControlFlowEdge> Incoming = new List<ControlFlowEdge>();
		
		/// <summary>
		/// List of outgoing control flow edges.
		/// </summary>
		public readonly List<ControlFlowEdge> Outgoing = new List<ControlFlowEdge>();
		
		/// <summary>
		/// Any user data
		/// </summary>
		public object UserData;
		
		internal ControlFlowNode(int blockIndex, int offset, ControlFlowNodeType nodeType)
		{
			BlockIndex = blockIndex;
			Offset = offset;
			NodeType = nodeType;
		}
		
		internal ControlFlowNode(int blockIndex, Instruction start, Instruction end)
		{
			if (start == null)
				throw new ArgumentNullException("start");
			if (end == null)
				throw new ArgumentNullException("end");
			BlockIndex = blockIndex;
			NodeType = ControlFlowNodeType.Normal;
			Start = start;
			End = end;
			Offset = start.Offset;
		}
		
		internal ControlFlowNode(int blockIndex, ExceptionHandler exceptionHandler, ControlFlowNode endFinallyOrFaultNode)
		{
			BlockIndex = blockIndex;
			NodeType = endFinallyOrFaultNode != null ? ControlFlowNodeType.FinallyOrFaultHandler : ControlFlowNodeType.CatchHandler;
			ExceptionHandler = exceptionHandler;
			EndFinallyOrFaultNode = endFinallyOrFaultNode;
			Debug.Assert((exceptionHandler.HandlerType == ExceptionHandlerType.Finally || exceptionHandler.HandlerType == ExceptionHandlerType.Fault) == (endFinallyOrFaultNode != null));
			Offset = exceptionHandler.HandlerStart.Offset;
		}
		
		/// <summary>
		/// Gets all predecessors (=sources of incoming edges)
		/// </summary>
		public IEnumerable<ControlFlowNode> Predecessors {
			get {
				return Incoming.Select(e => e.Source);
			}
		}
		
		/// <summary>
		/// Gets all successors (=targets of outgoing edges)
		/// </summary>
		public IEnumerable<ControlFlowNode> Successors {
			get {
				return Outgoing.Select(e => e.Target);
			}
		}
		
		/// <summary>
		/// Gets all instructions in this node.
		/// Returns an empty list for special nodes that don't have any instructions.
		/// </summary>
		public IEnumerable<Instruction> Instructions {
			get {
				Instruction inst = Start;
				if (inst != null) {
					yield return inst;
					while (inst != End) {
						inst = inst.Next;
						yield return inst;
					}
				}
			}
		}
		
		public void TraversePreOrder(Func<ControlFlowNode, IEnumerable<ControlFlowNode>> children, Action<ControlFlowNode> visitAction)
		{
			if (Visited)
				return;
			Visited = true;
			visitAction(this);
			foreach (ControlFlowNode t in children(this))
				t.TraversePreOrder(children, visitAction);
		}
		
		public void TraversePostOrder(Func<ControlFlowNode, IEnumerable<ControlFlowNode>> children, Action<ControlFlowNode> visitAction)
		{
			if (Visited)
				return;
			Visited = true;
			foreach (ControlFlowNode t in children(this))
				t.TraversePostOrder(children, visitAction);
			visitAction(this);
		}
		
		public override string ToString()
		{
			StringWriter writer = new StringWriter();
			switch (NodeType) {
				case ControlFlowNodeType.Normal:
					writer.Write("Block #{0}", BlockIndex);
					if (Start != null)
						writer.Write(": IL_{0:x4}", Start.Offset);
					if (End != null)
						writer.Write(" to IL_{0:x4}", End.GetEndOffset());
					break;
				case ControlFlowNodeType.CatchHandler:
				case ControlFlowNodeType.FinallyOrFaultHandler:
					writer.Write("Block #{0}: {1}: ", BlockIndex, NodeType);
					DisassemblerHelpers.WriteTo(ExceptionHandler, new PlainTextOutput(writer));
					break;
				default:
					writer.Write("Block #{0}: {1}", BlockIndex, NodeType);
					break;
			}
//			if (ImmediateDominator != null) {
//				writer.WriteLine();
//				writer.Write("ImmediateDominator: #{0}", ImmediateDominator.BlockIndex);
//			}
			if (DominanceFrontier != null && DominanceFrontier.Any()) {
				writer.WriteLine();
				writer.Write("DominanceFrontier: " + string.Join(",", DominanceFrontier.OrderBy(d => d.BlockIndex).Select(d => d.BlockIndex.ToString())));
			}
			foreach (Instruction inst in Instructions) {
				writer.WriteLine();
				DisassemblerHelpers.WriteTo(inst, new PlainTextOutput(writer));
			}
			if (UserData != null) {
				writer.WriteLine();
				writer.Write(UserData.ToString());
			}
			return writer.ToString();
		}
		
		/// <summary>
		/// Gets whether <c>this</c> dominates <paramref name="node"/>.
		/// </summary>
		public bool Dominates(ControlFlowNode node)
		{
			// TODO: this can be made O(1) by numbering the dominator tree
			ControlFlowNode tmp = node;
			while (tmp != null) {
				if (tmp == this)
					return true;
				tmp = tmp.ImmediateDominator;
			}
			return false;
		}
	}
}