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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
//! The condition flags as the instruction that last set them leaves them,
//! and each condition as a test on that instruction's values.
//!
//! A subtraction's flags are what a comparison of its operands decides,
//! but a sum's carry and overflow, and the carry a logical operation
//! clears, are not; each condition is turned into the comparison that
//! decides it exactly, preferring one that reads as the source would.

use vlk_base::target::Isa;
use vlk_ir::ir::semantics::{mask, operand_width};
use vlk_ir::ir::{BinOp, Cond, Expr, Operation, Stmt};
use vlk_types::types::Precision;

#[cfg(test)]
mod smt_tests;

/// The flags as the code of the block so far leaves them, for a lifter
/// that models them. The block builder sets them where a block begins and
/// reads them where it ends.
#[derive(Default)]
pub(super) struct State {
    /// How the flags were last set, where that is known.
    pub pending: Option<Flags>,
    /// Whether an instruction since the block began may have changed the
    /// flags.
    pub touched: bool,
    /// Whether the instruction being lifted has said how it leaves the
    /// flags.
    pub written: bool,
}

/// The operation whose result set the flags.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Source {
    /// `lhs - rhs`, as a compare or a subtraction sets them.
    Difference,
    /// `lhs + rhs`.
    Sum,
    /// A logical result `lhs`, with carry and overflow cleared.
    Logic,
    /// Only zero and sign follow the result `lhs`; carry and overflow mean
    /// something else (an x86 shift by its width or more, `bsf`).
    Result,
    /// Only the zero flag, as a bit in `lhs`.
    Zero,
    /// An x86 shift's result `lhs`, with the carry the bit `rhs` it shifted
    /// out last; overflow is unknown.
    Shift,
    /// Set to the given N, Z, C and V bits, from high to low (`AArch64`).
    Fixed(u8),
    /// Only the carry is known, equal to the bit `lhs` (x86 `bt`).
    Carry,
    /// Only the carry and the overflow are known, both set when `lhs` is
    /// nonzero (an x86 multiply whose product did not fit).
    CarryOverflow,
    /// A floating-point comparison of `lhs` with `rhs`: x86 `ucomisd`,
    /// `AArch64` `fcmp`.
    FloatCompare(Precision),
    /// Loaded from the byte `lhs` as x86 `sahf` loads them: sign from bit
    /// 7, zero 6, adjust 4, parity 2 and carry 0. Overflow, which the byte
    /// does not hold, is one or zero in `rhs` when known.
    Stored { overflow: bool },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Flags {
    pub source: Source,
    pub lhs: Expr,
    pub rhs: Expr,
    /// False when the instruction left the carry as it was (x86 `inc`, `dec`).
    pub carry: bool,
    /// Whether the x86 parity and adjust flags are clear whatever the
    /// result, as `popcnt` leaves them.
    pub low_cleared: bool,
    /// For a conditional compare, the condition under the earlier flags that
    /// chose this compare; when it fails the flags are `otherwise`.
    pub chosen: Option<Choice>,
    /// The width the operation computed at, when wider than its result reads:
    /// a 64-bit `ands` with `0xff` leaves a result whose sign bit is clear.
    pub width: Option<u32>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Choice {
    pub cond: Cond,
    pub lhs: Expr,
    pub rhs: Expr,
    pub otherwise: u8,
}

/// What a condition is under the flags.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum Test {
    Always,
    Never,
    Compare(Cond, Expr, Expr),
    /// The flags do not decide it.
    Unknown,
}

impl Flags {
    pub(super) fn byte_tests(&self, isa: Isa) -> [(Test, u32); 5] {
        [
            (self.test(Cond::Mi, isa), 7),
            (self.test(Cond::Eq, isa), 6),
            (self.adjust(), 4),
            (self.parity(true), 2),
            (self.test(Cond::Cc, isa), 0),
        ]
    }

    pub fn difference(lhs: Expr, rhs: Expr) -> Self {
        Self::new(Source::Difference, lhs, rhs)
    }

    pub fn sum(lhs: Expr, rhs: Expr) -> Self {
        Self::new(Source::Sum, lhs, rhs)
    }

    pub fn logic(result: Expr) -> Self {
        Self::new(Source::Logic, result, Expr::constant(0))
    }

    /// A logical result of an operation `width` bits wide.
    pub fn logic_at(result: Expr, width: u32) -> Self {
        Self {
            width: Some(width),
            ..Self::logic(result)
        }
    }

