fakir/src/VM.hpp

76 lines
1.8 KiB
C++
Raw Normal View History

#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"
#include "Lambda.hpp"
namespace fk
{
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-20 15:17:13 +00:00
struct Frame {
std::shared_ptr<Program> program;
2023-09-22 20:04:57 +00:00
std::shared_ptr<Lambda> lambda;
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-22 12:17:21 +00:00
class Module;
FK_ERROR(execution_error);
class VM
{
public:
2023-09-22 12:17:21 +00:00
explicit VM(Module& mod);
virtual ~VM();
2023-09-21 13:59:46 +00:00
Frame& frame() { return m_frames.back(); }
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);
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);
private:
2023-09-22 12:17:21 +00:00
Module& m_mod;
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;
};
}
#endif