printer: shorten text size when argsize is zero

This commit is contained in:
Michael McLoughlin
2018-12-27 13:48:31 -08:00
parent 9243d299e6
commit 5dea46407c
12 changed files with 158 additions and 35 deletions

View File

@@ -20,15 +20,14 @@ func main() {
DATA(38, U8(0x66)) DATA(38, U8(0x66))
DATA(39, U8(0x77)) DATA(39, U8(0x77))
// TEXT ·DataAt(SB),0,$0-9
TEXT("DataAt", "func(i int) byte") TEXT("DataAt", "func(i int) byte")
i := Load(Param("i"), GP64v()) // MOVQ i+0(FP), AX i := Load(Param("i"), GP64v())
ptr := Mem{Base: GP64v()} ptr := Mem{Base: GP64v()}
LEAQ(bytes, ptr.Base) // LEAQ b<>+0x00(SB), BX LEAQ(bytes, ptr.Base)
b := GP8v() b := GP8v()
MOVB(ptr.Idx(i, 1), b) // MOVB 0(BX)(AX*1), CL MOVB(ptr.Idx(i, 1), b)
Store(b, ReturnIndex(0)) // MOVB CL, ret+8(FP) Store(b, ReturnIndex(0))
RET() // RET RET()
Generate() Generate()
} }

View File

@@ -10,7 +10,7 @@ DATA bytes<>+32(SB)/4, $0x00112233
DATA bytes<>+36(SB)/2, $0x4455 DATA bytes<>+36(SB)/2, $0x4455
DATA bytes<>+38(SB)/1, $0x66 DATA bytes<>+38(SB)/1, $0x66
DATA bytes<>+39(SB)/1, $0x77 DATA bytes<>+39(SB)/1, $0x77
GLOBL ·bytes<>(SB), RODATA, $40 GLOBL bytes<>(SB), RODATA, $40
// func DataAt(i int) byte // func DataAt(i int) byte
TEXT ·DataAt(SB), 0, $0-9 TEXT ·DataAt(SB), 0, $0-9

View File

@@ -1,6 +1,7 @@
package printer package printer
import ( import (
"strconv"
"strings" "strings"
"github.com/mmcloughlin/avo" "github.com/mmcloughlin/avo"
@@ -48,7 +49,7 @@ func (p *goasm) include(path string) {
func (p *goasm) function(f *avo.Function) { func (p *goasm) function(f *avo.Function) {
p.NL() p.NL()
p.Comment(f.Stub()) p.Comment(f.Stub())
p.Printf("TEXT %s%s(SB),0,$%d-%d\n", dot, f.Name, f.FrameBytes(), f.ArgumentBytes()) p.Printf("TEXT %s%s(SB), 0, %s\n", dot, f.Name, textsize(f))
for _, node := range f.Nodes { for _, node := range f.Nodes {
switch n := node.(type) { switch n := node.(type) {
@@ -73,7 +74,24 @@ func (p *goasm) global(g *avo.Global) {
p.Printf("DATA %s/%d, %s\n", a.Asm(), d.Value.Bytes(), d.Value.Asm()) p.Printf("DATA %s/%d, %s\n", a.Asm(), d.Value.Bytes(), d.Value.Asm())
} }
// TODO(mbm): replace hardcoded RODATA with an attributes list // TODO(mbm): replace hardcoded RODATA with an attributes list
p.Printf("GLOBL %s%s(SB), RODATA, $%d\n", dot, g.Symbol, g.Size) p.Printf("GLOBL %s(SB), RODATA, $%d\n", g.Symbol, g.Size)
}
func textsize(f *avo.Function) string {
// Reference: https://github.com/golang/go/blob/b115207baf6c2decc3820ada4574ef4e5ad940ec/src/cmd/internal/obj/util.go#L260-L265
//
// case TYPE_TEXTSIZE:
// if a.Val.(int32) == objabi.ArgsSizeUnknown {
// str = fmt.Sprintf("$%d", a.Offset)
// } else {
// str = fmt.Sprintf("$%d-%d", a.Offset, a.Val.(int32))
// }
//
s := "$" + strconv.Itoa(f.FrameBytes())
if argsize := f.ArgumentBytes(); argsize > 0 {
return s + "-" + strconv.Itoa(argsize)
}
return s
} }
func joinOperands(operands []operand.Op) string { func joinOperands(operands []operand.Op) string {

63
printer/goasm_test.go Normal file
View File

@@ -0,0 +1,63 @@
package printer_test
import (
"testing"
"github.com/mmcloughlin/avo/build"
"github.com/mmcloughlin/avo/printer"
"github.com/mmcloughlin/avo/reg"
)
func TestBasic(t *testing.T) {
ctx := build.NewContext()
ctx.Function("add")
ctx.SignatureExpr("func(x, y uint64) uint64")
x := ctx.Load(ctx.Param("x"), reg.RAX)
y := ctx.Load(ctx.Param("y"), reg.R9)
ctx.ADDQ(x, y)
ctx.Store(y, ctx.ReturnIndex(0))
ctx.RET()
AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
"// Code generated by avo. DO NOT EDIT.",
"",
"#include \"textflag.h\"",
"",
"// func add(x uint64, y uint64) uint64",
"TEXT ·add(SB), 0, $0-24",
"\tMOVQ\tx(FP), AX",
"\tMOVQ\ty+8(FP), R9",
"\tADDQ\tAX, R9",
"\tMOVQ\tR9, ret+16(FP)",
"\tRET",
"",
})
}
func TestTextSize(t *testing.T) {
ctx := build.NewContext()
ctx.Function("noargs")
ctx.SignatureExpr("func()")
ctx.AllocLocal(16)
ctx.RET()
ctx.Function("withargs")
ctx.SignatureExpr("func(x, y uint64) uint64")
ctx.RET()
AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
"// Code generated by avo. DO NOT EDIT.",
"",
"#include \"textflag.h\"",
"",
"// func noargs()",
"TEXT ·noargs(SB), 0, $16", // expect only the frame size
"\tRET",
"",
"// func withargs(x uint64, y uint64) uint64",
"TEXT ·withargs(SB), 0, $0-24", // expect both frame size and argument size
"\tRET",
"",
})
}

43
printer/util_test.go Normal file
View File

@@ -0,0 +1,43 @@
package printer_test
import (
"strings"
"testing"
"github.com/mmcloughlin/avo/build"
"github.com/mmcloughlin/avo/printer"
)
func AssertPrintsLines(t *testing.T, ctx *build.Context, pb printer.Builder, expect []string) {
t.Helper()
output := Print(t, ctx, pb)
lines := strings.Split(output, "\n")
if len(expect) != len(lines) {
t.Fatalf("have %d lines of output; expected %d", len(lines), len(expect))
}
for i := range expect {
if expect[i] != lines[i] {
t.Errorf("mismatch on line %d:\n\tgot\t%s\n\texpect\t%s\n", i, lines[i], expect[i])
}
}
}
func Print(t *testing.T, ctx *build.Context, pb printer.Builder) string {
t.Helper()
f, errs := ctx.Result()
if errs != nil {
t.Fatal(errs)
}
p := pb(printer.NewDefaultConfig())
b, err := p.Print(f)
if err != nil {
t.Fatal(err)
}
return string(b)
}