-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.h
47 lines (35 loc) · 1.16 KB
/
vm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// Created by Kevin Tan on 2021/11/20.
//
#ifndef CODE_VM_H
#define CODE_VM_H
#include "instruction.h"
using Stack = vector<ObjectP>;
using StackP = shared_ptr<Stack>;
struct Frame {
unordered_map<IdentP, ObjectP> objects{CACHE_LINE_SIZE / sizeof(ObjectP)};
StackP stack = make_shared<Stack>();
long long return_offset = 0;
Frame() {
stack->reserve(CACHE_LINE_SIZE / sizeof(ObjectP));
}
};
using FrameP = shared_ptr<Frame>;
class StackMachine {
public:
vector<InstructionP> &instructions;
unordered_map<string, ObjectP> &globals;
ostream &outs;
vector<FrameP> frames{make_shared<Frame>()}; // dummy frame
StackP stack = frames.back()->stack;
explicit StackMachine(vector<InstructionP> &instructions, unordered_map<string, ObjectP> &globals,
ostream &outs) : instructions(instructions), globals(globals), outs(outs) {};
void run();
friend ostream &operator<<(ostream &out, const StackMachine &self) {
for (int i = 0; i < self.instructions.size(); i++) {
out << i << '\t' << *self.instructions[i] << endl;
}
return out;
}
};
#endif //CODE_VM_H