wip: adding instruction inputs and outputs
This commit is contained in:
@@ -14,6 +14,7 @@ type asmtest struct {
|
||||
sym string // reference to the test function symbol
|
||||
rel8 string // label to be used for near jumps
|
||||
rel32 string // label for far jumps
|
||||
generator
|
||||
}
|
||||
|
||||
func NewAsmTest(cfg Config) Interface {
|
||||
@@ -21,53 +22,51 @@ func NewAsmTest(cfg Config) Interface {
|
||||
}
|
||||
|
||||
func (a *asmtest) Generate(is []inst.Instruction) ([]byte, error) {
|
||||
p := &printer{}
|
||||
|
||||
p.Printf("// %s\n\n", a.cfg.GeneratedWarning())
|
||||
a.Printf("// %s\n\n", a.cfg.GeneratedWarning())
|
||||
|
||||
a.sym = "\u00b7loadertest(SB)"
|
||||
p.Printf("TEXT %s, 0, $0\n", a.sym)
|
||||
a.Printf("TEXT %s, 0, $0\n", a.sym)
|
||||
|
||||
// Define a label for far jumps.
|
||||
p.Printf("rel32:\n")
|
||||
a.Printf("rel32:\n")
|
||||
a.rel32 = "rel32"
|
||||
|
||||
counts := map[string]int{}
|
||||
|
||||
for _, i := range is {
|
||||
p.Printf("\t// %s %s\n", i.Opcode, i.Summary)
|
||||
a.Printf("\t// %s %s\n", i.Opcode, i.Summary)
|
||||
if skip, msg := a.skip(i.Opcode); skip {
|
||||
p.Printf("\t// SKIP: %s\n", msg)
|
||||
a.Printf("\t// SKIP: %s\n", msg)
|
||||
counts["skip"]++
|
||||
continue
|
||||
}
|
||||
|
||||
if i.Opcode[0] == 'J' {
|
||||
label := fmt.Sprintf("rel8_%s", strings.ToLower(i.Opcode))
|
||||
p.Printf("%s:\n", label)
|
||||
a.Printf("%s:\n", label)
|
||||
a.rel8 = label
|
||||
}
|
||||
|
||||
for _, f := range i.Forms {
|
||||
as := a.args(i.Opcode, f.Operands)
|
||||
if as == nil {
|
||||
p.Printf("\t// TODO: %s %#v\n", i.Opcode, f.Operands)
|
||||
a.Printf("\t// TODO: %s %#v\n", i.Opcode, f.Operands)
|
||||
counts["todo"]++
|
||||
continue
|
||||
}
|
||||
p.Printf("\t%s\t%s\n", i.Opcode, strings.Join(as, ", "))
|
||||
a.Printf("\t%s\t%s\n", i.Opcode, strings.Join(as, ", "))
|
||||
counts["total"]++
|
||||
}
|
||||
p.Printf("\n")
|
||||
a.Printf("\n")
|
||||
}
|
||||
|
||||
p.Printf("\tRET\n")
|
||||
a.Printf("\tRET\n")
|
||||
|
||||
for m, c := range counts {
|
||||
p.Printf("// %s: %d\n", m, c)
|
||||
a.Printf("// %s: %d\n", m, c)
|
||||
}
|
||||
|
||||
return p.Result()
|
||||
return a.Result()
|
||||
}
|
||||
|
||||
func (a asmtest) skip(opcode string) (bool, string) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import "github.com/mmcloughlin/avo/internal/inst"
|
||||
|
||||
type build struct {
|
||||
cfg Config
|
||||
printer
|
||||
generator
|
||||
}
|
||||
|
||||
func NewBuild(cfg Config) Interface {
|
||||
|
||||
@@ -4,14 +4,13 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/mmcloughlin/avo/internal/inst"
|
||||
)
|
||||
|
||||
type ctors struct {
|
||||
cfg Config
|
||||
printer
|
||||
generator
|
||||
}
|
||||
|
||||
func NewCtors(cfg Config) Interface {
|
||||
@@ -34,59 +33,23 @@ func (c *ctors) Generate(is []inst.Instruction) ([]byte, error) {
|
||||
}
|
||||
|
||||
func (c *ctors) instruction(i inst.Instruction) {
|
||||
for _, line := range c.doc(i) {
|
||||
for _, line := range doc(i) {
|
||||
c.Printf("// %s\n", line)
|
||||
}
|
||||
|
||||
s := params(i)
|
||||
|
||||
c.Printf("func %s(%s) (*avo.Instruction, error) {\n", i.Opcode, s.ParameterList())
|
||||
c.checkargs(i, s)
|
||||
c.Printf("\treturn &%s, nil\n", construct(i, s))
|
||||
c.forms(i, s)
|
||||
c.Printf("}\n\n")
|
||||
}
|
||||
|
||||
// doc generates the lines of the function comment.
|
||||
func (c *ctors) 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
|
||||
}
|
||||
|
||||
func construct(i inst.Instruction, s signature) string {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
fmt.Fprintf(buf, "avo.Instruction{\n")
|
||||
fmt.Fprintf(buf, "\tOpcode: %#v,\n", i.Opcode)
|
||||
fmt.Fprintf(buf, "\tOperands: %s,\n", s.ParameterSlice())
|
||||
if i.IsBranch() {
|
||||
fmt.Fprintf(buf, "\tIsBranch: true,\n")
|
||||
fmt.Fprintf(buf, "\tIsConditional: %#v,\n", i.IsConditionalBranch())
|
||||
}
|
||||
fmt.Fprintf(buf, "}")
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func (c *ctors) checkargs(i inst.Instruction, s signature) {
|
||||
func (c *ctors) forms(i inst.Instruction, s signature) {
|
||||
if i.IsNiladic() {
|
||||
if len(i.Forms) != 1 {
|
||||
c.AddError(fmt.Errorf("%s breaks assumption that niladic instructions have one form", i.Opcode))
|
||||
}
|
||||
c.Printf("return &%s, nil\n", construct(i, i.Forms[0], s))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -106,12 +69,42 @@ func (c *ctors) checkargs(i inst.Instruction, s signature) {
|
||||
}
|
||||
|
||||
c.Printf("case %s:\n", strings.Join(conds, " && "))
|
||||
c.Printf("return &%s, nil\n", construct(i, f, s))
|
||||
}
|
||||
|
||||
c.Printf("default:\n")
|
||||
c.Printf("return nil, ErrBadOperandTypes\n")
|
||||
|
||||
c.Printf("}\n")
|
||||
c.Printf("return nil, ErrBadOperandTypes\n")
|
||||
}
|
||||
|
||||
func construct(i inst.Instruction, f inst.Form, s signature) string {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
fmt.Fprintf(buf, "avo.Instruction{\n")
|
||||
fmt.Fprintf(buf, "\tOpcode: %#v,\n", i.Opcode)
|
||||
fmt.Fprintf(buf, "\tOperands: %s,\n", s.ParameterSlice())
|
||||
|
||||
// Input output.
|
||||
// TODO(mbm): handle implicit operands
|
||||
fmt.Fprintf(buf, "\tInputs: %s,\n", actionfilter(f.Operands, inst.R, s))
|
||||
fmt.Fprintf(buf, "\tOutputs: %s,\n", actionfilter(f.Operands, inst.W, s))
|
||||
|
||||
// Branch variables.
|
||||
if i.IsBranch() {
|
||||
fmt.Fprintf(buf, "\tIsBranch: true,\n")
|
||||
fmt.Fprintf(buf, "\tIsConditional: %#v,\n", i.IsConditionalBranch())
|
||||
}
|
||||
|
||||
fmt.Fprintf(buf, "}")
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func actionfilter(ops []inst.Operand, a inst.Action, s signature) string {
|
||||
opexprs := []string{}
|
||||
for i, op := range ops {
|
||||
if op.Action.Contains(a) {
|
||||
opexprs = append(opexprs, s.ParameterName(i))
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("[]%s{%s}", operandType, strings.Join(opexprs, ", "))
|
||||
}
|
||||
|
||||
// checkername returns the name of the function that checks an operand of type t.
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
type ctorstest struct {
|
||||
cfg Config
|
||||
printer
|
||||
generator
|
||||
}
|
||||
|
||||
func NewCtorsTest(cfg Config) Interface {
|
||||
|
||||
@@ -50,18 +50,26 @@ func GoFmt(i Interface) Interface {
|
||||
})
|
||||
}
|
||||
|
||||
type printer struct {
|
||||
type generator struct {
|
||||
buf bytes.Buffer
|
||||
err error
|
||||
}
|
||||
|
||||
func (p *printer) Printf(format string, args ...interface{}) {
|
||||
if p.err != nil {
|
||||
func (g *generator) Printf(format string, args ...interface{}) {
|
||||
if g.err != nil {
|
||||
return
|
||||
}
|
||||
_, p.err = fmt.Fprintf(&p.buf, format, args...)
|
||||
if _, err := fmt.Fprintf(&g.buf, format, args...); err != nil {
|
||||
g.AddError(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *printer) Result() ([]byte, error) {
|
||||
return p.buf.Bytes(), p.err
|
||||
func (g *generator) AddError(err error) {
|
||||
if err != nil && g.err == nil {
|
||||
g.err = err
|
||||
}
|
||||
}
|
||||
|
||||
func (g *generator) Result() ([]byte, error) {
|
||||
return g.buf.Bytes(), g.err
|
||||
}
|
||||
|
||||
@@ -6,80 +6,78 @@ import (
|
||||
|
||||
type godata struct {
|
||||
cfg Config
|
||||
generator
|
||||
}
|
||||
|
||||
func NewGoData(cfg Config) Interface {
|
||||
return GoFmt(godata{cfg: cfg})
|
||||
return GoFmt(&godata{cfg: cfg})
|
||||
}
|
||||
|
||||
func (g godata) Generate(is []inst.Instruction) ([]byte, error) {
|
||||
p := &printer{}
|
||||
func (g *godata) Generate(is []inst.Instruction) ([]byte, error) {
|
||||
g.Printf("// %s\n\n", g.cfg.GeneratedWarning())
|
||||
g.Printf("package inst\n\n")
|
||||
|
||||
p.Printf("// %s\n\n", g.cfg.GeneratedWarning())
|
||||
p.Printf("package inst\n\n")
|
||||
|
||||
p.Printf("var Instructions = []Instruction{\n")
|
||||
g.Printf("var Instructions = []Instruction{\n")
|
||||
|
||||
for _, i := range is {
|
||||
p.Printf("{\n")
|
||||
g.Printf("{\n")
|
||||
|
||||
p.Printf("Opcode: %#v,\n", i.Opcode)
|
||||
g.Printf("Opcode: %#v,\n", i.Opcode)
|
||||
if i.AliasOf != "" {
|
||||
p.Printf("AliasOf: %#v,\n", i.AliasOf)
|
||||
g.Printf("AliasOf: %#v,\n", i.AliasOf)
|
||||
}
|
||||
p.Printf("Summary: %#v,\n", i.Summary)
|
||||
g.Printf("Summary: %#v,\n", i.Summary)
|
||||
|
||||
p.Printf("Forms: []Form{\n")
|
||||
g.Printf("Forms: []Form{\n")
|
||||
for _, f := range i.Forms {
|
||||
p.Printf("{\n")
|
||||
g.Printf("{\n")
|
||||
|
||||
if f.ISA != nil {
|
||||
p.Printf("ISA: %#v,\n", f.ISA)
|
||||
g.Printf("ISA: %#v,\n", f.ISA)
|
||||
}
|
||||
|
||||
if f.Operands != nil {
|
||||
p.Printf("Operands: []Operand{\n")
|
||||
g.Printf("Operands: []Operand{\n")
|
||||
for _, op := range f.Operands {
|
||||
p.Printf("{Type: %#v, Action: %#v},\n", op.Type, op.Action)
|
||||
g.Printf("{Type: %#v, Action: %#v},\n", op.Type, op.Action)
|
||||
}
|
||||
p.Printf("},\n")
|
||||
g.Printf("},\n")
|
||||
}
|
||||
|
||||
if f.ImplicitOperands != nil {
|
||||
p.Printf("ImplicitOperands: []ImplicitOperand{\n")
|
||||
g.Printf("ImplicitOperands: []ImplicitOperand{\n")
|
||||
for _, op := range f.ImplicitOperands {
|
||||
p.Printf("{Register: %#v, Action: %#v},\n", op.Register, op.Action)
|
||||
g.Printf("{Register: %#v, Action: %#v},\n", op.Register, op.Action)
|
||||
}
|
||||
p.Printf("},\n")
|
||||
g.Printf("},\n")
|
||||
}
|
||||
|
||||
p.Printf("},\n")
|
||||
g.Printf("},\n")
|
||||
}
|
||||
p.Printf("},\n")
|
||||
g.Printf("},\n")
|
||||
|
||||
p.Printf("},\n")
|
||||
g.Printf("},\n")
|
||||
}
|
||||
|
||||
p.Printf("}\n")
|
||||
g.Printf("}\n")
|
||||
|
||||
return p.Result()
|
||||
return g.Result()
|
||||
}
|
||||
|
||||
type godatatest struct {
|
||||
cfg Config
|
||||
generator
|
||||
}
|
||||
|
||||
func NewGoDataTest(cfg Config) Interface {
|
||||
return GoFmt(godatatest{cfg: cfg})
|
||||
return GoFmt(&godatatest{cfg: cfg})
|
||||
}
|
||||
|
||||
func (g godatatest) Generate(is []inst.Instruction) ([]byte, error) {
|
||||
p := &printer{}
|
||||
func (g *godatatest) Generate(is []inst.Instruction) ([]byte, error) {
|
||||
g.Printf("// %s\n\n", g.cfg.GeneratedWarning())
|
||||
g.Printf("package inst_test\n\n")
|
||||
|
||||
p.Printf("// %s\n\n", g.cfg.GeneratedWarning())
|
||||
p.Printf("package inst_test\n\n")
|
||||
|
||||
p.Printf(`import (
|
||||
g.Printf(`import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
@@ -87,14 +85,14 @@ func (g godatatest) Generate(is []inst.Instruction) ([]byte, error) {
|
||||
)
|
||||
`, pkg)
|
||||
|
||||
p.Printf("var raw = %#v\n\n", is)
|
||||
g.Printf("var raw = %#v\n\n", is)
|
||||
|
||||
p.Printf(`func TestVerifyInstructionsList(t *testing.T) {
|
||||
g.Printf(`func TestVerifyInstructionsList(t *testing.T) {
|
||||
if !reflect.DeepEqual(raw, inst.Instructions) {
|
||||
t.Fatal("bad code generation for instructions list")
|
||||
}
|
||||
}
|
||||
`)
|
||||
|
||||
return p.Result()
|
||||
return g.Result()
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/mmcloughlin/avo/internal/inst"
|
||||
)
|
||||
@@ -95,3 +97,29 @@ func params(i inst.Instruction) signature {
|
||||
|
||||
return argslist(ops)
|
||||
}
|
||||
|
||||
// doc generates the lines of the function comment.
|
||||
func 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user