1 /* go-reflect-call.c -- call reflection support for Go.
3 Copyright 2009 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
12 #include "go-assert.h"
19 #if defined(USE_LIBFFI) && FFI_GO_CLOSURES
21 /* The functions in this file are only called from reflect_call. As
22 reflect_call calls a libffi function, which will be compiled
23 without -fsplit-stack, it will always run with a large stack. */
25 static size_t go_results_size (const struct __go_func_type
*)
26 __attribute__ ((no_split_stack
));
27 static void go_set_results (const struct __go_func_type
*, unsigned char *,
29 __attribute__ ((no_split_stack
));
31 /* Get the total size required for the result parameters of a
35 go_results_size (const struct __go_func_type
*func
)
38 const struct __go_type_descriptor
**types
;
43 count
= func
->__out
.__count
;
47 types
= (const struct __go_type_descriptor
**) func
->__out
.__values
;
49 /* A single integer return value is always promoted to a full
53 switch (types
[0]->__code
& GO_CODE_MASK
)
64 return sizeof (ffi_arg
);
73 for (i
= 0; i
< count
; ++i
)
77 align
= types
[i
]->__field_align
;
80 off
= (off
+ align
- 1) & ~ (align
- 1);
81 off
+= types
[i
]->__size
;
84 off
= (off
+ maxalign
- 1) & ~ (maxalign
- 1);
86 // The libffi library doesn't understand a struct with no fields.
87 // We generate a struct with a single field of type void. When used
88 // as a return value, libffi will think that requires a byte.
95 /* Copy the results of calling a function via FFI from CALL_RESULT
96 into the addresses in RESULTS. */
99 go_set_results (const struct __go_func_type
*func
, unsigned char *call_result
,
103 const struct __go_type_descriptor
**types
;
107 count
= func
->__out
.__count
;
111 types
= (const struct __go_type_descriptor
**) func
->__out
.__values
;
113 /* A single integer return value is always promoted to a full
117 switch (types
[0]->__code
& GO_CODE_MASK
)
131 unsigned char buf
[sizeof (ffi_arg
)];
136 __builtin_memcpy (&u
.buf
, call_result
, sizeof (ffi_arg
));
139 switch (types
[0]->__size
)
146 __builtin_memcpy (results
[0], &b
, 1);
155 __builtin_memcpy (results
[0], &s
, 2);
164 __builtin_memcpy (results
[0], &w
, 4);
173 __builtin_memcpy (results
[0], &d
, 8);
189 for (i
= 0; i
< count
; ++i
)
194 align
= types
[i
]->__field_align
;
195 size
= types
[i
]->__size
;
196 off
= (off
+ align
- 1) & ~ (align
- 1);
197 __builtin_memcpy (results
[i
], call_result
+ off
, size
);
202 /* The code that converts the Go type to an FFI type is written in Go,
203 so that it can allocate Go heap memory. */
204 extern void ffiFuncToCIF(const struct __go_func_type
*, _Bool
, _Bool
, ffi_cif
*)
205 __asm__ ("runtime.ffiFuncToCIF");
207 /* Call a function. The type of the function is FUNC_TYPE, and the
208 closure is FUNC_VAL. PARAMS is an array of parameter addresses.
209 RESULTS is an array of result addresses.
211 If IS_INTERFACE is true this is a call to an interface method and
212 the first argument is the receiver, which is always a pointer.
213 This argument, the receiver, is not described in FUNC_TYPE.
215 If IS_METHOD is true this is a call to a method expression. The
216 first argument is the receiver. It is described in FUNC_TYPE, but
217 regardless of FUNC_TYPE, it is passed as a pointer. */
220 reflect_call (const struct __go_func_type
*func_type
, FuncVal
*func_val
,
221 _Bool is_interface
, _Bool is_method
, void **params
,
225 unsigned char *call_result
;
227 __go_assert ((func_type
->__common
.__code
& GO_CODE_MASK
) == GO_FUNC
);
228 ffiFuncToCIF (func_type
, is_interface
, is_method
, &cif
);
230 call_result
= (unsigned char *) malloc (go_results_size (func_type
));
232 ffi_call_go (&cif
, func_val
->fn
, call_result
, params
, func_val
);
234 /* Some day we may need to free result values if RESULTS is
237 go_set_results (func_type
, call_result
, results
);
242 #else /* !defined(USE_LIBFFI) */
245 reflect_call (const struct __go_func_type
*func_type
__attribute__ ((unused
)),
246 FuncVal
*func_val
__attribute__ ((unused
)),
247 _Bool is_interface
__attribute__ ((unused
)),
248 _Bool is_method
__attribute__ ((unused
)),
249 void **params
__attribute__ ((unused
)),
250 void **results
__attribute__ ((unused
)))
252 /* Without FFI there is nothing we can do. */
253 runtime_throw("libgo built without FFI does not support "
254 "reflect.Call or runtime.SetFinalizer");
257 #endif /* !defined(USE_LIBFFI) */