-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterp.c
281 lines (272 loc) · 4.89 KB
/
interp.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
//#pragma pack(1)
typedef struct inst_t {
uint8_t type;
int16_t amt;
int16_t imm;
int16_t imm2;
int16_t imm3;
}
#ifndef NO_PACK
__attribute__((packed))
#endif
inst_t;
char* buff = NULL;
static inst_t dummy;
#define TYPE_END (((ptrdiff_t)&dummy.amt) - (ptrdiff_t)&dummy)
#define AMT_END (((ptrdiff_t)&dummy.imm) - (ptrdiff_t)&dummy)
#define IMM_END (((ptrdiff_t)&dummy.imm2) - (ptrdiff_t)&dummy)
#define IMM2_END (((ptrdiff_t)&dummy.imm3) - (ptrdiff_t)&dummy)
#define IMM3_END (sizeof(dummy))
int pc;
static void grow(int amt) {
static int prev = 0;
if (amt > prev) {
buff = realloc(buff, amt);
prev = amt;
}
}
static int push(inst_t* i, size_t sz) {
grow(pc+sz);
char* dest = buff+pc;
memcpy(dest, i, sz);
int ret = pc;
pc += sz;
return ret;
}
#define I_ZERO 0x80
#define I_LOOP_BACK 0x81
#define I_SHORT_SUM 0x82
#define I_SHORT_IN 0x83
#define I_SHORT_OUT 0x84
#define I_SHORT_ZERO 0x85
#define I_DIV_SET 0x86
#define I_OFF_SET 0x87
#define I_SHORT_SET 0x88
#define I_FULL_SET SET
void mkInsts(Node* n) {
inst_t inst;
//TODO why is this faster?
memset(&inst, 0, sizeof(inst));
if (n == NULL) {
return;
}
int sz = 0;
switch (n->type) {
case LOOP: {
sz = AMT_END;
int dst = pc+sz;
inst.type = n->type;
int offset = push(&inst, sz);
mkInsts(n->n[0].n);
inst_t* begin = (void*) (buff+offset);
begin->amt = pc+sz;
inst.type = I_LOOP_BACK;
inst.amt = dst;
push(&inst, sz);
}
break;
case STMTS:
mkInsts(n->n[0].n);
mkInsts(n->n[1].n);
break;
case SUM:
sz = IMM_END;
inst.type = n->type;
inst.amt = n->n[0].i;
inst.imm = n->n[1].i;
if (inst.imm == 0) {
inst.type = I_SHORT_SUM;
sz = AMT_END;
}
push(&inst, sz);
break;
case SHIFT:
inst.type = n->type;
inst.amt = n->n[0].i;
push(&inst, AMT_END);
break;
case OUT:
inst.amt = n->n[0].i;
if (inst.amt == 0) {
inst.type = I_SHORT_OUT;
push(&inst, TYPE_END);
} else {
inst.type = n->type;
push(&inst, AMT_END);
}
break;
case IN:
inst.amt = n->n[0].i;
if (inst.amt == 0) {
inst.type = I_SHORT_IN;
push(&inst, TYPE_END);
} else {
inst.type = n->type;
push(&inst, AMT_END);
}
break;
case SET: {
int off = n->n[0].i;
for (int z = 1; z < n->sz; z++) {
Point *p = n->n[z].p;
inst.amt = p->x;
inst.imm = p->y;
inst.imm2 = p->z;
inst.imm3 = off;
if (p->z == 1) {
if (off == 0) {
inst.type = I_SHORT_SET;
push(&inst, IMM_END);
} else {
inst.imm2 = off;
inst.type = I_OFF_SET;
push(&inst, IMM2_END);
}
} else {
if (off == 0) {
inst.type = I_DIV_SET;
push(&inst, IMM2_END);
} else {
inst.type = I_FULL_SET;
push(&inst, IMM3_END);
}
}
}
if (off == 0) {
inst.type = I_SHORT_ZERO;
push(&inst, TYPE_END);
} else {
inst.type = I_ZERO;
inst.amt = off;
push(&inst, AMT_END);
}
}
break;
}
}
void interpret(size_t end, CELL_T * m) {
size_t i = 0;
I = &i;
for (size_t k = 0; k < end;) {
inst_t* inst = (void*) (buff+k);
switch (inst->type) {
case LOOP: {
if (m[i] == 0) {
k = inst->amt;
continue;
}
k += AMT_END;
}
break;
case I_LOOP_BACK: {
if (m[i] != 0) {
k = inst->amt;
continue;
}
k += AMT_END;
}
break;
case I_SHORT_SUM:
m[i] += inst->amt;
k += AMT_END;
break;
case SUM: {
int off = inst->imm;
m[i+off] += inst->amt;
k += IMM_END;
}
break;
case SHIFT:
i += inst->amt;
k += AMT_END;
break;
case I_SHORT_OUT:
putchar(m[i]);
fflush(stdout);
k += TYPE_END;
break;
case I_SHORT_IN:
m[i] = readChar(m[i]);
k += TYPE_END;
break;
case OUT: {
int off = inst->amt;
putchar(m[i+off]);
fflush(stdout);
k += AMT_END;
}
break;
case IN: {
int indx = i + inst->amt;
m[indx] = readChar(m[indx]);
k += AMT_END;
}
break;
case I_DIV_SET: {
int x = inst->amt;
int y = inst->imm;
int scale = inst->imm2;
m[i+x] += (y*m[i])/scale;
k += IMM2_END;
}
break;
case I_SHORT_SET: {
int x = inst->amt;
int y = inst->imm;
m[i+x] += (y*m[i]);
k += IMM_END;
}
break;
case I_FULL_SET: {
int x = inst->amt;
int y = inst->imm;
int scale = inst->imm2;
int off = inst->imm3;
m[i+x+off] += (y*m[i+off])/scale;
k += IMM3_END;
}
break;
case I_OFF_SET: {
int x = inst->amt;
int y = inst->imm;
int off = inst->imm2;
m[i+x+off] += (y*m[i+off]);
k += IMM2_END;
}
break;
case I_ZERO: {
int off = inst->amt;
m[i+off] = 0;
k += AMT_END;
}
break;
case I_SHORT_ZERO: {
m[i] = 0;
k += TYPE_END;
}
break;
default:
fprintf(stderr, "Internal error! unrecognized instruction %X\n", inst->type);
exit(1);
}
}
}
void interp() {
CELL_T m[numCells];
for (int z = 0; z < numCells; z++) {
m[z] = 0;
}
buff = malloc(0);
pc = 0;
size_t *isz = 0;
mkInsts(root);
size_t end = pc;
pc = 0;
interpret(end, m);
free(buff);
buff = NULL;
}