| 1 |
/* Area: ffi_call, closure_call |
| 2 |
Purpose: Test long doubles passed in variable argument lists. |
| 3 |
Limitations: none. |
| 4 |
PR: none. |
| 5 |
Originator: Blake Chaffin 6/6/2007 */ |
| 6 |
|
| 7 |
/* { dg-do run { xfail strongarm*-*-* xscale*-*-* } } */ |
| 8 |
/* { dg-output "" { xfail avr32*-*-* } } */ |
| 9 |
/* { dg-output "" { xfail mips-sgi-irix6* } } PR libffi/46660 */ |
| 10 |
|
| 11 |
#include "ffitest.h" |
| 12 |
#include <stdarg.h> |
| 13 |
|
| 14 |
#define BUF_SIZE 50 |
| 15 |
static char buffer[BUF_SIZE]; |
| 16 |
|
| 17 |
static int |
| 18 |
wrap_printf(char* fmt, ...) { |
| 19 |
va_list ap; |
| 20 |
va_start(ap, fmt); |
| 21 |
long double ldArg = va_arg(ap, long double); |
| 22 |
va_end(ap); |
| 23 |
CHECK((int)ldArg == 7); |
| 24 |
return printf(fmt, ldArg); |
| 25 |
} |
| 26 |
|
| 27 |
static void |
| 28 |
cls_longdouble_va_fn(ffi_cif* cif __UNUSED__, void* resp, |
| 29 |
void** args, void* userdata __UNUSED__) |
| 30 |
{ |
| 31 |
char* format = *(char**)args[0]; |
| 32 |
long double ldValue = *(long double*)args[1]; |
| 33 |
|
| 34 |
*(ffi_arg*)resp = printf(format, ldValue); |
| 35 |
CHECK(*(ffi_arg*)resp == 4); |
| 36 |
snprintf(buffer, BUF_SIZE, format, ldValue); |
| 37 |
CHECK(strncmp(buffer, "7.0\n", BUF_SIZE) == 0); |
| 38 |
} |
| 39 |
|
| 40 |
int main (void) |
| 41 |
{ |
| 42 |
ffi_cif cif; |
| 43 |
void *code; |
| 44 |
ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code); |
| 45 |
void* args[3]; |
| 46 |
ffi_type* arg_types[3]; |
| 47 |
|
| 48 |
char* format = "%.1Lf\n"; |
| 49 |
long double ldArg = 7; |
| 50 |
ffi_arg res = 0; |
| 51 |
|
| 52 |
arg_types[0] = &ffi_type_pointer; |
| 53 |
arg_types[1] = &ffi_type_longdouble; |
| 54 |
arg_types[2] = NULL; |
| 55 |
|
| 56 |
/* This printf call is variadic */ |
| 57 |
CHECK(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 2, &ffi_type_sint, |
| 58 |
arg_types) == FFI_OK); |
| 59 |
|
| 60 |
args[0] = &format; |
| 61 |
args[1] = &ldArg; |
| 62 |
args[2] = NULL; |
| 63 |
|
| 64 |
ffi_call(&cif, FFI_FN(wrap_printf), &res, args); |
| 65 |
/* { dg-output "7.0" } */ |
| 66 |
printf("res: %d\n", (int) res); |
| 67 |
/* { dg-output "\nres: 4" } */ |
| 68 |
CHECK(res == 4); |
| 69 |
|
| 70 |
CHECK(ffi_prep_closure_loc(pcl, &cif, cls_longdouble_va_fn, NULL, |
| 71 |
code) == FFI_OK); |
| 72 |
|
| 73 |
res = ((int(*)(char*, ...))(code))(format, ldArg); |
| 74 |
/* { dg-output "\n7.0" } */ |
| 75 |
printf("res: %d\n", (int) res); |
| 76 |
/* { dg-output "\nres: 4" } */ |
| 77 |
CHECK(res == 4); |
| 78 |
|
| 79 |
exit(0); |
| 80 |
} |