    pub fn result(result: Expr) -> Self {
        Self::new(Source::Result, result, Expr::constant(0))
    }

    pub fn zero(bit: Expr) -> Self {
        Self::new(Source::Zero, bit, Expr::constant(0))
    }

    pub fn shift(result: Expr, carry: Expr) -> Self {
        Self::new(Source::Shift, result, carry)
    }

    pub fn float_compare(lhs: Expr, rhs: Expr, precision: Precision) -> Self {
        Self::new(Source::FloatCompare(precision), lhs, rhs)
    }

    /// Whether the x86 parity flag is set (`even`) or clear: set by an even
    /// count of ones in the result's low byte, and after a floating-point
    /// comparison by a NaN.
    pub fn parity(&self, even: bool) -> Test {
        let set = match self.source {
            _ if self.chosen.is_some() => Test::Unknown,
            Source::FloatCompare(precision) => {
                holds(Operation::FloatUnordered(precision), &self.lhs, &self.rhs)
            }
            _ if self.low_cleared => Test::Never,
            Source::Stored { .. } => bit_set(&self.lhs, 2),
            Source::Difference | Source::Sum | Source::Logic | Source::Shift => {
                let low = Expr::bin(BinOp::And, self.value(), Expr::constant(0xff));
                let folded = Expr::bin(
                    BinOp::And,
                    Expr::bin(
                        BinOp::Xor,
                        low.clone(),
                        Expr::bin(BinOp::Shr, low, Expr::constant(4)),
                    ),
                    Expr::constant(0xf),
                );
                // Bit n of 0x9669 is set when n has an even count of ones.
                bit_set(&Expr::bin(BinOp::Shr, Expr::constant(0x9669), folded), 0)
            }
            _ => Test::Unknown,
        };
        if even { set } else { not(set) }
    }

    /// Whether the x86 adjust flag is set: by a carry or borrow out of the
    /// result's low four bits.
    pub fn adjust(&self) -> Test {
        match self.source {
            _ if self.chosen.is_some() => Test::Unknown,
            Source::FloatCompare(_) => Test::Never,
            _ if self.low_cleared => Test::Never,
            Source::Stored { .. } => bit_set(&self.lhs, 4),
            Source::Difference | Source::Sum => {
                let operands = Expr::bin(BinOp::Xor, self.lhs.clone(), self.rhs.clone());
                bit_set(&Expr::bin(BinOp::Xor, operands, self.value()), 4)
            }
            _ => Test::Unknown,
        }
    }

    /// The value whose low bits set parity and adjust.
    fn value(&self) -> Expr {
        let op = match self.source {
            Source::Difference => BinOp::Sub,
            Source::Sum => BinOp::Add,
            _ => return self.lhs.clone(),
        };
        Expr::bin(op, self.lhs.clone(), self.rhs.clone())
    }

    /// Flags loaded from `byte` as x86 `sahf` loads them, with the overflow
    /// as one or zero when known.
    pub fn stored(byte: Expr, overflow: Option<Expr>) -> Self {
        let known = overflow.is_some();
        Self::new(
            Source::Stored { overflow: known },
            byte,
            overflow.unwrap_or(Expr::constant(0)),
        )
    }

    /// The same flags with parity and adjust clear.
    pub fn clearing_low(self) -> Self {
        Self {
            low_cleared: true,
            ..self
        }
    }

    pub fn carry(bit: Expr) -> Self {
        Self::new(Source::Carry, bit, Expr::constant(0))
    }

    pub fn carry_overflow(lost: Expr) -> Self {
        Self::new(Source::CarryOverflow, lost, Expr::constant(0))
    }

    pub fn fixed(nzcv: u8) -> Self {
        Self::new(Source::Fixed(nzcv), Expr::constant(0), Expr::constant(0))
    }

    fn new(source: Source, lhs: Expr, rhs: Expr) -> Self {
        Self {
            source,
            lhs,
            rhs,
            carry: true,
            low_cleared: false,
            chosen: None,
            width: None,
        }
    }

