| 1 |
/* Area: ffi_call, closure_call |
| 2 |
Purpose: Test doubles passed in variable argument lists. |
| 3 |
Limitations: none. |
| 4 |
PR: none. |
| 5 |
Originator: Blake Chaffin 6/6/2007 */ |
| 6 |
|
| 7 |
/* { dg-do run } */ |
| 8 |
/* { dg-output "" { xfail avr32*-*-* } } */ |
| 9 |
#include "ffitest.h" |
| 10 |
|
| 11 |
struct small_tag |
| 12 |
{ |
| 13 |
unsigned char a; |
| 14 |
unsigned char b; |
| 15 |
}; |
| 16 |
|
| 17 |
struct large_tag |
| 18 |
{ |
| 19 |
unsigned a; |
| 20 |
unsigned b; |
| 21 |
unsigned c; |
| 22 |
unsigned d; |
| 23 |
unsigned e; |
| 24 |
}; |
| 25 |
|
| 26 |
static void |
| 27 |
test_fn (ffi_cif* cif __UNUSED__, void* resp, |
| 28 |
void** args, void* userdata __UNUSED__) |
| 29 |
{ |
| 30 |
int n = *(int*)args[0]; |
| 31 |
struct small_tag s1 = * (struct small_tag *) args[1]; |
| 32 |
struct large_tag l1 = * (struct large_tag *) args[2]; |
| 33 |
struct small_tag s2 = * (struct small_tag *) args[3]; |
| 34 |
|
| 35 |
printf ("%d %d %d %d %d %d %d %d %d %d\n", n, s1.a, s1.b, |
| 36 |
l1.a, l1.b, l1.c, l1.d, l1.e, |
| 37 |
s2.a, s2.b); |
| 38 |
CHECK(n == 4); |
| 39 |
CHECK(s1.a == 5); |
| 40 |
CHECK(s1.b == 6); |
| 41 |
CHECK(l1.a == 10); |
| 42 |
CHECK(l1.b == 11); |
| 43 |
CHECK(l1.c == 12); |
| 44 |
CHECK(l1.d == 13); |
| 45 |
CHECK(l1.e == 14); |
| 46 |
CHECK(s2.a == 20); |
| 47 |
CHECK(s2.b == 21); |
| 48 |
* (ffi_arg*) resp = 42; |
| 49 |
} |
| 50 |
|
| 51 |
int |
| 52 |
main (void) |
| 53 |
{ |
| 54 |
ffi_cif cif; |
| 55 |
void *code; |
| 56 |
ffi_closure *pcl = ffi_closure_alloc (sizeof (ffi_closure), &code); |
| 57 |
ffi_type* arg_types[5]; |
| 58 |
|
| 59 |
ffi_arg res = 0; |
| 60 |
|
| 61 |
ffi_type s_type; |
| 62 |
ffi_type *s_type_elements[3]; |
| 63 |
|
| 64 |
ffi_type l_type; |
| 65 |
ffi_type *l_type_elements[6]; |
| 66 |
|
| 67 |
struct small_tag s1; |
| 68 |
struct small_tag s2; |
| 69 |
struct large_tag l1; |
| 70 |
|
| 71 |
int si; |
| 72 |
|
| 73 |
s_type.size = 0; |
| 74 |
s_type.alignment = 0; |
| 75 |
s_type.type = FFI_TYPE_STRUCT; |
| 76 |
s_type.elements = s_type_elements; |
| 77 |
|
| 78 |
s_type_elements[0] = &ffi_type_uchar; |
| 79 |
s_type_elements[1] = &ffi_type_uchar; |
| 80 |
s_type_elements[2] = NULL; |
| 81 |
|
| 82 |
l_type.size = 0; |
| 83 |
l_type.alignment = 0; |
| 84 |
l_type.type = FFI_TYPE_STRUCT; |
| 85 |
l_type.elements = l_type_elements; |
| 86 |
|
| 87 |
l_type_elements[0] = &ffi_type_uint; |
| 88 |
l_type_elements[1] = &ffi_type_uint; |
| 89 |
l_type_elements[2] = &ffi_type_uint; |
| 90 |
l_type_elements[3] = &ffi_type_uint; |
| 91 |
l_type_elements[4] = &ffi_type_uint; |
| 92 |
l_type_elements[5] = NULL; |
| 93 |
|
| 94 |
arg_types[0] = &ffi_type_sint; |
| 95 |
arg_types[1] = &s_type; |
| 96 |
arg_types[2] = &l_type; |
| 97 |
arg_types[3] = &s_type; |
| 98 |
arg_types[4] = NULL; |
| 99 |
|
| 100 |
CHECK(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 4, &ffi_type_sint, |
| 101 |
arg_types) == FFI_OK); |
| 102 |
|
| 103 |
si = 4; |
| 104 |
s1.a = 5; |
| 105 |
s1.b = 6; |
| 106 |
|
| 107 |
s2.a = 20; |
| 108 |
s2.b = 21; |
| 109 |
|
| 110 |
l1.a = 10; |
| 111 |
l1.b = 11; |
| 112 |
l1.c = 12; |
| 113 |
l1.d = 13; |
| 114 |
l1.e = 14; |
| 115 |
|
| 116 |
CHECK(ffi_prep_closure_loc(pcl, &cif, test_fn, NULL, code) == FFI_OK); |
| 117 |
|
| 118 |
res = ((int (*)(int, ...))(code))(si, s1, l1, s2); |
| 119 |
/* { dg-output "4 5 6 10 11 12 13 14 20 21" } */ |
| 120 |
printf("res: %d\n", (int) res); |
| 121 |
/* { dg-output "\nres: 42" } */ |
| 122 |
CHECK(res == 42); |
| 123 |
|
| 124 |
exit(0); |
| 125 |
} |