Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Refactoring Object Node data parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
A-And committed Aug 18, 2016
1 parent f6c021b commit ba46798
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/ILCompiler.Compiler/src/CppCodeGen/CppWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,10 @@ private String GetCodeForVirtualMethod(MethodDesc method, int slot)
return sb.ToString();
}

private String GetCodeForNode(DependencyNode node, NodeFactory factory)
private String GetCodeForObjectNode(ObjectNode node, NodeFactory factory)
{
// virtual slots
var nodeData = (node as ObjectNode).GetData(factory, false);
var nodeData = node.GetData(factory, false);

CppGenerationBuffer nodeCode = new CppGenerationBuffer();

Expand All @@ -741,7 +741,7 @@ private String GetCodeForNode(DependencyNode node, NodeFactory factory)
* Second val - size of byte data if first value of tuple is false
*/

List<Tuple<bool, int>> nodeDataDivs = new List<Tuple<bool, int>>();
List<NodeDataSection> nodeDataSections = new List<NodeDataSection>();
byte[] actualData = new byte[nodeData.Data.Length];
Relocation[] relocs = nodeData.Relocs;

Expand Down Expand Up @@ -780,7 +780,7 @@ private String GetCodeForNode(DependencyNode node, NodeFactory factory)
{
nextRelocOffset = relocs[nextRelocIndex].Offset;
}
nodeDataDivs.Add(new Tuple<bool, int>(true, size));
nodeDataSections.Add(new NodeDataSection() { SectionType = NodeDataSectionType.Relocation, SectionSize = size });
i += size;
lastByteIndex = i;
}
Expand All @@ -789,7 +789,7 @@ private String GetCodeForNode(DependencyNode node, NodeFactory factory)
i++;
if (i + 1 == nextRelocOffset || i + 1 == nodeData.Data.Length)
{
nodeDataDivs.Add(new Tuple<bool, int>(false, (i + 1) - lastByteIndex));
nodeDataSections.Add(new NodeDataSection() { SectionType = NodeDataSectionType.ByteData, SectionSize = (i + 1) - lastByteIndex });
}
}
}
Expand All @@ -811,11 +811,11 @@ private String GetCodeForNode(DependencyNode node, NodeFactory factory)
nodeCode.Append("static struct {");

nodeCode.AppendLine();
nodeCode.Append(GetCodeForNodeStruct(nodeDataDivs, node));
nodeCode.Append(GetCodeForNodeStruct(nodeDataSections, node));

nodeCode.AppendLine();
nodeCode.Append("} mt = {");
nodeCode.Append(GetCodeForNodeData(nodeDataDivs, relocs, nodeData.Data, node, offset));
nodeCode.Append(GetCodeForNodeData(nodeDataSections, relocs, nodeData.Data, node, offset));

