simplify alias_stack push/pop a bit

master
Daniel Kolesa 2021-04-02 23:50:35 +02:00
parent 52b305954f
commit 95e7ae320c
3 changed files with 24 additions and 30 deletions

View File

@ -134,13 +134,20 @@ bool ident_is_used_arg(ident *id, thread_state &ts) {
return ts.callstack->usedargs[id->get_index()]; return ts.callstack->usedargs[id->get_index()];
} }
void alias_stack::push(ident_stack &st) {
st.next = node;
node = &st;
}
void alias_stack::pop() {
node = node->next;
}
void alias_stack::set_arg(alias *a, thread_state &ts, any_value &v) { void alias_stack::set_arg(alias *a, thread_state &ts, any_value &v) {
if (ident_is_used_arg(a, ts)) { if (ident_is_used_arg(a, ts)) {
node->code = bcode_ref{}; node->code = bcode_ref{};
} else { } else {
auto &st = ts.idstack.emplace_back(*ts.pstate); push(ts.idstack.emplace_back(*ts.pstate));
st.next = node;
node = &st;
ts.callstack->usedargs[a->get_index()] = true; ts.callstack->usedargs[a->get_index()] = true;
} }
node->val_s = std::move(v); node->val_s = std::move(v);
@ -356,17 +363,14 @@ LIBCUBESCRIPT_EXPORT alias_local::alias_local(state &cs, ident *a) {
auto &ts = *cs.thread_pointer(); auto &ts = *cs.thread_pointer();
p_alias = static_cast<alias *>(a); p_alias = static_cast<alias *>(a);
auto &ast = ts.get_astack(p_alias); auto &ast = ts.get_astack(p_alias);
auto &st = ts.idstack.emplace_back(cs); ast.push(ts.idstack.emplace_back(cs));
st.next = ast.node;
ast.node = &st;
p_sp = &ast; p_sp = &ast;
static_cast<alias_impl *>(p_alias)->p_flags &= ~IDENT_FLAG_UNKNOWN; static_cast<alias_impl *>(p_alias)->p_flags &= ~IDENT_FLAG_UNKNOWN;
} }
LIBCUBESCRIPT_EXPORT alias_local::~alias_local() { LIBCUBESCRIPT_EXPORT alias_local::~alias_local() {
if (p_alias) { if (p_alias) {
auto &st = *static_cast<alias_stack *>(p_sp); static_cast<alias_stack *>(p_sp)->pop();
st.node = st.node->next;
} }
} }

View File

@ -26,6 +26,9 @@ struct ident_stack {
struct alias_stack { struct alias_stack {
ident_stack *node; ident_stack *node;
void push(ident_stack &st);
void pop();
void set_arg(alias *a, thread_state &ts, any_value &v); void set_arg(alias *a, thread_state &ts, any_value &v);
void set_alias(alias *a, thread_state &ts, any_value &v); void set_alias(alias *a, thread_state &ts, any_value &v);
}; };

View File

@ -12,18 +12,14 @@ namespace cubescript {
static inline void push_alias(thread_state &ts, ident *id, ident_stack &st) { static inline void push_alias(thread_state &ts, ident *id, ident_stack &st) {
if (id->is_alias() && !(id->get_flags() & IDENT_FLAG_ARG)) { if (id->is_alias() && !(id->get_flags() & IDENT_FLAG_ARG)) {
auto *aimp = static_cast<alias_impl *>(id); auto *aimp = static_cast<alias_impl *>(id);
auto &ast = ts.get_astack(aimp); ts.get_astack(aimp).push(st);
st.next = ast.node;
ast.node = &st;
aimp->p_flags &= ~IDENT_FLAG_UNKNOWN; aimp->p_flags &= ~IDENT_FLAG_UNKNOWN;
} }
} }
static inline void pop_alias(thread_state &ts, ident *id) { static inline void pop_alias(thread_state &ts, ident *id) {
if (id->is_alias() && !(id->get_flags() & IDENT_FLAG_ARG)) { if (id->is_alias() && !(id->get_flags() & IDENT_FLAG_ARG)) {
auto *aimp = static_cast<alias_impl *>(id); ts.get_astack(static_cast<alias *>(id)).pop();
auto &ast = ts.get_astack(aimp);
ast.node = ast.node->next;
} }
} }
@ -224,8 +220,7 @@ bool exec_alias(
static_cast<alias *>(ts.istate->identmap[i]) static_cast<alias *>(ts.istate->identmap[i])
); );
auto &st = ts.idstack.emplace_back(*ts.pstate); auto &st = ts.idstack.emplace_back(*ts.pstate);
st.next = ast.node; ast.push(st);
ast.node = &st;
st.val_s = std::move(args[offset + i]); st.val_s = std::move(args[offset + i]);
uargs[i] = true; uargs[i] = true;
} }
@ -250,18 +245,16 @@ bool exec_alias(
ts.pstate->identflags = oldflags; ts.pstate->identflags = oldflags;
auto amask = aliaslink.usedargs; auto amask = aliaslink.usedargs;
for (std::size_t i = 0; i < callargs; i++) { for (std::size_t i = 0; i < callargs; i++) {
auto &ast = ts.get_astack( ts.get_astack(
static_cast<alias *>(ts.istate->identmap[i]) static_cast<alias *>(ts.istate->identmap[i])
); ).pop();
ast.node = ast.node->next;
amask[i] = false; amask[i] = false;
} }
for (; amask.any(); ++callargs) { for (; amask.any(); ++callargs) {
if (amask[callargs]) { if (amask[callargs]) {
auto &ast = ts.get_astack( ts.get_astack(
static_cast<alias *>(ts.istate->identmap[callargs]) static_cast<alias *>(ts.istate->identmap[callargs])
); ).pop();
ast.node = ast.node->next;
amask[callargs] = false; amask[callargs] = false;
} }
} }
@ -763,10 +756,7 @@ std::uint32_t *vm_exec(
(a->get_flags() & IDENT_FLAG_ARG) && (a->get_flags() & IDENT_FLAG_ARG) &&
!ident_is_used_arg(a, ts) !ident_is_used_arg(a, ts)
) { ) {
auto &ast = ts.get_astack(a); ts.get_astack(a).push(ts.idstack.emplace_back(*ts.pstate));
auto &st = ts.idstack.emplace_back(*ts.pstate);
st.next = ast.node;
ast.node = &st;
ts.callstack->usedargs[a->get_index()] = true; ts.callstack->usedargs[a->get_index()] = true;
} }
args.emplace_back(cs).set_ident(a); args.emplace_back(cs).set_ident(a);
@ -783,10 +773,7 @@ std::uint32_t *vm_exec(
!ident_is_used_arg(id, ts) !ident_is_used_arg(id, ts)
) { ) {
auto *a = static_cast<alias *>(id); auto *a = static_cast<alias *>(id);
auto &ast = ts.get_astack(a); ts.get_astack(a).push(ts.idstack.emplace_back(*ts.pstate));
auto &st = ts.idstack.emplace_back(*ts.pstate);
st.next = ast.node;
ast.node = &st;
ts.callstack->usedargs[id->get_index()] = true; ts.callstack->usedargs[id->get_index()] = true;
} }
arg.set_ident(id); arg.set_ident(id);