summaryrefslogtreecommitdiff
path: root/Source/Core
diff options
context:
space:
mode:
authorsmurf3tte <75271109+smurf3tte@users.noreply.github.com>2020-12-19 13:33:47 -0800
committerTryTwo <taolas@gmail.com>2022-10-06 21:34:44 -0700
commit1224575b0be603562af65009287c4b501ae0a97f (patch)
tree9e491c380264a1e1c8da77deb95744f3161bdd4c /Source/Core
parent341792016746eecd2fef255a4d4c62a8332f9b50 (diff)
Add cast expression functions u8(), s8(), u16(), etc.
This allows for easy reinterpretation of GPRs in an expression. Co-authored-by: TryTwo <taolas@gmail.com>
Diffstat (limited to 'Source/Core')
-rw-r--r--Source/Core/Core/PowerPC/Expression.cpp23
1 files changed, 22 insertions, 1 deletions
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 <algorithm>
#include <cstdlib>
#include <optional>
#include <string>
@@ -11,8 +12,28 @@
#include <expr.h>
+#include "Common/BitUtils.h"
+#include "Common/CommonTypes.h"
#include "Core/PowerPC/PowerPC.h"
+template <typename T, typename U = T>
+static double CastFunc(expr_func* f, vec_expr_t* args, void* c)
+{
+ if (vec_len(args) != 1)
+ return 0;
+ return Common::BitCast<T>(static_cast<U>(expr_eval(&vec_nth(args, 0))));
+}
+
+static std::array<expr_func, 8> g_expr_funcs{{
+ {"u8", CastFunc<u8>},
+ {"s8", CastFunc<s8, u8>},
+ {"u16", CastFunc<u16>},
+ {"s16", CastFunc<s16, u16>},
+ {"u32", CastFunc<u32>},
+ {"s32", CastFunc<s32, u32>},
+ {},
+}};
+
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> 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;