Debian Codegraph @

internal/gen/asmtest.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package gen
2
3 import (
4 "fmt"
5 "math"
6 "strconv"
7 "strings"
8
9 "github.com/mmcloughlin/avo/internal/inst"
10 "github.com/mmcloughlin/avo/internal/prnt"
11 "github.com/mmcloughlin/avo/printer"
12 )
13
14 type asmtest struct {
15 cfg printer.Config
16 sym string // reference to the test function symbol
17 rel8 string // label to be used for near jumps
18 rel32 string // label for far jumps
19 prnt.Generator
20 }
21
22 // NewAsmTest prints one massive assembly function containing a line for every
23 // instruction form in the database. The intention is to pass this to the Go
24 // assembler and confirm there are no errors, thus helping to ensure our
25 // database is compatible.
26 func NewAsmTest(cfg printer.Config) Interface {
27 return &asmtest{cfg: cfg}
28 }
29
30 func (a *asmtest) Generate(is []inst.Instruction) ([]byte, error) {
31 a.Printf("// %s\n\n", a.cfg.GeneratedWarning())
32
33 a.sym = "\u00b7loadertest(SB)"
34 a.Printf("TEXT %s, 0, $0\n", a.sym)
35
36 // Define a label for far jumps.
37 a.Printf("rel32:\n")
38 a.rel32 = "rel32"
39
40 for _, i := range is {
41 a.Printf("\t// %s %s\n", i.Opcode, i.Summary)
42 if skip, msg := a.skip(i.Opcode); skip {
43 a.Printf("\t// SKIP: %s\n", msg)
44 continue
45 }
46
47 if i.Opcode[0] == 'J' {
48 label := fmt.Sprintf("rel8_%s", strings.ToLower(i.Opcode))
49 a.Printf("%s:\n", label)
50 a.rel8 = label
51 }
52
53 for _, f := range i.Forms {
54 as, err := a.args(i.Opcode, f.Operands)
55 if err != nil {
56 return nil, fmt.Errorf("tests for %s: %w", i.Opcode, err)
57 }
58 for _, suffixes := range f.SupportedSuffixes() {
59 opcode := i.Opcode
60 if len(suffixes) > 0 {
61 opcode += "." + suffixes.String()
62 }
63 a.Printf("\t%s\t%s\n", opcode, strings.Join(as, ", "))
64 }
65 }
66 a.Printf("\n")
67 }
68
69 a.Printf("\tRET\n")
70
71 return a.Result()
72 }
73
74 func (a asmtest) skip(opcode string) (bool, string) {
75 prefixes := map[string]string{
76 "PUSH": "PUSH can produce 'unbalanced PUSH/POP' assembler error",
77 "POP": "POP can produce 'unbalanced PUSH/POP' assembler error",
78 }
79 for p, m := range prefixes {
80 if strings.HasPrefix(opcode, p) {
81 return true, m
82 }
83 }
84 return false, ""
85 }
86
87 func (a asmtest) args(opcode string, ops []inst.Operand) ([]string, error) {
88 // Special case for CALL, since it needs a different type of rel32 argument than others.
89 if opcode == "CALL" {
90 return []string{a.sym}, nil
91 }
92
93 as := make([]string, len(ops))
94 for i, op := range ops {
95 a := a.arg(op.Type, i)
96 if a == "" {
97 return nil, fmt.Errorf("unsupported operand type %q", op.Type)
98 }
99 as[i] = a
100 }
101 return as, nil
102 }
103
104 // arg generates an argument for an operand of the given type.
105 func (a asmtest) arg(t string, i int) string {
106 m := map[string]string{
107 "1": "$1", // <xs:enumeration value="1" />
108 "3": "$3", // <xs:enumeration value="3" />
109 "imm2u": "$3",
110 // <xs:enumeration value="imm4" />
111 "imm8": fmt.Sprintf("$%d", math.MaxInt8), // <xs:enumeration value="imm8" />
112 "imm16": fmt.Sprintf("$%d", math.MaxInt16), // <xs:enumeration value="imm16" />
113 "imm32": fmt.Sprintf("$%d", math.MaxInt32), // <xs:enumeration value="imm32" />
•114 "imm64": fmt.Sprintf("$%d", int64(math.MaxInt64)), // <xs:enumeration value="imm64" />
115
116 "al": "AL", // <xs:enumeration value="al" />
117 "cl": "CL", // <xs:enumeration value="cl" />
118 "r8": "CH", // <xs:enumeration value="r8" />
119 "ax": "AX", // <xs:enumeration value="ax" />
120 "r16": "SI", // <xs:enumeration value="r16" />
121 "eax": "AX", // <xs:enumeration value="eax" />
122 "r32": "DX", // <xs:enumeration value="r32" />
123 "rax": "AX", // <xs:enumeration value="rax" />
124 "r64": "R15", // <xs:enumeration value="r64" />
125 "mm": "M5", // <xs:enumeration value="mm" />
126 "xmm0": "X0", // <xs:enumeration value="xmm0" />
127 "xmm": "X" + strconv.Itoa(7+i), // <xs:enumeration value="xmm" />
128 // <xs:enumeration value="xmm{k}" />
129 // <xs:enumeration value="xmm{k}{z}" />
130 "ymm": "Y" + strconv.Itoa(3+i), // <xs:enumeration value="ymm" />
131 // <xs:enumeration value="ymm{k}" />
132 // <xs:enumeration value="ymm{k}{z}" />
133 "zmm": "Z" + strconv.Itoa(16+i), // <xs:enumeration value="zmm" />
134 // <xs:enumeration value="zmm{k}" />
135 // <xs:enumeration value="zmm{k}{z}" />
136 "k": "K" + strconv.Itoa(1+i), // <xs:enumeration value="k" />
137 // <xs:enumeration value="k{k}" />
138 // <xs:enumeration value="moffs32" />
139 // <xs:enumeration value="moffs64" />
140 "m": "0(AX)(CX*2)", // <xs:enumeration value="m" />
141 "m8": "8(AX)(CX*2)", // <xs:enumeration value="m8" />
142 "m16": "16(AX)(CX*2)", // <xs:enumeration value="m16" />
143 // <xs:enumeration value="m16{k}{z}" />
144 "m32": "32(AX)(CX*2)", // <xs:enumeration value="m32" />
145 // <xs:enumeration value="m32{k}" />
146 // <xs:enumeration value="m32{k}{z}" />
147 "m64": "64(AX)(CX*2)", // <xs:enumeration value="m64" />
148 // <xs:enumeration value="m64{k}" />
149 // <xs:enumeration value="m64{k}{z}" />
150 "m128": "128(AX)(CX*2)", // <xs:enumeration value="m128" />
151 // <xs:enumeration value="m128{k}{z}" />
152 "m256": "256(AX)(CX*2)", // <xs:enumeration value="m256" />
153 // <xs:enumeration value="m256{k}{z}" />
154 "m512": "512(AX)(CX*2)", // <xs:enumeration value="m512" />
155 // <xs:enumeration value="m512{k}{z}" />
156 "vm32x": "32(X14*8)", // <xs:enumeration value="vm32x" />
157 // <xs:enumeration value="vm32x{k}" />
158 "vm64x": "64(X14*8)", // <xs:enumeration value="vm64x" />
159 // <xs:enumeration value="vm64x{k}" />
160 "vm32y": "32(Y13*8)", // <xs:enumeration value="vm32y" />
161 // <xs:enumeration value="vm32y{k}" />
162 "vm64y": "64(Y13*8)", // <xs:enumeration value="vm64y" />
163 // <xs:enumeration value="vm64y{k}" />
164 "vm32z": "32(Z13*8)", // <xs:enumeration value="vm32z" />
165 // <xs:enumeration value="vm32z{k}" />
166 "vm64z": "64(Z13*8)", // <xs:enumeration value="vm64z" />
167 // <xs:enumeration value="vm64z{k}" />
168 "rel8": a.rel8, // <xs:enumeration value="rel8" />
169 "rel32": a.rel32, // <xs:enumeration value="rel32" />
170 // <xs:enumeration value="{er}" />
171 // <xs:enumeration value="{sae}" />
172
173 // Appear unused:
174 "r8l": "????", // <xs:enumeration value="r8l" />
175 "r16l": "????", // <xs:enumeration value="r16l" />
176 "r32l": "????", // <xs:enumeration value="r32l" />
177 }
178 return m[t]
179 }