-
Notifications
You must be signed in to change notification settings - Fork 1
/
print_x86_64_unofficial.c
330 lines (288 loc) · 7.98 KB
/
print_x86_64_unofficial.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
328
329
330
#include "print_x86_64_unofficial.h"
#include "print_x86_64.h"
#include "std.h"
#include "std_io.h"
static void gen_raw_call_partC_with_stacksize(int arg_stacksize);
/*******************
* unofficial APIs *
*******************/
/* write to local mem what's at the top of the stack. does not consume stack. */
void gen_write_to_local(int offset)
{
assert(offset < 0);
printf("//gen_write_to_local(%d)\n", offset);
printf(" movl (%%rsp), %%eax\n");
printf(" movl %%eax, %d(%%rbp)\n", offset);
}
/* write to local mem what's at the top of the stack. does not consume stack. */
void gen_write_to_local_8byte(int offset)
{
assert(offset < 0);
printf("//gen_write_to_local_8byte(%d)\n", offset);
printf(" movq (%%rsp), %%rax\n");
printf(" movq %%rax, %d(%%rbp)\n", offset);
}
void gen_write_to_local_1byte(int offset)
{
assert(offset < 0);
printf("//gen_write_to_local_1byte(%d)\n", offset);
printf(" movb (%%rsp), %%al\n");
printf(" movb %%al, %d(%%rbp)\n", offset);
}
/* push what's on local mem */
void gen_push_from_local_4byte(int offset)
{
assert(offset < 0);
printf("//gen_push_from_local_4byte(%d)\n", offset);
printf(" subq $8, %%rsp\n"
" movl %d(%%rbp), %%eax\n"
" movl %%eax, (%%rsp)\n",
offset);
}
void gen_peek_deref_push_4byte(void)
{
printf("//gen_peek_deref_push_4byte()\n");
puts(" movq (%rsp), %rax \n"
" movl (%rax), %edx\n"
" subq $8, %rsp\n"
" movq %rdx, (%rsp)");
}
void gen_peek_and_dereference_4byte(void)
{
printf("//gen_peek_and_dereference_4byte()\n");
puts(" movq (%rsp), %rax \n"
" movl (%rax), %eax\n"
" movl %eax, (%rsp)");
}
/*
dereference what's at the top of the stack.
*/
void gen_peek_and_dereference_8byte(void)
{
printf("//gen_peek_and_dereference_8byte()\n");
puts(" movq (%rsp), %rax \n"
" movq (%rax), %rax\n"
" movq %rax, (%rsp)");
}
void gen_peek_deref_push_8byte(void)
{
printf("//gen_peek_deref_push_8byte()\n");
puts(" movq (%rsp), %rax \n"
" movq (%rax), %rdx\n"
" subq $8, %rsp\n"
" movq %rdx, (%rsp)");
}
void gen_peek_and_dereference_1byte(void)
{
printf("//gen_peek_and_dereference_1byte()\n");
printf(" movq (%%rsp), %%rax\n"
" movsbl (%%rax), %%eax\n"
" movl %%eax, (%%rsp)\n");
}
void gen_peek_deref_push_1byte(void)
{
printf("//gen_peek_deref_push_1byte()\n");
puts(" movq (%rsp), %rax \n"
" movb (%rax), %dl\n"
" subq $8, %rsp\n"
" movb %dl, (%rsp)");
}
/* push what's on local mem */
void gen_push_from_local_8byte(int offset)
{
assert(offset < 0);
printf("//gen_push_from_local_8byte(%d)\n", offset);
printf(" subq $8, %%rsp\n"
" movq %d(%%rbp), %%rax\n"
" movq %%rax, (%%rsp)\n",
offset);
}
void gen_push_from_local_1byte(int offset)
{
assert(offset < 0);
printf("//gen_push_from_local_1byte(%d)\n", offset);
printf(" subq $8, %%rsp\n"
" movsbl %d(%%rbp), %%eax\n"
" movl %%eax, (%%rsp)\n",
offset);
}
void gen_swap(void)
{
printf("//gen_swap()\n");
printf(" movq (%%rsp), %%rax\n"
" movq 8(%%rsp), %%rdx\n"
" movq %%rdx, (%%rsp)\n"
" movq %%rax, 8(%%rsp)\n");
}
void gen_push_ret_of_1byte(const char *fname, int arg_stacksize)
{
printf("//gen_push_ret_of_1byte(\"%s\")\n", fname);
gen_raw_call_partB(PREFIX, fname);
gen_raw_call_partC_with_stacksize(arg_stacksize);
printf(" movsbl %%al, %%eax\n"
" movl %%eax, (%%rsp)\n");
}
void gen_push_ret_of_4byte(const char *fname, int arg_stacksize)
{
printf("//gen_push_ret_of_4byte(\"%s\")\n", fname);
gen_raw_call_partB(PREFIX, fname);
gen_raw_call_partC_with_stacksize(arg_stacksize);
printf(" movl %%eax, (%%rsp)\n");
}
void gen_push_ret_of_8byte(const char *fname, int arg_stacksize)
{
printf("//gen_push_ret_of_8byte(\"%s\")\n", fname);
gen_raw_call_partB(PREFIX, fname);
gen_raw_call_partC_with_stacksize(arg_stacksize);
printf(" movq %%rax, (%%rsp)\n");
}
void gen_call_reg_and_push_ret_of_1byte(const char *reg)
{
printf("//gen_call_reg_and_push_ret_of_1byte(\"%s\")\n", reg);
gen_raw_call_partB("*%", reg);
gen_raw_call_partC();
printf(" movsbl %%al, %%eax\n"
" movl %%eax, (%%rsp)\n");
}
void gen_call_reg_and_push_ret_of_4byte(const char *reg)
{
printf("//gen_call_reg_and_push_ret_of_4byte(\"%s\")\n", reg);
gen_raw_call_partB("*%", reg);
gen_raw_call_partC();
printf(" movl %%eax, (%%rsp)\n");
}
void gen_call_reg_and_push_ret_of_8byte(const char *reg)
{
printf("//gen_call_reg_and_push_ret_of_8byte(\"%s\")\n", reg);
gen_raw_call_partB("*%", reg);
gen_raw_call_partC();
printf(" movq %%rax, (%%rsp)\n");
}
void gen_raw_call_partA()
{
/* alignment */
/*
if it is already aligned:
`subq $8, %rsp` will de-align; mod 16 gives 8.
Thus, `subq %rax, %rsp\n` subtracts another 8.
`movq %rax, (%rsp)` puts 8 on top of the stack.
*/
/*
if it is not aligned:
`subq $8, %rsp` will align; mod 16 gives 0.
Thus, `subq %rax, %rsp\n` will not subtract.
`movq %rax, (%rsp)` puts 0 on top of the stack.
*/
printf(" subq $8, %%rsp\n"
" movq %%rsp, %%rax\n"
" andq $15, %%rax\n"
" subq %%rax, %%rsp\n"
" movq %%rax, (%%rsp)\n");
}
void gen_raw_call_partB(const char *s1, const char *s2)
{
printf(" movb $0, %%al\n"); /* printf */
printf(" call %s%s\n", s1, s2);
}
static void gen_raw_call_partC_with_stacksize(int arg_stacksize)
{
printf(" addq $%d, %%rsp\n", arg_stacksize);
/*
if it was already aligned:
the top contains 8, and you must add 16 to the stack in order to resume.
Since this function is a `push`, you need to subtract 8 to push the
returned value. Hence, you only need to add 8.
*/
/*
if it was not aligned:
the top contains 0, and you must add 8 to the stack in order to resume.
Since this function is a `push`, you need to subtract 8 to push the
returned value. Hence, you only need to add 0.
*/
printf(" addq (%%rsp), %%rsp\n");
}
void gen_raw_call_partC() { gen_raw_call_partC_with_stacksize(0); }
void gen_discard3rd(void)
{
printf("//gen_discard3rd()\n");
printf(" movq 8(%%rsp), %%rax\n"
" movq %%rax, 16(%%rsp)\n"
" movq (%%rsp), %%rax\n"
" movq %%rax, 8(%%rsp)\n"
" addq $8, %%rsp\n");
}
/* for both 4byte and 1byte */
void gen_epilogue(int label)
{
printf("//gen_epilogue(%d)\n", label);
printf(".L%d:"
" movl (%%rsp), %%eax\n"
" leave\n"
" ret\n",
label);
}
void gen_epilogue_8byte(int label)
{
printf("//gen_epilogue_8byte(%d)\n", label);
printf(".L%d:"
" movq (%%rsp), %%rax\n"
" leave\n"
" ret\n",
label);
}
/*
value = pop();
addr = pop();
*addr = value;
push(value);
*/
void gen_assign_8byte(void)
{
printf("//gen_assign_8byte()\n");
printf(" movq (%%rsp), %%rax\n");
printf(" movq 8(%%rsp), %%rdx\n");
printf(" movq %%rax, (%%rdx)\n"
" addq $8, %%rsp\n"
" movq %%rax, (%%rsp)\n");
}
/*
value = pop();
addr = pop();
*addr = value;
push(value);
*/
void gen_assign_4byte(void)
{
printf("//gen_assign_4byte()\n");
printf(" movl (%%rsp), %%eax\n");
printf(" movq 8(%%rsp), %%rdx\n");
printf(" movl %%eax, (%%rdx)\n"
" addq $8, %%rsp\n"
" movl %%eax, (%%rsp)\n");
}
/*
value = pop();
addr = pop();
*addr = value;
push(value);
*/
void gen_assign_1byte(void)
{
printf("//gen_assign_1byte()\n");
printf(" movb (%%rsp), %%al\n");
printf(" movq 8(%%rsp), %%rdx\n");
printf(" movb %%al, (%%rdx)\n");
printf(" addq $8, %%rsp\n");
printf(" movb %%al, (%%rsp)\n");
}
void gen_initialize_va_list(int dst_struct_offset, int gp_offset, int fp_offset,
int reg_save_area_offset)
{
printf(" movl $%d, %d(%%rbp)\n", gp_offset, dst_struct_offset);
printf(" movl $%d, %d(%%rbp)\n", fp_offset, dst_struct_offset + 4);
printf(" leaq 16(%%rbp), %%rax\n"
" movq %%rax, %d(%%rbp)\n",
dst_struct_offset + 8);
printf(" leaq %d(%%rbp), %%rax\n", reg_save_area_offset);
printf(" movq %%rax, %d(%%rbp)\n", dst_struct_offset + 16);
}