Repository navigation
Expand file tree
/
Copy pathILAstDumper.cs
More file actions
209 lines (190 loc) · 8.11 KB
/
Copy pathILAstDumper.cs
File metadata and controls
209 lines (190 loc) · 8.11 KB
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
// Copyright (c) 2026 Siegfried Pammer
//
// 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.
#if DEBUG
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection.Metadata;
using System.Threading;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.IL;
using ICSharpCode.Decompiler.IL.Transforms;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.ILSpyCmd
{
/// <summary>
/// Writes the decompiler's intermediate representation (ILAst) of a method body, optionally
/// stopping the IL transform pipeline after a chosen transform. This is the command-line
/// counterpart of the UI's Debug Steps pane, and makes transform output diffable.
/// </summary>
class ILAstDumper
{
static readonly IReadOnlyList<IILTransform> transforms = CSharpDecompiler.GetILTransforms();
readonly ILAstWritingOptions writingOptions = new ILAstWritingOptions {
// same sugar as the Debug Steps pane's ILAst dump, so its output and this one are diffable
UseFieldSugar = true,
UseLogicOperationSugar = true,
};
/// <summary>
/// Names of the IL transforms, in the order they run, as <c>--after-transform</c> accepts
/// them. Transforms nested inside a BlockILTransform (LoopDetection, ConditionDetection,
/// the statement transforms, ...) have no name of their own here: they run as part of
/// their containing entry and cannot be stopped after individually.
/// </summary>
public static IReadOnlyList<string> TransformNames { get; } =
transforms.Select(t => t.GetType().Name).ToArray();
public static int TransformCount => TransformNames.Count;
/// <summary>
/// How a pipeline entry is displayed: a BlockILTransform also names the transforms it
/// runs, which are otherwise invisible - and two BlockILTransform entries would be
/// indistinguishable.
/// </summary>
static string Describe(int index)
{
return transforms[index] is BlockILTransform block ? block.ToString() : TransformNames[index];
}
/// <summary>
/// The 1-based index of the pipeline entry running <paramref name="name"/> as one of its
/// nested transforms, or 0 if no entry does.
/// </summary>
static int FindContainingEntry(string name)
{
for (int i = 0; i < transforms.Count; i++)
{
if (transforms[i] is BlockILTransform block
&& block.PreOrderTransforms.Concat(block.PostOrderTransforms)
.Any(t => string.Equals(t.GetType().Name, name, StringComparison.OrdinalIgnoreCase)))
{
return i + 1;
}
}
return 0;
}
/// <summary>
/// The pipeline as displayed to the user: one transform per line, prefixed by the
/// 1-based index that <c>--after-transform</c> accepts.
/// </summary>
public static string DescribePipeline()
{
return string.Join(Environment.NewLine,
Enumerable.Range(0, TransformCount).Select(index => $" {index + 1,3} {Describe(index)}"));
}
/// <summary>
/// Maps the value of <c>--after-transform</c> to the number of transforms to run.
/// Accepts a 1-based pipeline index, or a transform name if it occurs exactly once;
/// names that run repeatedly (SplitVariables, ControlFlowSimplification, ...) have to
/// be selected by index.
/// </summary>
public static bool TryResolveTransformCount(string nameOrIndex, out int count, out string error)
{
count = 0;
error = null;
string trimmed = nameOrIndex.Trim();
if (int.TryParse(trimmed, NumberStyles.None, CultureInfo.InvariantCulture, out int index))
{
if (index < 1 || index > TransformCount)
{
error = $"'{trimmed}' is out of range; the pipeline has {TransformCount} transforms:{Environment.NewLine}{DescribePipeline()}";
return false;
}
count = index;
return true;
}
var occurrences = TransformNames
.Select((name, i) => (name, position: i + 1))
.Where(t => string.Equals(t.name, trimmed, StringComparison.OrdinalIgnoreCase))
.Select(t => t.position)
.ToArray();
if (occurrences.Length == 0)
{
int containingEntry = FindContainingEntry(trimmed);
error = containingEntry > 0
? $"'{trimmed}' runs inside the transform at index {containingEntry} and cannot be stopped after on its own. Pass that index to stop after the whole entry:{Environment.NewLine}{DescribePipeline()}"
: $"Unknown transform '{trimmed}'. Pass one of these names, or its index:{Environment.NewLine}{DescribePipeline()}";
return false;
}
if (occurrences.Length > 1)
{
error = $"'{trimmed}' runs {occurrences.Length} times, at index {string.Join(", ", occurrences)}. Pass the index of the occurrence to stop after.";
return false;
}
count = occurrences[0];
return true;
}
/// <summary>
/// Writes the ILAst of a single method, or nothing at all if the method has no body
/// (abstract, extern or a runtime-provided implementation). Returns the failure if the
/// body could not be read, transformed or written, so that one broken method neither
/// aborts the dump nor makes the run look successful.
/// </summary>
public DecompilerException WriteMethod(CSharpDecompiler decompiler, DecompilerSettings settings, IMethod method,
int transformCount, ITextOutput output, CancellationToken cancellationToken)
{
if (method == null || method.MetadataToken.IsNil || method.MetadataToken.Kind != HandleKind.MethodDefinition)
return null;
var module = decompiler.TypeSystem.MainModule;
var metadataFile = module.MetadataFile;
var handle = (MethodDefinitionHandle)method.MetadataToken;
var methodDefinition = metadataFile.Metadata.GetMethodDefinition(handle);
if (!methodDefinition.HasBody())
return null;
output.WriteLine($"// {method.FullName}");
output.WriteLine($"// ILAst after {transformCount} of {TransformCount} transforms ({Describe(transformCount - 1)})");
DecompilerException error = null;
ILFunction function = null;
try
{
var reader = new ILReader(module) {
UseDebugSymbols = settings.UseDebugSymbols,
UseRefLocalsForAccurateOrderOfEvaluation = settings.UseRefLocalsForAccurateOrderOfEvaluation,
DebugInfo = decompiler.DebugInfoProvider,
};
var body = metadataFile.GetMethodBody(methodDefinition.RelativeVirtualAddress);
function = reader.ReadIL(handle, body, kind: ILFunctionKind.TopLevelFunction,
cancellationToken: cancellationToken);
ILTransformContext context = decompiler.CreateILTransformContext(function);
function.RunTransforms(transforms.Take(transformCount), context);
}
catch (Exception ex)
{
// Showing how far the pipeline got is the point of this command, so a crashing
// transform prints its exception and then the partially transformed function
// rather than aborting the whole dump.
output.WriteLine(ex.ToString());
output.WriteLine("// ILAst after the crash:");
error = new DecompilerException(module, method, ex);
}
try
{
function?.WriteTo(output, writingOptions);
}
catch (Exception ex)
{
output.WriteLine(ex.ToString());
error ??= new DecompilerException(module, method, ex);
}
output.WriteLine();
output.WriteLine();
return error;
}
}
}
#endif