aboutsummaryrefslogtreecommitdiff
path: root/src/ARMeilleure/Translation/Cache
diff options
context:
space:
mode:
Diffstat (limited to 'src/ARMeilleure/Translation/Cache')
-rw-r--r--src/ARMeilleure/Translation/Cache/CacheEntry.cs8
-rw-r--r--src/ARMeilleure/Translation/Cache/CacheMemoryAllocator.cs2
-rw-r--r--src/ARMeilleure/Translation/Cache/JitCache.cs32
-rw-r--r--src/ARMeilleure/Translation/Cache/JitCacheInvalidation.cs8
-rw-r--r--src/ARMeilleure/Translation/Cache/JitUnwindWindows.cs113
5 files changed, 85 insertions, 78 deletions
diff --git a/src/ARMeilleure/Translation/Cache/CacheEntry.cs b/src/ARMeilleure/Translation/Cache/CacheEntry.cs
index dc5503b1..25b06f78 100644
--- a/src/ARMeilleure/Translation/Cache/CacheEntry.cs
+++ b/src/ARMeilleure/Translation/Cache/CacheEntry.cs
@@ -7,14 +7,14 @@ namespace ARMeilleure.Translation.Cache
readonly struct CacheEntry : IComparable<CacheEntry>
{
public int Offset { get; }
- public int Size { get; }
+ public int Size { get; }
public UnwindInfo UnwindInfo { get; }
public CacheEntry(int offset, int size, UnwindInfo unwindInfo)
{
- Offset = offset;
- Size = size;
+ Offset = offset;
+ Size = size;
UnwindInfo = unwindInfo;
}
@@ -23,4 +23,4 @@ namespace ARMeilleure.Translation.Cache
return Offset.CompareTo(other.Offset);
}
}
-} \ No newline at end of file
+}
diff --git a/src/ARMeilleure/Translation/Cache/CacheMemoryAllocator.cs b/src/ARMeilleure/Translation/Cache/CacheMemoryAllocator.cs
index 4c22de40..dd67e420 100644
--- a/src/ARMeilleure/Translation/Cache/CacheMemoryAllocator.cs
+++ b/src/ARMeilleure/Translation/Cache/CacheMemoryAllocator.cs
@@ -23,7 +23,7 @@ namespace ARMeilleure.Translation.Cache
}
}
- private readonly List<MemoryBlock> _blocks = new List<MemoryBlock>();
+ private readonly List<MemoryBlock> _blocks = new();
public CacheMemoryAllocator(int capacity)
{
diff --git a/src/ARMeilleure/Translation/Cache/JitCache.cs b/src/ARMeilleure/Translation/Cache/JitCache.cs
index 03cba5ad..91a05412 100644
--- a/src/ARMeilleure/Translation/Cache/JitCache.cs
+++ b/src/ARMeilleure/Translation/Cache/JitCache.cs
@@ -13,8 +13,8 @@ namespace ARMeilleure.Translation.Cache
{
static partial class JitCache
{
- private static readonly int PageSize = (int)MemoryBlock.GetPageSize();
- private static readonly int PageMask = PageSize - 1;
+ private static readonly int _pageSize = (int)MemoryBlock.GetPageSize();
+ private static readonly int _pageMask = _pageSize - 1;
private const int CodeAlignment = 4; // Bytes.
private const int CacheSize = 2047 * 1024 * 1024;
@@ -24,7 +24,7 @@ namespace ARMeilleure.Translation.Cache
private static CacheMemoryAllocator _cacheAllocator;
- private static readonly List<CacheEntry> _cacheEntries = new List<CacheEntry>();
+ private static readonly List<CacheEntry> _cacheEntries = new();
private static readonly object _lock = new();
private static bool _initialized;
@@ -35,11 +35,17 @@ namespace ARMeilleure.Translation.Cache
public static void Initialize(IJitMemoryAllocator allocator)
{
- if (_initialized) return;
+ if (_initialized)
+ {
+ return;
+ }
lock (_lock)
{
- if (_initialized) return;
+ if (_initialized)
+ {
+ return;
+ }
_jitRegion = new ReservedRegion(allocator, CacheSize);
@@ -52,7 +58,7 @@ namespace ARMeilleure.Translation.Cache
if (OperatingSystem.IsWindows())
{
- JitUnwindWindows.InstallFunctionTableHandler(_jitRegion.Pointer, CacheSize, _jitRegion.Pointer + Allocate(PageSize));
+ JitUnwindWindows.InstallFunctionTableHandler(_jitRegion.Pointer, CacheSize, _jitRegion.Pointer + Allocate(_pageSize));
}
_initialized = true;
@@ -75,7 +81,7 @@ namespace ARMeilleure.Translation.Cache
{
unsafe
{
- fixed (byte *codePtr = code)
+ fixed (byte* codePtr = code)
{
JitSupportDarwin.Copy(funcPtr, (IntPtr)codePtr, (ulong)code.Length);
}
@@ -124,8 +130,8 @@ namespace ARMeilleure.Translation.Cache
{
int endOffs = offset + size;
- int regionStart = offset & ~PageMask;
- int regionEnd = (endOffs + PageMask) & ~PageMask;
+ int regionStart = offset & ~_pageMask;
+ int regionEnd = (endOffs + _pageMask) & ~_pageMask;
_jitRegion.Block.MapAsRwx((ulong)regionStart, (ulong)(regionEnd - regionStart));
}
@@ -134,8 +140,8 @@ namespace ARMeilleure.Translation.Cache
{
int endOffs = offset + size;
- int regionStart = offset & ~PageMask;
- int regionEnd = (endOffs + PageMask) & ~PageMask;
+ int regionStart = offset & ~_pageMask;
+ int regionEnd = (endOffs + _pageMask) & ~_pageMask;
_jitRegion.Block.MapAsRx((ulong)regionStart, (ulong)(regionEnd - regionStart));
}
@@ -163,7 +169,7 @@ namespace ARMeilleure.Translation.Cache
private static void Add(int offset, int size, UnwindInfo unwindInfo)
{
- CacheEntry entry = new CacheEntry(offset, size, unwindInfo);
+ CacheEntry entry = new(offset, size, unwindInfo);
int index = _cacheEntries.BinarySearch(entry);
@@ -212,4 +218,4 @@ namespace ARMeilleure.Translation.Cache
return false;
}
}
-} \ No newline at end of file
+}
diff --git a/src/ARMeilleure/Translation/Cache/JitCacheInvalidation.cs b/src/ARMeilleure/Translation/Cache/JitCacheInvalidation.cs
index 57f7bf12..3aa2e19f 100644
--- a/src/ARMeilleure/Translation/Cache/JitCacheInvalidation.cs
+++ b/src/ARMeilleure/Translation/Cache/JitCacheInvalidation.cs
@@ -6,7 +6,7 @@ namespace ARMeilleure.Translation.Cache
{
class JitCacheInvalidation
{
- private static int[] _invalidationCode = new int[]
+ private static readonly int[] _invalidationCode = new int[]
{
unchecked((int)0xd53b0022), // mrs x2, ctr_el0
unchecked((int)0xd3504c44), // ubfx x4, x2, #16, #4
@@ -40,8 +40,8 @@ namespace ARMeilleure.Translation.Cache
private delegate void InvalidateCache(ulong start, ulong end);
- private InvalidateCache _invalidateCache;
- private ReservedRegion _invalidateCacheCodeRegion;
+ private readonly InvalidateCache _invalidateCache;
+ private readonly ReservedRegion _invalidateCacheCodeRegion;
private readonly bool _needsInvalidation;
@@ -76,4 +76,4 @@ namespace ARMeilleure.Translation.Cache
}
}
}
-} \ No newline at end of file
+}
diff --git a/src/ARMeilleure/Translation/Cache/JitUnwindWindows.cs b/src/ARMeilleure/Translation/Cache/JitUnwindWindows.cs
index 77727bf1..91fd19c2 100644
--- a/src/ARMeilleure/Translation/Cache/JitUnwindWindows.cs
+++ b/src/ARMeilleure/Translation/Cache/JitUnwindWindows.cs
@@ -29,15 +29,15 @@ namespace ARMeilleure.Translation.Cache
private enum UnwindOp
{
- PushNonvol = 0,
- AllocLarge = 1,
- AllocSmall = 2,
- SetFpreg = 3,
- SaveNonvol = 4,
+ PushNonvol = 0,
+ AllocLarge = 1,
+ AllocSmall = 2,
+ SetFpreg = 3,
+ SaveNonvol = 4,
SaveNonvolFar = 5,
- SaveXmm128 = 8,
+ SaveXmm128 = 8,
SaveXmm128Far = 9,
- PushMachframe = 10
+ PushMachframe = 10,
}
private unsafe delegate RuntimeFunction* GetRuntimeFunctionCallback(ulong controlPc, IntPtr context);
@@ -111,72 +111,73 @@ namespace ARMeilleure.Translation.Cache
switch (entry.PseudoOp)
{
case UnwindPseudoOp.SaveXmm128:
- {
- int stackOffset = entry.StackOffsetOrAllocSize;
-
- Debug.Assert(stackOffset % 16 == 0);
-
- if (stackOffset <= 0xFFFF0)
- {
- _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.SaveXmm128, entry.PrologOffset, entry.RegIndex);
- _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(stackOffset / 16);
- }
- else
{
- _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.SaveXmm128Far, entry.PrologOffset, entry.RegIndex);
- _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(stackOffset >> 0);
- _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(stackOffset >> 16);
+ int stackOffset = entry.StackOffsetOrAllocSize;
+
+ Debug.Assert(stackOffset % 16 == 0);
+
+ if (stackOffset <= 0xFFFF0)
+ {
+ _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.SaveXmm128, entry.PrologOffset, entry.RegIndex);
+ _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(stackOffset / 16);
+ }
+ else
+ {
+ _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.SaveXmm128Far, entry.PrologOffset, entry.RegIndex);
+ _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(stackOffset >> 0);
+ _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(stackOffset >> 16);
+ }
+
+ break;
}
- break;
- }
-
case UnwindPseudoOp.AllocStack:
- {
- int allocSize = entry.StackOffsetOrAllocSize;
-
- Debug.Assert(allocSize % 8 == 0);
-
- if (allocSize <= 128)
{
- _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.AllocSmall, entry.PrologOffset, (allocSize / 8) - 1);
- }
- else if (allocSize <= 0x7FFF8)
- {
- _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.AllocLarge, entry.PrologOffset, 0);
- _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(allocSize / 8);
- }
- else
- {
- _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.AllocLarge, entry.PrologOffset, 1);
- _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(allocSize >> 0);
- _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(allocSize >> 16);
+ int allocSize = entry.StackOffsetOrAllocSize;
+
+ Debug.Assert(allocSize % 8 == 0);
+
+ if (allocSize <= 128)
+ {
+ _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.AllocSmall, entry.PrologOffset, (allocSize / 8) - 1);
+ }
+ else if (allocSize <= 0x7FFF8)
+ {
+ _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.AllocLarge, entry.PrologOffset, 0);
+ _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(allocSize / 8);
+ }
+ else
+ {
+ _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.AllocLarge, entry.PrologOffset, 1);
+ _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(allocSize >> 0);
+ _unwindInfo->UnwindCodes[codeIndex++] = (ushort)(allocSize >> 16);
+ }
+
+ break;
}
- break;
- }
-
case UnwindPseudoOp.PushReg:
- {
- _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.PushNonvol, entry.PrologOffset, entry.RegIndex);
+ {
+ _unwindInfo->UnwindCodes[codeIndex++] = PackUnwindOp(UnwindOp.PushNonvol, entry.PrologOffset, entry.RegIndex);
- break;
- }
+ break;
+ }
- default: throw new NotImplementedException($"({nameof(entry.PseudoOp)} = {entry.PseudoOp})");
+ default:
+ throw new NotImplementedException($"({nameof(entry.PseudoOp)} = {entry.PseudoOp})");
}
}
Debug.Assert(codeIndex <= MaxUnwindCodesArraySize);
- _unwindInfo->VersionAndFlags = 1; // Flags: The function has no handler.
- _unwindInfo->SizeOfProlog = (byte)unwindInfo.PrologSize;
+ _unwindInfo->VersionAndFlags = 1; // Flags: The function has no handler.
+ _unwindInfo->SizeOfProlog = (byte)unwindInfo.PrologSize;
_unwindInfo->CountOfUnwindCodes = (byte)codeIndex;
- _unwindInfo->FrameRegister = 0;
+ _unwindInfo->FrameRegister = 0;
_runtimeFunction->BeginAddress = (uint)funcEntry.Offset;
- _runtimeFunction->EndAddress = (uint)(funcEntry.Offset + funcEntry.Size);
- _runtimeFunction->UnwindData = (uint)_sizeOfRuntimeFunction;
+ _runtimeFunction->EndAddress = (uint)(funcEntry.Offset + funcEntry.Size);
+ _runtimeFunction->UnwindData = (uint)_sizeOfRuntimeFunction;
return _runtimeFunction;
}
@@ -186,4 +187,4 @@ namespace ARMeilleure.Translation.Cache
return (ushort)(prologOffset | ((int)op << 8) | (opInfo << 12));
}
}
-} \ No newline at end of file
+}