-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
353 lines (335 loc) · 5.61 KB
/
types.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"fmt"
)
type GoType uint
const (
tUndefined GoType = iota
tU8
tU16
tU32
tU64
tF32
tF64
)
var types = map[string]GoType{
"uint8": tU8,
"uint32": tU32,
"float32": tF32,
"float64": tF64,
"byte": tU8,
}
type Op int
func (o Op) String() string { return opstring[o] }
const (
ADD Op = iota
SUB
MUL
DIV
AND
XOR
OR
SHL
SHR
)
var opstring = [...]string{
ADD: "+",
SUB: "-",
MUL: "*",
DIV: "/",
AND: "&",
OR: "|",
XOR: "^",
SHL: "<<",
SHR: ">>",
}
type Arch struct {
PtrSize int
UintType GoType // the type of unsized uint
CounterReg string
LengthReg string
InputRegs []string
VectorRegs []string
VectorWidth int
Types map[GoType]Type
}
func (a *Arch) Opcode(op Op, typename string) (opcode string, ok bool) {
typ := types[typename]
if typename == "uint" {
typ = a.UintType
}
if t, ok := a.Types[typ]; ok {
op, ok := t.Ops[op]
return op, ok
}
return "", false
}
func (a *Arch) LogWidth(typename string) int {
typ := types[typename]
if typename == "uint" {
typ = a.UintType
}
return a.Types[typ].LogSize
}
func (a *Arch) Width(typename string) int {
typ := types[typename]
if typename == "uint" {
typ = a.UintType
}
return a.Types[typ].Size
}
type Type struct {
Size int
LogSize int // Size = 1 << LogSize
Ops map[Op]string
}
func FindArch(goarch, gosubarch string) Arch {
switch goarch {
case "amd64":
if gosubarch == "avx2" {
return avx2
}
return amd64
case "arm":
return armv7
default:
err := fmt.Errorf("unsupported GOARCH=%q", goarch)
panic(err)
}
}
// Description of the amd64 architecture output.
var amd64 = Arch{
PtrSize: 8,
UintType: tU64,
CounterReg: "CX",
LengthReg: "DX",
InputRegs: []string{"BX", "SI", "DI",
"R8", "R9", "R10", "R11",
"R12", "R13", "R14", "R15"},
VectorRegs: []string{
"X0", "X1", "X2", "X4", "X5", "X6", "X7",
"X8", "X9", "X10", "X11", "X12", "X13", "X14", "X15"},
VectorWidth: 16,
Types: map[GoType]Type{
tU8: Type{
Size: 1,
LogSize: 0,
Ops: map[Op]string{
ADD: "PADDB",
SUB: "PSUBB",
AND: "PAND",
OR: "POR",
XOR: "PXOR",
},
},
tU16: Type{
Size: 2,
LogSize: 1,
Ops: map[Op]string{
ADD: "PADDW",
MUL: "PMULLW",
SUB: "PSUBW",
AND: "PAND",
OR: "POR",
XOR: "PXOR",
SHL: "PSLLW",
SHR: "PSRLW",
},
},
tU32: Type{
Size: 4,
LogSize: 2,
Ops: map[Op]string{
ADD: "PADDL",
SUB: "PSUBL",
AND: "PAND",
OR: "POR",
XOR: "PXOR",
SHL: "PSLLL",
SHR: "PSRLL",
},
},
tU64: Type{
Size: 8,
LogSize: 3,
Ops: map[Op]string{
ADD: "PADDQ",
SUB: "PSUBQ",
AND: "PAND",
OR: "POR",
XOR: "PXOR",
SHL: "PSLLQ",
SHR: "PSRLQ",
},
},
tF32: Type{
Size: 4,
LogSize: 2,
Ops: map[Op]string{
ADD: "ADDPS",
MUL: "MULPS",
SUB: "SUBPS",
DIV: "DIVPS",
},
},
tF64: Type{
Size: 8,
LogSize: 3,
Ops: map[Op]string{
ADD: "ADDPD",
MUL: "MULPD",
SUB: "SUBPD",
DIV: "DIVPD",
},
},
},
}
var avx2 = Arch{
PtrSize: 8,
UintType: tU64,
CounterReg: "CX",
LengthReg: "DX",
InputRegs: []string{"BX", "SI", "DI",
"R8", "R9", "R10", "R11",
"R12", "R13", "R14", "R15"},
VectorRegs: []string{
"Y0", "Y1", "Y2", "Y4", "Y5", "Y6", "Y7",
"Y8", "Y9", "Y10", "Y11", "Y12", "Y13", "Y14", "Y15"},
VectorWidth: 32, // 256-bit AVX registers
Types: map[GoType]Type{
tU8: Type{
Size: 1,
LogSize: 0,
Ops: map[Op]string{
ADD: "VPADDB",
SUB: "VPSUBB",
AND: "VPAND",
OR: "VPOR",
XOR: "VPXOR",
},
},
tU16: Type{
Size: 2,
LogSize: 1,
Ops: map[Op]string{
ADD: "VPADDW",
MUL: "VPMULLW",
SUB: "VPSUBW",
AND: "VPAND",
OR: "VPOR",
XOR: "VPXOR",
SHL: "VPSLLW",
SHR: "VPSRLW",
},
},
tU32: Type{
Size: 4,
LogSize: 2,
Ops: map[Op]string{
ADD: "VPADDL",
SUB: "VPSUBL",
AND: "VPAND",
OR: "VPOR",
XOR: "VPXOR",
SHL: "VPSLLL",
SHR: "VPSRLL",
},
},
tU64: Type{
Size: 8,
LogSize: 3,
Ops: map[Op]string{
ADD: "VPADDQ",
SUB: "VPSUBQ",
AND: "VPAND",
OR: "VPOR",
XOR: "VPXOR",
SHL: "VPSLLQ",
SHR: "VPSRLQ",
},
},
tF32: Type{
Size: 4,
LogSize: 2,
Ops: map[Op]string{
ADD: "VADDPS",
MUL: "VMULPS",
SUB: "VSUBPS",
DIV: "VDIVPS",
},
},
tF64: Type{
Size: 8,
LogSize: 3,
Ops: map[Op]string{
ADD: "VADDPD",
MUL: "VMULPD",
SUB: "VSUBPD",
DIV: "VDIVPD",
},
},
},
}
// Description of the ARM NEON SIMD instructions
// Looks good on Cortex-A9 and Cortex-A53.
var armv7 = Arch{
PtrSize: 4,
UintType: tU32,
// R10 is g, R13 is sp, R14 is lr, R15 is pc.
CounterReg: "R11",
LengthReg: "R12",
InputRegs: []string{
"R1", "R2", "R3", "R4",
"R5", "R6", "R7", "R8"},
VectorRegs: []string{
"Q0", "Q1", "Q2", "Q3", "Q4", "Q5", "Q6", "Q7"},
VectorWidth: 16, // 128-bit registers
Types: neonTypes,
}
var neonTypes = map[GoType]Type{
tU8: Type{
Size: 1,
LogSize: 0,
Ops: map[Op]string{
ADD: "VADD.I8",
SUB: "VSUB.I8",
MUL: "VMUL.I8",
AND: "VAND",
OR: "VORR",
XOR: "VEOR",
},
},
tU16: Type{
Size: 2,
LogSize: 1,
Ops: map[Op]string{
ADD: "VADD.I16",
SUB: "VSUB.I16",
MUL: "VMUL.I16",
AND: "VAND",
OR: "VORR",
XOR: "VEOR",
},
},
tU32: Type{
Size: 4,
LogSize: 2,
Ops: map[Op]string{
ADD: "VADD.I32",
SUB: "VSUB.I32",
MUL: "VMUL.I32",
AND: "VAND",
OR: "VORR",
XOR: "VEOR",
},
},
tF32: Type{
Size: 4,
LogSize: 2,
Ops: map[Op]string{
ADD: "VADD.F32",
MUL: "VMUL.F32",
SUB: "VSUB.F32",
},
},
}