Browse Source

port BlockBuilder

pull/1198/head
Siegfried Pammer 8 years ago
parent
commit
ddb74945b8
  1. 26
      ICSharpCode.Decompiler/IL/BlockBuilder.cs
  2. 8
      ICSharpCode.Decompiler/SRMExtensions.cs

26
ICSharpCode.Decompiler/IL/BlockBuilder.cs

@ -29,9 +29,9 @@ namespace ICSharpCode.Decompiler.IL
{ {
class BlockBuilder class BlockBuilder
{ {
readonly Mono.Cecil.Cil.MethodBody body; readonly System.Reflection.Metadata.MethodBodyBlock body;
readonly IDecompilerTypeSystem typeSystem; readonly IDecompilerTypeSystem typeSystem;
readonly Dictionary<Mono.Cecil.Cil.ExceptionHandler, ILVariable> variableByExceptionHandler; readonly Dictionary<System.Reflection.Metadata.ExceptionRegion, ILVariable> variableByExceptionHandler;
/// <summary> /// <summary>
/// Gets/Sets whether to create extended basic blocks instead of basic blocks. /// Gets/Sets whether to create extended basic blocks instead of basic blocks.
@ -39,8 +39,8 @@ namespace ICSharpCode.Decompiler.IL
/// </summary> /// </summary>
public bool CreateExtendedBlocks; public bool CreateExtendedBlocks;
internal BlockBuilder(Mono.Cecil.Cil.MethodBody body, IDecompilerTypeSystem typeSystem, internal BlockBuilder(System.Reflection.Metadata.MethodBodyBlock body, IDecompilerTypeSystem typeSystem,
Dictionary<Mono.Cecil.Cil.ExceptionHandler, ILVariable> variableByExceptionHandler) Dictionary<System.Reflection.Metadata.ExceptionRegion, ILVariable> variableByExceptionHandler)
{ {
Debug.Assert(body != null); Debug.Assert(body != null);
Debug.Assert(typeSystem != null); Debug.Assert(typeSystem != null);
@ -56,17 +56,17 @@ namespace ICSharpCode.Decompiler.IL
void CreateContainerStructure() void CreateContainerStructure()
{ {
List<TryCatch> tryCatchList = new List<TryCatch>(); List<TryCatch> tryCatchList = new List<TryCatch>();
foreach (var eh in body.ExceptionHandlers) { foreach (var eh in body.ExceptionRegions) {
var tryRange = new Interval(eh.TryStart.Offset, eh.TryEnd != null ? eh.TryEnd.Offset : body.CodeSize); var tryRange = new Interval(eh.TryOffset, eh.TryOffset + eh.TryLength);
var handlerBlock = new BlockContainer(); 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()); handlerBlock.Blocks.Add(new Block());
handlerContainers.Add(handlerBlock.ILRange.Start, handlerBlock); 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(); var tryBlock = new BlockContainer();
tryBlock.ILRange = tryRange; 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)); tryInstructionList.Add(new TryFinally(tryBlock, handlerBlock));
else else
tryInstructionList.Add(new TryFault(tryBlock, handlerBlock)); tryInstructionList.Add(new TryFault(tryBlock, handlerBlock));
@ -83,9 +83,9 @@ namespace ICSharpCode.Decompiler.IL
} }
ILInstruction filter; 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); 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()); filterBlock.Blocks.Add(new Block());
handlerContainers.Add(filterBlock.ILRange.Start, filterBlock); handlerContainers.Add(filterBlock.ILRange.Start, filterBlock);
filter = filterBlock; filter = filterBlock;
@ -111,7 +111,7 @@ namespace ICSharpCode.Decompiler.IL
public void CreateBlocks(BlockContainer mainContainer, List<ILInstruction> instructions, BitArray incomingBranches, CancellationToken cancellationToken) public void CreateBlocks(BlockContainer mainContainer, List<ILInstruction> instructions, BitArray incomingBranches, CancellationToken cancellationToken)
{ {
CreateContainerStructure(); CreateContainerStructure();
mainContainer.ILRange = new Interval(0, body.CodeSize); mainContainer.ILRange = new Interval(0, body.GetCodeSize());
currentContainer = mainContainer; currentContainer = mainContainer;
if (instructions.Count == 0) { if (instructions.Count == 0) {
currentContainer.Blocks.Add(new Block { currentContainer.Blocks.Add(new Block {
@ -161,7 +161,7 @@ namespace ICSharpCode.Decompiler.IL
else if (!CreateExtendedBlocks && inst.HasFlag(InstructionFlags.MayBranch)) else if (!CreateExtendedBlocks && inst.HasFlag(InstructionFlags.MayBranch))
FinalizeCurrentBlock(inst.ILRange.End, fallthrough: true); FinalizeCurrentBlock(inst.ILRange.End, fallthrough: true);
} }
FinalizeCurrentBlock(body.CodeSize, fallthrough: false); FinalizeCurrentBlock(mainContainer.ILRange.End, fallthrough: false);
containerStack.Clear(); containerStack.Clear();
ConnectBranches(mainContainer, cancellationToken); ConnectBranches(mainContainer, cancellationToken);
} }

8
ICSharpCode.Decompiler/SRMExtensions.cs

@ -112,6 +112,14 @@ namespace ICSharpCode.Decompiler
(methodDefinition.ImplAttributes & MethodImplAttributes.Runtime) == 0; (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) public static MethodDefinitionHandle GetAny(this PropertyAccessors accessors)
{ {
if (!accessors.Getter.IsNil) if (!accessors.Getter.IsNil)

Loading…
Cancel
Save