diff --git a/cs_vm.cc b/cs_vm.cc index d95356a4..4240a762 100644 --- a/cs_vm.cc +++ b/cs_vm.cc @@ -61,7 +61,8 @@ ostd::ConstCharRange cs_debug_line( } void cs_debug_alias(CsState &cs) { - if (!cs.dbgalias->get_value()) { + Ivar *dalias = static_cast(cs.identmap[DbgaliasIdx]); + if (!dalias->get_value()) { return; } int total = 0, depth = 0; @@ -71,11 +72,11 @@ void cs_debug_alias(CsState &cs) { for (IdentLink *l = cs.p_stack; l != &cs.noalias; l = l->next) { Ident *id = l->id; ++depth; - if (depth < cs.dbgalias->get_value()) { + if (depth < dalias->get_value()) { ostd::err.writefln(" %d) %s", total - depth + 1, id->get_name()); } else if (l->next == &cs.noalias) { ostd::err.writefln( - depth == cs.dbgalias->get_value() ? " %d) %s" : " ..%d) %s", + depth == dalias->get_value() ? " %d) %s" : " ..%d) %s", total - depth + 1, id->get_name() ); } @@ -388,7 +389,7 @@ static inline void callcommand( if (rep) { break; } - args[i].set_ident(cs.dummy); + args[i].set_ident(cs.identmap[DummyIdx]); fakeargs++; } else { cs.force_ident(args[i]); @@ -449,14 +450,15 @@ static inline void cs_call_alias( CsState &cs, Alias *a, CsValue *args, CsValue &result, int callargs, int &nargs, int offset, int skip, ostd::Uint32 op ) { + Ivar *anargs = static_cast(cs.identmap[NumargsIdx]); IdentStack argstack[MaxArguments]; for(int i = 0; i < callargs; i++) { static_cast(cs.identmap[i])->push_arg( args[offset + i], argstack[i], false ); } - int oldargs = cs.numargs->get_value(); - cs.numargs->set_value(callargs); + int oldargs = anargs->get_value(); + anargs->set_value(callargs); int oldflags = cs.identflags; cs.identflags |= a->get_flags()&IDF_OVERRIDDEN; IdentLink aliaslink = { @@ -480,7 +482,7 @@ static inline void cs_call_alias( } } force_arg(result, op & CODE_RET_MASK); - cs.numargs->set_value(oldargs); + anargs->set_value(oldargs); nargs = offset - skip; } @@ -946,7 +948,7 @@ static ostd::Uint32 const *runcode( } case CODE_IDENTU: { CsValue &arg = args[numargs - 1]; - Ident *id = cs.dummy; + Ident *id = cs.identmap[DummyIdx]; if ( arg.get_type() == VAL_STR || arg.get_type() == VAL_MACRO || diff --git a/cs_vm.hh b/cs_vm.hh index 0da3aeef..6c7b2aa5 100644 --- a/cs_vm.hh +++ b/cs_vm.hh @@ -13,6 +13,10 @@ namespace cscript { static constexpr int MaxArguments = 25; static constexpr int MaxResults = 7; +static constexpr int DummyIdx = MaxArguments; +static constexpr int NumargsIdx = MaxArguments + 1; +static constexpr int DbgaliasIdx = MaxArguments + 2; + enum { ID_UNKNOWN = -1, ID_IVAR, ID_FVAR, ID_SVAR, ID_COMMAND, ID_ALIAS, ID_LOCAL, ID_DO, ID_DOARGS, ID_IF, ID_RESULT, ID_NOT, ID_AND, ID_OR @@ -213,7 +217,7 @@ struct GenState { } void gen_ident() { - gen_ident(cs.dummy); + gen_ident(cs.identmap[DummyIdx]); } void gen_ident(ostd::ConstCharRange word) { diff --git a/cubescript.cc b/cubescript.cc index 3d3f7e64..96df3a1d 100644 --- a/cubescript.cc +++ b/cubescript.cc @@ -258,9 +258,15 @@ CsState::CsState() { snprintf(buf, sizeof(buf), "arg%d", i + 1); new_ident(static_cast(buf), IDF_ARG); } - dummy = new_ident("//dummy"); - numargs = add_ident("numargs", MaxArguments, 0, 0); - dbgalias = add_ident("dbgalias", 0, 1000, 4); + Ident *id = new_ident("//dummy"); + assert(id->get_index() == DummyIdx); + + id = add_ident("numargs", MaxArguments, 0, 0); + assert(id->get_index() == NumargsIdx); + + id = add_ident("dbgalias", 0, 1000, 4); + assert(id->get_index() == DbgaliasIdx); + cs_init_lib_base(*this); } @@ -336,7 +342,7 @@ Ident *CsState::new_ident(ostd::ConstCharRange name, int flags) { cs_debug_code( *this, "number %s is not a valid identifier name", name ); - return dummy; + return identmap[DummyIdx]; } id = add_ident(name, flags); } @@ -361,8 +367,8 @@ Ident *CsState::force_ident(CsValue &v) { } } v.cleanup(); - v.set_ident(dummy); - return dummy; + v.set_ident(identmap[DummyIdx]); + return identmap[DummyIdx]; } bool CsState::reset_var(ostd::ConstCharRange name) { diff --git a/cubescript.hh b/cubescript.hh index b91821ba..abdfb936 100644 --- a/cubescript.hh +++ b/cubescript.hh @@ -377,8 +377,6 @@ struct OSTD_EXPORT CsState { CsMap idents; CsVector identmap; - Ident *dummy = nullptr; - IdentLink noalias; IdentLink *p_stack = &noalias; @@ -387,8 +385,6 @@ struct OSTD_EXPORT CsState { int identflags = 0; int nodebug = 0; - Ivar *numargs = nullptr; - Ivar *dbgalias = nullptr; CsState(); ~CsState();