1 /* Area: ffi_call, closure_call
2 Purpose: Single argument structs have a different ABI in emscripten.
3 Limitations: none.
4 PR: none.
5 Originator: <hood@mit.edu> */
6
7 /* { dg-do run } */
8 #include "ffitest.h"
9
10 typedef struct A {
11 int a, b;
12 } A;
13
14 typedef struct B {
15 struct A y;
16 } B;
17
18 static struct B B_fn(int b0, struct B b1)
19 {
20 b1.y.a += b0;
21 b1.y.b -= b0;
22 return b1;
23 }
24
25 static void
26 B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
27 void* userdata __UNUSED__)
28 {
29 int b0;
30 struct B b1;
31
32 b0 = *(int*)(args[0]);
33 b1 = *(struct B*)(args[1]);
34
35 *(B*)resp = B_fn(b0, b1);
36 }
37
38 int main (void)
39 {
40 ffi_cif cif;
41 void *code;
42 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
43 void* args_dbl[3];
44 ffi_type* cls_struct_fields[3];
45 ffi_type* cls_struct_fields1[2];
46 ffi_type cls_struct_type, cls_struct_type1;
47 ffi_type* dbl_arg_types[3];
48
49 int e_dbl = 12125;
50 struct B f_dbl = { { 31625, 16723 } };
51
52 struct B res_dbl;
53
54 cls_struct_type.size = 0;
55 cls_struct_type.alignment = 0;
56 cls_struct_type.type = FFI_TYPE_STRUCT;
57 cls_struct_type.elements = cls_struct_fields;
58
59 cls_struct_type1.size = 0;
60 cls_struct_type1.alignment = 0;
61 cls_struct_type1.type = FFI_TYPE_STRUCT;
62 cls_struct_type1.elements = cls_struct_fields1;
63
64 cls_struct_fields[0] = &ffi_type_sint;
65 cls_struct_fields[1] = &ffi_type_sint;
66 cls_struct_fields[2] = NULL;
67
68 cls_struct_fields1[0] = &cls_struct_type;
69 cls_struct_fields1[1] = NULL;
70
71
72 dbl_arg_types[0] = &ffi_type_sint;
73 dbl_arg_types[1] = &cls_struct_type1;
74 dbl_arg_types[2] = NULL;
75
76 res_dbl = B_fn(e_dbl, f_dbl);
77 printf("0 res: %d %d\n", res_dbl.y.a, res_dbl.y.b);
78 /* { dg-output "0 res: 43750 4598" } */
79
80 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type1,
81 dbl_arg_types) == FFI_OK);
82
83 args_dbl[0] = &e_dbl;
84 args_dbl[1] = &f_dbl;
85 args_dbl[2] = NULL;
86
87 ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
88 printf("1 res: %d %d\n", res_dbl.y.a, res_dbl.y.b);
89 /* { dg-output "\n1 res: 43750 4598" } */
90 CHECK( res_dbl.y.a == (f_dbl.y.a + e_dbl));
91 CHECK( res_dbl.y.b == (f_dbl.y.b - e_dbl));
92
93 CHECK(ffi_prep_closure_loc(pcl, &cif, B_gn, NULL, code) == FFI_OK);
94
95 res_dbl = ((B(*)(int, B))(code))(e_dbl, f_dbl);
96 printf("2 res: %d %d\n", res_dbl.y.a, res_dbl.y.b);
97 /* { dg-output "\n2 res: 43750 4598" } */
98 CHECK( res_dbl.y.a == (f_dbl.y.a + e_dbl));
99 CHECK( res_dbl.y.b == (f_dbl.y.b - e_dbl));
100
101 exit(0);
102 }