| 1 |
/* Area: ffi_call |
| 2 |
Purpose: Test function with multiple fixed args and variable argument list. |
| 3 |
Limitations: none. |
| 4 |
PR: none. |
| 5 |
Originator: ARM Ltd., Oracle */ |
| 6 |
|
| 7 |
/* { dg-do run } */ |
| 8 |
/* { dg-output "" { xfail avr32*-*-* m68k-*-* } } */ |
| 9 |
|
| 10 |
#include "ffitest.h" |
| 11 |
#include <stdarg.h> |
| 12 |
|
| 13 |
/* |
| 14 |
* This is a modified version of va_2.c that has fixed arguments with "small" types that |
| 15 |
* are not allowed as variable arguments, but they should be still allowed as fixed args. |
| 16 |
*/ |
| 17 |
|
| 18 |
static int |
| 19 |
test_fn (char a1, float a2, int n, ...) |
| 20 |
{ |
| 21 |
va_list ap; |
| 22 |
unsigned char uc; |
| 23 |
signed char sc; |
| 24 |
unsigned short us; |
| 25 |
signed short ss; |
| 26 |
unsigned int ui; |
| 27 |
signed int si; |
| 28 |
unsigned long ul; |
| 29 |
signed long sl; |
| 30 |
float f; |
| 31 |
double d; |
| 32 |
|
| 33 |
va_start (ap, n); |
| 34 |
|
| 35 |
uc = va_arg (ap, unsigned); |
| 36 |
sc = va_arg (ap, signed); |
| 37 |
|
| 38 |
us = va_arg (ap, unsigned); |
| 39 |
ss = va_arg (ap, signed); |
| 40 |
|
| 41 |
ui = va_arg (ap, unsigned int); |
| 42 |
si = va_arg (ap, signed int); |
| 43 |
|
| 44 |
ul = va_arg (ap, unsigned long); |
| 45 |
sl = va_arg (ap, signed long); |
| 46 |
|
| 47 |
f = va_arg (ap, double); /* C standard promotes float->double |
| 48 |
when anonymous */ |
| 49 |
d = va_arg (ap, double); |
| 50 |
|
| 51 |
printf ("%d %f uc=%u sc=%d %u %d %u %d %lu %ld %f %f\n", |
| 52 |
a1, a2, |
| 53 |
uc, sc, |
| 54 |
us, ss, |
| 55 |
ui, si, |
| 56 |
ul, sl, |
| 57 |
f, d); |
| 58 |
|
| 59 |
va_end (ap); |
| 60 |
|
| 61 |
CHECK(a1 == 1); |
| 62 |
CHECK((int)a2 == 2); |
| 63 |
CHECK(uc == 9); |
| 64 |
CHECK(sc == 10); |
| 65 |
CHECK(us == 11); |
| 66 |
CHECK(ss == 12); |
| 67 |
CHECK(ui == 13); |
| 68 |
CHECK(si == 14); |
| 69 |
CHECK(ul == 15); |
| 70 |
CHECK(sl == 16); |
| 71 |
CHECK((int)f == 2); |
| 72 |
CHECK((int)d == 3); |
| 73 |
|
| 74 |
return n + 1; |
| 75 |
} |
| 76 |
|
| 77 |
int |
| 78 |
main (void) |
| 79 |
{ |
| 80 |
ffi_cif cif; |
| 81 |
void* args[14]; |
| 82 |
ffi_type* arg_types[14]; |
| 83 |
|
| 84 |
char a1; |
| 85 |
float a2; |
| 86 |
int n; |
| 87 |
ffi_arg res; |
| 88 |
|
| 89 |
unsigned int uc; |
| 90 |
signed int sc; |
| 91 |
unsigned int us; |
| 92 |
signed int ss; |
| 93 |
unsigned int ui; |
| 94 |
signed int si; |
| 95 |
unsigned long ul; |
| 96 |
signed long sl; |
| 97 |
double d1; |
| 98 |
double f1; |
| 99 |
|
| 100 |
arg_types[0] = &ffi_type_schar; |
| 101 |
arg_types[1] = &ffi_type_float; |
| 102 |
arg_types[2] = &ffi_type_sint; |
| 103 |
arg_types[3] = &ffi_type_uint; |
| 104 |
arg_types[4] = &ffi_type_sint; |
| 105 |
arg_types[5] = &ffi_type_uint; |
| 106 |
arg_types[6] = &ffi_type_sint; |
| 107 |
arg_types[7] = &ffi_type_uint; |
| 108 |
arg_types[8] = &ffi_type_sint; |
| 109 |
arg_types[9] = &ffi_type_ulong; |
| 110 |
arg_types[10] = &ffi_type_slong; |
| 111 |
arg_types[11] = &ffi_type_double; |
| 112 |
arg_types[12] = &ffi_type_double; |
| 113 |
arg_types[13] = NULL; |
| 114 |
|
| 115 |
CHECK(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 3, 13, &ffi_type_sint, arg_types) == FFI_OK); |
| 116 |
|
| 117 |
a1 = 1; |
| 118 |
a2 = 2.0f; |
| 119 |
n = 41; |
| 120 |
|
| 121 |
uc = 9; |
| 122 |
sc = 10; |
| 123 |
us = 11; |
| 124 |
ss = 12; |
| 125 |
ui = 13; |
| 126 |
si = 14; |
| 127 |
ul = 15; |
| 128 |
sl = 16; |
| 129 |
f1 = 2.12; |
| 130 |
d1 = 3.13; |
| 131 |
|
| 132 |
args[0] = &a1; |
| 133 |
args[1] = &a2; |
| 134 |
args[2] = &n; |
| 135 |
args[3] = &uc; |
| 136 |
args[4] = ≻ |
| 137 |
args[5] = &us; |
| 138 |
args[6] = &ss; |
| 139 |
args[7] = &ui; |
| 140 |
args[8] = &si; |
| 141 |
args[9] = &ul; |
| 142 |
args[10] = &sl; |
| 143 |
args[11] = &f1; |
| 144 |
args[12] = &d1; |
| 145 |
args[13] = NULL; |
| 146 |
|
| 147 |
ffi_call(&cif, FFI_FN(test_fn), &res, args); |
| 148 |
/* { dg-output "1 2.000000 uc=9 sc=10 11 12 13 14 15 16 2.120000 3.130000" } */ |
| 149 |
printf("res: %d\n", (int) res); |
| 150 |
/* { dg-output "\nres: 42" } */ |
| 151 |
CHECK(res == 42); |
| 152 |
|
| 153 |
return 0; |
| 154 |
} |