| 1 | package printer_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/mmcloughlin/avo/attr" |
| 7 | "github.com/mmcloughlin/avo/build" |
| 8 | "github.com/mmcloughlin/avo/buildtags" |
| 9 | "github.com/mmcloughlin/avo/printer" |
| 10 | "github.com/mmcloughlin/avo/reg" |
| 11 | ) |
| 12 | |
| 13 | func TestBasic(t *testing.T) { |
| 14 | ctx := build.NewContext() |
| 15 | ctx.Function("add") |
| 16 | ctx.SignatureExpr("func(x, y uint64) uint64") |
| 17 | x := ctx.Load(ctx.Param("x"), reg.RAX) |
| 18 | y := ctx.Load(ctx.Param("y"), reg.R9) |
| 19 | ctx.ADDQ(x, y) |
| 20 | ctx.Store(y, ctx.ReturnIndex(0)) |
| 21 | ctx.RET() |
| 22 | |
| 23 | AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{ |
| 24 | "// Code generated by avo. DO NOT EDIT.", |
| 25 | "", |
| 26 | "// func add(x uint64, y uint64) uint64", |
| 27 | "TEXT ·add(SB), $0-24", |
| 28 | "\tMOVQ x+0(FP), AX", |
| 29 | "\tMOVQ y+8(FP), R9", |
| 30 | "\tADDQ AX, R9", |
| 31 | "\tMOVQ R9, ret+16(FP)", |
| 32 | "\tRET", |
| 33 | "", |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | func TestTextDecl(t *testing.T) { |
| 38 | ctx := build.NewContext() |
| 39 | |
| 40 | ctx.Function("noargs") |
| 41 | ctx.SignatureExpr("func()") |
| 42 | ctx.AllocLocal(16) |
| 43 | ctx.RET() |
| 44 | |
| 45 | ctx.Function("withargs") |
| 46 | ctx.SignatureExpr("func(x, y uint64) uint64") |
| 47 | ctx.RET() |
| 48 | |
| 49 | ctx.Function("withattr") |
| 50 | ctx.SignatureExpr("func()") |
| 51 | ctx.Attributes(attr.NOSPLIT | attr.TLSBSS) |
| 52 | ctx.RET() |
| 53 | |
| 54 | AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{ |
| 55 | "// Code generated by avo. DO NOT EDIT.", |
| 56 | "", |
| 57 | "// func noargs()", |
| 58 | "TEXT ·noargs(SB), $16", // expect only the frame size |
| 59 | "\tRET", |
| 60 | "", |
| 61 | "// func withargs(x uint64, y uint64) uint64", |
| 62 | "TEXT ·withargs(SB), $0-24", // expect both frame size and argument size |
| 63 | "\tRET", |
| 64 | "", |
| 65 | "// func withattr()", |
| 66 | "TEXT ·withattr(SB), NOSPLIT|TLSBSS, $0", // expect to see attributes |
| 67 | "\tRET", |
| 68 | "", |
| 69 | }) |
| 70 | } |
| 71 | |
| 72 | func TestConstraints(t *testing.T) { |
| 73 | ctx := build.NewContext() |
| 74 | ctx.ConstraintExpr("linux,386 darwin,!cgo") |
| 75 | ctx.ConstraintExpr("!noasm") |
| 76 | |
| 77 | expect := []string{ |
| 78 | "// Code generated by avo. DO NOT EDIT.", |
| 79 | "", |
| 80 | } |
| 81 | if buildtags.GoBuildSyntaxSupported() { |
| 82 | expect = append(expect, |
| 83 | "//go:build ((linux && 386) || (darwin && !cgo)) && !noasm", |
| 84 | ) |
| 85 | } |
| 86 | if buildtags.PlusBuildSyntaxSupported() { |
| 87 | expect = append(expect, |
| 88 | "// +build linux,386 darwin,!cgo", |
| 89 | "// +build !noasm", |
| 90 | ) |
| 91 | } |
| 92 | expect = append(expect, "") |
| 93 | |
| 94 | AssertPrintsLines(t, ctx, printer.NewGoAsm, expect) |
| 95 | } |
| 96 | |
| 97 | func TestAlignmentNoOperands(t *testing.T) { |
| 98 | ctx := build.NewContext() |
| 99 | ctx.Function("alignment") |
| 100 | ctx.SignatureExpr("func()") |
| 101 | ctx.ADDQ(reg.RAX, reg.RBX) |
| 102 | ctx.VMOVDQU(reg.Y4, reg.Y11) |
| 103 | ctx.VZEROUPPER() |
| 104 | ctx.ADDQ(reg.R9, reg.R13) |
| 105 | ctx.RET() |
| 106 | |
| 107 | AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{ |
| 108 | "// Code generated by avo. DO NOT EDIT.", |
| 109 | "", |
| 110 | "// func alignment()", |
| 111 | "TEXT ·alignment(SB), $0", |
| 112 | "\tADDQ AX, BX", |
| 113 | "\tVMOVDQU Y4, Y11", |
| 114 | "\tVZEROUPPER", // instruction with no alignment doesn't affect width |
| 115 | "\tADDQ R9, R13", // retains alignment from above |
| 116 | "\tRET", |
| 117 | "", |
| 118 | }) |
| 119 | } |
| 120 | |
| 121 | func TestOpcodeSuffixes(t *testing.T) { |
| 122 | ctx := build.NewContext() |
| 123 | ctx.Function("suffixes") |
| 124 | ctx.SignatureExpr("func()") |
| 125 | ctx.VADDPD_RD_SAE_Z(reg.Z1, reg.Z2, reg.K1, reg.Z3) |
| 126 | ctx.ADDQ(reg.RAX, reg.RBX) |
| 127 | |
| 128 | AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{ |
| 129 | "// Code generated by avo. DO NOT EDIT.", |
| 130 | "", |
| 131 | "// func suffixes()", |
| 132 | "TEXT ·suffixes(SB), $0", |
| 133 | "\tVADDPD.RD_SAE.Z Z1, Z2, K1, Z3", |
| 134 | "\tADDQ AX, BX", // suffixes count towards alignment width |
| 135 | "", |
| 136 | }) |
| 137 | } |