-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.cpp
265 lines (219 loc) · 9.16 KB
/
code.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
#include "code.hpp"
// Initialise global Definitions
std::map<OpType, std::shared_ptr<Definition>> definitions = {
{OpType::OpConstant, new_definition("OpConstant", 2)},
{OpType::OpAdd, new_definition("OpAdd")},
{OpType::OpSub, new_definition("OpSub")},
{OpType::OpMul, new_definition("OpMul")},
{OpType::OpDiv, new_definition("OpDiv")},
{OpType::OpPop, new_definition("OpPop")},
{OpType::OpTrue, new_definition("OpTrue")},
{OpType::OpFalse, new_definition("OpFalse")},
{OpType::OpEqual, new_definition("OpEqual")},
{OpType::OpNotEqual, new_definition("OpNotEqual")},
{OpType::OpGreaterThan, new_definition("OpGreaterThan")},
{OpType::OpMinus, new_definition("OpMinus")},
{OpType::OpBang, new_definition("OpBang")},
{OpType::OpJumpNotTruthy, new_definition("OpJumpNotTruthy", 2)},
{OpType::OpJump, new_definition("OpJump", 2)},
{OpType::OpNull, new_definition("OpNull")},
{OpType::OpSetGlobal, new_definition("OpSetGlobal", 2)},
{OpType::OpGetGlobal, new_definition("OpGetGlobal", 2)},
{OpType::OpArray, new_definition("OpArray", 2)},
{OpType::OpHash, new_definition("OpHash", 2)},
{OpType::OpIndex, new_definition("OpIndex")},
{OpType::OpCall, new_definition("OpCall", 1)},
{OpType::OpReturnValue, new_definition("OpReturnValue")},
{OpType::OpReturn, new_definition("OpReturn")},
{OpType::OpSetLocal, new_definition("OpSetLocal", 1)},
{OpType::OpGetLocal, new_definition("OpGetLocal", 1)},
{OpType::OpGetBuiltin, new_definition("OpGetBuiltin", 1)},
{OpType::OpClosure, new_definition("OpClosure", 2, 1)},
{OpType::OpGetFree, new_definition("OpGetFree", 1)},
{OpType::OpCurrentClosure, new_definition("OpCurrentClosure")},
};
Definition::Definition(std::string name) : name{name}, operand_widths{} {}
std::shared_ptr<Definition> new_definition(std::string name) {
return std::make_shared<Definition>(Definition(name));
}
std::shared_ptr<Definition> new_definition(std::string name, int operand_width) {
auto definition = std::make_shared<Definition>(Definition{name});
definition->operand_widths.push_back(operand_width);
return definition;
}
std::shared_ptr<Definition> new_definition(std::string name, int first_operand_width, int second_operand_width) {
auto definition = std::make_shared<Definition>(Definition{name});
definition->operand_widths.push_back(first_operand_width);
definition->operand_widths.push_back(second_operand_width);
return definition;
}
std::tuple<std::shared_ptr<Definition>, bool> lookup(const OpType &op)
{
auto contains = definitions.find(op);
// If OpType is not defined, then return false to indicate "not ok"
if (contains == definitions.end()) {
// TODO: should we instead define and return an "error" object with message instead of boolean?
//std::cerr << "opcode undefined." << std::endl;
return std::make_tuple(nullptr, false);
}
return std::make_tuple(definitions[op], true);
}
std::string fmt_instruction(std::shared_ptr<Definition> def, std::vector<int> operands) {
auto operand_count = def->operand_widths.size();
if (operands.size() != operand_count) {
return "ERROR: operand length " + std::to_string(operands.size()) + "does not match defined" + std::to_string(operand_count) + "\n";
}
switch (operand_count) {
case 0:
return def->name;
case 1:
return def->name + " " + std::to_string(operands.at(0));
case 2:
return def->name + " " + std::to_string(operands.at(0)) +
+ " " + std::to_string(operands.at(1));
default:
return "ERROR: unhandled operand_count for " + def->name;
}
}
std::ostream& operator<<(std::ostream& out, const Instructions& ins) {
int i = 0;
out.fill('0');
while (i < static_cast<int>(ins.size())) {
auto[def, ok] = lookup(static_cast<OpType>(ins.at(i)));
if (!ok) {
// TODO: if lookup defined a range of error messages, then remove hard-coded message here.
out << std::string("ERROR: opcode undefined.") << std::endl;
continue;
}
Instructions slice = Instructions(ins.begin() + i + 1, ins.end());
auto[operands, read] = read_operands(def, slice);
// Write instruction in this form: "0003 OpConstant 2"
out << std::setw(4) << i << " ";
out << fmt_instruction(def, operands) << std::endl;
i += (1 + read);
}
return out;
}
Instructions make(OpType op) {
auto [def, ok] = lookup(op);
if (!ok) {
std::cerr << "fatal error: " << def->name << " instruction not defined." << std::endl;
throw std::exception();
}
int instruction_len = std::accumulate(def->operand_widths.begin(), def->operand_widths.end(), 1);
// Ensure that instruction length is correct (should have no additional operands)
if (instruction_len != 1) {
std::cerr << "fatal error: unexpectedly received operands for an " << def->name << " instruction." << std::endl;
throw std::exception();
}
std::vector<uint8_t> instruction = {as_opcode(op)};
return instruction;
}
Instructions make(OpType op, int operand) {
auto [def, ok] = lookup(op);
if (!ok) {
std::cerr << "fatal error: " << def->name << " instruction not defined." << std::endl;
throw std::exception();
}
int instruction_len = std::accumulate(def->operand_widths.begin(), def->operand_widths.end(), 1);
std::vector<uint8_t> instruction(instruction_len);
instruction[0] = as_opcode(op);
int offset = 1;
auto width = def->operand_widths.at(0);
switch (width) {
// Store 16-bit instruction as 2 bytes (big endian byte order)
case 2:
instruction[offset] = static_cast<uint16_t>(operand) >> 8;
instruction[offset + 1] = static_cast<uint16_t>(operand) & 0xFF;
break;
// Store 8-bit instruction directly in a single byte
case 1:
instruction[offset] = static_cast<uint8_t>(operand);
break;
default:
break;
}
return instruction;
}
Instructions make(OpType op, int first_operand, int second_operand) {
auto [def, ok] = lookup(op);
if (!ok) {
std::cerr << "fatal error: " << def->name << " instruction not defined." << std::endl;
throw std::exception();
}
if (def->operand_widths.size() != 2) {
std::cerr << "fatal error: unexpectedly received 2 operands for an " << def->name << " instruction." << std::endl;
throw std::exception();
}
int instruction_len = std::accumulate(def->operand_widths.begin(), def->operand_widths.end(), 1);
std::vector<uint8_t> instruction(instruction_len);
instruction[0] = as_opcode(op);
int offset = 1;
int width;
// Process first operand
width = def->operand_widths.at(0);
switch (width) {
// Store 16-bit instruction as 2 bytes (big endian byte order)
case 2:
instruction[offset] = static_cast<uint16_t>(first_operand) >> 8;
instruction[offset + 1] = static_cast<uint16_t>(first_operand) & 0xFF;
offset += 2;
break;
// Store 8-bit instruction directly in a single byte
case 1:
instruction[offset] = static_cast<uint8_t>(first_operand);
offset += 1;
break;
default:
break;
}
// Process second operand
width = def->operand_widths.at(1);
switch (width) {
// Store 16-bit instruction as 2 bytes (big endian byte order)
case 2:
instruction[offset] = static_cast<uint16_t>(second_operand) >> 8;
instruction[offset + 1] = static_cast<uint16_t>(second_operand) & 0xFF;
offset += 2;
break;
// Store 8-bit instruction directly in a single byte
case 1:
instruction[offset] = static_cast<uint8_t>(second_operand);
offset += 1;
break;
default:
break;
}
return instruction;
}
int read_uint_8(Instructions ins, int offset) {
return static_cast<int>(ins[offset]);
}
int read_uint_16(Instructions ins, int offset) {
// Interpret 2 sequential bytes (big endian) as a 16-bit int
auto hi = static_cast<uint16_t>(ins[offset]) << 8;
auto lo = static_cast<uint16_t>(ins[offset+1]);
return static_cast<int>(lo | hi);
}
std::tuple<std::vector<int>, int> read_operands(std::shared_ptr<Definition> def, Instructions ins) {
// Initialise vector<int> to be of same size as def->operands_widths
std::vector<int> operands(def->operand_widths.size());
int offset = 0;
for (int i = 0; i < static_cast<int>(def->operand_widths.size()); i++) {
auto width = def->operand_widths.at(i);
switch (width) {
// Read 2 byte instruction (big endian byte order)
case 2:
operands[i] = read_uint_16(ins, offset);
break;
// Read 1 byte instruction
case 1:
operands[i] = read_uint_8(ins, offset);
break;
default:
break;
}
offset += width;
}
return std::make_tuple(operands, offset);
}