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
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public ObjectNode.ObjectData Encode(ISymbolDefinitionNode symbolDefinitionNode)
{
#if READYTORUN
byte[] encodedThunk = new byte[FunctionBody.EncodeSize()];
FunctionBody.Encode(encodedThunk);

Relocation[] relocs = new Relocation[FunctionBody.EncodeRelocationCount()];
FunctionBody.Encode(encodedThunk.AsSpan());
FunctionBody.EncodeRelocations(relocs.AsSpan());

return new ObjectNode.ObjectData(encodedThunk, relocs, 1, new ISymbolDefinitionNode[] { symbolDefinitionNode });
Expand Down
23 changes: 2 additions & 21 deletions src/coreclr/tools/Common/Compiler/ObjectWriter/ObjectWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,6 @@ private protected ObjectWriter(NodeFactory factory, ObjectWritingOptions options
private protected SectionWriter GetOrCreateSection(ObjectNodeSection section)
=> GetOrCreateSection(section, default, default);

private readonly SectionWriter.Params _defaultParams = new SectionWriter.Params
{
LengthEncodeFormat = LengthEncodeFormat.None
};

/// <summary>
/// Some architectures may require section-specific params for the writer. For example, on Wasm,
/// certain sections require length prefixes before each object entry which the section writer does support,
/// but this has to be indicated by a particular implementation.
/// </summary>
/// <param name="section"></param>
/// <returns></returns>
private protected virtual SectionWriter.Params WriterParams(ObjectNodeSection section) => _defaultParams;

/// <summary>
/// Get or creates an object file section.
/// </summary>
Expand Down Expand Up @@ -139,8 +125,7 @@ private protected SectionWriter GetOrCreateSection(ObjectNodeSection section, Ut
return new SectionWriter(
this,
sectionIndex,
sectionData,
WriterParams(section));
sectionData);
}

private protected bool ShouldShareSymbol(ObjectNode node)
Expand Down Expand Up @@ -482,13 +467,9 @@ public virtual void EmitObject(Stream outputFileStream, IReadOnlyCollection<Depe

if (nodeContents.Relocs is not null)
{
// For platforms such as Wasm where we must prepend the length before writing blocks,
// we need to adjust the offset of the relocation by the length prefix
uint additionalOffset = sectionWriter.HasLengthPrefix ? sectionWriter.LengthPrefixSize(nodeContents.Data.Length) : 0;

blocksToRelocate.Add(new BlockToRelocate(
sectionWriter.SectionIndex,
sectionWriter.Position + additionalOffset,
sectionWriter.Position,
nodeContents.Data,
nodeContents.Relocs));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,26 @@

namespace ILCompiler.ObjectWriter
{
internal enum LengthEncodeFormat
{
ULEB128,
None,
}

internal struct SectionWriter
{
private readonly ObjectWriter _objectWriter;
Comment thread
adamperlin marked this conversation as resolved.
private readonly SectionData _sectionData;

private readonly Params _params;

public int SectionIndex { get; init; }
public readonly IBufferWriter<byte> Buffer => _sectionData.BufferWriter;

public struct Params
{
public LengthEncodeFormat LengthEncodeFormat;
}

public bool HasLengthPrefix => _params.LengthEncodeFormat != LengthEncodeFormat.None;

internal SectionWriter(
ObjectWriter objectWriter,
int sectionIndex,
SectionData sectionData,
Params ps)
SectionData sectionData)
{
_objectWriter = objectWriter;
SectionIndex = sectionIndex;
_sectionData = sectionData;
_params = ps;
}

public readonly void EmitLengthPrefix(ulong length)
{
switch (_params.LengthEncodeFormat)
{
case LengthEncodeFormat.ULEB128:
WriteULEB128(length);
break;
case LengthEncodeFormat.None:
break;
default:
throw new InvalidOperationException("Length prefix encoding not specified");
}
}

public readonly uint LengthPrefixSize(int length)
{
switch (_params.LengthEncodeFormat)
{
case LengthEncodeFormat.ULEB128:
return DwarfHelper.SizeOfULEB128((ulong)length);
default:
return 0;
}
}

public readonly void EmitData(ReadOnlyMemory<byte> data)
{
EmitLengthPrefix((ulong)data.Length);
_sectionData.AppendData(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,17 @@ private int BodyContentSize()

public int EncodeSize()
{
return BodyContentSize();
int bodySize = BodyContentSize();
int sizePrefixLength = (int)DwarfHelper.SizeOfULEB128((ulong)bodySize);
return sizePrefixLength + bodySize;
}

public int Encode(Span<byte> buffer)
{
_locals.CopyTo(buffer);
int pos = _locals.Length;
int contentSize = BodyContentSize();
int pos = DwarfHelper.WriteULEB128(buffer, (ulong)contentSize);
_locals.CopyTo(buffer.Slice(pos));
pos += _locals.Length;
pos += _body.Encode(buffer.Slice(pos));

return pos;
Expand All @@ -104,8 +108,10 @@ public int EncodeRelocationCount()

public int EncodeRelocations(Span<Relocation> buffer)
{
uint bodySize = (uint)BodyContentSize();
int bodySizePrefixLength = (int)DwarfHelper.SizeOfULEB128(bodySize);
int relocsEncoded = _body.EncodeRelocations(buffer);
WasmExpr.OffsetRelocationsByOffset(buffer.Slice(0, relocsEncoded), _locals.Length);
WasmExpr.OffsetRelocationsByOffset(buffer.Slice(0, relocsEncoded), bodySizePrefixLength + _locals.Length);
return relocsEncoded;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,6 @@ private void InsertWasmStub(Utf8String name, WasmFunctionBody body)
byte[] data = new byte[codeSize];
body.Encode(data);

// We must emit the length prefix explicitly
Debug.Assert(!codeWriter.HasLengthPrefix);
codeWriter.WriteULEB128((ulong)codeSize);
codeWriter.EmitData(data);
_uniqueSymbols.Add(name.ToString(), _methodCount);
_methodCount++;
Expand Down Expand Up @@ -480,14 +477,6 @@ private protected override ObjectNodeSection GetEmitSection(ObjectNodeSection se
return section;
}

private protected override SectionWriter.Params WriterParams(ObjectNodeSection section)
{
return new SectionWriter.Params
{
LengthEncodeFormat = LengthEncodeFormat.None
};
}

private protected override void CreateSection(ObjectNodeSection section, Utf8String comdatName, Utf8String symbolName, int sectionIndex, Stream sectionStream)
{
WasmSectionType sectionType = GetWasmSectionType(section);
Expand Down
Loading