From 1224575b0be603562af65009287c4b501ae0a97f Mon Sep 17 00:00:00 2001 From: smurf3tte <75271109+smurf3tte@users.noreply.github.com> Date: Sat, 19 Dec 2020 13:33:47 -0800 Subject: [PATCH] Add cast expression functions u8(), s8(), u16(), etc. This allows for easy reinterpretation of GPRs in an expression. Co-authored-by: TryTwo --- Source/Core/Core/PowerPC/Expression.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Source/Core/Core/PowerPC/Expression.cpp b/Source/Core/Core/PowerPC/Expression.cpp index d54ba84e71..cde531e94b 100644 --- a/Source/Core/Core/PowerPC/Expression.cpp +++ b/Source/Core/Core/PowerPC/Expression.cpp @@ -3,6 +3,7 @@ #include "Core/PowerPC/Expression.h" +#include #include #include #include @@ -11,8 +12,28 @@ #include +#include "Common/BitUtils.h" +#include "Common/CommonTypes.h" #include "Core/PowerPC/PowerPC.h" +template +static double CastFunc(expr_func* f, vec_expr_t* args, void* c) +{ + if (vec_len(args) != 1) + return 0; + return Common::BitCast(static_cast(expr_eval(&vec_nth(args, 0)))); +} + +static std::array g_expr_funcs{{ + {"u8", CastFunc}, + {"s8", CastFunc}, + {"u16", CastFunc}, + {"s16", CastFunc}, + {"u32", CastFunc}, + {"s32", CastFunc}, + {}, +}}; + void ExprDeleter::operator()(expr* expression) const { expr_destroy(expression, nullptr); @@ -69,7 +90,7 @@ Expression::Expression(std::string_view text, ExprPointer ex, ExprVarListPointer std::optional Expression::TryParse(std::string_view text) { ExprVarListPointer vars{new expr_var_list{}}; - ExprPointer ex{expr_create(text.data(), text.length(), vars.get(), nullptr)}; + ExprPointer ex{expr_create(text.data(), text.length(), vars.get(), g_expr_funcs.data())}; if (!ex) return std::nullopt;