    /// These flags when `earlier` holds, and `otherwise` when it does not,
    /// as a conditional compare sets them; None when `earlier` is unknown.
    pub fn chosen_by(self, earlier: Test, otherwise: u8) -> Option<Self> {
        match earlier {
            Test::Always => Some(self),
            Test::Never => Some(Self::fixed(otherwise)),
            Test::Compare(cond, lhs, rhs) => Some(Self {
                chosen: Some(Choice {
                    cond,
                    lhs,
                    rhs,
                    otherwise,
                }),
                ..self
            }),
            Test::Unknown => None,
        }
    }

    /// The same flags with each operand read from a `FLAG_CARRIED` register
    /// of its width, and the operands those registers receive.
    pub fn carried(&self) -> (Flags, Vec<Expr>) {
        let mut template = self.clone();
        let mut values = Vec::new();
        for (slot, operand) in vlk_ir::ir::FLAG_CARRIED
            .into_iter()
            .zip(template.operands_mut())
        {
            let width = match vlk_ir::ir::semantics::width(operand) {
                0 => 64,
                bits => bits as u8,
            };
            values.push(std::mem::replace(operand, Expr::reg(slot, width)));
        }
        (template, values)
    }

    /// Every expression the flags read, in the order of `FLAG_OPERANDS`.
    pub fn operands_mut(&mut self) -> Vec<&mut Expr> {
        let mut operands = vec![&mut self.lhs, &mut self.rhs];
        if let Some(choice) = &mut self.chosen {
            operands.push(&mut choice.lhs);
            operands.push(&mut choice.rhs);
        }
        operands
    }

    /// The flags with the carry left unknown.
    pub fn keeping_carry(self) -> Self {
        Self {
            carry: false,
            ..self
        }
    }

    /// `cond` under these flags. Conditions are named as after a compare,
    /// so on x86, whose carry is a borrow, `Cs` is "above or equal".
    pub fn test(&self, cond: Cond, isa: Isa) -> Test {
        let own = self.own_test(cond, isa);
        let Some(choice) = &self.chosen else {
            return own;
        };
        let when = Test::Compare(choice.cond, choice.lhs.clone(), choice.rhs.clone());
        if fixed_holds(cond, choice.otherwise) {
            not(both(when, not(own)))
        } else {
            both(when, own)
        }
    }

