From feec574ece30f4fdd8dc61c8e8d6510cfdd314ed Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Sat, 7 Mar 2015 23:04:48 +0100 Subject: [PATCH] Arm64Emitter: Add some helper functions to add / sub by immediate --- Common/Arm64Emitter.cpp | 54 ++++++++++++++++++++++++++++++++++++++--- Common/Arm64Emitter.h | 4 +++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/Common/Arm64Emitter.cpp b/Common/Arm64Emitter.cpp index 65c9299c64..9b08170942 100644 --- a/Common/Arm64Emitter.cpp +++ b/Common/Arm64Emitter.cpp @@ -40,6 +40,19 @@ bool IsPowerOfTwo(uint64_t x) { #define V8_UINT64_C(x) ((uint64_t)(x)) +static bool IsImmArithmetic(uint64_t input, u32 *val, bool *shift) { + if (input < 4096) { + *val = input; + *shift = false; + return true; + } else if ((input & 0xFFF000) == input) { + *val = input >> 12; + *shift = true; + return true; + } + return false; +} + static bool IsImmLogical(uint64_t value, unsigned int width, unsigned int *n, @@ -3065,7 +3078,7 @@ void ARM64XEmitter::ANDI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch) if (IsImmLogical(imm, Is64Bit(Rn) ? 64 : 32, &n, &imm_s, &imm_r)) { AND(Rd, Rn, imm_r, imm_s); } else { - _assert_msg_(JIT, scratch != INVALID_REG, "ANDSI2R - failed to construct immediate value from %08x, need scratch", (u32)imm) + _assert_msg_(JIT, scratch != INVALID_REG, "ANDSI2R - failed to construct immediate value from %08x, need scratch", (u32)imm); MOVI2R(scratch, imm); AND(Rd, Rn, scratch); } @@ -3076,7 +3089,7 @@ void ARM64XEmitter::ORI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch) { if (IsImmLogical(imm, Is64Bit(Rn) ? 64 : 32, &n, &imm_s, &imm_r)) { ORR(Rd, Rn, imm_r, imm_s); } else { - _assert_msg_(JIT, scratch != INVALID_REG, "ORI2R - failed to construct immediate value from %08x, need scratch", (u32)imm) + _assert_msg_(JIT, scratch != INVALID_REG, "ORI2R - failed to construct immediate value from %08x, need scratch", (u32)imm); MOVI2R(scratch, imm); ORR(Rd, Rn, scratch); } @@ -3087,11 +3100,46 @@ void ARM64XEmitter::ANDSI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch) if (IsImmLogical(imm, Is64Bit(Rn) ? 64 : 32, &n, &imm_s, &imm_r)) { ANDS(Rd, Rn, imm_r, imm_s); } else { - _assert_msg_(JIT, scratch != INVALID_REG, "ANDSI2R - failed to construct immediate value from %08x, need scratch", (u32)imm) + _assert_msg_(JIT, scratch != INVALID_REG, "ANDSI2R - failed to construct immediate value from %08x, need scratch", (u32)imm); MOVI2R(scratch, imm); ANDS(Rd, Rn, scratch); } } +void ARM64XEmitter::ADDI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch) { + u32 val; + bool shift; + if (IsImmArithmetic(imm, &val, &shift)) { + ADD(Rd, Rn, val, shift); + } else { + _assert_msg_(JIT, scratch != INVALID_REG, "ANDSI2R - failed to construct immediate value from %08x, need scratch", (u32)imm); + MOVI2R(scratch, imm); + ADD(Rd, Rn, scratch); + } } +void ARM64XEmitter::SUBI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch) { + u32 val; + bool shift; + if (IsImmArithmetic(imm, &val, &shift)) { + SUB(Rd, Rn, val, shift); + } else { + _assert_msg_(JIT, scratch != INVALID_REG, "ANDSI2R - failed to construct immediate value from %08x, need scratch", (u32)imm); + MOVI2R(scratch, imm); + SUB(Rd, Rn, scratch); + } +} + +void ARM64XEmitter::SUBSI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch) { + u32 val; + bool shift; + if (IsImmArithmetic(imm, &val, &shift)) { + SUBS(Rd, Rn, val, shift); + } else { + _assert_msg_(JIT, scratch != INVALID_REG, "ANDSI2R - failed to construct immediate value from %08x, need scratch", (u32)imm); + MOVI2R(scratch, imm); + SUBS(Rd, Rn, scratch); + } +} + +} // namespace diff --git a/Common/Arm64Emitter.h b/Common/Arm64Emitter.h index a74e083c1d..68ac3ae093 100644 --- a/Common/Arm64Emitter.h +++ b/Common/Arm64Emitter.h @@ -649,6 +649,10 @@ public: void TSTI2R(ARM64Reg Rn, u64 imm, ARM64Reg scratch = INVALID_REG) { ANDSI2R(Is64Bit(Rn) ? SP : WSP, Rn, imm, scratch); } void ORI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch = INVALID_REG); + void ADDI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch = INVALID_REG); + void SUBI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch = INVALID_REG); + void SUBSI2R(ARM64Reg Rd, ARM64Reg Rn, u64 imm, ARM64Reg scratch = INVALID_REG); + // ABI related void ABI_PushRegisters(BitSet32 registers); void ABI_PopRegisters(BitSet32 registers, BitSet32 ignore_mask = BitSet32(0));