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_len+8(FP), CX
XORQ DX, DX
loop:
CMPQ CX, $0x00
JE done
@@ -147,6 +148,7 @@ loop:
ADDQ $0x08, AX
DECQ CX
JMP loop
done:
MOVQ DX, ret+24(FP)
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 Y4, Y4, Y4
VXORPS Y5, Y5, Y5
blockloop:
CMPQ DX, $0x00000030
JL tail
@@ -32,8 +33,10 @@ blockloop:
ADDQ $0x000000c0, CX
SUBQ $0x00000030, DX
JMP blockloop
tail:
VXORPS X12, X12, X12
tailloop:
CMPQ DX, $0x00000000
JE reduce
@@ -43,6 +46,7 @@ tailloop:
ADDQ $0x00000004, CX
DECQ DX
JMP tailloop
reduce:
VADDPS Y0, Y1, Y0
VADDPS Y0, Y2, Y0

View File

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

View File

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

View File

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

View File

@@ -4,6 +4,7 @@ package prnt
import (
"bytes"
"fmt"
"io"
)
// Generator provides convenience methods for code generators. In particular it
@@ -15,6 +16,11 @@ type Generator struct {
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.
func (g *Generator) Printf(format string, args ...interface{}) {
if g.err != nil {

View File

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

View File

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