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
use super::{
    BinOp, Expr, Flags, Instruction, LaneParity, Lifter, Operand, Operation, Precision, RegRef,
    Rounding, Stmt, a64, destination, immediate_lane, lane, lane_value, mask_bits, register_width,
    replicated, shift_count, simd, source_halves, unzip, vector, write_narrow,
};

fn unary_operation(opcode: a64::Opcode, precision: Precision) -> Option<Operation> {
    use a64::Opcode as O;
    Some(match opcode {
        O::FABS => Operation::FloatAbs(precision),
        O::FNEG => Operation::FloatNegate(precision),
        O::FSQRT => Operation::FloatSqrt(precision),
        O::FRINTA => Operation::FloatRound(precision, Rounding::NearestAway),
        O::FRINTM => Operation::FloatRound(precision, Rounding::Down),
        O::FRINTP => Operation::FloatRound(precision, Rounding::Up),
        O::FRINTZ => Operation::FloatRound(precision, Rounding::TowardZero),
        O::FRINTN | O::FRINTX | O::FRINTI => {
            Operation::FloatRound(precision, Rounding::NearestEven)
        }
        _ => return None,
    })
}

fn integer_conversion(opcode: a64::Opcode) -> Option<(bool, Rounding)> {
    use a64::Opcode as O;
    Some(match opcode {
        O::FCVTZS => (true, Rounding::TowardZero),
        O::FCVTZU => (false, Rounding::TowardZero),
        O::FCVTNS => (true, Rounding::NearestEven),
        O::FCVTNU => (false, Rounding::NearestEven),
        O::FCVTAS => (true, Rounding::NearestAway),
        O::FCVTAU => (false, Rounding::NearestAway),
        O::FCVTMS => (true, Rounding::Down),
        O::FCVTMU => (false, Rounding::Down),
        O::FCVTPS => (true, Rounding::Up),
        O::FCVTPU => (false, Rounding::Up),
        _ => return None,
    })
}

/// `2^exponent` as a float of `precision`.
fn power_of_two(exponent: u32, precision: Precision) -> Expr {
    let value = 2f64.powi(exponent as i32);
    Expr::constant(match precision {
        Precision::Double => value.to_bits(),
        Precision::Single => u64::from((value as f32).to_bits()),
    })
}

fn precision(bits: u16) -> Option<Precision> {
    match bits {
        32 => Some(Precision::Single),
        64 => Some(Precision::Double),
        _ => None,
    }
}

pub(super) fn half_immediate(value: f64) -> Option<u64> {
    let bits = value.to_bits();
    let Some(exponent) = ((bits >> 52) & 0x7ff).checked_sub(1008) else {
        return None;
    };
    if !(12..=19).contains(&exponent) || bits & ((1 << 48) - 1) != 0 {
        return None;
    }
    Some(((bits >> 48) & 0x8000) | (exponent << 10) | ((bits >> 42) & 0x3ff))
}

/// `f` on each float lane of `bits` of `args`, taken lane by lane: a
/// double in each half, or two singles. A scalar keeps only the lowest.
fn float_lanes(
    (bits, full, scalar): (u8, bool, bool),
    args: &[(Expr, Expr)],
    f: impl Fn(Vec<Expr>) -> Expr,
) -> (Expr, Expr) {
    let half = |part: fn(&(Expr, Expr)) -> Expr| {
        let parts: Vec<Expr> = args.iter().map(part).collect();
        if bits == 64 {
            return f(parts);
        }
        let low = f(parts
            .iter()
            .map(|part| Expr::bin(BinOp::And, part.clone(), Expr::constant(0xffff_ffff)))
            .collect());
        if scalar {
            return low;
        }
        let high = f(parts
            .iter()
            .map(|part| Expr::bin(BinOp::Shr, part.clone(), Expr::constant(32)))
            .collect());
        Expr::bin(
            BinOp::Or,
            low,
            Expr::bin(BinOp::Shl, high, Expr::constant(32)),
        )
    };
    let low = half(|part| part.0.clone());
    let high = if full {
        half(|part| part.1.clone())
    } else {
        Expr::constant(0)
    };
    (low, high)
}

