add skeleton for instruction constructors

This commit is contained in:
Michael McLoughlin
2018-11-25 21:50:46 -08:00
parent 4dcfed6e16
commit af02be06ba
7 changed files with 165 additions and 77 deletions

View File

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

View File

@@ -0,0 +1,121 @@
package gen
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"
"text/tabwriter"
"github.com/mmcloughlin/avo/internal/inst"
)
type constructors struct {
cfg Config
printer
}
func NewConstructors(cfg Config) Interface {
return GoFmt(&constructors{cfg: cfg})
}
func (c *constructors) Generate(is []inst.Instruction) ([]byte, error) {
c.Printf("// %s\n\n", c.cfg.GeneratedWarning())
c.Printf("package x86\n\n")
c.Printf("import \"github.com/mmcloughlin/avo\"\n\n")
for _, i := range is {
c.instruction(i)
}
return c.Result()
}
func (c *constructors) instruction(i inst.Instruction) {
for _, line := range c.doc(i) {
c.Printf("// %s\n", line)
}
paramlist, _ := params(i)
c.Printf("func %s(%s) error {\n", i.Opcode, paramlist)
c.Printf("\treturn nil\n")
c.Printf("}\n\n")
}
// doc generates the lines of the function comment.
func (c *constructors) doc(i inst.Instruction) []string {
lines := []string{
fmt.Sprintf("%s: %s.", i.Opcode, i.Summary),
"",
"Forms:",
"",
}
// Write a table of instruction forms.
buf := bytes.NewBuffer(nil)
w := tabwriter.NewWriter(buf, 0, 0, 1, ' ', 0)
for _, f := range i.Forms {
row := i.Opcode + "\t" + strings.Join(f.Signature(), "\t") + "\n"
fmt.Fprint(w, row)
}
w.Flush()
tbl := strings.TrimSpace(buf.String())
for _, line := range strings.Split(tbl, "\n") {
lines = append(lines, "\t"+line)
}
return lines
}
// params generates the function parameters and a function.
func params(i inst.Instruction) (string, func(int) string) {
a := i.Arities()
// Handle the case of forms with multiple arities.
if len(a) > 1 {
return "ops ...avo.Operand", func(j int) string {
return fmt.Sprintf("ops[%d]", j)
}
}
// All forms have the same arity.
n := a[0]
if n == 0 {
return "", func(int) string { panic("unreachable") }
}
// Generate nice-looking variable names.
ops := make([]string, n)
count := map[string]int{}
for j := 0; j < n; j++ {
// Collect unique lowercase bytes from first characters of operand types.
s := map[byte]bool{}
for _, f := range i.Forms {
c := f.Operands[j].Type[0]
if 'a' <= c && c <= 'z' {
s[c] = true
}
}
// Operand name is the sorted bytes.
var b []byte
for c := range s {
b = append(b, c)
}
sort.Slice(b, func(i, j int) bool { return b[i] < b[j] })
name := string(b)
// Append a counter if we've seen it already.
m := count[name]
count[name]++
if m > 0 {
name += strconv.Itoa(m)
}
ops[j] = name
}
return strings.Join(ops, ", ") + " avo.Operand", func(j int) string { return ops[j] }
}

View File

@@ -0,0 +1,28 @@
package gen
import (
"testing"
"github.com/mmcloughlin/avo/internal/inst"
)
func TestParamsUniqueArgNames(t *testing.T) {
for _, i := range inst.Instructions {
_, argname := params(i)
for _, n := range i.Arities() {
if n == 0 {
continue
}
names := map[string]bool{}
for j := 0; j < n; j++ {
names[argname(j)] = true
}
if len(names) != n {
t.Errorf("repeated argument for instruction %s", i.Opcode)
}
if _, found := names[""]; found {
t.Errorf("empty argument name for instruction %s", i.Opcode)
}
}
}
}

View File

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

View File

@@ -40,6 +40,9 @@ func TestInstructionProperties(t *testing.T) {
if len(i.Forms) == 0 {
t.Errorf("instruction %s has no forms", i.Opcode)
}
if len(i.Arities()) == 0 {
t.Errorf("instruction %s has empty arities list", i.Opcode)
}
}
}

View File

@@ -28,6 +28,14 @@ type Form struct {
ImplicitOperands []ImplicitOperand
}
func (f Form) Signature() []string {
s := make([]string, len(f.Operands))
for i, op := range f.Operands {
s[i] = op.Type
}
return s
}
type Operand struct {
Type string
Action Action