Files
avo/internal/gen/loadertest.go

180 lines
5.5 KiB
Go
Raw Normal View History

2018-11-21 13:02:18 -06:00
package gen
import (
2018-11-21 23:06:29 -06:00
"fmt"
2018-11-21 13:02:18 -06:00
"io"
2018-11-21 23:06:29 -06:00
"math"
2018-11-21 13:02:18 -06:00
"strings"
"github.com/mmcloughlin/avo/internal/inst"
)
2018-11-22 16:21:05 -06:00
type LoaderTest struct {
sym string // reference to the test function symbol
rel8 string // label to be used for near jumps
rel32 string // label for far jumps
}
2018-11-21 13:02:18 -06:00
2018-11-22 16:21:05 -06:00
func (l *LoaderTest) Generate(w io.Writer, is []*inst.Instruction) error {
2018-11-21 13:02:18 -06:00
p := &printer{w: w}
2018-11-22 16:21:05 -06:00
l.sym = "\u00b7loadertest(SB)"
p.printf("TEXT %s, 0, $0\n", l.sym)
// Define a label for far jumps.
p.printf("rel32:\n")
l.rel32 = "rel32"
2018-11-21 13:02:18 -06:00
2018-11-22 14:12:20 -06:00
counts := map[string]int{}
2018-11-21 13:02:18 -06:00
for _, i := range is {
p.printf("\t// %s %s\n", i.Opcode, i.Summary)
2018-11-21 23:06:29 -06:00
if skip, msg := l.skip(i.Opcode); skip {
p.printf("\t// SKIP: %s\n", msg)
2018-11-22 14:12:20 -06:00
counts["skip"]++
2018-11-21 22:28:55 -06:00
continue
}
2018-11-22 16:21:05 -06:00
if i.Opcode[0] == 'J' {
label := fmt.Sprintf("rel8_%s", strings.ToLower(i.Opcode))
p.printf("%s:\n", label)
l.rel8 = label
}
2018-11-21 13:02:18 -06:00
for _, f := range i.Forms {
2018-11-22 16:21:05 -06:00
as := l.args(i.Opcode, f.Operands)
2018-11-21 13:02:18 -06:00
p.printf("\t// %#v\n", f.Operands)
if as == nil {
2018-11-21 23:06:29 -06:00
p.printf("\t// TODO\n")
2018-11-22 14:12:20 -06:00
counts["todo"]++
2018-11-21 13:02:18 -06:00
continue
}
p.printf("\t%s\t%s\n", i.Opcode, strings.Join(as, ", "))
2018-11-22 14:12:20 -06:00
counts["total"]++
2018-11-21 13:02:18 -06:00
}
p.printf("\n")
}
p.printf("\tRET\n")
2018-11-22 14:12:20 -06:00
for m, c := range counts {
p.printf("// %s: %d\n", m, c)
}
2018-11-21 13:02:18 -06:00
return p.Err()
}
2018-11-21 23:06:29 -06:00
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, ""
}
2018-11-22 16:21:05 -06:00
func (l LoaderTest) args(opcode string, ops []inst.Operand) []string {
// Special case for CALL, since it needs a different type of rel32 argument than others.
if opcode == "CALL" {
return []string{l.sym}
}
2018-11-21 13:02:18 -06:00
as := make([]string, len(ops))
for i, op := range ops {
2018-11-22 16:21:05 -06:00
a := l.arg(op.Type)
2018-11-21 13:02:18 -06:00
if a == "" {
return nil
}
as[i] = a
}
return as
}
// arg generates an argument for an operand of the given type.
2018-11-22 16:21:05 -06:00
func (l LoaderTest) arg(t string) string {
2018-11-21 13:02:18 -06:00
m := map[string]string{
2018-11-22 14:12:20 -06:00
"1": "$1", // <xs:enumeration value="1" />
"3": "$3", // <xs:enumeration value="3" />
2018-11-22 11:17:46 -06:00
"imm2u": "$3",
2018-11-21 13:02:18 -06:00
// <xs:enumeration value="imm4" />
2018-11-22 11:17:46 -06:00
"imm8": fmt.Sprintf("$%d", math.MaxInt8), // <xs:enumeration value="imm8" />
"imm16": fmt.Sprintf("$%d", math.MaxInt16), // <xs:enumeration value="imm16" />
2018-11-21 23:06:29 -06:00
"imm32": fmt.Sprintf("$%d", math.MaxInt32), // <xs:enumeration value="imm32" />
"imm64": fmt.Sprintf("$%d", math.MaxInt64), // <xs:enumeration value="imm64" />
2018-11-22 14:58:31 -06:00
2018-11-22 15:24:28 -06:00
"al": "AL", // <xs:enumeration value="al" />
"cl": "CL", // <xs:enumeration value="cl" />
"r8": "CH", // <xs:enumeration value="r8" />
"ax": "AX", // <xs:enumeration value="ax" />
"r16": "SI", // <xs:enumeration value="r16" />
"eax": "AX", // <xs:enumeration value="eax" />
"r32": "DX", // <xs:enumeration value="r32" />
"rax": "AX", // <xs:enumeration value="rax" />
"r64": "R15", // <xs:enumeration value="r64" />
"mm": "M5", // <xs:enumeration value="mm" />
"xmm0": "X0", // <xs:enumeration value="xmm0" />
"xmm": "X7", // <xs:enumeration value="xmm" />
2018-11-21 13:02:18 -06:00
// <xs:enumeration value="xmm{k}" />
// <xs:enumeration value="xmm{k}{z}" />
2018-11-22 14:58:31 -06:00
"ymm": "Y13", // <xs:enumeration value="ymm" />
2018-11-21 13:02:18 -06:00
// <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" />
2018-11-22 14:12:20 -06:00
"m8": "8(AX)(CX*2)", // <xs:enumeration value="m8" />
"m16": "16(AX)(CX*2)", // <xs:enumeration value="m16" />
2018-11-21 13:02:18 -06:00
// <xs:enumeration value="m16{k}{z}" />
2018-11-22 14:12:20 -06:00
"m32": "32(AX)(CX*2)", // <xs:enumeration value="m32" />
2018-11-21 13:02:18 -06:00
// <xs:enumeration value="m32{k}" />
// <xs:enumeration value="m32{k}{z}" />
2018-11-22 14:12:20 -06:00
"m64": "64(AX)(CX*2)", // <xs:enumeration value="m64" />
2018-11-21 13:02:18 -06:00
// <xs:enumeration value="m64{k}" />
// <xs:enumeration value="m64{k}{z}" />
2018-11-22 14:12:20 -06:00
"m128": "128(AX)(CX*2)", // <xs:enumeration value="m128" />
2018-11-21 13:02:18 -06:00
// <xs:enumeration value="m128{k}{z}" />
2018-11-22 15:24:28 -06:00
"m256": "256(AX)(CX*2)", // <xs:enumeration value="m256" />
2018-11-21 13:02:18 -06:00
// <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}" />
2018-11-22 16:21:05 -06:00
"rel8": l.rel8, // <xs:enumeration value="rel8" />
"rel32": l.rel32, // <xs:enumeration value="rel32" />
2018-11-21 13:02:18 -06:00
// <xs:enumeration value="{er}" />
// <xs:enumeration value="{sae}" />
2018-11-22 14:58:31 -06:00
// Appear unused:
"r8l": "????", // <xs:enumeration value="r8l" />
"r16l": "????", // <xs:enumeration value="r16l" />
"r32l": "????", // <xs:enumeration value="r32l" />
2018-11-21 13:02:18 -06:00
}
return m[t]
}