Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions source/MetadataProcessor.Core/DumpGenerator/AttributeCustom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Copyright (c) 2020 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

namespace nanoFramework.Tools.MetadataProcessor.Core
{
public class AttributeCustom
{
public string ReferenceId;

public string Name;

public string TypeToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class DumpAllTable
public List<AssemblyRef> AssemblyReferences = new List<AssemblyRef>();
public List<TypeRef> TypeReferences = new List<TypeRef>();
public List<TypeDef> TypeDefinitions = new List<TypeDef>();
public List<AttributeCustom> Attributes = new List<AttributeCustom>();
public List<UserString> UserStrings = new List<UserString>();
}
}
9 changes: 6 additions & 3 deletions source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace nanoFramework.Tools.MetadataProcessor
internal partial class DumpTemplates
{
internal static string DumpAllTemplate =
@"
{{#AssemblyReferences}}
@"{{#AssemblyReferences}}
AssemblyRefProps [{{ReferenceId}}]: Flags: {{Flags}} '{{Name}}'
{{/AssemblyReferences}}

Expand All @@ -26,7 +25,7 @@ internal partial class DumpTemplates
FieldDefProps [{{ReferenceId}}]: Attr: {{Attributes}} Flags: {{Flags}} '{{Name}}' [{{Signature}}]
{{/FieldDefinitions}}
{{#MethodDefinitions}}
MethodDefProps [{{ReferenceId}}]: Flags: {{Flags}} Impl: {{Implementation}} RVA: {{RVA}} '{{Name}}' [{{Signature}}]
MethodDefProps [{{ReferenceId}}]: Flags: {{Flags}} Impl: {{Implementation}} RVA: {{RVA}} '{{Name}}' [{{Signature}}]
{{#Locals}}
Locals {{Locals}}
{{/Locals}}
Expand All @@ -46,6 +45,10 @@ internal partial class DumpTemplates

{{/TypeDefinitions}}

{{#Attributes}}
Attribute: {{Name}}::[{{ReferenceId}} {{TypeToken}}]
{{/Attributes}}

{{#UserStrings}}
UserString [{{ReferenceId}}]: '{{Content}}'
{{/UserStrings}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DumpGenerator\ExceptionHandler.cs" />
<Compile Include="DumpGenerator\AttributeCustom.cs" />
<Compile Include="DumpGenerator\UserString.cs" />
<Compile Include="DumpGenerator\ILCode.cs" />
<Compile Include="DumpGenerator\LocalDef.cs" />
Expand Down
81 changes: 61 additions & 20 deletions source/MetadataProcessor.Core/nanoDumperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
using nanoFramework.Tools.MetadataProcessor.Core.Extensions;
using Stubble.Core.Builders;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -55,9 +56,28 @@ public void DumpAll()

private void DumpCustomAttributes(DumpAllTable dumpTable)
{
foreach (var a in _tablesContext.AssemblyDefinition.CustomAttributes)
foreach (var a in _tablesContext.TypeDefinitionTable.Items.Where(td => td.HasCustomAttributes))
{
foreach (var ma in a.Methods)
{
var attribute = new AttributeCustom()
{
Name = a.Module.Assembly.Name.Name,
ReferenceId = ma.MetadataToken.ToInt32().ToString("x8"),
TypeToken = a.CustomAttributes[0].Constructor.MetadataToken.ToInt32().ToString("x8")
};

dumpTable.Attributes.Add(attribute);
}

var attribute1 = new AttributeCustom()
{
Name = a.Module.Assembly.Name.Name,
ReferenceId = a.MetadataToken.ToInt32().ToString("x8"),
TypeToken = a.CustomAttributes[0].Constructor.MetadataToken.ToInt32().ToString("x8")
};

dumpTable.Attributes.Add(attribute1);
}
}

Expand Down Expand Up @@ -85,7 +105,7 @@ private void DumpUserStrings(DumpAllTable dumpTable)

private void DumpTypeDefinitions(DumpAllTable dumpTable)
{
foreach (var t in _tablesContext.TypeDefinitionTable.Items)
foreach (var t in _tablesContext.TypeDefinitionTable.Items.OrderBy(tr => tr.MetadataToken.ToInt32()))
{
// fill type definition
var typeDef = new TypeDef()
Expand Down Expand Up @@ -142,7 +162,7 @@ private void DumpTypeDefinitions(DumpAllTable dumpTable)
ReferenceId = m.MetadataToken.ToInt32().ToString("x8"),
Name = m.Name,
RVA = m.RVA.ToString("x8"),
Implementation = "Implementation",
Implementation = "00000000",
Signature = PrintSignatureForMethod(m)
};

Expand Down Expand Up @@ -231,24 +251,37 @@ private void DumpTypeDefinitions(DumpAllTable dumpTable)

private void DumpTypeReferences(DumpAllTable dumpTable)
{
foreach (var t in _tablesContext.TypeReferencesTable.Items)
foreach (var t in _tablesContext.TypeReferencesTable.Items.OrderBy(tr => tr.MetadataToken.ToInt32()))
{
ushort refId;

var typeRef = new TypeRef()
{
Name = t.Name,
Scope = _tablesContext.TypeReferencesTable.GetScope(t).ToString("x8")
Name = t.FullName,
Scope = new MetadataToken(TokenType.AssemblyRef, _tablesContext.TypeReferencesTable.GetScope(t)).ToInt32().ToString("x8")
};

if (_tablesContext.TypeReferencesTable.TryGetTypeReferenceId(t, out refId))
{
typeRef.ReferenceId = "0x" + refId.ToString("x8");
typeRef.Name = t.FullName;
typeRef.ReferenceId = new MetadataToken(TokenType.TypeRef, refId).ToInt32().ToString("x8");
}

// TODO
// list member refs
// list member refs
foreach(var m in _tablesContext.MethodReferencesTable.Items.Where(mr => mr.DeclaringType == t))
{
var memberRef = new MemberRef()
{
Name = m.Name
};

if (_tablesContext.MethodReferencesTable.TryGetMethodReferenceId(m, out refId))
{
memberRef.ReferenceId = new MetadataToken(TokenType.MemberRef, refId).ToInt32().ToString("x8");
memberRef.Signature = PrintSignatureForMethod(m);
}

typeRef.MemberReferences.Add(memberRef);
}

dumpTable.TypeReferences.Add(typeRef);
}
Expand All @@ -266,8 +299,8 @@ private void DumpAssemblyReferences(DumpAllTable dumpTable)
dumpTable.AssemblyReferences.Add(new AssemblyRef()
{
Name = a.Name,
ReferenceId = "0x" + _tablesContext.AssemblyReferenceTable.GetReferenceId(a).ToString("x8"),
Flags = "0x"
ReferenceId = new MetadataToken(TokenType.AssemblyRef, _tablesContext.AssemblyReferenceTable.GetReferenceId(a)).ToInt32().ToString("x8"),
Flags = "00000000"
});
}
}
Expand Down Expand Up @@ -330,20 +363,29 @@ private string PrintSignatureForType(TypeReference type)
return arraySig.ToString();
}

if(type.MetadataType == MetadataType.IntPtr)
{
return "I";
}

if (type.MetadataType == MetadataType.UIntPtr)
{
return "U";
}

return "";
}

private string PrintSignatureForMethod(MethodReference method)
{
var sig = new StringBuilder(PrintSignatureForType(method.ReturnType));

sig.Append("(");
sig.Append("( ");

foreach(var p in method.Parameters)
{
sig.Append(" ");
sig.Append(PrintSignatureForType(p.ParameterType));
sig.Append(" , ");
sig.Append(", ");
}

// remove trailing", "
Expand All @@ -356,7 +398,7 @@ private string PrintSignatureForMethod(MethodReference method)
sig.Append(" ");
}

sig.Append(")");
sig.Append(" )");

return sig.ToString();
}
Expand All @@ -365,13 +407,12 @@ private string PrintSignatureForMethod(MethodReference method)
private string PrintSignatureForLocalVar(Collection<VariableDefinition> variables)
{
StringBuilder sig = new StringBuilder();
sig.Append("{");
sig.Append("{ ");

foreach (var l in variables)
{
sig.Append(" ");
sig.Append(PrintSignatureForType(l.VariableType));
sig.Append(" , ");
sig.Append(", ");
}

// remove trailing", "
Expand All @@ -384,7 +425,7 @@ private string PrintSignatureForLocalVar(Collection<VariableDefinition> variable
sig.Append(" ");
}

sig.Append("}");
sig.Append(" }");

return sig.ToString();
}
Expand Down
24 changes: 17 additions & 7 deletions source/native/Tools.Parser/AssemblyParserDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,15 @@ void MetaData::Parser::Dump_PrintSigForLocalVar( LocalVarSignature& sig )
fwprintf( m_output, L" " );

Dump_PrintSigForType( *it++ );
if(it != sig.m_lstVars.end()) fwprintf( m_output, L"," );

if (it != sig.m_lstVars.end())
{
fwprintf(m_output, L",");
}
else
{
fwprintf(m_output, L" ");
}
}

fwprintf( m_output, L"}" );
Expand Down Expand Up @@ -383,9 +391,9 @@ void MetaData::Parser::Dump_EnumAssemblyRefs()
AssemblyRef& ar = it->second;

fwprintf( m_output, L"AssemblyRefProps [%08x]: Flags: %08x '%s'\n", ar.m_ar, ar.m_flags, ar.m_name.c_str() );

fwprintf( m_output, L"\n" );
}

fwprintf( m_output, L"\n" );
}

void MetaData::Parser::Dump_EnumModuleRefs()
Expand Down Expand Up @@ -418,9 +426,9 @@ void MetaData::Parser::Dump_EnumTypeRefs()
Dump_PrintSigForTypeSpec( mr.m_sig );
fwprintf( m_output, L"]\n" );
}

fwprintf( m_output, L"\n" );
}

fwprintf(m_output, L"\n");
}

void MetaData::Parser::Dump_EnumTypeDefs( bool fNoByteCode )
Expand Down Expand Up @@ -548,9 +556,9 @@ void MetaData::Parser::Dump_EnumTypeDefs( bool fNoByteCode )

fwprintf( m_output, L" InterfaceImplProps [%08x]: Itf: %08x\n", *it3, ii.m_itf );
}

fwprintf( m_output, L"\n" );
}

fwprintf(m_output, L"\n");
}

void MetaData::Parser::Dump_EnumCustomAttributes()
Expand Down Expand Up @@ -590,6 +598,8 @@ void MetaData::Parser::Dump_EnumCustomAttributes()
fwprintf( m_output, L"\n" );
}
}

fwprintf(m_output, L"\n");
}

void MetaData::Parser::Dump_EnumUserStrings()
Expand Down