Browse Source

WIP on MetadataLoader, ILReader and DecompilerTypeSystem

pull/1198/head
Siegfried Pammer 7 years ago
parent
commit
f8446e113a
  1. 26
      ICSharpCode.Decompiler/IL/ILReader.cs
  2. 2
      ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs
  3. 2
      ICSharpCode.Decompiler/TypeSystem/IDecompilerTypeSystem.cs
  4. 3
      ICSharpCode.Decompiler/TypeSystem/MetadataLoader.cs

26
ICSharpCode.Decompiler/IL/ILReader.cs

@ -50,7 +50,6 @@ namespace ICSharpCode.Decompiler.IL @@ -50,7 +50,6 @@ namespace ICSharpCode.Decompiler.IL
}
MetadataReader metadata;
Metadata.MethodDefinition methodDefinition;
IMethod method;
MethodBodyBlock body;
Metadata.IDebugInfoProvider debugInfo;
@ -73,14 +72,13 @@ namespace ICSharpCode.Decompiler.IL @@ -73,14 +72,13 @@ namespace ICSharpCode.Decompiler.IL
List<(ILVariable, ILVariable)> stackMismatchPairs;
IEnumerable<ILVariable> stackVariables;
void Init(Metadata.MethodDefinition method, MethodBodyBlock body)
void Init(Metadata.PEFile module, MethodDefinitionHandle methodDefinitionHandle, MethodBodyBlock body)
{
if (body == null)
throw new ArgumentNullException(nameof(body));
this.metadata = method.Module.GetMetadataReader();
this.methodDefinition = method;
this.method = typeSystem.ResolveAsMethod(method.Handle);
this.methodSignature = metadata.GetMethodDefinition(method.Handle).DecodeSignature(new TypeSystem.Implementation.TypeReferenceSignatureDecoder(), default);
this.metadata = module.GetMetadataReader();
this.method = typeSystem.ResolveAsMethod(methodDefinitionHandle);
this.methodSignature = metadata.GetMethodDefinition(methodDefinitionHandle).DecodeSignature(new TypeSystem.Implementation.TypeReferenceSignatureDecoder(), default);
this.body = body;
this.reader = body.GetILReader();
//this.debugInfo = metadata.GetMethodDebugInformation(method.Handle.ToDebugInformationHandle());
@ -157,7 +155,7 @@ namespace ICSharpCode.Decompiler.IL @@ -157,7 +155,7 @@ namespace ICSharpCode.Decompiler.IL
{
VariableKind kind = IsPinned(type) ? VariableKind.PinnedLocal : VariableKind.Local;
ILVariable ilVar = new ILVariable(kind, type.Resolve(resolveContext), index);
if (!UseDebugSymbols || debugInfo == null || !debugInfo.TryGetName(methodDefinition, index, out string name)) {
if (!UseDebugSymbols || debugInfo == null || !debugInfo.TryGetName((MethodDefinitionHandle)method.MetadataToken, index, out string name)) {
ilVar.Name = "V_" + index;
ilVar.HasGeneratedName = true;
} else if (string.IsNullOrWhiteSpace(name)) {
@ -387,9 +385,9 @@ namespace ICSharpCode.Decompiler.IL @@ -387,9 +385,9 @@ namespace ICSharpCode.Decompiler.IL
/// <summary>
/// Debugging helper: writes the decoded instruction stream interleaved with the inferred evaluation stack layout.
/// </summary>
public void WriteTypedIL(Metadata.MethodDefinition method, MethodBodyBlock body, ITextOutput output, CancellationToken cancellationToken = default(CancellationToken))
public void WriteTypedIL(Metadata.PEFile module, MethodDefinitionHandle method, MethodBodyBlock body, ITextOutput output, CancellationToken cancellationToken = default(CancellationToken))
{
Init(body);
Init(module, method, body);
ReadInstructions(cancellationToken);
foreach (var inst in instructionBuilder) {
if (inst is StLoc stloc && stloc.IsStackAdjustment) {
@ -421,21 +419,21 @@ namespace ICSharpCode.Decompiler.IL @@ -421,21 +419,21 @@ namespace ICSharpCode.Decompiler.IL
output.WriteLine();
}
new Disassembler.MethodBodyDisassembler(output, cancellationToken) { DetectControlStructure = false }
.WriteExceptionHandlers(method, body);
.WriteExceptionHandlers(new Metadata.MethodDefinition(module, method), body);
}
/// <summary>
/// Decodes the specified method body and returns an ILFunction.
/// </summary>
public ILFunction ReadIL(Metadata.MethodDefinition method, MethodBodyBlock body, CancellationToken cancellationToken = default(CancellationToken))
public ILFunction ReadIL(Metadata.PEFile module, MethodDefinitionHandle method, MethodBodyBlock body, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
Init(method, body);
Init(module, method, body);
ReadInstructions(cancellationToken);
var blockBuilder = new BlockBuilder(body, typeSystem, variableByExceptionHandler);
blockBuilder.CreateBlocks(mainContainer, instructionBuilder, isBranchTarget, cancellationToken);
var resolvedMethod = typeSystem.Resolve(method);
var function = new ILFunction(resolvedMethod, method, mainContainer);
var resolvedMethod = typeSystem.ResolveAsMethod(method);
var function = new ILFunction(resolvedMethod, body.GetCodeSize(), mainContainer);
CollectionExtensions.AddRange(function.Variables, parameterVariables);
CollectionExtensions.AddRange(function.Variables, localVariables);
CollectionExtensions.AddRange(function.Variables, stackVariables);

2
ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs

@ -89,6 +89,8 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -89,6 +89,8 @@ namespace ICSharpCode.Decompiler.TypeSystem
get { return moduleDefinition; }
}
public SRM.MetadataReader GetMetadata() => moduleDefinition.GetMetadataReader();
void StoreMemberReference(IUnresolvedEntity entity, SRM.EntityHandle mr)
{
// This is a callback from the type system, which is multi-threaded and may be accessed externally

2
ICSharpCode.Decompiler/TypeSystem/IDecompilerTypeSystem.cs

@ -32,6 +32,8 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -32,6 +32,8 @@ namespace ICSharpCode.Decompiler.TypeSystem
IMethod ResolveAsMethod(EntityHandle methodReference);
IMember ResolveAsMember(EntityHandle memberReference);
MetadataReader GetMetadata();
/// <summary>
/// Gets a type system instance that automatically specializes the results
/// of each Resolve() call with the provided substitution.

3
ICSharpCode.Decompiler/TypeSystem/MetadataLoader.cs

@ -279,7 +279,7 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -279,7 +279,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
/// a type system type reference.</param>
/// <param name="typeAttributes">Attributes associated with the Cecil type reference.
/// This is used to support the 'dynamic' type.</param>
public ITypeReference ReadTypeReference(EntityHandle type, SRM.CustomAttributeHandleCollection typeAttributes = default(CustomAttributeHandleCollection))
public ITypeReference ReadTypeReference(EntityHandle type, CustomAttributeHandleCollection typeAttributes = default(CustomAttributeHandleCollection))
{
ITypeReference CreateTypeReference(TypeReferenceHandle handle)
{
@ -1689,7 +1689,6 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -1689,7 +1689,6 @@ namespace ICSharpCode.Decompiler.TypeSystem
{
OnEntityLoaded?.Invoke(typeSystemObject, cecilObject);
}
}
#endregion
}
}

Loading…
Cancel
Save