1/* -*-c-*- */ 2#include "ffitest.h" 3#include <complex.h> 4 5typedef struct cls_struct_align { 6 unsigned char a; 7 _Complex T_C_TYPE b; 8 unsigned char c; 9} cls_struct_align; 10 11cls_struct_align cls_struct_align_fn( 12 struct cls_struct_align a1, struct cls_struct_align a2) 13{ 14 struct cls_struct_align result; 15 16 result.a = a1.a + a2.a; 17 result.b = a1.b + a2.b; 18 result.c = a1.c + a2.c; 19 20 printf("%d %f,%fi %d %d %f,%fi %d: %d %f,%fi %d\n", 21 a1.a, T_CONV creal (a1.b), T_CONV cimag (a1.b), a1.c, 22 a2.a, T_CONV creal (a2.b), T_CONV cimag (a2.b), a2.c, 23 result.a, T_CONV creal (result.b), T_CONV cimag (result.b), result.c); 24 25 return result; 26} 27 28static void 29cls_struct_align_gn(ffi_cif* cif __UNUSED__, void* resp, void** args, 30 void* userdata __UNUSED__) 31{ 32 33 struct cls_struct_align a1, a2; 34 35 a1 = *(struct cls_struct_align*)(args[0]); 36 a2 = *(struct cls_struct_align*)(args[1]); 37 38 *(cls_struct_align*)resp = cls_struct_align_fn(a1, a2); 39} 40 41int main (void) 42{ 43 ffi_cif cif; 44 void *code; 45 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code); 46 void* args_c[5]; 47 ffi_type* cls_struct_fields[4]; 48 ffi_type cls_struct_type; 49 ffi_type* c_arg_types[5]; 50 51 struct cls_struct_align g_c = { 12, 4951 + 7 * I, 127 }; 52 struct cls_struct_align f_c = { 1, 9320 + 1 * I, 13 }; 53 struct cls_struct_align res_c; 54 55 cls_struct_type.size = 0; 56 cls_struct_type.alignment = 0; 57 cls_struct_type.type = FFI_TYPE_STRUCT; 58 cls_struct_type.elements = cls_struct_fields; 59 60 cls_struct_fields[0] = &ffi_type_uchar; 61 cls_struct_fields[1] = &T_FFI_TYPE; 62 cls_struct_fields[2] = &ffi_type_uchar; 63 cls_struct_fields[3] = NULL; 64 65 c_arg_types[0] = &cls_struct_type; 66 c_arg_types[1] = &cls_struct_type; 67 c_arg_types[2] = NULL; 68 69 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type, 70 c_arg_types) == FFI_OK); 71 72 args_c[0] = &g_c; 73 args_c[1] = &f_c; 74 args_c[2] = NULL; 75 76 ffi_call(&cif, FFI_FN(cls_struct_align_fn), &res_c, args_c); 77 /* { dg-output "12 4951,7i 127 1 9320,1i 13: 13 14271,8i 140" } */ 78 printf("res: %d %f,%fi %d\n", 79 res_c.a, T_CONV creal (res_c.b), T_CONV cimag (res_c.b), res_c.c); 80 /* { dg-output "\nres: 13 14271,8i 140" } */ 81 82 CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_align_gn, NULL, code) == FFI_OK); 83 84 res_c = ((cls_struct_align(*)(cls_struct_align, cls_struct_align))(code))(g_c, f_c); 85 /* { dg-output "\n12 4951,7i 127 1 9320,1i 13: 13 14271,8i 140" } */ 86 printf("res: %d %f,%fi %d\n", 87 res_c.a, T_CONV creal (res_c.b), T_CONV cimag (res_c.b), res_c.c); 88 /* { dg-output "\nres: 13 14271,8i 140" } */ 89 90 exit(0); 91} 92