2024-08-12 22:05:40 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
|
|
|
|
class VM {
|
|
|
|
public:
|
|
|
|
VM() {}
|
2024-09-12 23:54:32 +00:00
|
|
|
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)) {}
|
2024-08-17 22:22:21 +00:00
|
|
|
VM(VM&& vm)
|
2024-09-12 23:54:32 +00:00
|
|
|
: _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)) {}
|
|
|
|
|
2024-08-12 22:05:40 +00:00
|
|
|
VM(const VM&) = delete;
|
|
|
|
|
2024-09-12 23:54:32 +00:00
|
|
|
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));
|
2024-08-12 22:05:40 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 23:54:32 +00:00
|
|
|
Result<Value> run();
|
2024-08-12 22:05:40 +00:00
|
|
|
|
|
|
|
Result<void> step();
|
|
|
|
Result<void> vm_mov(Opcode& oc);
|
2024-08-13 00:11:23 +00:00
|
|
|
Result<void> vm_add(Opcode& oc);
|
|
|
|
Result<void> vm_mul(Opcode& oc);
|
|
|
|
Result<void> vm_sub(Opcode& oc);
|
|
|
|
Result<void> vm_div(Opcode& oc);
|
2024-08-17 22:22:21 +00:00
|
|
|
Result<void> vm_call(Opcode& oc);
|
2024-08-31 18:42:27 +00:00
|
|
|
Result<void> vm_selfcall(Opcode& oc);
|
2024-08-24 23:55:11 +00:00
|
|
|
Result<void> vm_call_lisp(Opcode& oc, Function& fun);
|
|
|
|
Result<void> vm_call_stdlib(Opcode& oc, StdlibFunction& fun);
|
2024-08-17 22:22:21 +00:00
|
|
|
Result<void> vm_ret(Opcode& oc);
|
2024-08-12 22:05:40 +00:00
|
|
|
|
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);
|
|
|
|
|
2024-08-21 22:45:19 +00:00
|
|
|
Result<void> vm_make_closure(Opcode& oc);
|
2024-08-22 16:21:05 +00:00
|
|
|
Result<void> vm_closure_load(Opcode& oc);
|
2024-08-21 22:45:19 +00:00
|
|
|
|
2024-08-23 23:28:29 +00:00
|
|
|
Result<void> vm_global_load(Opcode& oc);
|
2024-09-01 13:31:25 +00:00
|
|
|
Result<void> vm_global_store(Opcode& oc);
|
2024-08-23 23:28:29 +00:00
|
|
|
|
2024-08-12 22:05:40 +00:00
|
|
|
Result<Value> get(bool is_const, uint64_t idx);
|
|
|
|
Result<Value> getconst(uint64_t idx);
|
|
|
|
Result<Value> getreg(uint64_t idx);
|
2024-09-12 23:54:32 +00:00
|
|
|
Result<StackFrame> setreg(uint64_t idx, const Value& value);
|
2024-08-12 22:05:40 +00:00
|
|
|
|
2024-09-12 21:43:38 +00:00
|
|
|
Result<Dict> globals() { return _globals.copy(); }
|
2024-09-01 13:31:25 +00:00
|
|
|
|
2024-08-12 22:05:40 +00:00
|
|
|
private:
|
2024-09-12 21:43:38 +00:00
|
|
|
StackFrame _stack;
|
2024-08-12 22:05:40 +00:00
|
|
|
Function _fun;
|
|
|
|
Array _code;
|
|
|
|
Array _constants;
|
2024-08-22 16:21:05 +00:00
|
|
|
Array _closure;
|
2024-08-23 23:28:29 +00:00
|
|
|
Dict _globals;
|
2024-08-12 22:05:40 +00:00
|
|
|
uint64_t _pc;
|
2024-08-13 00:11:23 +00:00
|
|
|
Value _res;
|
2024-08-12 22:05:40 +00:00
|
|
|
};
|