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. */
13 #include "go-assert.h"
17 #if defined(USE_LIBFFI) && FFI_GO_CLOSURES
19 /* The functions in this file are only called from reflect_call. As
20 reflect_call calls a libffi function, which will be compiled
21 without -fsplit-stack, it will always run with a large stack. */
23 static size_t go_results_size (const struct __go_func_type
*)
24 __attribute__ ((no_split_stack
));
25 static void go_set_results (const struct __go_func_type
*, unsigned char *,
27 __attribute__ ((no_split_stack
));
29 /* Get the total size required for the result parameters of a
33 go_results_size (const struct __go_func_type
*func
)
36 const struct __go_type_descriptor
**types
;
41 count
= func
->__out
.__count
;
45 types
= (const struct __go_type_descriptor
**) func
->__out
.__values
;
47 /* A single integer return value is always promoted to a full
51 switch (types
[0]->__code
& GO_CODE_MASK
)
62 return sizeof (ffi_arg
);
71 for (i
= 0; i
< count
; ++i
)
75 align
= types
[i
]->__field_align
;
78 off
= (off
+ align
- 1) & ~ (align
- 1);
79 off
+= types
[i
]->__size
;
82 off
= (off
+ maxalign
- 1) & ~ (maxalign
- 1);
87 /* Copy the results of calling a function via FFI from CALL_RESULT
88 into the addresses in RESULTS. */
91 go_set_results (const struct __go_func_type
*func
, unsigned char *call_result
,
95 const struct __go_type_descriptor
**types
;
99 count
= func
->__out
.__count
;
103 types
= (const struct __go_type_descriptor
**) func
->__out
.__values
;
105 /* A single integer return value is always promoted to a full
109 switch (types
[0]->__code
& GO_CODE_MASK
)
123 unsigned char buf
[sizeof (ffi_arg
)];
128 __builtin_memcpy (&u
.buf
, call_result
, sizeof (ffi_arg
));
131 switch (types
[0]->__size
)
138 __builtin_memcpy (results
[0], &b
, 1);
147 __builtin_memcpy (results
[0], &s
, 2);
156 __builtin_memcpy (results
[0], &w
, 4);
165 __builtin_memcpy (results
[0], &d
, 8);
181 for (i
= 0; i
< count
; ++i
)
186 align
= types
[i
]->__field_align
;
187 size
= types
[i
]->__size
;
188 off
= (off
+ align
- 1) & ~ (align
- 1);
189 __builtin_memcpy (results
[i
], call_result
+ off
, size
);
194 /* Call a function. The type of the function is FUNC_TYPE, and the
195 closure is FUNC_VAL. PARAMS is an array of parameter addresses.
196 RESULTS is an array of result addresses.
198 If IS_INTERFACE is true this is a call to an interface method and
199 the first argument is the receiver, which is always a pointer.
200 This argument, the receiver, is not described in FUNC_TYPE.
202 If IS_METHOD is true this is a call to a method expression. The
203 first argument is the receiver. It is described in FUNC_TYPE, but
204 regardless of FUNC_TYPE, it is passed as a pointer. */
207 reflect_call (const struct __go_func_type
*func_type
, FuncVal
*func_val
,
208 _Bool is_interface
, _Bool is_method
, void **params
,
212 unsigned char *call_result
;
214 __go_assert ((func_type
->__common
.__code
& GO_CODE_MASK
) == GO_FUNC
);
215 __go_func_to_cif (func_type
, is_interface
, is_method
, &cif
);
217 call_result
= (unsigned char *) malloc (go_results_size (func_type
));
219 ffi_call_go (&cif
, func_val
->fn
, call_result
, params
, func_val
);
221 /* Some day we may need to free result values if RESULTS is
224 go_set_results (func_type
, call_result
, results
);
229 #else /* !defined(USE_LIBFFI) */
232 reflect_call (const struct __go_func_type
*func_type
__attribute__ ((unused
)),
233 FuncVal
*func_val
__attribute__ ((unused
)),
234 _Bool is_interface
__attribute__ ((unused
)),
235 _Bool is_method
__attribute__ ((unused
)),
236 void **params
__attribute__ ((unused
)),
237 void **results
__attribute__ ((unused
)))
239 /* Without FFI there is nothing we can do. */
240 runtime_throw("libgo built without FFI does not support "
241 "reflect.Call or runtime.SetFinalizer");
244 #endif /* !defined(USE_LIBFFI) */