asmkit-rs 0.3.1

Portable assembler toolkit: decoding and encoding of various architectures
Documentation
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use crate::x86::assembler::*;
use crate::x86::operands::*;
use super::super::opcodes::*;
use crate::core::emitter::*;
use crate::core::operand::*;

/// A dummy operand that represents no register. Here just for simplicity.
const NOREG: Operand = Operand::new();

/// `ANDN` (ANDN). 
/// Performs a bitwise logical AND of inverted second operand (the first source operand) with the third operand (the
///
///
/// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/ANDN.html).
///
/// Supported operand variants:
///
/// ```text
/// +---+---------------+
/// | # | Operands      |
/// +---+---------------+
/// | 1 | Gpd, Gpd, Gpd |
/// | 2 | Gpd, Gpd, Mem |
/// | 3 | Gpq, Gpq, Gpq |
/// | 4 | Gpq, Gpq, Mem |
/// +---+---------------+
/// ```
pub trait AndnEmitter<A, B, C> {
    fn andn(&mut self, op0: A, op1: B, op2: C);
}

impl<'a> AndnEmitter<Gpd, Gpd, Gpd> for Assembler<'a> {
    fn andn(&mut self, op0: Gpd, op1: Gpd, op2: Gpd) {
        self.emit(ANDN32RRR, op0.as_operand(), op1.as_operand(), op2.as_operand(), &NOREG);
    }
}

impl<'a> AndnEmitter<Gpd, Gpd, Mem> for Assembler<'a> {
    fn andn(&mut self, op0: Gpd, op1: Gpd, op2: Mem) {
        self.emit(ANDN32RRM, op0.as_operand(), op1.as_operand(), op2.as_operand(), &NOREG);
    }
}

impl<'a> AndnEmitter<Gpq, Gpq, Gpq> for Assembler<'a> {
    fn andn(&mut self, op0: Gpq, op1: Gpq, op2: Gpq) {
        self.emit(ANDN64RRR, op0.as_operand(), op1.as_operand(), op2.as_operand(), &NOREG);
    }
}

impl<'a> AndnEmitter<Gpq, Gpq, Mem> for Assembler<'a> {
    fn andn(&mut self, op0: Gpq, op1: Gpq, op2: Mem) {
        self.emit(ANDN64RRM, op0.as_operand(), op1.as_operand(), op2.as_operand(), &NOREG);
    }
}

/// `BEXTR` (BEXTR). 
/// Extracts contiguous bits from the first source operand (the second operand) using an index value and length value specified in the second source operand (the third operand). Bit 7:0 of the second source operand specifies the starting bit position of bit extraction. A START value exceeding the operand size will not extract any bits from the second source operand. Bit 15:8 of the second source operand specifies the maximum number of bits (LENGTH) beginning at the START position to extract. Only bit positions up to (OperandSize -1) of the first source operand are extracted. The extracted bits are written to the destination register, starting from the least significant bit. All higher order bits in the destination operand (starting at bit position LENGTH) are zeroed. The destination register is cleared if no bits are extracted.
///
///
/// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/BEXTR.html).
///
/// Supported operand variants:
///
/// ```text
/// +---+---------------+
/// | # | Operands      |
/// +---+---------------+
/// | 1 | Gpd, Gpd, Gpd |
/// | 2 | Gpd, Mem, Gpd |
/// | 3 | Gpq, Gpq, Gpq |
/// | 4 | Gpq, Mem, Gpq |
/// +---+---------------+
/// ```
pub trait BextrEmitter<A, B, C> {
    fn bextr(&mut self, op0: A, op1: B, op2: C);
}

impl<'a> BextrEmitter<Gpd, Gpd, Gpd> for Assembler<'a> {
    fn bextr(&mut self, op0: Gpd, op1: Gpd, op2: Gpd) {
        self.emit(BEXTR32RRR, op0.as_operand(), op1.as_operand(), op2.as_operand(), &NOREG);
    }
}

impl<'a> BextrEmitter<Gpd, Mem, Gpd> for Assembler<'a> {
    fn bextr(&mut self, op0: Gpd, op1: Mem, op2: Gpd) {
        self.emit(BEXTR32RMR, op0.as_operand(), op1.as_operand(), op2.as_operand(), &NOREG);
    }
}

impl<'a> BextrEmitter<Gpq, Gpq, Gpq> for Assembler<'a> {
    fn bextr(&mut self, op0: Gpq, op1: Gpq, op2: Gpq) {
        self.emit(BEXTR64RRR, op0.as_operand(), op1.as_operand(), op2.as_operand(), &NOREG);
    }
}

