ditch a bunch of reinterpret_casts

master
Daniel Kolesa 2021-03-22 21:37:13 +01:00
parent 3d2f115de5
commit 83e92e6881
4 changed files with 26 additions and 21 deletions

View File

@ -7,29 +7,29 @@ namespace cscript {
/* public API impls */ /* public API impls */
LIBCUBESCRIPT_EXPORT cs_bcode_ref::cs_bcode_ref(cs_bcode *v): p_code(v) { LIBCUBESCRIPT_EXPORT cs_bcode_ref::cs_bcode_ref(cs_bcode *v): p_code(v) {
bcode_ref(reinterpret_cast<std::uint32_t *>(p_code)); bcode_ref(v->get_raw());
} }
LIBCUBESCRIPT_EXPORT cs_bcode_ref::cs_bcode_ref(cs_bcode_ref const &v): LIBCUBESCRIPT_EXPORT cs_bcode_ref::cs_bcode_ref(cs_bcode_ref const &v):
p_code(v.p_code) p_code(v.p_code)
{ {
bcode_ref(reinterpret_cast<std::uint32_t *>(p_code)); bcode_ref(p_code->get_raw());
} }
LIBCUBESCRIPT_EXPORT cs_bcode_ref::~cs_bcode_ref() { LIBCUBESCRIPT_EXPORT cs_bcode_ref::~cs_bcode_ref() {
bcode_unref(reinterpret_cast<std::uint32_t *>(p_code)); bcode_unref(p_code->get_raw());
} }
LIBCUBESCRIPT_EXPORT cs_bcode_ref &cs_bcode_ref::operator=( LIBCUBESCRIPT_EXPORT cs_bcode_ref &cs_bcode_ref::operator=(
cs_bcode_ref const &v cs_bcode_ref const &v
) { ) {
bcode_unref(reinterpret_cast<std::uint32_t *>(p_code)); bcode_unref(p_code->get_raw());
p_code = v.p_code; p_code = v.p_code;
bcode_ref(reinterpret_cast<std::uint32_t *>(p_code)); bcode_ref(p_code->get_raw());
return *this; return *this;
} }
LIBCUBESCRIPT_EXPORT cs_bcode_ref &cs_bcode_ref::operator=(cs_bcode_ref &&v) { LIBCUBESCRIPT_EXPORT cs_bcode_ref &cs_bcode_ref::operator=(cs_bcode_ref &&v) {
bcode_unref(reinterpret_cast<std::uint32_t *>(p_code)); bcode_unref(p_code->get_raw());
p_code = v.p_code; p_code = v.p_code;
v.p_code = nullptr; v.p_code = nullptr;
return *this; return *this;
@ -40,7 +40,7 @@ LIBCUBESCRIPT_EXPORT cs_bcode_ref &cs_bcode_ref::operator=(cs_bcode_ref &&v) {
struct bcode_hdr { struct bcode_hdr {
cs_shared_state *cs; /* needed to construct the allocator */ cs_shared_state *cs; /* needed to construct the allocator */
std::size_t asize; /* alloc size of the bytecode block */ std::size_t asize; /* alloc size of the bytecode block */
std::uint32_t init; /* CS_CODE_START + refcount */ cs_bcode bc; /* CS_CODE_START + refcount */
}; };
/* returned address is the 'init' member of the header */ /* returned address is the 'init' member of the header */

View File

@ -8,7 +8,17 @@
namespace cscript { namespace cscript {
struct cs_bcode; struct cs_bcode {
std::uint32_t init;
std::uint32_t *get_raw() {
return &init;
}
std::uint32_t const *get_raw() const {
return &init;
}
};
enum { enum {
CS_VAL_NULL = 0, CS_VAL_INT, CS_VAL_FLOAT, CS_VAL_STRING, CS_VAL_NULL = 0, CS_VAL_INT, CS_VAL_FLOAT, CS_VAL_STRING,

View File

@ -166,7 +166,7 @@ void cs_value::set_none() {
void cs_value::set_code(cs_bcode *val) { void cs_value::set_code(cs_bcode *val) {
csv_cleanup(p_type, p_stor); csv_cleanup(p_type, p_stor);
p_type = cs_value_type::CODE; p_type = cs_value_type::CODE;
bcode_ref(reinterpret_cast<uint32_t *>(val)); bcode_ref(val->get_raw());
csv_get<cs_bcode *>(p_stor) = val; csv_get<cs_bcode *>(p_stor) = val;
} }
@ -328,9 +328,7 @@ LIBCUBESCRIPT_EXPORT bool cs_code_is_empty(cs_bcode *code) {
if (!code) { if (!code) {
return true; return true;
} }
return ( return (*code->get_raw() & CS_CODE_OP_MASK) == CS_CODE_EXIT;
*reinterpret_cast<uint32_t *>(code) & CS_CODE_OP_MASK
) == CS_CODE_EXIT;
} }
bool cs_value::code_is_empty() const { bool cs_value::code_is_empty() const {

View File

@ -136,9 +136,8 @@ cs_stack_state cs_error::save_stack(cs_state &cs) {
} }
void cs_alias_impl::clean_code() { void cs_alias_impl::clean_code() {
uint32_t *bcode = reinterpret_cast<uint32_t *>(p_acode); if (p_acode) {
if (bcode) { bcode_decr(p_acode->get_raw());
bcode_decr(bcode);
p_acode = nullptr; p_acode = nullptr;
} }
} }
@ -158,7 +157,7 @@ cs_bcode *cs_alias_impl::compile_code(cs_state &cs) {
} }
static inline uint32_t *forcecode(cs_state &cs, cs_value &v) { static inline uint32_t *forcecode(cs_state &cs, cs_value &v) {
uint32_t *code = reinterpret_cast<uint32_t *>(v.get_code()); auto *code = v.get_code();
if (!code) { if (!code) {
cs_gen_state gs(cs); cs_gen_state gs(cs);
gs.code.reserve(64); gs.code.reserve(64);
@ -167,9 +166,9 @@ static inline uint32_t *forcecode(cs_state &cs, cs_value &v) {
uint32_t *cbuf = bcode_alloc(cs, gs.code.size()); uint32_t *cbuf = bcode_alloc(cs, gs.code.size());
memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(uint32_t)); memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(uint32_t));
v.set_code(reinterpret_cast<cs_bcode *>(cbuf + 1)); v.set_code(reinterpret_cast<cs_bcode *>(cbuf + 1));
code = reinterpret_cast<uint32_t *>(v.get_code()); code = v.get_code();
} }
return code; return code->get_raw();
} }
static inline void forcecond(cs_state &cs, cs_value &v) { static inline void forcecond(cs_state &cs, cs_value &v) {
@ -385,9 +384,7 @@ static inline void cs_call_alias(
a, cs.p_callstack, (1<<callargs)-1, &argstack[0] a, cs.p_callstack, (1<<callargs)-1, &argstack[0]
}; };
cs.p_callstack = &aliaslink; cs.p_callstack = &aliaslink;
uint32_t *codep = reinterpret_cast<uint32_t *>( uint32_t *codep = static_cast<cs_alias_impl *>(a)->compile_code(cs)->get_raw();
static_cast<cs_alias_impl *>(a)->compile_code(cs)
);
bcode_incr(codep); bcode_incr(codep);
cs_do_and_cleanup([&]() { cs_do_and_cleanup([&]() {
runcode(cs, codep+1, result); runcode(cs, codep+1, result);