    fn own_test(&self, cond: Cond, isa: Isa) -> Test {
        let (a, b) = (&self.lhs, &self.rhs);
        let reads_carry = matches!(cond, Cond::Cs | Cond::Cc | Cond::Hi | Cond::Ls);
        if reads_carry && !self.carry {
            return Test::Unknown;
        }
        let bits = operand_width(a, b);
        let compare = |cond, lhs: &Expr, rhs: &Expr| Test::Compare(cond, lhs.clone(), rhs.clone());
        let zero = Expr::constant(0);
        match self.source {
            Source::Zero => match cond {
                Cond::Eq => compare(Cond::Ne, a, &zero),
                Cond::Ne => compare(Cond::Eq, a, &zero),
                _ => Test::Unknown,
            },
            Source::CarryOverflow => match cond {
                Cond::Cc | Cond::Vs => compare(Cond::Ne, a, &zero),
                Cond::Cs | Cond::Vc => compare(Cond::Eq, a, &zero),
                _ => Test::Unknown,
            },
            Source::Carry => {
                // On x86 "below" asks for a set carry, on AArch64 `hs`.
                let set = if matches!(isa, Isa::I386 | Isa::X86_64) {
                    Cond::Cc
                } else {
                    Cond::Cs
                };
                if cond == set {
                    compare(Cond::Ne, a, &zero)
                } else if cond == set.invert() {
                    compare(Cond::Eq, a, &zero)
                } else {
                    Test::Unknown
                }
            }
            Source::FloatCompare(precision) => float_test(a, b, precision, cond, isa),
            Source::Stored { overflow } => stored_test(a, b, overflow, cond),
            Source::Fixed(nzcv) => {
                if fixed_holds(cond, nzcv) {
                    Test::Always
                } else {
                    Test::Never
                }
            }
            Source::Difference => {
                let difference = || Expr::bin(BinOp::Sub, a.clone(), b.clone());
                // Overflow when the operands differ in sign and the
                // difference differs from the first.
                let overflow = || {
                    Expr::bin(
                        BinOp::And,
                        Expr::bin(BinOp::Xor, a.clone(), b.clone()),
                        Expr::bin(BinOp::Xor, a.clone(), difference()),
                    )
                };
                match (cond, b) {
                    (Cond::Mi, Expr::Const(0)) => compare(Cond::Lt, a, b),
                    (Cond::Pl, Expr::Const(0)) => compare(Cond::Ge, a, b),
                    (Cond::Mi, _) => Test::Compare(Cond::Lt, difference(), zero),
                    (Cond::Pl, _) => Test::Compare(Cond::Ge, difference(), zero),
                    (Cond::Vs, _) => Test::Compare(Cond::Lt, overflow(), zero),
                    (Cond::Vc, _) => Test::Compare(Cond::Ge, overflow(), zero),
                    _ => compare(cond, a, b),
                }
            }
            // The result reads narrower than the operation computed it: its
            // sign bit, that of the operation, is clear.
            Source::Logic
                if self.width.is_some_and(|width| width > bits)
                    && matches!(
                        cond,
                        Cond::Mi | Cond::Pl | Cond::Lt | Cond::Ge | Cond::Gt | Cond::Le
                    ) =>
            {
                match cond {
                    Cond::Mi | Cond::Lt => Test::Never,
                    Cond::Pl | Cond::Ge => Test::Always,
                    Cond::Gt => compare(Cond::Ne, a, &zero),
                    _ => compare(Cond::Eq, a, &zero),
                }
            }
            Source::Logic | Source::Result | Source::Shift => {
                let sign = |cond| match cond {
                    Cond::Mi => Cond::Lt,
                    Cond::Pl => Cond::Ge,
                    other => other,
                };
                match cond {
                    Cond::Eq | Cond::Ne | Cond::Mi | Cond::Pl => compare(sign(cond), a, &zero),
                    // On x86, where "below" asks for a set carry.
                    Cond::Cc if self.source == Source::Shift => compare(Cond::Ne, b, &zero),
                    Cond::Cs if self.source == Source::Shift => compare(Cond::Eq, b, &zero),
                    Cond::Hi | Cond::Ls if self.source == Source::Shift => {
                        let above = both(
                            Test::Compare(Cond::Eq, b.clone(), zero.clone()),
                            Test::Compare(Cond::Ne, a.clone(), zero),
                        );
                        if cond == Cond::Hi { above } else { not(above) }
                    }
                    _ if self.source != Source::Logic => Test::Unknown,
                    Cond::Vs => Test::Never,
                    Cond::Vc => Test::Always,
                    Cond::Ge | Cond::Lt | Cond::Gt | Cond::Le => compare(cond, a, &zero),
                    // The carry is clear: on AArch64 that fails `hs`; on x86
                    // it passes "above or equal", and "above" asks for nonzero.
                    _ => match (isa, cond) {
                        (Isa::I386 | Isa::X86_64, Cond::Cs)
                        | (Isa::AArch64, Cond::Cc | Cond::Ls) => Test::Always,
                        (Isa::I386 | Isa::X86_64, Cond::Cc)
                        | (Isa::AArch64, Cond::Cs | Cond::Hi) => Test::Never,
                        (_, Cond::Hi) => compare(Cond::Ne, a, &zero),
                        _ => compare(Cond::Eq, a, &zero),
                    },
                }
            }
            Source::Sum => sum(a, b, bits, cond, isa),
        }
    }
}

/// Whether bit `bit` of `value` is set.
fn bit_set(value: &Expr, bit: u64) -> Test {
    let shifted = match bit {
        0 => value.clone(),
        _ => Expr::bin(BinOp::Shr, value.clone(), Expr::constant(bit)),
    };
    Test::Compare(
        Cond::Ne,
        Expr::bin(BinOp::And, shifted, Expr::constant(1)),
        Expr::constant(0),
    )
}

