liblisa 0.1.4

A tool for automated discovery and analysis of the ISA of a CPU.
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Builder for [`Expr`](super::Expr)s.

use std::fmt::Debug;
use std::ptr::copy_nonoverlapping;

use super::ops::*;

const MAX_OPS: usize = 1024;

#[derive(Copy, Clone)]
// Hide this struct from documentation, because it is an implementation detail of the `expr!` macro.
#[doc(hidden)]
pub struct OpWriter {
    data: [Op; MAX_OPS],
    index: usize,
}

impl Default for OpWriter {
    fn default() -> Self {
        Self::new()
    }
}

impl OpWriter {
    #[inline]
    pub const fn new() -> Self {
        OpWriter {
            data: [Op::const_default(); MAX_OPS],
            index: 0,
        }
    }

    #[inline]
    pub const fn write(mut self, op: Op) -> Self {
        self.data[self.index] = op;
        self.index += 1;
        self
    }

    #[inline]
    pub const fn write_builder<const N: usize, I: FastOpImpl>(mut self, ops: ExprBuilder<N, I>) -> Self {
        assert!(self.index + ops.ops.index < MAX_OPS);
        // SAFETY: Assert above ensures correctness
        unsafe {
            let target = self.data.as_mut_ptr().add(self.index);
            copy_nonoverlapping(ops.ops.data.as_ptr(), target, ops.ops.index);
        }

        self.index += ops.ops.index;

        self
    }

    #[inline]
    pub const fn unop<const N: usize, I: FastOpImpl>(val: ExprBuilder<N, I>, suffix: Op) -> Self {
        val.ops.write(suffix)
    }

    #[inline]
    pub const fn binop<const N: usize, const M: usize, I1: FastOpImpl, I2: FastOpImpl>(
        lhs: ExprBuilder<N, I1>, rhs: ExprBuilder<M, I2>, suffix: Op,
    ) -> Self {
        lhs.ops.write_builder(rhs).write(suffix)
    }

    #[inline]
    pub const fn copy_to(self, target: &mut [Op]) {
        let mut n = 0;
        while n < self.index {
            target[n] = self.data[n];
            n += 1;
        }
    }

    #[inline]
    pub const fn as_slice(&self) -> &[Op] {
        assert!(self.index <= MAX_OPS);
        // SAFETY: The assertion above ensures correctness
        unsafe { std::slice::from_raw_parts(self.data.as_ptr(), self.index) }
    }
}

impl Debug for OpWriter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Debug::fmt(self.as_slice(), f)
    }
}

#[doc(hidden)] // Only public for macro
#[derive(Copy, Clone, Debug)]
pub struct ExprBuilder<const N: usize, I: FastOpImpl> {
    num_ops: usize,
    ops: OpWriter,
    pub builder: I,
}

type AbsResult<const N: usize, I> = ExprBuilder<N, IfZeroOp<SelectOp<127, 1, I>, I, AddOp<NotOp<I>, ConstOp<1>>>>;

impl<const N: usize, I: FastOpImpl> ExprBuilder<N, I> {
    #[inline]
    pub const fn into_inner(self) -> I {
        self.builder
    }

    #[inline]
    pub const fn num_ops(&self) -> usize {
        self.num_ops
    }

    #[inline]
    pub const fn ops(&self) -> OpWriter {
        self.ops
    }

    #[inline(always)]
    pub fn compute(&self, args: &impl Fn(usize) -> i128) -> i128 {
        // By making this not depend on self, the compiler will optimize out construction of `ops`
        I::reconstruct().compute(args)
    }

    #[inline]
    pub const fn crop<const NUM_BITS: u8>(self) -> ExprBuilder<{ N + 1 }, CropOp<NUM_BITS, I>> {
        ExprBuilder {
            num_ops: self.num_ops + 1,
            ops: self.ops.write(Op::Crop {
                num_bits: NUM_BITS,
            }),
            builder: CropOp(self.builder),
        }
    }

    #[inline]
    pub const fn rol<const NUM_BITS: u8, const M: usize, B: FastOpImpl>(
        self, rhs: ExprBuilder<M, B>,
    ) -> ExprBuilder<{ N + M + 1 }, RolOp<NUM_BITS, I, B>> {
        ExprBuilder {
            num_ops: self.num_ops + rhs.num_ops + 1,
            ops: OpWriter::binop(
                self,
                rhs,
                Op::Rol {
                    num_bits: NUM_BITS,
                },
            ),
            builder: RolOp(self.builder, rhs.builder),
        }
    }

    #[inline]
    pub const fn sign_extend<const NUM_BITS: u8>(self) -> ExprBuilder<{ N + 1 }, SignExtendOp<NUM_BITS, I>> {
        ExprBuilder {
            num_ops: self.num_ops + 1,
            ops: self.ops.write(Op::SignExtend {
                num_bits: NUM_BITS,
            }),
            builder: SignExtendOp(self.builder),
        }
    }

