-
Notifications
You must be signed in to change notification settings - Fork 2
/
compiler.c
160 lines (146 loc) · 3.45 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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "compiler.h"
#include <stdbool.h>
compile_result compile(const char* bf, void* mem)
{
char* exec = (char*)malloc(65536);
char* exec_ptr = exec;
size_t len = strlen(bf);
int mem_addr = (int)mem;
int putchar_addr = (int)putchar;
int getchar_addr = (int)getchar;
int* jmp_stack = (int*)malloc(sizeof(int)*1024);
*exec++ = 0x55; // push ebp
*exec++ = 0x89; // mov ebp, esp
*exec++ = 0xE5;
*exec++ = 0x81; // and esp, -16
*exec++ = 0xE4;
*exec++ = 0xF0; // => -16
*exec++ = 0xFF; //
*exec++ = 0xFF; //
*exec++ = 0xFF; //
*exec++ = 0x81; // sub esp, 16
*exec++ = 0xEC; //
*exec++ = 0x10; // => 16
*exec++ = 0x00; //
*exec++ = 0x00; //
*exec++ = 0x00; //
*exec++ = 0x31; // xor eax, eax
*exec++ = 0xC0; //
*exec++ = 0xBB; // mov ebx
*(int*)exec = mem_addr;
exec += 4;
bool alInvalidated = true;
for(int i = 0; i < len; i++)
{
switch(bf[i])
{
case '>':
if(i != 0 && bf[i-1] != '>')
{
*exec++ = 0x88; // mov [ebx], al
*exec++ = 0x03; //
}
*exec++ = 0x43; // inc ebx
alInvalidated = true;
break;
case '<':
if(i != 0 && bf[i-1] != '<')
{
*exec++ = 0x88; // mov [ebx], al
*exec++ = 0x03; //
}
*exec++ = 0x4B; // dec ebx
alInvalidated = true;
break;
case '+':
if(alInvalidated)
{
*exec++ = 0x8A; // mov al, [ebx]
*exec++ = 0x03; //
alInvalidated = false;
}
*exec++ = 0xFE; // inc al
*exec++ = 0xC0; //
break;
case '-':
if(alInvalidated)
{
*exec++ = 0x8A; // mov al, [ebx]
*exec++ = 0x03; //
alInvalidated = false;
}
*exec++ = 0xFE; // dec al
*exec++ = 0xC8; //
break;
case '[':
*jmp_stack++ = (int)exec; // store location of current thingy to be filled in later.
if(alInvalidated)
{
*exec++ = 0x8A; // mov al, [ebx]
*exec++ = 0x03; //
alInvalidated = false;
}
else
{
// padding to make sure we don't throw off the offsets (grr, offsets)
*exec++ = 0x90; // nop
*exec++ = 0x90; // nop
}
*exec++ = 0xA8; // TEST al, 0xFF
*exec++ = 0xFF; //
*exec++ = 0x0F; // JZ ?
*exec++ = 0x84;
*exec++ = 0x00; // ?
*exec++ = 0x00; // ?
*exec++ = 0x00; // ?
*exec++ = 0x00; // ?
break;
case ']':
--jmp_stack;
int jmp_to = *jmp_stack--;
*exec++ = 0xB9; // mov ecx, jmp_to
*(int*)exec = jmp_to;
exec += 4;
*exec++ = 0xFF; // jmp ecx
*exec++ = 0xE1; //
// fill in current exec location offset to matching bracket
int offset = (((int)exec)-jmp_to)-10;
*(int*)(jmp_to+6) = offset;
break;
case '.':
if(alInvalidated)
{
*exec++ = 0x8A; // mov al, [ebx]
*exec++ = 0x03; //
alInvalidated = false;
}
*exec++ = 0x89; // mov [esp], eax
*exec++ = 0x04; //
*exec++ = 0x24; //
*exec++ = 0xB9; // mov ecx putchar_addr
*(int*)exec = putchar_addr;
exec += 4;
*exec++ = 0xFF; // call ecx
*exec++ = 0xD1; //
break;
case ',':
*exec++ = 0xB9; // mov ecx putchar_addr
*(int*)exec = getchar_addr;
exec += 4;
*exec++ = 0xFF; // call ecx
*exec++ = 0xD1; //
*exec++ = 0x88; // mov [ebx], al
*exec++ = 0x03; //
break;
}
}
*exec++ = 0xC9; // leave
*exec++ = 0xC3; // ret
compile_result ret;
ret.length = exec - exec_ptr;
ret.code.buff = exec_ptr;
return ret;
}