Daily bump.
[official-gcc.git] / libgo / go / reflect / makefunc_ffi_c.c
blobef5fb9f083f63500db60abad9435b827a85bcce7
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 #include "runtime.h"
6 #include "go-type.h"
8 #ifdef USE_LIBFFI
10 #include "ffi.h"
12 #if FFI_GO_CLOSURES
13 #define USE_LIBFFI_CLOSURES
14 #endif
16 #endif /* defined(USE_LIBFFI) */
18 /* Declare C functions with the names used to call from Go. */
20 void makeFuncFFI(void *cif, void *impl)
21 __asm__ (GOSYM_PREFIX "reflect.makeFuncFFI");
23 #ifdef USE_LIBFFI_CLOSURES
25 /* The function that we pass to ffi_prep_closure_loc. This calls the Go
26 function ffiCall with the pointer to the arguments, the results area,
27 and the closure structure. */
29 extern void FFICallbackGo(void *result, void **args, ffi_go_closure *closure)
30 __asm__ (GOSYM_PREFIX "reflect.FFICallbackGo");
32 extern void makefuncfficanrecover(Slice)
33 __asm__ (GOSYM_PREFIX "runtime.makefuncfficanrecover");
35 extern void makefuncreturning(void)
36 __asm__ (GOSYM_PREFIX "runtime.makefuncreturning");
38 static void ffi_callback (ffi_cif *, void *, void **, void *)
39 __asm__ ("reflect.ffi_callback");
41 static void
42 ffi_callback (ffi_cif* cif __attribute__ ((unused)), void *results,
43 void **args, void *closure)
45 Location locs[8];
46 int n;
47 int i;
49 /* This function is called from some series of FFI closure functions
50 called by a Go function. We want to see whether the caller of
51 the closure functions can recover. Look up the stack and skip
52 the FFI functions. */
53 n = runtime_callers (1, &locs[0], sizeof locs / sizeof locs[0], true);
54 for (i = 0; i < n; i++)
56 const byte *name;
58 if (locs[i].function.len == 0)
59 continue;
60 if (locs[i].function.len < 4)
61 break;
62 name = locs[i].function.str;
63 if (name[0] != 'f' || name[1] != 'f' || name[2] != 'i' || name[3] != '_')
64 break;
66 if (i < n)
68 Slice s;
70 s.__values = (void *) &locs[i];
71 s.__count = n - i;
72 s.__capacity = n - i;
73 makefuncfficanrecover (s);
76 FFICallbackGo(results, args, closure);
78 if (i < n)
79 makefuncreturning ();
82 /* Allocate an FFI closure and arrange to call ffi_callback. */
84 void
85 makeFuncFFI(void *cif, void *impl)
87 ffi_prep_go_closure(impl, (ffi_cif*)cif, ffi_callback);
90 #else /* !defined(USE_LIBFFI_CLOSURES) */
92 void
93 makeFuncFFI(void *cif __attribute__ ((unused)),
94 void *impl __attribute__ ((unused)))
96 runtime_panicstring ("libgo built without FFI does not support "
97 "reflect.MakeFunc");
100 #endif