-
Notifications
You must be signed in to change notification settings - Fork 12
/
predecoded.c
323 lines (304 loc) · 9.36 KB
/
predecoded.c
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
/* predecoded.c - a predecoded interpreter for a stack virtual machine.
Copyright (c) 2015, 2016 Grigory Rechistov. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of interpreters-comparison nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include "common.h"
static inline decode_t decode_at_address(const Instr_t* prog, uint32_t addr) {
assert(addr < PROGRAM_SIZE);
decode_t result = {0};
Instr_t raw_instr = prog[addr];
result.opcode = raw_instr;
switch (raw_instr) {
case Instr_Nop:
case Instr_Halt:
case Instr_Print:
case Instr_Swap:
case Instr_Dup:
case Instr_Inc:
case Instr_Add:
case Instr_Sub:
case Instr_Mul:
case Instr_Rand:
case Instr_Dec:
case Instr_Drop:
case Instr_Over:
case Instr_Mod:
case Instr_And:
case Instr_Or:
case Instr_Xor:
case Instr_SHL:
case Instr_SHR:
case Instr_Rot:
case Instr_SQRT:
case Instr_Pick:
result.length = 1;
break;
case Instr_Push:
case Instr_JNE:
case Instr_JE:
case Instr_Jump:
result.length = 2;
if (!(addr+1 < PROGRAM_SIZE)) {
result.length = 1;
result.opcode = Instr_Break;
break;
}
result.immediate = (int32_t)prog[addr+1];
break;
case Instr_Break:
default: /* Undefined instructions equal to Break */
result.length = 1;
result.opcode = Instr_Break;
break;
}
return result;
}
/*** Service routines ***/
#define BAIL_ON_ERROR() if (cpu.state != Cpu_Running) break;
static inline void push(cpu_t *pcpu, uint32_t v) {
assert(pcpu);
if (pcpu->sp >= STACK_CAPACITY-1) {
printf("Stack overflow\n");
pcpu->state = Cpu_Break;
return;
}
pcpu->stack[++pcpu->sp] = v;
}
static inline uint32_t pop(cpu_t *pcpu) {
assert(pcpu);
if (pcpu->sp < 0) {
printf("Stack underflow\n");
pcpu->state = Cpu_Break;
return 0;
}
return pcpu->stack[pcpu->sp--];
}
static inline uint32_t pick(cpu_t *pcpu, int32_t pos) {
assert(pcpu);
if (pcpu->sp - 1 < pos) {
printf("Out of bound picking\n");
pcpu->state = Cpu_Break;
return 0;
}
return pcpu->stack[pcpu->sp - pos];
}
static void predecode_program(const Instr_t *prog,
decode_t *dec, int len) {
assert(prog);
assert(dec);
/* The program is short, so we can decode it as a whole.
Otherwise, some sort of lazy decoding will be required */
for (int i=0; i < len; i++) {
dec[i] = decode_at_address(prog, i);
}
}
int main(int argc, char **argv) {
uint64_t steplimit = parse_args(argc, argv);
cpu_t cpu = init_cpu();
decode_t decoded_cache[PROGRAM_SIZE];
predecode_program(cpu.pmem, decoded_cache, PROGRAM_SIZE);
while (cpu.state == Cpu_Running && cpu.steps < steplimit) {
if (!(cpu.pc < PROGRAM_SIZE)) {
printf("PC out of bounds\n");
cpu.state = Cpu_Break;
break;
}
decode_t decoded = decoded_cache[cpu.pc];
uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;
/* Execute - a big switch */
switch(decoded.opcode) {
case Instr_Nop:
/* Do nothing */
break;
case Instr_Halt:
cpu.state = Cpu_Halted;
break;
case Instr_Push:
push(&cpu, decoded.immediate);
break;
case Instr_Print:
tmp1 = pop(&cpu); BAIL_ON_ERROR();
printf("[%d]\n", tmp1);
break;
case Instr_Swap:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1);
push(&cpu, tmp2);
break;
case Instr_Dup:
tmp1 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1);
push(&cpu, tmp1);
break;
case Instr_Over:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp2);
push(&cpu, tmp1);
push(&cpu, tmp2);
break;
case Instr_Inc:
tmp1 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1+1);
break;
case Instr_Add:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1 + tmp2);
break;
case Instr_Sub:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1 - tmp2);
break;
case Instr_Mod:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
BAIL_ON_ERROR();
if (tmp2 == 0) {
cpu.state = Cpu_Break;
break;
}
push(&cpu, tmp1 % tmp2);
break;
case Instr_Mul:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1 * tmp2);
break;
case Instr_Rand:
tmp1 = rand();
push(&cpu, tmp1);
break;
case Instr_Dec:
tmp1 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1-1);
break;
case Instr_Drop:
(void)pop(&cpu);
break;
case Instr_JE:
tmp1 = pop(&cpu);
BAIL_ON_ERROR();
if (tmp1 == 0)
cpu.pc += decoded.immediate;
break;
case Instr_JNE:
tmp1 = pop(&cpu);
BAIL_ON_ERROR();
if (tmp1 != 0)
cpu.pc += decoded.immediate;
break;
case Instr_Jump:
cpu.pc += decoded.immediate;
break;
case Instr_And:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1 & tmp2);
break;
case Instr_Or:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1 | tmp2);
break;
case Instr_Xor:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1 ^ tmp2);
break;
case Instr_SHL:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1 << tmp2);
break;
case Instr_SHR:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1 >> tmp2);
break;
case Instr_Rot:
tmp1 = pop(&cpu);
tmp2 = pop(&cpu);
tmp3 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, tmp1);
push(&cpu, tmp3);
push(&cpu, tmp2);
break;
case Instr_SQRT:
tmp1 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, sqrt(tmp1));
break;
case Instr_Pick:
tmp1 = pop(&cpu);
BAIL_ON_ERROR();
push(&cpu, pick(&cpu, tmp1));
break;
case Instr_Break:
cpu.state = Cpu_Break;
break;
default:
assert("Unreachable" && false);
break;
}
cpu.pc += decoded.length; /* Advance PC */
cpu.steps++;
}
assert(cpu.state != Cpu_Running || cpu.steps == steplimit);
/* Print CPU state */
printf("CPU executed %ld steps. End state \"%s\".\n",
cpu.steps, cpu.state == Cpu_Halted? "Halted":
cpu.state == Cpu_Running? "Running": "Break");
printf("PC = %#x, SP = %d\n", cpu.pc, cpu.sp);
printf("Stack: ");
for (int32_t i=cpu.sp; i >= 0 ; i--) {
printf("%#10x ", cpu.stack[i]);
}
printf("%s\n", cpu.sp == -1? "(empty)": "");
free(LoadedProgram);
return cpu.state == Cpu_Halted ||
(cpu.state == Cpu_Running &&
cpu.steps == steplimit)?0:1;
}