per-thread alias stack (the vm should be mostly resumable now)

master
Daniel Kolesa 2021-04-02 19:12:46 +02:00
parent 881ba4bce9
commit 52b305954f
4 changed files with 18 additions and 16 deletions

View File

@ -71,8 +71,7 @@ svar_impl::svar_impl(
alias_impl::alias_impl( alias_impl::alias_impl(
state &cs, string_ref name, string_ref a, int fl state &cs, string_ref name, string_ref a, int fl
): ):
ident_impl{ident_type::ALIAS, name, fl}, ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs}
p_initial{cs}, p_astack{&p_initial}
{ {
p_initial.val_s.set_str(a); p_initial.val_s.set_str(a);
} }
@ -80,36 +79,31 @@ alias_impl::alias_impl(
alias_impl::alias_impl( alias_impl::alias_impl(
state &cs, string_ref name, std::string_view a, int fl state &cs, string_ref name, std::string_view a, int fl
): ):
ident_impl{ident_type::ALIAS, name, fl}, ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs}
p_initial{cs}, p_astack{&p_initial}
{ {
p_initial.val_s.set_str(a); p_initial.val_s.set_str(a);
} }
alias_impl::alias_impl(state &cs, string_ref name, integer_type a, int fl): alias_impl::alias_impl(state &cs, string_ref name, integer_type a, int fl):
ident_impl{ident_type::ALIAS, name, fl}, ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs}
p_initial{cs}, p_astack{&p_initial}
{ {
p_initial.val_s.set_int(a); p_initial.val_s.set_int(a);
} }
alias_impl::alias_impl(state &cs, string_ref name, float_type a, int fl): alias_impl::alias_impl(state &cs, string_ref name, float_type a, int fl):
ident_impl{ident_type::ALIAS, name, fl}, ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs}
p_initial{cs}, p_astack{&p_initial}
{ {
p_initial.val_s.set_float(a); p_initial.val_s.set_float(a);
} }
alias_impl::alias_impl(state &cs, string_ref name, int fl): alias_impl::alias_impl(state &cs, string_ref name, int fl):
ident_impl{ident_type::ALIAS, name, fl}, ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs}
p_initial{cs}, p_astack{&p_initial}
{ {
p_initial.val_s.set_none(); p_initial.val_s.set_none();
} }
alias_impl::alias_impl(state &cs, string_ref name, any_value v, int fl): alias_impl::alias_impl(state &cs, string_ref name, any_value v, int fl):
ident_impl{ident_type::ALIAS, name, fl}, ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs}
p_initial{cs}, p_astack{&p_initial}
{ {
p_initial.val_s = v; p_initial.val_s = v;
} }

View File

@ -104,7 +104,6 @@ struct alias_impl: ident_impl, alias {
alias_impl(state &cs, string_ref n, any_value v, int flags); alias_impl(state &cs, string_ref n, any_value v, int flags);
ident_stack p_initial; ident_stack p_initial;
alias_stack p_astack;
}; };
struct command_impl: ident_impl, command { struct command_impl: ident_impl, command {

View File

@ -5,7 +5,7 @@
namespace cubescript { namespace cubescript {
thread_state::thread_state(internal_state *cs): thread_state::thread_state(internal_state *cs):
vmstack{cs}, idstack{cs}, errbuf{cs} vmstack{cs}, idstack{cs}, astacks{cs}, errbuf{cs}
{ {
vmstack.reserve(32); vmstack.reserve(32);
idstack.reserve(MAX_ARGUMENTS); idstack.reserve(MAX_ARGUMENTS);
@ -18,7 +18,11 @@ hook_func thread_state::set_hook(hook_func f) {
} }
alias_stack &thread_state::get_astack(alias *a) { alias_stack &thread_state::get_astack(alias *a) {
return static_cast<alias_impl *>(a)->p_astack; auto it = astacks.try_emplace(a->get_index());
if (it.second) {
it.first->second.node = &static_cast<alias_impl *>(a)->p_initial;
}
return it.first->second;
} }
} /* namespace cubescript */ } /* namespace cubescript */

View File

@ -14,6 +14,7 @@ namespace cubescript {
struct codegen_state; struct codegen_state;
struct thread_state { struct thread_state {
using astack_allocator = std_allocator<std::pair<int const, alias_stack>>;
/* thread call stack */ /* thread call stack */
ident_link *callstack{}; ident_link *callstack{};
/* the shared state pointer */ /* the shared state pointer */
@ -24,8 +25,12 @@ struct thread_state {
codegen_state *cstate{}; codegen_state *cstate{};
/* VM stack */ /* VM stack */
valbuf<any_value> vmstack; valbuf<any_value> vmstack;
/* alias stack */ /* ident stack */
valbuf<ident_stack> idstack; valbuf<ident_stack> idstack;
/* per-alias stack pointer */
std::unordered_map<
int, alias_stack, std::hash<int>, std::equal_to<int>, astack_allocator
> astacks;
/* per-thread storage buffer for error messages */ /* per-thread storage buffer for error messages */
charbuf errbuf; charbuf errbuf;
/* we can attach a hook to vm events */ /* we can attach a hook to vm events */