-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGeneratingMethodBodyDisassembler.cs
295 lines (280 loc) · 13.2 KB
/
GeneratingMethodBodyDisassembler.cs
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Disassembler;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
namespace ILGenerationLanguage.Plugin
{
public class GeneratingMethodBodyDisassembler
{
private readonly ITextOutput output;
private readonly CancellationToken cancellationToken;
private readonly MethodInstructionComparer instructionComparer = new MethodInstructionComparer();
public GeneratingMethodBodyDisassembler(ITextOutput output, CancellationToken cancellationToken)
{
this.output = output ?? throw new ArgumentNullException(nameof(output));
this.cancellationToken = cancellationToken;
}
private List<TResult> FlattenStructure<TResult, TAggregate>(TAggregate root, Func<TAggregate, IEnumerable<TAggregate>> propertySelector, Func<TAggregate, int, TResult> resultSelector)
{
List<TResult> flatList = new List<TResult>();
flatList.Add(resultSelector(root, 0));
InternalFlattenStructure(propertySelector(root), propertySelector, flatList, 1, resultSelector);
return flatList;
}
private void InternalFlattenStructure<TResult, TAggregate>(IEnumerable<TAggregate> listToFlatten, Func<TAggregate, IEnumerable<TAggregate>> propertySelector, List<TResult> currentAggregate, int level, Func<TAggregate, int, TResult> resultSelector)
{
foreach (var element in listToFlatten)
{
currentAggregate.Add(resultSelector(element, level));
}
foreach (var element in listToFlatten)
{
InternalFlattenStructure(propertySelector(element), propertySelector, currentAggregate, level + 1, resultSelector);
}
}
public virtual void Disassemble(MethodBody body)
{
Dictionary<VariableReference, string> locals = ProcessLocalVariables(body);
Dictionary<Instruction, string> labelDictionary = ProcessLabels(body);
ILStructure iLStructure = new ILStructure(body);
var flattenedStructure = FlattenStructure(iLStructure, il => il.Children, (il, level) => new IlStructureWithLevel { Level = level, Structure = il });
foreach (var inst in body.Method.Body.Instructions)
{
WriteInstruction(output, inst, flattenedStructure, labelDictionary, locals);
}
}
private Dictionary<Instruction, string> ProcessLabels(MethodBody body)
{
Dictionary<Instruction, string> labelDictionary = GenerateLabels(body.Method.Body.Instructions);
WriteLabels(output, labelDictionary);
output.WriteLine();
return labelDictionary;
}
private Dictionary<Instruction, string> GenerateLabels(Collection<Instruction> instructions)
{
var labels =
instructions.Select(i => i.Operand)
.OfType<Instruction>()
.Distinct()
.ToDictionary(i => i, i => $"label_{i.Offset}", instructionComparer);
var multiLabels =
instructions.Select(i => i.Operand)
.OfType<Instruction[]>()
.SelectMany(l => l)
.Distinct()
.ToDictionary(i => i, i => $"label_{i.Offset}", instructionComparer);
foreach (var multiLabel in multiLabels)
{
if (!labels.Any(l => l.Key.Offset == multiLabel.Key.Offset))
{
labels.Add(multiLabel.Key, multiLabel.Value);
}
}
return labels;
}
private void WriteLabels(ITextOutput output, Dictionary<Instruction, string> labelDictionary)
{
foreach (var label in labelDictionary.Values)
{
output.WriteLine($"var {label} = generator.DefineLabel();");
}
output.WriteLine();
}
private Dictionary<VariableReference, string> ProcessLocalVariables(MethodBody body)
{
var locals = GetLocalVariables(body);
WriteLocalVariables(locals);
output.WriteLine();
return locals;
}
private Dictionary<VariableReference, string> GetLocalVariables(MethodBody body)
{
return body.Variables.ToDictionary(v => (VariableReference)v, v => $"variable_{v.Index}");
}
private void WriteLocalVariables(Dictionary<VariableReference, string> variables)
{
foreach (var local in variables)
{
output.Write($"Localbuilder {local.Value} = generator.DeclareLocal({local.Key.VariableType.ToTypeOf()});");
output.WriteLine();
}
}
protected virtual void WriteInstruction(ITextOutput output, Instruction instruction, List<IlStructureWithLevel> iLStructures, Dictionary<Instruction, string> labels, Dictionary<VariableReference, string> variables)
{
if (labels.TryGetValue(instruction, out var labelName))
{
output.WriteLine($"generator.MarkLabel({labelName});");
}
var startStructures = iLStructures.Where(s=>s.Structure.Type!=ILStructureType.Root && s.Structure.Type != ILStructureType.Loop)
.Where(s => s.Structure.StartOffset == instruction.Offset)
.ToList();
foreach (var startStructure in startStructures)
{
switch (startStructure.Structure.Type)
{
case ILStructureType.Try:
output.WriteLine("generator.BeginExceptionBlock();");
break;
case ILStructureType.Handler:
switch (startStructure.Structure.ExceptionHandler.HandlerType)
{
case ExceptionHandlerType.Catch:
case ExceptionHandlerType.Filter:
output.WriteLine($"generator.BeginCatchBlock({startStructure.Structure.ExceptionHandler.CatchType?.ToTypeOf() ?? string.Empty});");
break;
case ExceptionHandlerType.Finally:
output.WriteLine("generator.BeginFinallyBlock();");
break;
case ExceptionHandlerType.Fault:
output.WriteLine("generator.BeginFaultBlock();");
break;
}
break;
case ILStructureType.Filter:
output.WriteLine("generator.BeginExceptFilterBlock();");
break;
}
}
var endStructures = iLStructures.Where(s => s.Structure.Type != ILStructureType.Root && s.Structure.Type != ILStructureType.Loop)
.Where(s => s.Structure.EndOffset == instruction.Offset)
.ToList();
foreach (var endStructure in endStructures)
{
switch (endStructure.Structure.Type)
{
case ILStructureType.Try:
output.WriteLine("// end try");
break;
case ILStructureType.Handler:
switch (endStructure.Structure.ExceptionHandler.HandlerType)
{
case ExceptionHandlerType.Catch:
case ExceptionHandlerType.Filter:
if (iLStructures.Where(il => il.Level == endStructure.Level).OrderBy(il => il.Structure.EndOffset).Last() == endStructure)
output.WriteLine("generator.EndExceptionBlock();");
output.WriteLine("// end catch");
break;
case ExceptionHandlerType.Finally:
output.WriteLine("generator.EndExceptionBlock();");
output.WriteLine("// end finally");
break;
case ExceptionHandlerType.Fault:
output.WriteLine("// end fault");
break;
}
break;
case ILStructureType.Filter:
output.WriteLine("// end filter");
break;
}
}
output.Write($"generator.Emit(OpCodes.{instruction.OpCode.Code}");
if (instruction.Operand != null)
{
output.Write(",");
WriteOperand(output, instruction.Operand, labels, variables);
}
output.WriteLine(");");
}
private void WriteOperand(ITextOutput output, object operand, Dictionary<Instruction, string> labels, Dictionary<VariableReference, string> locals)
{
if (operand is MethodReference methodReference)
{
var methodDefinition = methodReference.Resolve();
output.WriteTypeOf(methodDefinition.DeclaringType);
if (methodDefinition.IsGetter)
{
output.Write($".GetProperty(\"{methodDefinition.Name.Split('_')[1]}\").GetGetMethod(true)");
}
else if (methodDefinition.IsSetter)
{
output.Write($".GetProperty(\"{methodDefinition.Name.Split('_')[1]}\").GetSetMethod(true)");
}
else
{
if (methodDefinition.IsConstructor)
{
output.Write($".GetConstructor(");
}
else
{
output.Write($".GetMethod(\"{methodDefinition.Name}\",");
}
if (methodReference.HasParameters)
{
output.Write($"new Type[] {{ {string.Join(", ", methodDefinition.Parameters.Select(p => p.ParameterType.ToTypeOf()))} }}");
}
else
{
output.Write("Type.EmptyTypes");
}
output.Write(")");
}
}
else if (operand is string stringOperand)
{
output.Write($"\"{stringOperand}\"");
}
else if (operand is double doubleOperand)
{
output.Write(doubleOperand.ToString(CultureInfo.InvariantCulture));
}
else if (operand is float floatOperand)
{
output.Write(floatOperand.ToString(CultureInfo.InvariantCulture));
}
else if (operand is byte byteOperand)
{
output.Write(byteOperand.ToString(CultureInfo.InvariantCulture));
}
else if (operand is int intOperand)
{
output.Write(intOperand.ToString(CultureInfo.InvariantCulture));
}
else if (operand is long longOperand)
{
output.Write(longOperand.ToString(CultureInfo.InvariantCulture));
}
else if (operand is sbyte sbyteOperand)
{
output.Write(sbyteOperand.ToString(CultureInfo.InvariantCulture));
}
else if (operand is Instruction instruction)
{
output.Write(labels.Single(l => l.Key.Offset == instruction.Offset).Value);
}
else if (operand is Instruction[] instructions)
{
var labelParams = string.Join(", ", instructions.Select(i => labels[i]));
output.Write($"new Label[] {{ {labelParams} }}");
}
else if (operand is FieldReference fieldReference)
{
output.WriteTypeOf(fieldReference.FieldType);
output.Write($".GetField(\"{fieldReference.Name}\", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)");
}
else if (operand is VariableReference variable)
{
output.Write(locals.Single(l => l.Key.Index == variable.Index).Value);
}
else if (operand is TypeReference type)
{
output.WriteTypeOf(type);
}
else if (operand is ParameterReference param)
{
output.Write(param.Index.ToString());
}
else
{
output.WriteLine($"Unsupported parameter: {operand.GetType()}");
}
}
}
}