valeri/src/vm.hpp

88 lines
2.5 KiB
C++
Raw Normal View History

#pragma once
#include "common.hpp"
class VM {
public:
VM() {}
VM(StackFrame&& stack, Function&& fun, Array&& code, Array&& constants,
Array&& closure, Dict&& globals, uint64_t pc, Value&& res)
: _stack(std::move(stack)),
_fun(std::move(fun)),
_code(std::move(code)),
_constants(std::move(constants)),
_closure(std::move(closure)),
_globals(std::move(globals)),
_pc(pc),
_res(std::move(res)) {}
VM(VM&& vm)
: _stack(std::move(vm._stack)),
_fun(std::move(vm._fun)),
_code(std::move(vm._code)),
_constants(std::move(vm._constants)),
_closure(std::move(vm._closure)),
_globals(std::move(vm._globals)),
_pc(vm._pc),
_res(std::move(vm._res)) {}
VM(const VM&) = delete;
static Result<VM> create(const Module& mod, const Dict& globals) {
auto fun = TRY(mod.fun());
auto nil = Value(TRY(Nil::create()));
auto stack = TRY(StackFrame::create(nil, fun, 0));
uint64_t pc = 0;
auto code = TRY(fun.code());
auto constants = TRY(fun.constants());
auto closure = TRY(fun.closure());
auto globals_copy = TRY(globals.copy());
return VM(std::move(stack), std::move(fun), std::move(code),
std::move(constants), std::move(closure), std::move(globals_copy),
pc, std::move(nil));
}
Result<Value> run();
Result<void> step();
Result<void> vm_mov(Opcode& oc);
Result<void> vm_add(Opcode& oc);
Result<void> vm_mul(Opcode& oc);
Result<void> vm_sub(Opcode& oc);
Result<void> vm_div(Opcode& oc);
Result<void> vm_call(Opcode& oc);
Result<void> vm_selfcall(Opcode& oc);
Result<void> vm_call_lisp(Opcode& oc, Function& fun);
Result<void> vm_call_stdlib(Opcode& oc, StdlibFunction& fun);
Result<void> vm_ret(Opcode& oc);
2024-08-23 20:30:05 +00:00
Result<void> vm_equal(Opcode& oc);
Result<void> vm_less(Opcode& oc);
Result<void> vm_less_equal(Opcode& oc);
2024-08-15 00:18:05 +00:00
Result<void> vm_jump(Opcode& oc);
Result<void> vm_make_closure(Opcode& oc);
Result<void> vm_closure_load(Opcode& oc);
Result<void> vm_global_load(Opcode& oc);
Result<void> vm_global_store(Opcode& oc);
Result<Value> get(bool is_const, uint64_t idx);
Result<Value> getconst(uint64_t idx);
Result<Value> getreg(uint64_t idx);
Result<StackFrame> setreg(uint64_t idx, const Value& value);
Result<Dict> globals() { return _globals.copy(); }
private:
StackFrame _stack;
Function _fun;
Array _code;
Array _constants;
Array _closure;
Dict _globals;
uint64_t _pc;
Value _res;
};