    #[inline]
    pub const fn if_zero<const M: usize, const L: usize, ZERO: FastOpImpl, NZERO: FastOpImpl>(
        self, zero: ExprBuilder<M, ZERO>, nonzero: ExprBuilder<L, NZERO>,
    ) -> ExprBuilder<{ N + M + L + 1 }, IfZeroOp<I, ZERO, NZERO>> {
        ExprBuilder {
            num_ops: self.num_ops + zero.num_ops + nonzero.num_ops + 1,
            ops: OpWriter::new()
                .write_builder(self)
                .write_builder(zero)
                .write_builder(nonzero)
                .write(Op::IfZero),
            builder: IfZeroOp(self.builder, zero.builder, nonzero.builder),
        }
    }

    #[inline]
    pub const fn byte_mask(self) -> ExprBuilder<{ N + 1 }, ByteMaskOp<I>> {
        ExprBuilder {
            num_ops: self.num_ops + 1,
            ops: OpWriter::unop(self, Op::ByteMask),
            builder: ByteMaskOp(self.builder),
        }
    }

    #[inline]
    pub const fn trailing_zeros(self) -> ExprBuilder<{ N + 1 }, TrailingZerosOp<I>> {
        ExprBuilder {
            num_ops: self.num_ops + 1,
            ops: OpWriter::unop(self, Op::TrailingZeros),
            builder: TrailingZerosOp(self.builder),
        }
    }

    #[inline]
    pub const fn leading_zeros(self) -> ExprBuilder<{ N + 1 }, LeadingZerosOp<I>> {
        ExprBuilder {
            num_ops: self.num_ops + 1,
            ops: OpWriter::unop(self, Op::LeadingZeros),
            builder: LeadingZerosOp(self.builder),
        }
    }

    #[inline]
    pub const fn popcount(self) -> ExprBuilder<{ N + 1 }, PopCountOp<I>>
    where
        Self: Clone,
    {
        ExprBuilder {
            num_ops: self.num_ops + 1,
            ops: OpWriter::unop(self, Op::PopCount),
            builder: PopCountOp(self.builder),
        }
    }

    #[inline]
    pub const fn abs(self) -> AbsResult<{ N * 3 + 5 }, I> {
        // if_zero(select[127:+1](self), self, add(not(self), 1))
        ExprBuilder {
            num_ops: self.num_ops * 3 + 5,
            ops: OpWriter::new()
                .write_builder(self)
                .write(Op::Select {
                    num_skip: 127,
                    num_take: 1,
                })
                .write_builder(self)
                .write_builder(self)
                .write(Op::Not)
                .write(Op::Const(1))
                .write(Op::Add)
                .write(Op::IfZero),
            builder: IfZeroOp(
                SelectOp::<127, 1, _>(self.builder),
                self.builder,
                AddOp(NotOp(self.builder), ConstOp::<1>),
            ),
        }
    }

    #[inline]
    pub const fn extract_bits<const M: usize, B: FastOpImpl>(
        self, selector: ExprBuilder<M, B>,
    ) -> ExprBuilder<{ N + M + 1 }, ExtractBitsOp<I, B>>
    where
        Self: Clone,
    {
        ExprBuilder {
            num_ops: self.num_ops + selector.num_ops + 1,
            ops: OpWriter::binop(self, selector, Op::ExtractBits),
            builder: ExtractBitsOp(self.builder, selector.builder),
        }
    }

    #[inline]
    pub const fn deposit_bits<const M: usize, B: FastOpImpl>(
        self, selector: ExprBuilder<M, B>,
    ) -> ExprBuilder<{ N + M + 1 }, DepositBitsOp<I, B>>
    where
        Self: Clone,
    {
        ExprBuilder {
            num_ops: self.num_ops + selector.num_ops + 1,
            ops: OpWriter::binop(self, selector, Op::DepositBits),
            builder: DepositBitsOp(self.builder, selector.builder),
        }
    }
}

/// Multiply two values.
#[inline]
pub const fn mul<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, MulOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::Mul),
        builder: MulOp(a.builder, b.builder),
    }
}

/// Perform a carryless multiplication.
#[inline]
pub const fn carryless_mul<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, CarrylessMulOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::CarrylessMul),
        builder: CarrylessMulOp(a.builder, b.builder),
    }
}

/// Perform a division.
#[inline]
pub const fn div<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, DivOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::Div),
        builder: DivOp(a.builder, b.builder),
    }
}

/// Perform an unsigned divide.
#[inline]
pub const fn udiv<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, UnsignedDivOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::UnsignedDiv),
        builder: UnsignedDivOp(a.builder, b.builder),
    }
}

/// Compute the remainder of a value.
#[inline]
pub const fn rem<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, RemOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::Rem),
        builder: RemOp(a.builder, b.builder),
    }
}

/// Compute the unsigned remainder of a value.
#[inline]
pub const fn urem<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, UnsignedRemOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::UnsignedRem),
        builder: UnsignedRemOp(a.builder, b.builder),
    }
}

/// Perform a left shift.
#[inline]
pub const fn shl<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, ShlOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::Shl),
        builder: ShlOp(a.builder, b.builder),
    }
}

/// Perform a right shift.
#[inline]
pub const fn shr<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, ShrOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::Shr),
        builder: ShrOp(a.builder, b.builder),
    }
}

