-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathwincallback.go
More file actions
262 lines (216 loc) · 8.37 KB
/
wincallback.go
File metadata and controls
262 lines (216 loc) · 8.37 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2014 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.
//go:build ignore
// Generate Windows callback assembly file.
// This was copied from the Go repository and modified to generate non-Windows assembly files.
package main
import (
"bytes"
"fmt"
"os"
)
const maxCallback = 2000
func genasm386() {
var buf bytes.Buffer
buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
//go:build linux
// External code calls into callbackasm at an offset corresponding
// to the callback index. Callbackasm is a table of MOVL and JMP instructions.
// The MOVL instruction loads CX with the callback index, and the
// JMP instruction branches to callbackasm1.
// callbackasm1 takes the callback index from CX and
// indexes into an array that stores information about each callback.
// It then calls the Go implementation for that callback.
#include "textflag.h"
TEXT callbackasm(SB), NOSPLIT|NOFRAME, $0
`)
for i := 0; i < maxCallback; i++ {
fmt.Fprintf(&buf, "\tMOVL $%d, CX\n", i)
buf.WriteString("\tJMP callbackasm1(SB)\n")
}
if err := os.WriteFile("zcallback_386.s", buf.Bytes(), 0644); err != nil {
fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
os.Exit(2)
}
}
func genasmAmd64() {
var buf bytes.Buffer
buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
//go:build darwin || freebsd || linux || netbsd
// runtime·callbackasm is called by external code to
// execute Go implemented callback function. It is not
// called from the start, instead runtime·compilecallback
// always returns address into runtime·callbackasm offset
// appropriately so different callbacks start with different
// CALL instruction in runtime·callbackasm. This determines
// which Go callback function is executed later on.
#include "textflag.h"
TEXT callbackasm(SB), NOSPLIT|NOFRAME, $0
`)
for i := 0; i < maxCallback; i++ {
buf.WriteString("\tCALL callbackasm1(SB)\n")
}
if err := os.WriteFile("zcallback_amd64.s", buf.Bytes(), 0644); err != nil {
fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
os.Exit(2)
}
}
func genasmArm() {
var buf bytes.Buffer
buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
//go:build linux
// External code calls into callbackasm at an offset corresponding
// to the callback index. Callbackasm is a table of MOVW and B instructions.
// The MOVW instruction loads R12 with the callback index, and the
// B instruction branches to callbackasm1.
// callbackasm1 takes the callback index from R12 and
// indexes into an array that stores information about each callback.
// It then calls the Go implementation for that callback.
#include "textflag.h"
TEXT callbackasm(SB), NOSPLIT|NOFRAME, $0
`)
for i := 0; i < maxCallback; i++ {
fmt.Fprintf(&buf, "\tMOVW $%d, R12\n", i)
buf.WriteString("\tB callbackasm1(SB)\n")
}
if err := os.WriteFile("zcallback_arm.s", buf.Bytes(), 0644); err != nil {
fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
os.Exit(2)
}
}
func genasmArm64() {
var buf bytes.Buffer
buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
//go:build darwin || freebsd || linux || netbsd
// External code calls into callbackasm at an offset corresponding
// to the callback index. Callbackasm is a table of MOV and B instructions.
// The MOV instruction loads R12 with the callback index, and the
// B instruction branches to callbackasm1.
// callbackasm1 takes the callback index from R12 and
// indexes into an array that stores information about each callback.
// It then calls the Go implementation for that callback.
#include "textflag.h"
TEXT callbackasm(SB), NOSPLIT|NOFRAME, $0
`)
for i := 0; i < maxCallback; i++ {
fmt.Fprintf(&buf, "\tMOVD $%d, R12\n", i)
buf.WriteString("\tB callbackasm1(SB)\n")
}
if err := os.WriteFile("zcallback_arm64.s", buf.Bytes(), 0644); err != nil {
fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
os.Exit(2)
}
}
func genasmLoong64() {
var buf bytes.Buffer
buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
//go:build darwin || freebsd || linux || netbsd
// External code calls into callbackasm at an offset corresponding
// to the callback index. Callbackasm is a table of MOVV and JMP instructions.
// The MOVV instruction loads R12 with the callback index, and the
// JMP instruction branches to callbackasm1.
// callbackasm1 takes the callback index from R12 and
// indexes into an array that stores information about each callback.
// It then calls the Go implementation for that callback.
#include "textflag.h"
TEXT callbackasm(SB), NOSPLIT|NOFRAME, $0
`)
for i := 0; i < maxCallback; i++ {
fmt.Fprintf(&buf, "\tMOVV $%d, R12\n", i)
buf.WriteString("\tJMP callbackasm1(SB)\n")
}
if err := os.WriteFile("zcallback_loong64.s", buf.Bytes(), 0644); err != nil {
fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
os.Exit(2)
}
}
func genasmPpc64le() {
var buf bytes.Buffer
buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
//go:build linux
// External code calls into callbackasm at an offset corresponding
// to the callback index. Callbackasm is a table of MOVD and BR instructions.
// The MOVD instruction loads R11 with the callback index, and the
// BR instruction branches to callbackasm1.
// callbackasm1 takes the callback index from R11 and
// indexes into an array that stores information about each callback.
// It then calls the Go implementation for that callback.
#include "textflag.h"
TEXT callbackasm(SB), NOSPLIT|NOFRAME, $0
`)
for i := 0; i < maxCallback; i++ {
fmt.Fprintf(&buf, "\tMOVD $%d, R11\n", i)
buf.WriteString("\tBR callbackasm1(SB)\n")
}
if err := os.WriteFile("zcallback_ppc64le.s", buf.Bytes(), 0644); err != nil {
fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
os.Exit(2)
}
}
func genasmRiscv64() {
var buf bytes.Buffer
buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
//go:build darwin || freebsd || linux || netbsd
// External code calls into callbackasm at an offset corresponding
// to the callback index. Callbackasm is a table of MOV and JMP instructions.
// Since Go 1.26, MOV instructions with immediate values lower than or equal to 32
// are encoded in 2 bytes rather than 4 bytes, which breaks the assumption that each
// callback entry is 8 bytes long. Therefore, for callback indices less than or equal to 32,
// add a PCALIGN directive to align the next instruction to an 8-byte boundary.
// The MOV instruction loads X7 with the callback index, and the
// JMP instruction branches to callbackasm1.
// callbackasm1 takes the callback index from X7 and
// indexes into an array that stores information about each callback.
// It then calls the Go implementation for that callback.
#include "textflag.h"
TEXT callbackasm(SB), NOSPLIT|NOFRAME, $0
`)
for i := 0; i < maxCallback; i++ {
space := " "
if i <= 32 {
fmt.Fprintf(&buf, "\tPCALIGN $8\n")
space = " "
}
fmt.Fprintf(&buf, "\tMOV%s$%d, X7\n", space, i)
fmt.Fprintf(&buf, "\tJMP%scallbackasm1(SB)\n", space)
}
if err := os.WriteFile("zcallback_riscv64.s", buf.Bytes(), 0644); err != nil {
fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
os.Exit(2)
}
}
func genasmS390x() {
var buf bytes.Buffer
buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
//go:build linux
// External code calls into callbackasm at an offset corresponding
// to the callback index. Callbackasm is a table of MOVD and BR instructions.
// The MOVD instruction loads R0 with the callback index, and the
// BR instruction branches to callbackasm1.
// callbackasm1 takes the callback index from R0 and
// indexes into an array that stores information about each callback.
// It then calls the Go implementation for that callback.
// NOTE: We use R0 instead of R11 because R11 is callee-saved on S390X.
#include "textflag.h"
TEXT callbackasm(SB), NOSPLIT|NOFRAME, $0
`)
for i := 0; i < maxCallback; i++ {
fmt.Fprintf(&buf, "\tMOVD $%d, R0\n", i)
buf.WriteString("\tBR callbackasm1(SB)\n")
}
if err := os.WriteFile("zcallback_s390x.s", buf.Bytes(), 0644); err != nil {
fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
os.Exit(2)
}
}
func main() {
genasm386()
genasmAmd64()
genasmArm()
genasmArm64()
genasmLoong64()
genasmPpc64le()
genasmRiscv64()
genasmS390x()
}