-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.c
237 lines (230 loc) · 5.3 KB
/
codegen.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
#include <stdio.h>
#include "codegen.h"
#include "icode.h"
#include "bf_functions.h"
void write_prologue(CODE* code)
{
// push ebp
code_push(code, "c", 0x55);
// mov ebp, esp
code_push(code, "cc", 0x89, 0xe5);
// push ebx
code_push(code, "c", 0x53);
// mov ebx, [ebp+0x08] <- memory
code_push(code, "ccc", 0x8b, 0x5d, 0x08);
}
void write_epilogue(CODE* code)
{
// mov eax, BF_ERROR_SUCCESS
code_push(code, "ci", 0xb8, BF_ERROR_SUCCESS);
// pop ebx
code_push(code, "c", 0x5b);
// leave
code_push(code, "c", 0xc9);
// ret
code_push(code, "c", 0xc3);
}
int calc_loop_begin_jump_to(struct ICodeData* idata)
{
struct BFInstruction* target = idata->data->bracket;
int size = 0;
for(; idata != NULL; idata = idata->next)
{
switch(idata->data->inst)
{
case BFINST_INCPTR:
size += 16;
break;
case BFINST_DECPTR:
size += 16;
break;
case BFINST_INCVAL:
size += 2;
break;
case BFINST_DECVAL:
size += 2;
break;
case BFINST_PUTCHAR:
size += 9;
break;
case BFINST_GETCHAR:
size += 11;
break;
case BFINST_LOOP_BEGIN:
size += 9;
break;
case BFINST_LOOP_END:
size += 9;
break;
}
if (idata->data == target) break;
}
return size;
}
int calc_loop_end_jump_to(struct ICodeData* idata)
{
struct BFInstruction* target = idata->data->bracket;
int size = 0;
for(; idata != NULL; idata = idata->prev)
{
if (idata->data == target) break;
switch(idata->data->inst)
{
case BFINST_INCPTR:
size -= 16;
break;
case BFINST_DECPTR:
size -= 16;
break;
case BFINST_INCVAL:
size -= 2;
break;
case BFINST_DECVAL:
size -= 2;
break;
case BFINST_PUTCHAR:
size -= 9;
break;
case BFINST_GETCHAR:
size -= 11;
break;
case BFINST_LOOP_BEGIN:
size -= 9;
break;
case BFINST_LOOP_END:
size -= 9;
break;
}
}
// XXX: なんでこれが必要なのかよくわかっていないが必要っぽい
size--;
return size;
}
// コード生成
void generate_executable_code(CODE* code, ICODE* icode)
{
int pos;
struct ICodeData* idata;
for (idata = icode->first; idata != NULL; idata = idata->next)
{
switch(idata->data->inst)
{
case BFINST_INCPTR:
// ebxが[ebp+0x08]+BF_MEMORY_SIZEならエラー終了
// mov eax, [ebp+0x08]
code_push(code, "ccc", 0x8b, 0x45, 0x08);
// add eax, BF_MEMORY_SIZE
code_push(code, "ci", 0x05, BF_MEMORY_SIZE);
// cmp eax, ebx
code_push(code, "cc", 0x39, 0xd8);
// jne +8
code_push(code, "cc", 0x75, 0x08);
// mov eax, BF_ERROR_INVALID_DECPTR
code_push(code, "ci", 0xb8, BF_ERROR_INVALID_INCPTR);
// pop ebx
code_push(code, "c", 0x5b);
// leave
code_push(code, "c", 0xc9);
// ret
code_push(code, "c", 0xc3);
// inc ebx
code_push(code, "c", 0x43);
break;
case BFINST_DECPTR:
// ebxが[ebp+0x08]ならエラー終了
// mov eax, [ebp+0x08]
code_push(code, "ccc", 0x8b, 0x45, 0x08);
// cmp eax, ebx
code_push(code, "cc", 0x39, 0xd8);
// jne +8
code_push(code, "cc", 0x75, 0x08);
// mov eax, BF_ERROR_INVALID_DECPTR
code_push(code, "ci", 0xb8, BF_ERROR_INVALID_DECPTR);
// pop ebx
code_push(code, "c", 0x5b);
// leave
code_push(code, "c", 0xc9);
// ret
code_push(code, "c", 0xc3);
// dec ebx
code_push(code, "c", 0x4b);
break;
case BFINST_INCVAL:
// inc byte ptr [ebx]
code_push(code, "cc", 0xfe, 0x03);
break;
case BFINST_DECVAL:
// dec byte ptr [ebx]
code_push(code, "cc", 0xfe, 0x0b);
break;
case BFINST_PUTCHAR:
// mov cl, byte ptr [ebx]
code_push(code, "cc", 0x8a, 0x0b);
// mov eax, bf_putchar
code_push(code, "ci", 0xb8, bf_putchar);
// call eax
code_push(code, "cc", 0xff, 0xd0);
break;
case BFINST_GETCHAR:
// mov eax, bf_getchar
code_push(code, "ci", 0xb8, bf_getchar);
// call eax
code_push(code, "cc", 0xff, 0xd0);
// mov cl, al
code_push(code, "cc", 0x88, 0xc1);
// mov byte ptr [ebx], cl
code_push(code, "cc", 0x88, 0x0b);
break;
case BFINST_LOOP_BEGIN:
// cmp byte ptr [ebx], 0
code_push(code, "ccc", 0x80, 0x3b, 0x00);
pos = calc_loop_begin_jump_to(idata);
if (pos < 0x7f)
{
// je +pos
code_push(code, "cc", 0x74, pos);
// nop nop nop nop
code_push(code, "cccc", 0x90, 0x90, 0x90, 0x90);
}
else
{
// je +pos
// XXX: 動作未確認
code_push(code, "cci", 0x0f, 0x84, pos);
}
// je ...
// jmp先が0xffより遠い場合0x0f 0x84 XX XX XX XX、
// 0xffまでの場合0x74 0xXX
// めんどくさいので0xffの場合は後ろにnopを4つ置く
//code_push(code, "cc", 0x74, 0xff);
break;
case BFINST_LOOP_END:
// cmp byte ptr [ebx], 0
code_push(code, "ccc", 0x80, 0x3b, 0x00);
pos = calc_loop_end_jump_to(idata);
if (pos < 0x7f)
{
// jne +pos
code_push(code, "cc", 0x75, pos);
// nop nop nop nop
code_push(code, "cccc", 0x90, 0x90, 0x90, 0x90);
}
else
{
// jne +pos
// XXX: 動作未確認
code_push(code, "cci", 0x0f, 0x85, pos);
}
// jnz ...
// 0xffより遠いとき0x0f 0x85
// 近い時0x0x75 0xXX
break;
}
}
}
void generate_code(CODE* code, ICODE* icode)
{
write_prologue(code);
generate_executable_code(code, icode);
write_epilogue(code);
}