vlk-analysis 0.3.0

Lifting, SSA and simplification passes of the vlk decompiler
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
use super::{
    BinOp, Cond, ConditionCode, Expr, Flags, Instruction, Lifter, Mnemonic, OpKind, Operation, RAX,
    RDX, RSP, Stmt, gpr, operand, operand_width, write_destination, write_operand,
};

/// A division of the dividend in rdx:rax (or ah:al for bytes) by `divisor`
/// of `width` bits: the quotient into rax (al) and the remainder into rdx
/// (ah).
pub(super) fn wide_divide(lifter: &mut Lifter, signed: bool, width: u8, divisor: Expr) {
    let divide = |remainder, high: Expr, low: Expr| {
        Expr::apply(
            Operation::WideDivide {
                signed,
                remainder,
                bits: width,
            },
            vec![high, low, divisor.clone()],
        )
    };
    if width == 8 {
        let ax = Lifter::read_reg(RAX, 16);
        let high = Expr::bin(BinOp::Shr, ax.clone(), Expr::constant(8));
        let low = Expr::bin(BinOp::And, ax, Expr::constant(0xff));
        let quotient = divide(false, high.clone(), low.clone());
        let remainder = divide(true, high, low);
        // ax takes the remainder above the quotient.
        let joined = Expr::bin(
            BinOp::Or,
            Expr::bin(BinOp::Shl, remainder, Expr::constant(8)),
            quotient,
        );
        lifter.assign(RAX, 16, joined);
        return;
    }
    let (high, low) = (Lifter::read_reg(RDX, width), Lifter::read_reg(RAX, width));
    let quotient = divide(false, high.clone(), low.clone());
    // Both read the dividend, so the quotient waits while rdx changes.
    let quotient = hold(lifter, quotient);
    lifter.assign(RDX, width, divide(true, high, low));
    lifter.assign(RAX, width, quotient);
}

pub(super) fn hold(lifter: &mut Lifter, value: Expr) -> Expr {
    lifter.out.hold(value)
}

/// `xchg`, `xadd` and `cmpxchg`, whose destination is the first operand
/// (the memory one, if any) and whose source register receives the old value.
pub(super) fn lift_exchange(lifter: &mut Lifter, instruction: &Instruction) {
    let (Some(current), Some(source), Some(width)) = (
        operand(lifter, instruction, 0),
        operand(lifter, instruction, 1),
        operand_width(instruction, 0),
    ) else {
        lifter.out.stmts.push(Stmt::unsupported(format!(
            "{:?} at 0x{:x}",
            instruction.mnemonic(),
            instruction.ip()
        )));
        return;
    };
    let same_register = instruction.op0_kind() == OpKind::Register
        && instruction.op1_kind() == OpKind::Register
        && instruction.op0_register() == instruction.op1_register();
    match instruction.mnemonic() {
        Mnemonic::Xchg if same_register => {}
        Mnemonic::Xchg => {
            let old = hold(lifter, current);
            write_operand(lifter, instruction, 0, source);
            write_operand(lifter, instruction, 1, old);
        }
        Mnemonic::Xadd if same_register => {
            lifter.set_flags(Flags::sum(current.clone(), source.clone()));
            let sum = Expr::bin(BinOp::Add, current, source);
            write_destination(lifter, instruction, sum);
        }
        Mnemonic::Xadd => {
            let old = hold(lifter, current);
            lifter.set_flags(Flags::sum(old.clone(), source.clone()));
            let sum = Expr::bin(BinOp::Add, old.clone(), source);
            write_destination(lifter, instruction, sum);
            write_operand(lifter, instruction, 1, old);
        }
        // The destination receives the source if it held the accumulator,
        // and the accumulator receives the destination if it did not.
        _ => {
            let expected = Lifter::read_reg(RAX, width);
            let old = hold(lifter, current);
            let select = |then: Expr, els: Expr| Expr::Select {
                cond: Cond::Eq,
                lhs: Box::new(old.clone()),
                rhs: Box::new(expected.clone()),
                then: Box::new(then),
                els: Box::new(els),
            };
            write_destination(lifter, instruction, select(source, old.clone()));
            // The flags compare the accumulator with the old value.
            lifter.set_flags(Flags::difference(expected.clone(), old.clone()));
            let mismatch = if width >= 32 {
                old.clone()
            } else {
                let mask = (1u64 << width) - 1;
                Expr::bin(
                    BinOp::Or,
                    Expr::bin(BinOp::And, Expr::reg(RAX, 64), Expr::constant(!mask)),
                    old.clone(),
                )
            };
            lifter.assign(RAX, 64, select(Expr::reg(RAX, 64), mismatch));
        }
    }
}

