2023-09-20 11:31:35 +00:00
|
|
|
#ifndef fk_VM_HPP
|
|
|
|
#define fk_VM_HPP
|
|
|
|
|
|
|
|
#include "commons.hpp"
|
|
|
|
#include "Program.hpp"
|
2023-09-20 15:17:13 +00:00
|
|
|
#include "NativeFunction.hpp"
|
2023-09-21 19:17:39 +00:00
|
|
|
#include "Lambda.hpp"
|
2023-09-20 11:31:35 +00:00
|
|
|
|
|
|
|
namespace fk
|
|
|
|
{
|
2023-09-21 19:17:39 +00:00
|
|
|
using global_t = std::variant<std::shared_ptr<NativeFunction>,
|
2023-09-23 17:21:55 +00:00
|
|
|
std::shared_ptr<Module>,
|
2023-09-23 22:40:08 +00:00
|
|
|
std::shared_ptr<Lambda>,
|
|
|
|
std::shared_ptr<Array>
|
2023-09-21 19:17:39 +00:00
|
|
|
>;
|
2023-09-20 15:17:13 +00:00
|
|
|
|
2023-09-20 11:31:35 +00:00
|
|
|
struct Frame {
|
|
|
|
std::shared_ptr<Program> program;
|
2023-09-22 20:04:57 +00:00
|
|
|
std::shared_ptr<Lambda> lambda;
|
2023-09-21 19:17:39 +00:00
|
|
|
addr_t ret_addr;
|
|
|
|
size_t stack_sz;
|
2023-09-21 13:59:46 +00:00
|
|
|
std::unordered_map<addr_t, std::shared_ptr<Constant>> locals;
|
2023-09-20 11:31:35 +00:00
|
|
|
};
|
2023-09-22 12:17:21 +00:00
|
|
|
class Module;
|
2023-09-20 11:31:35 +00:00
|
|
|
|
2023-09-24 10:17:42 +00:00
|
|
|
FK_ERROR(execution_error);
|
|
|
|
|
2023-09-20 11:31:35 +00:00
|
|
|
class VM
|
|
|
|
{
|
|
|
|
public:
|
2023-09-22 12:17:21 +00:00
|
|
|
explicit VM(Module& mod);
|
2023-09-20 11:31:35 +00:00
|
|
|
virtual ~VM();
|
|
|
|
|
2023-09-21 13:59:46 +00:00
|
|
|
Frame& frame() { return m_frames.back(); }
|
2023-09-20 11:31:35 +00:00
|
|
|
|
|
|
|
void mount(std::shared_ptr<Program> program);
|
|
|
|
void run();
|
|
|
|
|
2023-09-22 11:27:13 +00:00
|
|
|
std::shared_ptr<Constant>
|
|
|
|
call(std::shared_ptr<Lambda> lambda,
|
|
|
|
size_t ref,
|
|
|
|
std::vector<std::shared_ptr<Constant>> const& args,
|
|
|
|
Loc const& loc);
|
|
|
|
|
2023-09-20 15:17:13 +00:00
|
|
|
global_t load_global(addr_t addr) const;
|
|
|
|
void store_global(addr_t addr, global_t value);
|
|
|
|
size_t store_global(global_t value);
|
|
|
|
|
2023-09-21 13:59:46 +00:00
|
|
|
std::shared_ptr<Constant> load_local(addr_t addr);
|
|
|
|
void store_local(addr_t addr, std::shared_ptr<Constant> constant);
|
|
|
|
|
2023-09-20 11:31:35 +00:00
|
|
|
std::string string() const;
|
2023-09-20 15:17:13 +00:00
|
|
|
|
2023-09-22 11:27:13 +00:00
|
|
|
void push(addr_t addr);
|
|
|
|
addr_t top();
|
|
|
|
addr_t pop();
|
|
|
|
|
|
|
|
std::shared_ptr<Constant>
|
|
|
|
run_lambda(VM const& parent,
|
|
|
|
std::shared_ptr<Lambda> lambda,
|
|
|
|
std::vector<std::shared_ptr<Constant>> args,
|
|
|
|
size_t ref,
|
|
|
|
Loc const& loc);
|
|
|
|
|
2023-09-20 11:31:35 +00:00
|
|
|
private:
|
2023-09-22 12:17:21 +00:00
|
|
|
Module& m_mod;
|
2023-09-20 11:31:35 +00:00
|
|
|
addr_t m_pc = 0;
|
|
|
|
std::vector<Frame> m_frames;
|
|
|
|
std::vector<addr_t> m_stack;
|
2023-09-20 15:17:13 +00:00
|
|
|
std::unordered_map<addr_t, global_t> m_globals;
|
2023-09-22 11:27:13 +00:00
|
|
|
std::shared_ptr<Constant> m_return_const;
|
2023-09-20 11:31:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|