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 @@ -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<Mono.Cecil.Cil.ExceptionHandler, ILVariable> variableByExceptionHandler;
readonly Dictionary<System.Reflection.Metadata.ExceptionRegion, ILVariable> variableByExceptionHandler;
/// <summary>
/// Gets/Sets whether to create extended basic blocks instead of basic blocks.
@ -39,8 +39,8 @@ namespace ICSharpCode.Decompiler.IL @@ -39,8 +39,8 @@ namespace ICSharpCode.Decompiler.IL
/// </summary>
public bool CreateExtendedBlocks;
internal BlockBuilder(Mono.Cecil.Cil.MethodBody body, IDecompilerTypeSystem typeSystem,
Dictionary<Mono.Cecil.Cil.ExceptionHandler, ILVariable> variableByExceptionHandler)
internal BlockBuilder(System.Reflection.Metadata.MethodBodyBlock body, IDecompilerTypeSystem typeSystem,
Dictionary<System.Reflection.Metadata.ExceptionRegion, ILVariable> variableByExceptionHandler)
{
Debug.Assert(body != null);
Debug.Assert(typeSystem != null);
@ -56,17 +56,17 @@ namespace ICSharpCode.Decompiler.IL @@ -56,17 +56,17 @@ namespace ICSharpCode.Decompiler.IL
void CreateContainerStructure()
{
List<TryCatch> tryCatchList = new List<TryCatch>();
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 @@ -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 @@ -111,7 +111,7 @@ namespace ICSharpCode.Decompiler.IL
public void CreateBlocks(BlockContainer mainContainer, List<ILInstruction> 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 @@ -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);
}

8
ICSharpCode.Decompiler/SRMExtensions.cs

@ -112,6 +112,14 @@ namespace ICSharpCode.Decompiler @@ -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)

Loading…
Cancel
Save