/// `rol`, `ror`, `rcl`, `rcr`, `shld`, `shrd`, `bswap` and `bt`.
pub(super) fn lift_bits(lifter: &mut Lifter, instruction: &Instruction) {
    let (Some(value), Some(width)) = (
        operand(lifter, instruction, 0),
        operand_width(instruction, 0),
    ) else {
        lifter.out.stmts.push(Stmt::unsupported(format!(
            "{:?} at 0x{:x}",
            instruction.mnemonic(),
            instruction.ip()
        )));
        return;
    };
    let bits = u64::from(width);
    match instruction.mnemonic() {
        // The result of swapping a 16-bit register is undefined.
        Mnemonic::Bswap if width == 16 => lifter.out.stmts.push(Stmt::unsupported(format!(
            "bswap of a 16-bit register at 0x{:x}",
            instruction.ip()
        ))),
        Mnemonic::Bswap => {
            let swapped = super::reverse_bytes(value, width, width / 8);
            write_destination(lifter, instruction, swapped);
        }
        // A rotate through the carry, with the carry known: the operand and
        // the carry rotate together as one value a bit wider.
        Mnemonic::Rcl | Mnemonic::Rcr if lifter.carry().is_some() => {
            let Some(amount) = operand(lifter, instruction, 1) else {
                return;
            };
            let carry = lifter.carry().expect("a known carry");
            // The carry may read registers that keep the earlier flags'
            // operands, which keeping the new flags' operands overwrites.
            let carry = hold(lifter, carry);
            let count = match shift_count(amount, width) {
                Expr::Const(count) if width < 32 => Expr::constant(count % (bits + 1)),
                count if width < 32 => Expr::bin(BinOp::ModU, count, Expr::constant(bits + 1)),
                count => count,
            };
            let shifted = |op, value: Expr, by: Expr| Expr::bin(op, value, by);
            let sub =
                |a: u64, count: &Expr| Expr::bin(BinOp::Sub, Expr::constant(a), count.clone());
            let one = || Expr::constant(1);
            let (towards, from) = if instruction.mnemonic() == Mnemonic::Rcl {
                (BinOp::Shl, BinOp::Shr)
            } else {
                (BinOp::Shr, BinOp::Shl)
            };
            // For a count from one to the width: the operand shifted by the
            // count, the carry beside it and the bits that wrap past it.
            let carry_at = if towards == BinOp::Shl {
                Expr::bin(BinOp::Sub, count.clone(), one())
            } else {
                sub(bits, &count)
            };
            let wrapped = shifted(from, shifted(from, value.clone(), sub(bits, &count)), one());
            let rotated = Expr::bin(
                BinOp::Or,
                Expr::bin(
                    BinOp::Or,
                    shifted(towards, value.clone(), count.clone()),
                    shifted(BinOp::Shl, carry.clone(), carry_at),
                ),
                wrapped,
            );
            let rotated = if bits == 64 {
                rotated
            } else {
                Expr::bin(BinOp::And, rotated, Expr::constant((1u64 << bits) - 1))
            };
            let out_at = if towards == BinOp::Shl {
                sub(bits, &count)
            } else {
                Expr::bin(BinOp::Sub, count.clone(), one())
            };
            let out = Expr::bin(
                BinOp::And,
                shifted(BinOp::Shr, value.clone(), out_at),
                one(),
            );
            let (rotated, out) = match count {
                Expr::Const(0) => (value, carry),
                Expr::Const(_) => (rotated, out),
                count => {
                    let unless_zero = |then: Expr, els: Expr| Expr::Select {
                        cond: Cond::Eq,
                        lhs: Box::new(count.clone()),
                        rhs: Box::new(Expr::constant(0)),
                        then: Box::new(then),
                        els: Box::new(els),
                    };
                    (unless_zero(value, rotated), unless_zero(carry, out))
                }
            };
            lifter.set_flags(Flags::carry(out));
            write_destination(lifter, instruction, rotated);
        }
        Mnemonic::Rcl | Mnemonic::Rcr => lifter.out.stmts.push(Stmt::unsupported(format!(
            "{:?} on a carry not modelled at 0x{:x}",
            instruction.mnemonic(),
            instruction.ip()
        ))),
        Mnemonic::Rol | Mnemonic::Ror => {
            let Some(amount) = operand(lifter, instruction, 1) else {
                return;
            };
            let amount = shift_count(amount, width);
            let right = instruction.mnemonic() == Mnemonic::Ror;
            write_destination(
                lifter,
                instruction,
                super::rotate(value, amount, width, right),
            );
        }
        // A bit string in memory may lie past the addressed operand.
        Mnemonic::Bt | Mnemonic::Bts | Mnemonic::Btr | Mnemonic::Btc
            if instruction.op0_kind() == OpKind::Memory
                && instruction.op1_kind() == OpKind::Register =>
        {
            lifter
                .out
                .stmts
                .push(Stmt::unsupported(format!("bt at 0x{:x}", instruction.ip())));
        }
        // The carry holds the bit; zero is left as it was. bts, btr and btc
        // then set, clear or flip it.
        Mnemonic::Bt | Mnemonic::Bts | Mnemonic::Btr | Mnemonic::Btc => {
            let Some(bit) = operand(lifter, instruction, 1) else {
                return;
            };
            let bit = Expr::bin(BinOp::And, bit, Expr::constant(bits - 1));
            let flag = Expr::bin(
                BinOp::And,
                Expr::bin(BinOp::Shr, value.clone(), bit.clone()),
                Expr::constant(1),
            );
            lifter.set_flags(Flags::carry(flag));
            let single = Expr::bin(BinOp::Shl, Expr::constant(1), bit);
            let changed = match instruction.mnemonic() {
                Mnemonic::Bts => Expr::bin(BinOp::Or, value, single),
                Mnemonic::Btr => Expr::bin(
                    BinOp::And,
                    value,
                    Expr::bin(BinOp::Xor, single, Expr::constant(u64::MAX)),
                ),
                Mnemonic::Btc => Expr::bin(BinOp::Xor, value, single),
                _ => return,
            };
            write_destination(lifter, instruction, changed);
        }
        _ => {
            let (Some(source), Some(count)) = (
                operand(lifter, instruction, 1),
                operand(lifter, instruction, 2),
            ) else {
                return;
            };
            if width < 32 {
                lifter.out.stmts.push(Stmt::unsupported(format!(
                    "{:?} at 0x{:x}",
                    instruction.mnemonic(),
                    instruction.ip()
                )));
                return;
            }
            let (towards, from) = match instruction.mnemonic() {
                Mnemonic::Shld => (BinOp::Shl, BinOp::Shr),
                _ => (BinOp::Shr, BinOp::Shl),
            };
            let shifted = |amount: Expr, back: Expr| {
                Expr::bin(
                    BinOp::Or,
                    Expr::bin(towards, value.clone(), amount),
                    Expr::bin(from, source.clone(), back),
                )
            };
            lifter.out.flags.pending = None;
            let value = match count {
                Expr::Const(count) => match count & (bits - 1) {
                    0 => return,
                    count => shifted(Expr::constant(count), Expr::constant(bits - count)),
                },
                count => {
                    let count = Expr::bin(BinOp::And, count, Expr::constant(bits - 1));
                    Expr::Select {
                        cond: Cond::Eq,
                        lhs: Box::new(count.clone()),
                        rhs: Box::new(Expr::constant(0)),
                        then: Box::new(value.clone()),
                        els: Box::new(shifted(
                            count.clone(),
                            Expr::bin(BinOp::Sub, Expr::constant(bits), count),
                        )),
                    }
                }
            };
            write_destination(lifter, instruction, value);
        }
    }
}

