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>
This commit is contained in:
smurf3tte 2020-12-19 13:33:47 -08:00 committed by TryTwo
parent 3417920167
commit 1224575b0b

View file

@ -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;