/// `cond` under x86 flags loaded from `byte`, with the overflow as one or
/// zero in `overflow` when `known`.
fn stored_test(byte: &Expr, overflow: &Expr, known: bool, cond: Cond) -> Test {
    let zero = || Expr::constant(0);
    let sign = || {
        Expr::bin(
            BinOp::And,
            Expr::bin(BinOp::Shr, byte.clone(), Expr::constant(7)),
            Expr::constant(1),
        )
    };
    // Neither carry nor zero: "above".
    let above = Test::Compare(
        Cond::Eq,
        Expr::bin(BinOp::And, byte.clone(), Expr::constant(0x41)),
        zero(),
    );
    let sign_is_overflow = || Test::Compare(Cond::Eq, sign(), overflow.clone());
    let greater = || both(not(bit_set(byte, 6)), sign_is_overflow());
    match cond {
        Cond::Eq => bit_set(byte, 6),
        Cond::Ne => not(bit_set(byte, 6)),
        Cond::Cc => bit_set(byte, 0),
        Cond::Cs => not(bit_set(byte, 0)),
        Cond::Mi => bit_set(byte, 7),
        Cond::Pl => not(bit_set(byte, 7)),
        Cond::Hi => above,
        Cond::Ls => not(above),
        _ if !known => Test::Unknown,
        Cond::Vs => Test::Compare(Cond::Ne, overflow.clone(), zero()),
        Cond::Vc => Test::Compare(Cond::Eq, overflow.clone(), zero()),
        Cond::Ge => sign_is_overflow(),
        Cond::Lt => not(sign_is_overflow()),
        Cond::Gt => greater(),
        Cond::Le => not(greater()),
    }
}

/// The test that a floating-point comparison holds.
fn holds(op: Operation, a: &Expr, b: &Expr) -> Test {
    Test::Compare(
        Cond::Ne,
        Expr::apply(op, vec![a.clone(), b.clone()]),
        Expr::constant(0),
    )
}

/// `cond` after comparing the floats `a` and `b`. On x86 a NaN sets zero,
/// parity and carry, and a smaller `a` the carry, while sign and overflow
/// are clear; on `AArch64` a smaller `a` sets N, an equal one Z and C, a
/// larger one C, and a NaN C and V.
fn float_test(a: &Expr, b: &Expr, precision: Precision, cond: Cond, isa: Isa) -> Test {
    let less = |x, y| holds(Operation::FloatLess(precision), x, y);
    let less_equal = |x, y| holds(Operation::FloatLessEqual(precision), x, y);
    let equal = holds(Operation::FloatEqual(precision), a, b);
    let unordered = holds(Operation::FloatUnordered(precision), a, b);
    // Equal, or not comparable.
    let equal_or_unordered = || not(both(not(equal.clone()), not(unordered.clone())));
    match isa {
        Isa::I386 | Isa::X86_64 => match cond {
            Cond::Eq | Cond::Le => equal_or_unordered(),
            Cond::Ne | Cond::Gt => not(equal_or_unordered()),
            Cond::Cc => not(less_equal(b, a)),
            Cond::Cs => less_equal(b, a),
            Cond::Hi => less(b, a),
            Cond::Ls => not(less(b, a)),
            Cond::Mi | Cond::Vs | Cond::Lt => Test::Never,
            Cond::Pl | Cond::Vc | Cond::Ge => Test::Always,
        },
        _ => match cond {
            Cond::Eq => equal,
            Cond::Ne => not(equal),
            Cond::Cc | Cond::Mi => less(a, b),
            Cond::Cs | Cond::Pl => not(less(a, b)),
            Cond::Vs => unordered,
            Cond::Vc => not(unordered),
            Cond::Hi => not(less_equal(a, b)),
            Cond::Ls => less_equal(a, b),
            Cond::Ge => less_equal(b, a),
            Cond::Lt => not(less_equal(b, a)),
            Cond::Gt => less(b, a),
            Cond::Le => not(less(b, a)),
        },
    }
}