/// One-operand `mul` and `imul`: the accumulator times the operand, with
/// the product's low half in the accumulator and its high half in rdx (edx,
/// dx), or the whole product in ax for bytes.
pub(super) fn lift_mul_high(lifter: &mut Lifter, instruction: &Instruction, signed: bool) {
    let (Some(source), Some(width)) = (
        operand(lifter, instruction, 0),
        operand_width(instruction, 0),
    ) else {
        lifter.out.incomplete.set(true);
        return;
    };
    let multiplicand = Lifter::read_reg(RAX, width);
    if width == 64 {
        // The operand may be rdx, which the high half overwrites first.
        let source = hold(lifter, source);
        let high_op = if signed {
            BinOp::MulHighS
        } else {
            BinOp::MulHigh
        };
        lifter.assign(
            RDX,
            64,
            Expr::bin(high_op, multiplicand.clone(), source.clone()),
        );
        lifter.assign(RAX, 64, Expr::bin(BinOp::Mul, multiplicand, source));
        lifter.set_flags(Flags::carry_overflow(lost_high(RDX, RAX, 64, signed)));
        return;
    }
    let widen = |value: Expr| {
        if signed {
            Expr::SignExtend {
                value: Box::new(value),
                width: width / 8,
            }
        } else {
            value
        }
    };
    let product = Expr::bin(BinOp::Mul, widen(multiplicand), widen(source));
    if width == 8 {
        lifter.assign(RAX, 16, product);
        // ah against what al's sign (or zero) would put there.
        let ax = Lifter::read_reg(RAX, 16);
        let low = Expr::bin(BinOp::And, ax.clone(), Expr::constant(0xff));
        let expected = if signed {
            Expr::SignExtend {
                value: Box::new(low),
                width: 1,
            }
        } else {
            low
        };
        let lost = Expr::bin(
            BinOp::And,
            Expr::bin(BinOp::Xor, ax, expected),
            Expr::constant(0xffff),
        );
        lifter.set_flags(Flags::carry_overflow(lost));
        return;
    }
    let product = hold(lifter, product);
    lifter.assign(
        RDX,
        width,
        Expr::bin(
            BinOp::Shr,
            product.clone(),
            Expr::constant(u64::from(width)),
        ),
    );
    lifter.assign(RAX, width, product);
    lifter.set_flags(Flags::carry_overflow(lost_high(RDX, RAX, width, signed)));
}

