aboutsummaryrefslogtreecommitdiff
path: root/libgo/go
diff options
context:
space:
mode:
authorIan Lance Taylor <ian@gcc.gnu.org>2016-11-18 00:15:38 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2016-11-18 00:15:38 +0000
commit5302cd025063e00bde7d102284f638977dde5f27 (patch)
tree67be8f93c3e7e9a2a592f5932622a54083146948 /libgo/go
parentf97db488a6ad72106426990653fa397d690d1654 (diff)
downloadgcc-5302cd025063e00bde7d102284f638977dde5f27.zip
gcc-5302cd025063e00bde7d102284f638977dde5f27.tar.gz
gcc-5302cd025063e00bde7d102284f638977dde5f27.tar.bz2
runtime, reflect: rewrite Go to FFI type conversion in Go
As we move toward the Go 1.7 garbage collector, it's essential that all allocation of values that can contain Go pointers be done using the correct type descriptor. That is simplest if we do all such allocation in Go code. This rewrites the code that converts from a Go type to a libffi CIF into Go. Reviewed-on: https://go-review.googlesource.com/33353 From-SVN: r242578
Diffstat (limited to 'libgo/go')
-rw-r--r--libgo/go/reflect/makefunc.go6
-rw-r--r--libgo/go/reflect/makefunc_ffi.go5
-rw-r--r--libgo/go/reflect/makefunc_ffi_c.c15
-rw-r--r--libgo/go/runtime/ffi.go315
4 files changed, 327 insertions, 14 deletions
diff --git a/libgo/go/reflect/makefunc.go b/libgo/go/reflect/makefunc.go
index 543a335..3a9fd28 100644
--- a/libgo/go/reflect/makefunc.go
+++ b/libgo/go/reflect/makefunc.go
@@ -63,7 +63,7 @@ func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value {
method: -1,
}
- makeFuncFFI(ftyp, unsafe.Pointer(impl))
+ makeFuncFFI(makeCIF(ftyp), unsafe.Pointer(impl))
return Value{t, unsafe.Pointer(&impl), flag(Func) | flagIndir}
}
@@ -102,7 +102,7 @@ func makeMethodValue(op string, v Value) Value {
rcvr: rcvr,
}
- makeFuncFFI(ftyp, unsafe.Pointer(fv))
+ makeFuncFFI(makeCIF(ftyp), unsafe.Pointer(fv))
return Value{ft, unsafe.Pointer(&fv), v.flag&flagRO | flag(Func) | flagIndir}
}
@@ -128,7 +128,7 @@ func makeValueMethod(v Value) Value {
rcvr: v,
}
- makeFuncFFI(ftyp, unsafe.Pointer(impl))
+ makeFuncFFI(makeCIF(ftyp), unsafe.Pointer(impl))
return Value{t, unsafe.Pointer(&impl), v.flag&flagRO | flag(Func) | flagIndir}
}
diff --git a/libgo/go/reflect/makefunc_ffi.go b/libgo/go/reflect/makefunc_ffi.go
index c821131..2acf7bb 100644
--- a/libgo/go/reflect/makefunc_ffi.go
+++ b/libgo/go/reflect/makefunc_ffi.go
@@ -10,7 +10,10 @@ import (
// The makeFuncFFI function, written in C, fills in an FFI closure.
// It arranges for ffiCall to be invoked directly from FFI.
-func makeFuncFFI(ftyp *funcType, impl unsafe.Pointer)
+func makeFuncFFI(cif unsafe.Pointer, impl unsafe.Pointer)
+
+// The makeCIF function, implemented in the runtime package, allocates a CIF.
+func makeCIF(ft *funcType) unsafe.Pointer
// FFICallbackGo implements the Go side of the libffi callback.
// It is exported so that C code can call it.
diff --git a/libgo/go/reflect/makefunc_ffi_c.c b/libgo/go/reflect/makefunc_ffi_c.c
index 06a41ef..d3935eb 100644
--- a/libgo/go/reflect/makefunc_ffi_c.c
+++ b/libgo/go/reflect/makefunc_ffi_c.c
@@ -8,7 +8,7 @@
#ifdef USE_LIBFFI
-#include "go-ffi.h"
+#include "ffi.h"
#if FFI_GO_CLOSURES
#define USE_LIBFFI_CLOSURES
@@ -18,7 +18,7 @@
/* Declare C functions with the names used to call from Go. */
-void makeFuncFFI(const struct __go_func_type *ftyp, void *impl)
+void makeFuncFFI(void *cif, void *impl)
__asm__ (GOSYM_PREFIX "reflect.makeFuncFFI");
#ifdef USE_LIBFFI_CLOSURES
@@ -70,20 +70,15 @@ ffi_callback (ffi_cif* cif __attribute__ ((unused)), void *results,
/* Allocate an FFI closure and arrange to call ffi_callback. */
void
-makeFuncFFI(const struct __go_func_type *ftyp, void *impl)
+makeFuncFFI(void *cif, void *impl)
{
- ffi_cif *cif;
-
- cif = (ffi_cif *) __go_alloc (sizeof (ffi_cif));
- __go_func_to_cif (ftyp, 0, 0, cif);
-
- ffi_prep_go_closure(impl, cif, ffi_callback);
+ ffi_prep_go_closure(impl, (ffi_cif*)cif, ffi_callback);
}
#else /* !defined(USE_LIBFFI_CLOSURES) */
void
-makeFuncFFI(const struct __go_func_type *ftyp __attribute__ ((unused)),
+makeFuncFFI(void *cif __attribute__ ((unused)),
void *impl __attribute__ ((unused)))
{
runtime_panicstring ("libgo built without FFI does not support "
diff --git a/libgo/go/runtime/ffi.go b/libgo/go/runtime/ffi.go
new file mode 100644
index 0000000..164e177
--- /dev/null
+++ b/libgo/go/runtime/ffi.go
@@ -0,0 +1,315 @@
+// 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.
+
+// Only build this file if libffi is supported.
+
+// +build libffi
+
+package runtime
+
+import "unsafe"
+
+// This file contains the code that converts a Go type to an FFI type.
+// This has to be written in Go because it allocates memory in the Go heap.
+
+// C functions to return pointers to libffi variables.
+
+func ffi_type_pointer() *__ffi_type
+func ffi_type_sint8() *__ffi_type
+func ffi_type_sint16() *__ffi_type
+func ffi_type_sint32() *__ffi_type
+func ffi_type_sint64() *__ffi_type
+func ffi_type_uint8() *__ffi_type
+func ffi_type_uint16() *__ffi_type
+func ffi_type_uint32() *__ffi_type
+func ffi_type_uint64() *__ffi_type
+func ffi_type_float() *__ffi_type
+func ffi_type_double() *__ffi_type
+func ffi_supports_complex() bool
+func ffi_type_complex_float() *__ffi_type
+func ffi_type_complex_double() *__ffi_type
+func ffi_type_void() *__ffi_type
+
+// C functions defined in libffi.
+
+//extern ffi_prep_cif
+func ffi_prep_cif(*_ffi_cif, _ffi_abi, uint32, *__ffi_type, **__ffi_type) _ffi_status
+
+// ffiFuncToCIF is called from C code.
+//go:linkname ffiFuncToCIF runtime.ffiFuncToCIF
+
+// ffiFuncToCIF builds an _ffi_cif struct for function described by ft.
+func ffiFuncToCIF(ft *functype, isInterface bool, isMethod bool, cif *_ffi_cif) {
+ nparams := len(ft.in)
+ nargs := nparams
+ if isInterface {
+ nargs++
+ }
+ args := make([]*__ffi_type, nargs)
+ i := 0
+ off := 0
+ if isInterface {
+ args[0] = ffi_type_pointer()
+ off = 1
+ } else if isMethod {
+ args[0] = ffi_type_pointer()
+ i = 1
+ }
+ for ; i < nparams; i++ {
+ args[i+off] = typeToFFI(ft.in[i])
+ }
+
+ rettype := funcReturnFFI(ft)
+
+ var pargs **__ffi_type
+ if len(args) > 0 {
+ pargs = &args[0]
+ }
+ status := ffi_prep_cif(cif, _FFI_DEFAULT_ABI, uint32(nargs), rettype, pargs)
+ if status != _FFI_OK {
+ throw("ffi_prep_cif failed")
+ }
+}
+
+// funcReturnFFI returns the FFI definition of the return type of ft.
+func funcReturnFFI(ft *functype) *__ffi_type {
+ c := len(ft.out)
+ if c == 0 {
+ return ffi_type_void()
+ }
+
+ // Compile a function that returns a zero-sized value as
+ // though it returns void. This works around a problem in
+ // libffi: it can't represent a zero-sized value.
+ var size uintptr
+ for _, v := range ft.out {
+ size += v.size
+ }
+ if size == 0 {
+ return ffi_type_void()
+ }
+
+ if c == 1 {
+ return typeToFFI(ft.out[0])
+ }
+
+ elements := make([]*__ffi_type, c+1)
+ for i, v := range ft.out {
+ elements[i] = typeToFFI(v)
+ }
+ elements[c] = nil
+
+ return &__ffi_type{
+ _type: _FFI_TYPE_STRUCT,
+ elements: &elements[0],
+ }
+}
+
+// typeToFFI returns the __ffi_type for a Go type.
+func typeToFFI(typ *_type) *__ffi_type {
+ switch typ.kind & kindMask {
+ case kindBool:
+ switch unsafe.Sizeof(false) {
+ case 1:
+ return ffi_type_uint8()
+ case 4:
+ return ffi_type_uint32()
+ default:
+ throw("bad bool size")
+ return nil
+ }
+ case kindInt:
+ return intToFFI()
+ case kindInt8:
+ return ffi_type_sint8()
+ case kindInt16:
+ return ffi_type_sint16()
+ case kindInt32:
+ return ffi_type_sint32()
+ case kindInt64:
+ return ffi_type_sint64()
+ case kindUint:
+ switch unsafe.Sizeof(uint(0)) {
+ case 4:
+ return ffi_type_uint32()
+ case 8:
+ return ffi_type_uint64()
+ default:
+ throw("bad uint size")
+ return nil
+ }
+ case kindUint8:
+ return ffi_type_uint8()
+ case kindUint16:
+ return ffi_type_uint16()
+ case kindUint32:
+ return ffi_type_uint32()
+ case kindUint64:
+ return ffi_type_uint64()
+ case kindUintptr:
+ switch unsafe.Sizeof(uintptr(0)) {
+ case 4:
+ return ffi_type_uint32()
+ case 8:
+ return ffi_type_uint64()
+ default:
+ throw("bad uinptr size")
+ return nil
+ }
+ case kindFloat32:
+ return ffi_type_float()
+ case kindFloat64:
+ return ffi_type_double()
+ case kindComplex64:
+ if ffi_supports_complex() {
+ return ffi_type_complex_float()
+ } else {
+ return complexToFFI(ffi_type_float())
+ }
+ case kindComplex128:
+ if ffi_supports_complex() {
+ return ffi_type_complex_double()
+ } else {
+ return complexToFFI(ffi_type_double())
+ }
+ case kindArray:
+ return arrayToFFI((*arraytype)(unsafe.Pointer(typ)))
+ case kindChan, kindFunc, kindMap, kindPtr, kindUnsafePointer:
+ // These types are always simple pointers, and for FFI
+ // purposes nothing else matters.
+ return ffi_type_pointer()
+ case kindInterface:
+ return interfaceToFFI()
+ case kindSlice:
+ return sliceToFFI((*slicetype)(unsafe.Pointer(typ)))
+ case kindString:
+ return stringToFFI()
+ case kindStruct:
+ return structToFFI((*structtype)(unsafe.Pointer(typ)))
+ default:
+ throw("unknown type kind")
+ return nil
+ }
+}
+
+// interfaceToFFI returns an ffi_type for a Go interface type.
+// This is used for both empty and non-empty interface types.
+func interfaceToFFI() *__ffi_type {
+ elements := make([]*__ffi_type, 3)
+ elements[0] = ffi_type_pointer()
+ elements[1] = elements[0]
+ elements[2] = nil
+ return &__ffi_type{
+ _type: _FFI_TYPE_STRUCT,
+ elements: &elements[0],
+ }
+}
+
+// stringToFFI returns an ffi_type for a Go string type.
+func stringToFFI() *__ffi_type {
+ elements := make([]*__ffi_type, 3)
+ elements[0] = ffi_type_pointer()
+ elements[1] = intToFFI()
+ elements[2] = nil
+ return &__ffi_type{
+ _type: _FFI_TYPE_STRUCT,
+ elements: &elements[0],
+ }
+}
+
+// structToFFI returns an ffi_type for a Go struct type.
+func structToFFI(typ *structtype) *__ffi_type {
+ c := len(typ.fields)
+ if c == 0 {
+ return emptyStructToFFI()
+ }
+
+ fields := make([]*__ffi_type, c+1)
+ for i, v := range typ.fields {
+ fields[i] = typeToFFI(v.typ)
+ }
+ fields[c] = nil
+ return &__ffi_type{
+ _type: _FFI_TYPE_STRUCT,
+ elements: &fields[0],
+ }
+}
+
+// sliceToFFI returns an ffi_type for a Go slice type.
+func sliceToFFI(typ *slicetype) *__ffi_type {
+ elements := make([]*__ffi_type, 4)
+ elements[0] = ffi_type_pointer()
+ elements[1] = intToFFI()
+ elements[2] = elements[1]
+ elements[3] = nil
+ return &__ffi_type{
+ _type: _FFI_TYPE_STRUCT,
+ elements: &elements[0],
+ }
+}
+
+// complexToFFI returns an ffi_type for a Go complex type.
+// This is only used if libffi does not support complex types internally
+// for this target.
+func complexToFFI(ffiFloatType *__ffi_type) *__ffi_type {
+ elements := make([]*__ffi_type, 3)
+ elements[0] = ffiFloatType
+ elements[1] = ffiFloatType
+ elements[2] = nil
+ return &__ffi_type{
+ _type: _FFI_TYPE_STRUCT,
+ elements: &elements[0],
+ }
+}
+
+// arrayToFFI returns an ffi_type for a Go array type.
+func arrayToFFI(typ *arraytype) *__ffi_type {
+ if typ.len == 0 {
+ return emptyStructToFFI()
+ }
+ elements := make([]*__ffi_type, typ.len+1)
+ et := typeToFFI(typ.elem)
+ for i := uintptr(0); i < typ.len; i++ {
+ elements[i] = et
+ }
+ elements[typ.len] = nil
+ return &__ffi_type{
+ _type: _FFI_TYPE_STRUCT,
+ elements: &elements[0],
+ }
+}
+
+// intToFFI returns an ffi_type for the Go int type.
+func intToFFI() *__ffi_type {
+ switch unsafe.Sizeof(0) {
+ case 4:
+ return ffi_type_sint32()
+ case 8:
+ return ffi_type_sint64()
+ default:
+ throw("bad int size")
+ return nil
+ }
+}
+
+// emptyStructToFFI returns an ffi_type for an empty struct.
+// The libffi library won't accept a struct with no fields.
+func emptyStructToFFI() *__ffi_type {
+ elements := make([]*__ffi_type, 2)
+ elements[0] = ffi_type_void()
+ elements[1] = nil
+ return &__ffi_type{
+ _type: _FFI_TYPE_STRUCT,
+ elements: &elements[0],
+ }
+}
+
+//go:linkname makeCIF reflect.makeCIF
+
+// makeCIF is used by the reflect package to allocate a CIF.
+func makeCIF(ft *functype) *_ffi_cif {
+ cif := new(_ffi_cif)
+ ffiFuncToCIF(ft, false, false, cif)
+ return cif
+}