ir/value: Add a GetImmediateAsS64() function

Provides a signed analogue to GetImmediateAsU64() for consistency with
both integral classes when it comes to signed/unsigned..
This commit is contained in:
Lioncash 2018-10-04 05:10:08 -04:00 committed by MerryMage
parent 4a3c064b15
commit e3258e8525
2 changed files with 29 additions and 1 deletions

View file

@ -5,6 +5,7 @@
*/ */
#include "common/assert.h" #include "common/assert.h"
#include "common/bit_util.h"
#include "frontend/ir/microinstruction.h" #include "frontend/ir/microinstruction.h"
#include "frontend/ir/opcodes.h" #include "frontend/ir/opcodes.h"
#include "frontend/ir/type.h" #include "frontend/ir/type.h"
@ -155,6 +156,25 @@ Cond Value::GetCond() const {
return inner.imm_cond; return inner.imm_cond;
} }
s64 Value::GetImmediateAsS64() const {
ASSERT(IsImmediate());
switch (GetType()) {
case IR::Type::U1:
return s64(GetU1());
case IR::Type::U8:
return s64(Common::SignExtend<8, u64>(GetU8()));
case IR::Type::U16:
return s64(Common::SignExtend<16, u64>(GetU16()));
case IR::Type::U32:
return s64(Common::SignExtend<32, u64>(GetU32()));
case IR::Type::U64:
return s64(GetU64());
default:
ASSERT_MSG(false, "GetImmediateAsS64 called on an incompatible Value type.");
}
}
u64 Value::GetImmediateAsU64() const { u64 Value::GetImmediateAsU64() const {
ASSERT(IsImmediate()); ASSERT(IsImmediate());

View file

@ -67,7 +67,15 @@ public:
Cond GetCond() const; Cond GetCond() const;
/** /**
* Retrieves the immediate of a Value instance. * Retrieves the immediate of a Value instance as a signed 64-bit value.
*
* @pre The value contains either a U1, U8, U16, U32, or U64 value.
* Breaking this precondition will cause an assertion to be invoked.
*/
s64 GetImmediateAsS64() const;
/**
* Retrieves the immediate of a Value instance as an unsigned 64-bit value.
* *
* @pre The value contains either a U1, U8, U16, U32, or U64 value. * @pre The value contains either a U1, U8, U16, U32, or U64 value.
* Breaking this precondition will cause an assertion to be invoked. * Breaking this precondition will cause an assertion to be invoked.