The procedure calling standard for ARM architecture mandate the use of the base standard for variadic function. Therefore, hgen float aggregate must be returned via stack when greater than 4 bytes and via core registers else in case of variadic function. This patch improve gfunc_sret() to take into account whether the function is variadic or not and make use of gfunc_sret() return value to determine whether to pass a structure via stack in gfunc_prolog(). It also take advantage of knowing if a function is variadic or not move float result value from VFP register to core register in gfunc_epilog().
Unified · Split
| @@ -839,12 +839,12 @@ int floats_in_core_regs(SValue *sval) · 838 unmodified lines | ||
| 839 | 839 | |
| 840 | 840 | /* Return the number of registers needed to return the struct, or 0 if |
| 841 | 841 | returning via struct pointer. */ |
| 842 | −ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) { | |
| 842 | +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) { | |
| 843 | 843 | #ifdef TCC_ARM_EABI |
| 844 | 844 | int size, align; |
| 845 | 845 | size = type_size(vt, &align); |
| 846 | 846 | #ifdef TCC_ARM_HARDFLOAT |
| 847 | − if (is_float(vt->t) || is_hgen_float_aggr(vt)) { | |
| 847 | + if (!variadic && (is_float(vt->t) || is_hgen_float_aggr(vt))) { | |
| 848 | 848 | *ret_align = 8; |
| 849 | 849 | ret->ref = NULL; |
| 850 | 850 | ret->t = VT_DOUBLE; |
| @@ -1221,21 +1221,19 @@ void gfunc_call(int nb_args) · 370 unmodified lines | ||
| 1221 | 1221 | void gfunc_prolog(CType *func_type) |
| 1222 | 1222 | { |
| 1223 | 1223 | Sym *sym,*sym2; |
| 1224 | − int n,nf,size,align, variadic, struct_ret = 0; | |
| 1224 | + int n, nf, size, align, struct_ret = 0; | |
| 1225 | 1225 | #ifdef TCC_ARM_HARDFLOAT |
| 1226 | 1226 | struct avail_regs avregs = AVAIL_REGS_INITIALIZER; |
| 1227 | 1227 | #endif |
| 1228 | + CType ret_type; | |
| 1228 | 1229 | |
| 1229 | 1230 | sym = func_type->ref; |
| 1230 | 1231 | func_vt = sym->type; |
| 1232 | + func_var = (func_type->ref->c == FUNC_ELLIPSIS); | |
| 1231 | 1233 | |
| 1232 | 1234 | n = nf = 0; |
| 1233 | − variadic = (func_type->ref->c == FUNC_ELLIPSIS); | |
| 1234 | − if((func_vt.t & VT_BTYPE) == VT_STRUCT | |
| 1235 | −#ifdef TCC_ARM_HARDFLOAT | |
| 1236 | − && (variadic || !is_hgen_float_aggr(&func_vt)) | |
| 1237 | −#endif | |
| 1238 | − && type_size(&func_vt,&align) > 4) | |
| 1235 | + if ((func_vt.t & VT_BTYPE) == VT_STRUCT && | |
| 1236 | + !gfunc_sret(&func_vt, func_var, &ret_type, &align)) | |
| 1239 | 1237 | { |
| 1240 | 1238 | n++; |
| 1241 | 1239 | struct_ret = 1; |
| @@ -1244,7 +1242,7 @@ void gfunc_prolog(CType *func_type) · 2 unmodified lines | ||
| 1244 | 1242 | for(sym2=sym->next;sym2 && (n<4 || nf<16);sym2=sym2->next) { |
| 1245 | 1243 | size = type_size(&sym2->type, &align); |
| 1246 | 1244 | #ifdef TCC_ARM_HARDFLOAT |
| 1247 | − if (!variadic && (is_float(sym2->type.t) | |
| 1245 | + if (!func_var && (is_float(sym2->type.t) | |
| 1248 | 1246 | || is_hgen_float_aggr(&sym2->type))) { |
| 1249 | 1247 | int tmpnf = assign_vfpreg(&avregs, align, size); |
| 1250 | 1248 | tmpnf += (size + 3) / 4; |
| @@ -1255,9 +1253,9 @@ void gfunc_prolog(CType *func_type) · 4 unmodified lines | ||
| 1255 | 1253 | n += (size + 3) / 4; |
| 1256 | 1254 | } |
| 1257 | 1255 | o(0xE1A0C00D); /* mov ip,sp */ |
| 1258 | − if(variadic) | |
| 1256 | + if (func_var) | |
| 1259 | 1257 | n=4; |
| 1260 | − if(n) { | |
| 1258 | + if (n) { | |
| 1261 | 1259 | if(n>4) |
| 1262 | 1260 | n=4; |
| 1263 | 1261 | #ifdef TCC_ARM_EABI |
| @@ -1289,7 +1287,7 @@ void gfunc_prolog(CType *func_type) · 25 unmodified lines | ||
| 1289 | 1287 | size = (size + 3) >> 2; |
| 1290 | 1288 | align = (align + 3) & ~3; |
| 1291 | 1289 | #ifdef TCC_ARM_HARDFLOAT |
| 1292 | − if (!variadic && (is_float(sym->type.t) | |
| 1290 | + if (!func_var && (is_float(sym->type.t) | |
| 1293 | 1291 | || is_hgen_float_aggr(&sym->type))) { |
| 1294 | 1292 | int fpn = assign_vfpreg(&avregs, align, size << 2); |
| 1295 | 1293 | if (fpn >= 0) { |
| @@ -1329,10 +1327,14 @@ void gfunc_epilog(void) · 33 unmodified lines | ||
| 1329 | 1327 | { |
| 1330 | 1328 | uint32_t x; |
| 1331 | 1329 | int diff; |
| 1330 | + /* Copy float return value to core register if base standard is used and | |
| 1331 | + float computation is made with VFP */ | |
| 1332 | 1332 | #ifdef TCC_ARM_EABI |
| 1333 | − /* Useless but harmless copy of the float result into main register(s) in case | |
| 1334 | − of variadic function in the hardfloat variant */ | |
| 1335 | − if(is_float(func_vt.t)) { | |
| 1333 | + if ( | |
| 1334 | +#ifdef TCC_ARM_HARDFLOAT | |
| 1335 | + func_var && | |
| 1336 | +#endif | |
| 1337 | + is_float(func_vt.t)) { | |
| 1336 | 1338 | if((func_vt.t & VT_BTYPE) == VT_FLOAT) |
| 1337 | 1339 | o(0xEE100A10); /* fmrs r0, s0 */ |
| 1338 | 1340 | else { |
| @@ -1881,7 +1881,7 @@ static void gcall_or_jmp(int is_jmp) · 1880 unmodified lines | ||
| 1881 | 1881 | |
| 1882 | 1882 | /* Return the number of registers needed to return the struct, or 0 if |
| 1883 | 1883 | returning via struct pointer. */ |
| 1884 | −ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) { | |
| 1884 | +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) { | |
| 1885 | 1885 | *ret_align = 1; // Never have to re-align return values for x86-64 |
| 1886 | 1886 | return 0; |
| 1887 | 1887 | } |
| @@ -1971,6 +1971,7 @@ void gfunc_prolog(CType * func_type) · 83 unmodified lines | ||
| 1971 | 1971 | /* if the function returns a structure, then add an |
| 1972 | 1972 | implicit pointer parameter */ |
| 1973 | 1973 | func_vt = sym->type; |
| 1974 | + func_var = (sym->c == FUNC_ELLIPSIS); | |
| 1974 | 1975 | if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { |
| 1975 | 1976 | func_vc = addr; |
| 1976 | 1977 | addr += 4; |
| @@ -376,7 +376,7 @@ static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX }; · 375 unmodified lines | ||
| 376 | 376 | |
| 377 | 377 | /* Return the number of registers needed to return the struct, or 0 if |
| 378 | 378 | returning via struct pointer. */ |
| 379 | −ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) | |
| 379 | +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) | |
| 380 | 380 | { |
| 381 | 381 | #ifdef TCC_TARGET_PE |
| 382 | 382 | int size, align; |
| @@ -527,6 +527,7 @@ ST_FUNC void gfunc_prolog(CType *func_type) · 144 unmodified lines | ||
| 527 | 527 | /* if the function returns a structure, then add an |
| 528 | 528 | implicit pointer parameter */ |
| 529 | 529 | func_vt = sym->type; |
| 530 | + func_var = (sym->c == FUNC_ELLIPSIS); | |
| 530 | 531 | #ifdef TCC_TARGET_PE |
| 531 | 532 | size = type_size(&func_vt,&align); |
| 532 | 533 | if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) { |
| @@ -441,6 +441,7 @@ void gfunc_prolog(int t) · 440 unmodified lines | ||
| 441 | 441 | /* if the function returns a structure, then add an |
| 442 | 442 | implicit pointer parameter */ |
| 443 | 443 | func_vt = sym->t; |
| 444 | + func_var = (sym->c == FUNC_ELLIPSIS); | |
| 444 | 445 | if ((func_vt & VT_BTYPE) == VT_STRUCT) { |
| 445 | 446 | func_vc = addr; |
| 446 | 447 | addr++; |
| @@ -1176,6 +1176,7 @@ ST_DATA int const_wanted; /* true if constant wanted */ · 1175 unmodified lines | ||
| 1176 | 1176 | ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */ |
| 1177 | 1177 | ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */ |
| 1178 | 1178 | ST_DATA CType func_vt; /* current function return type (used by return instruction) */ |
| 1179 | +ST_DATA int func_var; /* true if current function is variadic */ | |
| 1179 | 1180 | ST_DATA int func_vc; |
| 1180 | 1181 | ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */ |
| 1181 | 1182 | ST_DATA char *funcname; |
| @@ -1288,7 +1289,7 @@ ST_FUNC void gsym_addr(int t, int a); · 106 unmodified lines | ||
| 1288 | 1289 | ST_FUNC void gsym(int t); |
| 1289 | 1290 | ST_FUNC void load(int r, SValue *sv); |
| 1290 | 1291 | ST_FUNC void store(int r, SValue *v); |
| 1291 | −ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *align); | |
| 1292 | +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *align); | |
| 1292 | 1293 | ST_FUNC void gfunc_call(int nb_args); |
| 1293 | 1294 | ST_FUNC void gfunc_prolog(CType *func_type); |
| 1294 | 1295 | ST_FUNC void gfunc_epilog(void); |
| @@ -66,6 +66,7 @@ ST_DATA int const_wanted; /* true if constant wanted */ · 65 unmodified lines | ||
| 66 | 66 | ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */ |
| 67 | 67 | ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */ |
| 68 | 68 | ST_DATA CType func_vt; /* current function return type (used by return instruction) */ |
| 69 | +ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */ | |
| 69 | 70 | ST_DATA int func_vc; |
| 70 | 71 | ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */ |
| 71 | 72 | ST_DATA char *funcname; |
| @@ -3960,7 +3961,7 @@ ST_FUNC void unary(void) · 3888 unmodified lines | ||
| 3960 | 3961 | } else if (tok == '(') { |
| 3961 | 3962 | SValue ret; |
| 3962 | 3963 | Sym *sa; |
| 3963 | − int nb_args, ret_nregs, ret_align; | |
| 3964 | + int nb_args, ret_nregs, ret_align, variadic; | |
| 3964 | 3965 | |
| 3965 | 3966 | /* function call */ |
| 3966 | 3967 | if ((vtop->type.t & VT_BTYPE) != VT_FUNC) { |
| @@ -3984,7 +3985,9 @@ ST_FUNC void unary(void) · 17 unmodified lines | ||
| 3984 | 3985 | ret.r2 = VT_CONST; |
| 3985 | 3986 | /* compute first implicit argument if a structure is returned */ |
| 3986 | 3987 | if ((s->type.t & VT_BTYPE) == VT_STRUCT) { |
| 3987 | − ret_nregs = gfunc_sret(&s->type, &ret.type, &ret_align); | |
| 3988 | + variadic = (s->c == FUNC_ELLIPSIS); | |
| 3989 | + ret_nregs = gfunc_sret(&s->type, variadic, &ret.type, | |
| 3990 | + &ret_align); | |
| 3988 | 3991 | if (!ret_nregs) { |
| 3989 | 3992 | /* get some space for the returned structure */ |
| 3990 | 3993 | size = type_size(&s->type, &align); |
| @@ -4637,7 +4640,8 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 646 unmodified lines | ||
| 4637 | 4640 | if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { |
| 4638 | 4641 | CType type, ret_type; |
| 4639 | 4642 | int ret_align, ret_nregs; |
| 4640 | − ret_nregs = gfunc_sret(&func_vt, &ret_type, &ret_align); | |
| 4643 | + ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type, | |
| 4644 | + &ret_align); | |
| 4641 | 4645 | if (0 == ret_nregs) { |
| 4642 | 4646 | /* if returning structure, must copy it to implicit |
| 4643 | 4647 | first pointer arg location */ |
| @@ -5747,6 +5751,7 @@ static void gen_function(Sym *sym) · 1103 unmodified lines | ||
| 5747 | 5751 | cur_text_section = NULL; |
| 5748 | 5752 | funcname = ""; /* for safety */ |
| 5749 | 5753 | func_vt.t = VT_VOID; /* for safety */ |
| 5754 | + func_var = 0; /* for safety */ | |
| 5750 | 5755 | ind = 0; /* for safety */ |
| 5751 | 5756 | nocode_wanted = saved_nocode_wanted; |
| 5752 | 5757 | } |
| @@ -658,7 +658,7 @@ void gen_offs_sp(int b, int r, int d) · 657 unmodified lines | ||
| 658 | 658 | |
| 659 | 659 | /* Return the number of registers needed to return the struct, or 0 if |
| 660 | 660 | returning via struct pointer. */ |
| 661 | −ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) | |
| 661 | +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) | |
| 662 | 662 | { |
| 663 | 663 | int size, align; |
| 664 | 664 | *ret_align = 1; // Never have to re-align return values for x86-64 |
| @@ -833,6 +833,7 @@ void gfunc_prolog(CType *func_type) · 168 unmodified lines | ||
| 833 | 833 | /* if the function returns a structure, then add an |
| 834 | 834 | implicit pointer parameter */ |
| 835 | 835 | func_vt = sym->type; |
| 836 | + func_var = (sym->c == FUNC_ELLIPSIS); | |
| 836 | 837 | size = gfunc_arg_size(&func_vt); |
| 837 | 838 | if (size > 8) { |
| 838 | 839 | gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr); |