-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfc.c
327 lines (268 loc) · 6.97 KB
/
bfc.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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdbool.h>
#include <sys/mman.h>
#include "bf_jit_x86_64.h"
#define TAP_SIZE 1048576
#define HALT 0
#define BF_RIGHT 1
#define BF_LEFT 2
#define BF_INC 3
#define BF_DEC 4
#define BF_OUT 5
#define BF_IN 6
#define BF_OPEN 7
#define BF_CLOSE 8
void gen_prologue(FILE *ofile) {
fprintf(ofile,
"section .text\n"
"\tglobal _start\n\n"
"_start:\n"
);
fprintf(ofile,
"\tmov rdi, %d\n", TAP_SIZE
);
fprintf(ofile, "\tmov rsi, 1\n");
fprintf(ofile,
"\textern calloc\n"
"\tcall calloc\n"
"\tmov rsi, rax\n"
);
}
void gen_epilogue(FILE *ofile) {
fprintf(ofile,
"\tmov rax, 60\n"
"\txor rdi, rdi\n"
"\tsyscall\n"
);
}
int bf_aot_comp(unsigned char *code, FILE *ofile) {
int loop_stack[100];
int loop_stack_pos = 0;
int loop_count = 0;
gen_prologue(ofile);
while(*code) {
switch(*code) {
case '>':
fprintf(ofile, "\tinc rsi\n");
break;
case '<':
fprintf(ofile, "\tdec rsi\n");
break;
case '+':
fprintf(ofile, "\tinc byte [rsi]\n");
break;
case '-':
fprintf(ofile, "\tdec byte [rsi]\n");
break;
case '.':
fprintf(ofile,
"\tmov rax, 1\n"
"\tmov rdi, 1\n"
"\tmov rdx, 1\n"
"\tsyscall\n"
);
break;
case ',':
break;
case '[':
loop_stack[loop_stack_pos++] = loop_count;
fprintf(ofile, "loop_start_%d:\n", loop_count);
fprintf(ofile, "\tcmp byte [rsi], 0\n");
fprintf(ofile, "\tje loop_end_%d\n", loop_count);
loop_count++;
break;
case ']':
loop_stack_pos--;
int cur_loop_count = loop_stack[loop_stack_pos];
fprintf(ofile, "\tcmp byte [rsi], 0\n");
fprintf(ofile, "\tjne loop_start_%d\n", cur_loop_count);
fprintf(ofile, "loop_end_%d:\n", cur_loop_count);
break;
default:
break;
}
code++;
}
gen_epilogue(ofile);
return 0;
}
uint32_t compute_pc_rel32(uint32_t from, uint32_t to) {
if (to >= from)
return to - from;
else
return ~(from - to) + 1;
}
void replace_bytes(uint8_t *buf, uint32_t offset, uint32_t value, int size) {
for (int i = 0; i < size; i++) {
buf[offset + i] = (value >> (i * 8)) & 0xff;
}
}
void bf_jit_com_x86_64(unsigned char *code, int len) {
struct jit_state state;
state.buf = (uint8_t *)malloc(MAX_OFFSET);
state.offset = 0;
uint32_t open_bracket_off[MAX_NESTING];
uint32_t open_br_off;
uint32_t open_bracket_index = 0;
// push callee saved registers
emit_push(&state, RBX);
emit_push(&state, RBP);
emit_push(&state, R12);
emit_push(&state, R13);
emit_push(&state, R14);
emit_push(&state, R15);
for (int i = 0; i < len; i++) {
switch(code[i]) {
case '>':
/*
* Tape is supplied as a pointer by the called in rdi
*
* inc rdi
*/
emit1(&state, 0x48);
emit1(&state, 0xff);
emit1(&state, 0xc7);
break;
case '<':
// dec rdi
emit1(&state, 0x48);
emit1(&state, 0xff);
emit1(&state, 0xcf);
break;
case '+':
// inc byte [rdi]
emit1(&state, 0xfe);
emit1(&state, 0x07);
break;
case '-':
// dec byte [rdi]
emit1(&state, 0xfe);
emit1(&state, 0x0f);
break;
case '.':
// mov rbx, rdi ;save rdi for write syscall
emit1(&state, 0x48);
emit1(&state, 0x89);
emit1(&state, 0xfb);
// mov al, byte [rbx] ;arg2 char to write
emit1(&state, 0x8a);
emit1(&state, 0x03);
// mov rax, 1 ;syscall number
emit1(&state, 0xb8);
emit4(&state, 0x01000000);
// mov rdi, 1 ; arg1 stdout
emit1(&state, 0xbf);
emit4(&state, 0x01000000);
// mov rdx, 1; arg3 size
emit1(&state, 0xba);
emit4(&state, 0x01000000);
// syscall
emit1(&state, 0x0f);
emit1(&state, 0x05);
// mov rdi, rbx
emit1(&state, 0x48);
emit1(&state, 0x89);
emit1(&state, 0xdf);
break;
case '[':
// cmp byte [rdi], 0
emit1(&state, 0x80);
emit1(&state, 0x3f);
emit1(&state, 0x00);
open_bracket_off[open_bracket_index++] = state.offset;
// jz 0
emit1(&state, 0x0f);
emit1(&state, 0x84);
emit4(&state, 0x00000000);
break;
case ']':
open_br_off = open_bracket_off[--open_bracket_index];
// cmp byte [rdi], 0
emit1(&state, 0x80);
emit1(&state, 0x3f);
emit1(&state, 0x00);
uint32_t jmp_open_from = state.offset + 6;
uint32_t jmp_open_to = open_br_off + 6;
uint32_t jmp_open_off = compute_pc_rel32(jmp_open_from, jmp_open_to);
// jnz jmp_open_off
emit1(&state, 0x0f);
emit1(&state, 0x85);
emit4(&state, jmp_open_off);
uint32_t jmp_close_from = open_br_off + 6;
uint32_t jmp_close_to = state.offset;
uint32_t jmp_close_off = compute_pc_rel32(jmp_close_from, jmp_close_to);
// replace off
replace_bytes(state.buf, open_br_off + 2, jmp_close_off, 4);
break;
}
}
emit_pop(&state, R15);
emit_pop(&state, R14);
emit_pop(&state, R13);
emit_pop(&state, R12);
emit_pop(&state, RBP);
emit_pop(&state, RBX);
void *jitted_code = mmap(NULL, state.offset, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
memcpy(jitted_code, state.buf, state.offset);
typedef void (*jit_fn)(uint64_t *);
jit_fn fn = (jit_fn)jitted_code;
uint64_t *tape = (uint64_t *)calloc(TAP_SIZE, sizeof(uint64_t));
fn(tape);
}
int main(int argc, char *argv[]) {
FILE *ofile;
struct option longopts[] = {
{.name = "aot", .val = 'a', },
{.name = "jit", .val = 'j', },
};
bool aot = false;
int opt;
while ((opt = getopt_long(argc, argv, "aj", longopts, NULL)) != -1) {
switch(opt) {
case 'a':
aot = true;
break;
case 'j':
break;
default:
printf("Unkown option\n");
return 1;
}
}
if (optind >= argc) {
printf("Error: No input file\n");
return 1;
}
FILE *ifile = fopen(argv[optind++], "r");
if(!ifile) {
printf("Error: Could not open file\n");
return 1;
}
if (aot) {
if (optind < argc) {
ofile = fopen(argv[optind], "w");
if(!ofile) {
printf("Error: Could not open file\n");
return 1;
}
}
else {
ofile = stdout;
}
}
fseek(ifile, 0, SEEK_END);
long length = ftell(ifile);
fseek(ifile, 0, SEEK_SET);
unsigned char *code = (unsigned char *)malloc(length + 1);
int rv = fread(code, 1, length, ifile);
if (rv != length)
return 1;
code[length] = '\0';
fclose(ifile);
(void)ofile;
// bf_aot_comp(code, ofile);
bf_jit_com_x86_64(code, length);
return 0;
}