-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen_nasm.c
More file actions
337 lines (304 loc) · 11.6 KB
/
codegen_nasm.c
File metadata and controls
337 lines (304 loc) · 11.6 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
#include <stdio.h>
#include "bft.h"
static const i32 use_shift_and_mask = 1;
static int get_shift(const 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;
}
void codegen_nasm(const Program *program, FILE *output) {
fprintf(output, "section .bss\n");
fprintf(output, " cells resb %d\n", CELL_COUNT * 2);
fprintf(output, "\n");
fprintf(output, "section .text\n");
fprintf(output, "global _start\n");
fprintf(output, "\n");
fprintf(output, "_start:\n");
fprintf(output,
" mov rbx, cells + %d ; rbx = data pointer (center of tape)\n",
CELL_COUNT);
fprintf(output, "\n");
for (addr_t i = 0; i < program->size; i++) {
const Instruction *instr = &program->instructions[i];
switch (instr->op) {
case OP_RIGHT:
if (instr->right.distance == 1) {
fprintf(output, " inc rbx\n");
} else if (instr->right.distance == -1) {
fprintf(output, " dec rbx\n");
} else if (instr->right.distance > 0) {
fprintf(output, " add rbx, %d\n", instr->right.distance);
} else {
fprintf(output, " sub rbx, %d\n", -instr->right.distance);
}
break;
case OP_INC:
if (instr->inc.offset == 0) {
if (instr->inc.count == 1) {
fprintf(output, " inc byte [rbx]\n");
} else if (instr->inc.count == -1) {
fprintf(output, " dec byte [rbx]\n");
} else if (instr->inc.count > 0) {
fprintf(output, " add byte [rbx], %d\n", instr->inc.count);
} else {
fprintf(output, " sub byte [rbx], %d\n", -instr->inc.count);
}
} else {
if (instr->inc.count == 1) {
fprintf(output, " inc byte [rbx%+d]\n", instr->inc.offset);
} else if (instr->inc.count == -1) {
fprintf(output, " dec byte [rbx%+d]\n", instr->inc.offset);
} else if (instr->inc.count > 0) {
fprintf(output, " add byte [rbx%+d], %d\n", instr->inc.offset,
instr->inc.count);
} else {
fprintf(output, " sub byte [rbx%+d], %d\n", instr->inc.offset,
-instr->inc.count);
}
}
break;
case OP_OUT:
fprintf(output, " mov rax, 1 ; write\n");
fprintf(output, " mov rdi, 1 ; stdout\n");
if (instr->out.offset == 0) {
fprintf(output, " mov rsi, rbx ; current cell\n");
} else {
fprintf(output, " lea rsi, [rbx%+d] ; cell at offset %d\n",
instr->out.offset, instr->out.offset);
}
fprintf(output, " mov rdx, 1 ; 1 byte\n");
fprintf(output, " syscall\n");
break;
case OP_IN:
fprintf(output, " mov rax, 0 ; read\n");
fprintf(output, " mov rdi, 0 ; stdin\n");
if (instr->in.offset == 0) {
fprintf(output, " mov rsi, rbx ; current cell\n");
} else {
fprintf(output, " lea rsi, [rbx%+d] ; cell at offset %d\n",
instr->in.offset, instr->in.offset);
}
fprintf(output, " mov rdx, 1 ; 1 byte\n");
fprintf(output, " syscall\n");
break;
case OP_LOOP:
fprintf(output, ".loop%d_start:\n", i);
if (instr->loop.offset == 0) {
fprintf(output, " cmp byte [rbx], 0\n");
} else {
fprintf(output, " cmp byte [rbx%+d], 0\n", instr->loop.offset);
}
fprintf(output, " je .loop%d_end\n", i);
break;
case OP_END:
if (instr->end.offset == 0) {
fprintf(output, " cmp byte [rbx], 0\n");
} else {
fprintf(output, " cmp byte [rbx%+d], 0\n", instr->end.offset);
}
fprintf(output, " jne .loop%d_start\n", instr->end.match_addr);
fprintf(output, ".loop%d_end:\n", instr->end.match_addr);
break;
case OP_SET:
if (instr->set.count <= 1) {
if (instr->set.offset == 0) {
fprintf(output, " mov byte [rbx], %d\n", instr->set.value);
} else {
fprintf(output, " mov byte [rbx%+d], %d\n", instr->set.offset,
instr->set.value);
}
} else if (instr->set.stride == 0 || instr->set.stride == 1) {
if (instr->set.offset == 0) {
fprintf(output, " ; memset %d cells to %d\n", instr->set.count,
instr->set.value);
fprintf(output, " mov rdi, rbx\n");
} else {
fprintf(output, " lea rdi, [rbx%+d]\n", instr->set.offset);
}
fprintf(output, " mov al, %d\n", instr->set.value);
fprintf(output, " mov rcx, %d\n", instr->set.count);
fprintf(output, " rep stosb\n");
} else {
fprintf(output, " ; strided set: %d cells, stride %d, value %d\n",
instr->set.count, instr->set.stride, instr->set.value);
fprintf(output, " mov rcx, %d\n", instr->set.count);
if (instr->set.offset == 0) {
fprintf(output, " mov rdi, rbx\n");
} else {
fprintf(output, " lea rdi, [rbx%+d]\n", instr->set.offset);
}
fprintf(output, ".strided_set_%d:\n", i);
fprintf(output, " mov byte [rdi], %d\n", instr->set.value);
fprintf(output, " add rdi, %d\n", instr->set.stride);
fprintf(output, " dec rcx\n");
fprintf(output, " jnz .strided_set_%d\n", i);
}
break;
case OP_SEEK_EMPTY:
fprintf(output, ".find_empty_%d:\n", i);
if (instr->seek.offset == 0) {
fprintf(output, " cmp byte [rbx], 0\n");
} else {
fprintf(output, " cmp byte [rbx%+d], 0\n", instr->seek.offset);
}
fprintf(output, " je .find_empty_done_%d\n", i);
if (instr->seek.step == 1) {
fprintf(output, " inc rbx\n");
} else if (instr->seek.step == -1) {
fprintf(output, " dec rbx\n");
} else if (instr->seek.step > 1) {
fprintf(output, " add rbx, %d\n", instr->seek.step);
} else {
fprintf(output, " sub rbx, %d\n", -instr->seek.step);
}
fprintf(output, " jmp .find_empty_%d\n", i);
fprintf(output, ".find_empty_done_%d:\n", i);
break;
case OP_TRANSFER:
if (instr->transfer.src_offset == 0) {
fprintf(output, " movzx eax, byte [rbx]\n");
} else {
fprintf(output, " movzx eax, byte [rbx%+d]\n",
instr->transfer.src_offset);
}
if (instr->transfer.is_assignment && instr->transfer.target_count == 1) {
// Assignment mode
const i32 offset = instr->transfer.targets[0].offset;
const i32 factor = instr->transfer.targets[0].factor;
const i32 bias = instr->transfer.targets[0].bias;
if (factor == 1) {
if (bias != 0) {
fprintf(output, " add al, %d\n", bias);
}
fprintf(output, " mov byte [rbx%+d], al\n", offset);
} else if (factor == -1) {
fprintf(output, " neg al\n");
if (bias != 0) {
fprintf(output, " add al, %d\n", bias);
}
fprintf(output, " mov byte [rbx%+d], al\n", offset);
} else if (factor > 0) {
fprintf(output, " mov cl, %d\n", factor);
fprintf(output, " imul cl\n");
if (bias != 0) {
fprintf(output, " add al, %d\n", bias);
}
fprintf(output, " mov byte [rbx%+d], al\n", offset);
} else {
fprintf(output, " mov cl, %d\n", -factor);
fprintf(output, " imul cl\n");
fprintf(output, " neg al\n");
if (bias != 0) {
fprintf(output, " add al, %d\n", bias);
}
fprintf(output, " mov byte [rbx%+d], al\n", offset);
}
} else {
for (int t = 0; t < instr->transfer.target_count; t++) {
const i32 bias = instr->transfer.targets[t].bias;
if (bias != 0) {
const i32 offset = instr->transfer.targets[t].offset;
if (bias > 0) {
fprintf(output, " add byte [rbx%+d], %d\n", offset, bias);
} else {
fprintf(output, " sub byte [rbx%+d], %d\n", offset, -bias);
}
}
}
fprintf(output, " test al, al\n");
fprintf(output, " jz .transfer_done_%d\n", i);
for (int t = 0; t < instr->transfer.target_count; t++) {
const i32 offset = instr->transfer.targets[t].offset;
const i32 factor = instr->transfer.targets[t].factor;
if (factor == 1) {
fprintf(output, " add byte [rbx%+d], al\n", offset);
} else if (factor == -1) {
fprintf(output, " sub byte [rbx%+d], al\n", offset);
} else if (factor > 0) {
fprintf(output, " mov cl, %d\n", factor);
fprintf(output, " imul cl\n");
fprintf(output, " add byte [rbx%+d], al\n", offset);
if (t < instr->transfer.target_count - 1) {
if (instr->transfer.src_offset == 0) {
fprintf(output, " movzx eax, byte [rbx]\n");
} else {
fprintf(output, " movzx eax, byte [rbx%+d]\n",
instr->transfer.src_offset);
}
}
} else {
fprintf(output, " mov cl, %d\n", -factor);
fprintf(output, " imul cl\n");
fprintf(output, " sub byte [rbx%+d], al\n", offset);
if (t < instr->transfer.target_count - 1) {
if (instr->transfer.src_offset == 0) {
fprintf(output, " movzx eax, byte [rbx]\n");
} else {
fprintf(output, " movzx eax, byte [rbx%+d]\n",
instr->transfer.src_offset);
}
}
}
}
fprintf(output, ".transfer_done_%d:\n", i);
}
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);
fprintf(output, " ; div by %d\n", divisor);
fprintf(output, " movzx eax, byte [rbx%+d]\n", div_off);
if (shift > 0) {
// Use bit shift for power of 2
fprintf(output, " shr al, %d\n", shift);
fprintf(output, " add byte [rbx%+d], al\n", quot_off);
} else {
fprintf(output, " xor edx, edx\n");
fprintf(output, " mov cl, %d\n", divisor);
fprintf(output, " div cl\n");
fprintf(output, " add byte [rbx%+d], al\n", quot_off);
}
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);
fprintf(output, " ; mod by %d\n", divisor);
fprintf(output, " movzx eax, byte [rbx%+d]\n", div_off);
if (shift > 0) {
// Use bit mask for power of 2
fprintf(output, " and al, %d\n", divisor - 1);
fprintf(output, " mov byte [rbx%+d], al\n", rem_off);
} else {
fprintf(output, " xor edx, edx\n");
fprintf(output, " mov cl, %d\n", divisor);
fprintf(output, " div cl\n");
fprintf(output, " mov byte [rbx%+d], ah\n", rem_off);
}
break;
}
default:
fprintf(output, " ; UNKNOWN OP: %c\n", instr->op);
break;
}
}
fprintf(output, "\n");
fprintf(output, " mov rax, 60\n");
fprintf(output, " xor rdi, rdi\n");
fprintf(output, " syscall\n");
}