abstract away ugly bcode reference creation
parent
dcd5f61748
commit
c0fc1aa452
|
@ -4,12 +4,17 @@
|
|||
|
||||
#include "cs_gen.hh"
|
||||
|
||||
#include "cs_bcode.hh"
|
||||
#include "cs_ident.hh"
|
||||
#include "cs_parser.hh"
|
||||
|
||||
namespace cubescript {
|
||||
|
||||
bcode_ref gen_state::steal_ref() {
|
||||
auto *cp = bcode_alloc(ts.istate, code.size());
|
||||
std::memcpy(cp, code.data(), code.size() * sizeof(std::uint32_t));
|
||||
return bcode_ref{reinterpret_cast<bcode *>(cp + 1)};
|
||||
}
|
||||
|
||||
void gen_state::gen_val_null() {
|
||||
code.push_back(BC_INST_VAL_INT | BC_RET_NULL);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <cubescript/cubescript.hh>
|
||||
|
||||
#include "cs_std.hh"
|
||||
#include "cs_bcode.hh"
|
||||
#include "cs_thread.hh"
|
||||
|
||||
namespace cubescript {
|
||||
|
@ -21,6 +22,8 @@ struct gen_state {
|
|||
ts{tsr}, code{tsr.istate}
|
||||
{}
|
||||
|
||||
bcode_ref steal_ref();
|
||||
|
||||
void gen_val_null();
|
||||
|
||||
void gen_val_integer(integer_type v = 0);
|
||||
|
|
|
@ -661,9 +661,7 @@ static any_value do_run(
|
|||
ps.src_name = file;
|
||||
ps.gen_main(code, VAL_ANY);
|
||||
}
|
||||
std::uint32_t *cbuf = bcode_alloc(ts.istate, gs.code.size());
|
||||
std::memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t));
|
||||
bcode_ref cref{reinterpret_cast<bcode *>(cbuf + 1)};
|
||||
auto cref = gs.steal_ref();
|
||||
bcode *p = cref;
|
||||
vm_exec(ts, p->get_raw(), ret);
|
||||
return ret;
|
||||
|
|
|
@ -271,11 +271,9 @@ bcode_ref any_value::force_code(state &cs) {
|
|||
}
|
||||
gen_state gs{state_p{cs}.ts()};
|
||||
parser_state{state_p{cs}.ts(), gs}.gen_main(get_string());
|
||||
uint32_t *cbuf = bcode_alloc(state_p{cs}.ts().istate, gs.code.size());
|
||||
std::memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t));
|
||||
auto *bc = reinterpret_cast<bcode *>(cbuf + 1);
|
||||
auto bc = gs.steal_ref();
|
||||
set_code(bc);
|
||||
return bcode_ref{bc};
|
||||
return bc;
|
||||
}
|
||||
|
||||
ident &any_value::force_ident(state &cs) {
|
||||
|
|
23
src/cs_vm.cc
23
src/cs_vm.cc
|
@ -241,10 +241,7 @@ bool exec_alias(
|
|||
if (!aast.node->code) {
|
||||
gen_state gs{ts};
|
||||
parser_state{ts, gs}.gen_main(aast.node->val_s.get_string());
|
||||
/* i wish i could steal the memory somehow */
|
||||
uint32_t *code = bcode_alloc(ts.istate, gs.code.size());
|
||||
memcpy(code, gs.code.data(), gs.code.size() * sizeof(uint32_t));
|
||||
aast.node->code = bcode_ref{reinterpret_cast<bcode *>(code + 1)};
|
||||
aast.node->code = gs.steal_ref();
|
||||
}
|
||||
bcode_ref coderef = aast.node->code;
|
||||
auto cleanup = [&]() {
|
||||
|
@ -701,14 +698,7 @@ std::uint32_t *vm_exec(
|
|||
gs.gen_main_null();
|
||||
break;
|
||||
}
|
||||
std::uint32_t *cbuf = bcode_alloc(ts.istate, gs.code.size());
|
||||
std::memcpy(
|
||||
cbuf, gs.code.data(),
|
||||
gs.code.size() * sizeof(std::uint32_t)
|
||||
);
|
||||
arg.set_code(
|
||||
reinterpret_cast<bcode *>(cbuf + 1)
|
||||
);
|
||||
arg.set_code(gs.steal_ref());
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -720,14 +710,7 @@ std::uint32_t *vm_exec(
|
|||
if (!s.empty()) {
|
||||
gen_state gs{ts};
|
||||
parser_state{ts, gs}.gen_main(s);
|
||||
std::uint32_t *cbuf = bcode_alloc(
|
||||
ts.istate, gs.code.size()
|
||||
);
|
||||
std::memcpy(
|
||||
cbuf, gs.code.data(),
|
||||
gs.code.size() * sizeof(std::uint32_t)
|
||||
);
|
||||
arg.set_code(reinterpret_cast<bcode *>(cbuf + 1));
|
||||
arg.set_code(gs.steal_ref());
|
||||
} else {
|
||||
arg.force_none();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue