blob: dbf20002dfc9c584446c463d727b0262d1812fd8 [file] [log] [blame]
Matthias Klosea8349752010-03-15 13:25:28 +00001/* 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 { xfail strongarm*-*-* xscale*-*-* } } */
8/* { dg-output "" { xfail avr32*-*-* } } */
doko@ubuntu.com2a918762012-06-26 17:56:44 +02009/* { dg-output "" { xfail mips-sgi-irix6* } } PR libffi/46660 */
10
Matthias Klosea8349752010-03-15 13:25:28 +000011#include "ffitest.h"
12
13static void
14cls_double_va_fn(ffi_cif* cif __UNUSED__, void* resp,
15 void** args, void* userdata __UNUSED__)
16{
17 char* format = *(char**)args[0];
18 double doubleValue = *(double*)args[1];
19
20 *(ffi_arg*)resp = printf(format, doubleValue);
21}
22
23int main (void)
24{
25 ffi_cif cif;
26 void *code;
27 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
28 void* args[3];
29 ffi_type* arg_types[3];
30
31 char* format = "%.1f\n";
32 double doubleArg = 7;
33 ffi_arg res = 0;
34
35 arg_types[0] = &ffi_type_pointer;
36 arg_types[1] = &ffi_type_double;
37 arg_types[2] = NULL;
38
doko@ubuntu.com2a918762012-06-26 17:56:44 +020039 /* This printf call is variadic */
40 CHECK(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 2, &ffi_type_sint,
Matthias Klosea8349752010-03-15 13:25:28 +000041 arg_types) == FFI_OK);
42
43 args[0] = &format;
44 args[1] = &doubleArg;
45 args[2] = NULL;
46
47 ffi_call(&cif, FFI_FN(printf), &res, args);
48 // { dg-output "7.0" }
49 printf("res: %d\n", (int) res);
50 // { dg-output "\nres: 4" }
51
doko@ubuntu.com2a918762012-06-26 17:56:44 +020052 /* The call to cls_double_va_fn is static, so have to use a normal prep_cif */
53 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_sint, arg_types) == FFI_OK);
54
Matthias Klosea8349752010-03-15 13:25:28 +000055 CHECK(ffi_prep_closure_loc(pcl, &cif, cls_double_va_fn, NULL, code) == FFI_OK);
56
57 res = ((int(*)(char*, double))(code))(format, doubleArg);
58 // { dg-output "\n7.0" }
59 printf("res: %d\n", (int) res);
60 // { dg-output "\nres: 4" }
61
62 exit(0);
63}