From a95010bc72c0a5ad0c6b0604a18c929e5dc779b1 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Wed, 12 Apr 2017 07:52:40 +0100 Subject: [PATCH] x64Emitter: Allow code alignment to arbitrary power of 2 --- Source/Core/Common/x64Emitter.cpp | 25 +++++++++++++------------ Source/Core/Common/x64Emitter.h | 1 + 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Source/Core/Common/x64Emitter.cpp b/Source/Core/Common/x64Emitter.cpp index 34965a6a24..7db8beac51 100644 --- a/Source/Core/Common/x64Emitter.cpp +++ b/Source/Core/Common/x64Emitter.cpp @@ -120,28 +120,29 @@ void XEmitter::ReserveCodeSpace(int bytes) *code++ = 0xCC; } +const u8* XEmitter::AlignCodeTo(size_t alignment) +{ + _assert_msg_(DYNA_REC, alignment != 0 && (alignment & (alignment - 1)) == 0, + "Alignment must be power of two"); + u64 c = reinterpret_cast(code) & (alignment - 1); + if (c) + ReserveCodeSpace(static_cast(alignment - c)); + return code; +} + const u8* XEmitter::AlignCode4() { - int c = int((u64)code & 3); - if (c) - ReserveCodeSpace(4 - c); - return code; + return AlignCodeTo(4); } const u8* XEmitter::AlignCode16() { - int c = int((u64)code & 15); - if (c) - ReserveCodeSpace(16 - c); - return code; + return AlignCodeTo(16); } const u8* XEmitter::AlignCodePage() { - int c = int((u64)code & 4095); - if (c) - ReserveCodeSpace(4096 - c); - return code; + return AlignCodeTo(4096); } // This operation modifies flags; check to see the flags are locked. diff --git a/Source/Core/Common/x64Emitter.h b/Source/Core/Common/x64Emitter.h index b294ed1358..1879d6b16c 100644 --- a/Source/Core/Common/x64Emitter.h +++ b/Source/Core/Common/x64Emitter.h @@ -412,6 +412,7 @@ public: virtual ~XEmitter() {} void SetCodePtr(u8* ptr); void ReserveCodeSpace(int bytes); + const u8* AlignCodeTo(size_t alignment); const u8* AlignCode4(); const u8* AlignCode16(); const u8* AlignCodePage();