printer: use tabwriter to align instructions (#8)

This commit is contained in:
Michael McLoughlin
2019-01-10 21:21:41 -08:00
parent 0e253b3753
commit f77a2e3b9e
20 changed files with 1876 additions and 1821 deletions

View File

@@ -140,6 +140,7 @@ TEXT ·Sum(SB), NOSPLIT, $0-32
MOVQ xs_base(FP), AX MOVQ xs_base(FP), AX
MOVQ xs_len+8(FP), CX MOVQ xs_len+8(FP), CX
XORQ DX, DX XORQ DX, DX
loop: loop:
CMPQ CX, $0x00 CMPQ CX, $0x00
JE done JE done
@@ -147,6 +148,7 @@ loop:
ADDQ $0x08, AX ADDQ $0x08, AX
DECQ CX DECQ CX
JMP loop JMP loop
done: done:
MOVQ DX, ret+24(FP) MOVQ DX, ret+24(FP)
RET RET

BIN
examples/backup.tar.gz Normal file

Binary file not shown.

View File

@@ -13,6 +13,7 @@ TEXT ·Dot(SB), NOSPLIT, $0-52
VXORPS Y3, Y3, Y3 VXORPS Y3, Y3, Y3
VXORPS Y4, Y4, Y4 VXORPS Y4, Y4, Y4
VXORPS Y5, Y5, Y5 VXORPS Y5, Y5, Y5
blockloop: blockloop:
CMPQ DX, $0x00000030 CMPQ DX, $0x00000030
JL tail JL tail
@@ -32,8 +33,10 @@ blockloop:
ADDQ $0x000000c0, CX ADDQ $0x000000c0, CX
SUBQ $0x00000030, DX SUBQ $0x00000030, DX
JMP blockloop JMP blockloop
tail: tail:
VXORPS X12, X12, X12 VXORPS X12, X12, X12
tailloop: tailloop:
CMPQ DX, $0x00000000 CMPQ DX, $0x00000000
JE reduce JE reduce
@@ -43,6 +46,7 @@ tailloop:
ADDQ $0x00000004, CX ADDQ $0x00000004, CX
DECQ DX DECQ DX
JMP tailloop JMP tailloop
reduce: reduce:
VADDPS Y0, Y1, Y0 VADDPS Y0, Y1, Y0
VADDPS Y0, Y2, Y0 VADDPS Y0, Y2, Y0

View File

@@ -8,6 +8,7 @@ TEXT ·Hash64(SB), NOSPLIT, $0-32
MOVQ data_len+8(FP), BX MOVQ data_len+8(FP), BX
MOVQ $0xcbf29ce484222325, AX MOVQ $0xcbf29ce484222325, AX
MOVQ $0x00000100000001b3, BP MOVQ $0x00000100000001b3, BP
loop: loop:
CMPQ BX, $0x00 CMPQ BX, $0x00
JE done JE done
@@ -17,6 +18,7 @@ loop:
INCQ CX INCQ CX
DECQ BX DECQ BX
JMP loop JMP loop
done: done:
MOVQ AX, ret+24(FP) MOVQ AX, ret+24(FP)
RET RET

View File

@@ -31,6 +31,7 @@ TEXT ·Hash(SB), NOSPLIT, $0-40
JE shortCore2 JE shortCore2
CMPQ SI, $0x00000003 CMPQ SI, $0x00000003
JE shortCore3 JE shortCore3
shortCore3: shortCore3:
MOVQ (CX), SI MOVQ (CX), SI
MOVQ $0x9c1b8e1e9628323f, DI MOVQ $0x9c1b8e1e9628323f, DI
@@ -42,6 +43,7 @@ shortCore3:
ADDQ BX, BP ADDQ BX, BP
ADDQ $0x00000008, CX ADDQ $0x00000008, CX
SUBQ $0x00000008, DX SUBQ $0x00000008, DX
shortCore2: shortCore2:
MOVQ (CX), SI MOVQ (CX), SI
MOVQ $0x9c1b8e1e9628323f, DI MOVQ $0x9c1b8e1e9628323f, DI
@@ -53,6 +55,7 @@ shortCore2:
ADDQ BX, BP ADDQ BX, BP
ADDQ $0x00000008, CX ADDQ $0x00000008, CX
SUBQ $0x00000008, DX SUBQ $0x00000008, DX
shortCore1: shortCore1:
MOVQ (CX), SI MOVQ (CX), SI
MOVQ $0x9c1b8e1e9628323f, DI MOVQ $0x9c1b8e1e9628323f, DI
@@ -64,6 +67,7 @@ shortCore1:
ADDQ BX, BP ADDQ BX, BP
ADDQ $0x00000008, CX ADDQ $0x00000008, CX
SUBQ $0x00000008, DX SUBQ $0x00000008, DX
shortCore0: shortCore0:
CMPQ DX, $0x00000000 CMPQ DX, $0x00000000
JE shortTail0 JE shortTail0
@@ -81,36 +85,45 @@ shortCore0:
JE shortTail6 JE shortTail6
CMPQ DX, $0x00000007 CMPQ DX, $0x00000007
JE shortTail7 JE shortTail7
shortTail7: shortTail7:
MOVBQZX 6(CX), SI MOVBQZX 6(CX), SI
SHLQ $0x20, SI SHLQ $0x20, SI
ADDQ SI, BX ADDQ SI, BX
shortTail6: shortTail6:
MOVBQZX 5(CX), SI MOVBQZX 5(CX), SI
SHLQ $0x30, SI SHLQ $0x30, SI
ADDQ SI, BP ADDQ SI, BP
shortTail5: shortTail5:
MOVBQZX 4(CX), SI MOVBQZX 4(CX), SI
SHLQ $0x10, SI SHLQ $0x10, SI
ADDQ SI, BX ADDQ SI, BX
shortTail4: shortTail4:
MOVLQZX (CX), SI MOVLQZX (CX), SI
ADDQ SI, BP ADDQ SI, BP
JMP shortAfter JMP shortAfter
shortTail3: shortTail3:
MOVBQZX 2(CX), SI MOVBQZX 2(CX), SI
SHLQ $0x30, SI SHLQ $0x30, SI
ADDQ SI, BX ADDQ SI, BX
shortTail2: shortTail2:
MOVWQZX (CX), SI MOVWQZX (CX), SI
ADDQ SI, BP ADDQ SI, BP
JMP shortAfter JMP shortAfter
shortTail1: shortTail1:
MOVBQZX (CX), SI MOVBQZX (CX), SI
ADDQ SI, BX ADDQ SI, BX
shortTail0: shortTail0:
RORQ $0x20, BP RORQ $0x20, BP
XORQ $0x000000ff, BP XORQ $0x000000ff, BP
shortAfter: shortAfter:
XORQ BX, BP XORQ BX, BP
RORQ $0x21, BX RORQ $0x21, BX
@@ -138,6 +151,7 @@ shortAfter:
XORQ BP, BX XORQ BP, BX
MOVQ BX, ret+32(FP) MOVQ BX, ret+32(FP)
RET RET
coreLong: coreLong:
MOVQ 16(AX), DI MOVQ 16(AX), DI
MOVQ 24(AX), AX MOVQ 24(AX), AX
@@ -151,6 +165,7 @@ coreLong:
MOVQ $0x9c1b8e1e9628323f, R8 MOVQ $0x9c1b8e1e9628323f, R8
IMULQ R8, SI IMULQ R8, SI
XORQ SI, AX XORQ SI, AX
block: block:
MOVQ (CX), SI MOVQ (CX), SI
MOVQ $0x00000000802910e3, R8 MOVQ $0x00000000802910e3, R8
@@ -191,6 +206,7 @@ block:
JE longCore2 JE longCore2
CMPQ SI, $0x00000003 CMPQ SI, $0x00000003
JE longCore3 JE longCore3
longCore3: longCore3:
MOVQ (CX), SI MOVQ (CX), SI
MOVQ $0x00000000802910e3, R9 MOVQ $0x00000000802910e3, R9
@@ -200,6 +216,7 @@ longCore3:
XORQ AX, BX XORQ AX, BX
ADDQ $0x00000008, CX ADDQ $0x00000008, CX
SUBQ $0x00000008, DX SUBQ $0x00000008, DX
longCore2: longCore2:
MOVQ (CX), SI MOVQ (CX), SI
MOVQ $0x00000000819b13af, R9 MOVQ $0x00000000819b13af, R9
@@ -209,6 +226,7 @@ longCore2:
XORQ DI, BP XORQ DI, BP
ADDQ $0x00000008, CX ADDQ $0x00000008, CX
SUBQ $0x00000008, DX SUBQ $0x00000008, DX
longCore1: longCore1:
MOVQ (CX), SI MOVQ (CX), SI
MOVQ $0x0000000091cb27e5, R9 MOVQ $0x0000000091cb27e5, R9
@@ -218,6 +236,7 @@ longCore1:
ADDQ BX, DI ADDQ BX, DI
ADDQ $0x00000008, CX ADDQ $0x00000008, CX
SUBQ $0x00000008, DX SUBQ $0x00000008, DX
longCore0: longCore0:
RORQ $0x0b, AX RORQ $0x0b, AX
SUBQ BP, AX SUBQ BP, AX
@@ -241,35 +260,44 @@ longCore0:
JE longTail6 JE longTail6
CMPQ DX, $0x00000007 CMPQ DX, $0x00000007
JE longTail7 JE longTail7
longTail7: longTail7:
MOVBQZX 6(CX), SI MOVBQZX 6(CX), SI
ADDQ SI, BP ADDQ SI, BP
longTail6: longTail6:
MOVWQZX 4(CX), SI MOVWQZX 4(CX), SI
ADDQ SI, DI ADDQ SI, DI
MOVLQZX (CX), SI MOVLQZX (CX), SI
ADDQ SI, AX ADDQ SI, AX
JMP longAfter JMP longAfter
longTail5: longTail5:
MOVBQZX 4(CX), SI MOVBQZX 4(CX), SI
ADDQ SI, BP ADDQ SI, BP
longTail4: longTail4:
MOVLQZX (CX), SI MOVLQZX (CX), SI
ADDQ SI, DI ADDQ SI, DI
JMP longAfter JMP longAfter
longTail3: longTail3:
MOVBQZX 2(CX), SI MOVBQZX 2(CX), SI
ADDQ SI, AX ADDQ SI, AX
longTail2: longTail2:
MOVWQZX (CX), SI MOVWQZX (CX), SI
ADDQ SI, BP ADDQ SI, BP
JMP longAfter JMP longAfter
longTail1: longTail1:
MOVBQZX (CX), SI MOVBQZX (CX), SI
ADDQ SI, DI ADDQ SI, DI
longTail0: longTail0:
ROLQ $0x20, AX ROLQ $0x20, AX
XORQ $0x000000ff, AX XORQ $0x000000ff, AX
longAfter: longAfter:
SUBQ DI, BP SUBQ DI, BP
RORQ $0x13, BX RORQ $0x13, BX

View File

@@ -7,6 +7,7 @@ TEXT ·Sum(SB), NOSPLIT, $0-32
MOVQ xs_base(FP), AX MOVQ xs_base(FP), AX
MOVQ xs_len+8(FP), CX MOVQ xs_len+8(FP), CX
XORQ DX, DX XORQ DX, DX
loop: loop:
CMPQ CX, $0x00 CMPQ CX, $0x00
JE done JE done
@@ -14,6 +15,7 @@ loop:
ADDQ $0x08, AX ADDQ $0x08, AX
DECQ CX DECQ CX
JMP loop JMP loop
done: done:
MOVQ DX, ret+24(FP) MOVQ DX, ret+24(FP)
RET RET

View File

@@ -4,6 +4,7 @@ package prnt
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io"
) )
// Generator provides convenience methods for code generators. In particular it // Generator provides convenience methods for code generators. In particular it
@@ -15,6 +16,11 @@ type Generator struct {
err error err error
} }
// Raw provides direct access to the underlying output stream.
func (g *Generator) Raw() io.Writer {
return &g.buf
}
// Printf prints to the internal buffer. // Printf prints to the internal buffer.
func (g *Generator) Printf(format string, args ...interface{}) { func (g *Generator) Printf(format string, args ...interface{}) {
if g.err != nil { if g.err != nil {

View File

@@ -1,8 +1,10 @@
package printer package printer
import ( import (
"fmt"
"strconv" "strconv"
"strings" "strings"
"text/tabwriter"
"github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/internal/prnt"
"github.com/mmcloughlin/avo/ir" "github.com/mmcloughlin/avo/ir"
@@ -81,20 +83,29 @@ func (p *goasm) function(f *ir.Function) {
} }
p.Printf(", %s\n", textsize(f)) p.Printf(", %s\n", textsize(f))
w := p.tabwriter()
for _, node := range f.Nodes { for _, node := range f.Nodes {
switch n := node.(type) { switch n := node.(type) {
case *ir.Instruction: case *ir.Instruction:
leader := []byte{tabwriter.Escape, '\t', tabwriter.Escape}
fmt.Fprint(w, string(leader)+n.Opcode)
if len(n.Operands) > 0 { if len(n.Operands) > 0 {
p.Printf("\t%s\t%s\n", n.Opcode, joinOperands(n.Operands)) fmt.Fprintf(w, "\t%s", joinOperands(n.Operands))
} else {
p.Printf("\t%s\n", n.Opcode)
} }
fmt.Fprint(w, "\n")
case ir.Label: case ir.Label:
p.Printf("%s:\n", n) w.Flush()
w = p.tabwriter()
p.Printf("\n%s:\n", n)
default: default:
panic("unexpected node type") panic("unexpected node type")
} }
} }
w.Flush()
}
func (p *goasm) tabwriter() *tabwriter.Writer {
return tabwriter.NewWriter(p.Raw(), 4, 4, 1, ' ', tabwriter.StripEscape)
} }
func (p *goasm) global(g *ir.Global) { func (p *goasm) global(g *ir.Global) {

View File

@@ -24,10 +24,10 @@ func TestBasic(t *testing.T) {
"", "",
"// func add(x uint64, y uint64) uint64", "// func add(x uint64, y uint64) uint64",
"TEXT ·add(SB), $0-24", "TEXT ·add(SB), $0-24",
"\tMOVQ\tx(FP), AX", "\tMOVQ x(FP), AX",
"\tMOVQ\ty+8(FP), R9", "\tMOVQ y+8(FP), R9",
"\tADDQ\tAX, R9", "\tADDQ AX, R9",
"\tMOVQ\tR9, ret+16(FP)", "\tMOVQ R9, ret+16(FP)",
"\tRET", "\tRET",
"", "",
}) })