-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoil.c
485 lines (453 loc) · 14.4 KB
/
soil.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#define MEMORY_SIZE 1000000000
#define TRACE_INSTRUCTIONS 1
#define TRACE_CALLS 0
#define TRACE_CALL_ARGS 0
#define TRACE_SYSCALLS 1
void eprintf(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
void panic(int exit_code, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(exit_code);
}
typedef uint8_t Byte;
typedef int64_t Word;
Word reg[8]; // sp, st, a, b, c, d, e, f
#define SP reg[0]
#define ST reg[1]
#define REGA reg[2]
#define REGB reg[3]
#define REGC reg[4]
#define REGD reg[5]
#define REGE reg[6]
#define REGF reg[7]
Byte* byte_code;
Word ip;
Byte* mem;
Word call_stack[1024];
Word call_stack_len;
typedef struct { Word catch; Word call_stack_len; Word sp; } Try;
Try try_stack[1024];
Word try_stack_len;
void (*syscall_handlers[256])();
typedef struct { int pos; char* label; int len; } LabelAndPos;
typedef struct { LabelAndPos* entries; int len; } Labels;
Labels labels;
LabelAndPos find_label(Word pos) {
for (int j = labels.len - 1; j >= 0; j--)
if (labels.entries[j].pos <= pos)
return labels.entries[j];
LabelAndPos lap;
lap.pos = 0;
lap.len = 0;
return lap;
}
void print_stack_entry(Word pos) {
eprintf("%8lx ", pos);
for (int j = labels.len - 1; j >= 0; j--)
if (labels.entries[j].pos <= pos) {
for (int k = 0; k < labels.entries[j].len; k++)
eprintf("%c", labels.entries[j].label[k]);
break;
}
eprintf("\n");
}
void dump_and_panic(char* fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
eprintf("\n");
eprintf("Stack:\n");
for (int i = 0; i < call_stack_len; i++) print_stack_entry(call_stack[i] - 1);
print_stack_entry(ip);
eprintf("\n");
eprintf("Registers:\n");
eprintf("sp = %8ld %8lx\n", SP, SP);
eprintf("st = %8ld %8lx\n", ST, ST);
eprintf("a = %8ld %8lx\n", REGA, REGA);
eprintf("b = %8ld %8lx\n", REGB, REGB);
eprintf("c = %8ld %8lx\n", REGC, REGC);
eprintf("d = %8ld %8lx\n", REGD, REGD);
eprintf("e = %8ld %8lx\n", REGE, REGE);
eprintf("f = %8ld %8lx\n", REGF, REGF);
eprintf("\n");
FILE* dump = fopen("crash", "w+");
fwrite(mem, 1, MEMORY_SIZE, dump);
fclose(dump);
eprintf("Memory dumped to crash.\n");
exit(1);
}
void init_syscalls(void);
void init_vm(Byte* bin, int bin_len) {
for (int i = 0; i < 8; i++) reg[i] = 0;
SP = MEMORY_SIZE;
byte_code = 0;
ip = 0;
mem = malloc(MEMORY_SIZE);
call_stack_len = 0;
init_syscalls();
int cursor = 0;
#define EAT_BYTE ({ \
if (cursor >= bin_len) panic(1, "binary incomplete"); \
Byte byte = bin[cursor]; \
cursor++; \
byte; \
})
#define EAT_WORD ({ \
if (cursor > bin_len - 8) panic(1, "binary incomplete"); \
Word word; \
for (int i = 7; i >= 0; i--) word = (word << 8) + bin[cursor + i]; \
cursor += 8; \
word; \
})
#define CHECK_MAGIC_BYTE(c) \
if (EAT_BYTE != c) panic(1, "magic bytes don't match");
CHECK_MAGIC_BYTE('s')
CHECK_MAGIC_BYTE('o')
CHECK_MAGIC_BYTE('i')
CHECK_MAGIC_BYTE('l')
while (cursor < bin_len) {
int section_type = EAT_BYTE;
int section_len = EAT_WORD;
if (section_type == 0) {
// byte code
byte_code = malloc(section_len);
for (int j = 0; j < section_len; j++) byte_code[j] = EAT_BYTE;
} else if (section_type == 1) {
// initial memory
if (section_len >= MEMORY_SIZE) panic(1, "initial memory too big");
for (int j = 0; j < section_len; j++) mem[j] = EAT_BYTE;
} else if (section_type == 3) {
// debug info
labels.len = EAT_WORD;
labels.entries = malloc(sizeof(LabelAndPos) * labels.len);
for (int i = 0; i < labels.len; i++) {
labels.entries[i].pos = EAT_WORD;
labels.entries[i].len = EAT_WORD;
labels.entries[i].label = bin + cursor;
cursor += labels.entries[i].len;
}
} else {
cursor += section_len;
}
}
// eprintf("Memory:");
// for (int i = 0; i < MEMORY_SIZE; i++) eprintf(" %02x", mem[i]);
// eprintf("\n");
}
void dump_reg(void) {
eprintf(
"ip = %lx, sp = %lx, st = %lx, a = %lx, b = %lx, c = %lx, d = %lx, e = "
"%lx, f = %lx\n", ip, SP, ST, REGA, REGB, REGC, REGD, REGE, REGF);
}
typedef Byte Reg; // 4 bits would actually be enough, but meh
typedef union { double f; int64_t i; } fi;
void run_single(void) {
#define REG1 reg[byte_code[ip + 1] & 0x0f]
#define REG2 reg[byte_code[ip + 1] >> 4]
Byte opcode = byte_code[ip];
switch (opcode) {
case 0x00: ip += 1; break; // nop
case 0xe0: { // panic
if (try_stack_len > 0) {
try_stack_len--;
call_stack_len = try_stack[try_stack_len].call_stack_len;
ip = try_stack[try_stack_len].catch;
} else {
dump_and_panic("panicked"); return;
}
}
case 0xe1: { // trystart
Word catch = *(Word*)(byte_code + ip + 1);
try_stack[try_stack_len].catch = catch;
try_stack[try_stack_len].call_stack_len = call_stack_len;
try_stack[try_stack_len].sp = SP;
try_stack_len++;
ip += 9; break;
}
case 0xe2: try_stack_len--; ip += 1; break; // tryend
case 0xd0: REG1 = REG2; ip += 2; break; // move
case 0xd1: REG1 = *(Word*)(byte_code + ip + 2); ip += 10; break; // movei
case 0xd2: REG1 = byte_code[ip + 2]; ip += 3; break; // moveib
case 0xd3: { // load
if (REG2 >= MEMORY_SIZE) dump_and_panic("invalid load");
REG1 = *(Word*)(mem + REG2); ip += 2; break;
}
case 0xd4: { // loadb
if (REG2 >= MEMORY_SIZE) dump_and_panic("invalid loadb");
REG1 = mem[REG2]; ip += 2; break;
}
case 0xd5: { // store
if (REG1 >= MEMORY_SIZE) dump_and_panic("invalid store");
*(Word*)(mem + REG1) = REG2; ip += 2; break;
}
case 0xd6: { // storeb
if (REG1 >= MEMORY_SIZE) dump_and_panic("invalid storeb");
mem[REG1] = REG2; ip += 2; break;
}
case 0xd7: SP -= 8; *(Word*)(mem + SP) = REG1; ip += 2; break; // push
case 0xd8: REG1 = *(Word*)(mem + SP); SP += 8; ip += 2; break; // pop
case 0xf0: ip = *(Word*)(byte_code + ip + 1); break; // jump
case 0xf1: { // cjump
if (ST != 0) ip = *(Word*)(byte_code + ip + 1); else ip += 9; break;
}
case 0xf2: { // call
if (TRACE_CALLS) {
for (int i = 0; i < call_stack_len; i++)
eprintf(" ");
LabelAndPos lap = find_label(*(Word*)(byte_code + ip + 1));
for (int i = 0; i < lap.len; i++) eprintf("%c", lap.label[i]);
if (TRACE_CALL_ARGS) {
for (int i = call_stack_len + lap.len; i < 50; i++) eprintf(" ");
for (int i = SP; i < MEMORY_SIZE && i < SP + 40; i++) {
if (i % 8 == 0) eprintf(" |");
eprintf(" %02x", mem[i]);
}
}
eprintf("\n");
}
Word return_target = ip + 9;
call_stack[call_stack_len] = return_target; call_stack_len++;
ip = *(Word*)(byte_code + ip + 1); break;
}
case 0xf3: { // ret
call_stack_len--;
ip = call_stack[call_stack_len];
break;
}
case 0xf4: ip += 2; syscall_handlers[byte_code[ip - 1]](); break; // syscall
case 0xc0: ST = REG1 - REG2; ip += 2; break; // cmp
case 0xc1: ST = ST == 0 ? 1 : 0; ip += 1; break; // isequal
case 0xc2: ST = ST < 0 ? 1 : 0; ip += 1; break; // isless
case 0xc3: ST = ST > 0 ? 1 : 0; ip += 1; break; // isgreater
case 0xc4: ST = ST <= 0 ? 1 : 0; ip += 1; break; // islessequal
case 0xc5: ST = ST >= 0 ? 1 : 0; ip += 1; break; // isgreaterequal
case 0xc6: ST = ST != 0 ? 1 : 0; ip += 1; break; // isnotequal
case 0xc7: { // fcmp
fi fi1 = {.i = REG1};
fi fi2 = {.i = REG2};
fi res = {.f = fi1.f - fi2.f};
ST = res.i; ip += 2; break;
}
case 0xc8: { // fisequal
fi fi = {.i = ST};
ST = fi.f == 0.0 ? 1 : 0; ip += 1; break;
}
case 0xc9: { // fisless
fi fi = {.i = ST};
ST = fi.f < 0.0 ? 1 : 0; ip += 1; break;
}
case 0xca: { // fisgreater
fi fi = {.i = ST};
ST = fi.f > 0.0 ? 1 : 0; ip += 1; break;
}
case 0xcb: { // fislessqual
fi fi = {.i = ST};
ST = fi.f <= 0.0 ? 1 : 0; ip += 1; break;
}
case 0xcc: { // fisgreaterequal
fi fi = {.i = ST};
ST = fi.f >= 0.0 ? 1 : 0; ip += 1; break;
}
case 0xcd: { // fisnotequal
fi fi = {.i = ST};
ST = fi.f != 0.0 ? 1 : 0; ip += 1; break;
}
case 0xce: { // inttofloat
fi fi = {.f = (double)REG1};
REG1 = fi.i; ip += 2; break;
}
case 0xcf: { // floattoint
fi fi = {.i = REG1};
REG1 = (int64_t)fi.f; ip += 2; break;
}
case 0xa0: REG1 += REG2; ip += 2; break; // add
case 0xa1: REG1 -= REG2; ip += 2; break; // sub
case 0xa2: REG1 *= REG2; ip += 2; break; // mul
case 0xa3: { // div
if (REG2 == 0) dump_and_panic("div by zero");
REG1 /= REG2; ip += 2; break;
}
case 0xa4: { // rem
if (REG2 == 0) dump_and_panic("rem by zero");
REG1 %= REG2; ip += 2; break;
}
case 0xa5: { // fadd
fi fi1 = {.i = REG1};
fi fi2 = {.i = REG2};
fi res = {.f = fi1.f + fi2.f};
REG1 = res.i; ip += 2; break;
}
case 0xa6: { // fsub
fi fi1 = {.i = REG1};
fi fi2 = {.i = REG2};
fi res = {.f = fi1.f - fi2.f};
REG1 = res.i; ip += 2; break;
}
case 0xa7: { // fmul
fi fi1 = {.i = REG1};
fi fi2 = {.i = REG2};
fi res = {.f = fi1.f * fi2.f};
REG1 = res.i; ip += 2; break;
}
case 0xa8: { // fdiv
fi fi1 = {.i = REG1};
fi fi2 = {.i = REG2};
if (fi2.f == 0.0) dump_and_panic("fdiv by zero");
fi res = {.f = fi1.f / fi2.f};
REG1 = res.i; ip += 2; break;
}
case 0xb0: REG1 &= REG2; ip += 2; break; // and
case 0xb1: REG1 |= REG2; ip += 2; break; // or
case 0xb2: REG1 ^= REG2; ip += 2; break; // xor
case 0xb3: REG1 = ~REG1; ip += 2; break; // not
default: dump_and_panic("invalid instruction %dx", opcode); return;
}
if (TRACE_INSTRUCTIONS) {
eprintf("ran %x -> ", opcode);
dump_reg();
}
}
void run(void) {
for (int i = 0; 1; i++) {
// dump_reg();
// eprintf("Memory:");
// for (int i = 0x18650; i < MEMORY_SIZE; i++)
// eprintf("%c%02x", i == SP ? '|' : ' ', mem[i]);
// eprintf("\n");
run_single();
}
}
void syscall_none(void) { dump_and_panic("invalid syscall number"); }
void syscall_exit(void) {
if (TRACE_SYSCALLS) eprintf("syscall exit(%ld)\n", REGA);
eprintf("exited with %ld\n", REGA);
exit(REGA);
}
void syscall_print(void) {
if (TRACE_SYSCALLS) eprintf("syscall print(%lx, %ld)\n", REGA, REGB);
for (int i = 0; i < REGB; i++) printf("%c", mem[REGA + i]);
if (TRACE_CALLS || TRACE_SYSCALLS) eprintf("\n");
}
void syscall_log(void) {
if (TRACE_SYSCALLS) eprintf("syscall log(%lx, %ld)\n", REGA, REGB);
for (int i = 0; i < REGB; i++) eprintf("%c", mem[REGA + i]);
if (TRACE_CALLS || TRACE_SYSCALLS) eprintf("\n");
}
void syscall_create(void) {
if (TRACE_SYSCALLS) eprintf("syscall create(%lx, %ld)\n", REGA, REGB);
char filename[REGB + 1];
for (int i = 0; i < REGB; i++) filename[i] = mem[REGA + i];
filename[REGB] = 0;
REGA = (Word)fopen(filename, "w+");
}
void syscall_open_reading(void) {
if (TRACE_SYSCALLS) eprintf("syscall open_reading(%lx, %ld)\n", REGA, REGB);
char filename[REGB + 1];
for (int i = 0; i < REGB; i++) filename[i] = mem[REGA + i];
filename[REGB] = 0;
REGA = (Word)fopen(filename, "r");
}
void syscall_open_writing(void) {
if (TRACE_SYSCALLS) eprintf("syscall open_writing(%lx, %ld)\n", REGA, REGB);
char filename[REGB + 1];
for (int i = 0; i < REGB; i++) filename[i] = mem[REGA + i];
filename[REGB] = 0;
REGA = (Word)fopen(filename, "w+");
}
void syscall_read(void) {
if (TRACE_SYSCALLS) eprintf("syscall read(%ld, %lx, %ld)\n", REGA, REGB, REGC);
REGA = fread(mem + REGB, 1, REGC, (FILE*)REGA);
}
void syscall_write(void) {
if (TRACE_SYSCALLS)
eprintf("syscall write(%ld, %lx, %ld)\n", REGA, REGB, REGC);
// TODO: assert that this worked
fwrite(mem + REGB, 1, REGC, (FILE*)REGA);
}
void syscall_close(void) {
if (TRACE_SYSCALLS) eprintf("syscall close(%ld)\n", REGA);
// TODO: assert that this worked
fclose((FILE*)REGA);
}
int global_argc;
void syscall_argc(void) {
if (TRACE_SYSCALLS) eprintf("syscall argc()\n");
REGA = global_argc - 1;
}
char** global_argv;
void syscall_arg(void) {
if (TRACE_SYSCALLS) eprintf("syscall arg(%ld, %lx, %ld)\n", REGA, REGB, REGC);
if (REGA < 0 || REGA >= global_argc) dump_and_panic("arg index out of bounds");
char* arg = REGA == 0 ? global_argv[0] : global_argv[REGA + 1];
int len = strlen(arg);
int written = len > REGC ? REGC : len;
for (int i = 0; i < written; i++) mem[REGB + i] = arg[i];
REGA = written;
}
void syscall_read_input(void) {
if (TRACE_SYSCALLS)
eprintf("syscall read_input(%lx, %ld)\n", REGA, REGB);
REGA = read(0, mem + REGA, REGB);
}
void syscall_execute(void) {
if (TRACE_SYSCALLS)
eprintf("syscall execute(%lx, %ld)\n", REGA, REGB);
int len = REGB;
Byte* bin = (Byte*)malloc(len);
if (bin == NULL) panic(2, "out of memory");
memcpy(bin, mem + REGA, len);
init_vm(bin, len);
}
void syscall_instant_now(void) {
if (TRACE_SYSCALLS) eprintf("syscall instant_now()\n");
REGA = 0;
}
void init_syscalls(void) {
for (int i = 0; i < 256; i++) syscall_handlers[i] = syscall_none;
syscall_handlers[0] = syscall_exit;
syscall_handlers[1] = syscall_print;
syscall_handlers[2] = syscall_log;
syscall_handlers[3] = syscall_create;
syscall_handlers[4] = syscall_open_reading;
syscall_handlers[5] = syscall_open_writing;
syscall_handlers[6] = syscall_read;
syscall_handlers[7] = syscall_write;
syscall_handlers[8] = syscall_close;
syscall_handlers[9] = syscall_argc;
syscall_handlers[10] = syscall_arg;
syscall_handlers[11] = syscall_read_input;
syscall_handlers[12] = syscall_execute;
syscall_handlers[16] = syscall_instant_now;
}
int main(int argc, char** argv) {
global_argc = argc;
global_argv = argv;
if (argc < 2) panic(1, "Usage: %s <file> [<args>]\n", argv[0]);
FILE* file = fopen(argv[1], "rb");
if (file == NULL) panic(3, "couldn't open file %s", argv[1]);
fseek(file, 0L, SEEK_END);
size_t len = ftell(file);
rewind(file);
Byte *bin = (Byte*) malloc(len + 1);
if (bin == NULL) panic(2, "out of memory");
size_t read = fread(bin, sizeof(char), len, file);
if (read != len) panic(4, "file size changed after fseek");
bin[read] = 0;
fclose(file);
init_vm(bin, len);
run();
}