-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathmsg_arm64.go
216 lines (195 loc) · 6.03 KB
/
msg_arm64.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2012-2022 The 'objc' Package Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package objc
/*
//#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -lobjc -framework Foundation
#define OBJC_OLD_DISPATCH_PROTOTYPES 1
// #cgo LDFLAGS: -lobjc -framework Foundation
#define __OBJC2__ 1
#include <objc/runtime.h>
#include <objc/message.h>
#include <stdlib.h>
#include <stdlib.h>
#include <dlfcn.h>
void *GoObjc_GetObjectSuperClassStruct(void *obj) {
struct objc_super *s = malloc(sizeof(struct objc_super));
s->receiver = obj;
s->super_class = class_getSuperclass(object_getClass(obj));
return s;
}
*/
import "C"
import (
"log"
"math"
"reflect"
"unsafe"
"github.com/progrium/macdriver/misc/variadic"
)
func unpackStruct(val reflect.Value, intArgs []uintptr, floatArgs []uintptr) (updatedIntArgs []uintptr, updatedFloatArgs []uintptr) {
for i := 0; i < val.NumField(); i++ {
v := val.Field(i)
kind := v.Kind()
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intArgs = append(intArgs, uintptr(v.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
intArgs = append(intArgs, uintptr(v.Uint()))
case reflect.Float32, reflect.Float64:
floatArgs = append(floatArgs, uintptr(math.Float64bits(v.Float())))
case reflect.Ptr:
intArgs = append(intArgs, val.Pointer())
case reflect.Struct:
ia, fa := unpackStruct(v, intArgs, floatArgs)
intArgs = ia
floatArgs = fa
default:
panic("unhandled case")
}
}
return intArgs, floatArgs
}
func sendMsg(obj Object, sendFunc variadic.Function, selector string, args ...interface{}) Object {
// Keep ObjC semantics: messages can be sent to nil objects,
// but the response is nil.
if obj.Pointer() == 0 {
return obj
}
sel := selectorWithName(selector)
if sel == nil {
return nil
}
intArgs := []uintptr{}
floatArgs := []uintptr{}
argOffset := 2 // self, op
typeInfo := simpleTypeInfoForMethod(obj, sel)
for i, arg := range args {
if args[i] == nil {
intArgs = append(intArgs, uintptr(0))
continue
}
switch t := arg.(type) {
case Object:
intArgs = append(intArgs, t.Pointer())
case Selector:
intArgs = append(intArgs, uintptr(selectorWithName(t.Selector())))
case uintptr:
intArgs = append(intArgs, t)
case int:
intArgs = append(intArgs, uintptr(t))
case uint:
intArgs = append(intArgs, uintptr(t))
case int8:
intArgs = append(intArgs, uintptr(t))
case uint8:
intArgs = append(intArgs, uintptr(t))
case int16:
intArgs = append(intArgs, uintptr(t))
case uint16:
intArgs = append(intArgs, uintptr(t))
case int32:
intArgs = append(intArgs, uintptr(t))
case uint32:
intArgs = append(intArgs, uintptr(t))
case int64:
intArgs = append(intArgs, uintptr(t))
case uint64:
intArgs = append(intArgs, uintptr(t))
case bool:
if t {
intArgs = append(intArgs, uintptr(1))
} else {
intArgs = append(intArgs, uintptr(0))
}
case float32:
floatArgs = append(floatArgs, uintptr(math.Float32bits(t)))
// Float64 is a bit of a special case. Since SendMsg is a variadic
// Go function, implicit floats will be of type float64, but we can't
// be sure that the receiver expects that; they might expect a float32
// instead.
//
// To remedy this, we query the selector's type encoding, and check
// whether it expects a 32-bit or 64-bit float.
case float64:
typeEnc := string(typeInfo[i+3])
switch typeEnc {
case encFloat:
floatArgs = append(floatArgs, uintptr(math.Float32bits(float32(t))))
case encDouble:
floatArgs = append(floatArgs, uintptr(math.Float64bits(t)))
default:
panic("objc: float argument mismatch")
}
default:
val := reflect.ValueOf(args[i])
switch val.Kind() {
case reflect.Ptr:
intArgs = append(intArgs, val.Pointer())
case reflect.Uintptr:
intArgs = append(intArgs, uintptr(val.Uint()))
case reflect.Struct:
updatedIntArgs, updatedFloatArgs := unpackStruct(val, intArgs, floatArgs)
intArgs = updatedIntArgs
floatArgs = updatedFloatArgs
// B.4 If the argument type is a Composite Type that is larger than
// 16 bytes, then the argument is copied to memory allocated by the
// caller and the argument is replaced by a pointer to the copy.
//if len(args) > 2 {
// hdr := (*reflect.SliceHeader)(unsafe.Pointer(&args))
// intArgs = append(intArgs, uintptr(hdr.Data))
//} else {
// intArgs = append(intArgs, args...)
//}
case reflect.Uint, reflect.Int, reflect.Int64, reflect.Uint64:
intArgs = append(intArgs, uintptr(val.Uint()))
default:
log.Panicf("unhandled kind: %s", val.Kind())
}
}
}
if sendFunc.IsStRet() {
panic("objc: stret is not yet supported on arm64")
}
fc := sendFunc.NewCall()
if sendFunc.IsSuper() {
superPtr := C.GoObjc_GetObjectSuperClassStruct(unsafe.Pointer(obj.Pointer()))
defer C.free(superPtr)
fc.Words[0] = uintptr(superPtr)
} else {
fc.Words[0] = obj.Pointer()
}
fc.Words[1] = uintptr(sel)
if len(intArgs) > 8 {
panic("too many int args")
}
if len(floatArgs) > 8 {
panic("too many float args")
}
for i, v := range intArgs {
fc.Words[argOffset+i] = v
}
for i, v := range floatArgs {
fc.Simd[i].Upper = v
}
if len(typeInfo) > 0 {
retEnc := string(typeInfo[0])
if retEnc == encFloat {
return object{ptr: uintptr(math.Float32bits(fc.CallFloat32()))}
} else if retEnc == encDouble {
return object{ptr: uintptr(math.Float64bits(fc.CallFloat64()))}
}
}
return object{ptr: fc.Call()}
}
func (obj object) Send(selector string, args ...interface{}) Object {
return sendMsg(obj, variadic.F_msgSend, selector, args...)
}
// func (obj object) SendMsgStret(ret uintptr, selector string, args ...interface{}) {
// sel := selectorWithName(selector)
// C.Debug_MsgSend_Stret(unsafe.Pointer(ret), unsafe.Pointer(obj.Pointer()), sel)
// }
func (obj object) SendSuper(selector string, args ...interface{}) Object {
return sendMsg(obj, variadic.F_msgSendSuper, selector, args...)
}