-
Notifications
You must be signed in to change notification settings - Fork 1
/
asm_match.go
198 lines (181 loc) · 4.64 KB
/
asm_match.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package arm
// matchPattern returns true if the encoding at the current iterator position matches.
func (a *Assembler) matchPattern() bool {
a.SimdSize = 0
args, pattern, required, optional := a.Args, a.Pattern(), int(a.patternLen), 0
for i, m := range pattern {
if m.Op == MatEnd {
required = i
optional = int(a.patternLen) - (i + 1) // skip MatEnd
break
}
}
if len(args) != required && len(args) != required+optional {
return false
}
if !a.matchArgSlice(args[:required], pattern[:required]) {
return false
}
if len(args) == required || optional == 0 {
return true
}
return a.matchArgSlice(args[required:], pattern[required+1:]) // skip MatEnd
}
func (a *Assembler) matchArgSlice(args []Arg, pattern []EncOp) bool {
for i, m := range pattern {
if !a.matchArg(args[i], m) {
return false
}
}
return true
}
func (a *Assembler) matchArg(arg Arg, m EncOp) bool {
switch arg := arg.(type) {
case Reg:
if !checkReg(arg) {
return false
}
switch m.Op {
// scalar
case MatW:
return arg.Type == RW
case MatX:
return arg.Type == RX
case MatWSP:
return arg.Type == RWSP || (arg.Type == RW && arg != WZR)
case MatXSP:
return arg.Type == RXSP || (arg.Type == RX && arg != XZR)
// simd
case MatB:
return arg.Type == RB
case MatH:
return arg.Type == RH
case MatS:
return arg.Type == RS
case MatD:
return arg.Type == RD
case MatQ:
return arg.Type == RQ
case MatV:
return !arg.HasElem() && arg.IsVec() && arg.ElemSize() == Size(m.X[0]) && a.matchOrSetSimdSize(arg)
case MatVStatic:
return !arg.HasElem() && arg.IsVec() && arg.ElemSize() == Size(m.X[0]) && arg.Lanes() == m.X[1]
case MatVStaticElement:
return arg.HasElem() && arg.ElemSize() == Size(m.X[0]) && arg.Lanes() == m.X[1]
case MatVElement:
return arg.HasElem() && arg.ElemSize() == Size(m.X[0])
case MatVElementStatic:
return arg.HasElem() && arg.ElemSize() == Size(m.X[0]) && arg.GetElem() == m.X[1]
}
case RegList:
if !checkReg(arg.First) {
return false
}
switch m.Op {
case MatRegList:
return !arg.First.HasElem() && arg.Len == m.X[0] && arg.First.ElemSize() == Size(m.X[1]) && a.matchOrSetSimdSize(arg.First)
case MatRegListStatic:
return !arg.First.HasElem() && arg.Len == m.X[0] && arg.First.ElemSize() == Size(m.X[1]) && arg.First.Lanes() == m.X[2]
case MatRegListElement:
return arg.First.HasElem() && arg.Len == m.X[0] && arg.First.ElemSize() == Size(m.X[1])
}
case Imm:
switch m.Op {
case MatImm, MatOffset:
return true
case MatLitInt:
return arg == Imm(m.X[0])
}
case Wide:
switch m.Op {
case MatImm, MatOffset:
return true
case MatLitInt:
return arg == Wide(m.X[0])
}
case Float:
switch m.Op {
case MatFloat:
return true
case MatLitFloat:
return arg == Float(m.X[0])
}
case Mod:
switch m.Op {
case MatMod:
return checkMod(ModList[m.X[0]], arg.ID)
case MatLitMod:
return arg.ID == m.X[0]
}
case Ref:
return (m.Op == MatRefBase || m.Op == MatRefOffset) && checkReg(arg.Base) && checkRefBase(arg.Base)
case RefOffset:
return m.Op == MatRefOffset && checkReg(arg.Base) && checkRefBase(arg.Base)
case RefPreIndexed:
return m.Op == MatRefPre && checkReg(arg.Base) && checkRefBase(arg.Base)
case RefIndexed:
return m.Op == MatRefIndex && checkReg(arg.Base) && checkReg(arg.Idx) && checkRefBase(arg.Base) && arg.Idx.Family() == RegInt
case Label:
if int(arg.ID) >= len(a.LabelPC) {
return false
}
return m.Op == MatOffset
case Symbol:
switch m.Op {
case MatSymbol, MatCond:
return true
case MatLitSymbol:
return arg == Symbol(m.X[0])
}
}
return false
}
func checkReg(r Reg) bool {
switch r.Family() {
case RegInt:
switch r.Type {
case RW, RX:
return r.ID < 32 && !r.HasElem()
}
case RegSP:
switch r.Type {
case RWSP, RXSP:
return r.ID == 31 && !r.HasElem()
}
case RegFloat:
switch r.Type {
case RB, RH, RS, RD, RQ:
return r.ID < 32 && !r.HasElem()
}
case RegVec32:
switch r.Type {
case V4B, V2H:
return r.ID < 32 && (!r.HasElem() || r.GetElem() < r.Lanes())
}
case RegVec64:
switch r.Type {
case V8B, V4H, V2S, V1D:
return r.ID < 32 && (!r.HasElem() || r.GetElem() < r.Lanes())
}
case RegVec128:
switch r.Type {
case V16B, V8H, V4S, V2D, V1Q:
return r.ID < 32 && (!r.HasElem() || r.GetElem() < r.Lanes())
}
}
return false
}
func checkRefBase(r Reg) bool { return r.Family() == RegInt || r.Family() == RegSP }
func (a *Assembler) matchOrSetSimdSize(reg Reg) bool {
switch reg.Family() {
case RegInt, RegSP, RegFloat:
return true
default:
regSize := reg.Type.Bytes()
if a.SimdSize != 0 {
return a.SimdSize == regSize
}
a.SimdSize = regSize
return true
}
}