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

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