-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen_c.c
More file actions
368 lines (329 loc) · 11.8 KB
/
Copy pathcodegen_c.c
File metadata and controls
368 lines (329 loc) · 11.8 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "bft.h"
static void print_c_indent(FILE *output, const int level) {
for (int i = 0; i < level; i++) {
fprintf(output, " ");
}
}
static const i32 use_shift_and_mask = 0;
static const int inc_always_use_offset = 1;
static const int set_always_use_offset = 1;
static int get_shift(int n) {
if (!use_shift_and_mask) {
return -1;
}
if (n < 2) {
return -1;
}
if ((n & (n - 1)) != 0) {
return -1;
}
i32 shift = 0;
while ((1 << shift) < n) {
shift++;
}
return shift;
}
static void print_multiply_expr(FILE *output, const char *operand,
const int factor) {
const int factor_abs = abs(factor);
const int shift = get_shift(factor_abs);
if (factor < 0) {
fprintf(output, "-");
}
if (shift > 0) {
fprintf(output, "(%s << %d)", operand, shift);
} else if (factor_abs == 1) {
fprintf(output, "%s", operand);
} else {
fprintf(output, "%s * %d", operand, factor_abs);
}
}
void codegen_c(const Program *program, FILE *output) {
int *skip = calloc(program->size, sizeof(int));
if (!skip) {
fprintf(stderr, "Error: Could not allocate memory for skip array\n");
return;
}
for (addr_t i = 0; i < program->size; i++) {
const Instruction *instr = &program->instructions[i];
if (instr->op == OP_LOOP) {
const addr_t end_idx = instr->loop.match_addr;
if (end_idx > 0 && program->instructions[end_idx - 1].op == OP_RIGHT) {
skip[end_idx - 1] = 1;
if (i > 0 && program->instructions[i - 1].op == OP_SET &&
program->instructions[i - 1].set.count == 1 &&
program->instructions[i - 1].set.offset == 0) {
skip[i - 1] = 1;
}
}
}
}
fprintf(output, "#include <stdio.h>\n");
fprintf(output, "#include <string.h>\n");
fprintf(output, "\n");
fprintf(output, "int main(void) {\n");
fprintf(output, " unsigned char cells[%d] = {0};\n", CELL_COUNT * 2);
fprintf(output, " unsigned char *dp = cells + %d;\n", CELL_COUNT);
fprintf(output, "\n");
int indent_level = 1;
for (addr_t i = 0; i < program->size; i++) {
const Instruction *instr = &program->instructions[i];
if (skip[i]) {
continue;
}
switch (instr->op) {
case OP_RIGHT:
print_c_indent(output, indent_level);
if (instr->right.distance == 1) {
fprintf(output, "dp++;\n");
} else if (instr->right.distance == -1) {
fprintf(output, "dp--;\n");
} else if (instr->right.distance > 0) {
fprintf(output, "dp += %d;\n", instr->right.distance);
} else {
fprintf(output, "dp -= %d;\n", -instr->right.distance);
}
break;
case OP_INC:
print_c_indent(output, indent_level);
if (!inc_always_use_offset && instr->inc.offset == 0) {
if (instr->inc.count == 1) {
fprintf(output, "(*dp)++;\n");
} else if (instr->inc.count == -1) {
fprintf(output, "(*dp)--;\n");
} else if (instr->inc.count > 0) {
fprintf(output, "*dp += %d;\n", instr->inc.count);
} else {
fprintf(output, "*dp -= %d;\n", -instr->inc.count);
}
} else {
if (instr->inc.count == 1) {
fprintf(output, "dp[%d]++;\n", instr->inc.offset);
} else if (instr->inc.count == -1) {
fprintf(output, "dp[%d]--;\n", instr->inc.offset);
} else if (instr->inc.count > 0) {
fprintf(output, "dp[%d] += %d;\n", instr->inc.offset,
instr->inc.count);
} else {
fprintf(output, "dp[%d] -= %d;\n", instr->inc.offset,
-instr->inc.count);
}
}
break;
case OP_OUT:
print_c_indent(output, indent_level);
if (instr->out.offset == 0) {
fprintf(output, "putchar(*dp);\n");
} else {
fprintf(output, "putchar(dp[%d]);\n", instr->out.offset);
}
break;
case OP_IN:
print_c_indent(output, indent_level);
if (instr->in.offset == 0) {
fprintf(output, "*dp = getchar();\n");
} else {
fprintf(output, "dp[%d] = getchar();\n", instr->in.offset);
}
break;
case OP_LOOP:
print_c_indent(output, indent_level);
{
const addr_t end_idx = instr->loop.match_addr;
if (end_idx > 0 && skip[end_idx - 1] && instr->loop.offset == 0) {
const Instruction *right_instr = &program->instructions[end_idx - 1];
if (i > 0 && skip[i - 1] &&
program->instructions[i - 1].set.offset == 0) {
const Instruction *set_instr = &program->instructions[i - 1];
fprintf(output, "for (*dp = %d; *dp != 0; ", set_instr->set.value);
} else {
fprintf(output, "for (; *dp != 0; ");
}
if (right_instr->right.distance == 1) {
fprintf(output, "dp++");
} else if (right_instr->right.distance == -1) {
fprintf(output, "dp--");
} else if (right_instr->right.distance > 0) {
fprintf(output, "dp += %d", right_instr->right.distance);
} else {
fprintf(output, "dp -= %d", -right_instr->right.distance);
}
fprintf(output, ") {\n");
} else if (instr->loop.offset == 0) {
fprintf(output, "while (*dp != 0) {\n");
} else {
fprintf(output, "while (dp[%d] != 0) {\n", instr->loop.offset);
}
}
indent_level++;
break;
case OP_END:
indent_level--;
print_c_indent(output, indent_level);
fprintf(output, "}\n");
break;
case OP_SET:
print_c_indent(output, indent_level);
if (instr->set.count <= 1) {
if (!set_always_use_offset && instr->set.offset == 0) {
fprintf(output, "*dp = %d;\n", instr->set.value);
} else {
fprintf(output, "dp[%d] = %d;\n", instr->set.offset,
instr->set.value);
}
} else if (instr->set.stride == 0 || instr->set.stride == 1) {
if (!set_always_use_offset && instr->set.offset == 0) {
fprintf(output, "memset(dp, %d, %d);\n", instr->set.value,
instr->set.count);
} else {
const char sign = instr->set.offset >= 0 ? '+' : '-';
const i32 offset_abs =
instr->set.offset >= 0 ? instr->set.offset : -instr->set.offset;
fprintf(output, "memset(dp %c %d, %d, %d);\n", sign, offset_abs,
instr->set.value, instr->set.count);
}
} else {
fprintf(output, "for (int i = 0; i < %d; i++) dp[%d + i * %d] = %d;\n",
instr->set.count, instr->set.offset, instr->set.stride,
instr->set.value);
}
break;
case OP_SEEK_EMPTY:
print_c_indent(output, indent_level);
const char sign = instr->seek.step >= 0 ? '+' : '-';
const int step_abs = abs(instr->seek.step);
if (instr->seek.offset == 0) {
fprintf(output, "while (*dp != 0) dp %c= %d; // seek empty\n", sign,
step_abs);
} else {
fprintf(output, "while (dp[%d] != 0) dp %c= %d; // seek empty\n",
instr->seek.offset, sign, step_abs);
}
break;
case OP_TRANSFER:
print_c_indent(output, indent_level);
if (instr->transfer.target_count == 1) {
const int is_assignment = instr->transfer.is_assignment;
const int factor = instr->transfer.targets[0].factor;
const int factor_abs = abs(factor);
const int bias = instr->transfer.targets[0].bias;
const int shift = get_shift(factor_abs);
char src_operand[32];
snprintf(src_operand, sizeof(src_operand), "dp[%d]",
instr->transfer.src_offset);
if (is_assignment) {
fprintf(output, "dp[%d] = ", instr->transfer.targets[0].offset);
if (bias != 0) {
fprintf(output, "%d + ", bias);
}
print_multiply_expr(output, src_operand, factor);
fprintf(output, ";\n");
} else {
if (bias != 0) {
fprintf(output, "dp[%d] += ", instr->transfer.targets[0].offset);
print_multiply_expr(output, src_operand, factor);
if (bias > 0) {
fprintf(output, " + %d", bias);
} else {
fprintf(output, " - %d", -bias);
}
} else {
if (shift > 0) {
const char op = factor >= 0 ? '+' : '-';
fprintf(output, "dp[%d] %c= %s << %d",
instr->transfer.targets[0].offset, op, src_operand,
shift);
} else if (factor_abs == 1) {
const char factor_sign = factor >= 0 ? '+' : '-';
fprintf(output, "dp[%d] %c= %s",
instr->transfer.targets[0].offset, factor_sign,
src_operand);
} else {
const char factor_sign = factor >= 0 ? '+' : '-';
fprintf(output, "dp[%d] %c= %s * %d",
instr->transfer.targets[0].offset, factor_sign,
src_operand, factor_abs);
}
}
fprintf(output, ";\n");
}
} else {
for (int t = 0; t < instr->transfer.target_count; t++) {
if (t > 0) {
print_c_indent(output, indent_level);
}
const int offset = instr->transfer.targets[t].offset;
const int factor = instr->transfer.targets[t].factor;
const int bias = instr->transfer.targets[t].bias;
const int factor_abs = abs(factor);
const int shift = get_shift(factor_abs);
char src_operand[32];
snprintf(src_operand, sizeof(src_operand), "dp[%d]",
instr->transfer.src_offset);
if (bias != 0) {
fprintf(output, "dp[%d] += ", offset);
print_multiply_expr(output, src_operand, factor);
if (bias > 0) {
fprintf(output, " + %d", bias);
} else {
fprintf(output, " - %d", -bias);
}
} else {
if (shift > 0) {
const char op = factor >= 0 ? '+' : '-';
fprintf(output, "dp[%d] %c= %s << %d", offset, op, src_operand,
shift);
} else if (factor_abs == 1) {
const char factor_sign = factor >= 0 ? '+' : '-';
fprintf(output, "dp[%d] %c= %s", offset, factor_sign,
src_operand);
} else {
const char factor_sign = factor >= 0 ? '+' : '-';
fprintf(output, "dp[%d] %c= %s * %d", offset, factor_sign,
src_operand, factor_abs);
}
}
fprintf(output, ";\n");
}
}
break;
case OP_DIV: {
const i32 div_off = instr->div.src_offset;
const i32 quot_off = instr->div.dst_offset;
const i32 divisor = instr->div.divisor;
const int shift = get_shift(divisor);
print_c_indent(output, indent_level);
if (shift > 0) {
fprintf(output, "dp[%d] += dp[%d] >> %d;\n", quot_off, div_off, shift);
} else {
fprintf(output, "dp[%d] += dp[%d] / %d;\n", quot_off, div_off, divisor);
}
break;
}
case OP_MOD: {
const i32 div_off = instr->mod.src_offset;
const i32 rem_off = instr->mod.dst_offset;
const i32 divisor = instr->mod.divisor;
const int shift = get_shift(divisor);
print_c_indent(output, indent_level);
if (shift > 0) {
fprintf(output, "dp[%d] = dp[%d] & %d;\n", rem_off, div_off,
divisor - 1);
} else {
fprintf(output, "dp[%d] = dp[%d] %% %d;\n", rem_off, div_off, divisor);
}
break;
}
default:
print_c_indent(output, indent_level);
fprintf(output, "/* unknown op: %c */\n", instr->op);
break;
}
}
fprintf(output, "\n");
fprintf(output, " return 0;\n");
fprintf(output, "}\n");
free(skip);
}