From f9a49ffba74e8f741eed973d48355a2c0d233811 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 22 Mar 2021 22:01:49 +0100 Subject: [PATCH] get rid of emptyblock --- src/cs_bcode.cc | 24 ++++++++++++++++++++++++ src/cs_bcode.hh | 9 +++++++++ src/cs_util.hh | 11 ++++++++++- src/cs_vm.cc | 17 +++++------------ src/cubescript.cc | 1 - 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/cs_bcode.cc b/src/cs_bcode.cc index d0bbc2b..c111beb 100644 --- a/src/cs_bcode.cc +++ b/src/cs_bcode.cc @@ -110,4 +110,28 @@ void bcode_unref(std::uint32_t *code) { } } +/* empty fallbacks */ + +static std::uint32_t emptyrets[CS_VAL_ANY] = { + CS_RET_NULL, CS_RET_INT, CS_RET_FLOAT, CS_RET_STRING +}; + +empty_block *bcode_init_empty(cs_shared_state *cs) { + auto a = cs_allocator{cs}; + auto *p = a.allocate(CS_VAL_ANY); + for (std::size_t i = 0; i < CS_VAL_ANY; ++i) { + p[i].init.init = CS_CODE_START + 0x100; + p[i].code = CS_CODE_EXIT | emptyrets[i]; + } + return p; +} + +void bcode_free_empty(cs_shared_state *cs, empty_block *empty) { + cs_allocator{cs}.deallocate(empty, CS_VAL_ANY); +}; + +cs_bcode *bcode_get_empty(empty_block *empty, std::size_t val) { + return &empty[val].init + 1; +} + } /* namespace cscript */ diff --git a/src/cs_bcode.hh b/src/cs_bcode.hh index 3eda361..7ad32e4 100644 --- a/src/cs_bcode.hh +++ b/src/cs_bcode.hh @@ -79,6 +79,15 @@ void bcode_decr(std::uint32_t *code); void bcode_ref(std::uint32_t *code); void bcode_unref(std::uint32_t *code); +struct empty_block { + cs_bcode init; + std::uint32_t code; +}; + +empty_block *bcode_init_empty(cs_shared_state *cs); +void bcode_free_empty(cs_shared_state *cs, empty_block *empty); +cs_bcode *bcode_get_empty(empty_block *empty, std::size_t val); + } /* namespace cscript */ #endif diff --git a/src/cs_util.hh b/src/cs_util.hh index bc7ac83..c82e298 100644 --- a/src/cs_util.hh +++ b/src/cs_util.hh @@ -5,6 +5,8 @@ #include #include +#include "cs_bcode.hh" + namespace cscript { cs_int cs_parse_int( @@ -140,6 +142,7 @@ struct cs_shared_state { cs_vprint_cb varprintf; cs_strman *strman; + empty_block *empty; cs_shared_state() = delete; @@ -148,9 +151,15 @@ struct cs_shared_state { idents{allocator_type{this}}, identmap{allocator_type{this}}, varprintf{}, - strman{create(this)} + strman{create(this)}, + empty{bcode_init_empty(this)} {} + ~cs_shared_state() { + bcode_free_empty(this, empty); + destroy(strman); + } + void *alloc(void *ptr, size_t os, size_t ns) { void *p = allocf(aptr, ptr, os, ns); if (!p && ns) { diff --git a/src/cs_vm.cc b/src/cs_vm.cc index f943908..9288890 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -185,13 +185,6 @@ static inline void forcecond(cs_state &cs, cs_value &v) { } } -static uint32_t emptyblock[CS_VAL_ANY][2] = { - { CS_CODE_START + 0x100, CS_CODE_EXIT | CS_RET_NULL }, - { CS_CODE_START + 0x100, CS_CODE_EXIT | CS_RET_INT }, - { CS_CODE_START + 0x100, CS_CODE_EXIT | CS_RET_FLOAT }, - { CS_CODE_START + 0x100, CS_CODE_EXIT | CS_RET_STRING } -}; - static inline void force_arg(cs_value &v, int type) { switch (type) { case CS_RET_STRING: @@ -303,7 +296,7 @@ static inline void callcommand( break; } args[i].set_code( - reinterpret_cast(emptyblock[CS_VAL_NULL] + 1) + bcode_get_empty(cs_get_sstate(cs)->empty, CS_VAL_NULL) ); fakeargs++; } else { @@ -750,22 +743,22 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { case CS_CODE_EMPTY | CS_RET_NULL: args[numargs++].set_code( - reinterpret_cast(emptyblock[CS_VAL_NULL] + 1) + bcode_get_empty(cs_get_sstate(cs)->empty, CS_VAL_NULL) ); break; case CS_CODE_EMPTY | CS_RET_STRING: args[numargs++].set_code( - reinterpret_cast(emptyblock[CS_VAL_STRING] + 1) + bcode_get_empty(cs_get_sstate(cs)->empty, CS_VAL_STRING) ); break; case CS_CODE_EMPTY | CS_RET_INT: args[numargs++].set_code( - reinterpret_cast(emptyblock[CS_VAL_INT] + 1) + bcode_get_empty(cs_get_sstate(cs)->empty, CS_VAL_INT) ); break; case CS_CODE_EMPTY | CS_RET_FLOAT: args[numargs++].set_code( - reinterpret_cast(emptyblock[CS_VAL_FLOAT] + 1) + bcode_get_empty(cs_get_sstate(cs)->empty, CS_VAL_FLOAT) ); break; case CS_CODE_BLOCK: { diff --git a/src/cubescript.cc b/src/cubescript.cc index 8d959ef..bdc1319 100644 --- a/src/cubescript.cc +++ b/src/cubescript.cc @@ -421,7 +421,6 @@ LIBCUBESCRIPT_EXPORT void cs_state::destroy() { } p_state->destroy(i->p_impl); } - p_state->destroy(p_state->strman); p_state->destroy(static_cast(p_errbuf)); p_state->destroy(p_state); }