1  /* Area:       ffi_call, closure_call
       2     Purpose:    Check structure passing.
       3     Limitations:        none.
       4     PR:         none.
       5     Originator: <jincheng@ca.ibm.com> and <jakub@redhat.com> 20210609    */
       6  
       7  /* { dg-do run } */
       8  #include "ffitest.h"
       9  
      10  typedef struct A {
      11    float a, b;
      12  } A;
      13  
      14  typedef struct B {
      15    float x;
      16    struct A y;
      17  } B;
      18  
      19  B B_fn(float b0, struct B b1)
      20  {
      21    struct B result;
      22  
      23    result.x = b0 + b1.x;
      24    result.y.a = b0 + b1.y.a;
      25    result.y.b = b0 + b1.y.b;
      26  
      27    printf("%g %g %g %g: %g %g %g\n", b0, b1.x, b1.y.a, b1.y.b,
      28          result.x, result.y.a, result.y.b);
      29  
      30    return result;
      31  }
      32  
      33  static void
      34  B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
      35       void* userdata __UNUSED__)
      36  {
      37    float b0;
      38    struct B b1;
      39  
      40    b0 = *(float*)(args[0]);
      41    b1 = *(struct B*)(args[1]);
      42  
      43    *(B*)resp = B_fn(b0, b1);
      44  }
      45  
      46  int main (void)
      47  {
      48    ffi_cif cif;
      49    void *code;
      50    ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
      51    void* args_dbl[3];
      52    ffi_type* cls_struct_fields[3];
      53    ffi_type* cls_struct_fields1[3];
      54    ffi_type cls_struct_type, cls_struct_type1;
      55    ffi_type* dbl_arg_types[3];
      56  
      57    float e_dbl = 12.125f;
      58    struct B f_dbl = { 24.75f, { 31.625f, 32.25f } };
      59  
      60    struct B res_dbl;
      61  
      62    cls_struct_type.size = 0;
      63    cls_struct_type.alignment = 0;
      64    cls_struct_type.type = FFI_TYPE_STRUCT;
      65    cls_struct_type.elements = cls_struct_fields;
      66  
      67    cls_struct_type1.size = 0;
      68    cls_struct_type1.alignment = 0;
      69    cls_struct_type1.type = FFI_TYPE_STRUCT;
      70    cls_struct_type1.elements = cls_struct_fields1;
      71  
      72    cls_struct_fields[0] = &ffi_type_float;
      73    cls_struct_fields[1] = &ffi_type_float;
      74    cls_struct_fields[2] = NULL;
      75  
      76    cls_struct_fields1[0] = &ffi_type_float;
      77    cls_struct_fields1[1] = &cls_struct_type;
      78    cls_struct_fields1[2] = NULL;
      79  
      80  
      81    dbl_arg_types[0] = &ffi_type_float;
      82    dbl_arg_types[1] = &cls_struct_type1;
      83    dbl_arg_types[2] = NULL;
      84  
      85    CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type1,
      86                      dbl_arg_types) == FFI_OK);
      87  
      88    args_dbl[0] = &e_dbl;
      89    args_dbl[1] = &f_dbl;
      90    args_dbl[2] = NULL;
      91  
      92    ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
      93    /* { dg-output "12.125 24.75 31.625 32.25: 36.875 43.75 44.375" } */
      94    CHECK( res_dbl.x == (e_dbl + f_dbl.x));
      95    CHECK( res_dbl.y.a == (e_dbl + f_dbl.y.a));
      96    CHECK( res_dbl.y.b == (e_dbl + f_dbl.y.b));
      97  
      98    CHECK(ffi_prep_closure_loc(pcl, &cif, B_gn, NULL, code) == FFI_OK);
      99  
     100    res_dbl = ((B(*)(float, B))(code))(e_dbl, f_dbl);
     101    /* { dg-output "\n12.125 24.75 31.625 32.25: 36.875 43.75 44.375" } */
     102    CHECK( res_dbl.x == (e_dbl + f_dbl.x));
     103    CHECK( res_dbl.y.a == (e_dbl + f_dbl.y.a));
     104    CHECK( res_dbl.y.b == (e_dbl + f_dbl.y.b));
     105  
     106    exit(0);
     107  }