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()];
}
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<alias *>(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 = &ast;
static_cast<alias_impl *>(p_alias)->p_flags &= ~IDENT_FLAG_UNKNOWN;
}
LIBCUBESCRIPT_EXPORT alias_local::~alias_local() {
if (p_alias) {
auto &st = *static_cast<alias_stack *>(p_sp);
st.node = st.node->next;
static_cast<alias_stack *>(p_sp)->pop();
}
}

View File

@ -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);
};

View File

@ -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<alias_impl *>(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<alias_impl *>(id);
auto &ast = ts.get_astack(aimp);
ast.node = ast.node->next;
ts.get_astack(static_cast<alias *>(id)).pop();
}
}
@ -224,8 +220,7 @@ bool exec_alias(
static_cast<alias *>(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<alias *>(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<alias *>(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<alias *>(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);