File: mov.go

package info (click to toggle)
golang-github-mmcloughlin-avo 0.0~git20200523.4439b6b-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,304 kB
  • sloc: xml: 71,029; asm: 13,138; sh: 179; makefile: 35
file content (148 lines) | stat: -rw-r--r-- 3,303 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package gen

import (
	"errors"
	"fmt"
	"sort"
	"strings"

	"github.com/mmcloughlin/avo/internal/inst"
	"github.com/mmcloughlin/avo/internal/prnt"
	"github.com/mmcloughlin/avo/printer"
)

type mov struct {
	cfg printer.Config
	prnt.Generator
}

// NewMOV generates a function that will auto-select the correct MOV instruction
// based on operand types and and sizes.
func NewMOV(cfg printer.Config) Interface {
	return GoFmt(&mov{cfg: cfg})
}

func (m *mov) Generate(is []inst.Instruction) ([]byte, error) {
	m.Printf("// %s\n\n", m.cfg.GeneratedWarning())
	m.Printf("package build\n\n")

	m.Printf("import (\n")
	m.Printf("\t\"go/types\"\n")
	m.NL()
	m.Printf("\t\"%s/operand\"\n", pkg)
	m.Printf(")\n\n")

	m.Printf("func (c *Context) mov(a, b operand.Op, an, bn int, t *types.Basic) {\n")
	m.Printf("switch {\n")
	for _, i := range is {
		if ismov(i) {
			m.instruction(i)
		}
	}
	m.Printf("default:\n")
	m.Printf("c.adderrormessage(\"could not deduce mov instruction\")\n")
	m.Printf("}\n")
	m.Printf("}\n")
	return m.Result()
}

func (m *mov) instruction(i inst.Instruction) {
	f := flags(i)
	sizes, err := formsizes(i)
	if err != nil {
		m.AddError(err)
		return
	}
	for _, size := range sizes {
		conds := []string{
			fmt.Sprintf("an == %d", size.A),
			fmt.Sprintf("bn == %d", size.B),
		}
		for c, on := range f {
			cmp := map[bool]string{true: "!=", false: "=="}
			cond := fmt.Sprintf("(t.Info() & %s) %s 0", c, cmp[on])
			conds = append(conds, cond)
		}
		sort.Strings(conds)
		m.Printf("case %s:\n", strings.Join(conds, " && "))
		m.Printf("c.%s(a, b)\n", i.Opcode)
	}
}

// ismov decides whether the given instruction is a plain move instruction.
func ismov(i inst.Instruction) bool {
	if i.AliasOf != "" || !strings.HasPrefix(i.Opcode, "MOV") {
		return false
	}
	exclude := []string{"Packed", "Duplicate", "Aligned", "Hint", "Swapping"}
	for _, substring := range exclude {
		if strings.Contains(i.Summary, substring) {
			return false
		}
	}
	return true
}

func flags(i inst.Instruction) map[string]bool {
	f := map[string]bool{}
	switch {
	case strings.Contains(i.Summary, "Floating-Point"):
		f["types.IsFloat"] = true
	case strings.Contains(i.Summary, "Zero-Extend"):
		f["types.IsInteger"] = true
		f["types.IsUnsigned"] = true
	case strings.Contains(i.Summary, "Sign-Extension"):
		f["types.IsInteger"] = true
		f["types.IsUnsigned"] = false
	default:
		f["types.IsInteger"] = true
	}
	return f
}

type movsize struct{ A, B int8 }

func (s movsize) sortkey() uint16 { return (uint16(s.A) << 8) | uint16(s.B) }

func formsizes(i inst.Instruction) ([]movsize, error) {
	set := map[movsize]bool{}
	for _, f := range i.Forms {
		if f.Arity() != 2 {
			continue
		}
		s := movsize{
			A: opsize[f.Operands[0].Type],
			B: opsize[f.Operands[1].Type],
		}
		if s.A < 0 || s.B < 0 {
			continue
		}
		if s.A == 0 || s.B == 0 {
			return nil, errors.New("unknown operand type")
		}
		set[s] = true
	}
	var ss []movsize
	for s := range set {
		ss = append(ss, s)
	}
	sort.Slice(ss, func(i, j int) bool { return ss[i].sortkey() < ss[j].sortkey() })
	return ss, nil
}

var opsize = map[string]int8{
	"imm8":  -1,
	"imm16": -1,
	"imm32": -1,
	"imm64": -1,
	"r8":    1,
	"r16":   2,
	"r32":   4,
	"r64":   8,
	"xmm":   16,
	"m8":    1,
	"m16":   2,
	"m32":   4,
	"m64":   8,
	"m128":  16,
}