-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.c
88 lines (80 loc) · 1.9 KB
/
run.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
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include "stack.h"
#include "nooc.h"
#include "ir.h"
#include "util.h"
struct iproc *
findiproc(const struct toplevel *const toplevel, struct slice s)
{
for (size_t i = 0; i < toplevel->code.len; i++) {
if (slice_cmp(&toplevel->code.data[i].s, &s) == 0) {
return &toplevel->code.data[i];
}
}
return NULL;
}
void
runproc(struct iproc *proc)
{
uint64_t regs[32] = { 0 };
uint64_t curi = 0;
size_t localsize = 0;
void *locals = NULL;
size_t tmp;
while (1) {
switch (proc->data[curi].op) {
case IR_ASSIGN:
tmp = proc->data[curi].val;
curi++;
switch (proc->data[curi].op) {
case IR_ALLOC:
localsize += proc->data[curi].val;
locals = xrealloc(locals, localsize);
break;
case IR_IMM:
regs[proc->temps.data[tmp].reg] = proc->data[curi].val;
break;
default:
die("run: runproc: IR_ASSIGN: unhandled instruction");
}
break;
case IR_STORE:
size_t val = proc->data[curi].val;
curi++;
regs[proc->temps.data[proc->data[curi].val].reg] = val;
break;
case IR_CALL:
// syscall
if (proc->data[curi].val == 1) {
curi++;
assert(proc->data[curi].op == IR_CALLARG); // FIXME: skip return value for now
curi++;
assert(proc->data[curi].op == IR_CALLARG);
uint64_t arg2 = regs[proc->temps.data[proc->data[curi].val].reg];
curi++;
assert(proc->data[curi].op == IR_CALLARG);
uint64_t arg1 = regs[proc->temps.data[proc->data[curi].val].reg];
syscall(arg1, arg2);
}
break;
case IR_LABEL: // we already know where labels are from ir gen
break;
default:
die("run: runproc: unhandled instruction");
}
curi++;
}
}
void
run(const struct toplevel *const toplevel)
{
struct slice mainslice = {5, 4, "main\0" };
struct iproc *main = findiproc(toplevel, mainslice);
assert(main != NULL);
runproc(main);
}