Debian Codegraph @

internal/gen/mov.go

raw symbols SCIP
1 package gen
2
3 import (
4 "errors"
5 "fmt"
6 "strings"
7
8 "github.com/mmcloughlin/avo/internal/api"
9 "github.com/mmcloughlin/avo/internal/inst"
10 "github.com/mmcloughlin/avo/internal/prnt"
11 "github.com/mmcloughlin/avo/printer"
12 )
13
14 type mov struct {
15 cfg printer.Config
16 prnt.Generator
17 }
18
19 // NewMOV generates a function that will auto-select the correct MOV instruction
20 // based on operand types and sizes.
21 func NewMOV(cfg printer.Config) Interface {
22 return GoFmt(&mov{cfg: cfg})
23 }
24
25 func (m *mov) Generate(is []inst.Instruction) ([]byte, error) {
26 m.Printf("// %s\n\n", m.cfg.GeneratedWarning())
27 m.Printf("package build\n\n")
28
29 m.Printf("import (\n")
30 m.Printf("\t\"go/types\"\n")
31 m.NL()
32 m.Printf("\t%q\n", api.ImportPath(api.OperandPackage))
33 m.Printf(")\n\n")
34
35 m.Printf("func (c *Context) mov(a, b operand.Op, an, bn int, t *types.Basic) {\n")
36 m.Printf("switch {\n")
37 for _, i := range is {
38 if ismov(i) {
39 m.instruction(i)
40 }
41 }
42 m.Printf("default:\n")
43 m.Printf("c.adderrormessage(\"could not deduce mov instruction\")\n")
44 m.Printf("}\n")
45 m.Printf("}\n")
46 return m.Result()
47 }
48
49 func (m *mov) instruction(i inst.Instruction) {
50 tcs := typecases(i)
51 mfs, err := movforms(i)
52 if err != nil {
53 m.AddError(err)
54 return
55 }
56 for _, mf := range mfs {
57 conds := []string{
58 fmt.Sprintf("an == %d", opsize[mf.A]),
59 fmt.Sprintf("%s(a)", api.CheckerName(mf.A)),
60 fmt.Sprintf("bn == %d", opsize[mf.B]),
61 fmt.Sprintf("%s(b)", api.CheckerName(mf.B)),
62 }
63
64 for _, tc := range tcs {
65 typecase := fmt.Sprintf("(t.Info() & %s) %s %s", tc.Mask, tc.Op, tc.Value)
66 m.Printf("case %s && %s:\n", strings.Join(conds, " && "), typecase)
67 m.Printf("c.%s(a, b)\n", i.Opcode)
68 }
69 }
70 }
71
72 // ismov decides whether the given instruction is a plain move instruction.
73 func ismov(i inst.Instruction) bool {
74 // Ignore aliases.
75 if i.AliasOf != "" {
76 return false
77 }
78
79 // Accept specific move instruction prefixes.
80 prefixes := []string{"MOV", "KMOV", "VMOV"}
81 accept := false
82 for _, prefix := range prefixes {
83 accept = strings.HasPrefix(i.Opcode, prefix) || accept
84 }
85 if !accept {
86 return false
87 }
88
89 // Exclude some cases based on instruction descriptions.
90 exclude := []string{"Packed", "Duplicate", "Aligned", "Hint", "Swapping"}
91 for _, substring := range exclude {
92 if strings.Contains(i.Summary, substring) {
93 return false
94 }
95 }
96
97 return true
98 }
99
100 type typecase struct {
101 Mask string
102 Op string
103 Value string
104 }
105
106 func typecases(i inst.Instruction) []typecase {
107 switch {
108 case strings.Contains(i.Summary, "Floating-Point"):
109 return []typecase{
110 {"types.IsFloat", "!=", "0"},
111 }
112 case strings.Contains(i.Summary, "Zero-Extend"):
113 return []typecase{
114 {"(types.IsInteger|types.IsUnsigned)", "==", "(types.IsInteger|types.IsUnsigned)"},
115 {"types.IsBoolean", "!=", "0"},
116 }
117 case strings.Contains(i.Summary, "Sign-Extension"):
118 return []typecase{
119 {"(types.IsInteger|types.IsUnsigned)", "==", "types.IsInteger"},
120 }
121 default:
122 return []typecase{
123 {"(types.IsInteger|types.IsBoolean)", "!=", "0"},
124 }
125 }
126 }
127
128 type movform struct{ A, B string }
129
130 func movforms(i inst.Instruction) ([]movform, error) {
131 var mfs []movform
132 for _, f := range i.Forms {
133 if f.Arity() != 2 {
134 continue
135 }
136 mf := movform{
137 A: f.Operands[0].Type,
138 B: f.Operands[1].Type,
139 }
140 if opsize[mf.A] < 0 || opsize[mf.B] < 0 {
141 continue
142 }
143 if opsize[mf.A] == 0 || opsize[mf.B] == 0 {
144 return nil, errors.New("unknown operand type")
145 }
146 mfs = append(mfs, mf)
147 }
148 return mfs, nil
149 }
150
151 var opsize = map[string]int8{
152 "imm8": -1,
153 "imm16": -1,
154 "imm32": -1,
155 "imm64": -1,
156 "r8": 1,
157 "r16": 2,
158 "r32": 4,
159 "r64": 8,
160 "xmm": 16,
161 "ymm": 32,
162 "zmm": 64,
163 "m8": 1,
164 "m16": 2,
165 "m32": 4,
166 "m64": 8,
167 "m128": 16,
168 "m256": 32,
169 "m512": 64,
170 "k": 8,
171 }