1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/* go-reflect-call.c -- call reflection support for Go.
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file. */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "runtime.h"
#include "go-assert.h"
#ifdef USE_LIBFFI
#include "ffi.h"
#endif
#if defined(USE_LIBFFI) && FFI_GO_CLOSURES
/* The functions in this file are only called from reflect_call. As
reflect_call calls a libffi function, which will be compiled
without -fsplit-stack, it will always run with a large stack. */
static size_t go_results_size (const struct functype *)
__attribute__ ((no_split_stack));
static void go_set_results (const struct functype *, unsigned char *, void **)
__attribute__ ((no_split_stack));
/* Get the total size required for the result parameters of a
function. */
static size_t
go_results_size (const struct functype *func)
{
int count;
const struct _type **types;
size_t off;
size_t maxalign;
int i;
count = func->out.__count;
if (count == 0)
return 0;
types = (const struct _type **) func->out.__values;
/* A single integer return value is always promoted to a full
word. */
if (count == 1)
{
switch (types[0]->kind & kindMask)
{
case kindBool:
case kindInt8:
case kindInt16:
case kindInt32:
case kindUint8:
case kindUint16:
case kindUint32:
case kindInt:
case kindUint:
return sizeof (ffi_arg);
default:
break;
}
}
off = 0;
maxalign = 0;
for (i = 0; i < count; ++i)
{
size_t align;
align = types[i]->fieldAlign;
if (align > maxalign)
maxalign = align;
off = (off + align - 1) & ~ (align - 1);
off += types[i]->size;
}
off = (off + maxalign - 1) & ~ (maxalign - 1);
// The libffi library doesn't understand a struct with no fields.
// We generate a struct with a single field of type void. When used
// as a return value, libffi will think that requires a byte.
if (off == 0)
off = 1;
return off;
}
/* Copy the results of calling a function via FFI from CALL_RESULT
into the addresses in RESULTS. */
static void
go_set_results (const struct functype *func, unsigned char *call_result,
void **results)
{
int count;
const struct _type **types;
size_t off;
int i;
count = func->out.__count;
if (count == 0)
return;
types = (const struct _type **) func->out.__values;
/* A single integer return value is always promoted to a full
word. */
if (count == 1)
{
switch (types[0]->kind & kindMask)
{
case kindBool:
case kindInt8:
case kindInt16:
case kindInt32:
case kindUint8:
case kindUint16:
case kindUint32:
case kindInt:
case kindUint:
{
union
{
unsigned char buf[sizeof (ffi_arg)];
ffi_arg v;
} u;
ffi_arg v;
__builtin_memcpy (&u.buf, call_result, sizeof (ffi_arg));
v = u.v;
switch (types[0]->size)
{
case 1:
{
uint8_t b;
b = (uint8_t) v;
__builtin_memcpy (results[0], &b, 1);
}
break;
case 2:
{
uint16_t s;
s = (uint16_t) v;
__builtin_memcpy (results[0], &s, 2);
}
break;
case 4:
{
uint32_t w;
w = (uint32_t) v;
__builtin_memcpy (results[0], &w, 4);
}
break;
case 8:
{
uint64_t d;
d = (uint64_t) v;
__builtin_memcpy (results[0], &d, 8);
}
break;
default:
abort ();
}
}
return;
default:
break;
}
}
off = 0;
for (i = 0; i < count; ++i)
{
size_t align;
size_t size;
align = types[i]->fieldAlign;
size = types[i]->size;
off = (off + align - 1) & ~ (align - 1);
__builtin_memcpy (results[i], call_result + off, size);
off += size;
}
}
/* The code that converts the Go type to an FFI type is written in Go,
so that it can allocate Go heap memory. */
extern void ffiFuncToCIF(const struct functype*, _Bool, _Bool, ffi_cif*)
__asm__ ("runtime.ffiFuncToCIF");
/* Call a function. The type of the function is FUNC_TYPE, and the
closure is FUNC_VAL. PARAMS is an array of parameter addresses.
RESULTS is an array of result addresses.
If IS_INTERFACE is true this is a call to an interface method and
the first argument is the receiver, which is always a pointer.
This argument, the receiver, is not described in FUNC_TYPE.
If IS_METHOD is true this is a call to a method expression. The
first argument is the receiver. It is described in FUNC_TYPE, but
regardless of FUNC_TYPE, it is passed as a pointer. */
void
reflect_call (const struct functype *func_type, FuncVal *func_val,
_Bool is_interface, _Bool is_method, void **params,
void **results)
{
ffi_cif cif;
unsigned char *call_result;
__go_assert ((func_type->typ.kind & kindMask) == kindFunc);
ffiFuncToCIF (func_type, is_interface, is_method, &cif);
call_result = (unsigned char *) malloc (go_results_size (func_type));
ffi_call_go (&cif, (void (*)(void)) func_val->fn, call_result, params,
func_val);
/* Some day we may need to free result values if RESULTS is
NULL. */
if (results != NULL)
go_set_results (func_type, call_result, results);
free (call_result);
}
#else /* !defined(USE_LIBFFI) */
void
reflect_call (const struct functype *func_type __attribute__ ((unused)),
FuncVal *func_val __attribute__ ((unused)),
_Bool is_interface __attribute__ ((unused)),
_Bool is_method __attribute__ ((unused)),
void **params __attribute__ ((unused)),
void **results __attribute__ ((unused)))
{
/* Without FFI there is nothing we can do. */
runtime_throw("libgo built without FFI does not support "
"reflect.Call or runtime.SetFinalizer");
}
#endif /* !defined(USE_LIBFFI) */
|