From 83e92e6881f29cfce95b0df80ef0f88636b3265e Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 22 Mar 2021 21:37:13 +0100 Subject: [PATCH] ditch a bunch of reinterpret_casts --- src/cs_bcode.cc | 14 +++++++------- src/cs_bcode.hh | 12 +++++++++++- src/cs_val.cc | 6 ++---- src/cs_vm.cc | 15 ++++++--------- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/cs_bcode.cc b/src/cs_bcode.cc index e8f40b0..d0bbc2b 100644 --- a/src/cs_bcode.cc +++ b/src/cs_bcode.cc @@ -7,29 +7,29 @@ namespace cscript { /* public API impls */ LIBCUBESCRIPT_EXPORT cs_bcode_ref::cs_bcode_ref(cs_bcode *v): p_code(v) { - bcode_ref(reinterpret_cast(p_code)); + bcode_ref(v->get_raw()); } LIBCUBESCRIPT_EXPORT cs_bcode_ref::cs_bcode_ref(cs_bcode_ref const &v): p_code(v.p_code) { - bcode_ref(reinterpret_cast(p_code)); + bcode_ref(p_code->get_raw()); } LIBCUBESCRIPT_EXPORT cs_bcode_ref::~cs_bcode_ref() { - bcode_unref(reinterpret_cast(p_code)); + bcode_unref(p_code->get_raw()); } LIBCUBESCRIPT_EXPORT cs_bcode_ref &cs_bcode_ref::operator=( cs_bcode_ref const &v ) { - bcode_unref(reinterpret_cast(p_code)); + bcode_unref(p_code->get_raw()); p_code = v.p_code; - bcode_ref(reinterpret_cast(p_code)); + bcode_ref(p_code->get_raw()); return *this; } LIBCUBESCRIPT_EXPORT cs_bcode_ref &cs_bcode_ref::operator=(cs_bcode_ref &&v) { - bcode_unref(reinterpret_cast(p_code)); + bcode_unref(p_code->get_raw()); p_code = v.p_code; v.p_code = nullptr; return *this; @@ -40,7 +40,7 @@ LIBCUBESCRIPT_EXPORT cs_bcode_ref &cs_bcode_ref::operator=(cs_bcode_ref &&v) { struct bcode_hdr { cs_shared_state *cs; /* needed to construct the allocator */ 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 */ diff --git a/src/cs_bcode.hh b/src/cs_bcode.hh index 8205cd2..3eda361 100644 --- a/src/cs_bcode.hh +++ b/src/cs_bcode.hh @@ -8,7 +8,17 @@ 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 { CS_VAL_NULL = 0, CS_VAL_INT, CS_VAL_FLOAT, CS_VAL_STRING, diff --git a/src/cs_val.cc b/src/cs_val.cc index 77e3b08..0acd302 100644 --- a/src/cs_val.cc +++ b/src/cs_val.cc @@ -166,7 +166,7 @@ void cs_value::set_none() { void cs_value::set_code(cs_bcode *val) { csv_cleanup(p_type, p_stor); p_type = cs_value_type::CODE; - bcode_ref(reinterpret_cast(val)); + bcode_ref(val->get_raw()); csv_get(p_stor) = val; } @@ -328,9 +328,7 @@ LIBCUBESCRIPT_EXPORT bool cs_code_is_empty(cs_bcode *code) { if (!code) { return true; } - return ( - *reinterpret_cast(code) & CS_CODE_OP_MASK - ) == CS_CODE_EXIT; + return (*code->get_raw() & CS_CODE_OP_MASK) == CS_CODE_EXIT; } bool cs_value::code_is_empty() const { diff --git a/src/cs_vm.cc b/src/cs_vm.cc index 723b66d..f943908 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -136,9 +136,8 @@ cs_stack_state cs_error::save_stack(cs_state &cs) { } void cs_alias_impl::clean_code() { - uint32_t *bcode = reinterpret_cast(p_acode); - if (bcode) { - bcode_decr(bcode); + if (p_acode) { + bcode_decr(p_acode->get_raw()); 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) { - uint32_t *code = reinterpret_cast(v.get_code()); + auto *code = v.get_code(); if (!code) { cs_gen_state gs(cs); 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()); memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(uint32_t)); v.set_code(reinterpret_cast(cbuf + 1)); - code = reinterpret_cast(v.get_code()); + code = v.get_code(); } - return code; + return code->get_raw(); } 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<( - static_cast(a)->compile_code(cs) - ); + uint32_t *codep = static_cast(a)->compile_code(cs)->get_raw(); bcode_incr(codep); cs_do_and_cleanup([&]() { runcode(cs, codep+1, result);