impl<'a> BextrEmitter<Gpq, Mem, Gpq> for Assembler<'a> {
    fn bextr(&mut self, op0: Gpq, op1: Mem, op2: Gpq) {
        self.emit(BEXTR64RMR, op0.as_operand(), op1.as_operand(), op2.as_operand(), &NOREG);
    }
}

/// `BLSI` (BLSI). 
/// Extracts the lowest set bit from the source operand and set the corresponding bit in the destination register. All other bits in the destination operand are zeroed. If no bits are set in the source operand, BLSI sets all the bits in the destination to 0 and sets ZF and CF.
///
///
/// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/BLSI.html).
///
/// Supported operand variants:
///
/// ```text
/// +---+----------+
/// | # | Operands |
/// +---+----------+
/// | 1 | Gpd, Gpd |
/// | 2 | Gpd, Mem |
/// | 3 | Gpq, Gpq |
/// | 4 | Gpq, Mem |
/// +---+----------+
/// ```
pub trait BlsiEmitter<A, B> {
    fn blsi(&mut self, op0: A, op1: B);
}

impl<'a> BlsiEmitter<Gpd, Gpd> for Assembler<'a> {
    fn blsi(&mut self, op0: Gpd, op1: Gpd) {
        self.emit(BLSI32RR, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> BlsiEmitter<Gpd, Mem> for Assembler<'a> {
    fn blsi(&mut self, op0: Gpd, op1: Mem) {
        self.emit(BLSI32RM, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> BlsiEmitter<Gpq, Gpq> for Assembler<'a> {
    fn blsi(&mut self, op0: Gpq, op1: Gpq) {
        self.emit(BLSI64RR, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> BlsiEmitter<Gpq, Mem> for Assembler<'a> {
    fn blsi(&mut self, op0: Gpq, op1: Mem) {
        self.emit(BLSI64RM, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

/// `BLSMSK` (BLSMSK). 
/// Sets all the lower bits of the destination operand to “1” up to and including lowest set bit (=1) in the source operand. If source operand is zero, BLSMSK sets all bits of the destination operand to 1 and also sets CF to 1.
///
///
/// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/BLSMSK.html).
///
/// Supported operand variants:
///
/// ```text
/// +---+----------+
/// | # | Operands |
/// +---+----------+
/// | 1 | Gpd, Gpd |
/// | 2 | Gpd, Mem |
/// | 3 | Gpq, Gpq |
/// | 4 | Gpq, Mem |
/// +---+----------+
/// ```
pub trait BlsmskEmitter<A, B> {
    fn blsmsk(&mut self, op0: A, op1: B);
}

impl<'a> BlsmskEmitter<Gpd, Gpd> for Assembler<'a> {
    fn blsmsk(&mut self, op0: Gpd, op1: Gpd) {
        self.emit(BLSMSK32RR, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> BlsmskEmitter<Gpd, Mem> for Assembler<'a> {
    fn blsmsk(&mut self, op0: Gpd, op1: Mem) {
        self.emit(BLSMSK32RM, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> BlsmskEmitter<Gpq, Gpq> for Assembler<'a> {
    fn blsmsk(&mut self, op0: Gpq, op1: Gpq) {
        self.emit(BLSMSK64RR, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> BlsmskEmitter<Gpq, Mem> for Assembler<'a> {
    fn blsmsk(&mut self, op0: Gpq, op1: Mem) {
        self.emit(BLSMSK64RM, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

/// `BLSR` (BLSR). 
/// Copies all bits from the source operand to the destination operand and resets (=0) the bit position in the destination operand that corresponds to the lowest set bit of the source operand. If the source operand is zero BLSR sets CF.
///
///
/// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/BLSR.html).
///
/// Supported operand variants:
///
/// ```text
/// +---+----------+
/// | # | Operands |
/// +---+----------+
/// | 1 | Gpd, Gpd |
/// | 2 | Gpd, Mem |
/// | 3 | Gpq, Gpq |
/// | 4 | Gpq, Mem |
/// +---+----------+
/// ```
pub trait BlsrEmitter<A, B> {
    fn blsr(&mut self, op0: A, op1: B);
}

impl<'a> BlsrEmitter<Gpd, Gpd> for Assembler<'a> {
    fn blsr(&mut self, op0: Gpd, op1: Gpd) {
        self.emit(BLSR32RR, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> BlsrEmitter<Gpd, Mem> for Assembler<'a> {
    fn blsr(&mut self, op0: Gpd, op1: Mem) {
        self.emit(BLSR32RM, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> BlsrEmitter<Gpq, Gpq> for Assembler<'a> {
    fn blsr(&mut self, op0: Gpq, op1: Gpq) {
        self.emit(BLSR64RR, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> BlsrEmitter<Gpq, Mem> for Assembler<'a> {
    fn blsr(&mut self, op0: Gpq, op1: Mem) {
        self.emit(BLSR64RM, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

/// `TZCNT` (TZCNT). 
/// TZCNT counts the number of trailing least significant zero bits in source operand (second operand) and returns the result in destination operand (first operand). TZCNT is an extension of the BSF instruction. The key difference between TZCNT and BSF instruction is that TZCNT provides operand size as output when source operand is zero while in the case of BSF instruction, if source operand is zero, the content of destination operand are undefined. On processors that do not support TZCNT, the instruction byte encoding is executed as BSF.
///
///
/// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/TZCNT.html).
///
/// Supported operand variants:
///
/// ```text
/// +---+----------+
/// | # | Operands |
/// +---+----------+
/// | 1 | Gpd, Gpd |
/// | 2 | Gpd, Mem |
/// | 3 | Gpq, Gpq |
/// | 4 | Gpq, Mem |
/// | 5 | Gpw, Gpw |
/// | 6 | Gpw, Mem |
/// +---+----------+
/// ```
pub trait TzcntEmitter<A, B> {
    fn tzcnt(&mut self, op0: A, op1: B);
}

impl<'a> TzcntEmitter<Gpw, Gpw> for Assembler<'a> {
    fn tzcnt(&mut self, op0: Gpw, op1: Gpw) {
        self.emit(TZCNT16RR, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> TzcntEmitter<Gpw, Mem> for Assembler<'a> {
    fn tzcnt(&mut self, op0: Gpw, op1: Mem) {
        self.emit(TZCNT16RM, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> TzcntEmitter<Gpd, Gpd> for Assembler<'a> {
    fn tzcnt(&mut self, op0: Gpd, op1: Gpd) {
        self.emit(TZCNT32RR, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> TzcntEmitter<Gpd, Mem> for Assembler<'a> {
    fn tzcnt(&mut self, op0: Gpd, op1: Mem) {
        self.emit(TZCNT32RM, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> TzcntEmitter<Gpq, Gpq> for Assembler<'a> {
    fn tzcnt(&mut self, op0: Gpq, op1: Gpq) {
        self.emit(TZCNT64RR, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}

impl<'a> TzcntEmitter<Gpq, Mem> for Assembler<'a> {
    fn tzcnt(&mut self, op0: Gpq, op1: Mem) {
        self.emit(TZCNT64RM, op0.as_operand(), op1.as_operand(), &NOREG, &NOREG);
    }
}


impl<'a> Assembler<'a> {
    /// `ANDN` (ANDN). 
    /// Performs a bitwise logical AND of inverted second operand (the first source operand) with the third operand (the
    ///
    ///
    /// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/ANDN.html).
    ///
    /// Supported operand variants:
    ///
    /// ```text
    /// +---+---------------+
    /// | # | Operands      |
    /// +---+---------------+
    /// | 1 | Gpd, Gpd, Gpd |
    /// | 2 | Gpd, Gpd, Mem |
    /// | 3 | Gpq, Gpq, Gpq |
    /// | 4 | Gpq, Gpq, Mem |
    /// +---+---------------+
    /// ```
    #[inline]
    pub fn andn<A, B, C>(&mut self, op0: A, op1: B, op2: C)
    where Assembler<'a>: AndnEmitter<A, B, C> {
        <Self as AndnEmitter<A, B, C>>::andn(self, op0, op1, op2);
    }
    /// `BEXTR` (BEXTR). 
    /// Extracts contiguous bits from the first source operand (the second operand) using an index value and length value specified in the second source operand (the third operand). Bit 7:0 of the second source operand specifies the starting bit position of bit extraction. A START value exceeding the operand size will not extract any bits from the second source operand. Bit 15:8 of the second source operand specifies the maximum number of bits (LENGTH) beginning at the START position to extract. Only bit positions up to (OperandSize -1) of the first source operand are extracted. The extracted bits are written to the destination register, starting from the least significant bit. All higher order bits in the destination operand (starting at bit position LENGTH) are zeroed. The destination register is cleared if no bits are extracted.
    ///
    ///
    /// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/BEXTR.html).
    ///
    /// Supported operand variants:
    ///
    /// ```text
    /// +---+---------------+
    /// | # | Operands      |
    /// +---+---------------+
    /// | 1 | Gpd, Gpd, Gpd |
    /// | 2 | Gpd, Mem, Gpd |
    /// | 3 | Gpq, Gpq, Gpq |
    /// | 4 | Gpq, Mem, Gpq |
    /// +---+---------------+
    /// ```
    #[inline]
    pub fn bextr<A, B, C>(&mut self, op0: A, op1: B, op2: C)
    where Assembler<'a>: BextrEmitter<A, B, C> {
        <Self as BextrEmitter<A, B, C>>::bextr(self, op0, op1, op2);
    }
    /// `BLSI` (BLSI). 
    /// Extracts the lowest set bit from the source operand and set the corresponding bit in the destination register. All other bits in the destination operand are zeroed. If no bits are set in the source operand, BLSI sets all the bits in the destination to 0 and sets ZF and CF.
    ///
    ///
    /// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/BLSI.html).
    ///
    /// Supported operand variants:
    ///
    /// ```text
    /// +---+----------+
    /// | # | Operands |
    /// +---+----------+
    /// | 1 | Gpd, Gpd |
    /// | 2 | Gpd, Mem |
    /// | 3 | Gpq, Gpq |
    /// | 4 | Gpq, Mem |
    /// +---+----------+
    /// ```
    #[inline]
    pub fn blsi<A, B>(&mut self, op0: A, op1: B)
    where Assembler<'a>: BlsiEmitter<A, B> {
        <Self as BlsiEmitter<A, B>>::blsi(self, op0, op1);
    }
    /// `BLSMSK` (BLSMSK). 
    /// Sets all the lower bits of the destination operand to “1” up to and including lowest set bit (=1) in the source operand. If source operand is zero, BLSMSK sets all bits of the destination operand to 1 and also sets CF to 1.
    ///
    ///
    /// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/BLSMSK.html).
    ///
    /// Supported operand variants:
    ///
    /// ```text
    /// +---+----------+
    /// | # | Operands |
    /// +---+----------+
    /// | 1 | Gpd, Gpd |
    /// | 2 | Gpd, Mem |
    /// | 3 | Gpq, Gpq |
    /// | 4 | Gpq, Mem |
    /// +---+----------+
    /// ```
    #[inline]
    pub fn blsmsk<A, B>(&mut self, op0: A, op1: B)
    where Assembler<'a>: BlsmskEmitter<A, B> {
        <Self as BlsmskEmitter<A, B>>::blsmsk(self, op0, op1);
    }
    /// `BLSR` (BLSR). 
    /// Copies all bits from the source operand to the destination operand and resets (=0) the bit position in the destination operand that corresponds to the lowest set bit of the source operand. If the source operand is zero BLSR sets CF.
    ///
    ///
    /// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/BLSR.html).
    ///
    /// Supported operand variants:
    ///
    /// ```text
    /// +---+----------+
    /// | # | Operands |
    /// +---+----------+
    /// | 1 | Gpd, Gpd |
    /// | 2 | Gpd, Mem |
    /// | 3 | Gpq, Gpq |
    /// | 4 | Gpq, Mem |
    /// +---+----------+
    /// ```
    #[inline]
    pub fn blsr<A, B>(&mut self, op0: A, op1: B)
    where Assembler<'a>: BlsrEmitter<A, B> {
        <Self as BlsrEmitter<A, B>>::blsr(self, op0, op1);
    }
    /// `TZCNT` (TZCNT). 
    /// TZCNT counts the number of trailing least significant zero bits in source operand (second operand) and returns the result in destination operand (first operand). TZCNT is an extension of the BSF instruction. The key difference between TZCNT and BSF instruction is that TZCNT provides operand size as output when source operand is zero while in the case of BSF instruction, if source operand is zero, the content of destination operand are undefined. On processors that do not support TZCNT, the instruction byte encoding is executed as BSF.
    ///
    ///
    /// For more details, see the [Intel manual](https://www.felixcloutier.com/x86/TZCNT.html).
    ///
    /// Supported operand variants:
    ///
    /// ```text
    /// +---+----------+
    /// | # | Operands |
    /// +---+----------+
    /// | 1 | Gpd, Gpd |
    /// | 2 | Gpd, Mem |
    /// | 3 | Gpq, Gpq |
    /// | 4 | Gpq, Mem |
    /// | 5 | Gpw, Gpw |
    /// | 6 | Gpw, Mem |
    /// +---+----------+
    /// ```
    #[inline]
    pub fn tzcnt<A, B>(&mut self, op0: A, op1: B)
    where Assembler<'a>: TzcntEmitter<A, B> {
        <Self as TzcntEmitter<A, B>>::tzcnt(self, op0, op1);
    }
}