1 | /*
|
---|
2 | * X86 code generator for TCC
|
---|
3 | *
|
---|
4 | * Copyright (c) 2001-2004 Fabrice Bellard
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifdef TARGET_DEFS_ONLY
|
---|
22 |
|
---|
23 | /* number of available registers */
|
---|
24 | #define NB_REGS 5
|
---|
25 | #define NB_ASM_REGS 8
|
---|
26 | #define CONFIG_TCC_ASM
|
---|
27 |
|
---|
28 | /* a register can belong to several classes. The classes must be
|
---|
29 | sorted from more general to more precise (see gv2() code which does
|
---|
30 | assumptions on it). */
|
---|
31 | #define RC_INT 0x0001 /* generic integer register */
|
---|
32 | #define RC_FLOAT 0x0002 /* generic float register */
|
---|
33 | #define RC_EAX 0x0004
|
---|
34 | #define RC_ST0 0x0008
|
---|
35 | #define RC_ECX 0x0010
|
---|
36 | #define RC_EDX 0x0020
|
---|
37 | #define RC_EBX 0x0040
|
---|
38 |
|
---|
39 | #define RC_IRET RC_EAX /* function return: integer register */
|
---|
40 | #define RC_LRET RC_EDX /* function return: second integer register */
|
---|
41 | #define RC_FRET RC_ST0 /* function return: float register */
|
---|
42 |
|
---|
43 | /* pretty names for the registers */
|
---|
44 | enum {
|
---|
45 | TREG_EAX = 0,
|
---|
46 | TREG_ECX,
|
---|
47 | TREG_EDX,
|
---|
48 | TREG_EBX,
|
---|
49 | TREG_ST0,
|
---|
50 | TREG_ESP = 4
|
---|
51 | };
|
---|
52 |
|
---|
53 | /* return registers for function */
|
---|
54 | #define REG_IRET TREG_EAX /* single word int return register */
|
---|
55 | #define REG_LRET TREG_EDX /* second word return register (for long long) */
|
---|
56 | #define REG_FRET TREG_ST0 /* float return register */
|
---|
57 |
|
---|
58 | /* defined if function parameters must be evaluated in reverse order */
|
---|
59 | #define INVERT_FUNC_PARAMS
|
---|
60 |
|
---|
61 | /* defined if structures are passed as pointers. Otherwise structures
|
---|
62 | are directly pushed on stack. */
|
---|
63 | /* #define FUNC_STRUCT_PARAM_AS_PTR */
|
---|
64 |
|
---|
65 | /* pointer size, in bytes */
|
---|
66 | #define PTR_SIZE 4
|
---|
67 |
|
---|
68 | /* long double size and alignment, in bytes */
|
---|
69 | #define LDOUBLE_SIZE 12
|
---|
70 | #define LDOUBLE_ALIGN 4
|
---|
71 | /* maximum alignment (for aligned attribute support) */
|
---|
72 | #define MAX_ALIGN 8
|
---|
73 |
|
---|
74 | /******************************************************/
|
---|
75 | #else /* ! TARGET_DEFS_ONLY */
|
---|
76 | /******************************************************/
|
---|
77 | #include "tcc.h"
|
---|
78 |
|
---|
79 | /* define to 1/0 to [not] have EBX as 4th register */
|
---|
80 | #define USE_EBX 0
|
---|
81 |
|
---|
82 | ST_DATA const int reg_classes[NB_REGS] = {
|
---|
83 | /* eax */ RC_INT | RC_EAX,
|
---|
84 | /* ecx */ RC_INT | RC_ECX,
|
---|
85 | /* edx */ RC_INT | RC_EDX,
|
---|
86 | /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
|
---|
87 | /* st0 */ RC_FLOAT | RC_ST0,
|
---|
88 | };
|
---|
89 |
|
---|
90 | static unsigned long func_sub_sp_offset;
|
---|
91 | static int func_ret_sub;
|
---|
92 | #ifdef CONFIG_TCC_BCHECK
|
---|
93 | static addr_t func_bound_offset;
|
---|
94 | static unsigned long func_bound_ind;
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | /* XXX: make it faster ? */
|
---|
98 | ST_FUNC void g(int c)
|
---|
99 | {
|
---|
100 | int ind1;
|
---|
101 | if (nocode_wanted)
|
---|
102 | return;
|
---|
103 | ind1 = ind + 1;
|
---|
104 | if (ind1 > cur_text_section->data_allocated)
|
---|
105 | section_realloc(cur_text_section, ind1);
|
---|
106 | cur_text_section->data[ind] = c;
|
---|
107 | ind = ind1;
|
---|
108 | }
|
---|
109 |
|
---|
110 | ST_FUNC void o(unsigned int c)
|
---|
111 | {
|
---|
112 | while (c) {
|
---|
113 | g(c);
|
---|
114 | c = c >> 8;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | ST_FUNC void gen_le16(int v)
|
---|
119 | {
|
---|
120 | g(v);
|
---|
121 | g(v >> 8);
|
---|
122 | }
|
---|
123 |
|
---|
124 | ST_FUNC void gen_le32(int c)
|
---|
125 | {
|
---|
126 | g(c);
|
---|
127 | g(c >> 8);
|
---|
128 | g(c >> 16);
|
---|
129 | g(c >> 24);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* output a symbol and patch all calls to it */
|
---|
133 | ST_FUNC void gsym_addr(int t, int a)
|
---|
134 | {
|
---|
135 | while (t) {
|
---|
136 | unsigned char *ptr = cur_text_section->data + t;
|
---|
137 | uint32_t n = read32le(ptr); /* next value */
|
---|
138 | write32le(ptr, a - t - 4);
|
---|
139 | t = n;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | ST_FUNC void gsym(int t)
|
---|
144 | {
|
---|
145 | gsym_addr(t, ind);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /* instruction + 4 bytes data. Return the address of the data */
|
---|
149 | static int oad(int c, int s)
|
---|
150 | {
|
---|
151 | int t;
|
---|
152 | if (nocode_wanted)
|
---|
153 | return s;
|
---|
154 | o(c);
|
---|
155 | t = ind;
|
---|
156 | gen_le32(s);
|
---|
157 | return t;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /* generate jmp to a label */
|
---|
161 | #define gjmp2(instr,lbl) oad(instr,lbl)
|
---|
162 |
|
---|
163 | /* output constant with relocation if 'r & VT_SYM' is true */
|
---|
164 | ST_FUNC void gen_addr32(int r, Sym *sym, int c)
|
---|
165 | {
|
---|
166 | if (r & VT_SYM)
|
---|
167 | greloc(cur_text_section, sym, ind, R_386_32);
|
---|
168 | gen_le32(c);
|
---|
169 | }
|
---|
170 |
|
---|
171 | ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
|
---|
172 | {
|
---|
173 | if (r & VT_SYM)
|
---|
174 | greloc(cur_text_section, sym, ind, R_386_PC32);
|
---|
175 | gen_le32(c - 4);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* generate a modrm reference. 'op_reg' contains the additional 3
|
---|
179 | opcode bits */
|
---|
180 | static void gen_modrm(int op_reg, int r, Sym *sym, int c)
|
---|
181 | {
|
---|
182 | op_reg = op_reg << 3;
|
---|
183 | if ((r & VT_VALMASK) == VT_CONST) {
|
---|
184 | /* constant memory reference */
|
---|
185 | o(0x05 | op_reg);
|
---|
186 | gen_addr32(r, sym, c);
|
---|
187 | } else if ((r & VT_VALMASK) == VT_LOCAL) {
|
---|
188 | /* currently, we use only ebp as base */
|
---|
189 | if (c == (char)c) {
|
---|
190 | /* short reference */
|
---|
191 | o(0x45 | op_reg);
|
---|
192 | g(c);
|
---|
193 | } else {
|
---|
194 | oad(0x85 | op_reg, c);
|
---|
195 | }
|
---|
196 | } else {
|
---|
197 | g(0x00 | op_reg | (r & VT_VALMASK));
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | /* load 'r' from value 'sv' */
|
---|
202 | ST_FUNC void load(int r, SValue *sv)
|
---|
203 | {
|
---|
204 | int v, t, ft, fc, fr;
|
---|
205 | SValue v1;
|
---|
206 |
|
---|
207 | #ifdef TCC_TARGET_PE
|
---|
208 | SValue v2;
|
---|
209 | sv = pe_getimport(sv, &v2);
|
---|
210 | #endif
|
---|
211 |
|
---|
212 | fr = sv->r;
|
---|
213 | ft = sv->type.t & ~VT_DEFSIGN;
|
---|
214 | fc = sv->c.i;
|
---|
215 |
|
---|
216 | ft &= ~(VT_VOLATILE | VT_CONSTANT);
|
---|
217 |
|
---|
218 | v = fr & VT_VALMASK;
|
---|
219 | if (fr & VT_LVAL) {
|
---|
220 | if (v == VT_LLOCAL) {
|
---|
221 | v1.type.t = VT_INT;
|
---|
222 | v1.r = VT_LOCAL | VT_LVAL;
|
---|
223 | v1.c.i = fc;
|
---|
224 | fr = r;
|
---|
225 | if (!(reg_classes[fr] & RC_INT))
|
---|
226 | fr = get_reg(RC_INT);
|
---|
227 | load(fr, &v1);
|
---|
228 | }
|
---|
229 | if ((ft & VT_BTYPE) == VT_FLOAT) {
|
---|
230 | o(0xd9); /* flds */
|
---|
231 | r = 0;
|
---|
232 | } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
|
---|
233 | o(0xdd); /* fldl */
|
---|
234 | r = 0;
|
---|
235 | } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
|
---|
236 | o(0xdb); /* fldt */
|
---|
237 | r = 5;
|
---|
238 | } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
|
---|
239 | o(0xbe0f); /* movsbl */
|
---|
240 | } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
|
---|
241 | o(0xb60f); /* movzbl */
|
---|
242 | } else if ((ft & VT_TYPE) == VT_SHORT) {
|
---|
243 | o(0xbf0f); /* movswl */
|
---|
244 | } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
|
---|
245 | o(0xb70f); /* movzwl */
|
---|
246 | } else {
|
---|
247 | o(0x8b); /* movl */
|
---|
248 | }
|
---|
249 | gen_modrm(r, fr, sv->sym, fc);
|
---|
250 | } else {
|
---|
251 | if (v == VT_CONST) {
|
---|
252 | o(0xb8 + r); /* mov $xx, r */
|
---|
253 | gen_addr32(fr, sv->sym, fc);
|
---|
254 | } else if (v == VT_LOCAL) {
|
---|
255 | if (fc) {
|
---|
256 | o(0x8d); /* lea xxx(%ebp), r */
|
---|
257 | gen_modrm(r, VT_LOCAL, sv->sym, fc);
|
---|
258 | } else {
|
---|
259 | o(0x89);
|
---|
260 | o(0xe8 + r); /* mov %ebp, r */
|
---|
261 | }
|
---|
262 | } else if (v == VT_CMP) {
|
---|
263 | oad(0xb8 + r, 0); /* mov $0, r */
|
---|
264 | o(0x0f); /* setxx %br */
|
---|
265 | o(fc);
|
---|
266 | o(0xc0 + r);
|
---|
267 | } else if (v == VT_JMP || v == VT_JMPI) {
|
---|
268 | t = v & 1;
|
---|
269 | oad(0xb8 + r, t); /* mov $1, r */
|
---|
270 | o(0x05eb); /* jmp after */
|
---|
271 | gsym(fc);
|
---|
272 | oad(0xb8 + r, t ^ 1); /* mov $0, r */
|
---|
273 | } else if (v != r) {
|
---|
274 | o(0x89);
|
---|
275 | o(0xc0 + r + v * 8); /* mov v, r */
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | /* store register 'r' in lvalue 'v' */
|
---|
281 | ST_FUNC void store(int r, SValue *v)
|
---|
282 | {
|
---|
283 | int fr, bt, ft, fc;
|
---|
284 |
|
---|
285 | #ifdef TCC_TARGET_PE
|
---|
286 | SValue v2;
|
---|
287 | v = pe_getimport(v, &v2);
|
---|
288 | #endif
|
---|
289 |
|
---|
290 | ft = v->type.t;
|
---|
291 | fc = v->c.i;
|
---|
292 | fr = v->r & VT_VALMASK;
|
---|
293 | ft &= ~(VT_VOLATILE | VT_CONSTANT);
|
---|
294 | bt = ft & VT_BTYPE;
|
---|
295 | /* XXX: incorrect if float reg to reg */
|
---|
296 | if (bt == VT_FLOAT) {
|
---|
297 | o(0xd9); /* fsts */
|
---|
298 | r = 2;
|
---|
299 | } else if (bt == VT_DOUBLE) {
|
---|
300 | o(0xdd); /* fstpl */
|
---|
301 | r = 2;
|
---|
302 | } else if (bt == VT_LDOUBLE) {
|
---|
303 | o(0xc0d9); /* fld %st(0) */
|
---|
304 | o(0xdb); /* fstpt */
|
---|
305 | r = 7;
|
---|
306 | } else {
|
---|
307 | if (bt == VT_SHORT)
|
---|
308 | o(0x66);
|
---|
309 | if (bt == VT_BYTE || bt == VT_BOOL)
|
---|
310 | o(0x88);
|
---|
311 | else
|
---|
312 | o(0x89);
|
---|
313 | }
|
---|
314 | if (fr == VT_CONST ||
|
---|
315 | fr == VT_LOCAL ||
|
---|
316 | (v->r & VT_LVAL)) {
|
---|
317 | gen_modrm(r, v->r, v->sym, fc);
|
---|
318 | } else if (fr != r) {
|
---|
319 | o(0xc0 + fr + r * 8); /* mov r, fr */
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | static void gadd_sp(int val)
|
---|
324 | {
|
---|
325 | if (val == (char)val) {
|
---|
326 | o(0xc483);
|
---|
327 | g(val);
|
---|
328 | } else {
|
---|
329 | oad(0xc481, val); /* add $xxx, %esp */
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
|
---|
334 | static void gen_static_call(int v)
|
---|
335 | {
|
---|
336 | Sym *sym;
|
---|
337 |
|
---|
338 | sym = external_global_sym(v, &func_old_type, 0);
|
---|
339 | oad(0xe8, -4);
|
---|
340 | greloc(cur_text_section, sym, ind-4, R_386_PC32);
|
---|
341 | }
|
---|
342 | #endif
|
---|
343 |
|
---|
344 | /* 'is_jmp' is '1' if it is a jump */
|
---|
345 | static void gcall_or_jmp(int is_jmp)
|
---|
346 | {
|
---|
347 | int r;
|
---|
348 | if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
|
---|
349 | /* constant and relocation case */
|
---|
350 | greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
|
---|
351 | oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
|
---|
352 | } else {
|
---|
353 | /* otherwise, indirect call */
|
---|
354 | r = gv(RC_INT);
|
---|
355 | o(0xff); /* call/jmp *r */
|
---|
356 | o(0xd0 + r + (is_jmp << 4));
|
---|
357 | }
|
---|
358 | if (!is_jmp) {
|
---|
359 | int rt;
|
---|
360 | /* extend the return value to the whole register if necessary
|
---|
361 | visual studio and gcc do not always set the whole eax register
|
---|
362 | when assigning the return value of a function */
|
---|
363 | rt = vtop->type.ref->type.t;
|
---|
364 | switch (rt & VT_BTYPE) {
|
---|
365 | case VT_BYTE:
|
---|
366 | if (rt & VT_UNSIGNED) {
|
---|
367 | o(0xc0b60f); /* movzx %al, %eax */
|
---|
368 | }
|
---|
369 | else {
|
---|
370 | o(0xc0be0f); /* movsx %al, %eax */
|
---|
371 | }
|
---|
372 | break;
|
---|
373 | case VT_SHORT:
|
---|
374 | if (rt & VT_UNSIGNED) {
|
---|
375 | o(0xc0b70f); /* movzx %ax, %eax */
|
---|
376 | }
|
---|
377 | else {
|
---|
378 | o(0xc0bf0f); /* movsx %ax, %eax */
|
---|
379 | }
|
---|
380 | break;
|
---|
381 | default:
|
---|
382 | break;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
|
---|
388 | static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
|
---|
389 |
|
---|
390 | /* Return the number of registers needed to return the struct, or 0 if
|
---|
391 | returning via struct pointer. */
|
---|
392 | ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
|
---|
393 | {
|
---|
394 | #ifdef TCC_TARGET_PE
|
---|
395 | int size, align;
|
---|
396 | *ret_align = 1; // Never have to re-align return values for x86
|
---|
397 | *regsize = 4;
|
---|
398 | size = type_size(vt, &align);
|
---|
399 | if (size > 8 || (size & (size - 1)))
|
---|
400 | return 0;
|
---|
401 | if (size == 8)
|
---|
402 | ret->t = VT_LLONG;
|
---|
403 | else if (size == 4)
|
---|
404 | ret->t = VT_INT;
|
---|
405 | else if (size == 2)
|
---|
406 | ret->t = VT_SHORT;
|
---|
407 | else
|
---|
408 | ret->t = VT_BYTE;
|
---|
409 | ret->ref = NULL;
|
---|
410 | return 1;
|
---|
411 | #else
|
---|
412 | *ret_align = 1; // Never have to re-align return values for x86
|
---|
413 | return 0;
|
---|
414 | #endif
|
---|
415 | }
|
---|
416 |
|
---|
417 | /* Generate function call. The function address is pushed first, then
|
---|
418 | all the parameters in call order. This functions pops all the
|
---|
419 | parameters and the function address. */
|
---|
420 | ST_FUNC void gfunc_call(int nb_args)
|
---|
421 | {
|
---|
422 | int size, align, r, args_size, i, func_call;
|
---|
423 | Sym *func_sym;
|
---|
424 |
|
---|
425 | args_size = 0;
|
---|
426 | for(i = 0;i < nb_args; i++) {
|
---|
427 | if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
|
---|
428 | size = type_size(&vtop->type, &align);
|
---|
429 | /* align to stack align size */
|
---|
430 | size = (size + 3) & ~3;
|
---|
431 | /* allocate the necessary size on stack */
|
---|
432 | oad(0xec81, size); /* sub $xxx, %esp */
|
---|
433 | /* generate structure store */
|
---|
434 | r = get_reg(RC_INT);
|
---|
435 | o(0x89); /* mov %esp, r */
|
---|
436 | o(0xe0 + r);
|
---|
437 | vset(&vtop->type, r | VT_LVAL, 0);
|
---|
438 | vswap();
|
---|
439 | vstore();
|
---|
440 | args_size += size;
|
---|
441 | } else if (is_float(vtop->type.t)) {
|
---|
442 | gv(RC_FLOAT); /* only one float register */
|
---|
443 | if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
|
---|
444 | size = 4;
|
---|
445 | else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
|
---|
446 | size = 8;
|
---|
447 | else
|
---|
448 | size = 12;
|
---|
449 | oad(0xec81, size); /* sub $xxx, %esp */
|
---|
450 | if (size == 12)
|
---|
451 | o(0x7cdb);
|
---|
452 | else
|
---|
453 | o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
|
---|
454 | g(0x24);
|
---|
455 | g(0x00);
|
---|
456 | args_size += size;
|
---|
457 | } else {
|
---|
458 | /* simple type (currently always same size) */
|
---|
459 | /* XXX: implicit cast ? */
|
---|
460 | r = gv(RC_INT);
|
---|
461 | if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
|
---|
462 | size = 8;
|
---|
463 | o(0x50 + vtop->r2); /* push r */
|
---|
464 | } else {
|
---|
465 | size = 4;
|
---|
466 | }
|
---|
467 | o(0x50 + r); /* push r */
|
---|
468 | args_size += size;
|
---|
469 | }
|
---|
470 | vtop--;
|
---|
471 | }
|
---|
472 | save_regs(0); /* save used temporary registers */
|
---|
473 | func_sym = vtop->type.ref;
|
---|
474 | func_call = func_sym->f.func_call;
|
---|
475 | /* fast call case */
|
---|
476 | if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
|
---|
477 | func_call == FUNC_FASTCALLW) {
|
---|
478 | int fastcall_nb_regs;
|
---|
479 | uint8_t *fastcall_regs_ptr;
|
---|
480 | if (func_call == FUNC_FASTCALLW) {
|
---|
481 | fastcall_regs_ptr = fastcallw_regs;
|
---|
482 | fastcall_nb_regs = 2;
|
---|
483 | } else {
|
---|
484 | fastcall_regs_ptr = fastcall_regs;
|
---|
485 | fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
|
---|
486 | }
|
---|
487 | for(i = 0;i < fastcall_nb_regs; i++) {
|
---|
488 | if (args_size <= 0)
|
---|
489 | break;
|
---|
490 | o(0x58 + fastcall_regs_ptr[i]); /* pop r */
|
---|
491 | /* XXX: incorrect for struct/floats */
|
---|
492 | args_size -= 4;
|
---|
493 | }
|
---|
494 | }
|
---|
495 | #ifndef TCC_TARGET_PE
|
---|
496 | else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
|
---|
497 | args_size -= 4;
|
---|
498 | #endif
|
---|
499 | gcall_or_jmp(0);
|
---|
500 |
|
---|
501 | if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW)
|
---|
502 | gadd_sp(args_size);
|
---|
503 | vtop--;
|
---|
504 | }
|
---|
505 |
|
---|
506 | #ifdef TCC_TARGET_PE
|
---|
507 | #define FUNC_PROLOG_SIZE (10 + USE_EBX)
|
---|
508 | #else
|
---|
509 | #define FUNC_PROLOG_SIZE (9 + USE_EBX)
|
---|
510 | #endif
|
---|
511 |
|
---|
512 | /* generate function prolog of type 't' */
|
---|
513 | ST_FUNC void gfunc_prolog(CType *func_type)
|
---|
514 | {
|
---|
515 | int addr, align, size, func_call, fastcall_nb_regs;
|
---|
516 | int param_index, param_addr;
|
---|
517 | uint8_t *fastcall_regs_ptr;
|
---|
518 | Sym *sym;
|
---|
519 | CType *type;
|
---|
520 |
|
---|
521 | sym = func_type->ref;
|
---|
522 | func_call = sym->f.func_call;
|
---|
523 | addr = 8;
|
---|
524 | loc = 0;
|
---|
525 | func_vc = 0;
|
---|
526 |
|
---|
527 | if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
|
---|
528 | fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
|
---|
529 | fastcall_regs_ptr = fastcall_regs;
|
---|
530 | } else if (func_call == FUNC_FASTCALLW) {
|
---|
531 | fastcall_nb_regs = 2;
|
---|
532 | fastcall_regs_ptr = fastcallw_regs;
|
---|
533 | } else {
|
---|
534 | fastcall_nb_regs = 0;
|
---|
535 | fastcall_regs_ptr = NULL;
|
---|
536 | }
|
---|
537 | param_index = 0;
|
---|
538 |
|
---|
539 | ind += FUNC_PROLOG_SIZE;
|
---|
540 | func_sub_sp_offset = ind;
|
---|
541 | /* if the function returns a structure, then add an
|
---|
542 | implicit pointer parameter */
|
---|
543 | func_vt = sym->type;
|
---|
544 | func_var = (sym->f.func_type == FUNC_ELLIPSIS);
|
---|
545 | #ifdef TCC_TARGET_PE
|
---|
546 | size = type_size(&func_vt,&align);
|
---|
547 | if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
|
---|
548 | && (size > 8 || (size & (size - 1)))) {
|
---|
549 | #else
|
---|
550 | if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
|
---|
551 | #endif
|
---|
552 | /* XXX: fastcall case ? */
|
---|
553 | func_vc = addr;
|
---|
554 | addr += 4;
|
---|
555 | param_index++;
|
---|
556 | }
|
---|
557 | /* define parameters */
|
---|
558 | while ((sym = sym->next) != NULL) {
|
---|
559 | type = &sym->type;
|
---|
560 | size = type_size(type, &align);
|
---|
561 | size = (size + 3) & ~3;
|
---|
562 | #ifdef FUNC_STRUCT_PARAM_AS_PTR
|
---|
563 | /* structs are passed as pointer */
|
---|
564 | if ((type->t & VT_BTYPE) == VT_STRUCT) {
|
---|
565 | size = 4;
|
---|
566 | }
|
---|
567 | #endif
|
---|
568 | if (param_index < fastcall_nb_regs) {
|
---|
569 | /* save FASTCALL register */
|
---|
570 | loc -= 4;
|
---|
571 | o(0x89); /* movl */
|
---|
572 | gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
|
---|
573 | param_addr = loc;
|
---|
574 | } else {
|
---|
575 | param_addr = addr;
|
---|
576 | addr += size;
|
---|
577 | }
|
---|
578 | sym_push(sym->v & ~SYM_FIELD, type,
|
---|
579 | VT_LOCAL | lvalue_type(type->t), param_addr);
|
---|
580 | param_index++;
|
---|
581 | }
|
---|
582 | func_ret_sub = 0;
|
---|
583 | /* pascal type call or fastcall ? */
|
---|
584 | if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW)
|
---|
585 | func_ret_sub = addr - 8;
|
---|
586 | #ifndef TCC_TARGET_PE
|
---|
587 | else if (func_vc)
|
---|
588 | func_ret_sub = 4;
|
---|
589 | #endif
|
---|
590 |
|
---|
591 | #ifdef CONFIG_TCC_BCHECK
|
---|
592 | /* leave some room for bound checking code */
|
---|
593 | if (tcc_state->do_bounds_check) {
|
---|
594 | func_bound_offset = lbounds_section->data_offset;
|
---|
595 | func_bound_ind = ind;
|
---|
596 | oad(0xb8, 0); /* lbound section pointer */
|
---|
597 | oad(0xb8, 0); /* call to function */
|
---|
598 | }
|
---|
599 | #endif
|
---|
600 | }
|
---|
601 |
|
---|
602 | /* generate function epilog */
|
---|
603 | ST_FUNC void gfunc_epilog(void)
|
---|
604 | {
|
---|
605 | addr_t v, saved_ind;
|
---|
606 |
|
---|
607 | #ifdef CONFIG_TCC_BCHECK
|
---|
608 | if (tcc_state->do_bounds_check
|
---|
609 | && func_bound_offset != lbounds_section->data_offset) {
|
---|
610 | addr_t saved_ind;
|
---|
611 | addr_t *bounds_ptr;
|
---|
612 | Sym *sym_data;
|
---|
613 |
|
---|
614 | /* add end of table info */
|
---|
615 | bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
|
---|
616 | *bounds_ptr = 0;
|
---|
617 |
|
---|
618 | /* generate bound local allocation */
|
---|
619 | saved_ind = ind;
|
---|
620 | ind = func_bound_ind;
|
---|
621 | sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
|
---|
622 | func_bound_offset, lbounds_section->data_offset);
|
---|
623 | greloc(cur_text_section, sym_data,
|
---|
624 | ind + 1, R_386_32);
|
---|
625 | oad(0xb8, 0); /* mov %eax, xxx */
|
---|
626 | gen_static_call(TOK___bound_local_new);
|
---|
627 | ind = saved_ind;
|
---|
628 |
|
---|
629 | /* generate bound check local freeing */
|
---|
630 | o(0x5250); /* save returned value, if any */
|
---|
631 | greloc(cur_text_section, sym_data, ind + 1, R_386_32);
|
---|
632 | oad(0xb8, 0); /* mov %eax, xxx */
|
---|
633 | gen_static_call(TOK___bound_local_delete);
|
---|
634 | o(0x585a); /* restore returned value, if any */
|
---|
635 | }
|
---|
636 | #endif
|
---|
637 |
|
---|
638 | /* align local size to word & save local variables */
|
---|
639 | v = (-loc + 3) & -4;
|
---|
640 |
|
---|
641 | #if USE_EBX
|
---|
642 | o(0x8b);
|
---|
643 | gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
|
---|
644 | #endif
|
---|
645 |
|
---|
646 | o(0xc9); /* leave */
|
---|
647 | if (func_ret_sub == 0) {
|
---|
648 | o(0xc3); /* ret */
|
---|
649 | } else {
|
---|
650 | o(0xc2); /* ret n */
|
---|
651 | g(func_ret_sub);
|
---|
652 | g(func_ret_sub >> 8);
|
---|
653 | }
|
---|
654 | saved_ind = ind;
|
---|
655 | ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
|
---|
656 | #ifdef TCC_TARGET_PE
|
---|
657 | if (v >= 4096) {
|
---|
658 | oad(0xb8, v); /* mov stacksize, %eax */
|
---|
659 | gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
|
---|
660 | } else
|
---|
661 | #endif
|
---|
662 | {
|
---|
663 | o(0xe58955); /* push %ebp, mov %esp, %ebp */
|
---|
664 | o(0xec81); /* sub esp, stacksize */
|
---|
665 | gen_le32(v);
|
---|
666 | #ifdef TCC_TARGET_PE
|
---|
667 | o(0x90); /* adjust to FUNC_PROLOG_SIZE */
|
---|
668 | #endif
|
---|
669 | }
|
---|
670 | o(0x53 * USE_EBX); /* push ebx */
|
---|
671 | ind = saved_ind;
|
---|
672 | }
|
---|
673 |
|
---|
674 | /* generate a jump to a label */
|
---|
675 | ST_FUNC int gjmp(int t)
|
---|
676 | {
|
---|
677 | return gjmp2(0xe9, t);
|
---|
678 | }
|
---|
679 |
|
---|
680 | /* generate a jump to a fixed address */
|
---|
681 | ST_FUNC void gjmp_addr(int a)
|
---|
682 | {
|
---|
683 | int r;
|
---|
684 | r = a - ind - 2;
|
---|
685 | if (r == (char)r) {
|
---|
686 | g(0xeb);
|
---|
687 | g(r);
|
---|
688 | } else {
|
---|
689 | oad(0xe9, a - ind - 5);
|
---|
690 | }
|
---|
691 | }
|
---|
692 |
|
---|
693 | ST_FUNC void gtst_addr(int inv, int a)
|
---|
694 | {
|
---|
695 | int v = vtop->r & VT_VALMASK;
|
---|
696 | if (v == VT_CMP) {
|
---|
697 | inv ^= (vtop--)->c.i;
|
---|
698 | a -= ind + 2;
|
---|
699 | if (a == (char)a) {
|
---|
700 | g(inv - 32);
|
---|
701 | g(a);
|
---|
702 | } else {
|
---|
703 | g(0x0f);
|
---|
704 | oad(inv - 16, a - 4);
|
---|
705 | }
|
---|
706 | } else if ((v & ~1) == VT_JMP) {
|
---|
707 | if ((v & 1) != inv) {
|
---|
708 | gjmp_addr(a);
|
---|
709 | gsym(vtop->c.i);
|
---|
710 | } else {
|
---|
711 | gsym(vtop->c.i);
|
---|
712 | o(0x05eb);
|
---|
713 | gjmp_addr(a);
|
---|
714 | }
|
---|
715 | vtop--;
|
---|
716 | }
|
---|
717 | }
|
---|
718 |
|
---|
719 | /* generate a test. set 'inv' to invert test. Stack entry is popped */
|
---|
720 | ST_FUNC int gtst(int inv, int t)
|
---|
721 | {
|
---|
722 | int v = vtop->r & VT_VALMASK;
|
---|
723 | if (nocode_wanted) {
|
---|
724 | ;
|
---|
725 | } else if (v == VT_CMP) {
|
---|
726 | /* fast case : can jump directly since flags are set */
|
---|
727 | g(0x0f);
|
---|
728 | t = gjmp2((vtop->c.i - 16) ^ inv, t);
|
---|
729 | } else if (v == VT_JMP || v == VT_JMPI) {
|
---|
730 | /* && or || optimization */
|
---|
731 | if ((v & 1) == inv) {
|
---|
732 | /* insert vtop->c jump list in t */
|
---|
733 | uint32_t n1, n = vtop->c.i;
|
---|
734 | if (n) {
|
---|
735 | while ((n1 = read32le(cur_text_section->data + n)))
|
---|
736 | n = n1;
|
---|
737 | write32le(cur_text_section->data + n, t);
|
---|
738 | t = vtop->c.i;
|
---|
739 | }
|
---|
740 | } else {
|
---|
741 | t = gjmp(t);
|
---|
742 | gsym(vtop->c.i);
|
---|
743 | }
|
---|
744 | }
|
---|
745 | vtop--;
|
---|
746 | return t;
|
---|
747 | }
|
---|
748 |
|
---|
749 | /* generate an integer binary operation */
|
---|
750 | ST_FUNC void gen_opi(int op)
|
---|
751 | {
|
---|
752 | int r, fr, opc, c;
|
---|
753 |
|
---|
754 | switch(op) {
|
---|
755 | case '+':
|
---|
756 | case TOK_ADDC1: /* add with carry generation */
|
---|
757 | opc = 0;
|
---|
758 | gen_op8:
|
---|
759 | if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
---|
760 | /* constant case */
|
---|
761 | vswap();
|
---|
762 | r = gv(RC_INT);
|
---|
763 | vswap();
|
---|
764 | c = vtop->c.i;
|
---|
765 | if (c == (char)c) {
|
---|
766 | /* generate inc and dec for smaller code */
|
---|
767 | if (c==1 && opc==0 && op != TOK_ADDC1) {
|
---|
768 | o (0x40 | r); // inc
|
---|
769 | } else if (c==1 && opc==5 && op != TOK_SUBC1) {
|
---|
770 | o (0x48 | r); // dec
|
---|
771 | } else {
|
---|
772 | o(0x83);
|
---|
773 | o(0xc0 | (opc << 3) | r);
|
---|
774 | g(c);
|
---|
775 | }
|
---|
776 | } else {
|
---|
777 | o(0x81);
|
---|
778 | oad(0xc0 | (opc << 3) | r, c);
|
---|
779 | }
|
---|
780 | } else {
|
---|
781 | gv2(RC_INT, RC_INT);
|
---|
782 | r = vtop[-1].r;
|
---|
783 | fr = vtop[0].r;
|
---|
784 | o((opc << 3) | 0x01);
|
---|
785 | o(0xc0 + r + fr * 8);
|
---|
786 | }
|
---|
787 | vtop--;
|
---|
788 | if (op >= TOK_ULT && op <= TOK_GT) {
|
---|
789 | vtop->r = VT_CMP;
|
---|
790 | vtop->c.i = op;
|
---|
791 | }
|
---|
792 | break;
|
---|
793 | case '-':
|
---|
794 | case TOK_SUBC1: /* sub with carry generation */
|
---|
795 | opc = 5;
|
---|
796 | goto gen_op8;
|
---|
797 | case TOK_ADDC2: /* add with carry use */
|
---|
798 | opc = 2;
|
---|
799 | goto gen_op8;
|
---|
800 | case TOK_SUBC2: /* sub with carry use */
|
---|
801 | opc = 3;
|
---|
802 | goto gen_op8;
|
---|
803 | case '&':
|
---|
804 | opc = 4;
|
---|
805 | goto gen_op8;
|
---|
806 | case '^':
|
---|
807 | opc = 6;
|
---|
808 | goto gen_op8;
|
---|
809 | case '|':
|
---|
810 | opc = 1;
|
---|
811 | goto gen_op8;
|
---|
812 | case '*':
|
---|
813 | gv2(RC_INT, RC_INT);
|
---|
814 | r = vtop[-1].r;
|
---|
815 | fr = vtop[0].r;
|
---|
816 | vtop--;
|
---|
817 | o(0xaf0f); /* imul fr, r */
|
---|
818 | o(0xc0 + fr + r * 8);
|
---|
819 | break;
|
---|
820 | case TOK_SHL:
|
---|
821 | opc = 4;
|
---|
822 | goto gen_shift;
|
---|
823 | case TOK_SHR:
|
---|
824 | opc = 5;
|
---|
825 | goto gen_shift;
|
---|
826 | case TOK_SAR:
|
---|
827 | opc = 7;
|
---|
828 | gen_shift:
|
---|
829 | opc = 0xc0 | (opc << 3);
|
---|
830 | if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
---|
831 | /* constant case */
|
---|
832 | vswap();
|
---|
833 | r = gv(RC_INT);
|
---|
834 | vswap();
|
---|
835 | c = vtop->c.i & 0x1f;
|
---|
836 | o(0xc1); /* shl/shr/sar $xxx, r */
|
---|
837 | o(opc | r);
|
---|
838 | g(c);
|
---|
839 | } else {
|
---|
840 | /* we generate the shift in ecx */
|
---|
841 | gv2(RC_INT, RC_ECX);
|
---|
842 | r = vtop[-1].r;
|
---|
843 | o(0xd3); /* shl/shr/sar %cl, r */
|
---|
844 | o(opc | r);
|
---|
845 | }
|
---|
846 | vtop--;
|
---|
847 | break;
|
---|
848 | case '/':
|
---|
849 | case TOK_UDIV:
|
---|
850 | case TOK_PDIV:
|
---|
851 | case '%':
|
---|
852 | case TOK_UMOD:
|
---|
853 | case TOK_UMULL:
|
---|
854 | /* first operand must be in eax */
|
---|
855 | /* XXX: need better constraint for second operand */
|
---|
856 | gv2(RC_EAX, RC_ECX);
|
---|
857 | r = vtop[-1].r;
|
---|
858 | fr = vtop[0].r;
|
---|
859 | vtop--;
|
---|
860 | save_reg(TREG_EDX);
|
---|
861 | /* save EAX too if used otherwise */
|
---|
862 | save_reg_upstack(TREG_EAX, 1);
|
---|
863 | if (op == TOK_UMULL) {
|
---|
864 | o(0xf7); /* mul fr */
|
---|
865 | o(0xe0 + fr);
|
---|
866 | vtop->r2 = TREG_EDX;
|
---|
867 | r = TREG_EAX;
|
---|
868 | } else {
|
---|
869 | if (op == TOK_UDIV || op == TOK_UMOD) {
|
---|
870 | o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
|
---|
871 | o(0xf0 + fr);
|
---|
872 | } else {
|
---|
873 | o(0xf799); /* cltd, idiv fr, %eax */
|
---|
874 | o(0xf8 + fr);
|
---|
875 | }
|
---|
876 | if (op == '%' || op == TOK_UMOD)
|
---|
877 | r = TREG_EDX;
|
---|
878 | else
|
---|
879 | r = TREG_EAX;
|
---|
880 | }
|
---|
881 | vtop->r = r;
|
---|
882 | break;
|
---|
883 | default:
|
---|
884 | opc = 7;
|
---|
885 | goto gen_op8;
|
---|
886 | }
|
---|
887 | }
|
---|
888 |
|
---|
889 | /* generate a floating point operation 'v = t1 op t2' instruction. The
|
---|
890 | two operands are guaranteed to have the same floating point type */
|
---|
891 | /* XXX: need to use ST1 too */
|
---|
892 | ST_FUNC void gen_opf(int op)
|
---|
893 | {
|
---|
894 | int a, ft, fc, swapped, r;
|
---|
895 |
|
---|
896 | /* convert constants to memory references */
|
---|
897 | if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
---|
898 | vswap();
|
---|
899 | gv(RC_FLOAT);
|
---|
900 | vswap();
|
---|
901 | }
|
---|
902 | if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
|
---|
903 | gv(RC_FLOAT);
|
---|
904 |
|
---|
905 | /* must put at least one value in the floating point register */
|
---|
906 | if ((vtop[-1].r & VT_LVAL) &&
|
---|
907 | (vtop[0].r & VT_LVAL)) {
|
---|
908 | vswap();
|
---|
909 | gv(RC_FLOAT);
|
---|
910 | vswap();
|
---|
911 | }
|
---|
912 | swapped = 0;
|
---|
913 | /* swap the stack if needed so that t1 is the register and t2 is
|
---|
914 | the memory reference */
|
---|
915 | if (vtop[-1].r & VT_LVAL) {
|
---|
916 | vswap();
|
---|
917 | swapped = 1;
|
---|
918 | }
|
---|
919 | if (op >= TOK_ULT && op <= TOK_GT) {
|
---|
920 | /* load on stack second operand */
|
---|
921 | load(TREG_ST0, vtop);
|
---|
922 | save_reg(TREG_EAX); /* eax is used by FP comparison code */
|
---|
923 | if (op == TOK_GE || op == TOK_GT)
|
---|
924 | swapped = !swapped;
|
---|
925 | else if (op == TOK_EQ || op == TOK_NE)
|
---|
926 | swapped = 0;
|
---|
927 | if (swapped)
|
---|
928 | o(0xc9d9); /* fxch %st(1) */
|
---|
929 | if (op == TOK_EQ || op == TOK_NE)
|
---|
930 | o(0xe9da); /* fucompp */
|
---|
931 | else
|
---|
932 | o(0xd9de); /* fcompp */
|
---|
933 | o(0xe0df); /* fnstsw %ax */
|
---|
934 | if (op == TOK_EQ) {
|
---|
935 | o(0x45e480); /* and $0x45, %ah */
|
---|
936 | o(0x40fC80); /* cmp $0x40, %ah */
|
---|
937 | } else if (op == TOK_NE) {
|
---|
938 | o(0x45e480); /* and $0x45, %ah */
|
---|
939 | o(0x40f480); /* xor $0x40, %ah */
|
---|
940 | op = TOK_NE;
|
---|
941 | } else if (op == TOK_GE || op == TOK_LE) {
|
---|
942 | o(0x05c4f6); /* test $0x05, %ah */
|
---|
943 | op = TOK_EQ;
|
---|
944 | } else {
|
---|
945 | o(0x45c4f6); /* test $0x45, %ah */
|
---|
946 | op = TOK_EQ;
|
---|
947 | }
|
---|
948 | vtop--;
|
---|
949 | vtop->r = VT_CMP;
|
---|
950 | vtop->c.i = op;
|
---|
951 | } else {
|
---|
952 | /* no memory reference possible for long double operations */
|
---|
953 | if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
|
---|
954 | load(TREG_ST0, vtop);
|
---|
955 | swapped = !swapped;
|
---|
956 | }
|
---|
957 |
|
---|
958 | switch(op) {
|
---|
959 | default:
|
---|
960 | case '+':
|
---|
961 | a = 0;
|
---|
962 | break;
|
---|
963 | case '-':
|
---|
964 | a = 4;
|
---|
965 | if (swapped)
|
---|
966 | a++;
|
---|
967 | break;
|
---|
968 | case '*':
|
---|
969 | a = 1;
|
---|
970 | break;
|
---|
971 | case '/':
|
---|
972 | a = 6;
|
---|
973 | if (swapped)
|
---|
974 | a++;
|
---|
975 | break;
|
---|
976 | }
|
---|
977 | ft = vtop->type.t;
|
---|
978 | fc = vtop->c.i;
|
---|
979 | if ((ft & VT_BTYPE) == VT_LDOUBLE) {
|
---|
980 | o(0xde); /* fxxxp %st, %st(1) */
|
---|
981 | o(0xc1 + (a << 3));
|
---|
982 | } else {
|
---|
983 | /* if saved lvalue, then we must reload it */
|
---|
984 | r = vtop->r;
|
---|
985 | if ((r & VT_VALMASK) == VT_LLOCAL) {
|
---|
986 | SValue v1;
|
---|
987 | r = get_reg(RC_INT);
|
---|
988 | v1.type.t = VT_INT;
|
---|
989 | v1.r = VT_LOCAL | VT_LVAL;
|
---|
990 | v1.c.i = fc;
|
---|
991 | load(r, &v1);
|
---|
992 | fc = 0;
|
---|
993 | }
|
---|
994 |
|
---|
995 | if ((ft & VT_BTYPE) == VT_DOUBLE)
|
---|
996 | o(0xdc);
|
---|
997 | else
|
---|
998 | o(0xd8);
|
---|
999 | gen_modrm(a, r, vtop->sym, fc);
|
---|
1000 | }
|
---|
1001 | vtop--;
|
---|
1002 | }
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
|
---|
1006 | and 'long long' cases. */
|
---|
1007 | ST_FUNC void gen_cvt_itof(int t)
|
---|
1008 | {
|
---|
1009 | save_reg(TREG_ST0);
|
---|
1010 | gv(RC_INT);
|
---|
1011 | if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
|
---|
1012 | /* signed long long to float/double/long double (unsigned case
|
---|
1013 | is handled generically) */
|
---|
1014 | o(0x50 + vtop->r2); /* push r2 */
|
---|
1015 | o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
|
---|
1016 | o(0x242cdf); /* fildll (%esp) */
|
---|
1017 | o(0x08c483); /* add $8, %esp */
|
---|
1018 | } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
|
---|
1019 | (VT_INT | VT_UNSIGNED)) {
|
---|
1020 | /* unsigned int to float/double/long double */
|
---|
1021 | o(0x6a); /* push $0 */
|
---|
1022 | g(0x00);
|
---|
1023 | o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
|
---|
1024 | o(0x242cdf); /* fildll (%esp) */
|
---|
1025 | o(0x08c483); /* add $8, %esp */
|
---|
1026 | } else {
|
---|
1027 | /* int to float/double/long double */
|
---|
1028 | o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
|
---|
1029 | o(0x2404db); /* fildl (%esp) */
|
---|
1030 | o(0x04c483); /* add $4, %esp */
|
---|
1031 | }
|
---|
1032 | vtop->r = TREG_ST0;
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | /* convert fp to int 't' type */
|
---|
1036 | ST_FUNC void gen_cvt_ftoi(int t)
|
---|
1037 | {
|
---|
1038 | int bt = vtop->type.t & VT_BTYPE;
|
---|
1039 | if (bt == VT_FLOAT)
|
---|
1040 | vpush_global_sym(&func_old_type, TOK___fixsfdi);
|
---|
1041 | else if (bt == VT_LDOUBLE)
|
---|
1042 | vpush_global_sym(&func_old_type, TOK___fixxfdi);
|
---|
1043 | else
|
---|
1044 | vpush_global_sym(&func_old_type, TOK___fixdfdi);
|
---|
1045 | vswap();
|
---|
1046 | gfunc_call(1);
|
---|
1047 | vpushi(0);
|
---|
1048 | vtop->r = REG_IRET;
|
---|
1049 | vtop->r2 = REG_LRET;
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | /* convert from one floating point type to another */
|
---|
1053 | ST_FUNC void gen_cvt_ftof(int t)
|
---|
1054 | {
|
---|
1055 | /* all we have to do on i386 is to put the float in a register */
|
---|
1056 | gv(RC_FLOAT);
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | /* computed goto support */
|
---|
1060 | ST_FUNC void ggoto(void)
|
---|
1061 | {
|
---|
1062 | gcall_or_jmp(1);
|
---|
1063 | vtop--;
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | /* bound check support functions */
|
---|
1067 | #ifdef CONFIG_TCC_BCHECK
|
---|
1068 |
|
---|
1069 | /* generate a bounded pointer addition */
|
---|
1070 | ST_FUNC void gen_bounded_ptr_add(void)
|
---|
1071 | {
|
---|
1072 | /* prepare fast i386 function call (args in eax and edx) */
|
---|
1073 | gv2(RC_EAX, RC_EDX);
|
---|
1074 | /* save all temporary registers */
|
---|
1075 | vtop -= 2;
|
---|
1076 | save_regs(0);
|
---|
1077 | /* do a fast function call */
|
---|
1078 | gen_static_call(TOK___bound_ptr_add);
|
---|
1079 | /* returned pointer is in eax */
|
---|
1080 | vtop++;
|
---|
1081 | vtop->r = TREG_EAX | VT_BOUNDED;
|
---|
1082 | /* address of bounding function call point */
|
---|
1083 | vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | /* patch pointer addition in vtop so that pointer dereferencing is
|
---|
1087 | also tested */
|
---|
1088 | ST_FUNC void gen_bounded_ptr_deref(void)
|
---|
1089 | {
|
---|
1090 | addr_t func;
|
---|
1091 | int size, align;
|
---|
1092 | Elf32_Rel *rel;
|
---|
1093 | Sym *sym;
|
---|
1094 |
|
---|
1095 | size = 0;
|
---|
1096 | /* XXX: put that code in generic part of tcc */
|
---|
1097 | if (!is_float(vtop->type.t)) {
|
---|
1098 | if (vtop->r & VT_LVAL_BYTE)
|
---|
1099 | size = 1;
|
---|
1100 | else if (vtop->r & VT_LVAL_SHORT)
|
---|
1101 | size = 2;
|
---|
1102 | }
|
---|
1103 | if (!size)
|
---|
1104 | size = type_size(&vtop->type, &align);
|
---|
1105 | switch(size) {
|
---|
1106 | case 1: func = TOK___bound_ptr_indir1; break;
|
---|
1107 | case 2: func = TOK___bound_ptr_indir2; break;
|
---|
1108 | case 4: func = TOK___bound_ptr_indir4; break;
|
---|
1109 | case 8: func = TOK___bound_ptr_indir8; break;
|
---|
1110 | case 12: func = TOK___bound_ptr_indir12; break;
|
---|
1111 | case 16: func = TOK___bound_ptr_indir16; break;
|
---|
1112 | default:
|
---|
1113 | tcc_error("unhandled size when dereferencing bounded pointer");
|
---|
1114 | func = 0;
|
---|
1115 | break;
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | /* patch relocation */
|
---|
1119 | /* XXX: find a better solution ? */
|
---|
1120 | rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
|
---|
1121 | sym = external_global_sym(func, &func_old_type, 0);
|
---|
1122 | if (!sym->c)
|
---|
1123 | put_extern_sym(sym, NULL, 0, 0);
|
---|
1124 | rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
|
---|
1125 | }
|
---|
1126 | #endif
|
---|
1127 |
|
---|
1128 | /* Save the stack pointer onto the stack */
|
---|
1129 | ST_FUNC void gen_vla_sp_save(int addr) {
|
---|
1130 | /* mov %esp,addr(%ebp)*/
|
---|
1131 | o(0x89);
|
---|
1132 | gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | /* Restore the SP from a location on the stack */
|
---|
1136 | ST_FUNC void gen_vla_sp_restore(int addr) {
|
---|
1137 | o(0x8b);
|
---|
1138 | gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | /* Subtract from the stack pointer, and push the resulting value onto the stack */
|
---|
1142 | ST_FUNC void gen_vla_alloc(CType *type, int align) {
|
---|
1143 | #ifdef TCC_TARGET_PE
|
---|
1144 | /* alloca does more than just adjust %rsp on Windows */
|
---|
1145 | vpush_global_sym(&func_old_type, TOK_alloca);
|
---|
1146 | vswap(); /* Move alloca ref past allocation size */
|
---|
1147 | gfunc_call(1);
|
---|
1148 | #else
|
---|
1149 | int r;
|
---|
1150 | r = gv(RC_INT); /* allocation size */
|
---|
1151 | /* sub r,%rsp */
|
---|
1152 | o(0x2b);
|
---|
1153 | o(0xe0 | r);
|
---|
1154 | /* We align to 16 bytes rather than align */
|
---|
1155 | /* and ~15, %esp */
|
---|
1156 | o(0xf0e483);
|
---|
1157 | vpop();
|
---|
1158 | #endif
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | /* end of X86 code generator */
|
---|
1162 | /*************************************************************/
|
---|
1163 | #endif
|
---|
1164 | /*************************************************************/
|
---|