~ubuntu-branches/ubuntu/lucid/gauche-c-wrapper/lucid

« back to all changes in this revision

Viewing changes to libffi/testsuite/libffi.call/nested_struct1.c

  • Committer: Bazaar Package Importer
  • Author(s): NIIBE Yutaka
  • Date: 2008-04-07 09:15:03 UTC
  • Revision ID: james.westby@ubuntu.com-20080407091503-wu0h414koe95kj4i
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Area:        ffi_call, closure_call
 
2
   Purpose:     Check structure passing with different structure size.
 
3
                Contains structs as parameter of the struct itself.
 
4
   Limitations: none.
 
5
   PR:          none.
 
6
   Originator:  <andreast@gcc.gnu.org> 20030828  */
 
7
 
 
8
/* { dg-do run { xfail mips64*-*-* arm*-*-* strongarm*-*-* xscale*-*-* } } */
 
9
#include "ffitest.h"
 
10
 
 
11
typedef struct cls_struct_16byte1 {
 
12
  double a;
 
13
  float b;
 
14
  int c;
 
15
} cls_struct_16byte1;
 
16
 
 
17
typedef struct cls_struct_16byte2 {
 
18
  int ii;
 
19
  double dd;
 
20
  float ff;
 
21
} cls_struct_16byte2;
 
22
 
 
23
typedef struct cls_struct_combined {
 
24
  cls_struct_16byte1 d;
 
25
  cls_struct_16byte2 e;
 
26
} cls_struct_combined;
 
27
 
 
28
cls_struct_combined cls_struct_combined_fn(struct cls_struct_16byte1 b0,
 
29
                                           struct cls_struct_16byte2 b1,
 
30
                                           struct cls_struct_combined b2,
 
31
                                           struct cls_struct_16byte1 b3)
 
32
{
 
33
  struct cls_struct_combined result;
 
34
 
 
35
  result.d.a = b0.a + b1.dd + b2.d.a;
 
36
  result.d.b = b0.b + b1.ff + b2.d.b;
 
37
  result.d.c = b0.c + b1.ii + b2.d.c;
 
38
  result.e.ii = b0.c + b1.ii + b2.e.ii;
 
39
  result.e.dd = b0.a + b1.dd + b2.e.dd;
 
40
  result.e.ff = b0.b + b1.ff + b2.e.ff;
 
41
 
 
42
  printf("%g %g %d %d %g %g %g %g %d %d %g %g %g %g %d: %g %g %d %d %g %g\n",
 
43
         b0.a, b0.b, b0.c,
 
44
         b1.ii, b1.dd, b1.ff,
 
45
         b2.d.a, b2.d.b, b2.d.c,
 
46
         b2.e.ii, b2.e.dd, b2.e.ff,
 
47
         b3.a, b3.b, b3.c,
 
48
         result.d.a, result.d.b, result.d.c,
 
49
         result.e.ii, result.e.dd, result.e.ff);
 
50
 
 
51
  return result;
 
52
}
 
53
 
 
54
static void
 
55
cls_struct_combined_gn(ffi_cif* cif, void* resp, void** args, void* userdata)
 
56
{
 
57
  struct cls_struct_16byte1 b0;
 
58
  struct cls_struct_16byte2 b1;
 
59
  struct cls_struct_combined b2;
 
60
  struct cls_struct_16byte1 b3;
 
61
 
 
62
  b0 = *(struct cls_struct_16byte1*)(args[0]);
 
63
  b1 = *(struct cls_struct_16byte2*)(args[1]);
 
64
  b2 = *(struct cls_struct_combined*)(args[2]);
 
65
  b3 = *(struct cls_struct_16byte1*)(args[3]);
 
66
 
 
67
 
 
68
  *(cls_struct_combined*)resp = cls_struct_combined_fn(b0, b1, b2, b3);
 
69
}
 
70
 
 
71
int main (void)
 
