File: goasm_test.go

package info (click to toggle)
golang-github-mmcloughlin-avo 0.0~git20200523.4439b6b-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,304 kB
  • sloc: xml: 71,029; asm: 13,138; sh: 179; makefile: 35
file content (107 lines) | stat: -rw-r--r-- 2,478 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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 x+0(FP), AX",
		"\tMOVQ y+8(FP), R9",
		"\tADDQ AX, R9",
		"\tMOVQ R9, 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",
		"",
	})
}

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",
		"",
	})
}