aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/reflect/makefuncgo_386.go
blob: 96ca430d094b6631dccbcf891741db02d72e3fca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2013 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.

// MakeFunc 386 implementation.

package reflect

import "unsafe"

// The assembler stub will pass a pointer to this structure.  We
// assume that no parameters are passed in registers--that is, we do
// not support the -mregparm option.  On return we will set the
// registers that might hold result values.
type i386Regs struct {
	esp uint32
	eax uint32  // Value to return in %eax.
	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.
// This should not be called.  It is exported so that assembly code
// can call it.

func MakeFuncStubGo(regs *i386Regs, c *makeFuncImpl) {
	ftyp := c.typ

	// See if the result requires a struct.  If it does, the first
	// parameter is a pointer to the struct.
	retStruct := false
	retEmpty := false
	switch len(ftyp.out) {
	case 0:
		retEmpty = true
	case 1:
		if ftyp.out[0].size == 0 {
			retEmpty = true
		} else {
			switch ftyp.out[0].Kind() {
			case Complex64, Complex128, Array, Interface, Slice, String, Struct:
				retStruct = true
			}
		}
	default:
		size := uintptr(0)
		for _, typ := range ftyp.out {
			size += typ.size
		}
		if size == 0 {
			retEmpty = true
		} else {
			retStruct = true
		}
	}

	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 {
		ap = align(ap, ptrSize)

		// We have to copy the argument onto the heap in case
		// the function hangs on the reflect.Value we pass it.
		p := unsafe_New(rt)
		memmove(p, unsafe.Pointer(ap), rt.size)

		v := Value{rt, p, flag(rt.Kind()<<flagKindShift) | flagIndir}
		in = append(in, v)
		ap += rt.size
	}

	// Call the real function.

	out := c.call(in)

	if len(out) != len(ftyp.out) {
		panic("reflect: wrong return count from function created by MakeFunc")
	}

	for i, typ := range ftyp.out {
		v := out[i]
		if v.typ != typ {
			panic("reflect: function created by MakeFunc using " + funcName(c.fn) +
				" returned wrong type: have " +
				out[i].typ.String() + " for " + typ.String())
		}
		if v.flag&flagRO != 0 {
			panic("reflect: function created by MakeFunc using " + funcName(c.fn) +
				" returned value obtained from unexported field")
		}
	}

	if retEmpty {
		return
	}

	if retStruct {
		off := uintptr(0)
		for i, typ := range ftyp.out {
			v := out[i]
			off = align(off, uintptr(typ.fieldAlign))
			addr := unsafe.Pointer(uintptr(retPtr) + off)
			if v.flag&flagIndir == 0 && (v.kind() == Ptr || v.kind() == UnsafePointer) {
				storeIword(addr, iword(v.val), typ.size)
			} else {
				memmove(addr, v.val, typ.size)
			}
			off += typ.size
		}
		regs.eax = uint32(uintptr(retPtr))
		return
	}

	if len(ftyp.out) != 1 {
		panic("inconsistency")
	}

	v := out[0]
	w := v.iword()
	switch v.Kind() {
	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)))
	}
}