Debian Codegraph @

operand/checks.go

raw symbols SCIP
1 package operand
2
3 import "github.com/mmcloughlin/avo/reg"
4
5 // Pure type assertion checks:
6
7 // IsRegister returns whether op has type reg.Register.
8 func IsRegister(op Op) bool { _, ok := op.(reg.Register); return ok }
9
10 // IsMem returns whether op has type Mem.
11 func IsMem(op Op) bool { _, ok := op.(Mem); return ok }
12
13 // IsRel returns whether op has type Rel.
14 func IsRel(op Op) bool { _, ok := op.(Rel); return ok }
15
16 // Checks corresponding to specific operand types in the Intel Manual:
17
18 // Is1 returns true if op is the immediate constant 1.
19 func Is1(op Op) bool {
20 i, ok := op.(U8)
21 return ok && i == 1
22 }
23
24 // Is3 returns true if op is the immediate constant 3.
25 func Is3(op Op) bool {
26 i, ok := op.(U8)
27 return ok && i == 3
28 }
29
30 // IsIMM2U returns true if op is a 2-bit unsigned immediate (less than 4).
31 func IsIMM2U(op Op) bool {
32 i, ok := op.(U8)
33 return ok && i < 4
34 }
35
36 // IsIMM8 returns true is op is an 8-bit immediate.
37 func IsIMM8(op Op) bool {
38 _, uok := op.(U8)
39 _, iok := op.(I8)
40 return uok || iok
41 }
42
43 // IsIMM16 returns true is op is a 16-bit immediate.
44 func IsIMM16(op Op) bool {
45 _, uok := op.(U16)
46 _, iok := op.(I16)
47 return uok || iok
48 }
49
50 // IsIMM32 returns true is op is a 32-bit immediate.
51 func IsIMM32(op Op) bool {
52 _, uok := op.(U32)
53 _, iok := op.(I32)
54 return uok || iok
55 }
56
57 // IsIMM64 returns true is op is a 64-bit immediate.
58 func IsIMM64(op Op) bool {
59 _, uok := op.(U64)
60 _, iok := op.(I64)
61 return uok || iok
62 }
63
64 // IsAL returns true if op is the AL register.
65 func IsAL(op Op) bool {
66 return op == reg.AL
67 }
68
69 // IsCL returns true if op is the CL register.
70 func IsCL(op Op) bool {
71 return op == reg.CL
72 }
73
74 // IsAX returns true if op is the 16-bit AX register.
75 func IsAX(op Op) bool {
76 return op == reg.AX
77 }
78
79 // IsEAX returns true if op is the 32-bit EAX register.
80 func IsEAX(op Op) bool {
81 return op == reg.EAX
82 }
83
84 // IsRAX returns true if op is the 64-bit RAX register.
85 func IsRAX(op Op) bool {
86 return op == reg.RAX
87 }
88
89 // IsR8 returns true if op is an 8-bit general-purpose register.
90 func IsR8(op Op) bool {
91 return IsGP(op, 1)
92 }
93
94 // IsR16 returns true if op is a 16-bit general-purpose register.
95 func IsR16(op Op) bool {
96 return IsGP(op, 2)
97 }
98
99 // IsR32 returns true if op is a 32-bit general-purpose register.
100 func IsR32(op Op) bool {
101 return IsGP(op, 4)
102 }
103
104 // IsR64 returns true if op is a 64-bit general-purpose register.
105 func IsR64(op Op) bool {
106 return IsGP(op, 8)
107 }
108
109 // IsPseudo returns true if op is a pseudo register.
110 func IsPseudo(op Op) bool {
111 return IsRegisterKind(op, reg.KindPseudo)
112 }
113
114 // IsGP returns true if op is a general-purpose register of size n bytes.
115 func IsGP(op Op, n uint) bool {
116 return IsRegisterKindSize(op, reg.KindGP, n)
117 }
118
119 // IsXMM0 returns true if op is the X0 register.
120 func IsXMM0(op Op) bool {
121 return op == reg.X0
122 }
123
124 // IsXMM returns true if op is a 128-bit XMM register.
125 func IsXMM(op Op) bool {
126 return IsRegisterKindSize(op, reg.KindVector, 16)
127 }
128
129 // IsYMM returns true if op is a 256-bit YMM register.
130 func IsYMM(op Op) bool {
131 return IsRegisterKindSize(op, reg.KindVector, 32)
132 }
133
134 // IsZMM returns true if op is a 512-bit ZMM register.
135 func IsZMM(op Op) bool {
136 return IsRegisterKindSize(op, reg.KindVector, 64)
137 }
138
139 // IsK returns true if op is an Opmask register.
140 func IsK(op Op) bool {
141 return IsRegisterKind(op, reg.KindOpmask)
142 }
143
144 // IsRegisterKindSize returns true if op is a register of the given kind and size in bytes.
145 func IsRegisterKindSize(op Op, k reg.Kind, n uint) bool {
146 r, ok := op.(reg.Register)
147 return ok && r.Kind() == k && r.Size() == n
148 }
149
150 // IsRegisterKind returns true if op is a register of the given kind.
151 func IsRegisterKind(op Op, k reg.Kind) bool {
152 r, ok := op.(reg.Register)
153 return ok && r.Kind() == k
154 }
155
156 // IsM returns true if op is a 16-, 32- or 64-bit memory operand.
157 func IsM(op Op) bool {
158 // TODO(mbm): confirm "m" check is defined correctly
159 // Intel manual: "A 16-, 32- or 64-bit operand in memory."
160 return IsM16(op) || IsM32(op) || IsM64(op)
161 }
162
163 // IsM8 returns true if op is an 8-bit memory operand.
164 func IsM8(op Op) bool {
165 // TODO(mbm): confirm "m8" check is defined correctly
166 // Intel manual: "A byte operand in memory, usually expressed as a variable or
167 // array name, but pointed to by the DS:(E)SI or ES:(E)DI registers. In 64-bit
168 // mode, it is pointed to by the RSI or RDI registers."
169 return IsMSize(op, 1)
170 }
171
172 // IsM16 returns true if op is a 16-bit memory operand.
173 func IsM16(op Op) bool {
174 return IsMSize(op, 2)
175 }
176
177 // IsM32 returns true if op is a 16-bit memory operand.
178 func IsM32(op Op) bool {
179 return IsMSize(op, 4)
180 }
181
182 // IsM64 returns true if op is a 64-bit memory operand.
183 func IsM64(op Op) bool {
184 return IsMSize(op, 8)
185 }
186
187 // IsMSize returns true if op is a memory operand using general-purpose address
188 // registers of the given size in bytes.
189 func IsMSize(op Op, n uint) bool {
190 // TODO(mbm): should memory operands have a size attribute as well?
191 // TODO(mbm): m8,m16,m32,m64 checks do not actually check size
192 m, ok := op.(Mem)
193 return ok && IsMReg(m.Base) && (m.Index == nil || IsMReg(m.Index))
194 }
195
196 // IsMReg returns true if op is a register that can be used in a memory operand.
197 func IsMReg(op Op) bool {
198 return IsPseudo(op) || IsRegisterKind(op, reg.KindGP)
199 }
200
201 // IsM128 returns true if op is a 128-bit memory operand.
202 func IsM128(op Op) bool {
203 // TODO(mbm): should "m128" be the same as "m64"?
204 return IsM64(op)
205 }
206
207 // IsM256 returns true if op is a 256-bit memory operand.
208 func IsM256(op Op) bool {
209 // TODO(mbm): should "m256" be the same as "m64"?
210 return IsM64(op)
211 }
212
213 // IsM512 returns true if op is a 512-bit memory operand.
214 func IsM512(op Op) bool {
215 // TODO(mbm): should "m512" be the same as "m64"?
216 return IsM64(op)
217 }
218
219 // IsVM32X returns true if op is a vector memory operand with 32-bit XMM index.
220 func IsVM32X(op Op) bool {
221 return IsVmx(op)
222 }
223
224 // IsVM64X returns true if op is a vector memory operand with 64-bit XMM index.
225 func IsVM64X(op Op) bool {
226 return IsVmx(op)
227 }
228
229 // IsVmx returns true if op is a vector memory operand with XMM index.
230 func IsVmx(op Op) bool {
231 return isvm(op, IsXMM)
232 }
233
234 // IsVM32Y returns true if op is a vector memory operand with 32-bit YMM index.
235 func IsVM32Y(op Op) bool {
236 return IsVmy(op)
237 }
238
239 // IsVM64Y returns true if op is a vector memory operand with 64-bit YMM index.
240 func IsVM64Y(op Op) bool {
241 return IsVmy(op)
242 }
243
244 // IsVmy returns true if op is a vector memory operand with YMM index.
245 func IsVmy(op Op) bool {
246 return isvm(op, IsYMM)
247 }
248
249 // IsVM32Z returns true if op is a vector memory operand with 32-bit ZMM index.
250 func IsVM32Z(op Op) bool {
251 return IsVmz(op)
252 }
253
254 // IsVM64Z returns true if op is a vector memory operand with 64-bit ZMM index.
255 func IsVM64Z(op Op) bool {
256 return IsVmz(op)
257 }
258
259 // IsVmz returns true if op is a vector memory operand with ZMM index.
260 func IsVmz(op Op) bool {
261 return isvm(op, IsZMM)
262 }
263
264 func isvm(op Op, idx func(Op) bool) bool {
265 m, ok := op.(Mem)
266 return ok && IsR64(m.Base) && idx(m.Index)
267 }
268
269 // IsREL8 returns true if op is an 8-bit offset relative to instruction pointer.
270 func IsREL8(op Op) bool {
271 r, ok := op.(Rel)
272 return ok && r == Rel(int8(r))
273 }
274
275 // IsREL32 returns true if op is an offset relative to instruction pointer, or a
276 // label reference.
277 func IsREL32(op Op) bool {
278 // TODO(mbm): should labels be considered separately?
279 _, rel := op.(Rel)
280 _, label := op.(LabelRef)
281 return rel || label
282 }