x64Emitter: Allow code alignment to arbitrary power of 2

This commit is contained in:
MerryMage 2017-04-12 07:52:40 +01:00
parent 29344cb5ff
commit a95010bc72
2 changed files with 14 additions and 12 deletions

View File

@ -120,28 +120,29 @@ void XEmitter::ReserveCodeSpace(int bytes)
*code++ = 0xCC; *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<u64>(code) & (alignment - 1);
if (c)
ReserveCodeSpace(static_cast<int>(alignment - c));
return code;
}
const u8* XEmitter::AlignCode4() const u8* XEmitter::AlignCode4()
{ {
int c = int((u64)code & 3); return AlignCodeTo(4);
if (c)
ReserveCodeSpace(4 - c);
return code;
} }
const u8* XEmitter::AlignCode16() const u8* XEmitter::AlignCode16()
{ {
int c = int((u64)code & 15); return AlignCodeTo(16);
if (c)
ReserveCodeSpace(16 - c);
return code;
} }
const u8* XEmitter::AlignCodePage() const u8* XEmitter::AlignCodePage()
{ {
int c = int((u64)code & 4095); return AlignCodeTo(4096);
if (c)
ReserveCodeSpace(4096 - c);
return code;
} }
// This operation modifies flags; check to see the flags are locked. // This operation modifies flags; check to see the flags are locked.

View File

@ -412,6 +412,7 @@ public:
virtual ~XEmitter() {} virtual ~XEmitter() {}
void SetCodePtr(u8* ptr); void SetCodePtr(u8* ptr);
void ReserveCodeSpace(int bytes); void ReserveCodeSpace(int bytes);
const u8* AlignCodeTo(size_t alignment);
const u8* AlignCode4(); const u8* AlignCode4();
const u8* AlignCode16(); const u8* AlignCode16();
const u8* AlignCodePage(); const u8* AlignCodePage();