1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3
4package purego
5
6import (
7 "math"
8 "reflect"
9 "runtime"
10 "sync"
11 "unsafe"
12)
13
14var syscall9XABI0 uintptr
15
16type syscall9Args struct {
17 fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr
18 f1, f2, f3, f4, f5, f6, f7, f8 float64
19 r1, r2, err uintptr
20}
21
22//go:nosplit
23func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, err uintptr) {
24 args := syscall9Args{fn, a1, a2, a3, a4, a5, a6, a7, a8, a9,
25 math.Float64frombits(uint64(a1)), math.Float64frombits(uint64(a2)), math.Float64frombits(uint64(a3)),
26 math.Float64frombits(uint64(a4)), math.Float64frombits(uint64(a5)), math.Float64frombits(uint64(a6)),
27 math.Float64frombits(uint64(a7)), math.Float64frombits(uint64(a8)),
28 r1, r2, err}
29 runtime_cgocall(syscall9XABI0, unsafe.Pointer(&args))
30 return args.r1, args.r2, args.err
31}
32
33// NewCallback converts a Go function to a function pointer conforming to the C calling convention.
34// This is useful when interoperating with C code requiring callbacks. The argument is expected to be a
35// function with zero or one uintptr-sized result. The function must not have arguments with size larger than the size
36// of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory allocated
37// for these callbacks is never released. At least 2000 callbacks can always be created. Although this function
38// provides similar functionality to windows.NewCallback it is distinct.
39func NewCallback(fn interface{}) uintptr {
40 return compileCallback(fn)
41}
42
43// maxCb is the maximum number of callbacks
44// only increase this if you have added more to the callbackasm function
45const maxCB = 2000
46
47var cbs struct {
48 lock sync.Mutex
49 numFn int // the number of functions currently in cbs.funcs
50 funcs [maxCB]reflect.Value // the saved callbacks
51}
52
53type callbackArgs struct {
54 index uintptr
55 // args points to the argument block.
56 //
57 // For cdecl and stdcall, all arguments are on the stack.
58 //
59 // For fastcall, the trampoline spills register arguments to
60 // the reserved spill slots below the stack arguments,
61 // resulting in a layout equivalent to stdcall.
62 //
63 // For arm, the trampoline stores the register arguments just
64 // below the stack arguments, so again we can treat it as one
65 // big stack arguments frame.
66 args unsafe.Pointer
67 // Below are out-args from callbackWrap
68 result uintptr
69}
70
71func compileCallback(fn interface{}) uintptr {
72 val := reflect.ValueOf(fn)
73 if val.Kind() != reflect.Func {
74 panic("purego: type is not a function")
75 }
76 ty := val.Type()
77 for i := 0; i < ty.NumIn(); i++ {
78 in := ty.In(i)
79 switch in.Kind() {
80 case reflect.Struct, reflect.Float32, reflect.Float64,
81 reflect.Interface, reflect.Func, reflect.Slice,
82 reflect.Chan, reflect.Complex64, reflect.Complex128,
83 reflect.String, reflect.Map, reflect.Invalid:
84 panic("purego: unsupported argument type: " + in.Kind().String())
85 }
86 }
87output:
88 switch {
89 case ty.NumOut() == 1:
90 switch ty.Out(0).Kind() {
91 case reflect.Pointer, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
92 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
93 reflect.Bool, reflect.UnsafePointer:
94 break output
95 }
96 panic("purego: unsupported return type: " + ty.String())
97 case ty.NumOut() > 1:
98 panic("purego: callbacks can only have one return")
99 }
100 cbs.lock.Lock()
101 defer cbs.lock.Unlock()
102 if cbs.numFn >= maxCB {
103 panic("purego: the maximum number of callbacks has been reached")
104 }
105 cbs.funcs[cbs.numFn] = val
106 cbs.numFn++
107 return callbackasmAddr(cbs.numFn - 1)
108}
109
110const ptrSize = unsafe.Sizeof((*int)(nil))
111
112const callbackMaxFrame = 64 * ptrSize
113
114// callbackasmABI0 is implemented in zcallback_GOOS_GOARCH.s
115var callbackasmABI0 uintptr
116
117// callbackWrap_call allows the calling of the ABIInternal wrapper
118// which is required for runtime.cgocallback without the
119// <ABIInternal> tag which is only allowed in the runtime.
120// This closure is used inside sys_darwin_GOARCH.s
121var callbackWrap_call = callbackWrap
122
123// callbackWrap is called by assembly code which determines which Go function to call.
124// This function takes the arguments and passes them to the Go function and returns the result.
125func callbackWrap(a *callbackArgs) {
126 cbs.lock.Lock()
127 fn := cbs.funcs[a.index]
128 cbs.lock.Unlock()
129 fnType := fn.Type()
130 args := make([]reflect.Value, fnType.NumIn())
131 frame := (*[callbackMaxFrame]uintptr)(a.args)
132 for i := range args {
133 //TODO: support float32 and float64
134 args[i] = reflect.NewAt(fnType.In(i), unsafe.Pointer(&frame[i])).Elem()
135 }
136 ret := fn.Call(args)
137 if len(ret) > 0 {
138 switch k := ret[0].Kind(); k {
139 case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uintptr:
140 a.result = uintptr(ret[0].Uint())
141 case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
142 a.result = uintptr(ret[0].Int())
143 case reflect.Bool:
144 if ret[0].Bool() {
145 a.result = 1
146 } else {
147 a.result = 0
148 }
149 case reflect.Pointer:
150 a.result = ret[0].Pointer()
151 case reflect.UnsafePointer:
152 a.result = ret[0].Pointer()
153 default:
154 panic("purego: unsupported kind: " + k.String())
155 }
156 }
157}
158
159// callbackasmAddr returns address of runtime.callbackasm
160// function adjusted by i.
161// On x86 and amd64, runtime.callbackasm is a series of CALL instructions,
162// and we want callback to arrive at
163// correspondent call instruction instead of start of
164// runtime.callbackasm.
165// On ARM, runtime.callbackasm is a series of mov and branch instructions.
166// R12 is loaded with the callback index. Each entry is two instructions,
167// hence 8 bytes.
168func callbackasmAddr(i int) uintptr {
169 var entrySize int
170 switch runtime.GOARCH {
171 default:
172 panic("purego: unsupported architecture")
173 case "386", "amd64":
174 entrySize = 5
175 case "arm", "arm64":
176 // On ARM and ARM64, each entry is a MOV instruction
177 // followed by a branch instruction
178 entrySize = 8
179 }
180 return callbackasmABI0 + uintptr(i*entrySize)
181}