generate the instruction table

This commit is contained in:
Michael McLoughlin
2018-11-24 13:47:30 -08:00
parent f1e1da6387
commit 70dcf2b611
8 changed files with 25542 additions and 11 deletions

View File

@@ -11,9 +11,9 @@ import (
"github.com/mmcloughlin/avo/internal/load"
)
var generators = map[string]gen.Interface{
"asmtest": gen.NewAsmTest(),
"godata": gen.NewGoData(),
var generators = map[string]gen.Builder{
"asmtest": gen.NewAsmTest,
"godata": gen.NewGoData,
}
var datadir = flag.String(
@@ -29,18 +29,23 @@ func main() {
// Build generator.
t := flag.Arg(0)
g := generators[t]
if g == nil {
builder := generators[t]
if builder == nil {
log.Fatalf("unknown generator type '%s'", t)
}
g := builder(gen.Config{
Argv: os.Args,
})
// Determine output writer.
w := os.Stdout
if *output != "" {
f, err := os.Open(*output)
f, err := os.Create(*output)
if err != nil {
log.Fatal(err)
}
defer f.Close()
w = f
}

View File

@@ -10,18 +10,21 @@ import (
)
type asmtest struct {
cfg Config
sym string // reference to the test function symbol
rel8 string // label to be used for near jumps
rel32 string // label for far jumps
}
func NewAsmTest() Interface {
return &asmtest{}
func NewAsmTest(cfg Config) Interface {
return &asmtest{cfg: cfg}
}
func (a *asmtest) Generate(is []*inst.Instruction) ([]byte, error) {
p := &printer{}
p.Printf("# %s\n\n", a.cfg.GeneratedWarning())
a.sym = "\u00b7loadertest(SB)"
p.Printf("TEXT %s, 0, $0\n", a.sym)

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"go/format"
"strings"
"github.com/mmcloughlin/avo/internal/inst"
)
@@ -18,6 +19,24 @@ func (f Func) Generate(is []*inst.Instruction) ([]byte, error) {
return f(is)
}
type Config struct {
Name string
Argv []string
}
func (c Config) GeneratedBy() string {
if c.Argv == nil {
return c.Name
}
return fmt.Sprintf("command: %s", strings.Join(c.Argv, " "))
}
func (c Config) GeneratedWarning() string {
return fmt.Sprintf("Code generated by %s. DO NOT EDIT.", c.GeneratedBy())
}
type Builder func(Config) Interface
// GoFmt formats Go code produced from the given generator.
func GoFmt(i Interface) Interface {
return Func(func(is []*inst.Instruction) ([]byte, error) {

8
internal/gen/gen_test.go Normal file
View File

@@ -0,0 +1,8 @@
package gen
import "testing"
func TestBuilderInterfaces(t *testing.T) {
var _ Builder = NewAsmTest
var _ Builder = NewGoData
}

View File

@@ -5,15 +5,17 @@ import (
)
type godata struct {
cfg Config
}
func NewGoData() Interface {
return GoFmt(godata{})
func NewGoData(cfg Config) Interface {
return GoFmt(godata{cfg: cfg})
}
func (g godata) Generate(is []*inst.Instruction) ([]byte, error) {
p := &printer{}
p.Printf("// %s\n\n", g.cfg.GeneratedWarning())
p.Printf("package inst\n\n")
p.Printf("var Instructions = []Instruction{\n")

View File

@@ -1,5 +1,7 @@
package inst
//go:generate avogen -data ../data -output ztable.go godata
type Instruction struct {
Opcode string
Summary string

25487
internal/inst/ztable.go Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -2,6 +2,7 @@ package load
import (
"path/filepath"
"sort"
"strconv"
"strings"
@@ -61,12 +62,16 @@ func (l *Loader) Load() ([]*inst.Instruction, error) {
}
}
// Convert to a slice to return.
// Convert to a slice, sorted by opcode.
is := make([]*inst.Instruction, 0, len(im))
for _, i := range im {
is = append(is, i)
}
sort.Slice(is, func(i, j int) bool {
return is[i].Opcode < is[j].Opcode
})
return is, nil
}