2024-08-12 22:05:40 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
|
|
|
|
class VM {
|
|
|
|
public:
|
|
|
|
VM() {}
|
2024-09-15 00:43:25 +00:00
|
|
|
VM(StackFrame&& stack, Dict&& globals, Value&& res)
|
2024-09-12 23:54:32 +00:00
|
|
|
: _stack(std::move(stack)),
|
|
|
|
_globals(std::move(globals)),
|
|
|
|
_res(std::move(res)) {}
|
2024-09-15 00:43:25 +00:00
|
|
|
|
2024-08-17 22:22:21 +00:00
|
|
|
VM(VM&& vm)
|
2024-09-12 23:54:32 +00:00
|
|
|
: _stack(std::move(vm._stack)),
|
|
|
|
_globals(std::move(vm._globals)),
|
|
|
|
_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) {
|
2024-09-15 00:43:25 +00:00
|
|
|
auto fun = Value(TRY(mod.fun()));
|
2024-09-12 23:54:32 +00:00
|
|
|
auto nil = Value(TRY(Nil::create()));
|
2024-09-13 20:15:57 +00:00
|
|
|
auto stack = TRY(StackFrame::create(nil, fun));
|
2024-09-12 23:54:32 +00:00
|
|
|
|
2024-09-15 00:43:25 +00:00
|
|
|
auto globals_copy = TRY(globals.copy());
|
|
|
|
|
|
|
|
/*
|
2024-09-12 23:54:32 +00:00
|
|
|
auto code = TRY(fun.code());
|
|
|
|
auto constants = TRY(fun.constants());
|
|
|
|
auto closure = TRY(fun.closure());
|
|
|
|
auto globals_copy = TRY(globals.copy());
|
2024-09-15 00:43:25 +00:00
|
|
|
*/
|
2024-09-12 23:54:32 +00:00
|
|
|
|
2024-09-15 00:43:25 +00:00
|
|
|
return VM(std::move(stack), std::move(globals_copy), 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();
|
2024-09-15 00:43:25 +00:00
|
|
|
Result<void> step_bytecode();
|
|
|
|
Result<void> step_native();
|
|
|
|
|
2024-08-12 22:05:40 +00:00
|
|
|
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-09-27 01:33:41 +00:00
|
|
|
Result<StackFrame> handle_raise(const Continuation& cont);
|
|
|
|
|
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-09-15 00:43:25 +00:00
|
|
|
Result<Array> code() {
|
|
|
|
auto fun = TRY(_stack.fun());
|
|
|
|
if (!fun.is<Function>()) return ERROR(TypeMismatch);
|
|
|
|
return fun.to<Function>()->code();
|
|
|
|
}
|
|
|
|
Result<Array> constants() {
|
|
|
|
auto fun = TRY(_stack.fun());
|
|
|
|
if (!fun.is<Function>()) return ERROR(TypeMismatch);
|
|
|
|
return fun.to<Function>()->constants();
|
|
|
|
}
|
|
|
|
Result<Array> closure() {
|
|
|
|
auto fun = TRY(_stack.fun());
|
|
|
|
if (!fun.is<Function>()) return ERROR(TypeMismatch);
|
|
|
|
return fun.to<Function>()->closure();
|
|
|
|
}
|
|
|
|
|
2024-09-15 02:32:52 +00:00
|
|
|
Result<String> backtrace(uint64_t indent = 0) {
|
|
|
|
return _stack.backtrace(indent);
|
|
|
|
}
|
|
|
|
|
2024-08-12 22:05:40 +00:00
|
|
|
private:
|
2024-09-12 21:43:38 +00:00
|
|
|
StackFrame _stack;
|
2024-08-23 23:28:29 +00:00
|
|
|
Dict _globals;
|
2024-08-13 00:11:23 +00:00
|
|
|
Value _res;
|
2024-08-12 22:05:40 +00:00
|
|
|
};
|