Back to index

python3.2  3.2.2
cls_align_double.c
Go to the documentation of this file.
00001 /* Area:      ffi_call, closure_call
00002    Purpose:   Check structure alignment of double.
00003    Limitations:      none.
00004    PR:        none.
00005    Originator:       <hos@tamanegi.org> 20031203  */
00006 
00007 
00008 
00009 /* { dg-do run } */
00010 #include "ffitest.h"
00011 
00012 typedef struct cls_struct_align {
00013   unsigned char a;
00014   double b;
00015   unsigned char c;
00016 } cls_struct_align;
00017 
00018 cls_struct_align cls_struct_align_fn(struct cls_struct_align a1,
00019                          struct cls_struct_align a2)
00020 {
00021   struct cls_struct_align result;
00022 
00023   result.a = a1.a + a2.a;
00024   result.b = a1.b + a2.b;
00025   result.c = a1.c + a2.c;
00026 
00027   printf("%d %g %d %d %g %d: %d %g %d\n", a1.a, a1.b, a1.c, a2.a, a2.b, a2.c, result.a, result.b, result.c);
00028 
00029   return  result;
00030 }
00031 
00032 static void
00033 cls_struct_align_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
00034                   void* userdata __UNUSED__)
00035 {
00036 
00037   struct cls_struct_align a1, a2;
00038 
00039   a1 = *(struct cls_struct_align*)(args[0]);
00040   a2 = *(struct cls_struct_align*)(args[1]);
00041 
00042   *(cls_struct_align*)resp = cls_struct_align_fn(a1, a2);
00043 }
00044 
00045 int main (void)
00046 {
00047   ffi_cif cif;
00048   void *code;
00049   ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
00050   void* args_dbl[5];
00051   ffi_type* cls_struct_fields[4];
00052   ffi_type cls_struct_type;
00053   ffi_type* dbl_arg_types[5];
00054 
00055   cls_struct_type.size = 0;
00056   cls_struct_type.alignment = 0;
00057   cls_struct_type.type = FFI_TYPE_STRUCT;
00058   cls_struct_type.elements = cls_struct_fields;
00059 
00060   struct cls_struct_align g_dbl = { 12, 4951, 127 };
00061   struct cls_struct_align f_dbl = { 1, 9320, 13 };
00062   struct cls_struct_align res_dbl;
00063 
00064   cls_struct_fields[0] = &ffi_type_uchar;
00065   cls_struct_fields[1] = &ffi_type_double;
00066   cls_struct_fields[2] = &ffi_type_uchar;
00067   cls_struct_fields[3] = NULL;
00068 
00069   dbl_arg_types[0] = &cls_struct_type;
00070   dbl_arg_types[1] = &cls_struct_type;
00071   dbl_arg_types[2] = NULL;
00072 
00073   CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
00074                    dbl_arg_types) == FFI_OK);
00075 
00076   args_dbl[0] = &g_dbl;
00077   args_dbl[1] = &f_dbl;
00078   args_dbl[2] = NULL;
00079 
00080   ffi_call(&cif, FFI_FN(cls_struct_align_fn), &res_dbl, args_dbl);
00081   /* { dg-output "12 4951 127 1 9320 13: 13 14271 140" } */
00082   printf("res: %d %g %d\n", res_dbl.a, res_dbl.b, res_dbl.c);
00083   /* { dg-output "\nres: 13 14271 140" } */
00084 
00085   CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_align_gn, NULL, code) == FFI_OK);
00086 
00087   res_dbl = ((cls_struct_align(*)(cls_struct_align, cls_struct_align))(code))(g_dbl, f_dbl);
00088   /* { dg-output "\n12 4951 127 1 9320 13: 13 14271 140" } */
00089   printf("res: %d %g %d\n", res_dbl.a, res_dbl.b, res_dbl.c);
00090   /* { dg-output "\nres: 13 14271 140" } */
00091 
00092   exit(0);
00093 }