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
|
#include <lib94/lib94.hpp>
#include <functional>
#include <cassert>
#include <cstdio>
namespace lib94 {
void enqueue_process(number_t pc);
extern instruction core[LIB94_CORE_SIZE];
extern number_t program_counter;
extern instruction a_instruction, b_instruction;
extern instruction *a_instruction_writable;
extern instruction *b_instruction_writable;
void add_written_instruction(const instruction *instr);
void add_read_instruction(const instruction *instr);
#define maybe_add_written_instruction(instr) \
if constexpr (uas) add_written_instruction(instr)
#define maybe_add_read_instruction(instr) \
if constexpr (uas) add_read_instruction(instr)
struct single_target {
number_t *ptr;
inline bool assign(const single_target &left, const single_target &right,
std::function<std::optional<number_t> (number_t, number_t)> f) {
std::optional<number_t> result = f(*left.ptr, *right.ptr);
if (result.has_value()) {
*ptr = result.value();
return true;
}
return false;
}
inline bool is_zero() {
return *ptr == 0;
}
inline bool dec_not_zero() {
*ptr = (*ptr + LIB94_CORE_SIZE - 1) % LIB94_CORE_SIZE;
return *ptr != 0;
}
inline bool equal_to(const single_target &other) {
return *ptr == *other.ptr;
}
inline bool less_than(const single_target &other) {
return *ptr < *other.ptr;
}
};
struct pair_target {
number_t *ptr1;
number_t *ptr2;
inline bool assign(const pair_target &left, const pair_target &right,
std::function<std::optional<number_t> (number_t, number_t)> f) {
bool to_return = true;
std::optional<number_t> result1 = f(*left.ptr1, *right.ptr1);
if (result1.has_value())
*ptr1 = result1.value();
else
to_return = false;
std::optional<number_t> result2 = f(*left.ptr2, *right.ptr2);
if (result2.has_value())
*ptr2 = result2.value();
else
to_return = false;
return to_return;
}
inline bool is_zero() {
return *ptr1 == 0 && *ptr2 == 0;
}
inline bool dec_not_zero() {
*ptr1 = (*ptr1 + LIB94_CORE_SIZE - 1) % LIB94_CORE_SIZE;
*ptr2 = (*ptr2 + LIB94_CORE_SIZE - 1) % LIB94_CORE_SIZE;
return *ptr1 != 0 || *ptr2 != 0;
}
inline bool equal_to(const pair_target &other) {
return *ptr1 == *other.ptr1 &&
*ptr2 == *other.ptr2;
}
inline bool less_than(const pair_target &other) {
return *ptr1 < *other.ptr1 &&
*ptr2 < *other.ptr2;
}
};
struct instruction_target {
instruction *instr;
inline bool assign(const instruction_target &left, const instruction_target &right,
std::function<std::optional<number_t> (number_t, number_t)> f) {
bool to_return = true;
std::optional<number_t> result1 = f(left.instr->anumber, right.instr->anumber);
if (result1.has_value())
instr->anumber = result1.value();
else
to_return = false;
std::optional<number_t> result2 = f(left.instr->bnumber, right.instr->bnumber);
if (result2.has_value())
instr->bnumber = result2.value();
else
to_return = false;
return to_return;
}
inline bool is_zero() {
return instr->anumber == 0 && instr->bnumber == 0;
}
inline bool dec_not_zero() {
instr->anumber = (instr->anumber + LIB94_CORE_SIZE - 1) % LIB94_CORE_SIZE;
instr->bnumber = (instr->bnumber + LIB94_CORE_SIZE - 1) % LIB94_CORE_SIZE;
return instr->anumber != 0 || instr->bnumber != 0;
}
inline bool equal_to(const instruction_target &other) {
return instr->op == other.instr->op &&
instr->mod == other.instr->mod &&
instr->amode == other.instr->amode &&
instr->anumber == other.instr->anumber &&
instr->bmode == other.instr->bmode &&
instr->bnumber == other.instr->bnumber;
}
inline bool less_than(const instruction_target &other) {
return instr->anumber < other.instr->anumber &&
instr->bnumber < other.instr->bnumber;
}
};
template<modifier mod>
struct target;
template<> struct target<A> : single_target {};
template<> struct target<B> : single_target {};
template<> struct target<AB> : single_target {};
template<> struct target<BA> : single_target {};
template<> struct target<F> : pair_target {};
template<> struct target<X> : pair_target {};
template<> struct target<I> : instruction_target {};
template<opcode op, modifier mod, bool uas>
bool execute() {
target<mod> dest_out;
target<mod> dest_in;
target<mod> src_in;
if constexpr (mod == A) {
dest_out.ptr = &b_instruction_writable->anumber;
dest_in.ptr = &b_instruction.anumber;
src_in.ptr = &a_instruction.anumber;
}
else if constexpr (mod == B) {
dest_out.ptr = &b_instruction_writable->bnumber;
dest_in.ptr = &b_instruction.bnumber;
src_in.ptr = &a_instruction.bnumber;
}
else if constexpr (mod == AB) {
dest_out.ptr = &b_instruction_writable->bnumber;
dest_in.ptr = &b_instruction.bnumber;
src_in.ptr = &a_instruction.anumber;
}
else if constexpr (mod == BA) {
dest_out.ptr = &b_instruction_writable->anumber;
dest_in.ptr = &b_instruction.anumber;
src_in.ptr = &a_instruction.bnumber;
}
else if constexpr (mod == F) {
dest_out.ptr1 = &b_instruction_writable->anumber;
dest_out.ptr2 = &b_instruction_writable->bnumber;
dest_in.ptr1 = &b_instruction.anumber;
dest_in.ptr2 = &b_instruction.bnumber;
src_in.ptr1 = &a_instruction.anumber;
src_in.ptr2 = &a_instruction.bnumber;
}
else if constexpr (mod == X) {
dest_out.ptr1 = &b_instruction_writable->anumber;
dest_out.ptr2 = &b_instruction_writable->bnumber;
dest_in.ptr1 = &b_instruction.anumber;
dest_in.ptr2 = &b_instruction.bnumber;
src_in.ptr1 = &a_instruction.bnumber;
src_in.ptr2 = &a_instruction.anumber;
}
else if constexpr (mod == I) {
dest_out.instr = b_instruction_writable;
dest_in.instr = &b_instruction;
src_in.instr = &a_instruction;
}
else assert(false);
if constexpr (op == DAT)
return false;
else if constexpr (op == MOV) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_written_instruction(b_instruction_writable);
if constexpr (mod == I) {
dest_out.instr->op = src_in.instr->op;
dest_out.instr->mod = src_in.instr->mod;
dest_out.instr->amode = src_in.instr->amode;
dest_out.instr->bmode = src_in.instr->bmode;
}
return dest_out.assign(dest_in, src_in, [](number_t a, number_t b) -> std::optional<number_t> {
(void)a;
return b;
});
}
else if constexpr (op == ADD) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_read_instruction(b_instruction_writable);
maybe_add_written_instruction(b_instruction_writable);
return dest_out.assign(dest_in, src_in, [](number_t a, number_t b) -> std::optional<number_t> {
return (a + b) % LIB94_CORE_SIZE;
});
}
else if constexpr (op == SUB) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_read_instruction(b_instruction_writable);
maybe_add_written_instruction(b_instruction_writable);
return dest_out.assign(dest_in, src_in, [](number_t a, number_t b) -> std::optional<number_t> {
return (a + LIB94_CORE_SIZE - b) % LIB94_CORE_SIZE;
});
}
else if constexpr (op == MUL) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_read_instruction(b_instruction_writable);
maybe_add_written_instruction(b_instruction_writable);
return dest_out.assign(dest_in, src_in, [](number_t a, number_t b) -> std::optional<number_t> {
return (a * b) % LIB94_CORE_SIZE;
});
}
else if constexpr (op == DIV) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_read_instruction(b_instruction_writable);
maybe_add_written_instruction(b_instruction_writable);
return dest_out.assign(dest_in, src_in, [](number_t a, number_t b) -> std::optional<number_t> {
if (b == 0)
return {};
return a / b;
});
}
else if constexpr (op == MOD) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_read_instruction(b_instruction_writable);
maybe_add_written_instruction(b_instruction_writable);
return dest_out.assign(dest_in, src_in, [](number_t a, number_t b) -> std::optional<number_t> {
if (b == 0)
return {};
return a % b;
});
}
else if constexpr (op == JMP) {
maybe_add_read_instruction(a_instruction_writable);
program_counter = (a_instruction_writable - core + LIB94_CORE_SIZE - 1) % LIB94_CORE_SIZE;
return true;
}
else if constexpr (op == JMZ) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_read_instruction(b_instruction_writable);
if (dest_in.is_zero())
program_counter = (a_instruction_writable - core + LIB94_CORE_SIZE - 1) % LIB94_CORE_SIZE;
return true;
}
else if constexpr (op == JMN) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_read_instruction(b_instruction_writable);
if (!dest_in.is_zero())
program_counter = (a_instruction_writable - core + LIB94_CORE_SIZE - 1) % LIB94_CORE_SIZE;
return true;
}
else if constexpr (op == DJN) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_read_instruction(b_instruction_writable);
maybe_add_written_instruction(b_instruction_writable);
if (dest_out.dec_not_zero())
program_counter = (a_instruction_writable - core + LIB94_CORE_SIZE - 1) % LIB94_CORE_SIZE;
return true;
}
else if constexpr (op == SEQ) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_read_instruction(b_instruction_writable);
if (src_in.equal_to(dest_in))
program_counter = (program_counter + 1) % LIB94_CORE_SIZE;
return true;
}
else if constexpr (op == SNE) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_read_instruction(b_instruction_writable);
if (!src_in.equal_to(dest_in))
program_counter = (program_counter + 1) % LIB94_CORE_SIZE;
return true;
}
else if constexpr (op == SLT) {
maybe_add_read_instruction(a_instruction_writable);
maybe_add_read_instruction(b_instruction_writable);
if (src_in.less_than(dest_in))
program_counter = (program_counter + 1) % LIB94_CORE_SIZE;
return true;
}
else if constexpr (op == SPL) {
maybe_add_read_instruction(a_instruction_writable);
enqueue_process((program_counter + 1) % LIB94_CORE_SIZE);
program_counter = (a_instruction_writable - core + LIB94_CORE_SIZE - 1) % LIB94_CORE_SIZE;
return true;
}
else if constexpr (op == NOP)
return true;
else assert(false);
}
#define EXECUTOR_INST_LINE(mod, uas) \
template bool execute<DAT, mod, uas>(); template bool execute<MOV, mod, uas>(); \
template bool execute<ADD, mod, uas>(); template bool execute<SUB, mod, uas>(); \
template bool execute<MUL, mod, uas>(); template bool execute<DIV, mod, uas>(); \
template bool execute<MOD, mod, uas>(); template bool execute<JMP, mod, uas>(); \
template bool execute<JMZ, mod, uas>(); template bool execute<JMN, mod, uas>(); \
template bool execute<DJN, mod, uas>(); template bool execute<SEQ, mod, uas>(); \
template bool execute<SNE, mod, uas>(); template bool execute<SLT, mod, uas>(); \
template bool execute<SPL, mod, uas>(); template bool execute<NOP, mod, uas>();
#define EXECUTOR_INST(uas) \
EXECUTOR_INST_LINE(A, uas) \
EXECUTOR_INST_LINE(B, uas) \
EXECUTOR_INST_LINE(AB, uas) \
EXECUTOR_INST_LINE(BA, uas) \
EXECUTOR_INST_LINE(F, uas) \
EXECUTOR_INST_LINE(X, uas) \
EXECUTOR_INST_LINE(I, uas)
EXECUTOR_INST(true)
EXECUTOR_INST(false)
}
|