Files
avo/printer/goasm_test.go
Michael McLoughlin 602bb5197c build: unify Label function signatures
The Context.Label method and LABEL global function did not agree. Also
breaks the convention I'd like to set that capitalized functions must
agree with existing Go assembly syntax.

To help avoid a conflict with `avo.Label`, attributes were moved to
their own package.

Fixes #35
2019-01-05 18:18:49 -08:00

84 lines
1.8 KiB
Go

package printer_test
import (
"testing"
"github.com/mmcloughlin/avo/attr"
"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.",
"",
"// 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)",
"\tRET",
"",
})
}
func TestTextDecl(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()
ctx.Function("withattr")
ctx.SignatureExpr("func()")
ctx.Attributes(attr.NOSPLIT | attr.TLSBSS)
ctx.RET()
AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
"// Code generated by avo. DO NOT EDIT.",
"",
"// func noargs()",
"TEXT ·noargs(SB), $16", // expect only the frame size
"\tRET",
"",
"// func withargs(x uint64, y uint64) uint64",
"TEXT ·withargs(SB), $0-24", // expect both frame size and argument size
"\tRET",
"",
"// func withattr()",
"TEXT ·withattr(SB), NOSPLIT|TLSBSS, $0", // expect to see attributes
"\tRET",
"",
})
}
func TestConstraints(t *testing.T) {
ctx := build.NewContext()
ctx.ConstraintExpr("linux,386 darwin,!cgo")
ctx.ConstraintExpr("!noasm")
AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
"// Code generated by avo. DO NOT EDIT.",
"",
"// +build linux,386 darwin,!cgo",
"// +build !noasm",
"",
})
}