diff --git a/ICSharpCode.Decompiler/IL/BlockBuilder.cs b/ICSharpCode.Decompiler/IL/BlockBuilder.cs index e7259f3c3..b002dcaf3 100644 --- a/ICSharpCode.Decompiler/IL/BlockBuilder.cs +++ b/ICSharpCode.Decompiler/IL/BlockBuilder.cs @@ -29,9 +29,9 @@ namespace ICSharpCode.Decompiler.IL { class BlockBuilder { - readonly Mono.Cecil.Cil.MethodBody body; + readonly System.Reflection.Metadata.MethodBodyBlock body; readonly IDecompilerTypeSystem typeSystem; - readonly Dictionary variableByExceptionHandler; + readonly Dictionary variableByExceptionHandler; /// /// Gets/Sets whether to create extended basic blocks instead of basic blocks. @@ -39,8 +39,8 @@ namespace ICSharpCode.Decompiler.IL /// public bool CreateExtendedBlocks; - internal BlockBuilder(Mono.Cecil.Cil.MethodBody body, IDecompilerTypeSystem typeSystem, - Dictionary variableByExceptionHandler) + internal BlockBuilder(System.Reflection.Metadata.MethodBodyBlock body, IDecompilerTypeSystem typeSystem, + Dictionary variableByExceptionHandler) { Debug.Assert(body != null); Debug.Assert(typeSystem != null); @@ -56,17 +56,17 @@ namespace ICSharpCode.Decompiler.IL void CreateContainerStructure() { List tryCatchList = new List(); - foreach (var eh in body.ExceptionHandlers) { - var tryRange = new Interval(eh.TryStart.Offset, eh.TryEnd != null ? eh.TryEnd.Offset : body.CodeSize); + foreach (var eh in body.ExceptionRegions) { + var tryRange = new Interval(eh.TryOffset, eh.TryOffset + eh.TryLength); var handlerBlock = new BlockContainer(); - handlerBlock.ILRange = new Interval(eh.HandlerStart.Offset, eh.HandlerEnd != null ? eh.HandlerEnd.Offset : body.CodeSize); + handlerBlock.ILRange = new Interval(eh.HandlerOffset, eh.HandlerOffset + eh.HandlerLength); handlerBlock.Blocks.Add(new Block()); handlerContainers.Add(handlerBlock.ILRange.Start, handlerBlock); - if (eh.HandlerType == Mono.Cecil.Cil.ExceptionHandlerType.Fault || eh.HandlerType == Mono.Cecil.Cil.ExceptionHandlerType.Finally) { + if (eh.Kind == System.Reflection.Metadata.ExceptionRegionKind.Fault || eh.Kind == System.Reflection.Metadata.ExceptionRegionKind.Finally) { var tryBlock = new BlockContainer(); tryBlock.ILRange = tryRange; - if (eh.HandlerType == Mono.Cecil.Cil.ExceptionHandlerType.Finally) + if (eh.Kind == System.Reflection.Metadata.ExceptionRegionKind.Finally) tryInstructionList.Add(new TryFinally(tryBlock, handlerBlock)); else tryInstructionList.Add(new TryFault(tryBlock, handlerBlock)); @@ -83,9 +83,9 @@ namespace ICSharpCode.Decompiler.IL } ILInstruction filter; - if (eh.HandlerType == Mono.Cecil.Cil.ExceptionHandlerType.Filter) { + if (eh.Kind == System.Reflection.Metadata.ExceptionRegionKind.Filter) { var filterBlock = new BlockContainer(expectedResultType: StackType.I4); - filterBlock.ILRange = new Interval(eh.FilterStart.Offset, eh.HandlerStart.Offset); + filterBlock.ILRange = new Interval(eh.FilterOffset, eh.HandlerOffset); filterBlock.Blocks.Add(new Block()); handlerContainers.Add(filterBlock.ILRange.Start, filterBlock); filter = filterBlock; @@ -111,7 +111,7 @@ namespace ICSharpCode.Decompiler.IL public void CreateBlocks(BlockContainer mainContainer, List instructions, BitArray incomingBranches, CancellationToken cancellationToken) { CreateContainerStructure(); - mainContainer.ILRange = new Interval(0, body.CodeSize); + mainContainer.ILRange = new Interval(0, body.GetCodeSize()); currentContainer = mainContainer; if (instructions.Count == 0) { currentContainer.Blocks.Add(new Block { @@ -161,7 +161,7 @@ namespace ICSharpCode.Decompiler.IL else if (!CreateExtendedBlocks && inst.HasFlag(InstructionFlags.MayBranch)) FinalizeCurrentBlock(inst.ILRange.End, fallthrough: true); } - FinalizeCurrentBlock(body.CodeSize, fallthrough: false); + FinalizeCurrentBlock(mainContainer.ILRange.End, fallthrough: false); containerStack.Clear(); ConnectBranches(mainContainer, cancellationToken); } diff --git a/ICSharpCode.Decompiler/SRMExtensions.cs b/ICSharpCode.Decompiler/SRMExtensions.cs index 85e931051..3efa8915a 100644 --- a/ICSharpCode.Decompiler/SRMExtensions.cs +++ b/ICSharpCode.Decompiler/SRMExtensions.cs @@ -112,6 +112,14 @@ namespace ICSharpCode.Decompiler (methodDefinition.ImplAttributes & MethodImplAttributes.Runtime) == 0; } + public static int GetCodeSize(this MethodBodyBlock body) + { + if (body == null) + throw new ArgumentNullException(nameof(body)); + + return body.GetILReader().Length; + } + public static MethodDefinitionHandle GetAny(this PropertyAccessors accessors) { if (!accessors.Getter.IsNil)