/// Nonzero when the signed product of `a` and `b` does not fit in `width`
/// bits: the full product against its truncation, sign-extended.
pub(super) fn signed_product_lost(a: &Expr, b: &Expr, width: u8) -> Expr {
    if width == 64 {
        let product = Expr::bin(BinOp::Mul, a.clone(), b.clone());
        return Expr::bin(
            BinOp::Xor,
            Expr::bin(BinOp::MulHighS, a.clone(), b.clone()),
            Expr::bin(BinOp::Sar, product, Expr::constant(63)),
        );
    }
    let extend = |value: Expr, bytes: u8| Expr::SignExtend {
        value: Box::new(value),
        width: bytes,
    };
    let bytes = width / 8;
    let full = Expr::bin(
        BinOp::Mul,
        extend(a.clone(), bytes),
        extend(b.clone(), bytes),
    );
    let truncated = Expr::bin(
        BinOp::And,
        full.clone(),
        Expr::constant((1u64 << width) - 1),
    );
    Expr::bin(BinOp::Xor, full, extend(truncated, bytes))
}

/// What a multiply's high half `high` holds beyond what its low half
/// `low` implies, zero or its sign: nonzero when the product did not fit.
fn lost_high(high: u16, low: u16, width: u8, signed: bool) -> Expr {
    let high = Lifter::read_reg(high, width);
    let expected = if signed {
        Expr::bin(
            BinOp::Sar,
            Lifter::read_reg(low, width),
            Expr::constant(u64::from(width) - 1),
        )
    } else {
        Expr::constant(0)
    };
    let lost = Expr::bin(BinOp::Xor, high, expected);
    if width == 64 {
        lost
    } else {
        Expr::bin(BinOp::And, lost, Expr::constant((1u64 << width) - 1))
    }
}