/// `cond` after `a + b` at `bits` wide.
fn sum(a: &Expr, b: &Expr, bits: u32, cond: Cond, isa: Isa) -> Test {
    let all = mask(bits);
    let lowest = 1u64 << (bits - 1);
    let s = Expr::bin(BinOp::Add, a.clone(), b.clone());
    if let Expr::Const(c) = b {
        let c = c & all;
        // Adding zero carries nothing and cannot overflow: AArch64 reads
        // that as a logical result, x86 as a compare with zero.
        if c == 0 {
            let flags = match isa {
                Isa::I386 | Isa::X86_64 => Flags::difference(a.clone(), Expr::constant(0)),
                _ => Flags::logic(a.clone()),
            };
            return flags.test(cond, isa);
        }
        // `a + c` against zero is `a` against `-c`, which fits unless `c`
        // is the lowest signed value.
        if c != lowest {
            let negated = Expr::constant(c.wrapping_neg() & all);
            let cond = match (isa, cond) {
                (_, Cond::Mi) => return Test::Compare(Cond::Lt, s, Expr::constant(0)),
                (_, Cond::Pl) => return Test::Compare(Cond::Ge, s, Expr::constant(0)),
                (_, Cond::Vs | Cond::Vc) => cond,
                // On x86 the carry of a sum is set when `a >= -c`, which
                // fails "above or equal" and "above".
                (Isa::I386 | Isa::X86_64, Cond::Cs | Cond::Hi) => Cond::Cc,
                (Isa::I386 | Isa::X86_64, Cond::Cc | Cond::Ls) => Cond::Cs,
                (_, other) => other,
            };
            if !matches!(cond, Cond::Vs | Cond::Vc) {
                return Test::Compare(cond, a.clone(), negated);
            }
        }
    }
    let zero = || Expr::constant(0);
    let negated = match b {
        Expr::Const(c) => Expr::constant(c.wrapping_neg() & all),
        _ => Expr::bin(BinOp::Sub, zero(), b.clone()),
    };
    let is_zero = Test::Compare(Cond::Eq, a.clone(), negated);
    // A carry out of the sum: `a > ~b`.
    let inverted = Expr::bin(BinOp::Xor, b.clone(), Expr::constant(all));
    let carry = Test::Compare(Cond::Hi, a.clone(), inverted);
    // The flags' carry: set by a carry on AArch64, clear on x86.
    let carry_flag = match isa {
        Isa::I386 | Isa::X86_64 => not(carry),
        _ => carry,
    };
    // Overflow when both operands differ in sign from the sum.
    let overflow_bits = Expr::bin(
        BinOp::And,
        Expr::bin(BinOp::Xor, a.clone(), s.clone()),
        Expr::bin(BinOp::Xor, b.clone(), s.clone()),
    );
    let overflow = Test::Compare(Cond::Lt, overflow_bits.clone(), zero());
    // Sign equal to overflow: the sum before wrapping is not negative.
    let not_negative = Test::Compare(
        Cond::Ge,
        Expr::bin(BinOp::Xor, s.clone(), overflow_bits),
        zero(),
    );
    match cond {
        Cond::Eq => is_zero,
        Cond::Ne => not(is_zero),
        Cond::Mi => Test::Compare(Cond::Lt, s, zero()),
        Cond::Pl => Test::Compare(Cond::Ge, s, zero()),
        Cond::Vs => overflow,
        Cond::Vc => not(overflow),
        Cond::Cs => carry_flag,
        Cond::Cc => not(carry_flag),
        Cond::Hi => both(carry_flag, not(is_zero)),
        Cond::Ls => not(both(carry_flag, not(is_zero))),
        Cond::Ge => not_negative,
        Cond::Lt => not(not_negative),
        Cond::Gt => both(not(is_zero), not_negative),
        Cond::Le => not(both(not(is_zero), not_negative)),
    }
}

/// One when `a + b + carry` carries out of `bits`, for a carry of one or zero.
pub(super) fn carry_out(a: &Expr, b: &Expr, carry: Expr, bits: u32) -> Expr {
    let inverted = Expr::bin(BinOp::Xor, b.clone(), Expr::constant(mask(bits)));
    let beyond = |cond| truth(Test::Compare(cond, a.clone(), inverted.clone()));
    choose(carry, beyond(Cond::Cs), beyond(Cond::Hi))
}

/// One when `a - b - borrow` borrows, for a borrow of one or zero.
pub(super) fn borrow_out(a: &Expr, b: &Expr, borrow: Expr) -> Expr {
    let below = |cond| truth(Test::Compare(cond, a.clone(), b.clone()));
    choose(borrow, below(Cond::Ls), below(Cond::Cc))
}

/// `then` when `bit` is set and `els` otherwise.
fn choose(bit: Expr, then: Expr, els: Expr) -> Expr {
    Expr::Select {
        cond: Cond::Ne,
        lhs: Box::new(bit),
        rhs: Box::new(Expr::constant(0)),
        then: Box::new(then),
        els: Box::new(els),
    }
}

/// One when the test holds and zero otherwise.
fn truth(test: Test) -> Expr {
    test.select(Expr::constant(1), Expr::constant(0))
        .expect("a decided test")
}

fn not(test: Test) -> Test {
    match test {
        Test::Always => Test::Never,
        Test::Never => Test::Always,
        Test::Compare(cond, lhs, rhs) => Test::Compare(cond.invert(), lhs, rhs),
        Test::Unknown => Test::Unknown,
    }
}

