From 11cfa982a46f3703a9b2c352b3cc814abd371eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Thu, 13 Feb 2020 22:45:15 +0000 Subject: [PATCH] Improvements dump metadata generator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add code for typeref and custom attributes. - Update code in native implementation to match formating. Signed-off-by: José Simões --- .../DumpGenerator/AttributeCustom.cs | 16 ++++ .../DumpGenerator/DumpAllTable.cs | 1 + .../DumpGenerator/DumpTemplates.cs | 9 ++- .../MetadataProcessor.Core.csproj | 1 + .../nanoDumperGenerator.cs | 81 ++++++++++++++----- .../Tools.Parser/AssemblyParserDump.cpp | 24 ++++-- 6 files changed, 102 insertions(+), 30 deletions(-) create mode 100644 source/MetadataProcessor.Core/DumpGenerator/AttributeCustom.cs diff --git a/source/MetadataProcessor.Core/DumpGenerator/AttributeCustom.cs b/source/MetadataProcessor.Core/DumpGenerator/AttributeCustom.cs new file mode 100644 index 00000000..5f86fcf2 --- /dev/null +++ b/source/MetadataProcessor.Core/DumpGenerator/AttributeCustom.cs @@ -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; + } +} diff --git a/source/MetadataProcessor.Core/DumpGenerator/DumpAllTable.cs b/source/MetadataProcessor.Core/DumpGenerator/DumpAllTable.cs index f6fdbebe..086a9387 100644 --- a/source/MetadataProcessor.Core/DumpGenerator/DumpAllTable.cs +++ b/source/MetadataProcessor.Core/DumpGenerator/DumpAllTable.cs @@ -12,6 +12,7 @@ public class DumpAllTable public List AssemblyReferences = new List(); public List TypeReferences = new List(); public List TypeDefinitions = new List(); + public List Attributes = new List(); public List UserStrings = new List(); } } diff --git a/source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs b/source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs index 33b06882..3ce4dc8f 100644 --- a/source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs +++ b/source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs @@ -8,8 +8,7 @@ namespace nanoFramework.Tools.MetadataProcessor internal partial class DumpTemplates { internal static string DumpAllTemplate = -@" -{{#AssemblyReferences}} +@"{{#AssemblyReferences}} AssemblyRefProps [{{ReferenceId}}]: Flags: {{Flags}} '{{Name}}' {{/AssemblyReferences}} @@ -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}} @@ -46,6 +45,10 @@ internal partial class DumpTemplates {{/TypeDefinitions}} +{{#Attributes}} +Attribute: {{Name}}::[{{ReferenceId}} {{TypeToken}}] +{{/Attributes}} + {{#UserStrings}} UserString [{{ReferenceId}}]: '{{Content}}' {{/UserStrings}} diff --git a/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj b/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj index d461e122..bee450ee 100644 --- a/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj +++ b/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj @@ -81,6 +81,7 @@ + diff --git a/source/MetadataProcessor.Core/nanoDumperGenerator.cs b/source/MetadataProcessor.Core/nanoDumperGenerator.cs index d83c0a18..7d4c3597 100644 --- a/source/MetadataProcessor.Core/nanoDumperGenerator.cs +++ b/source/MetadataProcessor.Core/nanoDumperGenerator.cs @@ -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; @@ -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); } } @@ -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() @@ -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) }; @@ -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); } @@ -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" }); } } @@ -330,6 +363,16 @@ private string PrintSignatureForType(TypeReference type) return arraySig.ToString(); } + if(type.MetadataType == MetadataType.IntPtr) + { + return "I"; + } + + if (type.MetadataType == MetadataType.UIntPtr) + { + return "U"; + } + return ""; } @@ -337,13 +380,12 @@ 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", " @@ -356,7 +398,7 @@ private string PrintSignatureForMethod(MethodReference method) sig.Append(" "); } - sig.Append(")"); + sig.Append(" )"); return sig.ToString(); } @@ -365,13 +407,12 @@ private string PrintSignatureForMethod(MethodReference method) private string PrintSignatureForLocalVar(Collection 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", " @@ -384,7 +425,7 @@ private string PrintSignatureForLocalVar(Collection variable sig.Append(" "); } - sig.Append("}"); + sig.Append(" }"); return sig.ToString(); } diff --git a/source/native/Tools.Parser/AssemblyParserDump.cpp b/source/native/Tools.Parser/AssemblyParserDump.cpp index d70ae7a7..0523fea2 100644 --- a/source/native/Tools.Parser/AssemblyParserDump.cpp +++ b/source/native/Tools.Parser/AssemblyParserDump.cpp @@ -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"}" ); @@ -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() @@ -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 ) @@ -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() @@ -590,6 +598,8 @@ void MetaData::Parser::Dump_EnumCustomAttributes() fwprintf( m_output, L"\n" ); } } + + fwprintf(m_output, L"\n"); } void MetaData::Parser::Dump_EnumUserStrings()