/// The floating-point instructions on vectors, and on scalars where only
/// the vector forms' encodings give them.
pub(super) fn lift_vector_float(lifter: &mut Lifter, ins: &Instruction) -> Option<()> {
    use a64::Opcode as O;
    let ops = ins.operands;
    let (dest, scalar) = match ops[0] {
        Operand::SIMDRegister(a64::SIMDSizeCode::S, number) => {
            ((vlk_arch::arch::aarch64::V0 + 2 * number, false, 32), true)
        }
        operand => {
            let Some(vector_register) = vector(operand) else {
                return None;
            };
            (vector_register, false)
        }
    };
    let source = |operand: Operand| match operand {
        Operand::SIMDRegister(..) => {
            let Some((slot, _)) = simd(operand) else {
                return None;
            };
            Some((Expr::reg(slot, 64), Expr::constant(0)))
        }
        operand => source_halves(operand),
    };
    let shape = (dest.2, dest.1, scalar);
    let p = precision(u16::from(dest.2));
    let apply = |op: Operation, args: Vec<Expr>| Expr::apply(op, args);
    let truth = |value: Expr| {
        Expr::bin(
            BinOp::Mul,
            value,
            Expr::constant(mask_bits(u64::from(dest.2))),
        )
    };
    let (low, high) = match ins.opcode {
        O::FADD | O::FSUB | O::FMUL | O::FDIV | O::FABD => {
            let Some(p) = p else {
                return None;
            };
            let Some(first_source) = source(ops[1]) else {
                return None;
            };
            let Some(second_source) = source(ops[2]) else {
                return None;
            };
            let args = [first_source, second_source];
            float_lanes(shape, &args, |mut lane| {
                let (a, b) = (lane.remove(0), lane.remove(0));
                match ins.opcode {
                    O::FADD => apply(Operation::FloatAdd(p), vec![a, b]),
                    O::FSUB => apply(Operation::FloatSub(p), vec![a, b]),
                    O::FMUL => apply(Operation::FloatMul(p), vec![a, b]),
                    O::FDIV => apply(Operation::FloatDiv(p), vec![a, b]),
                    _ => apply(
                        Operation::FloatAbs(p),
                        vec![apply(Operation::FloatSub(p), vec![a, b])],
                    ),
                }
            })
        }
        // The product added to the destination, or for fmls the first
        // source negated, rounded once.
        O::FMLA | O::FMLS => {
            let Some(p) = p else {
                return None;
            };
            let accumulator = (Expr::reg(dest.0, 64), Expr::reg(dest.0 + 1, 64));
            let Some(first_source) = source(ops[1]) else {
                return None;
            };
            let Some(second_source) = source(ops[2]) else {
                return None;
            };
            let args = [first_source, second_source, accumulator];
            float_lanes(shape, &args, |mut lane| {
                let (n, m, accumulator) = (lane.remove(0), lane.remove(0), lane.remove(0));
                let n = if ins.opcode == O::FMLS {
                    apply(Operation::FloatNegate(p), vec![n])
                } else {
                    n
                };
                apply(Operation::FloatMulAdd(p), vec![n, m, accumulator])
            })
        }
        O::FABS
        | O::FNEG
        | O::FSQRT
        | O::FRINTN
        | O::FRINTA
        | O::FRINTM
        | O::FRINTP
        | O::FRINTZ
        | O::FRINTX
        | O::FRINTI => {
            let Some(p) = p else {
                return None;
            };
            let Some(op) = unary_operation(ins.opcode, p) else {
                return None;
            };
            let Some(first_source) = source(ops[1]) else {
                return None;
            };
            float_lanes(shape, &[first_source], |lane| apply(op, lane))
        }
        // All ones where the comparison holds; against zero, or the
        // absolute values for the facge and facgt.
        O::FCMEQ | O::FCMGE | O::FCMGT | O::FCMLE | O::FCMLT | O::FACGE | O::FACGT => {
            let Some(p) = p else {
                return None;
            };
            let Some(first_source) = source(ops[1]) else {
                return None;
            };
            let Some(second_source) = source(ops[2]) else {
                return None;
            };
            let args = [first_source, second_source];
            float_lanes(shape, &args, |mut lane| {
                let (a, b) = (lane.remove(0), lane.remove(0));
                let abs = |value| apply(Operation::FloatAbs(p), vec![value]);
                truth(match ins.opcode {
                    O::FCMEQ => apply(Operation::FloatEqual(p), vec![a, b]),
                    O::FCMGE => apply(Operation::FloatLessEqual(p), vec![b, a]),
                    O::FCMGT => apply(Operation::FloatLess(p), vec![b, a]),
                    O::FCMLE => apply(Operation::FloatLessEqual(p), vec![a, b]),
                    O::FCMLT => apply(Operation::FloatLess(p), vec![a, b]),
                    O::FACGE => apply(Operation::FloatLessEqual(p), vec![abs(b), abs(a)]),
                    _ => apply(Operation::FloatLess(p), vec![abs(b), abs(a)]),
                })
            })
        }
        // A fixed-point source has that many fraction bits; dividing by a
        // power of two adds no rounding.
        O::SCVTF | O::UCVTF => {
            let Some(p) = p else {
                return None;
            };
            let fraction = shift_count(ops[2]).unwrap_or(0) as u32;
            let Some(first_source) = source(ops[1]) else {
                return None;
            };
            float_lanes(shape, &[first_source], |lane| {
                let converted = apply(
                    Operation::IntToFloat {
                        signed: ins.opcode == O::SCVTF,
                        bits: dest.2,
                        to: p,
                    },
                    lane,
                );
                if fraction > 0 {
                    apply(
                        Operation::FloatDiv(p),
                        vec![converted, power_of_two(fraction, p)],
                    )
                } else {
                    converted
                }
            })
        }
        O::FCVTZS
        | O::FCVTZU
        | O::FCVTNS
        | O::FCVTNU
        | O::FCVTAS
        | O::FCVTAU
        | O::FCVTMS
        | O::FCVTMU
        | O::FCVTPS
        | O::FCVTPU => {
            let Some(p) = p else {
                return None;
            };
            let Some((signed, rounding)) = integer_conversion(ins.opcode) else {
                return None;
            };
            let fraction = shift_count(ops[2]).unwrap_or(0) as u32;
            let Some(first_source) = source(ops[1]) else {
                return None;
            };
            float_lanes(shape, &[first_source], |mut lane| {
                let value = lane.remove(0);
                // A fixed-point result keeps that many fraction bits.
                let value = if fraction > 0 {
                    apply(
                        Operation::FloatMul(p),
                        vec![value, power_of_two(fraction, p)],
                    )
                } else {
                    value
                };
                apply(
                    Operation::FloatToInt {
                        signed,
                        bits: dest.2,
                        from: p,
                        saturating: true,
                        rounding,
                    },
                    vec![value],
                )
            })
        }
        // Neighbouring lanes of the two sources taken as one added, or the
        // two lanes of one source for the scalar form.
        O::FADDP => {
            let Some(p) = p else {
                return None;
            };
            let add = |lane: Vec<Expr>| apply(Operation::FloatAdd(p), lane);
            let Some(a) = source(ops[1]) else {
                return None;
            };
            if matches!(ops[2], Operand::Nothing) {
                let pair = if dest.2 == 64 {
                    (a.0, a.1)
                } else {
                    (
                        Expr::bin(BinOp::And, a.0.clone(), Expr::constant(0xffff_ffff)),
                        Expr::bin(BinOp::Shr, a.0, Expr::constant(32)),
                    )
                };
                (add(vec![pair.0, pair.1]), Expr::constant(0))
            } else {
                let Some(b) = source(ops[2]) else {
                    return None;
                };
                let even = unzip(a.clone(), b.clone(), dest.2, dest.1, LaneParity::Even);
                let odd = unzip(a, b, dest.2, dest.1, LaneParity::Odd);
                float_lanes(shape, &[even, odd], add)
            }
        }
        // Singles to doubles, from the low half or, for fcvtl2, the high one.
        O::FCVTL | O::FCVTL2 if dest.2 == 64 => {
            let Some((low, high)) = source(ops[1]) else {
                return None;
            };
            let half = if ins.opcode == O::FCVTL2 { high } else { low };
            let widen = |single: Expr| {
                apply(
                    Operation::FloatConvert {
                        from: Precision::Single,
                        to: Precision::Double,
                    },
                    vec![single],
                )
            };
            (
                widen(Expr::bin(
                    BinOp::And,
                    half.clone(),
                    Expr::constant(0xffff_ffff),
                )),
                widen(Expr::bin(BinOp::Shr, half, Expr::constant(32))),
            )
        }
        // Doubles to singles, into the low half or, for fcvtn2, the high one.
        O::FCVTN | O::FCVTN2 if dest.2 == 32 => {
            let Some((low, high)) = source(ops[1]) else {
                return None;
            };
            let narrow = |double: Expr| {
                apply(
                    Operation::FloatConvert {
                        from: Precision::Double,
                        to: Precision::Single,
                    },
                    vec![double],
                )
            };
            let value = Expr::bin(
                BinOp::Or,
                narrow(low),
                Expr::bin(BinOp::Shl, narrow(high), Expr::constant(32)),
            );
            write_narrow(lifter, dest, ins.opcode == O::FCVTN2, value);
            return Some(());
        }
        _ => return None,
    };
    lifter.write_vector(dest, low, high);
    Some(())
}

