Skip to content

Commit

Permalink
Split tables out.
Browse files Browse the repository at this point in the history
Add optimization for Guid parsing.
  • Loading branch information
wasabii committed Jan 9, 2024
1 parent efdd3ee commit 755f87e
Show file tree
Hide file tree
Showing 48 changed files with 5,425 additions and 4,372 deletions.
1 change: 1 addition & 0 deletions src/IKVM.Reflection/IKVM.Reflection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<Description>IKVM implementation of System.Reflection[.Emit]</Description>
<DefineConstants>$(DefineConstants);EMITTERS</DefineConstants>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
113 changes: 113 additions & 0 deletions src/IKVM.Reflection/Metadata/AssemblyRefTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright (C) 2009-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
[email protected]
*/
using IKVM.Reflection.Reader;
using IKVM.Reflection.Writer;

namespace IKVM.Reflection.Metadata
{
sealed class AssemblyRefTable : Table<AssemblyRefTable.Record>
{

internal struct Record
{

internal ushort MajorVersion;
internal ushort MinorVersion;
internal ushort BuildNumber;
internal ushort RevisionNumber;
internal int Flags;
internal int PublicKeyOrToken;
internal int Name;
internal int Culture;
internal int HashValue;

}

internal const int Index = 0x23;

internal int FindOrAddRecord(Record rec)
{
for (int i = 0; i < rowCount; i++)
{
// note that we ignore HashValue here!
if (records[i].Name == rec.Name &&
records[i].MajorVersion == rec.MajorVersion &&
records[i].MinorVersion == rec.MinorVersion &&
records[i].BuildNumber == rec.BuildNumber &&
records[i].RevisionNumber == rec.RevisionNumber &&
records[i].Flags == rec.Flags &&
records[i].PublicKeyOrToken == rec.PublicKeyOrToken &&
records[i].Culture == rec.Culture)
return i + 1;
}

return AddRecord(rec);
}

internal override void Read(MetadataReader mr)
{
for (int i = 0; i < records.Length; i++)
{
records[i].MajorVersion = mr.ReadUInt16();
records[i].MinorVersion = mr.ReadUInt16();
records[i].BuildNumber = mr.ReadUInt16();
records[i].RevisionNumber = mr.ReadUInt16();
records[i].Flags = mr.ReadInt32();
records[i].PublicKeyOrToken = mr.ReadBlobIndex();
records[i].Name = mr.ReadStringIndex();
records[i].Culture = mr.ReadStringIndex();
records[i].HashValue = mr.ReadBlobIndex();
}
}

internal override void Write(MetadataWriter mw)
{
for (int i = 0; i < rowCount; i++)
{
mw.Write(records[i].MajorVersion);
mw.Write(records[i].MinorVersion);
mw.Write(records[i].BuildNumber);
mw.Write(records[i].RevisionNumber);
mw.Write(records[i].Flags);
mw.WriteBlobIndex(records[i].PublicKeyOrToken);
mw.WriteStringIndex(records[i].Name);
mw.WriteStringIndex(records[i].Culture);
mw.WriteBlobIndex(records[i].HashValue);
}
}

protected override int GetRowSize(RowSizeCalc rsc)
{
return rsc
.AddFixed(12)
.WriteBlobIndex()
.WriteStringIndex()
.WriteStringIndex()
.WriteBlobIndex()
.Value;
}

}

}
91 changes: 91 additions & 0 deletions src/IKVM.Reflection/Metadata/AssemblyTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright (C) 2009-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
[email protected]
*/
using IKVM.Reflection.Reader;
using IKVM.Reflection.Writer;

