package gen
import (
"fmt"
"io"
"math"
"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)
if skip, msg := l.skip(i.Opcode); skip {
p.printf("\t// SKIP: %s\n", msg)
continue
}
for _, f := range i.Forms {
as := args(f.Operands)
p.printf("\t// %#v\n", f.Operands)
if as == nil {
p.printf("\t// TODO\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 (l LoaderTest) skip(opcode string) (bool, string) {
prefixes := map[string]string{
"PUSH": "PUSH can produce 'unbalanced PUSH/POP' assembler error",
"POP": "POP can produce 'unbalanced PUSH/POP' assembler error",
}
for p, m := range prefixes {
if strings.HasPrefix(opcode, p) {
return true, m
}
}
return false, ""
}
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{
//
//
//
//
//
"imm32": fmt.Sprintf("$%d", math.MaxInt32), //
"imm64": fmt.Sprintf("$%d", math.MaxInt64), //
//
//
//
//
//
//
//
//
//
//
//
"r64": "R15", //
//
//
"xmm": "X7", //
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
}
return m[t]
}