This repository has been archived on 2023-09-10. You can view files and clone it, but cannot push or open issues/pull-requests.
2023-09-09 22:03:28 +00:00
|
|
|
#ifndef jk_VM_HPP
|
|
|
|
#define jk_VM_HPP
|
|
|
|
|
|
|
|
#include "commons.hpp"
|
|
|
|
#include "Program.hpp"
|
|
|
|
|
|
|
|
namespace jk
|
|
|
|
{
|
|
|
|
struct Frame {
|
|
|
|
std::shared_ptr<Program> program;
|
|
|
|
std::unordered_map<int, std::shared_ptr<Value>> locals;
|
2023-09-10 07:59:51 +00:00
|
|
|
size_t ret_addr;
|
|
|
|
size_t stack_sz;
|
2023-09-09 22:03:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class VM
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit VM();
|
|
|
|
virtual ~VM();
|
|
|
|
|
|
|
|
void execute(std::shared_ptr<Program> program);
|
2023-09-10 07:59:51 +00:00
|
|
|
void execute();
|
2023-09-09 22:03:28 +00:00
|
|
|
|
|
|
|
std::string string() const;
|
|
|
|
|
|
|
|
size_t push_heap(std::shared_ptr<Value> value);
|
|
|
|
std::shared_ptr<Value> heap(size_t addr);
|
|
|
|
|
|
|
|
void set_global(size_t addr, std::shared_ptr<Value> value);
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t m_pc = 0;
|
|
|
|
std::vector<Frame> m_frames;
|
|
|
|
std::vector<param_t> m_stack;
|
|
|
|
std::vector<std::shared_ptr<Value>> m_heap;
|
|
|
|
std::unordered_map<size_t, std::shared_ptr<Value>> m_globals;
|
|
|
|
|
|
|
|
void push(param_t param);
|
|
|
|
param_t pop();
|
2023-09-10 07:59:51 +00:00
|
|
|
|
|
|
|
std::shared_ptr<Program> program() const;
|
2023-09-09 22:03:28 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|