diff --git a/src/cs_bcode.cc b/src/cs_bcode.cc index 26bae41..6f474da 100644 --- a/src/cs_bcode.cc +++ b/src/cs_bcode.cc @@ -1,6 +1,5 @@ #include "cs_bcode.hh" #include "cs_state.hh" -#include "cs_thread.hh" namespace cubescript { @@ -44,12 +43,12 @@ struct bcode_hdr { }; /* returned address is the 'init' member of the header */ -std::uint32_t *bcode_alloc(state &cs, std::size_t sz) { - auto a = std_allocator{cs.p_tstate->istate}; +std::uint32_t *bcode_alloc(internal_state *cs, std::size_t sz) { + auto a = std_allocator{cs}; std::size_t hdrs = sizeof(bcode_hdr) / sizeof(std::uint32_t); auto p = a.allocate(sz + hdrs - 1); bcode_hdr *hdr = reinterpret_cast(p); - hdr->cs = cs.p_tstate->istate; + hdr->cs = cs; hdr->asize = sz + hdrs - 1; return p + hdrs - 1; } diff --git a/src/cs_bcode.hh b/src/cs_bcode.hh index 81b86ad..b7bef4b 100644 --- a/src/cs_bcode.hh +++ b/src/cs_bcode.hh @@ -77,7 +77,7 @@ enum { BC_INST_FLAG_FALSE = 0 << BC_INST_RET }; -std::uint32_t *bcode_alloc(state &cs, std::size_t sz); +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); diff --git a/src/cs_ident.cc b/src/cs_ident.cc index e9d4953..2fe6b1f 100644 --- a/src/cs_ident.cc +++ b/src/cs_ident.cc @@ -180,7 +180,7 @@ bcode *alias_impl::compile_code(state &cs) { gs.code.reserve(64); gs.gen_main(get_value().get_str()); /* i wish i could steal the memory somehow */ - uint32_t *code = bcode_alloc(cs, gs.code.size()); + uint32_t *code = bcode_alloc(cs.p_tstate->istate, gs.code.size()); memcpy(code, gs.code.data(), gs.code.size() * sizeof(uint32_t)); bcode_incr(code); p_acode = reinterpret_cast(code); diff --git a/src/cs_val.cc b/src/cs_val.cc index 0906803..c6bc7f0 100644 --- a/src/cs_val.cc +++ b/src/cs_val.cc @@ -260,7 +260,7 @@ bcode *any_value::force_code(state &cs) { gs.code.reserve(64); gs.gen_main(get_str()); gs.done(); - uint32_t *cbuf = bcode_alloc(cs, gs.code.size()); + uint32_t *cbuf = bcode_alloc(cs.p_tstate->istate, gs.code.size()); std::memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t)); auto *bc = reinterpret_cast(cbuf + 1); set_code(bc); diff --git a/src/cs_vm.cc b/src/cs_vm.cc index a12fa2b..4100ac3 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -308,33 +308,35 @@ struct run_depth_guard { ~run_depth_guard() { --rundepth; } }; -static inline alias *get_lookup_id(state &cs, std::uint32_t op) { - ident *id = cs.p_tstate->istate->identmap[op >> 8]; +static inline alias *get_lookup_id(thread_state &ts, std::uint32_t op) { + ident *id = ts.istate->identmap[op >> 8]; if (id->get_flags() & IDENT_FLAG_UNKNOWN) { - throw error(cs, "unknown alias lookup: %s", id->get_name().data()); + throw error{ + *ts.pstate, "unknown alias lookup: %s", id->get_name().data() + }; } return static_cast(id); } -static inline alias *get_lookuparg_id(state &cs, std::uint32_t op) { - ident *id = cs.p_tstate->istate->identmap[op >> 8]; - if (!ident_is_used_arg(id, cs)) { +static inline alias *get_lookuparg_id(thread_state &ts, std::uint32_t op) { + ident *id = ts.istate->identmap[op >> 8]; + if (!ident_is_used_arg(id, *ts.pstate)) { return nullptr; } return static_cast(id); } struct stack_guard { - state *csp; + thread_state *tsp; std::size_t oldtop; stack_guard() = delete; - stack_guard(state &cs): - csp{&cs}, oldtop{cs.p_tstate->vmstack.size()} + stack_guard(thread_state &ts): + tsp{&ts}, oldtop{ts.vmstack.size()} {} ~stack_guard() { - csp->p_tstate->vmstack.resize(oldtop, any_value{*csp}); + tsp->vmstack.resize(oldtop, any_value{*tsp->pstate}); } stack_guard(stack_guard const &) = delete; @@ -369,7 +371,7 @@ static inline int get_lookupu_type( return ID_FVAR; case ident_type::COMMAND: { /* make sure value stack gets restored */ - stack_guard s{*ts.pstate}; + stack_guard s{ts}; auto *cimpl = static_cast(id); auto &args = ts.vmstack; auto osz = args.size(); @@ -393,7 +395,7 @@ static std::uint32_t *runcode( result.set_none(); auto &cs = *ts.pstate; run_depth_guard level{cs}; /* incr and decr on scope exit */ - stack_guard guard{cs}; /* resize back to original */ + stack_guard guard{ts}; /* resize back to original */ auto &args = ts.vmstack; auto &chook = cs.get_call_hook(); if (chook) { @@ -734,7 +736,7 @@ static std::uint32_t *runcode( break; } gs.done(); - std::uint32_t *cbuf = bcode_alloc(gs.cs, gs.code.size()); + std::uint32_t *cbuf = bcode_alloc(ts.istate, gs.code.size()); std::memcpy( cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t) @@ -755,7 +757,7 @@ static std::uint32_t *runcode( gs.gen_main(s); gs.done(); std::uint32_t *cbuf = bcode_alloc( - gs.cs, gs.code.size() + ts.istate, gs.code.size() ); std::memcpy( cbuf, gs.code.data(), @@ -837,12 +839,12 @@ static std::uint32_t *runcode( } case BC_INST_LOOKUP | BC_RET_STRING: { auto &v = args.emplace_back(cs); - v = get_lookup_id(cs, op)->get_value(); + v = get_lookup_id(ts, op)->get_value(); v.force_str(); continue; } case BC_INST_LOOKUP_ARG | BC_RET_STRING: { - alias *a = get_lookuparg_id(cs, op); + alias *a = get_lookuparg_id(ts, op); if (!a) { args.emplace_back(cs).set_str(""); } else { @@ -883,11 +885,11 @@ static std::uint32_t *runcode( } case BC_INST_LOOKUP | BC_RET_INT: args.emplace_back(cs).set_int( - get_lookup_id(cs, op)->get_value().get_int() + get_lookup_id(ts, op)->get_value().get_int() ); continue; case BC_INST_LOOKUP_ARG | BC_RET_INT: { - alias *a = get_lookuparg_id(cs, op); + alias *a = get_lookuparg_id(ts, op); if (!a) { args.emplace_back(cs).set_int(0); } else { @@ -928,11 +930,11 @@ static std::uint32_t *runcode( } case BC_INST_LOOKUP | BC_RET_FLOAT: args.emplace_back(cs).set_float( - get_lookup_id(cs, op)->get_value().get_float() + get_lookup_id(ts, op)->get_value().get_float() ); continue; case BC_INST_LOOKUP_ARG | BC_RET_FLOAT: { - alias *a = get_lookuparg_id(cs, op); + alias *a = get_lookuparg_id(ts, op); if (!a) { args.emplace_back(cs).set_float(float_type(0)); } else { @@ -966,12 +968,12 @@ static std::uint32_t *runcode( } } case BC_INST_LOOKUP | BC_RET_NULL: - get_lookup_id(cs, op)->get_value().get_val( + get_lookup_id(ts, op)->get_value().get_val( args.emplace_back(cs) ); continue; case BC_INST_LOOKUP_ARG | BC_RET_NULL: { - alias *a = get_lookuparg_id(cs, op); + alias *a = get_lookuparg_id(ts, op); if (!a) { args.emplace_back(cs).set_none(); } else { @@ -1008,12 +1010,12 @@ static std::uint32_t *runcode( } case BC_INST_LOOKUP_M | BC_RET_STRING: { auto &v = args.emplace_back(cs); - v = get_lookup_id(cs, op)->get_value(); + v = get_lookup_id(ts, op)->get_value(); v.force_str(); continue; } case BC_INST_LOOKUP_MARG | BC_RET_STRING: { - alias *a = get_lookuparg_id(cs, op); + alias *a = get_lookuparg_id(ts, op); if (!a) { args.emplace_back(cs).set_str(""); } else { @@ -1047,10 +1049,10 @@ static std::uint32_t *runcode( } } case BC_INST_LOOKUP_M | BC_RET_NULL: - get_lookup_id(cs, op)->get_cval(args.emplace_back(cs)); + get_lookup_id(ts, op)->get_cval(args.emplace_back(cs)); continue; case BC_INST_LOOKUP_MARG | BC_RET_NULL: { - alias *a = get_lookuparg_id(cs, op); + alias *a = get_lookuparg_id(ts, op); if (!a) { args.emplace_back(cs).set_none(); } else { @@ -1456,7 +1458,7 @@ static void do_run( gs.code.reserve(64); gs.gen_main(code, VAL_ANY); gs.done(); - std::uint32_t *cbuf = bcode_alloc(gs.cs, gs.code.size()); + std::uint32_t *cbuf = bcode_alloc(cs.p_tstate->istate, gs.code.size()); std::memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t)); bcode_incr(cbuf); call_with_cleanup([&cs, cbuf, &ret]() { @@ -1490,7 +1492,7 @@ void state::run(ident *id, std::span args, any_value &ret) { case ident_type::COMMAND: { auto *cimpl = static_cast(id); if (nargs < std::size_t(cimpl->get_num_args())) { - stack_guard s{*this}; /* restore after call */ + stack_guard s{*p_tstate}; /* restore after call */ auto &targs = p_tstate->vmstack; auto osz = targs.size(); targs.resize(osz + cimpl->get_num_args(), any_value{*this});