-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFrameStack.h
89 lines (69 loc) · 2.06 KB
/
FrameStack.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef VM_FRAMESTACK_H
#define VM_FRAMESTACK_H
#include <cstdint>
#include <cassert>
#include <vector>
#include <stack>
#include "class.h"
struct Frame {
std::vector<int32_t> local_vars;
std::stack<int32_t> operand_stack;
};
/**
* Maintain a stack of frames, each containing a local variable array and an operand stack
*
* This implementation only gives access to the frame at the top, requiring pop() to access those below.
*/
class FrameStack {
private:
std::stack<Frame> stack;
public:
Item *const_pool_ptr; // TODO: Can't be sure this is still allocated
void frame_push(size_t local_var_size, size_t stack_size) {
Frame new_frame = Frame();
new_frame.local_vars = std::vector<int32_t>(local_var_size);
stack.push(new_frame);
}
void frame_pop() {
stack.pop();
}
int32_t get_local_var(size_t index) {
return stack.top().local_vars.at(index);
}
void set_local_var(size_t index, int32_t value) {
stack.top().local_vars.at(index) = value;
}
int32_t stack_pop() {
assert(stack.top().operand_stack.size() != 0);
int32_t ret = stack.top().operand_stack.top();
stack.top().operand_stack.pop();
return ret;
}
void stack_push(int32_t val) {
stack.top().operand_stack.push(val);
}
void stack_shrink(size_t len) {
for (int i = 0; i < len; ++i) {
assert(stack.top().operand_stack.size() != 0);
stack.top().operand_stack.pop();
}
}
String const_pool_string(size_t index) {
Item &item = const_pool_ptr[index];
assert(item.tag == STRING_UTF8);
return item.value.string;
}
int32_t const_pool_int(size_t index) {
Item &item = const_pool_ptr[index];
assert(item.tag == INTEGER);
return item.value.integer;
}
bool stack_is_empty() {
return stack.top().operand_stack.size() == 0;
}
Item &const_pool(int index) {
assert(index > 0);
return const_pool_ptr[index - 1];
}
};
#endif //VM_FRAMESTACK_H