From e3258e852546e3f119e54c5f77b1583498c280f2 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 4 Oct 2018 05:10:08 -0400 Subject: [PATCH] ir/value: Add a GetImmediateAsS64() function Provides a signed analogue to GetImmediateAsU64() for consistency with both integral classes when it comes to signed/unsigned.. --- src/frontend/ir/value.cpp | 20 ++++++++++++++++++++ src/frontend/ir/value.h | 10 +++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/frontend/ir/value.cpp b/src/frontend/ir/value.cpp index 5c3eb4b5..c3742227 100644 --- a/src/frontend/ir/value.cpp +++ b/src/frontend/ir/value.cpp @@ -5,6 +5,7 @@ */ #include "common/assert.h" +#include "common/bit_util.h" #include "frontend/ir/microinstruction.h" #include "frontend/ir/opcodes.h" #include "frontend/ir/type.h" @@ -155,6 +156,25 @@ Cond Value::GetCond() const { 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 { ASSERT(IsImmediate()); diff --git a/src/frontend/ir/value.h b/src/frontend/ir/value.h index a8496d7b..cd3bbf07 100644 --- a/src/frontend/ir/value.h +++ b/src/frontend/ir/value.h @@ -67,7 +67,15 @@ public: 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. * Breaking this precondition will cause an assertion to be invoked.