-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodegen.go
328 lines (306 loc) · 8.17 KB
/
codegen.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
package main
import (
"fmt"
"io"
"strconv"
"strings"
)
type codeWriter struct {
w io.Writer
goarch string
gosubarch string
arch Arch
}
func (w codeWriter) CodeGen(f *Function) error {
fmt.Fprintf(w.w, "// %s\n", f.ForwardDecl())
fmt.Fprintf(w.w, "TEXT ·%s(SB), 7, $0\n", f.Name)
err := f.Compile(w)
if err != nil {
return err
}
fmt.Fprintln(w.w, "")
return nil
}
func frameArg(name string, offset int) string {
return fmt.Sprintf("%s+%d(FP)", name, offset)
}
func (w codeWriter) preamble(f *Function) {
var mov, cmp, jne, jmp, call string
switch w.goarch {
case "amd64":
mov, cmp = "MOVQ", "CMPQ"
jne, jmp, call = "JNE", "JMP", "CALL"
case "arm":
mov, cmp = "MOVW", "TEQ"
jne, jmp, call = "BNE", "B", "B"
default:
panic("impossible")
}
ptrSize := w.arch.PtrSize
w.comment("Load pointers.")
outArg := f.Args[0]
inArgs := f.Args[1:]
if len(f.Args) > len(w.arch.InputRegs) {
panic("not enough registers")
}
for i, arg := range f.Args {
w.opcode(mov,
frameArg(arg, ptrSize*(3*i)),
w.arch.InputRegs[i])
}
// length and index.
w.comment("Check lengths.")
w.opcode(mov, frameArg(outArg, ptrSize), w.arch.LengthReg)
for i, arg := range inArgs {
if w.goarch == "arm" {
const spareReg = "R0"
w.opcode(mov, frameArg(arg, ptrSize*(3*i+3+1)), spareReg)
w.opcode(cmp, spareReg, w.arch.LengthReg)
} else {
w.opcode(cmp, w.arch.LengthReg,
frameArg(arg, ptrSize*(3*i+3+1)))
}
w.opcode(jne, f.Name+"__panic")
}
w.opcode(jmp, f.Name+"__ok")
//
w.label(f.Name, "panic")
w.opcode(call, "runtime·panicindex(SB)")
w.label(f.Name, "ok")
}
func (f *Function) Compile(w codeWriter) error {
outArg := f.Args[0]
ptrSize := w.arch.PtrSize
instrs, err := Compile(f, w)
if err != nil {
return err
}
for _, ins := range instrs {
if err := w.checkInstr(ins); err != nil {
return err
}
}
w.preamble(f)
stride := w.arch.VectorWidth / w.arch.Width(f.ScalarType)
// Emit code for the loop. It should look like:
// for i := 0; ; {
// if i > length-4 { i = length-4 }
// process(arrays[i:i+4])
// i += 4
// if i >= length { break }
// }
regc, regl := w.arch.CounterReg, w.arch.LengthReg
var add, sub, mov, cmp, jle, jge, jmp string
switch w.goarch {
case "amd64":
add, sub = "ADDQ", "SUBQ"
mov, cmp = "MOVQ", "CMPQ"
jle, jge, jmp = "JLE", "JGE", "JMP"
case "arm":
add, sub = "ADD", "RSB"
mov, cmp = "MOVW", "CMP" // will test LE or GE
jle, jge, jmp = "BLE", "BGE", "B"
default:
panic("impossible")
}
w.opcode(sub, fmt.Sprintf("$%d", stride), regl) // regl = length - 4
w.opcode(mov, "$0", regc)
w.label(f.Name, "loop")
w.opcode(cmp, regc, regl)
w.comment("if i > length-%d { i = length-%d }", stride, stride)
w.opcode(jle, f.Name+"__process")
w.opcode(mov, regl, regc)
w.label(f.Name, "process")
for _, ins := range instrs {
w.emitInstr(ins)
}
w.opcode(add, fmt.Sprintf("$%d", stride), regc)
w.comment("if i >= length { break }")
if w.goarch == "arm" {
w.opcode(mov, frameArg(outArg, ptrSize), "R0")
w.opcode(cmp, "R0", regc)
} else {
w.opcode(cmp, regc, frameArg(outArg, ptrSize))
}
w.opcode(jge, f.Name+"__return")
w.opcode(jmp, f.Name+"__loop")
w.label(f.Name, "return")
w.opcode("RET")
return nil
}
func (w codeWriter) checkInstr(ins Instr) error {
if w.arch.Width(ins.Var.Type) == 0 {
return fmt.Errorf("unsupported type %s on architecture", ins.Var.Type)
}
if ins.Kind == OP {
_, ok := w.arch.Opcode(ins.Op, ins.Var.Type)
if !ok {
return fmt.Errorf("no instruction for %s%s%s",
ins.Var.Type, ins.Op, ins.Var.Type)
}
}
return nil
}
func (w codeWriter) emitInstr(ins Instr) {
switch w.goarch {
case "amd64":
switch ins.Kind {
case LOAD:
width := w.arch.Width(ins.Var.Type)
loc := fmt.Sprintf("(%s)(%s*%d)", ins.Var.AddrReg,
w.arch.CounterReg, width)
if w.gosubarch == "avx2" {
w.opcode("VMOVDQU", loc, ins.Var.Location)
} else {
w.opcode("MOVUPS", loc, ins.Var.Location)
}
case STORE:
width := w.arch.Width(ins.Var.Type)
loc := fmt.Sprintf("(%s)(%s*%d)", ins.Var.AddrReg,
w.arch.CounterReg, width)
if w.gosubarch == "avx2" {
w.opcode("VMOVDQU", ins.Var.Location, loc)
} else {
w.opcode("MOVUPD", ins.Var.Location, loc)
}
case OP:
v := ins.Var
w.comment("%s = %s", v.Name, v.Expr())
opcode, ok := w.arch.Opcode(ins.Op, v.Type)
if !ok {
panic("unsupported operation")
}
if w.gosubarch == "avx2" {
// use ternary form
w.opcode(opcode, ins.Right.Location, ins.Left.Location, ins.Var.Location)
} else {
if ins.Var.Location == ins.Left.Location {
w.opcode(opcode, ins.Right.Location, ins.Left.Location)
} else {
w.opcode("MOVAPS", ins.Left.Location, ins.Var.Location)
w.opcode(opcode, ins.Right.Location, ins.Var.Location)
}
}
}
case "arm":
const spareReg = "R0"
switch ins.Kind {
case LOAD:
logwidth := w.arch.LogWidth(ins.Var.Type)
offset := fmt.Sprintf("%s<<%d", w.arch.CounterReg, logwidth)
w.opcode("ADD", offset, ins.Var.AddrReg, spareReg)
if strings.HasPrefix(ins.Var.Location, "Q") {
regd := regNr(ins.Var.Location) // encoded on bits 22, 15-12
regs := regNr(spareReg)
enc := assembleNEON("VLDM", false, regs, 4, 2*regd) // 4 = four words
fmt.Fprintf(w.w, "\tWORD\t$0x%08x\t// %s %s, %s\n",
enc, "VLDMIA", "("+spareReg+")", ins.Var.Location)
} else {
w.opcode("MOVD", "("+spareReg+")", ins.Var.Location)
}
case STORE:
logwidth := w.arch.LogWidth(ins.Var.Type)
offset := fmt.Sprintf("%s<<%d", w.arch.CounterReg, logwidth)
w.opcode("ADD", offset, ins.Var.AddrReg, spareReg)
if strings.HasPrefix(ins.Var.Location, "Q") {
regd := regNr(ins.Var.Location) // encoded on bits 22, 15-12
regs := regNr(spareReg)
enc := assembleNEON("VSTM", false, regs, 4, 2*regd) // 4 = four words
fmt.Fprintf(w.w, "\tWORD\t$0x%08x\t// %s %s, %s\n",
enc, "VSTMIA", "("+spareReg+")", ins.Var.Location)
} else {
w.opcode("MOVD", ins.Var.Location, "("+spareReg+")")
}
case OP:
v := ins.Var
w.comment("%s = %s", v.Name, v.Expr())
opcode, ok := w.arch.Opcode(ins.Op, v.Type)
if !ok {
panic("unsupported operation")
}
w.opcodeNEON(opcode, ins.Left.Location, ins.Right.Location, ins.Var.Location)
}
default:
panic("not implemented")
}
}
func (w codeWriter) comment(format string, args ...interface{}) {
fmt.Fprintf(w.w, "\n\t// "+format+"\n", args...)
}
func (w codeWriter) opcode(op string, args ...string) {
if len(args) > 0 {
fmt.Fprintf(w.w, "\t%s\t%s\n", op, strings.Join(args, ", "))
} else {
fmt.Fprintf(w.w, "\t%s\n", op)
}
}
func (w codeWriter) opcodeNEON(op string, reg1, reg2, regd string) {
if regd[0] == 'Q' {
// 128-bit neon
encoded := assembleNEON(op, true,
2*int(reg1[1]-'0'),
2*int(reg2[1]-'0'),
2*int(regd[1]-'0'))
fmt.Fprintf(w.w, "\tWORD\t$0x%08x\t// %s %s, %s, %s\n",
encoded, op, reg1, reg2, regd)
} else {
// 64-bit neon
encoded := assembleNEON(op, false,
int(reg1[1]-'0'),
int(reg2[1]-'0'),
int(regd[1]-'0'))
fmt.Fprintf(w.w, "\tWORD\t$0x%08x\t// %s %s, %s, %s\n",
encoded, op, reg1, reg2, regd)
}
}
func (w codeWriter) label(root, label string) {
fmt.Fprintln(w.w, "")
fmt.Fprintln(w.w, root+"__"+label+":")
}
func regNr(reg string) int {
n, err := strconv.Atoi(reg[1:])
if err != nil {
panic("invalid register name " + reg)
}
return n
}
func assembleNEON(op string, quad bool, reg1, reg2, regdest int) uint32 {
tpl := neonTemplates[op]
encoded := tpl |
uint32(reg1)<<16 |
uint32(reg2) |
uint32(regdest)<<12
if quad {
encoded |= 1 << 6
}
return encoded
}
// ARM Architecture Reference Manual, F5.4.1
var neonTemplates = map[string]uint32{
// Integer
"VADD.I8": 0xf2000800,
"VADD.I16": 0xf2100800,
"VADD.I32": 0xf2200800,
"VSUB.I8": 0xf3000800,
"VSUB.I16": 0xf3100800,
"VSUB.I32": 0xf3200800,
"VMUL.I8": 0xf2000910,
"VMUL.I16": 0xf2100910,
"VMUL.I32": 0xf2200910,
// Carryless
"VMULCL.I8": 0xf3000910,
"VMULCL.I16": 0xf3100910,
"VMULCL.I32": 0xf3200910,
// bitwise operations
"VAND": 0xf2000110,
"VORR": 0xf2200110,
"VORN": 0xf2300110, // or not
"VEOR": 0xf3000110, // xor
// Floating point
"VADD.F32": 0xf2000d00,
"VSUB.F32": 0xf2200d00,
"VMUL.F32": 0xf3000d10,
// Multiple load/store
"VLDM": 0xec900b00,
"VSTM": 0xec800b00,
}