-
Notifications
You must be signed in to change notification settings - Fork 23
/
abi_amd64.go
198 lines (169 loc) · 5.01 KB
/
abi_amd64.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
/*
* Copyright 2022 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package abi
import (
`fmt`
`reflect`
`sort`
`strings`
`unsafe`
`github.com/chenzhuoyu/iasm/x86_64`
`github.com/cloudwego/frugal/internal/rt`
)
const (
PtrSize = 8 // pointer size
PtrAlign = 8 // pointer alignment
)
type Parameter struct {
Mem uintptr
Reg x86_64.Register64
Type reflect.Type
InRegister bool
}
var (
intType = reflect.TypeOf(0)
ptrType = reflect.TypeOf(unsafe.Pointer(nil))
)
func mkReg(vt reflect.Type, reg x86_64.Register64) (p Parameter) {
p.Reg = reg
p.Type = vt
p.InRegister = true
return
}
func mkStack(vt reflect.Type, mem uintptr) (p Parameter) {
p.Mem = mem
p.Type = vt
p.InRegister = false
return
}
func (self Parameter) String() string {
if self.InRegister {
return fmt.Sprintf("%%%s", self.Reg)
} else {
return fmt.Sprintf("%d(%%rsp)", self.Mem)
}
}
func (self Parameter) IsPointer() bool {
switch self.Type.Kind() {
case reflect.Bool : fallthrough
case reflect.Int : fallthrough
case reflect.Int8 : fallthrough
case reflect.Int16 : fallthrough
case reflect.Int32 : fallthrough
case reflect.Int64 : fallthrough
case reflect.Uint : fallthrough
case reflect.Uint8 : fallthrough
case reflect.Uint16 : fallthrough
case reflect.Uint32 : fallthrough
case reflect.Uint64 : fallthrough
case reflect.Uintptr : return false
case reflect.Chan : fallthrough
case reflect.Func : fallthrough
case reflect.Map : fallthrough
case reflect.Ptr : fallthrough
case reflect.UnsafePointer : return true
case reflect.Float32 : fallthrough
case reflect.Float64 : fallthrough
case reflect.Complex64 : fallthrough
case reflect.Complex128 : fallthrough
case reflect.Array : fallthrough
case reflect.Struct : panic("abi: unsupported types")
default : panic("abi: invalid value type")
}
}
type _StackSlot struct {
p bool
m uintptr
}
type FunctionLayout struct {
Id int
Sp uintptr
Args []Parameter
Rets []Parameter
}
func (self *FunctionLayout) String() string {
if self.Id < 0 {
return fmt.Sprintf("{func,%s}", self.formatFn())
} else {
return fmt.Sprintf("{meth/%d,%s}", self.Id, self.formatFn())
}
}
func (self *FunctionLayout) StackMap() *rt.StackMap {
var st []_StackSlot
var mb rt.StackMapBuilder
/* add arguments */
for _, v := range self.Args {
st = append(st, _StackSlot {
m: v.Mem,
p: v.IsPointer(),
})
}
/* add stack-passed return values */
for _, v := range self.Rets {
if !v.InRegister {
st = append(st, _StackSlot {
m: v.Mem,
p: v.IsPointer(),
})
}
}
/* sort by memory offset */
sort.Slice(st, func(i int, j int) bool {
return st[i].m < st[j].m
})
/* add the bits */
for _, v := range st {
mb.AddField(v.p)
}
/* build the stack map */
return mb.Build()
}
func (self *FunctionLayout) formatFn() string {
return fmt.Sprintf("$%#x,(%s),(%s)", self.Sp, self.formatSeq(self.Args), self.formatSeq(self.Rets))
}
func (self *FunctionLayout) formatSeq(v []Parameter) string {
nb := len(v)
mm := make([]string, len(v))
/* convert each part */
for i := 0; i < nb; i++ {
mm[i] = v[i].String()
}
/* join them together */
return strings.Join(mm, ",")
}
type AMD64ABI struct {
FnTab map[int]*FunctionLayout
}
func ArchCreateABI() *AMD64ABI {
return &AMD64ABI {
FnTab: make(map[int]*FunctionLayout),
}
}
func (self *AMD64ABI) RegisterMethod(id int, mt rt.Method) int {
self.FnTab[id] = self.LayoutFunc(mt.Id, mt.Vt.Pack().Method(mt.Id).Type)
return mt.Id
}
func (self *AMD64ABI) RegisterFunction(id int, fn interface{}) (fp unsafe.Pointer) {
vv := rt.UnpackEface(fn)
vt := vv.Type.Pack()
/* must be a function */
if vt.Kind() != reflect.Func {
panic("fn is not a function")
}
/* layout the function, and get the real function address */
self.FnTab[id] = self.LayoutFunc(-1, vt)
return *(*unsafe.Pointer)(vv.Value)
}