Skip to content

Commit 2570f11

Browse files
authored
Improvements dump metadata generator (#50)
***NO_CI***
1 parent 4cc730d commit 2570f11

File tree

6 files changed

+102
-30
lines changed

6 files changed

+102
-30
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Copyright (c) 2020 The nanoFramework project contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace nanoFramework.Tools.MetadataProcessor.Core
7+
{
8+
public class AttributeCustom
9+
{
10+
public string ReferenceId;
11+
12+
public string Name;
13+
14+
public string TypeToken;
15+
}
16+
}

source/MetadataProcessor.Core/DumpGenerator/DumpAllTable.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class DumpAllTable
1212
public List<AssemblyRef> AssemblyReferences = new List<AssemblyRef>();
1313
public List<TypeRef> TypeReferences = new List<TypeRef>();
1414
public List<TypeDef> TypeDefinitions = new List<TypeDef>();
15+
public List<AttributeCustom> Attributes = new List<AttributeCustom>();
1516
public List<UserString> UserStrings = new List<UserString>();
1617
}
1718
}

source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace nanoFramework.Tools.MetadataProcessor
88
internal partial class DumpTemplates
99
{
1010
internal static string DumpAllTemplate =
11-
@"
12-
{{#AssemblyReferences}}
11+
@"{{#AssemblyReferences}}
1312
AssemblyRefProps [{{ReferenceId}}]: Flags: {{Flags}} '{{Name}}'
1413
{{/AssemblyReferences}}
1514
@@ -26,7 +25,7 @@ internal partial class DumpTemplates
2625
FieldDefProps [{{ReferenceId}}]: Attr: {{Attributes}} Flags: {{Flags}} '{{Name}}' [{{Signature}}]
2726
{{/FieldDefinitions}}
2827
{{#MethodDefinitions}}
29-
MethodDefProps [{{ReferenceId}}]: Flags: {{Flags}} Impl: {{Implementation}} RVA: {{RVA}} '{{Name}}' [{{Signature}}]
28+
MethodDefProps [{{ReferenceId}}]: Flags: {{Flags}} Impl: {{Implementation}} RVA: {{RVA}} '{{Name}}' [{{Signature}}]
3029
{{#Locals}}
3130
Locals {{Locals}}
3231
{{/Locals}}
@@ -46,6 +45,10 @@ internal partial class DumpTemplates
4645
4746
{{/TypeDefinitions}}
4847
48+
{{#Attributes}}
49+
Attribute: {{Name}}::[{{ReferenceId}} {{TypeToken}}]
50+
{{/Attributes}}
51+
4952
{{#UserStrings}}
5053
UserString [{{ReferenceId}}]: '{{Content}}'
5154
{{/UserStrings}}

source/MetadataProcessor.Core/MetadataProcessor.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
</ItemGroup>
8282
<ItemGroup>
8383
<Compile Include="DumpGenerator\ExceptionHandler.cs" />
84+
<Compile Include="DumpGenerator\AttributeCustom.cs" />
8485
<Compile Include="DumpGenerator\UserString.cs" />
8586
<Compile Include="DumpGenerator\ILCode.cs" />
8687
<Compile Include="DumpGenerator\LocalDef.cs" />

source/MetadataProcessor.Core/nanoDumperGenerator.cs

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Mono.Cecil;
77
using Mono.Cecil.Cil;
88
using Mono.Collections.Generic;
9+
using nanoFramework.Tools.MetadataProcessor.Core.Extensions;
910
using Stubble.Core.Builders;
1011
using System;
1112
using System.Collections.Generic;
@@ -55,9 +56,28 @@ public void DumpAll()
5556

5657
private void DumpCustomAttributes(DumpAllTable dumpTable)
5758
{
58-
foreach (var a in _tablesContext.AssemblyDefinition.CustomAttributes)
59+
foreach (var a in _tablesContext.TypeDefinitionTable.Items.Where(td => td.HasCustomAttributes))
5960
{
61+
foreach (var ma in a.Methods)
62+
{
63+
var attribute = new AttributeCustom()
64+
{
65+
Name = a.Module.Assembly.Name.Name,
66+
ReferenceId = ma.MetadataToken.ToInt32().ToString("x8"),
67+
TypeToken = a.CustomAttributes[0].Constructor.MetadataToken.ToInt32().ToString("x8")
68+
};
69+
70+
dumpTable.Attributes.Add(attribute);
71+
}
72+
73+
var attribute1 = new AttributeCustom()
74+
{
75+
Name = a.Module.Assembly.Name.Name,
76+
ReferenceId = a.MetadataToken.ToInt32().ToString("x8"),
77+
TypeToken = a.CustomAttributes[0].Constructor.MetadataToken.ToInt32().ToString("x8")
78+
};
6079

80+
dumpTable.Attributes.Add(attribute1);
6181
}
6282
}
6383

@@ -85,7 +105,7 @@ private void DumpUserStrings(DumpAllTable dumpTable)
85105

86106
private void DumpTypeDefinitions(DumpAllTable dumpTable)
87107
{
88-
foreach (var t in _tablesContext.TypeDefinitionTable.Items)
108+
foreach (var t in _tablesContext.TypeDefinitionTable.Items.OrderBy(tr => tr.MetadataToken.ToInt32()))
89109
{
90110
// fill type definition
91111
var typeDef = new TypeDef()
@@ -142,7 +162,7 @@ private void DumpTypeDefinitions(DumpAllTable dumpTable)
142162
ReferenceId = m.MetadataToken.ToInt32().ToString("x8"),
143163
Name = m.Name,
144164
RVA = m.RVA.ToString("x8"),
145-
Implementation = "Implementation",
165+
Implementation = "00000000",
146166
Signature = PrintSignatureForMethod(m)
147167
};
148168

@@ -231,24 +251,37 @@ private void DumpTypeDefinitions(DumpAllTable dumpTable)
231251

232252
private void DumpTypeReferences(DumpAllTable dumpTable)
233253
{
234-
foreach (var t in _tablesContext.TypeReferencesTable.Items)
254+
foreach (var t in _tablesContext.TypeReferencesTable.Items.OrderBy(tr => tr.MetadataToken.ToInt32()))
235255
{
236256
ushort refId;
237257

238258
var typeRef = new TypeRef()
239259
{
240-
Name = t.Name,
241-
Scope = _tablesContext.TypeReferencesTable.GetScope(t).ToString("x8")
260+
Name = t.FullName,
261+
Scope = new MetadataToken(TokenType.AssemblyRef, _tablesContext.TypeReferencesTable.GetScope(t)).ToInt32().ToString("x8")
242262
};
243263

244264
if (_tablesContext.TypeReferencesTable.TryGetTypeReferenceId(t, out refId))
245265
{
246-
typeRef.ReferenceId = "0x" + refId.ToString("x8");
247-
typeRef.Name = t.FullName;
266+
typeRef.ReferenceId = new MetadataToken(TokenType.TypeRef, refId).ToInt32().ToString("x8");
248267
}
249268

250-
// TODO
251-
// list member refs
269+
// list member refs
270+
foreach(var m in _tablesContext.MethodReferencesTable.Items.Where(mr => mr.DeclaringType == t))
271+
{
272+
var memberRef = new MemberRef()
273+
{
274+
Name = m.Name
275+
};
276+
277+
if (_tablesContext.MethodReferencesTable.TryGetMethodReferenceId(m, out refId))
278+
{
279+
memberRef.ReferenceId = new MetadataToken(TokenType.MemberRef, refId).ToInt32().ToString("x8");
280+
memberRef.Signature = PrintSignatureForMethod(m);
281+
}
282+
283+
typeRef.MemberReferences.Add(memberRef);
284+
}
252285

253286
dumpTable.TypeReferences.Add(typeRef);
254287
}
@@ -266,8 +299,8 @@ private void DumpAssemblyReferences(DumpAllTable dumpTable)
266299
dumpTable.AssemblyReferences.Add(new AssemblyRef()
267300
{
268301
Name = a.Name,
269-
ReferenceId = "0x" + _tablesContext.AssemblyReferenceTable.GetReferenceId(a).ToString("x8"),
270-
Flags = "0x"
302+
ReferenceId = new MetadataToken(TokenType.AssemblyRef, _tablesContext.AssemblyReferenceTable.GetReferenceId(a)).ToInt32().ToString("x8"),
303+
Flags = "00000000"
271304
});
272305
}
273306
}
@@ -330,20 +363,29 @@ private string PrintSignatureForType(TypeReference type)
330363
return arraySig.ToString();
331364
}
332365

366+
if(type.MetadataType == MetadataType.IntPtr)
367+
{
368+
return "I";
369+
}
370+
371+
if (type.MetadataType == MetadataType.UIntPtr)
372+
{
373+
return "U";
374+
}
375+
333376
return "";
334377
}
335378

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

340-
sig.Append("(");
383+
sig.Append("( ");
341384

342385
foreach(var p in method.Parameters)
343386
{
344-
sig.Append(" ");
345387
sig.Append(PrintSignatureForType(p.ParameterType));
346-
sig.Append(" , ");
388+
sig.Append(", ");
347389
}
348390

349391
// remove trailing", "
@@ -356,7 +398,7 @@ private string PrintSignatureForMethod(MethodReference method)
356398
sig.Append(" ");
357399
}
358400

359-
sig.Append(")");
401+
sig.Append(" )");
360402

361403
return sig.ToString();
362404
}
@@ -365,13 +407,12 @@ private string PrintSignatureForMethod(MethodReference method)
365407
private string PrintSignatureForLocalVar(Collection<VariableDefinition> variables)
366408
{
367409
StringBuilder sig = new StringBuilder();
368-
sig.Append("{");
410+
sig.Append("{ ");
369411

370412
foreach (var l in variables)
371413
{
372-
sig.Append(" ");
373414
sig.Append(PrintSignatureForType(l.VariableType));
374-
sig.Append(" , ");
415+
sig.Append(", ");
375416
}
376417

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

387-
sig.Append("}");
428+
sig.Append(" }");
388429

389430
return sig.ToString();
390431
}

source/native/Tools.Parser/AssemblyParserDump.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,15 @@ void MetaData::Parser::Dump_PrintSigForLocalVar( LocalVarSignature& sig )
348348
fwprintf( m_output, L" " );
349349

350350
Dump_PrintSigForType( *it++ );
351-
if(it != sig.m_lstVars.end()) fwprintf( m_output, L"," );
351+
352+
if (it != sig.m_lstVars.end())
353+
{
354+
fwprintf(m_output, L",");
355+
}
356+
else
357+
{
358+
fwprintf(m_output, L" ");
359+
}
352360
}
353361

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

385393
fwprintf( m_output, L"AssemblyRefProps [%08x]: Flags: %08x '%s'\n", ar.m_ar, ar.m_flags, ar.m_name.c_str() );
386-
387-
fwprintf( m_output, L"\n" );
388394
}
395+
396+
fwprintf( m_output, L"\n" );
389397
}
390398

391399
void MetaData::Parser::Dump_EnumModuleRefs()
@@ -418,9 +426,9 @@ void MetaData::Parser::Dump_EnumTypeRefs()
418426
Dump_PrintSigForTypeSpec( mr.m_sig );
419427
fwprintf( m_output, L"]\n" );
420428
}
421-
422-
fwprintf( m_output, L"\n" );
423429
}
430+
431+
fwprintf(m_output, L"\n");
424432
}
425433

426434
void MetaData::Parser::Dump_EnumTypeDefs( bool fNoByteCode )
@@ -548,9 +556,9 @@ void MetaData::Parser::Dump_EnumTypeDefs( bool fNoByteCode )
548556

549557
fwprintf( m_output, L" InterfaceImplProps [%08x]: Itf: %08x\n", *it3, ii.m_itf );
550558
}
551-
552-
fwprintf( m_output, L"\n" );
553559
}
560+
561+
fwprintf(m_output, L"\n");
554562
}
555563

556564
void MetaData::Parser::Dump_EnumCustomAttributes()
@@ -590,6 +598,8 @@ void MetaData::Parser::Dump_EnumCustomAttributes()
590598
fwprintf( m_output, L"\n" );
591599
}
592600
}
601+
602+
fwprintf(m_output, L"\n");
593603
}
594604

595605
void MetaData::Parser::Dump_EnumUserStrings()

0 commit comments

Comments
 (0)