Unified · Split
| @@ -71,12 +71,14 @@ int reg_classes[NB_REGS] = { · 70 unmodified lines | ||
| 71 | 71 | /* function call context */ |
| 72 | 72 | typedef struct GFuncContext { |
| 73 | 73 | int args_size; |
| 74 | + int func_call; /* func call type (FUNC_STDCALL or FUNC_CDECL) */ | |
| 74 | 75 | } GFuncContext; |
| 75 | 76 | |
| 76 | 77 | /******************************************************/ |
| 77 | 78 | |
| 78 | 79 | static int *func_sub_sp_ptr; |
| 79 | 80 | static unsigned char *func_bound_ptr; |
| 81 | +static int func_ret_sub; | |
| 80 | 82 | |
| 81 | 83 | void g(int c) |
| 82 | 84 | { |
| @@ -279,9 +281,10 @@ void store(int r, SValue *v) · 196 unmodified lines | ||
| 279 | 281 | } |
| 280 | 282 | |
| 281 | 283 | /* start function call and return function call context */ |
| 282 | −void gfunc_start(GFuncContext *c) | |
| 284 | +void gfunc_start(GFuncContext *c, int func_call) | |
| 283 | 285 | { |
| 284 | 286 | c->args_size = 0; |
| 287 | + c->func_call = func_call; | |
| 285 | 288 | } |
| 286 | 289 | |
| 287 | 290 | /* push function parameter which is in (vtop->t, vtop->c). Stack entry |
| @@ -356,7 +359,7 @@ void gfunc_call(GFuncContext *c) · 68 unmodified lines | ||
| 356 | 359 | o(0xff); /* call *r */ |
| 357 | 360 | o(0xd0 + r); |
| 358 | 361 | } |
| 359 | − if (c->args_size) | |
| 362 | + if (c->args_size && c->func_call == FUNC_CDECL) | |
| 360 | 363 | oad(0xc481, c->args_size); /* add $xxx, %esp */ |
| 361 | 364 | vtop--; |
| 362 | 365 | } |
| @@ -364,10 +367,11 @@ void gfunc_call(GFuncContext *c) · 1 unmodified line | ||
| 364 | 367 | /* generate function prolog of type 't' */ |
| 365 | 368 | void gfunc_prolog(int t) |
| 366 | 369 | { |
| 367 | − int addr, align, size, u; | |
| 370 | + int addr, align, size, u, func_call; | |
| 368 | 371 | Sym *sym; |
| 369 | 372 | |
| 370 | 373 | sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT); |
| 374 | + func_call = sym->r; | |
| 371 | 375 | addr = 8; |
| 372 | 376 | /* if the function returns a structure, then add an |
| 373 | 377 | implicit pointer parameter */ |
| @@ -391,6 +395,10 @@ void gfunc_prolog(int t) · 17 unmodified lines | ||
| 391 | 395 | #endif |
| 392 | 396 | addr += size; |
| 393 | 397 | } |
| 398 | + func_ret_sub = 0; | |
| 399 | + /* pascal type call ? */ | |
| 400 | + if (func_call == FUNC_STDCALL) | |
| 401 | + func_ret_sub = addr - 8; | |
| 394 | 402 | o(0xe58955); /* push %ebp, mov %esp, %ebp */ |
| 395 | 403 | func_sub_sp_ptr = (int *)oad(0xec81, 0); /* sub $xxx, %esp */ |
| 396 | 404 | /* leave some room for bound checking code */ |
| @@ -423,7 +431,14 @@ void gfunc_epilog(void) · 26 unmodified lines | ||
| 423 | 431 | oad(0xe8, (int)__bound_local_delete - ind - 5); |
| 424 | 432 | o(0x585a); /* restore returned value, if any */ |
| 425 | 433 | } |
| 426 | − o(0xc3c9); /* leave, ret */ | |
| 434 | + o(0xc9); /* leave */ | |
| 435 | + if (func_ret_sub == 0) { | |
| 436 | + o(0xc3); /* ret */ | |
| 437 | + } else { | |
| 438 | + o(0xc2); /* ret n */ | |
| 439 | + g(func_ret_sub); | |
| 440 | + g(func_ret_sub >> 8); | |
| 441 | + } | |
| 427 | 442 | /* align local size to word & save local variables */ |
| 428 | 443 | *func_sub_sp_ptr = (-loc + 3) & -4; |
| 429 | 444 | } |
| @@ -613,7 +628,7 @@ void gen_opi(int op) · 183 unmodified lines | ||
| 613 | 628 | /* XXX: need to use ST1 too */ |
| 614 | 629 | void gen_opf(int op) |
| 615 | 630 | { |
| 616 | − int a, ft, fc, swapped; | |
| 631 | + int a, ft, fc, swapped, r; | |
| 617 | 632 | |
| 618 | 633 | /* convert constants to memory references */ |
| 619 | 634 | if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { |
| @@ -634,6 +649,7 @@ void gen_opf(int op) · 14 unmodified lines | ||
| 634 | 649 | if (op >= TOK_ULT && op <= TOK_GT) { |
| 635 | 650 | /* load on stack second operand */ |
| 636 | 651 | load(REG_ST0, vtop); |
| 652 | + save_reg(REG_EAX); /* eax is used by FP comparison code */ | |
| 637 | 653 | if (op == TOK_GE || op == TOK_GT) |
| 638 | 654 | o(0xc9d9); /* fxch %st(1) */ |
| 639 | 655 | o(0xe9da); /* fucompp */ |
| @@ -694,11 +710,23 @@ void gen_opf(int op) · 54 unmodified lines | ||
| 694 | 710 | o(0xde); /* fxxxp %st, %st(1) */ |
| 695 | 711 | o(0xc1 + (a << 3)); |
| 696 | 712 | } else { |
| 713 | + /* if saved lvalue, then we must reload it */ | |
| 714 | + r = vtop->r; | |
| 715 | + if ((r & VT_VALMASK) == VT_LLOCAL) { | |
| 716 | + SValue v1; | |
| 717 | + r = get_reg(RC_INT); | |
| 718 | + v1.t = VT_INT; | |
| 719 | + v1.r = VT_LOCAL | VT_LVAL; | |
| 720 | + v1.c.ul = fc; | |
| 721 | + load(r, &v1); | |
| 722 | + fc = 0; | |
| 723 | + } | |
| 724 | + | |
| 697 | 725 | if ((ft & VT_BTYPE) == VT_DOUBLE) |
| 698 | 726 | o(0xdc); |
| 699 | 727 | else |
| 700 | 728 | o(0xd8); |
| 701 | − gen_modrm(a, vtop->r, fc); | |
| 729 | + gen_modrm(a, r, fc); | |
| 702 | 730 | } |
| 703 | 731 | vtop--; |
| 704 | 732 | } |
| @@ -714,6 +742,7 @@ static unsigned short __tcc_int_fpu_control = 0x137f | 0x0c00; · 9 unmodified lines | ||
| 714 | 742 | and 'long long' cases. */ |
| 715 | 743 | void gen_cvt_itof(int t) |
| 716 | 744 | { |
| 745 | + save_reg(REG_ST0); | |
| 717 | 746 | gv(RC_INT); |
| 718 | 747 | if ((vtop->t & VT_BTYPE) == VT_LLONG) { |
| 719 | 748 | /* signed long long to float/double/long double (unsigned case |