/// The scalar instructions on floating-point registers.
pub(super) fn lift_float(lifter: &mut Lifter, ins: &Instruction) -> Option<()> {
    use a64::Opcode as O;
    let ops = ins.operands;
    let read = |lifter: &Lifter, operand: Operand| -> Option<(Expr, Precision)> {
        let Some((slot, bits)) = simd(operand) else {
            return None;
        };
        let Some(precision) = precision(bits) else {
            return None;
        };
        Some((lifter.read_simd(slot, bits), precision))
    };
    let float = |op: Operation, args: Vec<Expr>| Expr::apply(op, args);
    match ins.opcode {
        O::FADD | O::FSUB | O::FMUL | O::FDIV | O::FNMUL => {
            let Some((slot, bits)) = simd(ops[0]) else {
                return None;
            };
            let Some(first_operand) = read(lifter, ops[1]) else {
                return None;
            };
            let Some(second_operand) = read(lifter, ops[2]) else {
                return None;
            };
            let ((a, p), (b, _)) = (first_operand, second_operand);
            let value = match ins.opcode {
                O::FADD => float(Operation::FloatAdd(p), vec![a, b]),
                O::FSUB => float(Operation::FloatSub(p), vec![a, b]),
                O::FMUL => float(Operation::FloatMul(p), vec![a, b]),
                O::FDIV => float(Operation::FloatDiv(p), vec![a, b]),
                _ => float(
                    Operation::FloatNegate(p),
                    vec![float(Operation::FloatMul(p), vec![a, b])],
                ),
            };
            lifter.write_simd(slot, bits, (value, Expr::constant(0)));
        }
        O::FSQRT
        | O::FNEG
        | O::FABS
        | O::FRINTN
        | O::FRINTA
        | O::FRINTM
        | O::FRINTP
        | O::FRINTZ
        | O::FRINTX
        | O::FRINTI => {
            let Some((slot, bits)) = simd(ops[0]) else {
                return None;
            };
            let Some((a, p)) = read(lifter, ops[1]) else {
                return None;
            };
            let Some(op) = unary_operation(ins.opcode, p) else {
                return None;
            };
            lifter.write_simd(slot, bits, (float(op, vec![a]), Expr::constant(0)));
        }
        // fmadd d, n, m, a is a + n * m; fmsub negates the product, fnmadd
        // both the product and the addend, and fnmsub the addend.
        O::FMADD | O::FMSUB | O::FNMADD | O::FNMSUB => {
            let Some((slot, bits)) = simd(ops[0]) else {
                return None;
            };
            let Some(first_operand) = read(lifter, ops[1]) else {
                return None;
            };
            let Some(second_operand) = read(lifter, ops[2]) else {
                return None;
            };
            let Some(third_operand) = read(lifter, ops[3]) else {
                return None;
            };
            let ((n, p), (m, _), (a, _)) = (first_operand, second_operand, third_operand);
            let negate = |value| float(Operation::FloatNegate(p), vec![value]);
            let fused = |n, a| float(Operation::FloatMulAdd(p), vec![n, m.clone(), a]);
            let value = match ins.opcode {
                O::FMADD => fused(n, a),
                O::FMSUB => fused(negate(n), a),
                O::FNMADD => fused(negate(n), negate(a)),
                _ => fused(n, negate(a)),
            };
            lifter.write_simd(slot, bits, (value, Expr::constant(0)));
        }
        O::FCMP | O::FCMPE => {
            let Some((a, p)) = read(lifter, ops[0]) else {
                return None;
            };
            let b = match ops[1] {
                Operand::ImmediateDouble(_) => Expr::constant(0),
                operand => {
                    let Some(operand_register) = read(lifter, operand) else {
                        return None;
                    };
                    operand_register.0
                }
            };
            lifter.out.set_flags(Some(Flags::float_compare(a, b, p)));
        }
        // Compare when the condition holds, and otherwise set the flags to the
        // immediate, as ccmp does.
        O::FCCMP | O::FCCMPE => {
            let earlier = match ops[3] {
                Operand::ConditionCode(code) => lifter.test_code(code),
                _ => return None,
            };
            let Some(destination_operand) = read(lifter, ops[0]) else {
                return None;
            };
            let Some(first_operand) = read(lifter, ops[1]) else {
                return None;
            };
            let ((a, p), (b, _), Operand::Immediate(nzcv)) =
                (destination_operand, first_operand, ops[2])
            else {
                return None;
            };
            lifter
                .out
                .set_flags(Flags::float_compare(a, b, p).chosen_by(earlier, nzcv as u8));
        }
        O::FCSEL => {
            let Some((slot, bits)) = simd(ops[0]) else {
                return None;
            };
            let Some(first_operand) = read(lifter, ops[1]) else {
                return None;
            };
            let Some(second_operand) = read(lifter, ops[2]) else {
                return None;
            };
            let ((a, _), (b, _)) = (first_operand, second_operand);
            let Operand::ConditionCode(code) = ops[3] else {
                return None;
            };
            match lifter.test_code(code).select(a, b) {
                Some(value) => lifter.write_simd(slot, bits, (value, Expr::constant(0))),
                None => lifter.out.stmts.push(Stmt::unsupported(
                    "conditional select on flags not modelled here".to_owned(),
                )),
            }
        }
        O::FCVT => {
            let Some((slot, bits)) = simd(ops[0]) else {
                return None;
            };
            let Some((a, from)) = read(lifter, ops[1]) else {
                return None;
            };
            let Some(to) = precision(bits) else {
                return None;
            };
            let value = float(Operation::FloatConvert { from, to }, vec![a]);
            lifter.write_simd(slot, bits, (value, Expr::constant(0)));
        }
        O::SCVTF | O::UCVTF => {
            let Some((slot, bits)) = simd(ops[0]) else {
                return None;
            };
            let Some(to) = precision(bits) else {
                return None;
            };
            let Some(source) = register_width(ops[1]) else {
                return None;
            };
            let Some(value) = lifter.operand(ops[1], source) else {
                return None;
            };
            let converted = float(
                Operation::IntToFloat {
                    signed: ins.opcode == O::SCVTF,
                    bits: source,
                    to,
                },
                vec![value],
            );
            // A fixed-point source has that many fraction bits; dividing by
            // a power of two adds no rounding.
            let converted = match ops[2] {
                Operand::Immediate(fraction) if fraction > 0 => float(
                    Operation::FloatDiv(to),
                    vec![converted, power_of_two(fraction, to)],
                ),
                _ => converted,
            };
            lifter.write_simd(slot, bits, (converted, Expr::constant(0)));
        }
        O::FCVTZS
        | O::FCVTZU
        | O::FCVTNS
        | O::FCVTNU
        | O::FCVTAS
        | O::FCVTAU
        | O::FCVTMS
        | O::FCVTMU
        | O::FCVTPS
        | O::FCVTPU => {
            let Some((a, from)) = read(lifter, ops[1]) else {
                return None;
            };
            let Some((signed, rounding)) = integer_conversion(ins.opcode) else {
                return None;
            };
            // A fixed-point result keeps that many fraction bits.
            let a = match ops[2] {
                Operand::Immediate(fraction) if fraction > 0 => float(
                    Operation::FloatMul(from),
                    vec![a, power_of_two(fraction, from)],
                ),
                _ => a,
            };
            let Some(width) = register_width(ops[0]) else {
                return None;
            };
            let converted = float(
                Operation::FloatToInt {
                    signed,
                    bits: width,
                    from,
                    saturating: true,
                    rounding,
                },
                vec![a],
            );
            if let Some(dest) = destination(ops[0]) {
                lifter.assign(dest, converted);
            }
        }
        O::FMOV => match (ops[0], ops[1]) {
            (Operand::SIMDRegister(..), Operand::SIMDRegister(..)) => {
                let Some(destination) = simd(ops[0]) else {
                    return None;
                };
                let Some(source) = simd(ops[1]) else {
                    return None;
                };
                let ((slot, bits), (from, _)) = (destination, source);
                let value = lifter.read_simd(from, bits);
                lifter.write_simd(slot, bits, (value, Expr::constant(0)));
            }
            (Operand::SIMDRegister(..), Operand::Register(..)) => {
                let Some((slot, bits)) = simd(ops[0]) else {
                    return None;
                };
                let Some(value) = lifter.operand(ops[1], bits as u8) else {
                    return None;
                };
                lifter.write_simd(slot, bits, (value, Expr::constant(0)));
            }
            (Operand::Register(..), Operand::SIMDRegister(..)) => {
                let Some((from, bits)) = simd(ops[1]) else {
                    return None;
                };
                let value = lifter.read_simd(from, bits);
                if let Some(dest) = destination(ops[0]) {
                    lifter.assign(dest, value);
                }
            }
            (Operand::SIMDRegister(..), Operand::ImmediateDouble(value)) => {
                let Some((slot, bits)) = simd(ops[0]) else {
                    return None;
                };
                let pattern = if bits == 16 {
                    match half_immediate(value) {
                        Some(value) => value,
                        None => return None,
                    }
                } else {
                    let Some(precision) = precision(bits) else {
                        return None;
                    };
                    match precision {
                        Precision::Double => value.to_bits(),
                        Precision::Single => u64::from((value as f32).to_bits()),
                    }
                };
                lifter.write_simd(slot, bits, (Expr::constant(pattern), Expr::constant(0)));
            }
            // The constant in every lane.
            (Operand::SIMDRegisterElements(..), Operand::ImmediateDouble(value)) => {
                let Some(dest) = vector(ops[0]) else {
                    return None;
                };
                let Some(precision) = precision(u16::from(dest.2)) else {
                    return None;
                };
                let pattern = match precision {
                    Precision::Double => value.to_bits(),
                    Precision::Single => u64::from((value as f32).to_bits()),
                };
                let half = Expr::constant(replicated(pattern, dest.2));
                lifter.write_vector(dest, half.clone(), half);
            }
            // The upper half of a 128-bit register, which the rest keeps.
            (Operand::SIMDRegisterElementsLane(_, number, a64::SIMDSizeCode::D, 1), source) => {
                let Some(value) = lifter.operand(source, 64) else {
                    return None;
                };
                let slot = vlk_arch::arch::aarch64::V0 + 2 * number + 1;
                lifter.assign(
                    RegRef {
                        index: slot,
                        width: 64,
                    },
                    value,
                );
            }
            (dest, Operand::SIMDRegisterElementsLane(_, number, a64::SIMDSizeCode::D, 1)) => {
                let value = Expr::reg(vlk_arch::arch::aarch64::V0 + 2 * number + 1, 64);
                if let Some(dest) = destination(dest) {
                    lifter.assign(dest, value);
                }
            }
            _ => return None,
        },
        // A lane read out to a scalar or general register.
        O::DUP | O::UMOV | O::SMOV if lane(ops[1]).is_some() && vector(ops[0]).is_none() => {
            let Some(source) = lane(ops[1]) else {
                return None;
            };
            let value = lane_value(source);
            match simd(ops[0]) {
                Some((slot, bits)) if u64::from(bits) == source.2 => {
                    lifter.write_simd(slot, bits, (value, Expr::constant(0)))
                }
                Some(_) => return None,
                None => {
                    let value = if ins.opcode == O::SMOV {
                        Expr::SignExtend {
                            value: Box::new(value),
                            width: (source.2 / 8) as u8,
                        }
                    } else {
                        value
                    };
                    if let Some(dest) = destination(ops[0]) {
                        lifter.assign(dest, value);
                    }
                }
            }
        }
        // A lane written from a general register or another lane; the rest
        // of the register stays.
        O::INS => {
            let Some((slot, shift, bits)) = lane(ops[0]) else {
                return None;
            };
            let value = match lane(ops[1]) {
                Some(source) => lane_value(source),
                None => match lifter.operand(ops[1], bits as u8) {
                    Some(value) => value,
                    None => return None,
                },
            };
            let ones = if bits == 64 {
                u64::MAX
            } else {
                (1 << bits) - 1
            };
            let merged = if bits == 64 {
                value
            } else {
                Expr::bin(
                    BinOp::Or,
                    Expr::bin(
                        BinOp::And,
                        Expr::reg(slot, 64),
                        Expr::constant(!(ones << shift)),
                    ),
                    Expr::bin(
                        BinOp::Shl,
                        Expr::bin(BinOp::And, value, Expr::constant(ones)),
                        Expr::constant(shift),
                    ),
                )
            };
            lifter.assign(
                RegRef {
                    index: slot,
                    width: 64,
                },
                merged,
            );
        }
        // Zero, or a 64-bit pattern in each half.
        // A shifted immediate (or its complement) in every lane.
        O::MOVI | O::MVNI if matches!(ops[1], Operand::ImmShift(..) | Operand::ImmShiftMSL(..)) => {
            let Some(dest) = vector(ops[0]) else {
                return None;
            };
            let Some(lane) = immediate_lane(ops[1], ins.opcode == O::MVNI, dest.2) else {
                return None;
            };
            let half = Expr::constant(replicated(lane, dest.2));
            lifter.write_vector(dest, half.clone(), half);
        }
        O::MOVI => {
            let Operand::Imm64(value) = ops[1] else {
                return None;
            };
            match ops[0] {
                Operand::SIMDRegister(a64::SIMDSizeCode::D, number) => lifter.write_simd(
                    vlk_arch::arch::aarch64::V0 + 2 * number,
                    64,
                    (Expr::constant(value), Expr::constant(0)),
                ),
                Operand::SIMDRegisterElements(size, number, a64::SIMDSizeCode::D)
                    if value == 0 || size == a64::SIMDSizeCode::Q =>
                {
                    let high = if size == a64::SIMDSizeCode::Q {
                        value
                    } else {
                        0
                    };
                    lifter.write_simd(
                        vlk_arch::arch::aarch64::V0 + 2 * number,
                        128,
                        (Expr::constant(value), Expr::constant(high)),
                    )
                }
                Operand::SIMDRegisterElements(size, number, _) if value == 0 => lifter.write_simd(
                    vlk_arch::arch::aarch64::V0 + 2 * number,
                    if size == a64::SIMDSizeCode::Q {
                        128
                    } else {
                        64
                    },
                    (Expr::constant(0), Expr::constant(0)),
                ),
                _ => return None,
            }
        }
        _ => return None,
    }
    Some(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn opcode_properties_preserve_rounding_and_signedness() {
        use a64::Opcode as O;
        for p in [Precision::Single, Precision::Double] {
            for (opcode, expected) in [
                (O::FABS, Operation::FloatAbs(p)),
                (O::FNEG, Operation::FloatNegate(p)),
                (O::FSQRT, Operation::FloatSqrt(p)),
                (O::FRINTN, Operation::FloatRound(p, Rounding::NearestEven)),
                (O::FRINTA, Operation::FloatRound(p, Rounding::NearestAway)),
                (O::FRINTM, Operation::FloatRound(p, Rounding::Down)),
                (O::FRINTP, Operation::FloatRound(p, Rounding::Up)),
                (O::FRINTZ, Operation::FloatRound(p, Rounding::TowardZero)),
                (O::FRINTX, Operation::FloatRound(p, Rounding::NearestEven)),
                (O::FRINTI, Operation::FloatRound(p, Rounding::NearestEven)),
            ] {
                assert_eq!(unary_operation(opcode, p), Some(expected));
                assert_eq!(integer_conversion(opcode), None);
            }
        }
        for (opcode, signed, rounding) in [
            (O::FCVTZS, true, Rounding::TowardZero),
            (O::FCVTZU, false, Rounding::TowardZero),
            (O::FCVTNS, true, Rounding::NearestEven),
            (O::FCVTNU, false, Rounding::NearestEven),
            (O::FCVTAS, true, Rounding::NearestAway),
            (O::FCVTAU, false, Rounding::NearestAway),
            (O::FCVTMS, true, Rounding::Down),
            (O::FCVTMU, false, Rounding::Down),
            (O::FCVTPS, true, Rounding::Up),
            (O::FCVTPU, false, Rounding::Up),
        ] {
            assert_eq!(integer_conversion(opcode), Some((signed, rounding)));
            assert_eq!(unary_operation(opcode, Precision::Single), None);
        }
        for opcode in [O::FADD, O::FCVT, O::SCVTF, O::UCVTF] {
            assert_eq!(unary_operation(opcode, Precision::Double), None);
            assert_eq!(integer_conversion(opcode), None);
        }
        assert_eq!(precision(16), None);
        assert_eq!(precision(32), Some(Precision::Single));
        assert_eq!(precision(64), Some(Precision::Double));
    }
}