add instruction arities function
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user