pub(super) fn binary(lifter: &mut Lifter, instruction: &Instruction, op: BinOp) {
    let (Some(lhs), Some(rhs)) = (
        operand(lifter, instruction, 0),
        operand(lifter, instruction, 1),
    ) else {
        return;
    };
    // `xor r, r` and `sub r, r` clear the register without depending on its value.
    let value = if matches!(op, BinOp::Xor | BinOp::Sub) && lhs == rhs {
        Expr::constant(0)
    } else {
        Expr::bin(op, lhs.clone(), rhs.clone())
    };
    let width = operand_width(instruction, 0).map_or(64, u32::from);
    lifter.set_flags(match op {
        BinOp::Sub => Flags::difference(lhs, rhs),
        BinOp::Add => Flags::sum(lhs, rhs),
        _ => Flags::logic_at(value.clone(), width),
    });
    write_destination(lifter, instruction, value);
}

pub(super) fn select(
    lifter: &mut Lifter,
    instruction: &Instruction,
    then: Expr,
    els: Expr,
) -> Option<Expr> {
    lifter
        .condition_test(instruction.condition_code())
        .select(then, els)
}

pub(super) fn shift(lifter: &mut Lifter, instruction: &Instruction, op: BinOp) {
    let (Some(value), Some(amount), Some(width)) = (
        operand(lifter, instruction, 0),
        operand(lifter, instruction, 1),
        operand_width(instruction, 0),
    ) else {
        return;
    };
    let amount = shift_count(amount, width);
    let shifted = Expr::bin(op, value.clone(), amount.clone());
    // A shift by zero leaves the flags as they were. The carry holds the
    // last bit shifted out, which a count of the width or more leaves
    // undefined.
    match amount {
        Expr::Const(count) if count != 0 && count < u64::from(width) => {
            let out = if op == BinOp::Shl {
                u64::from(width) - count
            } else {
                count - 1
            };
            let carry = Expr::bin(
                BinOp::And,
                Expr::bin(BinOp::Shr, value, Expr::constant(out)),
                Expr::constant(1),
            );
            lifter.set_flags(Flags::shift(shifted.clone(), carry));
        }
        Expr::Const(count) if count != 0 => lifter.set_flags(Flags::result(shifted.clone())),
        Expr::Const(_) => {
            if let Some(flags) = lifter.out.flags.pending.clone() {
                lifter.set_flags(flags);
            }
        }
        amount => {
            if let Some(old) = lifter
                .test(Cond::Eq)
                .select(Expr::constant(1), Expr::constant(0))
            {
                let result = if width < 64 {
                    Expr::bin(
                        BinOp::And,
                        shifted.clone(),
                        Expr::constant((1u64 << width) - 1),
                    )
                } else {
                    shifted.clone()
                };
                let zero = Expr::truth(Cond::Eq, result, Expr::constant(0));
                lifter.set_flags(Flags::zero(Expr::Select {
                    cond: Cond::Eq,
                    lhs: Box::new(amount),
                    rhs: Box::new(Expr::constant(0)),
                    then: Box::new(old),
                    els: Box::new(zero),
                }));
            }
        }
    }
    write_destination(lifter, instruction, shifted);
}

