2018-12-27 13:48:31 -08:00
|
|
|
package printer_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-03-06 20:14:02 +00:00
|
|
|
"sources.truenas.cloud/code/avo/attr"
|
|
|
|
|
"sources.truenas.cloud/code/avo/build"
|
|
|
|
|
"sources.truenas.cloud/code/avo/buildtags"
|
|
|
|
|
"sources.truenas.cloud/code/avo/printer"
|
|
|
|
|
"sources.truenas.cloud/code/avo/reg"
|
2018-12-27 13:48:31 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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",
|
2018-12-30 23:35:49 -08:00
|
|
|
"TEXT ·add(SB), $0-24",
|
2019-01-13 11:59:14 -08:00
|
|
|
"\tMOVQ x+0(FP), AX",
|
2019-01-10 21:21:41 -08:00
|
|
|
"\tMOVQ y+8(FP), R9",
|
|
|
|
|
"\tADDQ AX, R9",
|
|
|
|
|
"\tMOVQ R9, ret+16(FP)",
|
2018-12-27 13:48:31 -08:00
|
|
|
"\tRET",
|
|
|
|
|
"",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-30 23:35:49 -08:00
|
|
|
func TestTextDecl(t *testing.T) {
|
2018-12-27 13:48:31 -08:00
|
|
|
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()
|
|
|
|
|
|
2018-12-30 23:35:49 -08:00
|
|
|
ctx.Function("withattr")
|
|
|
|
|
ctx.SignatureExpr("func()")
|
2019-01-05 18:18:49 -08:00
|
|
|
ctx.Attributes(attr.NOSPLIT | attr.TLSBSS)
|
2018-12-30 23:35:49 -08:00
|
|
|
ctx.RET()
|
|
|
|
|
|
2018-12-27 13:48:31 -08:00
|
|
|
AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
|
|
|
|
|
"// Code generated by avo. DO NOT EDIT.",
|
|
|
|
|
"",
|
|
|
|
|
"// func noargs()",
|
2018-12-30 23:35:49 -08:00
|
|
|
"TEXT ·noargs(SB), $16", // expect only the frame size
|
2018-12-27 13:48:31 -08:00
|
|
|
"\tRET",
|
|
|
|
|
"",
|
|
|
|
|
"// func withargs(x uint64, y uint64) uint64",
|
2018-12-30 23:35:49 -08:00
|
|
|
"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
|
2018-12-27 13:48:31 -08:00
|
|
|
"\tRET",
|
|
|
|
|
"",
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-01-02 21:35:17 -08:00
|
|
|
|
|
|
|
|
func TestConstraints(t *testing.T) {
|
|
|
|
|
ctx := build.NewContext()
|
2019-01-02 21:55:43 -08:00
|
|
|
ctx.ConstraintExpr("linux,386 darwin,!cgo")
|
|
|
|
|
ctx.ConstraintExpr("!noasm")
|
2019-01-02 21:35:17 -08:00
|
|
|
|
2021-10-29 01:08:02 -07:00
|
|
|
expect := []string{
|
2019-01-02 21:35:17 -08:00
|
|
|
"// Code generated by avo. DO NOT EDIT.",
|
|
|
|
|
"",
|
2021-10-29 01:08:02 -07:00
|
|
|
}
|
|
|
|
|
if buildtags.GoBuildSyntaxSupported() {
|
|
|
|
|
expect = append(expect,
|
|
|
|
|
"//go:build ((linux && 386) || (darwin && !cgo)) && !noasm",
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
if buildtags.PlusBuildSyntaxSupported() {
|
|
|
|
|
expect = append(expect,
|
|
|
|
|
"// +build linux,386 darwin,!cgo",
|
|
|
|
|
"// +build !noasm",
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
expect = append(expect, "")
|
|
|
|
|
|
|
|
|
|
AssertPrintsLines(t, ctx, printer.NewGoAsm, expect)
|
2019-01-02 21:35:17 -08:00
|
|
|
}
|
2020-01-27 21:05:33 -08:00
|
|
|
|
|
|
|
|
func TestAlignmentNoOperands(t *testing.T) {
|
|
|
|
|
ctx := build.NewContext()
|
|
|
|
|
ctx.Function("alignment")
|
|
|
|
|
ctx.SignatureExpr("func()")
|
|
|
|
|
ctx.ADDQ(reg.RAX, reg.RBX)
|
|
|
|
|
ctx.VMOVDQU(reg.Y4, reg.Y11)
|
|
|
|
|
ctx.VZEROUPPER()
|
|
|
|
|
ctx.ADDQ(reg.R9, reg.R13)
|
|
|
|
|
ctx.RET()
|
|
|
|
|
|
|
|
|
|
AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
|
|
|
|
|
"// Code generated by avo. DO NOT EDIT.",
|
|
|
|
|
"",
|
|
|
|
|
"// func alignment()",
|
|
|
|
|
"TEXT ·alignment(SB), $0",
|
|
|
|
|
"\tADDQ AX, BX",
|
|
|
|
|
"\tVMOVDQU Y4, Y11",
|
|
|
|
|
"\tVZEROUPPER", // instruction with no alignment doesn't affect width
|
|
|
|
|
"\tADDQ R9, R13", // retains alignment from above
|
|
|
|
|
"\tRET",
|
|
|
|
|
"",
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-11-12 18:35:36 -08:00
|
|
|
|
|
|
|
|
func TestOpcodeSuffixes(t *testing.T) {
|
|
|
|
|
ctx := build.NewContext()
|
|
|
|
|
ctx.Function("suffixes")
|
|
|
|
|
ctx.SignatureExpr("func()")
|
|
|
|
|
ctx.VADDPD_RD_SAE_Z(reg.Z1, reg.Z2, reg.K1, reg.Z3)
|
|
|
|
|
ctx.ADDQ(reg.RAX, reg.RBX)
|
|
|
|
|
|
|
|
|
|
AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
|
|
|
|
|
"// Code generated by avo. DO NOT EDIT.",
|
|
|
|
|
"",
|
|
|
|
|
"// func suffixes()",
|
|
|
|
|
"TEXT ·suffixes(SB), $0",
|
|
|
|
|
"\tVADDPD.RD_SAE.Z Z1, Z2, K1, Z3",
|
|
|
|
|
"\tADDQ AX, BX", // suffixes count towards alignment width
|
|
|
|
|
"",
|
|
|
|
|
})
|
|
|
|
|
}
|