Browse Source

Fix building TryCatchHandlers

pull/728/head
Daniel Grunwald 10 years ago
parent
commit
1e2147d1bd
  1. 15
      ICSharpCode.Decompiler/IL/BlockBuilder.cs
  2. 11
      ICSharpCode.Decompiler/IL/ILReader.cs
  3. 2
      NRefactory

15
ICSharpCode.Decompiler/IL/BlockBuilder.cs

@ -31,6 +31,7 @@ namespace ICSharpCode.Decompiler.IL @@ -31,6 +31,7 @@ namespace ICSharpCode.Decompiler.IL
{
readonly Mono.Cecil.Cil.MethodBody body;
readonly IDecompilerTypeSystem typeSystem;
readonly Dictionary<Mono.Cecil.Cil.ExceptionHandler, ILVariable> variableByExceptionHandler;
/// <summary>
/// Gets/Sets whether to create extended basic blocks instead of basic blocks.
@ -38,12 +39,15 @@ namespace ICSharpCode.Decompiler.IL @@ -38,12 +39,15 @@ namespace ICSharpCode.Decompiler.IL
/// </summary>
public bool CreateExtendedBlocks;
public BlockBuilder(Mono.Cecil.Cil.MethodBody body, IDecompilerTypeSystem typeSystem)
internal BlockBuilder(Mono.Cecil.Cil.MethodBody body, IDecompilerTypeSystem typeSystem,
Dictionary<Mono.Cecil.Cil.ExceptionHandler, ILVariable> variableByExceptionHandler)
{
Debug.Assert(body != null);
Debug.Assert(typeSystem != null);
Debug.Assert(variableByExceptionHandler != null);
this.body = body;
this.typeSystem = typeSystem;
this.variableByExceptionHandler = variableByExceptionHandler;
}
List<TryInstruction> tryInstructionList = new List<TryInstruction>();
@ -78,25 +82,18 @@ namespace ICSharpCode.Decompiler.IL @@ -78,25 +82,18 @@ namespace ICSharpCode.Decompiler.IL
tryInstructionList.Add(tryCatch);
}
ILVariable variable = null;
throw new NotImplementedException();
//var variable = new ILVariable(VariableKind.Exception, typeSystem.Resolve(eh.CatchType), handlerBlock.ILRange.Start);
variable.Name = "ex";
handlerBlock.EntryPoint.Instructions.Add(new LdLoc(variable));
ILInstruction filter;
if (eh.HandlerType == Mono.Cecil.Cil.ExceptionHandlerType.Filter) {
var filterBlock = new BlockContainer();
filterBlock.ILRange = new Interval(eh.FilterStart.Offset, eh.HandlerStart.Offset);
filterBlock.Blocks.Add(new Block());
filterBlock.EntryPoint.Instructions.Add(new LdLoc(variable));
handlerContainers.Add(filterBlock.ILRange.Start, filterBlock);
filter = filterBlock;
} else {
filter = new LdcI4(1);
}
tryCatch.Handlers.Add(new TryCatchHandler(filter, handlerBlock, variable));
tryCatch.Handlers.Add(new TryCatchHandler(filter, handlerBlock, variableByExceptionHandler[eh]));
}
if (tryInstructionList.Count > 0) {
tryInstructionList = tryInstructionList.OrderBy(tc => tc.TryBlock.ILRange.Start).ThenByDescending(tc => tc.TryBlock.ILRange.End).ToList();

11
ICSharpCode.Decompiler/IL/ILReader.cs

@ -68,6 +68,7 @@ namespace ICSharpCode.Decompiler.IL @@ -68,6 +68,7 @@ namespace ICSharpCode.Decompiler.IL
// Dictionary that stores stacks for each IL instruction
Dictionary<int, ImmutableStack<ILVariable>> stackByOffset;
Dictionary<Cil.ExceptionHandler, ILVariable> variableByExceptionHandler;
UnionFind<ILVariable> unionFind;
IEnumerable<ILVariable> stackVariables;
@ -84,6 +85,7 @@ namespace ICSharpCode.Decompiler.IL @@ -84,6 +85,7 @@ namespace ICSharpCode.Decompiler.IL
this.instructionBuilder = new List<ILInstruction>();
this.isBranchTarget = new BitArray(body.CodeSize);
this.stackByOffset = new Dictionary<int, ImmutableStack<ILVariable>>();
this.variableByExceptionHandler = new Dictionary<Cil.ExceptionHandler, ILVariable>();
}
IMetadataTokenProvider ReadAndDecodeMetadataToken()
@ -160,6 +162,7 @@ namespace ICSharpCode.Decompiler.IL @@ -160,6 +162,7 @@ namespace ICSharpCode.Decompiler.IL
void MergeStacks(ImmutableStack<ILVariable> a, ImmutableStack<ILVariable> b)
{
Debug.Assert(a.Count() == b.Count());
var enum1 = a.GetEnumerator();
var enum2 = b.GetEnumerator();
while (enum1.MoveNext() && enum2.MoveNext()) {
@ -183,9 +186,9 @@ namespace ICSharpCode.Decompiler.IL @@ -183,9 +186,9 @@ namespace ICSharpCode.Decompiler.IL
foreach (var eh in body.ExceptionHandlers) {
ImmutableStack<ILVariable> ehStack = null;
if (eh.HandlerType == Cil.ExceptionHandlerType.Catch || eh.HandlerType == Cil.ExceptionHandlerType.Filter) {
ehStack = ImmutableStack.Create(
new ILVariable(VariableKind.Exception, typeSystem.Resolve(eh.CatchType), eh.HandlerStart.Offset)
);
var v = new ILVariable(VariableKind.Exception, typeSystem.Resolve(eh.CatchType), eh.HandlerStart.Offset);
variableByExceptionHandler.Add(eh, v);
ehStack = ImmutableStack.Create(v);
} else {
ehStack = ImmutableStack<ILVariable>.Empty;
}
@ -262,7 +265,7 @@ namespace ICSharpCode.Decompiler.IL @@ -262,7 +265,7 @@ namespace ICSharpCode.Decompiler.IL
{
Init(body);
ReadInstructions(cancellationToken);
var container = new BlockBuilder(body, typeSystem).CreateBlocks(instructionBuilder, isBranchTarget);
var container = new BlockBuilder(body, typeSystem, variableByExceptionHandler).CreateBlocks(instructionBuilder, isBranchTarget);
var function = new ILFunction(body.Method, container);
function.Variables.AddRange(parameterVariables);
function.Variables.AddRange(localVariables);

2
NRefactory

@ -1 +1 @@ @@ -1 +1 @@
Subproject commit 3af570c381d98b9560f38058c728e0a97d918b07
Subproject commit 6e6d7778883fb135a0b6945cc4696c176b9c5e3a
Loading…
Cancel
Save