/// `tzcnt`, `lzcnt`, `popcnt`, and `bsf` and `bsr`, which leave the
/// destination as it was for a zero source (as processors do, though Intel
/// leaves it undefined), all 64 bits of it even for a 32-bit operand, and
/// set the zero flag for it.
pub(super) fn lift_count(lifter: &mut Lifter, instruction: &Instruction) {
    let (Some(source), Some(width)) = (
        operand(lifter, instruction, 1),
        operand_width(instruction, 1),
    ) else {
        return;
    };
    let count = |op| Expr::apply(op, vec![source.clone()]);
    let zero = || Expr::constant(0);
    // One when the source is zero.
    let empty = Expr::truth(Cond::Eq, source.clone(), zero());
    let (value, flags) = match instruction.mnemonic() {
        Mnemonic::Tzcnt => (
            count(Operation::CountTrailingZeros(width)),
            Flags::carry(empty),
        ),
        Mnemonic::Lzcnt => (
            count(Operation::CountLeadingZeros(width)),
            Flags::carry(empty),
        ),
        // Zero follows the source; carry, sign and overflow are clear, as
        // after comparing the small count with zero, and so are parity and
        // adjust.
        Mnemonic::Popcnt => {
            let value = count(Operation::PopCount(width));
            (
                value.clone(),
                Flags::difference(value, zero()).clearing_low(),
            )
        }
        _ => {
            let Some(old) = operand(lifter, instruction, 0) else {
                return;
            };
            // A 32-bit destination left as it was keeps its high half too.
            let whole = match (width, gpr(instruction.op0_register())) {
                (32, Some((index, _))) if instruction.op0_kind() == OpKind::Register => Some(index),
                _ => None,
            };
            let old = whole.map_or(old, |index| Lifter::read_reg(index, 64));
            let found = if instruction.mnemonic() == Mnemonic::Bsf {
                count(Operation::CountTrailingZeros(width))
            } else {
                Expr::bin(
                    BinOp::Sub,
                    Expr::constant(u64::from(width) - 1),
                    count(Operation::CountLeadingZeros(width)),
                )
            };
            let value = Expr::Select {
                cond: Cond::Eq,
                lhs: Box::new(source.clone()),
                rhs: Box::new(zero()),
                then: Box::new(old),
                els: Box::new(found),
            };
            lifter.set_flags(Flags::result(source.clone()));
            match whole {
                Some(index) => lifter.assign(index, 64, value),
                None => write_destination(lifter, instruction, value),
            }
            return;
        }
    };
    lifter.set_flags(flags);
    write_destination(lifter, instruction, value);
}

/// `adc` and `sbb`: a sum with the carry, or a difference less it. The
/// flags include the carry out and the truncated result.
pub(super) fn with_carry(lifter: &mut Lifter, instruction: &Instruction, subtract: bool) {
    let (Some(a), Some(b), Some(width)) = (
        operand(lifter, instruction, 0),
        operand(lifter, instruction, 1),
        operand_width(instruction, 0),
    ) else {
        return;
    };
    let Some(carry) = lifter.carry() else {
        lifter.out.stmts.push(Stmt::unsupported(format!(
            "{:?} on a carry not modelled at 0x{:x}",
            instruction.mnemonic(),
            instruction.ip()
        )));
        return;
    };
    // The carry may read registers that keep the earlier flags' operands,
    // which keeping the new flags' operands overwrites.
    let carry = hold(lifter, carry);
    let (out, value) = if subtract {
        (
            super::flags::borrow_out(&a, &b, carry.clone()),
            Expr::bin(
                BinOp::Sub,
                Expr::bin(BinOp::Sub, a.clone(), b.clone()),
                carry,
            ),
        )
    } else {
        (
            super::flags::carry_out(&a, &b, carry.clone(), u32::from(width)),
            Expr::bin(
                BinOp::Add,
                Expr::bin(BinOp::Add, a.clone(), b.clone()),
                carry,
            ),
        )
    };
    let result = Expr::bin(
        BinOp::And,
        value.clone(),
        Expr::constant(vlk_ir::ir::semantics::mask(u32::from(width))),
    );
    let bit = |value, shift| Expr::bin(BinOp::Shl, value, Expr::constant(shift));
    let truth = Expr::truth;
    let sign = |value| {
        Expr::bin(
            BinOp::And,
            Expr::bin(BinOp::Shr, value, Expr::constant(u64::from(width - 1))),
            Expr::constant(1),
        )
    };
    let zero = truth(Cond::Eq, result.clone(), Expr::constant(0));
    let adjust = Expr::bin(
        BinOp::And,
        Expr::bin(
            BinOp::Xor,
            Expr::bin(BinOp::Xor, a.clone(), b.clone()),
            result.clone(),
        ),
        Expr::constant(0x10),
    );
    let parity = Flags::logic(result.clone())
        .parity(true)
        .select(Expr::constant(4), Expr::constant(0))
        .expect("logical results define parity");
    let different = Expr::bin(BinOp::Xor, a.clone(), b);
    let overflow = sign(Expr::bin(
        BinOp::And,
        if subtract {
            different
        } else {
            Expr::bin(BinOp::Xor, different, Expr::constant(u64::MAX))
        },
        Expr::bin(BinOp::Xor, a, result.clone()),
    ));
    let byte = [bit(sign(result), 7), bit(zero, 6), adjust, parity]
        .into_iter()
        .fold(out, |byte, part| Expr::bin(BinOp::Or, byte, part));
    lifter.set_flags(Flags::stored(byte, Some(overflow)));
    write_destination(lifter, instruction, value);
}

