1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-11-14 06:27:01 +00:00

Refactor NativeContext (#1410)

* Refactor NativeContext

* Fix bugs

* Use correct counts

* Check index using register count constants
This commit is contained in:
gdkchan 2020-07-24 16:13:14 -03:00 committed by GitHub
parent 41ac824273
commit 80d4199fb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,21 +1,23 @@
using ARMeilleure.IntermediateRepresentation; using ARMeilleure.IntermediateRepresentation;
using ARMeilleure.Memory; using ARMeilleure.Memory;
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.CompilerServices;
namespace ARMeilleure.State namespace ARMeilleure.State
{ {
class NativeContext : IDisposable class NativeContext : IDisposable
{ {
private const int IntSize = 8; private unsafe struct NativeCtxStorage
private const int VecSize = 16; {
private const int FlagSize = 4; public fixed ulong X[RegisterConsts.IntRegsCount];
private const int ExtraSize = 8; public fixed ulong V[RegisterConsts.VecRegsCount * 2];
public fixed uint Flags[RegisterConsts.FlagsCount];
public fixed uint FpFlags[RegisterConsts.FpFlagsCount];
public int Counter;
public ulong CallAddress;
}
private const int TotalSize = RegisterConsts.IntRegsCount * IntSize + private static NativeCtxStorage _dummyStorage = new NativeCtxStorage();
RegisterConsts.VecRegsCount * VecSize +
RegisterConsts.FlagsCount * FlagSize +
RegisterConsts.FpFlagsCount * FlagSize + ExtraSize;
private readonly IJitMemoryBlock _block; private readonly IJitMemoryBlock _block;
@ -23,179 +25,150 @@ namespace ARMeilleure.State
public NativeContext(IJitMemoryAllocator allocator) public NativeContext(IJitMemoryAllocator allocator)
{ {
_block = allocator.Allocate(TotalSize); _block = allocator.Allocate((ulong)Unsafe.SizeOf<NativeCtxStorage>());
} }
public ulong GetX(int index) public unsafe ulong GetX(int index)
{ {
if ((uint)index >= RegisterConsts.IntRegsCount) if ((uint)index >= RegisterConsts.IntRegsCount)
{ {
throw new ArgumentOutOfRangeException(nameof(index)); throw new ArgumentOutOfRangeException(nameof(index));
} }
return (ulong)Marshal.ReadInt64(BasePtr, index * IntSize); return GetStorage().X[index];
} }
public void SetX(int index, ulong value) public unsafe void SetX(int index, ulong value)
{ {
if ((uint)index >= RegisterConsts.IntRegsCount) if ((uint)index >= RegisterConsts.IntRegsCount)
{ {
throw new ArgumentOutOfRangeException(nameof(index)); throw new ArgumentOutOfRangeException(nameof(index));
} }
Marshal.WriteInt64(BasePtr, index * IntSize, (long)value); GetStorage().X[index] = value;
} }
public V128 GetV(int index) public unsafe V128 GetV(int index)
{ {
if ((uint)index >= RegisterConsts.IntRegsCount) if ((uint)index >= RegisterConsts.VecRegsCount)
{ {
throw new ArgumentOutOfRangeException(nameof(index)); throw new ArgumentOutOfRangeException(nameof(index));
} }
int offset = RegisterConsts.IntRegsCount * IntSize + index * VecSize; return new V128(GetStorage().V[index * 2 + 0], GetStorage().V[index * 2 + 1]);
return new V128(
Marshal.ReadInt64(BasePtr, offset + 0),
Marshal.ReadInt64(BasePtr, offset + 8));
} }
public void SetV(int index, V128 value) public unsafe void SetV(int index, V128 value)
{ {
if ((uint)index >= RegisterConsts.IntRegsCount) if ((uint)index >= RegisterConsts.VecRegsCount)
{ {
throw new ArgumentOutOfRangeException(nameof(index)); throw new ArgumentOutOfRangeException(nameof(index));
} }
int offset = RegisterConsts.IntRegsCount * IntSize + index * VecSize; GetStorage().V[index * 2 + 0] = value.Extract<ulong>(0);
GetStorage().V[index * 2 + 1] = value.Extract<ulong>(1);
Marshal.WriteInt64(BasePtr, offset + 0, value.Extract<long>(0));
Marshal.WriteInt64(BasePtr, offset + 8, value.Extract<long>(1));
} }
public bool GetPstateFlag(PState flag) public unsafe bool GetPstateFlag(PState flag)
{ {
if ((uint)flag >= RegisterConsts.FlagsCount) if ((uint)flag >= RegisterConsts.FlagsCount)
{ {
throw new ArgumentException($"Invalid flag \"{flag}\" specified."); throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
} }
int offset = return GetStorage().Flags[(int)flag] != 0;
RegisterConsts.IntRegsCount * IntSize +
RegisterConsts.VecRegsCount * VecSize + (int)flag * FlagSize;
int value = Marshal.ReadInt32(BasePtr, offset);
return value != 0;
} }
public void SetPstateFlag(PState flag, bool value) public unsafe void SetPstateFlag(PState flag, bool value)
{ {
if ((uint)flag >= RegisterConsts.FlagsCount) if ((uint)flag >= RegisterConsts.FlagsCount)
{ {
throw new ArgumentException($"Invalid flag \"{flag}\" specified."); throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
} }
int offset = GetStorage().Flags[(int)flag] = value ? 1u : 0u;
RegisterConsts.IntRegsCount * IntSize +
RegisterConsts.VecRegsCount * VecSize + (int)flag * FlagSize;
Marshal.WriteInt32(BasePtr, offset, value ? 1 : 0);
} }
public bool GetFPStateFlag(FPState flag) public unsafe bool GetFPStateFlag(FPState flag)
{ {
if ((uint)flag >= RegisterConsts.FlagsCount) if ((uint)flag >= RegisterConsts.FpFlagsCount)
{ {
throw new ArgumentException($"Invalid flag \"{flag}\" specified."); throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
} }
int offset = return GetStorage().FpFlags[(int)flag] != 0;
RegisterConsts.IntRegsCount * IntSize +
RegisterConsts.VecRegsCount * VecSize +
RegisterConsts.FlagsCount * FlagSize + (int)flag * FlagSize;
int value = Marshal.ReadInt32(BasePtr, offset);
return value != 0;
} }
public void SetFPStateFlag(FPState flag, bool value) public unsafe void SetFPStateFlag(FPState flag, bool value)
{ {
if ((uint)flag >= RegisterConsts.FlagsCount) if ((uint)flag >= RegisterConsts.FpFlagsCount)
{ {
throw new ArgumentException($"Invalid flag \"{flag}\" specified."); throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
} }
int offset = GetStorage().FpFlags[(int)flag] = value ? 1u : 0u;
RegisterConsts.IntRegsCount * IntSize +
RegisterConsts.VecRegsCount * VecSize +
RegisterConsts.FlagsCount * FlagSize + (int)flag * FlagSize;
Marshal.WriteInt32(BasePtr, offset, value ? 1 : 0);
} }
public int GetCounter() public int GetCounter() => GetStorage().Counter;
{ public void SetCounter(int value) => GetStorage().Counter = value;
return Marshal.ReadInt32(BasePtr, GetCounterOffset());
}
public void SetCounter(int value) public unsafe static int GetRegisterOffset(Register reg)
{ {
Marshal.WriteInt32(BasePtr, GetCounterOffset(), value);
}
public static int GetRegisterOffset(Register reg)
{
int offset, size;
if (reg.Type == RegisterType.Integer) if (reg.Type == RegisterType.Integer)
{ {
offset = reg.Index * IntSize; if ((uint)reg.Index >= RegisterConsts.IntRegsCount)
size = IntSize;
}
else if (reg.Type == RegisterType.Vector)
{
offset = RegisterConsts.IntRegsCount * IntSize + reg.Index * VecSize;
size = VecSize;
}
else /* if (reg.Type == RegisterType.Flag) */
{
offset = RegisterConsts.IntRegsCount * IntSize +
RegisterConsts.VecRegsCount * VecSize + reg.Index * FlagSize;
size = FlagSize;
}
if ((uint)(offset + size) > (uint)TotalSize)
{ {
throw new ArgumentException("Invalid register."); throw new ArgumentException("Invalid register.");
} }
return offset; return StorageOffset(ref _dummyStorage, ref _dummyStorage.X[reg.Index]);
}
else if (reg.Type == RegisterType.Vector)
{
if ((uint)reg.Index >= RegisterConsts.VecRegsCount)
{
throw new ArgumentException("Invalid register.");
}
return StorageOffset(ref _dummyStorage, ref _dummyStorage.V[reg.Index * 2]);
}
else if (reg.Type == RegisterType.Flag)
{
if ((uint)reg.Index >= RegisterConsts.FlagsCount)
{
throw new ArgumentException("Invalid register.");
}
return StorageOffset(ref _dummyStorage, ref _dummyStorage.Flags[reg.Index]);
}
else /* if (reg.Type == RegisterType.FpFlag) */
{
if ((uint)reg.Index >= RegisterConsts.FpFlagsCount)
{
throw new ArgumentException("Invalid register.");
}
return StorageOffset(ref _dummyStorage, ref _dummyStorage.FpFlags[reg.Index]);
}
} }
public static int GetCounterOffset() public static int GetCounterOffset()
{ {
return RegisterConsts.IntRegsCount * IntSize + return StorageOffset(ref _dummyStorage, ref _dummyStorage.Counter);
RegisterConsts.VecRegsCount * VecSize +
RegisterConsts.FlagsCount * FlagSize +
RegisterConsts.FpFlagsCount * FlagSize;
} }
public static int GetCallAddressOffset() public static int GetCallAddressOffset()
{ {
return RegisterConsts.IntRegsCount * IntSize + return StorageOffset(ref _dummyStorage, ref _dummyStorage.CallAddress);
RegisterConsts.VecRegsCount * VecSize +
RegisterConsts.FlagsCount * FlagSize +
RegisterConsts.FpFlagsCount * FlagSize + 4;
} }
public void Dispose() private static int StorageOffset<T>(ref NativeCtxStorage storage, ref T target)
{ {
_block.Dispose(); return (int)Unsafe.ByteOffset(ref Unsafe.As<NativeCtxStorage, T>(ref storage), ref target);
} }
private unsafe ref NativeCtxStorage GetStorage() => ref Unsafe.AsRef<NativeCtxStorage>((void*)_block.Pointer);
public void Dispose() => _block.Dispose();
} }
} }