/// Perform a bitwise AND.
#[inline]
pub const fn and<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, AndOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::And),
        builder: AndOp(a.builder, b.builder),
    }
}

/// Perform an addition.
#[inline]
pub const fn add<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, AddOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::Add),
        builder: AddOp(a.builder, b.builder),
    }
}

/// Perform a subtraction.
#[inline]
pub const fn sub<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, SubOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::Sub),
        builder: SubOp(a.builder, b.builder),
    }
}

/// Perform a bitwise OR.
#[inline]
pub const fn or<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, OrOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::Or),
        builder: OrOp(a.builder, b.builder),
    }
}

/// Perform a bitwise XOR.
#[inline]
pub const fn xor<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, XorOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::Xor),
        builder: XorOp(a.builder, b.builder),
    }
}

/// Perform a bitwise NOT.
#[inline]
pub const fn not<const N: usize, A: FastOpImpl>(a: ExprBuilder<N, A>) -> ExprBuilder<{ N + 1 }, NotOp<A>> {
    ExprBuilder {
        num_ops: N + 1,
        ops: OpWriter::unop(a, Op::Not),
        builder: NotOp(a.builder),
    }
}

/// Computes the parity of the lower 8 bits.
#[inline]
pub const fn parity<const N: usize, A: FastOpImpl>(a: ExprBuilder<N, A>) -> ExprBuilder<{ N + 1 }, ParityOp<A>> {
    ExprBuilder {
        num_ops: N + 1,
        ops: OpWriter::unop(a, Op::Parity),
        builder: ParityOp(a.builder),
    }
}

/// Selects a range of bits.
#[inline]
pub const fn select<const NUM_SKIP: u8, const NUM_TAKE: u8, const N: usize, A: FastOpImpl>(
    a: ExprBuilder<N, A>,
) -> ExprBuilder<{ N + 1 }, SelectOp<NUM_SKIP, NUM_TAKE, A>> {
    ExprBuilder {
        num_ops: N + 1,
        ops: OpWriter::unop(
            a,
            Op::Select {
                num_skip: NUM_SKIP,
                num_take: NUM_TAKE,
            },
        ),
        builder: SelectOp(a.builder),
    }
}

/// Concatenate bits.
#[inline]
pub const fn concat_bit<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ (N + 2) + M + 1 }, OrOp<ShlOp<A, ConstOp<1>>, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::new()
            .write_builder(a)
            .write(Op::Const(1))
            .write(Op::Shl)
            .write_builder(b)
            .write(Op::Or),
        builder: OrOp(ShlOp(a.builder, ConstOp::<1>), b.builder),
    }
}

/// Returns `1` if the value is 0, otherwise returns `0`.
#[inline]
pub const fn is_zero<const N: usize, A: FastOpImpl>(a: ExprBuilder<N, A>) -> ExprBuilder<{ N + 1 }, IsZeroOp<A>> {
    ExprBuilder {
        num_ops: N + 1,
        ops: OpWriter::unop(a, Op::IsZero),
        builder: IsZeroOp(a.builder),
    }
}

/// Returns `1` if `a` is less than `b`. Returns `0` if `a` is equal to or greater than `b`.
#[inline]
pub const fn less_than<const N: usize, const M: usize, A: FastOpImpl, B: FastOpImpl>(
    a: ExprBuilder<N, A>, b: ExprBuilder<M, B>,
) -> ExprBuilder<{ N + M + 1 }, CmpLtOp<A, B>> {
    ExprBuilder {
        num_ops: N + M + 1,
        ops: OpWriter::binop(a, b, Op::CmpLt),
        builder: CmpLtOp(a.builder, b.builder),
    }
}

/// Computes (1 << N) - 1, which is an integer with the lowest N bits set to 1 and the rest to 0.
#[inline]
pub const fn bit_mask<const N: usize, A: FastOpImpl>(a: ExprBuilder<N, A>) -> ExprBuilder<{ N + 1 }, BitMaskOp<A>> {
    ExprBuilder {
        num_ops: N + 1,
        ops: OpWriter::unop(a, Op::BitMask),
        builder: BitMaskOp(a.builder),
    }
}

/// Returns a template hole.
#[inline]
pub const fn hole<const N: u16>() -> ExprBuilder<1, HoleOp<N>> {
    ExprBuilder {
        num_ops: 1,
        ops: OpWriter::new().write(Op::Hole(N)),
        builder: HoleOp::<N>,
    }
}

/// Returns the constant 0.
#[inline]
pub const fn zero() -> ExprBuilder<1, ConstOp<0>> {
    c::<0>()
}

/// Returns the constant 1.
#[inline]
pub const fn one() -> ExprBuilder<1, ConstOp<1>> {
    c::<1>()
}

/// Returns a constant.
#[inline]
pub const fn c<const VAL: i8>() -> ExprBuilder<1, ConstOp<VAL>> {
    ExprBuilder {
        num_ops: 1,
        ops: OpWriter::new().write(Op::Const(VAL)),
        builder: ConstOp::<VAL>,
    }
}