72
{
 
73
  ffi_cif cif;
 
74
#ifndef USING_MMAP
 
75
  static ffi_closure cl;
 
76
#endif
 
77
  ffi_closure *pcl;
 
78
  void* args_dbl[5];
 
79
  ffi_type* cls_struct_fields[5];
 
80
  ffi_type* cls_struct_fields1[5];
 
81
  ffi_type* cls_struct_fields2[5];
 
82
  ffi_type cls_struct_type, cls_struct_type1, cls_struct_type2;
 
83
  ffi_type* dbl_arg_types[5];
 
84
 
 
85
#ifdef USING_MMAP
 
86
  pcl = allocate_mmap (sizeof(ffi_closure));
 
87
#else
 
88
  pcl = &cl;
 
89
#endif
 
90
 
 
91
  cls_struct_type.size = 0;
 
92
  cls_struct_type.alignment = 0;
 
93
  cls_struct_type.type = FFI_TYPE_STRUCT;
 
94
  cls_struct_type.elements = cls_struct_fields;
 
95
 
 
96
  cls_struct_type1.size = 0;
 
97
  cls_struct_type1.alignment = 0;
 
98
  cls_struct_type1.type = FFI_TYPE_STRUCT;
 
99
  cls_struct_type1.elements = cls_struct_fields1;
 
100
 
 
101
  cls_struct_type2.size = 0;
 
102
  cls_struct_type2.alignment = 0;
 
103
  cls_struct_type2.type = FFI_TYPE_STRUCT;
 
104
  cls_struct_type2.elements = cls_struct_fields2;
 
105
 
 
106
  struct cls_struct_16byte1 e_dbl = { 9.0, 2.0, 6};
 
107
  struct cls_struct_16byte2 f_dbl = { 1, 2.0, 3.0};
 
108
  struct cls_struct_combined g_dbl = {{4.0, 5.0, 6},
 
109
                                      {3, 1.0, 8.0}};
 
110
  struct cls_struct_16byte1 h_dbl = { 3.0, 2.0, 4};
 
111
  struct cls_struct_combined res_dbl;
 
112
 
 
113
  cls_struct_fields[0] = &ffi_type_double;
 
114
  cls_struct_fields[1] = &ffi_type_float;
 
115
  cls_struct_fields[2] = &ffi_type_uint32;
 
116
  cls_struct_fields[3] = NULL;
 
117
 
 
118
  cls_struct_fields1[0] = &ffi_type_uint32;
 
119
  cls_struct_fields1[1] = &ffi_type_double;
 
120
  cls_struct_fields1[2] = &ffi_type_float;
 
121
  cls_struct_fields1[3] = NULL;
 
122
 
 
123
  cls_struct_fields2[0] = &cls_struct_type;
 
124
  cls_struct_fields2[1] = &cls_struct_type1;
 
125
  cls_struct_fields2[2] = NULL;
 
126
 
 
127
 
 
128
  dbl_arg_types[0] = &cls_struct_type;
 
129
  dbl_arg_types[1] = &cls_struct_type1;
 
130
  dbl_arg_types[2] = &cls_struct_type2;
 
131
  dbl_arg_types[3] = &cls_struct_type;
 
132
  dbl_arg_types[4] = NULL;
 
133
 
 
134
  CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 4, &cls_struct_type2,
 
135
                     dbl_arg_types) == FFI_OK);
 
136
 
 
137
  args_dbl[0] = &e_dbl;
 
138
  args_dbl[1] = &f_dbl;
 
139
  args_dbl[2] = &g_dbl;
 
140
  args_dbl[3] = &h_dbl;
 
141
  args_dbl[4] = NULL;
 
142
 
 
143
  ffi_call(&cif, FFI_FN(cls_struct_combined_fn), &res_dbl, args_dbl);
 
144
  /* { dg-output "9 2 6 1 2 3 4 5 6 3 1 8 3 2 4: 15 10 13 10 12 13" } */
 
145
  CHECK( res_dbl.d.a == (e_dbl.a + f_dbl.dd + g_dbl.d.a));
 
146
  CHECK( res_dbl.d.b == (e_dbl.b + f_dbl.ff + g_dbl.d.b));
 
147
  CHECK( res_dbl.d.c == (e_dbl.c + f_dbl.ii + g_dbl.d.c));
 
148
  CHECK( res_dbl.e.ii == (e_dbl.c + f_dbl.ii + g_dbl.e.ii));
 
149
  CHECK( res_dbl.e.dd == (e_dbl.a + f_dbl.dd + g_dbl.e.dd));
 
150
  CHECK( res_dbl.e.ff == (e_dbl.b + f_dbl.ff + g_dbl.e.ff));
 
151
 
 
152
  CHECK(ffi_prep_closure(pcl, &cif, cls_struct_combined_gn, NULL) == FFI_OK);
 
153
 
 
154
  res_dbl = ((cls_struct_combined(*)(cls_struct_16byte1,
 
155
                                     cls_struct_16byte2,
 
156
                                     cls_struct_combined,
 
157
                                     cls_struct_16byte1))
 
158
             (pcl))(e_dbl, f_dbl, g_dbl, h_dbl);
 
159
  /* { dg-output "\n9 2 6 1 2 3 4 5 6 3 1 8 3 2 4: 15 10 13 10 12 13" } */
 
160
  CHECK( res_dbl.d.a == (e_dbl.a + f_dbl.dd + g_dbl.d.a));
 
161
  CHECK( res_dbl.d.b == (e_dbl.b + f_dbl.ff + g_dbl.d.b));
 
162
  CHECK( res_dbl.d.c == (e_dbl.c + f_dbl.ii + g_dbl.d.c));
 
163
  CHECK( res_dbl.e.ii == (e_dbl.c + f_dbl.ii + g_dbl.e.ii));
 
164
  CHECK( res_dbl.e.dd == (e_dbl.a + f_dbl.dd + g_dbl.e.dd));
 
165
  CHECK( res_dbl.e.ff == (e_dbl.b + f_dbl.ff + g_dbl.e.ff));
 
166
  //  CHECK( 1 == 0);
 
167
  exit(0);
 
168
}