nodeCode.Append("};");
nodeCode.AppendLine();
Expand All @@ -828,19 +828,19 @@ private String GetCodeForNode(DependencyNode node, NodeFactory factory)


}
private String GetCodeForNodeData(List<Tuple<bool, int>> nodeDataDivs, Relocation[] relocs, byte[] byteData, DependencyNode node, int offset)
private String GetCodeForNodeData(List<NodeDataSection> nodeDataSections, Relocation[] relocs, byte[] byteData, DependencyNode node, int offset)
{
CppGenerationBuffer nodeDataDecl = new CppGenerationBuffer();
int relocCounter = 0;
int divisionStartIndex = offset;
nodeDataDecl.Indent();
nodeDataDecl.AppendLine();

for (int i = 0; i < nodeDataDivs.Count; i++)
for (int i = 0; i < nodeDataSections.Count; i++)
{
nodeDataDecl.Indent();

if (nodeDataDivs[i].Item1)
if (nodeDataSections[i].SectionType == NodeDataSectionType.Relocation)
{
Relocation reloc = relocs[relocCounter];
if (reloc.Target is CppMethodCodeNode)
Expand All @@ -865,9 +865,9 @@ private String GetCodeForNodeData(List<Tuple<bool, int>> nodeDataDivs, Relocatio
}
else
{
nodeDataDecl.Append(GetFormattedByteArray(byteData, divisionStartIndex, divisionStartIndex + nodeDataDivs[i].Item2));
GetFormattedByteArray(byteData, divisionStartIndex, divisionStartIndex + nodeDataSections[i].SectionSize, nodeDataDecl);
nodeDataDecl.Append(",");
divisionStartIndex += nodeDataDivs[i].Item2;
divisionStartIndex += nodeDataSections[i].SectionSize;

}
nodeDataDecl.AppendLine();
Expand All @@ -878,7 +878,7 @@ private String GetCodeForNodeData(List<Tuple<bool, int>> nodeDataDivs, Relocatio
return nodeDataDecl.ToString();

}
private String GetCodeForNodeStruct(List<Tuple<bool, int>> nodeDataDivs, DependencyNode node)
private String GetCodeForNodeStruct(List<NodeDataSection> nodeDataDivs, DependencyNode node)
{
CppGenerationBuffer nodeStructDecl = new CppGenerationBuffer();
int relocCounter = 1;
Expand All @@ -887,8 +887,8 @@ private String GetCodeForNodeStruct(List<Tuple<bool, int>> nodeDataDivs, Depende

for (i = 0; i < nodeDataDivs.Count; i++)
{
Tuple<bool, int> div = nodeDataDivs[i];
if (div.Item1)
NodeDataSection section = nodeDataDivs[i];
if (section.SectionType == NodeDataSectionType.Relocation)
{
nodeStructDecl.Append("void* reloc");
nodeStructDecl.Append(relocCounter);
Expand All @@ -900,7 +900,7 @@ private String GetCodeForNodeStruct(List<Tuple<bool, int>> nodeDataDivs, Depende
nodeStructDecl.Append("unsigned char data");
nodeStructDecl.Append((i + 1) - relocCounter);
nodeStructDecl.Append("[");
nodeStructDecl.Append(div.Item2);
nodeStructDecl.Append(section.SectionSize);
nodeStructDecl.Append("];");

}
Expand All @@ -910,14 +910,12 @@ private String GetCodeForNodeStruct(List<Tuple<bool, int>> nodeDataDivs, Depende

return nodeStructDecl.ToString();
}
private String GetFormattedByteArray(byte[] array, int startIndex, int endIndex)
private void GetFormattedByteArray(byte[] array, int startIndex, int endIndex, CppGenerationBuffer sb)
{
CppGenerationBuffer sb = new CppGenerationBuffer();
sb.Append("{");
sb.Append("0x");
sb.Append(BitConverter.ToString(array, startIndex, endIndex - startIndex).Replace("-", ",0x"));
sb.Append("}");
return sb.ToString();
}

private void BuildMethodLists(IEnumerable<DependencyNode> nodes)
Expand Down Expand Up @@ -992,7 +990,7 @@ public void OutputNodes(IEnumerable<DependencyNode> nodes, MethodDesc entrypoint
OutputMethodNode(node as CppMethodCodeNode, implementation);

else if (node is EETypeOptionalFieldsNode)
optionalFields.Append(GetCodeForNode(node, factory));
optionalFields.Append(GetCodeForObjectNode(node as EETypeOptionalFieldsNode, factory));
}

definitions.Append(forwardDefinitions.ToString());
Expand Down Expand Up @@ -1162,7 +1160,7 @@ private void OutputTypeNode(IEETypeNode typeNode, NodeFactory factory, CppGenera
// declare method table
if (!nodeType.IsPointer && !nodeType.IsByRef)
{
methodTable.Append(GetCodeForNode(typeNode as DependencyNode, factory));
methodTable.Append(GetCodeForObjectNode(typeNode as ObjectNode, factory));
methodTable.AppendEmptyLine();
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/ILCompiler.Compiler/src/CppCodeGen/NodeDataSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ILCompiler.Compiler.CppCodeGen
{
public enum NodeDataSectionType
{
Relocation,
ByteData
}

public struct NodeDataSection
{
public NodeDataSectionType SectionType;
public int SectionSize;
}
}
1 change: 1 addition & 0 deletions src/ILCompiler.Compiler/src/ILCompiler.Compiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<Compile Include="CppCodeGen\EvaluationStack.cs" />
<Compile Include="CppCodeGen\CppGenerationBuffer.cs" />
<Compile Include="CppCodeGen\CppWriter.cs" />
<Compile Include="CppCodeGen\NodeDataSection.cs" />
<Compile Include="IL\Stubs\MissingMethodBodyILEmitter.cs" />
<Compile Include="IL\Stubs\StartupCode\StartupCodeMainMethod.cs" />
</ItemGroup>
Expand Down

0 comments on commit ba46798

Please sign in to comment.