Debian Codegraph @

testsuite/libffi.closures/nested_struct12.c

raw
1 /* Area: ffi_call
2 Purpose: Check nested float struct.
3 Limitations: none.
4 PR: none.
5 Originator: Cheng Jin <[email protected]> */
6
7 /* { dg-do run } */
8 #include "ffitest.h"
9
10 typedef struct stru_FF stru_FF;
11 typedef struct stru_Nested_F stru_Nested_F;
12
13 struct stru_FF {
14 float elem1;
15 float elem2;
16 };
17
18 struct stru_Nested_F {
19 float elem1;
20 stru_FF elem2;
21 };
22
23 static float testNestedFloatStruct(float arg1, stru_Nested_F arg2)
24 {
25 float floatSum = arg1 + arg2.elem1 + arg2.elem2.elem1 + arg2.elem2.elem2;
26 return floatSum;
27 }
28
29 int main (void)
30 {
31 float ts12_result = 0;
32 int structElemNum = 2;
33 int nestedStructElemNum = 2;
34 int argNum = 2;
35
36 ffi_cif cif;
37 ffi_type **struct_float1 = (ffi_type **)malloc(sizeof(ffi_type *) * (structElemNum + 1));
38 ffi_type **struct_float2 = (ffi_type **)malloc(sizeof(ffi_type *) * (nestedStructElemNum + 1));
39 ffi_type **args = (ffi_type **)malloc(sizeof(ffi_type *) * (argNum + 1));
40 void **values = (void **)malloc(sizeof(void *) * (argNum + 1));
41 ffi_type struct_float_type1, struct_float_type2;
42 ffi_type *retType = &ffi_type_float;
43 float arg1;
44 float *arg2 = (float *)malloc(sizeof(stru_Nested_F));
45
46 struct_float2[0] = &ffi_type_float;
47 struct_float2[1] = &ffi_type_float;
48 struct_float2[2] = NULL;
49
50 struct_float_type2.size = 0;
51 struct_float_type2.alignment = 0;
52 struct_float_type2.type = FFI_TYPE_STRUCT;
53 struct_float_type2.elements = struct_float2;
54
55 struct_float1[0] = &ffi_type_float;
56 struct_float1[1] = &struct_float_type2;
57 struct_float1[2] = NULL;
58
59 struct_float_type1.size = 0;
60 struct_float_type1.alignment = 0;
61 struct_float_type1.type = FFI_TYPE_STRUCT;
62 struct_float_type1.elements = struct_float1;
63
64 args[0] = &ffi_type_float;
65 args[1] = &struct_float_type1;
66 args[2] = NULL;
67
68 arg1 = 37.88;
69 arg2[0] = 31.22;
70 arg2[1] = 33.44;
71 arg2[2] = 35.66;
72 values[0] = &arg1;
73 values[1] = arg2;
74 values[2] = NULL;
75
76 CHECK( ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, retType, args) == FFI_OK);
77 ffi_call(&cif, FFI_FN(testNestedFloatStruct), &ts12_result, values);
78 CHECK_FLOAT_EQ(ts12_result, 138.2f);
79
80 free(struct_float1);
81 free(struct_float2);
82 free(args);
83 free(values);
84
85 exit(0);
86 }