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