namespace IKVM.Reflection.Metadata
{
sealed class AssemblyTable : Table<AssemblyTable.Record>
{

internal struct Record
{
internal int HashAlgId;
internal ushort MajorVersion;
internal ushort MinorVersion;
internal ushort BuildNumber;
internal ushort RevisionNumber;
internal int Flags;
internal int PublicKey;
internal int Name;
internal int Culture;
}

internal const int Index = 0x20;

internal override void Read(MetadataReader mr)
{
for (int i = 0; i < records.Length; i++)
{
records[i].HashAlgId = mr.ReadInt32();
records[i].MajorVersion = mr.ReadUInt16();
records[i].MinorVersion = mr.ReadUInt16();
records[i].BuildNumber = mr.ReadUInt16();
records[i].RevisionNumber = mr.ReadUInt16();
records[i].Flags = mr.ReadInt32();
records[i].PublicKey = mr.ReadBlobIndex();
records[i].Name = mr.ReadStringIndex();
records[i].Culture = mr.ReadStringIndex();
}
}

internal override void Write(MetadataWriter mw)
{
for (int i = 0; i < rowCount; i++)
{
mw.Write(records[i].HashAlgId);
mw.Write(records[i].MajorVersion);
mw.Write(records[i].MinorVersion);
mw.Write(records[i].BuildNumber);
mw.Write(records[i].RevisionNumber);
mw.Write(records[i].Flags);
mw.WriteBlobIndex(records[i].PublicKey);
mw.WriteStringIndex(records[i].Name);
mw.WriteStringIndex(records[i].Culture);
}
}

protected override int GetRowSize(RowSizeCalc rsc)
{
return rsc
.AddFixed(16)
.WriteBlobIndex()
.WriteStringIndex()
.WriteStringIndex()
.Value;
}

}

}
79 changes: 79 additions & 0 deletions src/IKVM.Reflection/Metadata/ClassLayoutTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright (C) 2009-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
[email protected]
*/
using IKVM.Reflection.Reader;
using IKVM.Reflection.Writer;

namespace IKVM.Reflection.Metadata
{
sealed class ClassLayoutTable : SortedTable<ClassLayoutTable.Record>
{

internal struct Record : IRecord
{

internal short PackingSize;
internal int ClassSize;
internal int Parent;

int IRecord.SortKey => Parent;

int IRecord.FilterKey => Parent;

}

internal const int Index = 0x0f;

internal override void Read(MetadataReader mr)
{
for (int i = 0; i < records.Length; i++)
{
records[i].PackingSize = mr.ReadInt16();
records[i].ClassSize = mr.ReadInt32();
records[i].Parent = mr.ReadTypeDef();
}
}

internal override void Write(MetadataWriter mw)
{
Sort();

for (int i = 0; i < rowCount; i++)
{
mw.Write(records[i].PackingSize);
mw.Write(records[i].ClassSize);
mw.WriteTypeDef(records[i].Parent);
}
}

protected override int GetRowSize(RowSizeCalc rsc)
{
return rsc
.AddFixed(6)
.WriteTypeDef()
.Value;
}

}

}
22 changes: 4 additions & 18 deletions src/IKVM.Reflection/Metadata/CliHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,10 @@ Jeroen Frijters

namespace IKVM.Reflection.Metadata
{
struct RvaSize
{
internal uint VirtualAddress;
internal uint Size;

internal void Read(BinaryReader br)
{
VirtualAddress = br.ReadUInt32();
Size = br.ReadUInt32();
}

internal void Write(IKVM.Reflection.Writer.MetadataWriter mw)
{
mw.Write(VirtualAddress);
mw.Write(Size);
}
}

sealed class CliHeader
sealed class CliHeader
{

internal const uint COMIMAGE_FLAGS_ILONLY = 0x00000001;
internal const uint COMIMAGE_FLAGS_32BITREQUIRED = 0x00000002;
internal const uint COMIMAGE_FLAGS_STRONGNAMESIGNED = 0x00000008;
Expand Down Expand Up @@ -95,5 +79,7 @@ internal void Write(IKVM.Reflection.Writer.MetadataWriter mw)
ExportAddressTableJumps.Write(mw);
ManagedNativeHeader.Write(mw);
}

}

}
Loading

0 comments on commit 755f87e

Please sign in to comment.