-
Notifications
You must be signed in to change notification settings - Fork 3
/
BytecodeInterpreter.cpp
157 lines (131 loc) · 5.3 KB
/
BytecodeInterpreter.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
#include "BytecodeInterpreter.hpp"
#include <iostream>
namespace bytecodeinterpreter {
using namespace std;
InstructionFunction gInstructionFunctions[NUM_INSTRUCTIONS] = {
ExitInstruction,
AddIntInstruction,
PushIntInstruction,
PopIntInstruction,
PrintIntInstruction,
CompareIntLessThanInstruction,
LoadIntInstruction,
StoreIntInstruction,
JumpByIfZeroInstruction,
JumpByInstruction,
LoadIntBasepointerRelativeInstruction,
StoreIntBasepointerRelativeInstruction,
CallInstruction,
ReturnInstruction,
};
/*static*/ void BytecodeInterpreter::Run(Instruction* code, vector<int16_t> arguments, int16_t* result) {
InterpreterRegisters registers{ .currentInstruction = code };
if (result) {
registers.stack.push_back(0);
}
registers.stack.insert(registers.stack.end(), arguments.begin(), arguments.end());
registers.stack.push_back(0); // push old baseIndex.
registers.returnAddressStack.push_back(nullptr);
registers.baseIndex = registers.stack.size();
while (registers.currentInstruction != nullptr) {
gInstructionFunctions[registers.currentInstruction->opcode](registers);
}
size_t numArgs = arguments.size();
while( numArgs-- ) {
registers.stack.pop_back();
}
if (result) {
*result = registers.stack[0];
}
}
//! EXIT
void ExitInstruction(InterpreterRegisters& registers) {
registers.currentInstruction = nullptr;
}
//! ADD_INT
void AddIntInstruction(InterpreterRegisters& registers) {
int16_t rightHandSide = registers.stack.back();
registers.stack.pop_back();
int16_t leftHandSide = registers.stack.back();
registers.stack.pop_back();
registers.stack.push_back(leftHandSide + rightHandSide);
++registers.currentInstruction;
}
//! PUSH_INT
void PushIntInstruction(InterpreterRegisters& registers) {
registers.stack.push_back(registers.currentInstruction->p2);
++registers.currentInstruction;
}
//! POP_INT
void PopIntInstruction(InterpreterRegisters& registers) {
registers.stack.pop_back();
++registers.currentInstruction;
}
//! PRINT_INT
void PrintIntInstruction(InterpreterRegisters& registers) {
int16_t number = registers.stack.back();
registers.stack.pop_back();
cout << "Number Printed: " << number << endl;
++registers.currentInstruction;
}
//! COMP_INT_LT
void CompareIntLessThanInstruction(InterpreterRegisters& registers) {
int16_t rightHandSide = registers.stack.back();
registers.stack.pop_back();
int16_t leftHandSide = registers.stack.back();
registers.stack.pop_back();
registers.stack.push_back(leftHandSide < rightHandSide);
++registers.currentInstruction;
}
//! LOAD_INT
void LoadIntInstruction(InterpreterRegisters& registers) {
registers.stack.push_back(registers.stack[registers.currentInstruction->p2]);
++registers.currentInstruction;
}
//! STORE_INT
void StoreIntInstruction(InterpreterRegisters& registers) {
registers.stack[registers.currentInstruction->p2] = registers.stack.back();
registers.stack.pop_back();
++registers.currentInstruction;
}
//! JUMP_BY_IF_ZERO
void JumpByIfZeroInstruction(InterpreterRegisters& registers) {
int16_t condition = registers.stack.back();
registers.stack.pop_back();
if (condition == 0) {
registers.currentInstruction += registers.currentInstruction->p2;
} else {
++registers.currentInstruction; // no-op.
}
}
//! JUMP_BY
void JumpByInstruction(InterpreterRegisters& registers) {
registers.currentInstruction += registers.currentInstruction->p2;
}
//! LOAD_INT_BASEPOINTER_RELATIVE
void LoadIntBasepointerRelativeInstruction(InterpreterRegisters ®isters) {
registers.stack.push_back(registers.stack[registers.currentInstruction->p2 + registers.baseIndex]);
++registers.currentInstruction;
}
//! STORE_INT_BASEPOINTER_RELATIVE
void StoreIntBasepointerRelativeInstruction(InterpreterRegisters ®isters) {
registers.stack[registers.currentInstruction->p2 + registers.baseIndex] = registers.stack.back();
registers.stack.pop_back();
++registers.currentInstruction;
}
//! CALL
void CallInstruction(InterpreterRegisters ®isters) {
registers.stack.push_back(int16_t(registers.baseIndex)); // DANGER! Saved base pointer is limited to 16 bits signed!
registers.returnAddressStack.push_back(registers.currentInstruction + 1);
registers.baseIndex = registers.stack.size();
registers.currentInstruction += registers.currentInstruction->p2;
}
//! RETURN
void ReturnInstruction(InterpreterRegisters ®isters) {
Instruction * returnAddress = registers.returnAddressStack.back();
registers.returnAddressStack.pop_back();
registers.baseIndex = registers.stack.back();
registers.stack.pop_back();
registers.currentInstruction = returnAddress;
}
}