/// `lahf`: sign, zero, adjust, parity and carry into bits 7, 6, 4, 2 and 0
/// of `ah`, with bit 1 set; unsupported when the flags here leave one open.
pub(super) fn lift_lahf(lifter: &mut Lifter, instruction: &Instruction) {
    let bits = lifter.out.flags.pending.as_ref().and_then(|flags| {
        flags
            .byte_tests(vlk_base::target::Isa::X86_64)
            .into_iter()
            .try_fold(Expr::constant(2), |byte, (test, bit)| {
                let Some(value) = test.select(Expr::constant(1 << bit), Expr::constant(0)) else {
                    return None;
                };
                Some(Expr::bin(BinOp::Or, byte, value))
            })
    });
    let Some(byte) = bits else {
        lifter.out.stmts.push(Stmt::unsupported(format!(
            "lahf on flags not modelled at 0x{:x}",
            instruction.ip()
        )));
        return;
    };
    // The byte may read registers that keep the flags' operands, which
    // writing `rax` may move there.
    let byte = hold(lifter, byte);
    let kept = Expr::bin(BinOp::And, Expr::reg(RAX, 64), Expr::constant(!0xff00));
    let placed = Expr::bin(BinOp::Shl, byte, Expr::constant(8));
    lifter.assign(RAX, 64, Expr::bin(BinOp::Or, kept, placed));
}

/// The count a shift or rotate uses: its low six bits for a 64-bit operand,
/// its low five bits otherwise.
fn shift_count(amount: Expr, width: u8) -> Expr {
    let mask = if width == 64 { 63 } else { 31 };
    match amount {
        Expr::Const(count) => Expr::constant(count & mask),
        amount => Expr::bin(BinOp::And, amount, Expr::constant(mask)),
    }
}

pub(super) fn condition(code: ConditionCode) -> Option<Cond> {
    use ConditionCode::*;
    Some(match code {
        e => Cond::Eq,
        ne => Cond::Ne,
        l => Cond::Lt,
        le => Cond::Le,
        g => Cond::Gt,
        ge => Cond::Ge,
        b => Cond::Cc,
        be => Cond::Ls,
        a => Cond::Hi,
        ae => Cond::Cs,
        s => Cond::Mi,
        ns => Cond::Pl,
        o => Cond::Vs,
        no => Cond::Vc,
        _ => return Option::None,
    })
}

/// The value with each read of the stack pointer offset by `delta`, for a
/// value read before the stack pointer moves down by `delta`.
pub(super) fn stack_reads_before(value: Expr, delta: u64) -> Expr {
    match value {
        Expr::Reg {
            index: RSP,
            width: 64,
        } => Expr::bin(BinOp::Add, Lifter::read_reg(RSP, 64), Expr::constant(delta)),
        Expr::Bin { op, lhs, rhs } => Expr::bin(
            op,
            stack_reads_before(*lhs, delta),
            stack_reads_before(*rhs, delta),
        ),
        Expr::Load {
            addr,
            width,
            signed,
        } => Expr::Load {
            addr: Box::new(stack_reads_before(*addr, delta)),
            width,
            signed,
        },
        other => other,
    }
}

pub(super) fn stack_adjust(lifter: &mut Lifter, delta: i64) {
    let current = Lifter::read_reg(RSP, 64);
    let value = Expr::bin(BinOp::Add, current, Expr::constant(delta as u64));
    lifter.assign(RSP, lifter.word_bits(), value);
}