This commit is contained in:
Michael McLoughlin
2018-11-21 13:02:18 -06:00
parent cb259ce43b
commit 59e6af7d36
10 changed files with 553 additions and 2 deletions

34
internal/gen/gen.go Normal file
View File

@@ -0,0 +1,34 @@
package gen
import (
"fmt"
"io"
"github.com/mmcloughlin/avo/internal/inst"
)
type Interface interface {
Generate(io.Writer, []*inst.Instruction) error
}
type Func func(io.Writer, []*inst.Instruction) error
func (f Func) Generate(w io.Writer, is []*inst.Instruction) error {
return f(w, is)
}
type printer struct {
w io.Writer
err error
}
func (p *printer) printf(format string, args ...interface{}) {
if p.err != nil {
return
}
_, p.err = fmt.Fprintf(p.w, format, args...)
}
func (p *printer) Err() error {
return p.err
}

127
internal/gen/loadertest.go Normal file
View File

@@ -0,0 +1,127 @@
package gen
import (
"io"
"strings"
"github.com/mmcloughlin/avo/internal/inst"
)
type LoaderTest struct{}
func (l LoaderTest) Generate(w io.Writer, is []*inst.Instruction) error {
p := &printer{w: w}
p.printf("TEXT loadertest(SB), 0, $0\n")
for _, i := range is {
p.printf("\t// %s %s\n", i.Opcode, i.Summary)
for _, f := range i.Forms {
as := args(f.Operands)
p.printf("\t// %#v\n", f.Operands)
if as == nil {
p.printf("\t// skip\n")
continue
}
p.printf("\t%s\t%s\n", i.Opcode, strings.Join(as, ", "))
}
p.printf("\n")
}
p.printf("\tRET\n")
return p.Err()
}
func args(ops []inst.Operand) []string {
as := make([]string, len(ops))
for i, op := range ops {
a := arg(op.Type)
if a == "" {
return nil
}
as[i] = a
}
return as
}
// arg generates an argument for an operand of the given type.
func arg(t string) string {
m := map[string]string{
// <xs:enumeration value="1" />
// <xs:enumeration value="3" />
// <xs:enumeration value="imm4" />
// <xs:enumeration value="imm8" />
// <xs:enumeration value="imm16" />
// <xs:enumeration value="imm32" />
// <xs:enumeration value="imm64" />
// <xs:enumeration value="al" />
// <xs:enumeration value="cl" />
// <xs:enumeration value="r8" />
// <xs:enumeration value="r8l" />
// <xs:enumeration value="ax" />
// <xs:enumeration value="r16" />
// <xs:enumeration value="r16l" />
// <xs:enumeration value="eax" />
// <xs:enumeration value="r32" />
// <xs:enumeration value="r32l" />
// <xs:enumeration value="rax" />
// <xs:enumeration value="r64" />
"r64": "R8",
// <xs:enumeration value="mm" />
// <xs:enumeration value="xmm0" />
// <xs:enumeration value="xmm" />
// <xs:enumeration value="xmm{k}" />
// <xs:enumeration value="xmm{k}{z}" />
// <xs:enumeration value="ymm" />
// <xs:enumeration value="ymm{k}" />
// <xs:enumeration value="ymm{k}{z}" />
// <xs:enumeration value="zmm" />
// <xs:enumeration value="zmm{k}" />
// <xs:enumeration value="zmm{k}{z}" />
// <xs:enumeration value="k" />
// <xs:enumeration value="k{k}" />
// <xs:enumeration value="moffs32" />
// <xs:enumeration value="moffs64" />
// <xs:enumeration value="m" />
// <xs:enumeration value="m8" />
// <xs:enumeration value="m16" />
// <xs:enumeration value="m16{k}{z}" />
// <xs:enumeration value="m32" />
// <xs:enumeration value="m32{k}" />
// <xs:enumeration value="m32{k}{z}" />
// <xs:enumeration value="m64" />
// <xs:enumeration value="m64{k}" />
// <xs:enumeration value="m64{k}{z}" />
// <xs:enumeration value="m128" />
// <xs:enumeration value="m128{k}{z}" />
// <xs:enumeration value="m256" />
// <xs:enumeration value="m256{k}{z}" />
// <xs:enumeration value="m512" />
// <xs:enumeration value="m512{k}{z}" />
// <xs:enumeration value="m64/m32bcst" />
// <xs:enumeration value="m128/m32bcst" />
// <xs:enumeration value="m256/m32bcst" />
// <xs:enumeration value="m512/m32bcst" />
// <xs:enumeration value="m128/m64bcst" />
// <xs:enumeration value="m256/m64bcst" />
// <xs:enumeration value="m512/m64bcst" />
// <xs:enumeration value="vm32x" />
// <xs:enumeration value="vm32x{k}" />
// <xs:enumeration value="vm64x" />
// <xs:enumeration value="vm64x{k}" />
// <xs:enumeration value="vm32y" />
// <xs:enumeration value="vm32y{k}" />
// <xs:enumeration value="vm64y" />
// <xs:enumeration value="vm64y{k}" />
// <xs:enumeration value="vm32z" />
// <xs:enumeration value="vm32z{k}" />
// <xs:enumeration value="vm64z" />
// <xs:enumeration value="vm64z{k}" />
// <xs:enumeration value="rel8" />
// <xs:enumeration value="rel32" />
// <xs:enumeration value="{er}" />
// <xs:enumeration value="{sae}" />
}
return m[t]
}