Refactor PtcInfo (#2625)

* Refactor `PtcInfo`

This change reduces the coupling of `PtcInfo` by moving relocation
tracking to the backend. `RelocEntry`s remains as `RelocEntry`s through
out the pipeline until it actually needs to be written to the PTC
streams. Keeping this representation makes inspecting and manipulating
relocations after compilations less painful. This is something I needed
to do to patch relocations to 0 to diff dumps.

Contributes to #1125.

* Turn `Symbol` & `RelocInfo` into readonly structs

* Add documentation to `CompiledFunction`

* Remove `Compiler.Compile<T>`

Remove `Compiler.Compile<T>` and replace it by `Map<T>` of the
`CompiledFunction` returned.
This commit is contained in:
FICTURE7 2021-09-14 03:23:37 +04:00 committed by GitHub
parent ac4ec1a015
commit a9343c9364
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 256 additions and 247 deletions

View file

@ -1,6 +1,7 @@
using ARMeilleure.CodeGen.Linking;
using ARMeilleure.IntermediateRepresentation;
using ARMeilleure.Translation.PTC;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
@ -61,12 +62,12 @@ namespace ARMeilleure.CodeGen.X86
}
}
private static InstructionInfo[] _instTable;
private readonly static InstructionInfo[] _instTable;
private Stream _stream;
private readonly Stream _stream;
private PtcInfo _ptcInfo;
private bool _ptcDisabled;
public List<RelocEntry> Relocs { get; }
public bool HasRelocs => Relocs != null;
static Assembler()
{
@ -294,12 +295,10 @@ namespace ARMeilleure.CodeGen.X86
_instTable[(int)inst] = info;
}
public Assembler(Stream stream, PtcInfo ptcInfo = null)
public Assembler(Stream stream, bool relocatable)
{
_stream = stream;
_ptcInfo = ptcInfo;
_ptcDisabled = ptcInfo == null;
Relocs = relocatable ? new List<RelocEntry>() : null;
}
public void Add(Operand dest, Operand source, OperandType type)
@ -498,7 +497,7 @@ namespace ARMeilleure.CodeGen.X86
public void Jcc(X86Condition condition, long offset)
{
if (_ptcDisabled && ConstFitsOnS8(offset))
if (!HasRelocs && ConstFitsOnS8(offset))
{
WriteByte((byte)(0x70 | (int)condition));
@ -519,7 +518,7 @@ namespace ARMeilleure.CodeGen.X86
public void Jmp(long offset)
{
if (_ptcDisabled && ConstFitsOnS8(offset))
if (!HasRelocs && ConstFitsOnS8(offset))
{
WriteByte(0xeb);
@ -980,9 +979,9 @@ namespace ARMeilleure.CodeGen.X86
WriteByte((byte)(info.OpRImm64 + (dest.GetRegister().Index & 0b111)));
if (_ptcInfo != default && source.Relocatable)
if (HasRelocs && source.Relocatable)
{
_ptcInfo.WriteRelocEntry(new RelocEntry((int)_stream.Position, source.Symbol));
Relocs.Add(new RelocEntry((int)_stream.Position, source.Symbol));
}
WriteUInt64(imm);
@ -1396,9 +1395,9 @@ namespace ARMeilleure.CodeGen.X86
return ConstFitsOnS32(value);
}
public static int GetJccLength(long offset, bool ptcDisabled = true)
public static int GetJccLength(long offset, bool relocatable = false)
{
if (ptcDisabled && ConstFitsOnS8(offset < 0 ? offset - 2 : offset))
if (!relocatable && ConstFitsOnS8(offset < 0 ? offset - 2 : offset))
{
return 2;
}
@ -1412,9 +1411,9 @@ namespace ARMeilleure.CodeGen.X86
}
}
public static int GetJmpLength(long offset, bool ptcDisabled = true)
public static int GetJmpLength(long offset, bool relocatable = false)
{
if (ptcDisabled && ConstFitsOnS8(offset < 0 ? offset - 2 : offset))
if (!relocatable && ConstFitsOnS8(offset < 0 ? offset - 2 : offset))
{
return 2;
}