generate the instruction table
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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
8
internal/gen/gen_test.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package gen
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestBuilderInterfaces(t *testing.T) {
|
||||
var _ Builder = NewAsmTest
|
||||
var _ Builder = NewGoData
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user