x86: rel types and generated tests
This commit is contained in:
@@ -16,7 +16,8 @@ var generators = map[string]gen.Builder{
|
|||||||
"asmtest": gen.NewAsmTest,
|
"asmtest": gen.NewAsmTest,
|
||||||
"godata": gen.NewGoData,
|
"godata": gen.NewGoData,
|
||||||
"godatatest": gen.NewGoDataTest,
|
"godatatest": gen.NewGoDataTest,
|
||||||
"constructors": gen.NewConstructors,
|
"ctors": gen.NewCtors,
|
||||||
|
"ctorstest": gen.NewCtorsTest,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command-line flags.
|
// Command-line flags.
|
||||||
|
|||||||
@@ -11,16 +11,16 @@ import (
|
|||||||
"github.com/mmcloughlin/avo/internal/inst"
|
"github.com/mmcloughlin/avo/internal/inst"
|
||||||
)
|
)
|
||||||
|
|
||||||
type constructors struct {
|
type ctors struct {
|
||||||
cfg Config
|
cfg Config
|
||||||
printer
|
printer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConstructors(cfg Config) Interface {
|
func NewCtors(cfg Config) Interface {
|
||||||
return GoFmt(&constructors{cfg: cfg})
|
return GoFmt(&ctors{cfg: cfg})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *constructors) Generate(is []inst.Instruction) ([]byte, error) {
|
func (c *ctors) Generate(is []inst.Instruction) ([]byte, error) {
|
||||||
c.Printf("// %s\n\n", c.cfg.GeneratedWarning())
|
c.Printf("// %s\n\n", c.cfg.GeneratedWarning())
|
||||||
c.Printf("package x86\n\n")
|
c.Printf("package x86\n\n")
|
||||||
c.Printf("import (\n")
|
c.Printf("import (\n")
|
||||||
@@ -35,7 +35,7 @@ func (c *constructors) Generate(is []inst.Instruction) ([]byte, error) {
|
|||||||
return c.Result()
|
return c.Result()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *constructors) instruction(i inst.Instruction) {
|
func (c *ctors) instruction(i inst.Instruction) {
|
||||||
for _, line := range c.doc(i) {
|
for _, line := range c.doc(i) {
|
||||||
c.Printf("// %s\n", line)
|
c.Printf("// %s\n", line)
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,7 @@ func (c *constructors) instruction(i inst.Instruction) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// doc generates the lines of the function comment.
|
// doc generates the lines of the function comment.
|
||||||
func (c *constructors) doc(i inst.Instruction) []string {
|
func (c *ctors) doc(i inst.Instruction) []string {
|
||||||
lines := []string{
|
lines := []string{
|
||||||
fmt.Sprintf("%s: %s.", i.Opcode, i.Summary),
|
fmt.Sprintf("%s: %s.", i.Opcode, i.Summary),
|
||||||
"",
|
"",
|
||||||
@@ -74,7 +74,7 @@ func (c *constructors) doc(i inst.Instruction) []string {
|
|||||||
return lines
|
return lines
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *constructors) checkargs(i inst.Instruction, s signature) {
|
func (c *ctors) checkargs(i inst.Instruction, s signature) {
|
||||||
if i.IsNiladic() {
|
if i.IsNiladic() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
108
internal/gen/ctorstest.go
Normal file
108
internal/gen/ctorstest.go
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
package gen
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/mmcloughlin/avo/internal/inst"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ctorstest struct {
|
||||||
|
cfg Config
|
||||||
|
printer
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCtorsTest(cfg Config) Interface {
|
||||||
|
return GoFmt(&ctorstest{cfg: cfg})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ctorstest) Generate(is []inst.Instruction) ([]byte, error) {
|
||||||
|
c.Printf("// %s\n\n", c.cfg.GeneratedWarning())
|
||||||
|
c.Printf("package x86\n\n")
|
||||||
|
c.Printf("import (\n")
|
||||||
|
c.Printf("\t\"testing\"\n")
|
||||||
|
c.Printf("\t\"math\"\n")
|
||||||
|
c.Printf("\t\"%s/reg\"\n", pkg)
|
||||||
|
c.Printf("\t\"%s/operand\"\n", pkg)
|
||||||
|
c.Printf(")\n\n")
|
||||||
|
|
||||||
|
for _, i := range is {
|
||||||
|
c.instruction(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Result()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ctorstest) instruction(i inst.Instruction) {
|
||||||
|
c.Printf("func Test%sValidForms(t *testing.T) {", i.Opcode)
|
||||||
|
|
||||||
|
for _, f := range i.Forms {
|
||||||
|
name := strings.Join(f.Signature(), "_")
|
||||||
|
c.Printf("t.Run(\"form=%s\", func(t *testing.T) {\n", name)
|
||||||
|
|
||||||
|
for _, args := range validFormArgs(f) {
|
||||||
|
c.Printf("if _, err := %s(%s)", i.Opcode, strings.Join(args, ", "))
|
||||||
|
c.Printf("; err != nil { t.Fatal(err) }\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Printf("})\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Printf("}\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func validFormArgs(f inst.Form) [][]string {
|
||||||
|
n := len(f.Operands)
|
||||||
|
args := make([][]string, n)
|
||||||
|
for i, op := range f.Operands {
|
||||||
|
valid, ok := validArgs[op.Type]
|
||||||
|
if !ok {
|
||||||
|
panic("missing operands for type " + op.Type)
|
||||||
|
}
|
||||||
|
args[i] = valid
|
||||||
|
}
|
||||||
|
return cross(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
var validArgs = map[string][]string{
|
||||||
|
// Immediates
|
||||||
|
"1": {"operand.Imm(1)"},
|
||||||
|
"3": {"operand.Imm(3)"},
|
||||||
|
"imm2u": {"operand.Imm(3)"},
|
||||||
|
"imm8": {"operand.Imm(math.MaxInt8)"},
|
||||||
|
"imm16": {"operand.Imm(math.MaxInt16)"},
|
||||||
|
"imm32": {"operand.Imm(math.MaxInt32)"},
|
||||||
|
"imm64": {"operand.Imm(math.MaxInt64)"},
|
||||||
|
|
||||||
|
// Registers
|
||||||
|
"al": {"reg.AL"},
|
||||||
|
"cl": {"reg.CL"},
|
||||||
|
"ax": {"reg.AX"},
|
||||||
|
"eax": {"reg.EAX"},
|
||||||
|
"rax": {"reg.RAX"},
|
||||||
|
"r8": {"reg.CH"},
|
||||||
|
"r16": {"reg.R9W"},
|
||||||
|
"r32": {"reg.R10L"},
|
||||||
|
"r64": {"reg.R11"},
|
||||||
|
"xmm0": {"reg.X0"},
|
||||||
|
"xmm": {"reg.X7"},
|
||||||
|
"ymm": {"reg.Y15"},
|
||||||
|
|
||||||
|
// Memory
|
||||||
|
"m": {"operand.Mem{Base: reg.BX, Index: reg.CX, Scale: 2}"},
|
||||||
|
"m8": {"operand.Mem{Base: reg.BL, Index: reg.CH, Scale: 1}"},
|
||||||
|
"m16": {"operand.Mem{Base: reg.BX, Index: reg.CX, Scale: 2}"},
|
||||||
|
"m32": {"operand.Mem{Base: reg.EBX, Index: reg.ECX, Scale: 4}"},
|
||||||
|
"m64": {"operand.Mem{Base: reg.RBX, Index: reg.RCX, Scale: 8}"},
|
||||||
|
"m128": {"operand.Mem{Base: reg.RBX, Index: reg.RCX, Scale: 8}"},
|
||||||
|
"m256": {"operand.Mem{Base: reg.RBX, Index: reg.RCX, Scale: 8}"},
|
||||||
|
|
||||||
|
// Vector memory
|
||||||
|
"vm32x": {"operand.Mem{Base: reg.R13, Index: reg.X4, Scale: 1}"},
|
||||||
|
"vm64x": {"operand.Mem{Base: reg.R13, Index: reg.X8, Scale: 1}"},
|
||||||
|
"vm32y": {"operand.Mem{Base: reg.R13, Index: reg.Y4, Scale: 1}"},
|
||||||
|
"vm64y": {"operand.Mem{Base: reg.R13, Index: reg.Y8, Scale: 1}"},
|
||||||
|
|
||||||
|
// Relative
|
||||||
|
"rel8": {"operand.Rel(math.MaxInt8)"},
|
||||||
|
"rel32": {"operand.Rel(math.MaxInt32)", "operand.LabelRef(\"lbl\")"},
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ func TestBuilderInterfaces(t *testing.T) {
|
|||||||
NewAsmTest,
|
NewAsmTest,
|
||||||
NewGoData,
|
NewGoData,
|
||||||
NewGoDataTest,
|
NewGoDataTest,
|
||||||
NewConstructors,
|
NewCtors,
|
||||||
|
NewCtorsTest,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
19
internal/gen/util.go
Normal file
19
internal/gen/util.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package gen
|
||||||
|
|
||||||
|
// cross returns the cross product of the lists in x.
|
||||||
|
func cross(x [][]string) [][]string {
|
||||||
|
r := [][]string{nil}
|
||||||
|
for _, s := range x {
|
||||||
|
var nxt [][]string
|
||||||
|
for _, pre := range r {
|
||||||
|
for _, a := range s {
|
||||||
|
concat := make([]string, len(pre), len(pre)+1)
|
||||||
|
copy(concat, pre)
|
||||||
|
concat = append(concat, a)
|
||||||
|
nxt = append(nxt, concat)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r = nxt
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
46
internal/gen/util_test.go
Normal file
46
internal/gen/util_test.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package gen
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCross(t *testing.T) {
|
||||||
|
x := [][]string{
|
||||||
|
{"a", "b", "c"},
|
||||||
|
{"1", "2"},
|
||||||
|
{"!", "?"},
|
||||||
|
}
|
||||||
|
expect := [][]string{
|
||||||
|
{"a", "1", "!"},
|
||||||
|
{"a", "1", "?"},
|
||||||
|
{"a", "2", "!"},
|
||||||
|
{"a", "2", "?"},
|
||||||
|
{"b", "1", "!"},
|
||||||
|
{"b", "1", "?"},
|
||||||
|
{"b", "2", "!"},
|
||||||
|
{"b", "2", "?"},
|
||||||
|
{"c", "1", "!"},
|
||||||
|
{"c", "1", "?"},
|
||||||
|
{"c", "2", "!"},
|
||||||
|
{"c", "2", "?"},
|
||||||
|
}
|
||||||
|
got := cross(x)
|
||||||
|
if !reflect.DeepEqual(got, expect) {
|
||||||
|
t.Errorf("bad cross product")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCrossSimple(t *testing.T) {
|
||||||
|
x := [][]string{
|
||||||
|
{"a", "b"},
|
||||||
|
}
|
||||||
|
expect := [][]string{
|
||||||
|
{"a"},
|
||||||
|
{"b"},
|
||||||
|
}
|
||||||
|
got := cross(x)
|
||||||
|
if !reflect.DeepEqual(got, expect) {
|
||||||
|
t.Errorf("bad cross product")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -207,12 +207,17 @@ func isvm(op avo.Operand, idx func(avo.Operand) bool) bool {
|
|||||||
return ok && IsR64(m.Base) && idx(m.Index)
|
return ok && IsR64(m.Base) && idx(m.Index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsRel8 returns true if op is an 8-bit offset relative to instruction pointer.
|
||||||
func IsRel8(op avo.Operand) bool {
|
func IsRel8(op avo.Operand) bool {
|
||||||
// TODO(mbm): implement rel8 operand check
|
r, ok := op.(Rel)
|
||||||
return false
|
return ok && r == Rel(int8(r))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsRel32 returns true if op is an offset relative to instruction pointer, or a
|
||||||
|
// label reference.
|
||||||
func IsRel32(op avo.Operand) bool {
|
func IsRel32(op avo.Operand) bool {
|
||||||
// TODO(mbm): implement rel32 operand check
|
// TODO(mbm): should labels be considered separately?
|
||||||
return false
|
_, rel := op.(Rel)
|
||||||
|
_, label := op.(LabelRef)
|
||||||
|
return rel || label
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package operand
|
package operand
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -129,6 +130,18 @@ func TestChecks(t *testing.T) {
|
|||||||
{IsVm64y, Mem{Base: reg.R9, Index: reg.Y11}, true},
|
{IsVm64y, Mem{Base: reg.R9, Index: reg.Y11}, true},
|
||||||
{IsVm64y, Mem{Base: reg.R11L, Index: reg.Y11}, false},
|
{IsVm64y, Mem{Base: reg.R11L, Index: reg.Y11}, false},
|
||||||
{IsVm64y, Mem{Base: reg.R8, Index: reg.Z11}, false},
|
{IsVm64y, Mem{Base: reg.R8, Index: reg.Z11}, false},
|
||||||
|
|
||||||
|
// Relative operands
|
||||||
|
{IsRel8, Rel(math.MinInt8), true},
|
||||||
|
{IsRel8, Rel(math.MaxInt8), true},
|
||||||
|
{IsRel8, Rel(math.MinInt8 - 1), false},
|
||||||
|
{IsRel8, Rel(math.MaxInt8 + 1), false},
|
||||||
|
{IsRel8, reg.R9B, false},
|
||||||
|
|
||||||
|
{IsRel32, Rel(math.MinInt32), true},
|
||||||
|
{IsRel32, Rel(math.MaxInt32), true},
|
||||||
|
{IsRel32, LabelRef("label"), true},
|
||||||
|
{IsRel32, reg.R9L, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
|
|||||||
@@ -30,5 +30,19 @@ func (m Mem) Asm() string {
|
|||||||
type Imm uint64
|
type Imm uint64
|
||||||
|
|
||||||
func (i Imm) Asm() string {
|
func (i Imm) Asm() string {
|
||||||
return fmt.Sprintf("%#x", uint64(i))
|
return fmt.Sprintf("%#x", i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rel is an offset relative to the instruction pointer.
|
||||||
|
type Rel int32
|
||||||
|
|
||||||
|
func (r Rel) Asm() string {
|
||||||
|
return fmt.Sprintf(".%+d", r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LabelRef is a reference to a label.
|
||||||
|
type LabelRef string
|
||||||
|
|
||||||
|
func (l LabelRef) Asm() string {
|
||||||
|
return string(l)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ import "errors"
|
|||||||
|
|
||||||
var ErrBadOperandTypes = errors.New("bad operand types")
|
var ErrBadOperandTypes = errors.New("bad operand types")
|
||||||
|
|
||||||
//go:generate avogen -output zconstructors.go constructors
|
//go:generate avogen -output zctors.go ctors
|
||||||
|
//go:generate avogen -output zctors_test.go ctorstest
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// Code generated by command: avogen -output zconstructors.go constructors. DO NOT EDIT.
|
// Code generated by command: avogen -output zctors.go ctors. DO NOT EDIT.
|
||||||
|
|
||||||
package x86
|
package x86
|
||||||
|
|
||||||
17961
x86/zctors_test.go
Normal file
17961
x86/zctors_test.go
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user