-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvm.cpp
214 lines (212 loc) · 9.21 KB
/
vm.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// Created by Kevin Tan on 2021/11/20.
//
#include "vm.h"
void StackMachine::run() {
auto pc = instructions.begin();
while (pc < instructions.end()) {
switch ((*pc)->opcode) {
case OpCode::LOAD_FAST:
stack->push_back(cast<LoadFast>(*pc)->object);
++pc;
break;
case OpCode::LOAD_NAME: {
ObjectP info = cast<LoadName>(*pc)->object;
if (info->is_global) {
stack->push_back(globals[info->ident_info->name]);
} else {
auto &objects = frames.back()->objects;
auto it = objects.find(info->ident_info);
if (it != objects.end()) {
stack->push_back((*it).second);
} else {
cerr << "WARNING: use of unbound name " << info->ident_info->name << " (declared in line "
<< info->ident_info->line << "), has bound its value to 0" << endl;
IntObjectP o = make_shared<IntObject>();
objects[info->ident_info] = o;
stack->push_back(o);
}
}
++pc;
break;
}
case OpCode::STORE_NAME: {
ObjectP o = cast<StoreName>(*pc)->object;
auto &name = o->ident_info->name;
auto &locals = frames.back()->objects;
if (o->is_const) {
ERROR_NOT_SUPPORTED(modifing const);
} else if (o->is_global) {
if (o->type == TypeCode::INT_ARRAY) {
cast<ArrayObject>(globals[name])->data = cast<ArrayObject>(stack->back())->data;
} else {
globals[name] = stack->back()->copy(); // replace store, must be copied
}
} else {
if (o->type == TypeCode::INT_ARRAY) {
cast<ArrayObject>(stack->back())->dims = cast<ArrayObject>(o)->dims;
} // dim of array on stack may be unknown, so we need to copy them
locals[o->ident_info] = stack->back()->copy(); // replace store, must be copied
}
stack->pop_back();
++pc;
break;
}
case OpCode::POP_TOP:
stack->pop_back();
++pc;
break;
case OpCode::BUILD_ARRAY: {
long long size = cast<IntObject>(stack->back())->value;
stack->pop_back();
ArrayObjectP array = make_shared<ArrayObject>(size);
for (long long i = 0; i < size; i++) {
array->data->push_back(make_shared<IntObject>()); // zeroed for safety
}
stack->push_back(array);
++pc;
break;
}
case OpCode::INIT_ARRAY: {
ArrayObjectP array = make_shared<ArrayObject>((long long) stack->size());
// not safe to use `*array->data = *stack` because stack size may be smaller than the array size
// shouldn't use `insert` because zeros are not overwritten
// the operand of the instruction is not used because we don't have the `ArrayLiteral` class to
// record the actual size of the array literal when VLA is supported
for (auto &i: *stack) {
array->data->push_back(i->copy()); // must be copied
}
stack->clear();
stack->push_back(array);
++pc;
break;
}
case OpCode::STORE_SUBSCR: {
auto value = cast<IntObject>(stack->back())->value;
stack->pop_back();
cast<IntObject>(stack->back())->value = value; // in-place store, must not be copied
stack->pop_back();
++pc;
break;
}
case OpCode::SUBSCR_ARRAY: {
long long index = cast<IntObject>(stack->back())->value;
stack->pop_back();
ArrayObjectP array = cast<ArrayObject>(stack->back());
stack->pop_back();
stack->push_back((*array)[index]);
++pc;
break;
}
case OpCode::CALL_GETINT: {
int n;
cin >> n;
stack->push_back(make_shared<IntObject>(n));
++pc;
break;
}
case OpCode::JUMP_ABSOLUTE:
pc = instructions.begin() + cast<JumpAbsolute>(*pc)->get_offset();
break;
case OpCode::POP_JUMP_IF_FALSE: {
long long value = cast<IntObject>(stack->back())->value;
stack->pop_back();
if (!value) {
pc = instructions.begin() + cast<PopJumpIfFalse>(*pc)->get_offset();
} else {
++pc;
}
break;
}
case OpCode::POP_JUMP_IF_TRUE: {
long long value = cast<IntObject>(stack->back())->value;
stack->pop_back();
if (value) {
pc = instructions.begin() + cast<PopJumpIfTrue>(*pc)->get_offset();
} else {
++pc;
}
break;
}
case OpCode::CALL_PRINTF: {
FormatStringP fmt_str = cast<PrintF>(*pc)->format_string;
auto seg_it = fmt_str->segments.begin();
outs << *seg_it;
for (auto it = stack->end() - fmt_str->fmt_char_cnt; it != stack->end(); ++it) {
outs << cast<IntObject>(*it)->value << *++seg_it;
}
for (int i = 0; i < fmt_str->fmt_char_cnt; i++) {
stack->pop_back();
}
++pc;
break;
}
case OpCode::EXIT_INTERP:
return;
case OpCode::CALL_FUNCTION: {
FrameP new_frame = make_shared<Frame>();
new_frame->return_offset = pc - instructions.begin() + 1;
FuncObjectP func = cast<CallFunction>(*pc)->get_func();
auto ¶ms = func->params;
for (auto i = (long long) params.size() - 1; i >= 0; i--) {
ObjectP o = stack->back();
switch (o->type) {
case TypeCode::INT:
new_frame->objects[params[i]->ident_info] = cast<IntObject>(o)->copy();
break;
case TypeCode::INT_ARRAY: {
ArrayObjectP array = cast<ArrayObject>(o->copy());
new_frame->objects[params[i]->ident_info] = array;
array->dims = cast<ArrayObject>(params[i])->dims;
break; // note the dimension difference when addressing
}
default:
ERROR_LIMITED_SUPPORT(INT or INT_ARRAY function arguments);
}
stack->pop_back();
}
stack = new_frame->stack;
frames.push_back(new_frame);
pc = instructions.begin() + func->code_offset;
break;
}
case OpCode::RETURN_VALUE: {
if (!stack->empty()) {
assert(stack->size() == 1);
ObjectP value = stack->back();
stack->pop_back();
pc = instructions.begin() + frames.back()->return_offset;
frames.pop_back();
stack = frames.back()->stack;
stack->push_back(value);
} else {
pc = instructions.begin() + frames.back()->return_offset;
frames.pop_back();
stack = frames.back()->stack;
stack->push_back(make_shared<Object>());
// void object is returned for void function because the POP_TOP instruction is always added
// after the function call when the return value is not used
}
break;
}
case OpCode::UNARY_OP: {
auto value = cast<IntObject>(stack->back())->value;
stack->pop_back();
stack->push_back(make_shared<IntObject>(util::unary_operation(cast<UnaryOperation>(*pc)->unary_opcode, value)));
++pc;
break;
}
case OpCode::BINARY_OP: {
auto right = cast<IntObject>(stack->back())->value;
stack->pop_back();
auto left = cast<IntObject>(stack->back())->value;
stack->pop_back();
stack->push_back(make_shared<IntObject>(util::binary_operation(cast<BinaryOperation>(*pc)->binary_opcode, left, right)));
++pc;
break;
}
default:
++pc;
}
}
}