From 95e7ae320c4bd35456021f55234abbe21be7ab57 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 2 Apr 2021 23:50:35 +0200 Subject: [PATCH] simplify alias_stack push/pop a bit --- src/cs_ident.cc | 20 ++++++++++++-------- src/cs_ident.hh | 3 +++ src/cs_vm.cc | 31 +++++++++---------------------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/cs_ident.cc b/src/cs_ident.cc index 3dba1a1..3eb0508 100644 --- a/src/cs_ident.cc +++ b/src/cs_ident.cc @@ -134,13 +134,20 @@ bool ident_is_used_arg(ident *id, thread_state &ts) { 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) { if (ident_is_used_arg(a, ts)) { node->code = bcode_ref{}; } else { - auto &st = ts.idstack.emplace_back(*ts.pstate); - st.next = node; - node = &st; + push(ts.idstack.emplace_back(*ts.pstate)); ts.callstack->usedargs[a->get_index()] = true; } 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(); p_alias = static_cast(a); auto &ast = ts.get_astack(p_alias); - auto &st = ts.idstack.emplace_back(cs); - st.next = ast.node; - ast.node = &st; + ast.push(ts.idstack.emplace_back(cs)); p_sp = * static_cast(p_alias)->p_flags &= ~IDENT_FLAG_UNKNOWN; } LIBCUBESCRIPT_EXPORT alias_local::~alias_local() { if (p_alias) { - auto &st = *static_cast(p_sp); - st.node = st.node->next; + static_cast(p_sp)->pop(); } } diff --git a/src/cs_ident.hh b/src/cs_ident.hh index 3223c7e..d2545f7 100644 --- a/src/cs_ident.hh +++ b/src/cs_ident.hh @@ -26,6 +26,9 @@ struct ident_stack { struct alias_stack { ident_stack *node; + void push(ident_stack &st); + void pop(); + void set_arg(alias *a, thread_state &ts, any_value &v); void set_alias(alias *a, thread_state &ts, any_value &v); }; diff --git a/src/cs_vm.cc b/src/cs_vm.cc index 58ed22e..1560e92 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -12,18 +12,14 @@ namespace cubescript { static inline void push_alias(thread_state &ts, ident *id, ident_stack &st) { if (id->is_alias() && !(id->get_flags() & IDENT_FLAG_ARG)) { auto *aimp = static_cast(id); - auto &ast = ts.get_astack(aimp); - st.next = ast.node; - ast.node = &st; + ts.get_astack(aimp).push(st); aimp->p_flags &= ~IDENT_FLAG_UNKNOWN; } } static inline void pop_alias(thread_state &ts, ident *id) { if (id->is_alias() && !(id->get_flags() & IDENT_FLAG_ARG)) { - auto *aimp = static_cast(id); - auto &ast = ts.get_astack(aimp); - ast.node = ast.node->next; + ts.get_astack(static_cast(id)).pop(); } } @@ -224,8 +220,7 @@ bool exec_alias( static_cast(ts.istate->identmap[i]) ); auto &st = ts.idstack.emplace_back(*ts.pstate); - st.next = ast.node; - ast.node = &st; + ast.push(st); st.val_s = std::move(args[offset + i]); uargs[i] = true; } @@ -250,18 +245,16 @@ bool exec_alias( ts.pstate->identflags = oldflags; auto amask = aliaslink.usedargs; for (std::size_t i = 0; i < callargs; i++) { - auto &ast = ts.get_astack( + ts.get_astack( static_cast(ts.istate->identmap[i]) - ); - ast.node = ast.node->next; + ).pop(); amask[i] = false; } for (; amask.any(); ++callargs) { if (amask[callargs]) { - auto &ast = ts.get_astack( + ts.get_astack( static_cast(ts.istate->identmap[callargs]) - ); - ast.node = ast.node->next; + ).pop(); amask[callargs] = false; } } @@ -763,10 +756,7 @@ std::uint32_t *vm_exec( (a->get_flags() & IDENT_FLAG_ARG) && !ident_is_used_arg(a, ts) ) { - auto &ast = ts.get_astack(a); - auto &st = ts.idstack.emplace_back(*ts.pstate); - st.next = ast.node; - ast.node = &st; + ts.get_astack(a).push(ts.idstack.emplace_back(*ts.pstate)); ts.callstack->usedargs[a->get_index()] = true; } args.emplace_back(cs).set_ident(a); @@ -783,10 +773,7 @@ std::uint32_t *vm_exec( !ident_is_used_arg(id, ts) ) { auto *a = static_cast(id); - auto &ast = ts.get_astack(a); - auto &st = ts.idstack.emplace_back(*ts.pstate); - st.next = ast.node; - ast.node = &st; + ts.get_astack(a).push(ts.idstack.emplace_back(*ts.pstate)); ts.callstack->usedargs[id->get_index()] = true; } arg.set_ident(id);