-
Notifications
You must be signed in to change notification settings - Fork 5
/
x86.c
264 lines (236 loc) · 5.7 KB
/
x86.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "y.tab.h"
#define section_text "\
section .text\n\
\n\
global _start\n\
\n"
#define section_data "\
section .data\n\
\n\
buf times 64 db 0xA\n\
\n"
#define proc_dump32 "\
dump32:\n\
mov edi, buf + 7\n\
mov ebx, 8\n\
\n\
loop8:\n\
mov eax, esi\n\
and eax, 0xF\n\
add eax, '0'\n\
cmp al, '9'\n\
jle dump4\n\
add eax, 7\n\
dump4:\n\
mov byte [edi], al\n\
shr esi, 4\n\
dec edi\n\
dec ebx\n\
jnz loop8\n\
\n\
mov edx, 9 ; arg3 len\n\
mov ecx, buf ; arg2 ptr\n\
mov ebx, 1 ; arg1 stdio\n\
mov eax, 4 ; sys_write\n\
int 0X80 ; call kernel\n\
ret\n\
\n"
#define proc_main "\
_start:\n\
call function_main\n\
\n\
mov esi, eax\n\
call dump32\n\
\n\
mov ebx, 0 ; exit code\n\
mov eax, 1 ; sys_exit\n\
int 0X80 ; call kernel\n\
\n"
#ifndef DATA_SIZE
#define DATA_SIZE 4096
#endif
static char data_buf[DATA_SIZE];
#ifndef CODE_SIZE
#define CODE_SIZE 65536
#endif
static char code_buf[CODE_SIZE];
static char temp_buf[256];
static char * varstack[64];
static int vartop = -1;
static char * argstack[16];
static int argtop = -1;
static char * current_function;
int stack_index(char * name, char * stack[], int top) {
int i=0;
while (i<=top) {
if (!strcmp(stack[i], name))
return i;
i++;
}
return -1;
}
void gen_int(int value) {
sprintf(temp_buf," push %d\n", value);
strcat(code_buf, temp_buf);
};
void gen_intvar(char * name) {
int i = stack_index(name, varstack, vartop);
if (i >= 0)
sprintf(temp_buf," push dword [ebp-%02d] ; %s\n", 4*(i+1), name);
else {
i = stack_index(name, argstack, argtop);
if (i < 0) {
yyerror(name);
exit -1;
}
sprintf(temp_buf," push dword [ebp+%02d] ; %s\n", 4*(argtop-i+2), name);
}
//free(name);
strcat(code_buf, temp_buf);
};
void assign_intvar(char * name) {
int i = stack_index(name, varstack, vartop);
if (i >= 0)
sprintf(temp_buf," pop dword [ebp-%02d] ; %s\n", 4*(i+1), name);
else {
i = stack_index(name, argstack, argtop);
if (i < 0) {
yyerror(name);
exit -1;
}
sprintf(temp_buf," pop dword [ebp+%02d] ; %s\n", 4*(argtop-i+2), name);
}
//free(name);
strcat(code_buf, temp_buf);
};
void gen_op32(int op) {
strcat(code_buf, " pop ebx\n");
strcat(code_buf, " pop eax\n");
switch (op) {
case '+':
strcat(code_buf, " add eax, ebx\n");
break;
case '-':
strcat(code_buf, " sub eax, ebx\n");
break;
case '*':
strcat(code_buf, " imul eax, ebx\n");
break;
case '/':
strcat(code_buf, " idiv eax, ebx\n");
break;
}
strcat(code_buf, " push eax\n");
};
void gen_negative() {
strcat(code_buf, " pop ebx\n");
strcat(code_buf, " mov eax, 0\n");
strcat(code_buf, " sub eax, ebx\n");
strcat(code_buf, " push eax\n");
};
void gen_jmp(char * prefix, int jmpto) {
sprintf(temp_buf, " jmp %s%04d\n", prefix, jmpto);
strcat(code_buf, temp_buf);
};
void gen_jmpcond32(int op, char * prefix, int jmpto) {
if (op == 0) {
strcat(code_buf, " mov ebx, 0\n");
op = EQ;
}
else
strcat(code_buf, " pop ebx\n");
strcat(code_buf, " pop eax\n");
strcat(code_buf, " cmp eax, ebx\n");
switch (op) {
case EQ:
sprintf(temp_buf, " je %s%04d\n", prefix, jmpto);
break;
case NE:
sprintf(temp_buf, " jne %s%04d\n", prefix, jmpto);
break;
case LE:
sprintf(temp_buf, " jle %s%04d\n", prefix, jmpto);
break;
case GE:
sprintf(temp_buf, " jge %s%04d\n", prefix, jmpto);
break;
case '<':
sprintf(temp_buf, " jl %s%04d\n", prefix, jmpto);
break;
case '>':
sprintf(temp_buf, " jg %s%04d\n", prefix, jmpto);
break;
}
strcat(code_buf, temp_buf);
};
void gen_return() {
strcat(code_buf, " pop eax\n");
sprintf(temp_buf, " jmp function_%s_end\n", current_function);
strcat(code_buf, temp_buf);
}
void gen_label(char * prefix, int jmpto) {
sprintf(temp_buf, "%s%04d:\n", prefix, jmpto);
strcat(code_buf, temp_buf);
}
void declare_intvar(char * name) {
sprintf(temp_buf," push 0xffffffff ; %s\n", name);
strcat(code_buf, temp_buf);
varstack[++vartop] = name;
};
void declare_intarg(char * name) {
argstack[++argtop] = name;
}
void begin_func(char * name) {
current_function = name;
sprintf(temp_buf,"function_%s:\n", name);
strcat(code_buf, temp_buf);
strcat(code_buf, " push ebp\n");
strcat(code_buf, " mov ebp, esp\n");
//strcat(code_buf, " push ebx\n");
//strcat(code_buf, " push edi\n");
//strcat(code_buf, " push esi\n");
}
void end_func() {
int i;
sprintf(temp_buf,"function_%s_end:\n", current_function);
/*
free(current_function);
for (i=0; i<vartop; i++)
free(varstack[i]);
for (i=0; i<argtop; i++)
free(argstack[i]);
*/
strcat(code_buf, temp_buf);
//strcat(code_buf, " pop esi\n");
//strcat(code_buf, " pop edi\n");
//strcat(code_buf, " pop ebx\n");
strcat(code_buf, " mov esp, ebp\n");
strcat(code_buf, " pop ebp\n");
strcat(code_buf, " ret\n");
}
void gen_call(char * name, int nargs) {
sprintf(temp_buf, " call function_%s\n", name);
strcat(code_buf, temp_buf);
if (nargs) {
sprintf(temp_buf, " add esp, %d ; drop %d args\n", nargs * 4, nargs);
strcat(code_buf, temp_buf);
}
}
void gen_callresult() {
strcat(code_buf, " push eax\n");
}
int main()
{
strcpy(data_buf, section_text);
strcat(data_buf, section_data);
strcpy(code_buf, proc_dump32);
strcat(code_buf, proc_main);
if (yyparse())
return 1;
printf("%s\n",data_buf);
printf("%s\n",code_buf);
return 0;
}