-
Notifications
You must be signed in to change notification settings - Fork 0
/
asm.go
476 lines (406 loc) · 8.41 KB
/
asm.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"math"
"runtime"
"strconv"
"strings"
)
type operand interface {
Print() string
}
// ---------------------------------------------------------------------------------------------------------------------
type memOp struct {
base reg
idx reg
disp int16
scl int16
indir bool
deref bool
}
func (mo memOp) index(r reg) memOp {
mo.idx = r
return mo
}
func (mo memOp) scale(m int) memOp {
mo.scl = int16(m)
return mo
}
func (mo memOp) displace(m int) memOp {
mo.disp = int16(m)
return mo
}
func (mo memOp) indirect() memOp {
mo.indir = true
return mo
}
func (mo memOp) Print() string {
var buf bytes.Buffer
if mo.indir {
buf.WriteString("*")
}
if mo.disp != 0 {
buf.WriteString(strconv.Itoa(int(mo.disp)))
}
if mo.deref {
buf.WriteString("(")
}
if mo.base != 0 {
buf.WriteString(regNames[mo.base])
}
if mo.idx != 0 {
buf.WriteString(",")
buf.WriteString(regNames[mo.idx])
}
if mo.scl > 1 {
buf.WriteString(",")
buf.WriteString(strconv.Itoa(int(mo.scl)))
}
if mo.deref {
buf.WriteString(")")
}
return buf.String()
}
// ---------------------------------------------------------------------------------------------------------------------
type fnOp string
func (fo fnOp) Print() string {
if runtime.GOOS == "darwin" {
return "_" + string(fo) // OSX requires that all global symbols are prefixed with underscores
}
return string(fo)
}
// ---------------------------------------------------------------------------------------------------------------------
type symOp string
func (so symOp) Print() string {
if runtime.GOOS == "darwin" {
return fmt.Sprintf("$_%v", so) // OSX requires that all global symbols are prefixed with underscores
}
return "$" + string(so)
}
// ---------------------------------------------------------------------------------------------------------------------
type labelOp string
func (so labelOp) Print() string {
return string(so)
}
// ---------------------------------------------------------------------------------------------------------------------
type litOp string
func (lo litOp) Print() string {
return fmt.Sprintf("$%v", lo)
}
func intOp(i int) operand {
return litOp(strconv.Itoa(i))
}
func taggedIntOp(i int) operand {
return intOp((i << 1) + 1)
}
func strOp(s string) operand {
return litOp(s)
}
var _false = intOp(0)
var _true = intOp(1)
// ---------------------------------------------------------------------------------------------------------------------
type reg byte
func (r reg) Print() string {
return regNames[r]
}
// Fixed register op
const (
rax = reg(iota + 1)
rbx
rcx
rdx
rsi
rdi
rbp
rsp
r8
r9
r10
r11
r12
r13
r14
r15
// 8-bit
dil // rdi
sil // rsi
dl // rdx
cl // rcx
r8l // r8
r9l // r9
al // rax
bl // rbx
)
var regNames = map[reg]string{
// 64-bit
rax: "%rax",
rbx: "%rbx",
rcx: "%rcx",
rdx: "%rdx",
rsi: "%rsi",
rdi: "%rdi",
rbp: "%rbp",
rsp: "%rsp",
r8: "%r8",
r9: "%r9",
r10: "%r10",
r11: "%r11",
r12: "%r12",
r13: "%r13",
r14: "%r14",
r15: "%r15",
// 8-bit
dil: "%dil",
sil: "%sil",
dl: "%dl",
cl: "%cl",
r8l: "%r8l",
r9l: "%r9l",
al: "%al",
bl: "%bl",
}
func (r reg) index(off reg) memOp {
return r.deref().index(off)
}
func (r reg) indirect() memOp {
return memOp{ base: r, indir: true }
}
func (r reg) displace(i int) memOp {
if i > math.MaxInt16 || i < math.MinInt16 {
panic(fmt.Sprintf("Cannot represent displacement: %v", i))
}
return r.deref().displace(i)
}
func (r reg) deref() memOp {
return memOp{ base: r, deref: true }
}
func (r reg) _8bit() reg {
switch r {
case rax:
return al
case rbx:
return bl
case rdi:
return dil
case rsi:
return sil
case rdx:
return dl
case rcx:
return cl
case r8:
return r8l
case r9:
return r9l
default:
panic("No 8-bit version defined for: " + regNames[r])
}
}
// ---------------------------------------------------------------------------------------------------------------------
type inst byte
const (
movq = inst(iota + 1)
movb
movsbq
movabs
popq
pushq
// Pointer management
leaq
// Comparisons
notq
negq
orq
xorq
andq
cmpq
setg
setge
setl
setle
sete
shlq
sarq
// Arithmetic
addq
subq
imulq
idivq
cqo
// Looping instructions
jmp
jne
jae
je
// Function support
leave
enter
ret
call
)
var instNames = map[inst]string{
movq: "movq",
movb: "movb",
movsbq: "movsbq",
movabs: "movabs",
popq: "popq",
pushq: "pushq",
leaq: "leaq",
notq: "notq",
negq: "negq",
orq: "orq",
xorq: "xorq",
sarq: "sarq",
shlq: "shlq",
andq: "andq",
cmpq: "cmpq",
setg: "setg",
setge: "setge",
setl: "setl",
setle: "setle",
sete: "sete",
addq: "addq",
subq: "subq",
imulq: "imulq",
idivq: "idivq",
cqo: "cqo",
jmp: "jmp",
jne: "jne",
jae: "jae",
je: "je",
leave: "leave",
enter: "enter",
ret: "ret",
call: "call",
}
type asmWriter interface {
// General
tab(s... string) // TODO: This is a directive really...
spacer()
stringLit(s string) operand
label(s string)
newLabel(s string) string
raw(s string) // Remove me!
addr(op operand)
fnStart(name string)
fnEnd()
ins(i inst, ops ...operand)
flush()
roSymbol(name string, f func(w asmWriter)) operand
gcMap(name string, offsets []int) labelOp
taggedInt(i int)
}
// Writer for GNU AS format (https://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax)
type gasWriter struct {
w *bufio.Writer
debug bool
sIndex, lIndex int
literals map[string]string
}
func NewGasWriter(io io.Writer, debug bool) *gasWriter {
return &gasWriter { w : bufio.NewWriter(io), debug: debug, literals: make(map[string]string) }
}
func (gw *gasWriter) write(asm string, a...interface{}) {
_, err := gw.w.WriteString(fmt.Sprintf(asm, a...))
if err != nil {
panic(err)
}
gw.w.Flush()
}
func (gw *gasWriter) addr(op operand) {
switch op.(type) {
case symOp:
gw.tab(".8byte", op.Print()[1:]) // Trim '$'
case fnOp, labelOp:
gw.tab(".8byte", op.Print())
case litOp:
panic("Cannot output the address of literal")
}
}
func (gw *gasWriter) tab(s ... string) {
gw.write(fmt.Sprintf(" " + strings.Join(s, " ") + "\n"))
}
func (gw *gasWriter) spacer() {
gw.write(fmt.Sprintf("\n%s\n\n", strings.Repeat(";", 120)))
}
func (gw *gasWriter) fnStart(name string) {
gw.tab(".text")
gw.tab(".align", "8")
gw.taggedInt(readOnlyGcHeader(5)) // gc.go defines these values
if runtime.GOOS == "darwin" {
name = "_" + name
}
gw.tab(".globl", name)
if runtime.GOOS == "linux" {
gw.tab(".type", fmt.Sprintf("%v, @function", name))
}
gw.raw(fmt.Sprintf("%v:", name))
}
func (gw *gasWriter) stringLit(s string) operand {
const suffix = "+8" // Ensure the address points _after_ the header
if label, ok := gw.literals[s]; ok {
return litOp(label + suffix)
}
// Create new label
label := fmt.Sprintf(".LC%v", gw.sIndex)
gw.sIndex++
gw.literals[s] = label
return litOp(label + suffix)
}
func (gw *gasWriter) flush() {
gw.tab(".data")
for s, label := range gw.literals {
raw, err := strconv.Unquote(s)
if err != nil {
panic(err)
}
gw.tab(".align", "8")
gw.label(label)
gw.taggedInt(readOnlyGcHeader(4)) // gc.go defines fixed IDs
gw.taggedInt(len(raw))
gw.write(" .ascii \"%v\\0\"\n", s[1:len(s)-1])
}
gw.literals = make(map[string]string) // Clear values
}
func (gw *gasWriter) newLabel(s string) string {
label := fmt.Sprintf("%v_%v", s, gw.lIndex)
gw.lIndex++
return label
}
func (gw *gasWriter) label(s string) {
gw.write(fmt.Sprintf("%s:\n", s))
}
func (gw *gasWriter) raw(s string) {
gw.write(fmt.Sprintf("%v\n", s))
}
func (gw *gasWriter) ins(i inst, ops ...operand) {
s := make([]string, len(ops))
for i := 0; i < len(s); i++ {
s[i] = ops[i].Print()
}
gw.write(" %-8s%-50s\n", instNames[i], strings.Join(s, ", "))
}
func (gw *gasWriter) roSymbol(name string, f func(w asmWriter)) operand {
gw.taggedInt(2) // "Read-only" GC header, TODO: Set type ID here
gw.label(fnOp(name).Print()) // TODO: Fix this mess!
f(gw)
return symOp(name)
}
func (gw *gasWriter) gcMap(name string, offsets []int) labelOp {
gw.label(name)
gw.taggedInt(len(offsets))
var s []string
for _, off := range offsets {
s = append(s, strconv.Itoa((off<<1)+1)) // Ensure offsets are tagged!
}
gw.tab(".8byte", strings.Join(s, ","))
gw.spacer()
return labelOp(name)
}
func (gw *gasWriter) taggedInt(i int) {
gw.tab(".8byte", strconv.Itoa((i << 1) + 1))
}
func (gw *gasWriter) fnEnd() { /* ... */ }