/// Both tests hold, as one comparison of a selected value with zero.
fn both(first: Test, second: Test) -> Test {
    match (first, second) {
        (Test::Never, _) | (_, Test::Never) => Test::Never,
        (Test::Always, other) | (other, Test::Always) => other,
        (Test::Unknown, _) | (_, Test::Unknown) => Test::Unknown,
        (Test::Compare(c1, l1, r1), Test::Compare(c2, l2, r2)) => {
            let inner = Expr::truth(c2, l2, r2);
            let outer = Expr::Select {
                cond: c1,
                lhs: Box::new(l1),
                rhs: Box::new(r1),
                then: Box::new(inner),
                els: Box::new(Expr::constant(0)),
            };
            Test::Compare(Cond::Ne, outer, Expr::constant(0))
        }
    }
}

impl Test {
    /// `then` when the test holds and `els` otherwise; None when the flags
    /// do not decide it.
    pub fn select(self, then: Expr, els: Expr) -> Option<Expr> {
        // A test that a truth value is nonzero, choosing one or zero, is
        // that truth value.
        if let (Test::Compare(Cond::Ne, value, Expr::Const(0)), Expr::Const(1), Expr::Const(0)) =
            (&self, &then, &els)
            && is_truth(value)
        {
            return Some(value.clone());
        }
        match self {
            Test::Always => Some(then),
            Test::Never => Some(els),
            Test::Compare(cond, lhs, rhs) => Some(Expr::Select {
                cond,
                lhs: Box::new(lhs),
                rhs: Box::new(rhs),
                then: Box::new(then),
                els: Box::new(els),
            }),
            Test::Unknown => None,
        }
    }

    pub fn invert(self) -> Test {
        not(self)
    }

    /// A branch to `target` when the test holds; none when it never does,
    /// and one on flags from elsewhere, named by `cond`, when the flags
    /// here do not decide it.
    pub fn branch(self, cond: Cond, target: u64) -> Option<Stmt> {
        match self {
            Test::Always => Some(Stmt::Br {
                target: Some(target),
                at: None,
            }),
            Test::Never => None,
            Test::Compare(cond, lhs, rhs) => Some(Stmt::Brif {
                cond,
                lhs,
                rhs,
                target,
                at: None,
            }),
            Test::Unknown => Some(Stmt::BrifFlags {
                cond,
                target,
                at: None,
            }),
        }
    }
}

/// Whether `cond` holds for `AArch64` flags N, Z, C, V given as four bits.
fn fixed_holds(cond: Cond, nzcv: u8) -> bool {
    let (n, z, c, v) = (nzcv & 8 != 0, nzcv & 4 != 0, nzcv & 2 != 0, nzcv & 1 != 0);
    match cond {
        Cond::Eq => z,
        Cond::Ne => !z,
        Cond::Cs => c,
        Cond::Cc => !c,
        Cond::Mi => n,
        Cond::Pl => !n,
        Cond::Vs => v,
        Cond::Vc => !v,
        Cond::Hi => c && !z,
        Cond::Ls => !(c && !z),
        Cond::Ge => n == v,
        Cond::Lt => n != v,
        Cond::Gt => !z && n == v,
        Cond::Le => !(!z && n == v),
    }
}

