-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructions.cpp
427 lines (333 loc) · 12.8 KB
/
instructions.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <stdexcept>
#include <iostream>
#include <cstring>
#include "instructions.h"
#include "njvm.h"
namespace NJVM {
// Definition of every supported instruction.
static constexpr instruction_info_t INSTRUCTION_DATA[] = {
{"halt", false},
{"pushc", true},
{"add", false},
{"sub", false},
{"mul", false},
{"div", false},
{"mod", false},
{"rdint", false},
{"wrint", false},
{"rdchr", false},
{"wrchr", false},
{"pushg", true},
{"popg", true},
{"asf", true},
{"rsf", false},
{"pushl", true},
{"popl", true},
{"eq", false},
{"ne", false},
{"lt", false},
{"le", false},
{"gt", false},
{"ge", false},
{"jmp", true},
{"brf", true},
{"brt", true},
{"call", true},
{"ret", false},
{"drop", true},
{"pushr", false},
{"popr", false},
{"dup", false},
{"new", true},
{"getf", true},
{"putf", true},
{"newa", false},
{"getfa", false},
{"putfa", false},
{"getsz", false},
{"pushn", false},
{"refeq", false},
{"refne", false},
};
// Highest valid opcode. Computed at compile time.
static constexpr opcode_t max_opcode = (sizeof(INSTRUCTION_DATA) / sizeof(instruction_info_t)) - 1;
[[nodiscard]] const instruction_info_t &info_for_opcode(opcode_t opcode) {
if (opcode > max_opcode) {
std::stringstream explanation;
explanation << "Invalid opcode " << static_cast<int>(opcode) << " is out of range.";
throw std::invalid_argument(explanation.str());
}
return INSTRUCTION_DATA[opcode];
}
[[nodiscard]] constexpr opcode_t get_opcode(instruction_t instruction) {
return (instruction >> 24) & 0xFF; // Opcode is encoded in the highest 8 bits.
}
[[nodiscard]] constexpr immediate_t get_immediate(instruction_t instruction) {
auto intermediate = static_cast<immediate_t>(instruction & 0x00FFFFFF); // Operand is encoded in lowest 24 bits.
if (intermediate & 0x00800000) {
// If original immediate was negative, fill remaining bits to extend sign.
intermediate |= (0xFF << 24);
}
return intermediate;
}
// constexpr function to check if two C-strings are equal at compile time.
static constexpr bool strequals(const char *s1, const char *s2) {
for (size_t offset = 0; s1[offset] == s2[offset]; offset++) {
if (s1[offset] == '\0') return true;
}
return false;
}
[[nodiscard]] constexpr opcode_t opcode_for(const char *name) {
for (opcode_t opcode = 0; opcode <= max_opcode; opcode++) {
if (strequals(name, INSTRUCTION_DATA[opcode].name)) {
return opcode;
}
}
throw std::invalid_argument("Unknown instruction mnemonic.");
}
void print_instruction(instruction_t instruction) {
const instruction_info_t &info = info_for_opcode(get_opcode(instruction));
std::cout << info.name;
if (info.requires_operand) {
std::cout << " " << get_immediate(instruction);
}
std::cout << std::endl;
}
//-----------------------------------------------------------------------
// Implementation of instruction execution.
//-----------------------------------------------------------------------
// push and pop use vector implementation for automatic bounds checks.
static inline stack_slot &push() {
return stack.at(sp++);
}
static inline stack_slot &pop() {
return stack.at(--sp);
}
/**
* Generic function to perform a binary arithmetic operation
* using big-integer arguments from stack. Two integers are
* consumed from the stack and the result of the operation
* is placed onto it.
*
* The template parameter is the function implementing the
* binary operation. The function parameter is a reference
* to the bip-register holding the result of the operation.
*/
template<void Binary()>
static void do_arithmetic(void *&result_register) {
bip.op2 = pop().as_reference();
bip.op1 = pop().as_reference();
Binary();
push() = reinterpret_cast<ObjRef>(result_register);
}
/**
* Generic function to perform a binary comparison using
* big-integer arguments from stack. Two integers are
* consumed from the stack and compared using bigCmp().
* The result of this comparison is turned into a boolean
* value using the given Comparator.
*
* The template parameter for this function is a so-called
* "Functor", a struct with an apply-method that is used
* to convert the comparison result into a boolean value.
* A single instance of the Comparator is initialized
* for every specialization of this template function.
*/
template<typename Comparator>
static void do_comparison() {
static Comparator cmp{}; // Instantiate Comparator once for every specialization.
bip.op2 = pop().as_reference();
bip.op1 = pop().as_reference();
bool result = cmp(bigCmp(), 0);
push() = newNinjaInteger(result);
}
bool exec_instruction(instruction_t instruction) {
switch (get_opcode(instruction)) {
case opcode_for("halt"):
return false;
case opcode_for("pushc"):
push() = newNinjaInteger(get_immediate(instruction));
break;
case opcode_for("add"):
do_arithmetic<bigAdd>(bip.res);
break;
case opcode_for("sub"):
do_arithmetic<bigSub>(bip.res);
break;
case opcode_for("mul"):
do_arithmetic<bigMul>(bip.res);
break;
case opcode_for("div"):
do_arithmetic<bigDiv>(bip.res);
break;
case opcode_for("mod"):
do_arithmetic<bigDiv>(bip.rem);
break;
case opcode_for("rdint"): {
bigRead(stdin);
push() = reinterpret_cast<ObjRef>(bip.res);
break;
}
case opcode_for("wrint"):
bip.op1 = pop().as_reference();
bigPrint(stdout);
break;
case opcode_for("rdchr"): {
int32_t input;
std::cin >> reinterpret_cast<char &>(input);
push() = newNinjaInteger(input);
break;
}
case opcode_for("wrchr"):
bip.op1 = pop().as_reference();
std::cout << static_cast<char>(bigToInt());
break;
case opcode_for("pushg"):
push() = static_data.at(get_immediate(instruction));
break;
case opcode_for("popg"):
static_data.at(get_immediate(instruction)) = pop().as_reference();
break;
case opcode_for("asf"): {
immediate_t size = get_immediate(instruction);
if (size < 0) throw std::invalid_argument("Frame size can't be negative.");
push() = fp;
fp = sp;
while (size--) { // Initialize stack frame.
push() = nil;
}
break;
}
case opcode_for("rsf"):
sp = fp;
fp = pop().as_primitive();
break;
case opcode_for("pushl"):
push() = stack.at(fp + get_immediate(instruction)).as_reference();
break;
case opcode_for("popl"):
stack.at(fp + get_immediate(instruction)) = pop().as_reference();
break;
case opcode_for("eq"):
do_comparison<std::equal_to<int>>();
break;
case opcode_for("ne"):
do_comparison<std::not_equal_to<int>>();
break;
case opcode_for("lt"):
do_comparison<std::less<int>>();
break;
case opcode_for("le"):
do_comparison<std::less_equal<int>>();
break;
case opcode_for("gt"):
do_comparison<std::greater<int>>();
break;
case opcode_for("ge"):
do_comparison<std::greater_equal<int>>();
break;
case opcode_for("jmp"):
pc = get_immediate(instruction);
break;
case opcode_for("brf"):
bip.op1 = pop().as_reference();
if (bigToInt() == 0) pc = get_immediate(instruction);
break;
case opcode_for("brt"):
bip.op1 = pop().as_reference();
if (bigToInt() != 0) pc = get_immediate(instruction);
break;
case opcode_for("call"):
push() = pc;
pc = get_immediate(instruction);
break;
case opcode_for("ret"):
pc = pop().as_primitive();
break;
case opcode_for("drop"): {
immediate_t size = get_immediate(instruction);
if (size < 0) throw std::invalid_argument("Frame size can't be negative.");
if (static_cast<uint32_t>(size) > stack.size())
throw std::overflow_error("Not enough elements on the stack for drop.");
sp -= size;
break;
}
case opcode_for("pushr"):
push() = ret;
ret = nil;
break;
case opcode_for("popr"):
ret = pop().as_reference();
break;
case opcode_for("dup"): {
ObjRef duplicated = stack.at(sp - 1).as_reference();
push() = duplicated;
break;
}
case opcode_for("new"): {
push() = newNinjaObject(get_immediate(instruction));
break;
}
case opcode_for("getf"): {
ObjRef record = pop().as_reference();
immediate_t member = get_immediate(instruction);
push() = try_access_member(record, member);
break;
}
case opcode_for("putf"): {
ObjRef value = pop().as_reference();
ObjRef record = pop().as_reference();
immediate_t member = get_immediate(instruction);
try_access_member(record, member) = value;
break;
}
case opcode_for("newa"): {
bip.op1 = pop().as_reference();
push() = newNinjaObject(bigToInt());
break;
}
case opcode_for("getfa"): {
bip.op1 = pop().as_reference();
ObjRef array = pop().as_reference();
push() = try_access_member(array, bigToInt());
break;
}
case opcode_for("putfa"): {
ObjRef value = pop().as_reference();
bip.op1 = pop().as_reference();
ObjRef array = pop().as_reference();
try_access_member(array, bigToInt()) = value;
break;
}
case opcode_for("getsz"): {
ObjRef reference = pop().as_reference();
if (reference != nil && reference->is_compound()) {
push() = newNinjaInteger(reference->get_size());
} else {
push() = newNinjaInteger(-1);
}
break;
}
case opcode_for("pushn"):
push() = nil;
break;
case opcode_for("refeq"): {
bool result = pop().as_reference() == pop().as_reference();
push() = newNinjaInteger(result);
break;
}
case opcode_for("refne"): {
bool result = pop().as_reference() != pop().as_reference();
push() = newNinjaInteger(result);
break;
}
default: {
std::stringstream ss;
ss << "Opcode " << static_cast<int>(get_opcode(instruction))
<< " does not reference a known instruction.";
throw std::invalid_argument(ss.str());
}
}
return true;
}
}