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;
      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    return b1;
      22  }
      23  
      24  static void
      25  B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
      26       void* userdata __UNUSED__)
      27  {
      28    int b0;
      29    struct B b1;
      30  
      31    b0 = *(int*)(args[0]);
      32    b1 = *(struct B*)(args[1]);
      33  
      34    *(B*)resp = B_fn(b0, b1);
      35  }
      36  
      37  int main (void)
      38  {
      39    printf("123\n");
      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[2];
      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 } };
      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] = NULL;
      66  
      67    cls_struct_fields1[0] = &cls_struct_type;
      68    cls_struct_fields1[1] = NULL;
      69  
      70  
      71    dbl_arg_types[0] = &ffi_type_sint;
      72    dbl_arg_types[1] = &cls_struct_type1;
      73    dbl_arg_types[2] = NULL;
      74  
      75    res_dbl = B_fn(e_dbl, f_dbl);
      76    printf("0 res: %d\n", res_dbl.y.a);
      77    /* { dg-output "0 res: 43750" } */
      78  
      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  
      88    ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
      89    printf("1 res: %d\n", res_dbl.y.a);
      90    /* { dg-output "\n1 res: 43750" } */
      91    CHECK( res_dbl.y.a == (e_dbl + f_dbl.y.a));
      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\n", res_dbl.y.a);
      97    /* { dg-output "\n2 res: 43750" } */
      98    CHECK( res_dbl.y.a == (e_dbl + f_dbl.y.a));
      99  
     100    exit(0);
     101  }