remove some more raw bcode usage

master
Daniel Kolesa 2021-04-01 05:18:20 +02:00
parent b2caade276
commit 2b0392e27e
6 changed files with 20 additions and 35 deletions

View File

@ -75,11 +75,11 @@ static inline void bcode_free(std::uint32_t *bc) {
std_allocator<std::uint32_t>{hdr->cs}.deallocate(rp, hdr->asize);
}
void bcode_incr(std::uint32_t *bc) {
static inline void bcode_incr(std::uint32_t *bc) {
*bc += 0x100;
}
void bcode_decr(std::uint32_t *bc) {
static inline void bcode_decr(std::uint32_t *bc) {
*bc -= 0x100;
if (std::int32_t(*bc) < 0x100) {
bcode_free(bc);

View File

@ -207,8 +207,6 @@ enum {
std::uint32_t *bcode_alloc(internal_state *cs, std::size_t sz);
void bcode_incr(std::uint32_t *code);
void bcode_decr(std::uint32_t *code);
void bcode_addref(std::uint32_t *code);
void bcode_unref(std::uint32_t *code);

View File

@ -72,7 +72,7 @@ alias_impl::alias_impl(
state &cs, string_ref name, string_ref a, int fl
):
ident_impl{ident_type::ALIAS, name, fl},
p_initial{cs}, p_acode{nullptr}, p_astack{&p_initial}
p_initial{cs}, p_acode{}, p_astack{&p_initial}
{
p_initial.val_s.set_str(a);
}
@ -81,35 +81,35 @@ alias_impl::alias_impl(
state &cs, string_ref name, std::string_view a, int fl
):
ident_impl{ident_type::ALIAS, name, fl},
p_initial{cs}, p_acode{nullptr}, p_astack{&p_initial}
p_initial{cs}, p_acode{}, p_astack{&p_initial}
{
p_initial.val_s.set_str(a);
}
alias_impl::alias_impl(state &cs, string_ref name, integer_type a, int fl):
ident_impl{ident_type::ALIAS, name, fl},
p_initial{cs}, p_acode{nullptr}, p_astack{&p_initial}
p_initial{cs}, p_acode{}, p_astack{&p_initial}
{
p_initial.val_s.set_int(a);
}
alias_impl::alias_impl(state &cs, string_ref name, float_type a, int fl):
ident_impl{ident_type::ALIAS, name, fl},
p_initial{cs}, p_acode{nullptr}, p_astack{&p_initial}
p_initial{cs}, p_acode{}, p_astack{&p_initial}
{
p_initial.val_s.set_float(a);
}
alias_impl::alias_impl(state &cs, string_ref name, int fl):
ident_impl{ident_type::ALIAS, name, fl},
p_initial{cs}, p_acode{nullptr}, p_astack{&p_initial}
p_initial{cs}, p_acode{}, p_astack{&p_initial}
{
p_initial.val_s.set_none();
}
alias_impl::alias_impl(state &cs, string_ref name, any_value v, int fl):
ident_impl{ident_type::ALIAS, name, fl},
p_initial{cs}, p_acode{nullptr}, p_astack{&p_initial}
p_initial{cs}, p_acode{}, p_astack{&p_initial}
{
p_initial.val_s = v;
}
@ -144,9 +144,6 @@ void alias_impl::redo_arg(ident_stack &st) {
p_astack = st.next;
clean_code();
p_acode = st.val_s.get_code();
if (p_acode) {
bcode_incr(p_acode->get_raw());
}
}
void alias_impl::set_arg(thread_state &ts, any_value &v) {
@ -166,13 +163,10 @@ void alias_impl::set_alias(thread_state &ts, any_value &v) {
}
void alias_impl::clean_code() {
if (p_acode) {
bcode_decr(p_acode->get_raw());
p_acode = nullptr;
}
p_acode = bcode_ref{};
}
bcode *alias_impl::compile_code(thread_state &ts) {
bcode_ref const &alias_impl::compile_code(thread_state &ts) {
if (!p_acode) {
codegen_state gs(ts);
gs.code.reserve(64);
@ -180,8 +174,7 @@ bcode *alias_impl::compile_code(thread_state &ts) {
/* i wish i could steal the memory somehow */
uint32_t *code = bcode_alloc(ts.istate, gs.code.size());
memcpy(code, gs.code.data(), gs.code.size() * sizeof(uint32_t));
bcode_incr(code);
p_acode = reinterpret_cast<bcode *>(code);
p_acode = bcode_ref{reinterpret_cast<bcode *>(code + 1)};
}
return p_acode;
}

View File

@ -103,10 +103,10 @@ struct alias_impl: ident_impl, alias {
void set_alias(thread_state &ts, any_value &v);
void clean_code();
bcode *compile_code(thread_state &ts);
bcode_ref const &compile_code(thread_state &ts);
ident_stack p_initial;
bcode *p_acode;
bcode_ref p_acode;
ident_stack *p_astack;
};

View File

@ -796,14 +796,9 @@ static void do_run(
gs.done();
std::uint32_t *cbuf = bcode_alloc(ts.istate, gs.code.size());
std::memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t));
bcode_incr(cbuf);
try {
vm_exec(ts, cbuf + 1, ret);
} catch (...) {
bcode_decr(cbuf);
throw;
}
bcode_decr(cbuf);
bcode_ref cref{reinterpret_cast<bcode *>(cbuf + 1)};
bcode *p = cref;
vm_exec(ts, p->get_raw(), ret);
}
LIBCUBESCRIPT_EXPORT void state::run(std::string_view code, any_value &ret) {

View File

@ -221,12 +221,10 @@ void exec_alias(
ts.pstate->identflags |= a->get_flags()&IDENT_FLAG_OVERRIDDEN;
ident_link aliaslink = {a, ts.callstack, uargs};
ts.callstack = &aliaslink;
std::uint32_t *codep = static_cast<
bcode_ref coderef = static_cast<
alias_impl *
>(a)->compile_code(ts)->get_raw();
bcode_incr(codep);
>(a)->compile_code(ts);
auto cleanup = [&]() {
bcode_decr(codep);
ts.callstack = aliaslink.next;
ts.pstate->identflags = oldflags;
auto amask = aliaslink.usedargs;
@ -248,7 +246,8 @@ void exec_alias(
nargs = offset - skip;
};
try {
vm_exec(ts, codep + 1, result);
bcode *p = coderef;
vm_exec(ts, p->get_raw(), result);
} catch (...) {
cleanup();
throw;