-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVirtualMachine.cpp
More file actions
251 lines (229 loc) · 7.07 KB
/
VirtualMachine.cpp
File metadata and controls
251 lines (229 loc) · 7.07 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <fstream>
#include "VirtualMachine.h"
#include "Frame.h"
using namespace std;
// #define ON_DEBUG
#ifdef ON_DEBUG
#define DEBUG_OUT(message, target) cout << "debug | " << message << target << endl
#else
#define DEBUG_OUT(message, target)
#endif
uint32_t VirtualMachine::load(string file_name) {
input.clear();
position = 0;
file_name += ".wc";
ifstream fin;
fin.open(file_name.c_str(), ios::in | ios::binary);
if (!fin) {
cout << "error | file `"<< file_name.c_str() <<"` didn't open" << endl;
exit(1);
}
while (!fin.eof()) {
uint8_t byte = 0;
fin.read((char*) &byte, sizeof(uint8_t));
input.push_back(byte);
}
fin.close();
uint32_t magic = *read_int();
DEBUG_OUT("magic : ", magic);
if (magic != 0xdeadbeef) {
cout << "error | magic is invalid" << endl;
}
uint32_t entry_point = *read_int();
DEBUG_OUT("entry point : ", entry_point);
uint32_t registers_size = *read_int();
DEBUG_OUT("registers size : ", registers_size);
registers = new Object_*[registers_size];
uint32_t references_size = *read_int();
DEBUG_OUT("references size : ", references_size);
uint32_t functions_size = *read_int();
DEBUG_OUT("functions size : ", functions_size);
functions = new uint32_t[functions_size];
functions = (uint32_t*) read(sizeof(uint32_t) * functions_size);
int32_t instructions_size = *read_int();
DEBUG_OUT("instructions size : ", instructions_size);
instructions = new instruction[instructions_size];
instructions = (instruction*) read(sizeof(instruction) * instructions_size);
uint32_t constant_pool_size = *read_int();
DEBUG_OUT("constant pool size : ", constant_pool_size);
constant_pool = new Object_*[constant_pool_size];
for (uint32_t i = 0; i < constant_pool_size; i++) {
uint32_t tag = *read_int();
uint32_t size;
switch (tag) {
case 0x0: // int
constant_pool[i] = (Object_ *) *read_int();
break;
case 0x1: // string
size = *read_int();
constant_pool[i] = (Object_ *) read(size);
break;
default:
cout << "error | illegal constant type `" << tag << "`" << endl;
exit(1);
}
}
return entry_point;
}
// direct threading
#if defined __GNUC__ || defined __clnag__ || defined __INTEL_COMPILER
#define DIRECT_THREADED
#endif
#ifdef DIRECT_THREADED
#define INIT_DISPATCH JUMP;
#define CASE(op) L_ ## op:
#define NEXT i=*++pc; goto *table[i.type]
#define JUMP i=*pc; goto *table[i.type]
#define END_DISPATCH
#else
#define INIT_DISPATCH while(true) { i = *pc; switch (i.type) {
#define CASE(op) case op:
#define NEXT pc++; break;
#define JUMP break
#define END_DISPATCH }}
#endif
void VirtualMachine::execute(uint32_t entry_point) {
instruction* pc = instructions + functions[entry_point];
instruction i;
#ifdef DIRECT_THREADED
static void* table[] = {
&&L_STOP,
&&L_CALL,
&&L_RETURN,
&&L_BR,
&&L_BR_N,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_COPY,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_ADD0,
&&L_ADD1,
&&L_ADD2,
&&L_SUB0,
&&L_SUB1,
&&L_SUB2,
&&L_MUL0,
&&L_MUL1,
&&L_MUL2,
&&L_DIV0,
&&L_DIV1,
&&L_DIV2,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_NOP,
&&L_PRINT0,
&&L_PRINT1
};
#endif
INIT_DISPATCH {
CASE(NOP) {
DEBUG_OUT("nop called with instruction : ", (uint32_t) pc->type);
} NEXT;
CASE(STOP) {
DEBUG_OUT("stop called with instruction : ", (uint32_t) pc->type);
} return;
CASE(CALL) {
frame* f = new frame;
f->this_ = register_[-1];
f->return_address = pc + 1;
runtime_stack.push(f);
register_[-1] = register_[pc->operand0];
pc = register_[-1].o.dynamic_function[pc->operand1].offset;
} JUMP;
CASE(RETURN) {
frame* f = runtime_stack.top();
register_[-1] = f->this_;
pc = f->return_address;
runtime_stack.pop();
} JUMP;
CASE(BR) {
pc = instructions + i.operand0;
} JUMP;
CASE(BR_N) {
} JUMP;
CASE(COPY) {
registers[i.operand0] = constant_pool[i.operand1];
} NEXT;
CASE(ADD0) {
registers[i.operand0] = (Object_*) (registers[i.operand1]->i + registers[i.operand2]->i);
} NEXT;
CASE(ADD1) {
registers[i.operand0] = (Object_*) (registers[i.operand1]->i + constant_pool[i.operand2]->i);
} NEXT;
CASE(ADD2) {
registers[i.operand0] = (Object_*) (constant_pool[i.operand1]->i + constant_pool[i.operand2]->i);
} NEXT;
CASE(SUB0) {
registers[i.operand0] = (Object_*) (registers[i.operand1]->i - registers[i.operand2]->i);
} NEXT;
CASE(SUB1) {
registers[i.operand0] = (Object_*) (registers[i.operand1]->i - constant_pool[i.operand2]->i);
} NEXT;
CASE(SUB2) {
registers[i.operand0] = (Object_*) (constant_pool[i.operand1]->i - constant_pool[i.operand2]->i);
} NEXT;
CASE(MUL0) {
registers[i.operand0] = (Object_*) (registers[i.operand1]->i * registers[i.operand2]->i);
} NEXT;
CASE(MUL1) {
registers[i.operand0] = (Object_*) (registers[i.operand1]->i * constant_pool[i.operand2]->i);
} NEXT;
CASE(MUL2) {
registers[i.operand0] = (Object_*) (constant_pool[i.operand1]->i * constant_pool[i.operand2]->i);
} NEXT;
CASE(DIV0) {
registers[i.operand0] = (Object_*) (registers[i.operand1]->i / registers[i.operand2]->i);
} NEXT;
CASE(DIV1) {
registers[i.operand0] = (Object_*) (registers[i.operand1]->i / constant_pool[i.operand2]->i);
} NEXT;
CASE(DIV2) {
registers[i.operand0] = (Object_*) (constant_pool[i.operand1]->i / constant_pool[i.operand2]->i);
} NEXT;
CASE(PRINT0) {
printf("%s", constant_pool[i.operand0]->s);
} NEXT;
CASE(PRINT1) {
printf("%d", registers[i.operand0]->i);
} NEXT;
}
END_DISPATCH;
}
uint8_t* VirtualMachine::read(const int32_t length) {
auto bytes = new uint8_t[length];
for (int i = 0; i < length; i++) {
bytes[i] = input[position + i];
}
position += length;
return bytes;
}
uint32_t* VirtualMachine::read_int() {
auto bytes = read(4);
DEBUG_OUT("read_int called and returned : ", *(uint32_t*)bytes);
return (uint32_t*)bytes;
}
#undef DEBUG_OUT