x86: rel types and generated tests

This commit is contained in:
Michael McLoughlin
2018-11-27 22:08:11 -08:00
parent 3881907ec8
commit 4395adacc8
13 changed files with 18188 additions and 19 deletions

View File

@@ -13,10 +13,11 @@ import (
)
var generators = map[string]gen.Builder{
"asmtest": gen.NewAsmTest,
"godata": gen.NewGoData,
"godatatest": gen.NewGoDataTest,
"constructors": gen.NewConstructors,
"asmtest": gen.NewAsmTest,
"godata": gen.NewGoData,
"godatatest": gen.NewGoDataTest,
"ctors": gen.NewCtors,
"ctorstest": gen.NewCtorsTest,
}
// Command-line flags.

View File

@@ -11,16 +11,16 @@ import (
"github.com/mmcloughlin/avo/internal/inst"
)
type constructors struct {
type ctors struct {
cfg Config
printer
}
func NewConstructors(cfg Config) Interface {
return GoFmt(&constructors{cfg: cfg})
func NewCtors(cfg Config) Interface {
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("package x86\n\n")
c.Printf("import (\n")
@@ -35,7 +35,7 @@ func (c *constructors) Generate(is []inst.Instruction) ([]byte, error) {
return c.Result()
}
func (c *constructors) instruction(i inst.Instruction) {
func (c *ctors) instruction(i inst.Instruction) {
for _, line := range c.doc(i) {
c.Printf("// %s\n", line)
}
@@ -49,7 +49,7 @@ func (c *constructors) instruction(i inst.Instruction) {
}
// 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{
fmt.Sprintf("%s: %s.", i.Opcode, i.Summary),
"",
@@ -74,7 +74,7 @@ func (c *constructors) doc(i inst.Instruction) []string {
return lines
}
func (c *constructors) checkargs(i inst.Instruction, s signature) {
func (c *ctors) checkargs(i inst.Instruction, s signature) {
if i.IsNiladic() {
return
}

108
internal/gen/ctorstest.go Normal file
View 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\")"},
}

View File

@@ -7,6 +7,7 @@ func TestBuilderInterfaces(t *testing.T) {
NewAsmTest,
NewGoData,
NewGoDataTest,
NewConstructors,
NewCtors,
NewCtorsTest,
}
}

19
internal/gen/util.go Normal file
View 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
View 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")
}
}

View File

@@ -207,12 +207,17 @@ func isvm(op avo.Operand, idx func(avo.Operand) bool) bool {
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 {
// TODO(mbm): implement rel8 operand check
return false
r, ok := op.(Rel)
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 {
// TODO(mbm): implement rel32 operand check
return false
// TODO(mbm): should labels be considered separately?
_, rel := op.(Rel)
_, label := op.(LabelRef)
return rel || label
}

View File

@@ -1,6 +1,7 @@
package operand
import (
"math"
"reflect"
"runtime"
"testing"
@@ -129,6 +130,18 @@ func TestChecks(t *testing.T) {
{IsVm64y, Mem{Base: reg.R9, Index: reg.Y11}, true},
{IsVm64y, Mem{Base: reg.R11L, Index: reg.Y11}, 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 {

View File

@@ -30,5 +30,19 @@ func (m Mem) Asm() string {
type Imm uint64
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)
}

View File

@@ -4,4 +4,5 @@ import "errors"
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

View File

@@ -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

17961
x86/zctors_test.go Normal file

File diff suppressed because it is too large Load Diff