aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.8/libgo/go/reflect
diff options
context:
space:
mode:
Diffstat (limited to 'gcc-4.8/libgo/go/reflect')
-rw-r--r--gcc-4.8/libgo/go/reflect/all_test.go60
-rw-r--r--gcc-4.8/libgo/go/reflect/makefunc.go90
-rw-r--r--gcc-4.8/libgo/go/reflect/makefunc_386.S36
-rw-r--r--gcc-4.8/libgo/go/reflect/makefunc_amd64.S15
-rw-r--r--gcc-4.8/libgo/go/reflect/makefuncgo_386.go24
-rw-r--r--gcc-4.8/libgo/go/reflect/makefuncgo_amd64.go2
-rw-r--r--gcc-4.8/libgo/go/reflect/type.go2
-rw-r--r--gcc-4.8/libgo/go/reflect/value.go43
8 files changed, 219 insertions, 53 deletions
diff --git a/gcc-4.8/libgo/go/reflect/all_test.go b/gcc-4.8/libgo/go/reflect/all_test.go
index 526f09bb2..50f49e066 100644
--- a/gcc-4.8/libgo/go/reflect/all_test.go
+++ b/gcc-4.8/libgo/go/reflect/all_test.go
@@ -1430,6 +1430,46 @@ func TestFunc(t *testing.T) {
}
}
+type emptyStruct struct{}
+
+type nonEmptyStruct struct {
+ member int
+}
+
+func returnEmpty() emptyStruct {
+ return emptyStruct{}
+}
+
+func takesEmpty(e emptyStruct) {
+}
+
+func returnNonEmpty(i int) nonEmptyStruct {
+ return nonEmptyStruct{member: i}
+}
+
+func takesNonEmpty(n nonEmptyStruct) int {
+ return n.member
+}
+
+func TestCallWithStruct(t *testing.T) {
+ r := ValueOf(returnEmpty).Call([]Value{})
+ if len(r) != 1 || r[0].Type() != TypeOf(emptyStruct{}) {
+ t.Errorf("returning empty struct returned %s instead", r)
+ }
+ r = ValueOf(takesEmpty).Call([]Value{ValueOf(emptyStruct{})})
+ if len(r) != 0 {
+ t.Errorf("takesEmpty returned values: %s", r)
+ }
+ r = ValueOf(returnNonEmpty).Call([]Value{ValueOf(42)})
+ if len(r) != 1 || r[0].Type() != TypeOf(nonEmptyStruct{}) || r[0].Field(0).Int() != 42 {
+ t.Errorf("returnNonEmpty returned %s", r)
+ }
+ r = ValueOf(takesNonEmpty).Call([]Value{ValueOf(nonEmptyStruct{member: 42})})
+ if len(r) != 1 || r[0].Type() != TypeOf(1) || r[0].Int() != 42 {
+ t.Errorf("takesNonEmpty returned %s", r)
+ }
+}
+
func TestMakeFunc(t *testing.T) {
switch runtime.GOARCH {
case "amd64", "386":
@@ -1587,9 +1627,13 @@ func TestMethod(t *testing.T) {
}
}
-/* Not yet implemented for gccgo
-
func TestMethodValue(t *testing.T) {
+ switch runtime.GOARCH {
+ case "amd64", "386":
+ default:
+ t.Skip("reflect method values not implemented for " + runtime.GOARCH)
+ }
+
p := Point{3, 4}
var i int64
@@ -1658,8 +1702,6 @@ func TestMethodValue(t *testing.T) {
}
}
-*/
-
// Reflect version of $GOROOT/test/method5.go
// Concrete types implementing M method.
@@ -1744,14 +1786,18 @@ type Tm4 struct {
func (t4 Tm4) M(x int, b byte) (byte, int) { return b, x + 40 }
func TestMethod5(t *testing.T) {
- /* Not yet used for gccgo
+ switch runtime.GOARCH {
+ case "amd64", "386":
+ default:
+ t.Skip("reflect method values not implemented for " + runtime.GOARCH)
+ }
+
CheckF := func(name string, f func(int, byte) (byte, int), inc int) {
b, x := f(1000, 99)
if b != 99 || x != 1000+inc {
t.Errorf("%s(1000, 99) = %v, %v, want 99, %v", name, b, x, 1000+inc)
}
}
- */
CheckV := func(name string, i Value, inc int) {
bx := i.Method(0).Call([]Value{ValueOf(1000), ValueOf(byte(99))})
@@ -1761,9 +1807,7 @@ func TestMethod5(t *testing.T) {
t.Errorf("direct %s.M(1000, 99) = %v, %v, want 99, %v", name, b, x, 1000+inc)
}
- /* Not yet implemented for gccgo
CheckF(name+".M", i.Method(0).Interface().(func(int, byte) (byte, int)), inc)
- */
}
var TinterType = TypeOf(new(Tinter)).Elem()
diff --git a/gcc-4.8/libgo/go/reflect/makefunc.go b/gcc-4.8/libgo/go/reflect/makefunc.go
index 3e8085bec..9f150ea65 100644
--- a/gcc-4.8/libgo/go/reflect/makefunc.go
+++ b/gcc-4.8/libgo/go/reflect/makefunc.go
@@ -17,6 +17,11 @@ type makeFuncImpl struct {
code uintptr
typ *funcType
fn func([]Value) []Value
+
+ // For gccgo we use the same entry point for functions and for
+ // method values.
+ method int
+ rcvr Value
}
// MakeFunc returns a new function of the given Type
@@ -61,7 +66,7 @@ func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value {
dummy := makeFuncStub
code := **(**uintptr)(unsafe.Pointer(&dummy))
- impl := &makeFuncImpl{code: code, typ: ftyp, fn: fn}
+ impl := &makeFuncImpl{code: code, typ: ftyp, fn: fn, method: -1}
return Value{t, unsafe.Pointer(&impl), flag(Func<<flagKindShift) | flagIndir}
}
@@ -85,15 +90,94 @@ func makeMethodValue(op string, v Value) Value {
panic("reflect: internal error: invalid use of makePartialFunc")
}
+ switch runtime.GOARCH {
+ case "amd64", "386":
+ default:
+ panic("reflect.makeMethodValue not implemented for " + runtime.GOARCH)
+ }
+
// Ignoring the flagMethod bit, v describes the receiver, not the method type.
fl := v.flag & (flagRO | flagAddr | flagIndir)
fl |= flag(v.typ.Kind()) << flagKindShift
rcvr := Value{v.typ, v.val, fl}
+ // v.Type returns the actual type of the method value.
+ ft := v.Type().(*rtype)
+
+ // Indirect Go func value (dummy) to obtain
+ // actual code address. (A Go func value is a pointer
+ // to a C function pointer. http://golang.org/s/go11func.)
+ dummy := makeFuncStub
+ code := **(**uintptr)(unsafe.Pointer(&dummy))
+
// Cause panic if method is not appropriate.
// The panic would still happen during the call if we omit this,
// but we want Interface() and other operations to fail early.
- methodReceiver(op, rcvr, int(v.flag)>>flagMethodShift)
+ t, _, _ := methodReceiver(op, rcvr, int(v.flag)>>flagMethodShift)
+
+ fv := &makeFuncImpl{
+ code: code,
+ typ: (*funcType)(unsafe.Pointer(t)),
+ method: int(v.flag) >> flagMethodShift,
+ rcvr: rcvr,
+ }
+
+ return Value{ft, unsafe.Pointer(&fv), v.flag&flagRO | flag(Func)<<flagKindShift | flagIndir}
+}
+
+// makeValueMethod takes a method function and returns a function that
+// takes a value receiver and calls the real method with a pointer to
+// it.
+func makeValueMethod(v Value) Value {
+ typ := v.typ
+ if typ.Kind() != Func {
+ panic("reflect: call of makeValueMethod with non-Func type")
+ }
+ if v.flag&flagMethodFn == 0 {
+ panic("reflect: call of makeValueMethod with non-MethodFn")
+ }
- panic("reflect makeMethodValue not implemented")
+ switch runtime.GOARCH {
+ case "amd64", "386":
+ default:
+ panic("reflect.makeValueMethod not implemented for " + runtime.GOARCH)
+ }
+
+ t := typ.common()
+ ftyp := (*funcType)(unsafe.Pointer(t))
+
+ // Indirect Go func value (dummy) to obtain
+ // actual code address. (A Go func value is a pointer
+ // to a C function pointer. http://golang.org/s/go11func.)
+ dummy := makeFuncStub
+ code := **(**uintptr)(unsafe.Pointer(&dummy))
+
+ impl := &makeFuncImpl{
+ code: code,
+ typ: ftyp,
+ method: -2,
+ rcvr: v,
+ }
+
+ return Value{t, unsafe.Pointer(&impl), flag(Func<<flagKindShift) | flagIndir}
+}
+
+// Call the function represented by a makeFuncImpl.
+func (c *makeFuncImpl) call(in []Value) []Value {
+ if c.method == -1 {
+ return c.fn(in)
+ } else if c.method == -2 {
+ if c.typ.IsVariadic() {
+ return c.rcvr.CallSlice(in)
+ } else {
+ return c.rcvr.Call(in)
+ }
+ } else {
+ m := c.rcvr.Method(c.method)
+ if c.typ.IsVariadic() {
+ return m.CallSlice(in)
+ } else {
+ return m.Call(in)
+ }
+ }
}
diff --git a/gcc-4.8/libgo/go/reflect/makefunc_386.S b/gcc-4.8/libgo/go/reflect/makefunc_386.S
index 5878bc44c..0e2e76465 100644
--- a/gcc-4.8/libgo/go/reflect/makefunc_386.S
+++ b/gcc-4.8/libgo/go/reflect/makefunc_386.S
@@ -25,9 +25,13 @@ reflect.makeFuncStub:
struct {
esp uint32 // 0x0
eax uint32 // 0x4
- st0 uint64 // 0x8
+ st0 float64 // 0x8
+ sr bool // 0x10
+ sf bool // 0x11
}
- */
+ The sr field is set by the function to a non-zero value if
+ the function takes a struct hidden pointer that must be
+ popped off the stack. */
pushl %ebp
.LCFI0:
@@ -45,6 +49,15 @@ reflect.makeFuncStub:
leal 8(%ebp), %eax /* Set esp field in struct. */
movl %eax, -24(%ebp)
+ /* For MakeFunc functions that call recover. */
+ movl 4(%ebp), %eax
+ movl %eax, (%esp)
+#ifdef __PIC__
+ call __go_makefunc_can_recover@PLT
+#else
+ call __go_makefunc_can_recover
+#endif
+
#ifdef __PIC__
call __go_get_closure@PLT
#else
@@ -62,9 +75,20 @@ reflect.makeFuncStub:
call reflect.MakeFuncStubGo
#endif
+ /* MakeFunc functions can no longer call recover. */
+#ifdef __PIC__
+ call __go_makefunc_returning@PLT
+#else
+ call __go_makefunc_returning
+#endif
+
/* Set return registers. */
movl -20(%ebp), %eax
+
+ cmpb $0, -7(%ebp)
+ je 2f
+
fldl -16(%ebp)
#ifdef __SSE2__
@@ -73,12 +97,20 @@ reflect.makeFuncStub:
movsd -16(%ebp), %xmm0
#endif
+2:
+ movb -8(%ebp), %dl
+
addl $36, %esp
popl %ebx
.LCFI3:
popl %ebp
.LCFI4:
+
+ testb %dl,%dl
+ jne 1f
ret
+1:
+ ret $4
.LFE1:
#ifdef __ELF__
.size reflect.makeFuncStub, . - reflect.makeFuncStub
diff --git a/gcc-4.8/libgo/go/reflect/makefunc_amd64.S b/gcc-4.8/libgo/go/reflect/makefunc_amd64.S
index 9d12f193f..88302eee1 100644
--- a/gcc-4.8/libgo/go/reflect/makefunc_amd64.S
+++ b/gcc-4.8/libgo/go/reflect/makefunc_amd64.S
@@ -61,6 +61,14 @@ reflect.makeFuncStub:
movdqa %xmm6, 0xa0(%rsp)
movdqa %xmm7, 0xb0(%rsp)
+ /* For MakeFunc functions that call recover. */
+ movq 8(%rbp), %rdi
+#ifdef __PIC__
+ call __go_makefunc_can_recover@PLT
+#else
+ call __go_makefunc_can_recover
+#endif
+
# Get function type.
#ifdef __PIC__
call __go_get_closure@PLT
@@ -77,6 +85,13 @@ reflect.makeFuncStub:
call reflect.MakeFuncStubGo
#endif
+ /* MakeFunc functions can no longer call recover. */
+#ifdef __PIC__
+ call __go_makefunc_returning@PLT
+#else
+ call __go_makefunc_returning
+#endif
+
# The structure will be updated with any return values. Load
# all possible return registers before returning to the caller.
diff --git a/gcc-4.8/libgo/go/reflect/makefuncgo_386.go b/gcc-4.8/libgo/go/reflect/makefuncgo_386.go
index 0fac1f488..45dd2d759 100644
--- a/gcc-4.8/libgo/go/reflect/makefuncgo_386.go
+++ b/gcc-4.8/libgo/go/reflect/makefuncgo_386.go
@@ -15,7 +15,9 @@ import "unsafe"
type i386Regs struct {
esp uint32
eax uint32 // Value to return in %eax.
- st0 uint64 // Value to return in %st(0).
+ st0 float64 // Value to return in %st(0).
+ sr bool // Set to true if hidden struct pointer.
+ sf bool // Set to true if returning float
}
// MakeFuncStubGo implements the 386 calling convention for MakeFunc.
@@ -56,10 +58,13 @@ func MakeFuncStubGo(regs *i386Regs, c *makeFuncImpl) {
in := make([]Value, 0, len(ftyp.in))
ap := uintptr(regs.esp)
+ regs.sr = false
+ regs.sf = false
var retPtr unsafe.Pointer
if retStruct {
retPtr = *(*unsafe.Pointer)(unsafe.Pointer(ap))
ap += ptrSize
+ regs.sr = true
}
for _, rt := range ftyp.in {
@@ -77,7 +82,7 @@ func MakeFuncStubGo(regs *i386Regs, c *makeFuncImpl) {
// Call the real function.
- out := c.fn(in)
+ out := c.call(in)
if len(out) != len(ftyp.out) {
panic("reflect: wrong return count from function created by MakeFunc")
@@ -123,13 +128,16 @@ func MakeFuncStubGo(regs *i386Regs, c *makeFuncImpl) {
v := out[0]
w := v.iword()
- if v.Kind() != Ptr && v.Kind() != UnsafePointer {
- w = loadIword(unsafe.Pointer(w), v.typ.size)
- }
switch v.Kind() {
- case Float32, Float64:
- regs.st0 = uint64(uintptr(w))
- default:
+ case Ptr, UnsafePointer:
regs.eax = uint32(uintptr(w))
+ case Float32:
+ regs.st0 = float64(*(*float32)(unsafe.Pointer(w)))
+ regs.sf = true
+ case Float64:
+ regs.st0 = *(*float64)(unsafe.Pointer(w))
+ regs.sf = true
+ default:
+ regs.eax = uint32(uintptr(loadIword(unsafe.Pointer(w), v.typ.size)))
}
}
diff --git a/gcc-4.8/libgo/go/reflect/makefuncgo_amd64.go b/gcc-4.8/libgo/go/reflect/makefuncgo_amd64.go
index ecc50a425..42fe03a93 100644
--- a/gcc-4.8/libgo/go/reflect/makefuncgo_amd64.go
+++ b/gcc-4.8/libgo/go/reflect/makefuncgo_amd64.go
@@ -319,7 +319,7 @@ argloop:
// All the real arguments have been found and turned into
// Value's. Call the real function.
- out := c.fn(in)
+ out := c.call(in)
if len(out) != len(ftyp.out) {
panic("reflect: wrong return count from function created by MakeFunc")
diff --git a/gcc-4.8/libgo/go/reflect/type.go b/gcc-4.8/libgo/go/reflect/type.go
index d084f38eb..2b8342cb5 100644
--- a/gcc-4.8/libgo/go/reflect/type.go
+++ b/gcc-4.8/libgo/go/reflect/type.go
@@ -508,7 +508,7 @@ func (t *uncommonType) Method(i int) (m Method) {
m.Type = toType(mt)
x := new(unsafe.Pointer)
*x = unsafe.Pointer(&p.tfn)
- m.Func = Value{mt, unsafe.Pointer(x), fl | flagIndir}
+ m.Func = Value{mt, unsafe.Pointer(x), fl | flagIndir | flagMethodFn}
m.Index = i
return
}
diff --git a/gcc-4.8/libgo/go/reflect/value.go b/gcc-4.8/libgo/go/reflect/value.go
index b199f7088..492155781 100644
--- a/gcc-4.8/libgo/go/reflect/value.go
+++ b/gcc-4.8/libgo/go/reflect/value.go
@@ -98,6 +98,7 @@ const (
flagIndir
flagAddr
flagMethod
+ flagMethodFn // gccgo: first fn parameter is always pointer
flagKindShift = iota
flagKindWidth = 5 // there are 27 kinds
flagKindMask flag = 1<<flagKindWidth - 1
@@ -433,7 +434,7 @@ func (v Value) call(op string, in []Value) []Value {
if v.flag&flagMethod != 0 {
nin++
}
- firstPointer := len(in) > 0 && t.In(0).Kind() != Ptr && v.flag&flagMethod == 0 && isMethod(v.typ)
+ firstPointer := len(in) > 0 && t.In(0).Kind() != Ptr && v.flag&flagMethodFn != 0
params := make([]unsafe.Pointer, nin)
off := 0
if v.flag&flagMethod != 0 {
@@ -484,33 +485,6 @@ func (v Value) call(op string, in []Value) []Value {
return ret
}
-// gccgo specific test to see if typ is a method. We can tell by
-// looking at the string to see if there is a receiver. We need this
-// because for gccgo all methods take pointer receivers.
-func isMethod(t *rtype) bool {
- if Kind(t.kind) != Func {
- return false
- }
- s := *t.string
- parens := 0
- params := 0
- sawRet := false
- for i, c := range s {
- if c == '(' {
- if parens == 0 {
- params++
- }
- parens++
- } else if c == ')' {
- parens--
- } else if parens == 0 && c == ' ' && s[i+1] != '(' && !sawRet {
- params++
- sawRet = true
- }
- }
- return params > 2
-}
-
// methodReceiver returns information about the receiver
// described by v. The Value v may or may not have the
// flagMethod bit set, so the kind cached in v.flag should
@@ -917,6 +891,16 @@ func valueInterface(v Value, safe bool) interface{} {
v = makeMethodValue("Interface", v)
}
+ if v.flag&flagMethodFn != 0 {
+ if v.typ.Kind() != Func {
+ panic("reflect: MethodFn of non-Func")
+ }
+ ft := (*funcType)(unsafe.Pointer(v.typ))
+ if ft.in[0].Kind() != Ptr {
+ v = makeValueMethod(v)
+ }
+ }
+
k := v.kind()
if k == Interface {
// Special case: return the element inside the interface.
@@ -1230,8 +1214,7 @@ func (v Value) Pointer() uintptr {
// created via reflect have the same underlying code pointer,
// so their Pointers are equal. The function used here must
// match the one used in makeMethodValue.
- // This is not properly implemented for gccgo.
- f := Zero
+ f := makeFuncStub
return **(**uintptr)(unsafe.Pointer(&f))
}
p := v.val