github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libffi/testsuite/libffi.special/unwindtest.cc (about)

     1  /* Area:	ffi_closure, unwind info
     2     Purpose:	Check if the unwind information is passed correctly.
     3     Limitations:	none.
     4     PR:		none.
     5     Originator:	Jeff Sturm <jsturm@one-point.com>  */
     6  
     7  /* { dg-do run } */
     8  #include "ffitestcxx.h"
     9  
    10  #if defined HAVE_STDINT_H
    11  #include <stdint.h>
    12  #endif
    13  
    14  #if defined HAVE_INTTYPES_H
    15  #include <inttypes.h>
    16  #endif
    17  
    18  void
    19  closure_test_fn(ffi_cif* cif __UNUSED__, void* resp __UNUSED__,
    20  		void** args __UNUSED__, void* userdata __UNUSED__)
    21  {
    22    throw 9;
    23  }
    24  
    25  typedef void (*closure_test_type)();
    26  
    27  void closure_test_fn1(ffi_cif* cif __UNUSED__, void* resp,
    28  		      void** args, void* userdata __UNUSED__)
    29   {
    30      *(ffi_arg*)resp =
    31        (int)*(float *)args[0] +(int)(*(float *)args[1]) +
    32        (int)(*(float *)args[2]) + (int)*(float *)args[3] +
    33        (int)(*(signed short *)args[4]) + (int)(*(float *)args[5]) +
    34        (int)*(float *)args[6] + (int)(*(int *)args[7]) +
    35        (int)(*(double*)args[8]) + (int)*(int *)args[9] +
    36        (int)(*(int *)args[10]) + (int)(*(float *)args[11]) +
    37        (int)*(int *)args[12] + (int)(*(int *)args[13]) +
    38        (int)(*(int *)args[14]) + *(int *)args[15] + (int)(intptr_t)userdata;
    39  
    40      printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d: %d\n",
    41  	   (int)*(float *)args[0], (int)(*(float *)args[1]),
    42  	   (int)(*(float *)args[2]), (int)*(float *)args[3],
    43  	   (int)(*(signed short *)args[4]), (int)(*(float *)args[5]),
    44  	   (int)*(float *)args[6], (int)(*(int *)args[7]),
    45  	   (int)(*(double *)args[8]), (int)*(int *)args[9],
    46  	   (int)(*(int *)args[10]), (int)(*(float *)args[11]),
    47  	   (int)*(int *)args[12], (int)(*(int *)args[13]),
    48  	   (int)(*(int *)args[14]), *(int *)args[15],
    49  	   (int)(intptr_t)userdata, (int)*(ffi_arg*)resp);
    50  
    51      throw (int)*(ffi_arg*)resp;
    52  }
    53  
    54  typedef int (*closure_test_type1)(float, float, float, float, signed short,
    55  				  float, float, int, double, int, int, float,
    56  				  int, int, int, int);
    57  
    58  int main (void)
    59  {
    60    ffi_cif cif;
    61    void *code;
    62    ffi_closure *pcl = (ffi_closure *)ffi_closure_alloc(sizeof(ffi_closure), &code);
    63    ffi_type * cl_arg_types[17];
    64  
    65    {
    66      cl_arg_types[1] = NULL;
    67  
    68      CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 0,
    69  		       &ffi_type_void, cl_arg_types) == FFI_OK);
    70      CHECK(ffi_prep_closure_loc(pcl, &cif, closure_test_fn, NULL, code) == FFI_OK);
    71  
    72      try
    73        {
    74  	(*((closure_test_type)(code)))();
    75        } catch (int exception_code)
    76        {
    77  	CHECK(exception_code == 9);
    78        }
    79  
    80      printf("part one OK\n");
    81      /* { dg-output "part one OK" } */
    82      }
    83  
    84      {
    85  
    86        cl_arg_types[0] = &ffi_type_float;
    87        cl_arg_types[1] = &ffi_type_float;
    88        cl_arg_types[2] = &ffi_type_float;
    89        cl_arg_types[3] = &ffi_type_float;
    90        cl_arg_types[4] = &ffi_type_sshort;
    91        cl_arg_types[5] = &ffi_type_float;
    92        cl_arg_types[6] = &ffi_type_float;
    93        cl_arg_types[7] = &ffi_type_uint;
    94        cl_arg_types[8] = &ffi_type_double;
    95        cl_arg_types[9] = &ffi_type_uint;
    96        cl_arg_types[10] = &ffi_type_uint;
    97        cl_arg_types[11] = &ffi_type_float;
    98        cl_arg_types[12] = &ffi_type_uint;
    99        cl_arg_types[13] = &ffi_type_uint;
   100        cl_arg_types[14] = &ffi_type_uint;
   101        cl_arg_types[15] = &ffi_type_uint;
   102        cl_arg_types[16] = NULL;
   103  
   104        /* Initialize the cif */
   105        CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 16,
   106  			 &ffi_type_sint, cl_arg_types) == FFI_OK);
   107  
   108        CHECK(ffi_prep_closure_loc(pcl, &cif, closure_test_fn1,
   109                                   (void *) 3 /* userdata */, code)  == FFI_OK);
   110        try
   111  	{
   112  	  (*((closure_test_type1)code))
   113  	    (1.1, 2.2, 3.3, 4.4, 127, 5.5, 6.6, 8, 9, 10, 11, 12.0, 13,
   114  	     19, 21, 1);
   115  	  /* { dg-output "\n1 2 3 4 127 5 6 8 9 10 11 12 13 19 21 1 3: 255" } */
   116  	} catch (int exception_code)
   117  	{
   118  	  CHECK(exception_code == 255);
   119  	}
   120        printf("part two OK\n");
   121        /* { dg-output "\npart two OK" } */
   122      }
   123      exit(0);
   124  }