move some code around

master
Daniel Kolesa 2016-08-31 19:54:08 +01:00
parent 6494870a54
commit 5cfec4ac4e
4 changed files with 83 additions and 89 deletions

View File

@ -1694,14 +1694,25 @@ void GenState::gen_main(ostd::ConstCharRange s, int ret_type) {
code.push(CODE_EXIT | ((ret_type < VAL_ANY) ? (ret_type << CODE_RET) : 0));
}
ostd::Uint32 *compilecode(CsState &cs, ostd::ConstCharRange str) {
GenState gs(cs);
gs.code.reserve(64);
gs.gen_main(str);
ostd::Uint32 *code = new ostd::Uint32[gs.code.size()];
memcpy(code, gs.code.data(), gs.code.size() * sizeof(ostd::Uint32));
bcode_incr(code);
return code;
void CsAlias::clean_code() {
ostd::Uint32 *bcode = reinterpret_cast<ostd::Uint32 *>(p_acode);
if (bcode) {
bcode_decr(bcode);
p_acode = nullptr;
}
}
CsBytecode *CsAlias::compile_code(CsState &cs) {
if (!p_acode) {
GenState gs(cs);
gs.code.reserve(64);
gs.gen_main(p_val.get_str());
ostd::Uint32 *code = new ostd::Uint32[gs.code.size()];
memcpy(code, gs.code.data(), gs.code.size() * sizeof(ostd::Uint32));
bcode_incr(code);
p_acode = reinterpret_cast<CsBytecode *>(code);
}
return p_acode;
}
} /* namespace cscript */

View File

@ -6,6 +6,70 @@
namespace cscript {
void CsAlias::push_arg(CsValue const &v, CsIdentStack &st, bool um) {
if (p_astack == &st) {
/* prevent cycles and unnecessary code elsewhere */
p_val.cleanup();
p_val = v;
clean_code();
return;
}
st.val_s = p_val;
st.next = p_astack;
p_astack = &st;
p_val = v;
clean_code();
if (um) {
p_flags &= ~IDF_UNKNOWN;
}
}
void CsAlias::pop_arg() {
if (!p_astack) {
return;
}
CsIdentStack *st = p_astack;
p_val.cleanup();
p_val = p_astack->val_s;
clean_code();
p_astack = st->next;
}
void CsAlias::undo_arg(CsIdentStack &st) {
CsIdentStack *prev = p_astack;
st.val_s = p_val;
st.next = prev;
p_astack = prev->next;
p_val = prev->val_s;
clean_code();
}
void CsAlias::redo_arg(CsIdentStack const &st) {
CsIdentStack *prev = st.next;
prev->val_s = p_val;
p_astack = prev;
p_val = st.val_s;
clean_code();
}
void CsAlias::set_arg(CsState &cs, CsValue &v) {
if (cs.p_stack->usedargs & (1 << get_index())) {
p_val.cleanup();
p_val = v;
clean_code();
} else {
push_arg(v, cs.p_stack->argstack[get_index()], false);
cs.p_stack->usedargs |= 1 << get_index();
}
}
void CsAlias::set_alias(CsState &cs, CsValue &v) {
p_val.cleanup();
p_val = v;
clean_code();
p_flags = (p_flags & cs.identflags) | cs.identflags;
}
static inline bool cs_has_cmd_cb(CsIdent *id) {
if (!id->is_command() && !id->is_special()) {
return false;

View File

@ -150,8 +150,6 @@ void cs_debug_code_line(
cs_debug_alias(cs);
}
ostd::Uint32 *compilecode(CsState &cs, ostd::ConstCharRange str);
struct GenState {
CsState &cs;
CsVector<ostd::Uint32> code;

View File

@ -819,85 +819,6 @@ void CsAlias::get_cval(CsValue &v) const {
}
}
void CsAlias::clean_code() {
ostd::Uint32 *bcode = reinterpret_cast<ostd::Uint32 *>(p_acode);
if (bcode) {
bcode_decr(bcode);
p_acode = nullptr;
}
}
CsBytecode *CsAlias::compile_code(CsState &cs) {
if (!p_acode) {
p_acode = reinterpret_cast<CsBytecode *>(compilecode(cs, p_val.get_str()));
}
return p_acode;
}
void CsAlias::push_arg(CsValue const &v, CsIdentStack &st, bool um) {
if (p_astack == &st) {
/* prevent cycles and unnecessary code elsewhere */
p_val.cleanup();
p_val = v;
clean_code();
return;
}
st.val_s = p_val;
st.next = p_astack;
p_astack = &st;
p_val = v;
clean_code();
if (um) {
p_flags &= ~IDF_UNKNOWN;
}
}
void CsAlias::pop_arg() {
if (!p_astack) {
return;
}
CsIdentStack *st = p_astack;
p_val.cleanup();
p_val = p_astack->val_s;
clean_code();
p_astack = st->next;
}
void CsAlias::undo_arg(CsIdentStack &st) {
CsIdentStack *prev = p_astack;
st.val_s = p_val;
st.next = prev;
p_astack = prev->next;
p_val = prev->val_s;
clean_code();
}
void CsAlias::redo_arg(CsIdentStack const &st) {
CsIdentStack *prev = st.next;
prev->val_s = p_val;
p_astack = prev;
p_val = st.val_s;
clean_code();
}
void CsAlias::set_arg(CsState &cs, CsValue &v) {
if (cs.p_stack->usedargs & (1 << get_index())) {
p_val.cleanup();
p_val = v;
clean_code();
} else {
push_arg(v, cs.p_stack->argstack[get_index()], false);
cs.p_stack->usedargs |= 1 << get_index();
}
}
void CsAlias::set_alias(CsState &cs, CsValue &v) {
p_val.cleanup();
p_val = v;
clean_code();
p_flags = (p_flags & cs.identflags) | cs.identflags;
}
CsIdentType CsIdent::get_type() const {
if (p_type > ID_ALIAS) {
return CsIdentType::special;