History

Fix struct ret in variadic fct with ARM hardfloat

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().

Thomas Preud'homme committed 12 years ago

Commit 8efaa711 parent bcc19047 files · +35 −23Browse files

Unified · Split

7 files
  1. arm-gen.c +18 −16
  2. c67-gen.c +2 −1
  3. i386-gen.c +2 −1
  4. il-gen.c +1 −0
  5. tcc.h +2 −1
  6. tccgen.c +8 −3
  7. x86_64-gen.c +2 −1
arm-gen.c+18−16
@@ -839,12 +839,12 @@ int floats_in_core_regs(SValue *sval) · 838 unmodified lines
839839
840840 /* Return the number of registers needed to return the struct, or 0 if
841841 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) {
843843 #ifdef TCC_ARM_EABI
844844 int size, align;
845845 size = type_size(vt, &align);
846846 #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))) {
848848 *ret_align = 8;
849849 ret->ref = NULL;
850850 ret->t = VT_DOUBLE;
@@ -1221,21 +1221,19 @@ void gfunc_call(int nb_args) · 370 unmodified lines
12211221 void gfunc_prolog(CType *func_type)
12221222 {
12231223 Sym *sym,*sym2;
1224− int n,nf,size,align, variadic, struct_ret = 0;
1224+ int n, nf, size, align, struct_ret = 0;
12251225 #ifdef TCC_ARM_HARDFLOAT
12261226 struct avail_regs avregs = AVAIL_REGS_INITIALIZER;
12271227 #endif
1228+ CType ret_type;
12281229
12291230 sym = func_type->ref;
12301231 func_vt = sym->type;
1232+ func_var = (func_type->ref->c == FUNC_ELLIPSIS);
12311233
12321234 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))
12391237 {
12401238 n++;
12411239 struct_ret = 1;
@@ -1244,7 +1242,7 @@ void gfunc_prolog(CType *func_type) · 2 unmodified lines
12441242 for(sym2=sym->next;sym2 && (n<4 || nf<16);sym2=sym2->next) {
12451243 size = type_size(&sym2->type, &align);
12461244 #ifdef TCC_ARM_HARDFLOAT
1247− if (!variadic && (is_float(sym2->type.t)
1245+ if (!func_var && (is_float(sym2->type.t)
12481246 || is_hgen_float_aggr(&sym2->type))) {
12491247 int tmpnf = assign_vfpreg(&avregs, align, size);
12501248 tmpnf += (size + 3) / 4;
@@ -1255,9 +1253,9 @@ void gfunc_prolog(CType *func_type) · 4 unmodified lines
12551253 n += (size + 3) / 4;
12561254 }
12571255 o(0xE1A0C00D); /* mov ip,sp */
1258− if(variadic)
1256+ if (func_var)
12591257 n=4;
1260− if(n) {
1258+ if (n) {
12611259 if(n>4)
12621260 n=4;
12631261 #ifdef TCC_ARM_EABI
@@ -1289,7 +1287,7 @@ void gfunc_prolog(CType *func_type) · 25 unmodified lines
12891287 size = (size + 3) >> 2;
12901288 align = (align + 3) & ~3;
12911289 #ifdef TCC_ARM_HARDFLOAT
1292− if (!variadic && (is_float(sym->type.t)
1290+ if (!func_var && (is_float(sym->type.t)
12931291 || is_hgen_float_aggr(&sym->type))) {
12941292 int fpn = assign_vfpreg(&avregs, align, size << 2);
12951293 if (fpn >= 0) {
@@ -1329,10 +1327,14 @@ void gfunc_epilog(void) · 33 unmodified lines
13291327 {
13301328 uint32_t x;
13311329 int diff;
1330+ /* Copy float return value to core register if base standard is used and
1331+ float computation is made with VFP */
13321332 #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)) {
13361338 if((func_vt.t & VT_BTYPE) == VT_FLOAT)
13371339 o(0xEE100A10); /* fmrs r0, s0 */
13381340 else {
c67-gen.c+2−1
@@ -1881,7 +1881,7 @@ static void gcall_or_jmp(int is_jmp) · 1880 unmodified lines
18811881
18821882 /* Return the number of registers needed to return the struct, or 0 if
18831883 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) {
18851885 *ret_align = 1; // Never have to re-align return values for x86-64
18861886 return 0;
18871887 }
@@ -1971,6 +1971,7 @@ void gfunc_prolog(CType * func_type) · 83 unmodified lines
19711971 /* if the function returns a structure, then add an
19721972 implicit pointer parameter */
19731973 func_vt = sym->type;
1974+ func_var = (sym->c == FUNC_ELLIPSIS);
19741975 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
19751976 func_vc = addr;
19761977 addr += 4;
i386-gen.c+2−1
@@ -376,7 +376,7 @@ static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX }; · 375 unmodified lines
376376
377377 /* Return the number of registers needed to return the struct, or 0 if
378378 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)
380380 {
381381 #ifdef TCC_TARGET_PE
382382 int size, align;
@@ -527,6 +527,7 @@ ST_FUNC void gfunc_prolog(CType *func_type) · 144 unmodified lines
527527 /* if the function returns a structure, then add an
528528 implicit pointer parameter */
529529 func_vt = sym->type;
530+ func_var = (sym->c == FUNC_ELLIPSIS);
530531 #ifdef TCC_TARGET_PE
531532 size = type_size(&func_vt,&align);
532533 if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) {
il-gen.c+1−0
@@ -441,6 +441,7 @@ void gfunc_prolog(int t) · 440 unmodified lines
441441 /* if the function returns a structure, then add an
442442 implicit pointer parameter */
443443 func_vt = sym->t;
444+ func_var = (sym->c == FUNC_ELLIPSIS);
444445 if ((func_vt & VT_BTYPE) == VT_STRUCT) {
445446 func_vc = addr;
446447 addr++;
tcc.h+2−1
@@ -1176,6 +1176,7 @@ ST_DATA int const_wanted; /* true if constant wanted */ · 1175 unmodified lines
11761176 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
11771177 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
11781178 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 */
11791180 ST_DATA int func_vc;
11801181 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
11811182 ST_DATA char *funcname;
@@ -1288,7 +1289,7 @@ ST_FUNC void gsym_addr(int t, int a); · 106 unmodified lines
12881289 ST_FUNC void gsym(int t);
12891290 ST_FUNC void load(int r, SValue *sv);
12901291 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);
12921293 ST_FUNC void gfunc_call(int nb_args);
12931294 ST_FUNC void gfunc_prolog(CType *func_type);
12941295 ST_FUNC void gfunc_epilog(void);
tccgen.c+8−3
@@ -66,6 +66,7 @@ ST_DATA int const_wanted; /* true if constant wanted */ · 65 unmodified lines
6666 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
6767 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
6868 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) */
6970 ST_DATA int func_vc;
7071 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
7172 ST_DATA char *funcname;
@@ -3960,7 +3961,7 @@ ST_FUNC void unary(void) · 3888 unmodified lines
39603961 } else if (tok == '(') {
39613962 SValue ret;
39623963 Sym *sa;
3963− int nb_args, ret_nregs, ret_align;
3964+ int nb_args, ret_nregs, ret_align, variadic;
39643965
39653966 /* function call */
39663967 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
@@ -3984,7 +3985,9 @@ ST_FUNC void unary(void) · 17 unmodified lines
39843985 ret.r2 = VT_CONST;
39853986 /* compute first implicit argument if a structure is returned */
39863987 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);
39883991 if (!ret_nregs) {
39893992 /* get some space for the returned structure */
39903993 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
46374640 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
46384641 CType type, ret_type;
46394642 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);
46414645 if (0 == ret_nregs) {
46424646 /* if returning structure, must copy it to implicit
46434647 first pointer arg location */
@@ -5747,6 +5751,7 @@ static void gen_function(Sym *sym) · 1103 unmodified lines
57475751 cur_text_section = NULL;
57485752 funcname = ""; /* for safety */
57495753 func_vt.t = VT_VOID; /* for safety */
5754+ func_var = 0; /* for safety */
57505755 ind = 0; /* for safety */
57515756 nocode_wanted = saved_nocode_wanted;
57525757 }
x86_64-gen.c+2−1
@@ -658,7 +658,7 @@ void gen_offs_sp(int b, int r, int d) · 657 unmodified lines
658658
659659 /* Return the number of registers needed to return the struct, or 0 if
660660 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)
662662 {
663663 int size, align;
664664 *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
833833 /* if the function returns a structure, then add an
834834 implicit pointer parameter */
835835 func_vt = sym->type;
836+ func_var = (sym->c == FUNC_ELLIPSIS);
836837 size = gfunc_arg_size(&func_vt);
837838 if (size > 8) {
838839 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t