add instruction arities function

This commit is contained in:
Michael McLoughlin
2018-11-25 18:25:51 -08:00
parent 09848512cc
commit 4dcfed6e16
2 changed files with 35 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package inst_test
import (
"io/ioutil"
"reflect"
"strings"
"testing"
@@ -40,7 +41,6 @@ func TestInstructionProperties(t *testing.T) {
t.Errorf("instruction %s has no forms", i.Opcode)
}
}
}
func TestAssembles(t *testing.T) {
@@ -61,6 +61,25 @@ func TestLookup(t *testing.T) {
}
}
func TestInstructionArities(t *testing.T) {
cases := map[string][]int{
"AESDEC": {2},
"EXTRACTPS": {3},
"SHRQ": {2, 3},
"VMOVHPD": {2, 3},
}
for opcode, expect := range cases {
i, ok := inst.Lookup(opcode)
if !ok {
t.Fatalf("could not find %s", opcode)
}
got := i.Arities()
if !reflect.DeepEqual(got, expect) {
t.Errorf("arity of %s is %v expected %v", opcode, got, expect)
}
}
}
func TestStdLibOpcodes(t *testing.T) {
b, err := ioutil.ReadFile("testdata/stdlibopcodes.txt")
if err != nil {

View File

@@ -1,5 +1,7 @@
package inst
import "sort"
type Instruction struct {
Opcode string
AliasOf string
@@ -7,6 +9,19 @@ type Instruction struct {
Forms []Form
}
func (i Instruction) Arities() []int {
s := map[int]bool{}
for _, f := range i.Forms {
s[len(f.Operands)] = true
}
a := make([]int, 0, len(s))
for n := range s {
a = append(a, n)
}
sort.Ints(a)
return a
}
type Form struct {
ISA []string
Operands []Operand