/// Whether `expr` is one or zero: a choice between truth values.
fn is_truth(expr: &Expr) -> bool {
    match expr {
        Expr::Const(0 | 1) => true,
        Expr::Select { then, els, .. } => is_truth(then) && is_truth(els),
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::{Flags, Test, fixed_holds};
    use vlk_base::target::Isa;
    use vlk_eval::eval::hash;
    use vlk_ir::ir::semantics::{evaluate, holds, mask, operand_width, sign_extend};
    use vlk_ir::ir::{Cond, Expr};

    const CONDITIONS: [Cond; 14] = [
        Cond::Eq,
        Cond::Ne,
        Cond::Cs,
        Cond::Cc,
        Cond::Mi,
        Cond::Pl,
        Cond::Vs,
        Cond::Vc,
        Cond::Hi,
        Cond::Ls,
        Cond::Ge,
        Cond::Lt,
        Cond::Gt,
        Cond::Le,
    ];

    /// N, Z, C and V as four bits, with C as `AArch64` means it: a
    /// difference's "no borrow" and a sum's carry. x86 keeps a borrow and
    /// a carry, which the conditions read inverted.
    fn reference(kind: u8, a: u64, b: u64, bits: u32, isa: Isa) -> u8 {
        let all = mask(bits);
        let (a, b) = (a & all, b & all);
        let (result, carry, overflow) = match kind {
            0 => {
                let r = a.wrapping_sub(b) & all;
                (r, a >= b, ((a ^ b) & (a ^ r)) >> (bits - 1) & 1 == 1)
            }
            1 => {
                let r = a.wrapping_add(b) & all;
                let carry = u128::from(a) + u128::from(b) > u128::from(all);
                let carry = if matches!(isa, Isa::I386 | Isa::X86_64) {
                    !carry
                } else {
                    carry
                };
                (r, carry, ((a ^ r) & (b ^ r)) >> (bits - 1) & 1 == 1)
            }
            _ => (a, matches!(isa, Isa::I386 | Isa::X86_64), false),
        };
        let negative = sign_extend(result, bits) < 0;
        u8::from(negative) << 3
            | u8::from(result == 0) << 2
            | u8::from(carry) << 1
            | u8::from(overflow)
    }

    fn decided(test: &Test, registers: &[u64; 2]) -> Option<bool> {
        let value =
            |expr: &Expr| evaluate(expr, &|index| registers.get(usize::from(index)).copied());
        match test {
            Test::Always => Some(true),
            Test::Never => Some(false),
            Test::Compare(cond, lhs, rhs) => Some(holds(
                *cond,
                value(lhs)?,
                value(rhs)?,
                operand_width(lhs, rhs),
            )),
            Test::Unknown => None,
        }
    }

    #[test]
    fn every_condition_decides_as_the_flags_do() {
        const EDGES: [u64; 7] = [0, 1, 0x7f, 0x80, 0x7fff_ffff, 0x8000_0000, 1 << 63];
        for isa in [Isa::AArch64, Isa::X86_64] {
            for bits in [8u32, 32, 64] {
                for seed in 0..600u64 {
                    let pick = |key: u64| match hash(seed, key) % 3 {
                        0 => EDGES[(hash(seed, key + 9) % 7) as usize].wrapping_sub(hash(seed, 5) % 2),
                        _ => hash(seed, key + 3),
                    } & mask(bits);
                    let (a, b) = (pick(1), pick(2));
                    let lhs = Expr::reg(0, bits as u8);
                    // The second operand both as a register and as a constant.
                    for rhs in [Expr::reg(1, bits as u8), Expr::constant(b)] {
                        for kind in 0..3u8 {
                            let flags = match kind {
                                0 => Flags::difference(lhs.clone(), rhs.clone()),
                                1 => Flags::sum(lhs.clone(), rhs.clone()),
                                _ => Flags::logic(lhs.clone()),
                            };
                            let nzcv = reference(kind, a, b, bits, isa);
                            for cond in CONDITIONS {
                                let test = flags.test(cond, isa);
                                assert_eq!(
                                    decided(&test, &[a, b]),
                                    Some(fixed_holds(cond, nzcv)),
                                    "{isa:?} {bits}-bit kind {kind} {cond:?} a={a:#x} b={b:#x} rhs={rhs:?}: {test:?}"
                                );
                            }
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn a_conditional_compare_tests_both_conditions() {
        // cmp x0, x1; ccmp x2, x3, #0, eq: `eq` holds when both pairs match.
        let earlier = Flags::difference(Expr::reg(0, 64), Expr::reg(1, 64));
        let flags = Flags::difference(Expr::reg(2, 64), Expr::reg(3, 64))
            .chosen_by(earlier.test(Cond::Eq, Isa::AArch64), 0)
            .expect("known");
        let test = flags.test(Cond::Eq, Isa::AArch64);
        let value = |registers: [u64; 4]| match &test {
            Test::Compare(cond, lhs, rhs) => {
                let read = |expr: &Expr| {
                    evaluate(expr, &|index| registers.get(usize::from(index)).copied())
                        .expect("registers")
                };
                holds(*cond, read(lhs), read(rhs), operand_width(lhs, rhs))
            }
            other => panic!("{other:?}"),
        };
        assert!(value([1, 1, 2, 2]));
        assert!(!value([1, 0, 2, 2]));
        assert!(!value([1, 1, 2, 3]));
    }
}