-
Notifications
You must be signed in to change notification settings - Fork 4
/
compiler.c
executable file
·178 lines (146 loc) · 4.91 KB
/
compiler.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <stdint.h>
#include <utils/stack.h>
#include <compiler.h>
#include <dbg.h>
/* Conventions used in the generated code:
%rip is the instruction pointer
%rdx is the pointer to current memory cell (rdx wasn't chosen for any particular reason) */
typedef void (*jitted_code)();
/* Allocs a RWX page using mmap. */
void *alloc_executable_memory(size_t size) {
void* ptr = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
check(ptr != MAP_FAILED && ptr != NULL, "Couldn't mmap RWX page");
return ptr;
error:
return NULL;
}
/* Convert brainfuck instruction to x64 code, returned in **code, with code size size in *size.
Invalid brainfuck instructions (aka comments) returns NULL
Note: asm files that generated the x64 hex are in the asm/ directory */
static void code_for_instruction(unsigned char instruction, unsigned char **code, size_t *size) {
switch (instruction) {
case '>':
*code = "\x48\x83\xc2\x01"; // code for cc++, aka add rdx, 1
*size = 4;
return;
case '<':
*code = "\x48\x83\xea\x01"; // code for cc--, aka sub rdx, 1
*size = 4;
return;
case '+':
*code = "\x80\x02\x01"; // code for (*cc)++, aka add [rdx], 1
*size = 3;
return;
case '-':
*code = "\x80\x2a\x01"; // (*cc)--
*size = 3;
return;
case '.':
#ifdef __APPLE__
// OS X syscalls are a bit different
//*code = "\xb8\x04\x00\x00\x02\xbf\x01\x00\x00\x00\x48\x89\xd6\x52\xba\x01\x00\x00\x00\x0f\x05\x5a"; // write()
*code = "\xb8\x8d\x01\x00\x02\xbf\x01\x00\x00\x00\x48\x89\xd6\x52\xba\x01\x00\x00\x00\x0f\x05\x5a"; // write_nocancel()
*size = 22;
#else
*code = "\xb8\x01\x00\x00\x00\xbf\x01\x00\x00\x00\x48\x89\xd6\x52\xba\x01\x00\x00\x00\x0f\x05\x5a"; // print char stored at [rdx]
*size = 22;
#endif
return;
default:
*code = NULL;
*size = -1;
}
}
// TODO: refactor the code generation process to be instruction type agnostic
size_t compute_machine_code_size(unsigned char *command, size_t command_size) {
size_t size = 3; // size for setting up rdx + ret
unsigned char dummy[64];
for (unsigned char *cur_command = command; cur_command < command + command_size; cur_command++) {
if (*cur_command == '[') {
size += 5;
}
else if (*cur_command == ']') {
size += 9;
}
else {
size_t tmp = 0;
code_for_instruction(*cur_command, (void*)&dummy, &tmp);
size += tmp;
}
}
return size;
}
/* This function compiles and execute the `size` brainfucks instructions in `command`
*/
void jit_run(unsigned char *command, size_t command_size) {
// create the data space of the program
unsigned char *begin_cells = calloc(1, PLAYGROUND_CELLS);
check_mem(begin_cells);
size_t code_size = compute_machine_code_size(command, command_size);
unsigned char *big_chunk = alloc_executable_memory(code_size);
unsigned char *code = big_chunk;
// add code to set rdx to address of playground
code[0] = 0x48;
code[1] = 0xba;
code += 2;
memcpy(code, &begin_cells, sizeof(unsigned char *));
code += sizeof(unsigned char*);
stack *jumpback_stack = stack_create(sizeof(unsigned char*));
for (unsigned char *cur_command = command; cur_command < command + command_size; cur_command++) {
//debug("decoding command #%d", ++n);
unsigned char *ins_code = NULL;
size_t size;
if (*cur_command == '[') {
// will be filled later by the corresponding '['
unsigned char base[5] = "\xe9 FLL";
ins_code = base;
size = 5;
// note the current position (offset from start) for later use
unsigned char *next_ins = (unsigned char*)(code + size - big_chunk);
stack_push(jumpback_stack, &next_ins);
}
else if (*cur_command == ']') {
check(!stack_empty(jumpback_stack), "Mismatched '[' or ']' in input");
// jump is implemented in the form of jmp [RIP + const]
unsigned char *addr;
stack_pop(jumpback_stack, &addr);
addr += (size_t)big_chunk;
size = 9;
// jump address relative to next instruction => needs to account for size of current jump instruction
uint32_t jump_offset = addr - code - size;
debug("Making a %d bytes jump", jump_offset);
unsigned char base[9] = "\x80\x3a\x00\x0f\x85 ";
memcpy(&base[5], &jump_offset,4);
ins_code = base;
// now we also need to make the previous [ unconditionnaly jump here
// just putting the current address there, relatively speaking
uint32_t forward_offset = -(addr - code);
memcpy(addr - 4, &forward_offset, 4);
}
else {
// it's not a branch instruction, nothing special to do
code_for_instruction(*cur_command, &ins_code, &size);
}
// copy instruction to executable page
// ignoring comments
if(ins_code) {
memcpy(code, ins_code, size);
code += size;
}
}
// add `ret` to exit cleanly:
code[0] = 0xc3;
code++;
jitted_code func = (jitted_code)big_chunk;
debug("Running compiled code.");
func();
error:
if (begin_cells) {
free(begin_cells);
}
return;
}