binbun 0.1.0

Disassembler and decompiler engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
//! Recovered functions as C: each function's L1 lowered to L2, structured,
//! typed and printed, the passes on L1 and on L2 run by a pass manager per
//! function ([`binbun_semantics::core::pass`]), with the structure check
//! (L2's control flow against L1's CFG, [`validate::structure`]). Every
//! function reads the one interface of each function of the program,
//! found once for the whole program ([`callers`],
//! [`Callers::interfaces`]), so one function
//! decompiled alone and the whole program agree; the whole program is
//! decompiled callees first as [`stream`] hands the functions over, on
//! worker threads as the stream goes on ([`stream_parallel`],
//! [`threads`]).

mod callers;
mod stream;

use std::collections::{BTreeMap, BTreeSet};

use crate::cfg::{FunctionId, Program};
use binbun_ast::ir::{
    Block, CatchType, Escape, Function as Ast, HandlerKind, SehFilter, Stmt, StmtId,
};
use binbun_decompile::data::Data;
use binbun_decompile::itanium::{Action, CatchDispatch, Exceptions, Floor, TryRegions, Unknown};
use binbun_decompile::lower::{self as lowering, Context, Facts};
use binbun_decompile::mapping::Mapping;
use binbun_decompile::msvc::ExceptionCodes;
use binbun_decompile::names;
use binbun_decompile::pass::{self as l2, Feeds, L2Env};
use binbun_decompile::prototype::Prototypes;
use binbun_decompile::range::Indices;
use binbun_decompile::scopes::Scopes;
use binbun_decompile::simplify::coalesce::Coalesce;
use binbun_decompile::simplify::copies::{Narrow, Propagate, Prune};
use binbun_decompile::simplify::division::Divisions;
use binbun_decompile::simplify::fields::DeclaredFields;
use binbun_decompile::simplify::rules::Rewrite;
use binbun_decompile::simplify::signs::Signs;
use binbun_decompile::simplify::sink::Sink;
use binbun_decompile::structure::{
    ArrayLoops, CommonArms, Conditions, Exits, Forward, LoopHeads, Loops, Rotated, ShortCircuit,
    Structure, Switches,
};
use binbun_decompile::validate::{self, Link};
use binbun_loader::{CallSite, Catch, Clause, HandlerTable, Image, ScopeAction, TypeInfo};
use binbun_print::c::{Image as Printing, Opaque, Unknowns};
use binbun_print::layout;
use binbun_print::style::{IntSpelling, Naming, Placement, Style};
use binbun_semantics::Declarations;
use binbun_semantics::core::pass::{Broken, Changed, Fuel, Manager, Pass, PassStats, Refusal};
use binbun_semantics::ir::RegisterId;
use binbun_ssa::constant::FoldBranches;
use binbun_ssa::float::{AssumeEmptyX87, AssumeFloatControls, AssumeFloatResults, Control};
use binbun_ssa::implied::FoldImplied;
use binbun_ssa::ir::Function;
use binbun_ssa::pass::{self as l1, L1Env};
use binbun_ssa::stack::{
    AssumeAligned, AssumeAuthenticated, AssumeCanonical, AssumeDereferenced, TailJumps,
};

use crate::data::ImageData;
use crate::funclets::catch_all;
use crate::lift::{ImageEnvironment, lift_function, lift_function_leaving};
use crate::memory::ImageMemory;
use crate::summary::Summaries;
use crate::target::{convention, setup};

pub use binbun_decompile::typing::Outcome;
pub use callers::{Callers, callers, callers_for};
pub use stream::{Interfaces, stream, stream_parallel};

/// The threads whole-program decompilation works on
/// ([`stream_parallel`]): `BINBUN_THREADS` if it is a positive number (1:
/// all on the calling thread, as [`stream`] hands functions over), else
/// as many as the process may run at once
/// ([`std::thread::available_parallelism`], which honours the CPU
/// affinity `taskset` sets), else one.
pub fn threads() -> usize {
    std::env::var("BINBUN_THREADS")
        .ok()
        .and_then(|threads| threads.parse().ok())
        .filter(|&threads| threads > 0)
        .or_else(|| std::thread::available_parallelism().ok().map(Into::into))
        .unwrap_or(1)
}

/// How functions are decompiled.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Options {
    /// The name of the style preset the C is laid out in; `None`: no C
    /// (L1 and L2 only).
    pub style: Option<String>,
    /// The passes not run, by name ([`passes`]).
    pub disabled: Vec<String>,
    /// Which view: exact, or the reading view.
    pub view: View,
    /// How the C spells integer types: fixed-width (`int32_t`, the
    /// default) or C's own names for the image's data model (`int`,
    /// `long`).
    pub types: IntSpelling,
    /// Where the C declares locals: in the blocks using them (the
    /// default), or all at the top.
    pub declarations: Placement,
    /// How the C names variables with no name of their own: by kind and
    /// number (`a1`, `v7`, the default), or by where the machine code kept
    /// them (`arg1`, `rax`, `var_58`).
    pub names: Naming,
}

/// Which C decompilation makes: the exact C or the reading view.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum View {
    /// What the machine code does, every fault it can take included.
    #[default]
    Exact,
    /// The reading view: a second L1 made under named assumptions
    /// ([`View::ASSUMPTIONS`]; [`reading_l1`]), lowered and structured
    /// again, so the checks the assumptions settle and their traps are
    /// gone and the loops they broke take their shapes. It hides, never
    /// lies: everything else (wrap-around, undefined values, the faults
    /// of other kinds) stays. Its structure is checked against the
    /// assumed L1 ([`Decompiled::l1`]), and it compiles as the exact C
    /// does but where it has instructions without semantics, which it
    /// prints as `__asm` blocks ([`Opaque::Asm`]); it differs
    /// from the exact C only on runs that take a fault the assumptions
    /// rule out, and in its types: what a pointer points to is C's type
    /// (`int32_t *`), which asks its alignment of the address, where
    /// the exact C's is one of alignment 1 (`binbun_i32_u *`).
    Reading,
}

impl View {
    /// The reading view's assumptions, as its C states them.
    pub const ASSUMPTIONS: &'static str = "every address a function dereferences (loads from, stores to, calls or jumps to) is canonical, and aligned for the type it is read or written as; the stack pointer is aligned at entry as the ABI says (AArch64); a return address signed with pointer authentication authenticates (AArch64); floating-point instructions run with the controls a process starts with (to nearest, every exception masked, no flush to zero, denormals as they are) unless the function loads the control register itself, a NaN is not told from another and a float converted to an integer is in its range; the x87 register stack is empty at a function's entry, TOP 0, and the x87 unit holds no argument (x86-64); a futex operation made of a constant and another value has the constant's command, the other value giving only its private and clock flags (Linux); a register a function must keep, and saves and restores, holds no argument; an argument register a function only passes on as it found it holds no argument where the function's declaration places no parameter in it, where some call of the program leaves it undefined, or where it goes past a variadic function's named parameters, to an undeclared import, or to a call through a pointer after the last argument that call sets, and no call sets it";

    /// Whether the view assumes that an argument register a function
    /// only passes on as it found it holds no argument where the
    /// function's declaration places no parameter in it, where a call of
    /// the program leaves it undefined, or where nothing says the callee
    /// reads it (as after the last argument a call through a pointer
    /// sets) and no call sets it (the reading view; [`callers`] states
    /// the rules): the interfaces of the whole program are then found
    /// under it.
    pub const fn assumes_arguments_set(self) -> bool {
        matches!(self, Self::Reading)
    }

    /// How its C prints instructions without semantics: calls that
    /// compile (exact), `__asm` blocks (reading).
    pub const fn opaque(self) -> Opaque {
        match self {
            Self::Exact => Opaque::Calls,
            Self::Reading => Opaque::Asm,
        }
    }
}

/// The names of the passes, in the order they run: on L1, then (after
/// lowering) on L2.
pub fn passes() -> [&'static str; 26] {
    [
        TailJumps::default().name(),
        AssumeCanonical.name(),
        FoldBranches.name(),
        FoldImplied.name(),
        CatchDispatch::default().name(),
        ExceptionCodes.name(),
        Structure::default().name(),
        TryRegions.name(),
        Sink.name(),
        Propagate.name(),
        Coalesce.name(),
        Prune.name(),
        Narrow.name(),
        Rewrite::default().name(),
        Divisions.name(),
        ShortCircuit::default().name(),
        Exits::default().name(),
        CommonArms.name(),
        ArrayLoops::default().name(),
        Loops::default().name(),
        Switches.name(),
        DeclaredFields.name(),
        Signs.name(),
        Conditions.name(),
        Forward.name(),
        Scopes.name(),
    ]
}

/// Fuel per node of an L1 function (a block or an instruction) for the
/// passes its manager runs: each pass spends a unit per node it visits, so
/// this is 16 passes over the whole function (two run). A pass out of fuel
/// refuses and leaves the function as it was.
const FUEL: u64 = 16;

/// Fuel per node of an L2 function (a statement or an expression, as
/// lowered): structuring, the last dead code and loops (a pass each),
/// and [`ROUNDS`] rounds of the eight simplification passes (each
/// about a pass over the function, [`Rewrite`] up to two, [`Coalesce`]'s
/// liveness and values a few, more in nested loops), with room to spare.
/// At 64, libc's largest functions ran out before the last dead code.
const L2_FUEL: u64 = 128;

/// Rounds of the simplification passes ([`l2_passes`]) at most: each
/// round's copies, dead code and rewrites feed the next (a copy removed
/// makes a value read once, a rewrite makes a variable unread), and on
/// the fixture, bash and libc nearly every function is stable by the
/// third.
pub const ROUNDS: usize = 4;

/// The manager the L2 passes run under ([`l2_passes`]).
pub type L2Manager<'e> = Manager<Ast, L2Env<'e>>;

/// What a recording manager kept ([`Manager::record`]).
#[derive(Clone, Debug, Default)]
pub struct Recorded<F> {
    /// The function as each pass that changed it left it, with the pass's
    /// name, in order.
    pub stages: Vec<(&'static str, F)>,
    /// The refusals, in order, with the pass that refused.
    pub refusals: Vec<(&'static str, Refusal)>,
}

impl<F> Recorded<F> {
    /// What `manager` kept and its refusals.
    fn of<E>(manager: &mut Manager<F, E>) -> Self {
        Self {
            stages: manager.recorded(),
            refusals: manager.refusals().to_vec(),
        }
    }
}

/// The L2 passes after lowering, under one manager with L2's
/// verifier after each change, but the passes `disabled`:
///
/// 1. [`CatchDispatch`] (a landing pad's selector tests as its `Try`'s
///    handlers), [`ExceptionCodes`] (what an `__except` block receives
///    as the exception code), [`Structure`] (refused: the goto form, as
///    correct), then
///    [`TryRegions`] (the `Try`s of a landing pad as one, nested as the
///    exception tables say);
/// 2. to a fixed point (at most [`ROUNDS`] rounds), in order:
///    [`Sink`] (assignments with no effect but their write moved down to
///    their uses), [`Propagate`] (copies, values read once and call
///    results read once into their readers), [`Coalesce`] (the two
///    variables of a copy made one where they never differ while both
///    live), [`Prune`] (assignments nothing reads), [`Narrow`] (variables read
///    only in their low bits, or assigned only extensions), [`Rewrite`] (the
///    proved rules and constant
///    folding), [`Divisions`] (multiply-high sequences back to division
///    by a constant, which leaves the multiply-high's variables to the
///    next round's dead code), [`ShortCircuit`] (nested decisions to
///    `&&`/`||`, which the simplifications make possible where they
///    remove the code before an inner decision);
/// 3. [`Prune`] once more (what the fixed point's last round left:
///    coalescing's copies of a variable to itself);
///    then [`Exits`] (a `goto` to a small exit, at most three statements
///    ending in a `return` or a call that does not return, replaced by a
///    copy of it: after the fixed point, so that `&&`/`||` have merged
///    the decisions going to one exit and the exits are as small as they
///    get) and, if it copied one, the fixed point and [`Prune`] again
///    (what the copies make foldable: `v = 0; return v;`);
/// 4. [`CommonArms`] (the statements both arms of an `if` begin with
///    before it, those both end with after it: the guard of an inverted
///    `for` is then its loop's alone), [`ArrayLoops`] (pointer loops
///    through an array as loops of an index; then [`Prune`] for the end
///    pointers it leaves unread), then
///    [`Loops`] (`do`-`while`s guarded by their own condition as
///    `while`, `for` loops, proved exit conditions as `<`, counted rotated
///    loops as `for`);
///    then [`Switches`] (comparison chains on one value as `switch`);
/// 5. [`DeclaredFields`] (what is read and written through the thread
///    pointer, typed by the structure its platform declares);
/// 6. [`Signs`] (operations on values that read the same signed or
///    unsigned in the signedness of their operands' types);
/// 7. [`Conditions`] (an assignment into the condition after it that
///    reads it: `if ((v = f()) != 0)`, `while ((c = getc(s)) != -1)`;
///    last, so that no other pass sees an assignment expression) and,
///    if it moved one, [`ShortCircuit`] again (the `if` it leaves may be
///    all of its block's code now);
/// 8. [`Forward`] (the forward `goto`s the passes since [`Exits`] left
///    alone in an `if`'s arm beside the code they skip, folded into the
///    `if`: decisions with a load between them are one `&&` only now);
/// 9. [`Scopes`] (each local declared in the innermost block holding its
///    uses where no value flows in, its first assignment its
///    initializer, a `for`'s counter in its header; last, as the
///    others move statements).
///
/// Structuring comes first because the simplifications read the
/// structure (a value moves only forward within a block, into a loop only
/// if nothing in the loop changes what it reads). Decompilation
/// ([`decompile`]) and the corpus test both run this, so what is checked
/// is what is printed.
///
/// Returns the manager (its counts), why catch dispatch left each
/// landing pad's `Try`s on the goto floor ([`CatchDispatch::floors`]),
/// how many pointer loops became array loops ([`ArrayLoops::rewritten`])
/// and what became of the `do`-`while`s ([`Loops::rotated`]).
///
/// # Errors
///
/// A pass left the function failing its verifier.
pub fn l2_passes<'e>(
    l2: &mut Ast,
    env: &L2Env<'e>,
    disabled: &[String],
) -> Result<Passed<'e>, String> {
    l2_passes_recorded(l2, env, disabled, false)
}

/// [`l2_passes`], the manager recording ([`Manager::record`]: the
/// function after each pass that changed it, [`Passed::manager`]) if
/// `record`.
///
/// # Errors
///
/// As for [`l2_passes`].
pub(crate) fn l2_passes_recorded<'e>(
    l2: &mut Ast,
    env: &L2Env<'e>,
    disabled: &[String],
    record: bool,
) -> Result<Passed<'e>, String> {
    let mut manager =
        Manager::new(Fuel::new(L2_FUEL.saturating_mul(l2::size(l2)))).with_verifier(l2::verify);
    for name in disabled {
        manager.disable(name);
    }
    if record {
        manager.record();
    }
    let fails = |broken: Broken| fails("L2", &broken);
    let mut dispatch = CatchDispatch::default();
    manager.run(&mut dispatch, l2, env).map_err(fails)?;
    manager.run(&mut ExceptionCodes, l2, env).map_err(fails)?;
    manager
        .run(&mut Structure::default(), l2, env)
        .map_err(fails)?;
    manager.run(&mut TryRegions, l2, env).map_err(fails)?;
    let (mut rewrite, mut short) = (Rewrite::default(), ShortCircuit::default());
    let mut simplify = |manager: &mut L2Manager<'e>, l2: &mut Ast| {
        manager.fixpoint(
            &mut [
                &mut Sink,
                &mut Propagate,
                &mut Coalesce,
                &mut Prune,
                &mut Narrow,
                &mut rewrite,
                &mut Divisions,
                &mut short,
            ],
            ROUNDS,
            l2,
            env,
        )?;
        // The fixed point may end (at its last round) with what coalescing
        // left (a variable's copy to itself) or propagation made unread.
        manager.run(&mut Prune, l2, env)
    };
    simplify(&mut manager, l2).map_err(fails)?;
    if manager.run(&mut Exits::default(), l2, env).map_err(fails)? == Changed::Yes {
        simplify(&mut manager, l2).map_err(fails)?;
    }
    manager.run(&mut CommonArms, l2, env).map_err(fails)?;
    manager.run(&mut LoopHeads, l2, env).map_err(fails)?;
    let mut arrays = ArrayLoops::default();
    manager.run(&mut arrays, l2, env).map_err(fails)?;
    if arrays.rewritten() > 0 {
        // An end pointer only the rewritten tests read is dead.
        manager.run(&mut Prune, l2, env).map_err(fails)?;
    }
    let mut loops = Loops::default();
    manager.run(&mut loops, l2, env).map_err(fails)?;
    manager.run(&mut Switches, l2, env).map_err(fails)?;
    manager.run(&mut DeclaredFields, l2, env).map_err(fails)?;
    manager.run(&mut Signs, l2, env).map_err(fails)?;
    if manager.run(&mut Conditions, l2, env).map_err(fails)? == Changed::Yes {
        manager
            .run(&mut ShortCircuit::default(), l2, env)
            .map_err(fails)?;
    }
    manager.run(&mut Forward, l2, env).map_err(fails)?;
    manager.run(&mut Scopes, l2, env).map_err(fails)?;
    manager.run(&mut Indices, l2, env).map_err(fails)?;
    Ok(Passed {
        manager,
        floors: dispatch.floors,
        array_loops: arrays.rewritten(),
        rotated: loops.rotated().clone(),
        outcomes: loops.outcomes().clone(),
        rules: loops.applied().clone(),
    })
}

/// What the L2 passes ([`l2_passes`]) leave besides the function.
#[derive(Debug)]
pub struct Passed<'e> {
    /// The manager: each pass's and analysis's counts.
    pub manager: L2Manager<'e>,
    /// Why catch dispatch left each landing pad's `Try`s on the goto
    /// floor, by the pad's address.
    pub floors: BTreeMap<u64, Floor>,
    /// How many pointer loops became array loops.
    pub array_loops: usize,
    /// What became of the `do`-`while`s.
    pub rotated: BTreeMap<Rotated, usize>,
    /// What became of each `do`-`while`, by the statement that is the
    /// loop now ([`Loops::outcomes`]).
    pub outcomes: BTreeMap<StmtId, Rotated>,
    /// The loop rules applied to each loop, by the statement that is the
    /// loop now ([`Loops::applied`]).
    pub rules: BTreeMap<StmtId, BTreeSet<u8>>,
}

/// One function decompiled, or why it is not.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Output {
    /// The function.
    pub id: FunctionId,
    /// Its entry.
    pub entry: u64,
    /// Its name, if the image has one (a symbol, or an import thunk's
    /// import).
    pub name: Option<String>,
    /// What it decompiled to, or why it did not.
    pub result: Result<Decompiled, String>,
}

/// A function decompiled.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Decompiled {
    /// Its L1 after the L1 passes ([`decompile_l1`]).
    pub l1: Function,
    /// Its L2 after the L2 passes ([`l2_passes`]), named.
    pub l2: Ast,
    /// How its L2 stands for its L1 (lowering's: parameters, returns and
    /// calls in L1's state), which running it reads.
    pub mapping: Mapping,
    /// Its C, if asked for ([`Options::style`]).
    pub c: Option<C>,
    /// Its counts.
    pub stats: Stats,
}

/// A function as a C file.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct C {
    /// The file: the function and the declarations it needs; it includes
    /// `binbun_defs.h` ([`binbun_print::c::HEADER`]).
    pub text: String,
    /// The casts it has.
    pub casts: usize,
    /// Its `__asm` blocks (the reading view's instructions without
    /// semantics, [`Opaque::Asm`]): with
    /// one, it is no C a compiler takes.
    pub asm: usize,
    /// The unknown values it prints, by where they come from: those the
    /// architecture leaves undefined are processor-dependent.
    pub unknowns: Unknowns,
}

/// Counts of one function.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Stats {
    /// Gotos and escapes in its L2 ([`count`]).
    pub counts: Counts,
    /// What became of its calls' declared format strings.
    pub formats: lowering::Formats,
    /// Where its control flow differs from L1's (the structure check), if
    /// it does.
    pub structure: Option<String>,
    /// Its `Try`s left on the goto floor (a landing handler), by why
    /// catch dispatch left their landing pad ([`l2_passes`]).
    pub floors: BTreeMap<Floor, usize>,
    /// How many of its pointer loops became array loops ([`l2_passes`]).
    pub array_loops: usize,
    /// What became of its `do`-`while`s ([`l2_passes`]).
    pub rotated: BTreeMap<Rotated, usize>,
    /// Each pass's counts, by name: L1's, then L2's.
    pub passes: Vec<(&'static str, PassStats)>,
    /// How many times each analysis was computed, by type name.
    pub analyses: Vec<(&'static str, usize)>,
    /// What the type solver did ([`Options::solver`]).
    pub typing: Outcome,
    /// Whether it is a variadic definition, recognised and declared.
    pub variadic: lowering::Variadic,
    /// How many of the frame's slots each rule of lowering's step 3 took.
    pub frames: lowering::Frames,
    /// What became of what debug information states about it.
    pub debug: lowering::DebugCounts,
    /// The checks the reading view assumed ([`View::Reading`]; none in
    /// the exact view).
    pub assumed: Assumed,
}

/// The checks the reading view assumed away ([`reading_l1`]).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Assumed {
    /// Each check, one each, in order: the canonical-address checks, the
    /// stack-alignment checks, the return addresses' authentications,
    /// then the instructions whose floating-point controls and whose
    /// floating-point results were assumed, and those reading the x87
    /// stack's state at entry; each by the address of the machine
    /// instruction it guards or is part of (where the exact C has the
    /// check).
    pub sites: Vec<u64>,
    /// How many of them are canonical-address checks
    /// ([`AssumeDereferenced`]), stack-alignment checks
    /// ([`AssumeAligned`]), return-address authentications
    /// ([`AssumeAuthenticated`]), instructions run under the default
    /// floating-point controls ([`AssumeFloatControls`]), instructions
    /// whose NaN or converted integer is the operation's own
    /// ([`AssumeFloatResults`]) and instructions reading an empty x87
    /// stack ([`AssumeEmptyX87`]), in that order.
    pub kinds: [usize; 6],
}

impl Assumed {
    /// Whether no check was assumed.
    pub fn is_empty(&self) -> bool {
        self.sites.is_empty()
    }
}

/// The C gotos, the escapes and the `Try`s of an L2 function.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Counts {
    /// `goto` statements, and `break`s and `continue`s of a statement other
    /// than the innermost one C's would leave (which C says with a goto).
    pub gotos: usize,
    /// Escapes: what C cannot say ([`Escape`](binbun_ast::ir::Escape)).
    pub escapes: usize,
    /// `Try` statements.
    pub tries: usize,
    /// Their handlers, by kind.
    pub handlers: Handlers,
}

/// Counts of handlers by kind ([`HandlerKind`]).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Handlers {
    /// Landing handlers: the goto floor.
    pub landing: usize,
    /// Catches of a type.
    pub catch: usize,
    /// Catches of every exception.
    pub catch_all: usize,
    /// Cleanups.
    pub cleanup: usize,
    /// Exception specifications' handlers.
    pub unexpected: usize,
    /// `__except` handlers.
    pub except: usize,
    /// `__finally` handlers.
    pub finally: usize,
}

impl Handlers {
    /// Counts one handler of `kind`.
    fn add(&mut self, kind: &HandlerKind) {
        let slot = match kind {
            HandlerKind::Landing(_) => &mut self.landing,
            HandlerKind::Catch(_) => &mut self.catch,
            HandlerKind::CatchAll => &mut self.catch_all,
            HandlerKind::Cleanup => &mut self.cleanup,
            HandlerKind::Unexpected(_) => &mut self.unexpected,
            HandlerKind::Except(_) => &mut self.except,
            HandlerKind::Finally => &mut self.finally,
        };
        *slot += 1;
    }
}

/// The [`Counts`] of `function`.
pub fn count(function: &Ast) -> Counts {
    let mut counts = Counts::default();
    count_block(function, &function.body, None, None, &mut counts);
    counts
}

/// Counts in `block`, inside the innermost loop or switch `breakable` and
/// the innermost loop `loop_`.
fn count_block(
    function: &Ast,
    block: &Block,
    breakable: Option<StmtId>,
    loop_: Option<StmtId>,
    counts: &mut Counts,
) {
    for &id in block {
        let Some(stmt) = function.stmt(id) else {
            continue;
        };
        match stmt {
            Stmt::Goto(_) => counts.gotos += 1,
            // C says an atomic update: no escape.
            Stmt::Escape {
                kind: Escape::Update(_),
                ..
            } => {}
            Stmt::Escape { .. } => counts.escapes += 1,
            &Stmt::Break(target) if Some(target) != breakable => counts.gotos += 1,
            &Stmt::Continue(target) if Some(target) != loop_ => counts.gotos += 1,
            Stmt::While { .. } | Stmt::DoWhile { .. } | Stmt::For { .. } => {
                for inner in stmt.blocks() {
                    count_block(function, inner, Some(id), Some(id), counts);
                }
            }
            Stmt::Switch { .. } => {
                for inner in stmt.blocks() {
                    count_block(function, inner, Some(id), loop_, counts);
                }
            }
            Stmt::Try { handlers, .. } => {
                counts.tries += 1;
                for handler in handlers {
                    counts.handlers.add(&handler.kind);
                }
                for inner in stmt.blocks() {
                    count_block(function, inner, breakable, loop_, counts);
                }
            }
            Stmt::If { .. } => {
                for inner in stmt.blocks() {
                    count_block(function, inner, breakable, loop_, counts);
                }
            }
            Stmt::Assign { .. }
            | Stmt::Call { .. }
            | Stmt::Intrinsic { .. }
            | Stmt::Expr(_)
            | Stmt::Break(_)
            | Stmt::Continue(_)
            | Stmt::Label(_)
            | Stmt::Return(_)
            | Stmt::Trap(_)
            | Stmt::Unreachable
            | Stmt::VaStart { .. } => {}
        }
    }
}

/// What the pipeline is given of an image beside its code and the
/// interfaces of its functions.
pub struct ImageFacts<'f> {
    /// The declared prototypes.
    pub prototypes: &'f dyn Prototypes,
    /// The image's data.
    pub data: &'f ImageData<'f>,
    /// The image's exceptions ([`exceptions`]).
    pub exceptions: &'f Exceptions,
}

/// The `Try`s of `function` left with a landing handler (the goto floor),
/// counted by why catch dispatch left their pad (`reasons`, by pad; a
/// pad it gave no reason for, as when it did not run, is not counted).
fn floors(function: &Ast, reasons: &BTreeMap<u64, Floor>) -> BTreeMap<Floor, usize> {
    let mut floors = BTreeMap::new();
    let mut work = vec![&function.body];
    while let Some(block) = work.pop() {
        for stmt in block.iter().filter_map(|&id| function.stmt(id)) {
            work.extend(stmt.blocks());
            let Stmt::Try { handlers, .. } = stmt else {
                continue;
            };
            let reason = handlers.iter().find_map(|handler| match handler.kind {
                HandlerKind::Landing(pad) => reasons.get(&pad),
                HandlerKind::Catch(_)
                | HandlerKind::CatchAll
                | HandlerKind::Cleanup
                | HandlerKind::Unexpected(_)
                | HandlerKind::Except(_)
                | HandlerKind::Finally => None,
            });
            if let Some(&reason) = reason {
                *floors.entry(reason).or_default() += 1;
            }
        }
    }
    floors
}

impl std::fmt::Debug for ImageFacts<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ImageFacts")
            .field("exceptions", self.exceptions)
            .finish_non_exhaustive()
    }
}

/// The pipeline for a function `prepared` in L1 (of `image`, named
/// `name`), its callees' `interfaces` final: lowering with the declared
/// prototypes and the image's data (of `facts`), then the L2 passes
/// ([`l2_passes`]), the variables named from evidence ([`names::name`]),
/// then the check against L1's CFG, both knowing the image's exceptions;
/// then, under `options`, the C. In the reading view
/// ([`View::Reading`]) all of it starts from the reading view's L1
/// ([`reading_l1`]), and the C says how many checks it assumed.
///
/// # Errors
///
/// The image's code is not lifted, lowering fails, a pass leaves L2
/// failing its verifier, or the C cannot be printed (no such style
/// preset, or the printer refuses the function).
pub fn decompile(
    image: &Image<'_>,
    prepared: Prepared,
    name: Option<&str>,
    interfaces: &Interfaces,
    facts: &ImageFacts<'_>,
    options: &Options,
) -> Result<Decompiled, String> {
    let ImageFacts {
        prototypes,
        data,
        exceptions,
    } = *facts;
    let environment = ImageEnvironment::new(image, &[]).map_err(|e| e.0)?;
    let declarations = environment.declarations();
    let (prepared, assumed) = match options.view {
        View::Exact => (prepared, Assumed::default()),
        View::Reading => reading_l1(image, &prepared, &options.disabled)?,
    };
    let (mut l2, mapping, lowered) =
        lower_function(image, &prepared, interfaces, prototypes, data)?;
    l2.name = name.map(str::to_owned);
    let feeds = Feeds::new(&prepared.l1);
    let env = L2Env {
        declarations,
        exceptions,
        feeds: Some(&feeds),
    };
    let Passed {
        manager,
        floors: reasons,
        array_loops,
        rotated,
        ..
    } = l2_passes(&mut l2, &env, &options.disabled)?;
    names::name(&mut l2, prototypes);
    let structure = validate::structure(&prepared.l1, &l2, exceptions)
        .err()
        .map(|mismatch| mismatch.to_string());
    let stats = Stats {
        counts: count(&l2),
        formats: lowered.formats,
        structure,
        floors: floors(&l2, &reasons),
        array_loops,
        rotated,
        passes: prepared.passes.into_iter().chain(manager.stats()).collect(),
        analyses: prepared
            .analyses
            .into_iter()
            .chain(manager.computed())
            .collect(),
        typing: lowered.typing,
        variadic: lowered.variadic,
        frames: lowered.frames,
        debug: lowered.debug,
        assumed,
    };
    let c = match &options.style {
        Some(style) => {
            let style = Style::preset(style)
                .ok_or_else(|| format!("no style preset {style:?}"))?
                .with_int_spelling(options.types)
                .with_declarations(options.declarations)
                .with_naming(options.names);
            let mut c = print(&l2, declarations, &style, data, options.view.opaque())?;
            if options.view == View::Reading && !stats.assumed.is_empty() {
                c.text.insert_str(0, &reading_comment(&stats.assumed));
            }
            Some(c)
        }
        None => None,
    };
    Ok(Decompiled {
        l1: prepared.l1,
        l2,
        mapping,
        c,
        stats,
    })
}

/// The error of a pass that left a function of `level` (L1, L2) failing
/// its verifier.
fn fails(level: &str, broken: &Broken) -> String {
    format!(
        "{level} after {} fails its verifier: {}",
        broken.pass, broken.message
    )
}

/// The line the reading view's C starts with when it assumed the checks
/// `assumed` (not empty): how many of each kind, singular for one.
pub fn reading_comment(assumed: &Assumed) -> String {
    let kinds = [
        ("canonical-address check", "canonical-address checks"),
        ("stack-alignment check", "stack-alignment checks"),
        (
            "return-address authentication",
            "return-address authentications",
        ),
        (
            "instruction's floating-point controls",
            "instructions' floating-point controls",
        ),
        (
            "instruction's floating-point result",
            "instructions' floating-point results",
        ),
        (
            "instruction's empty x87 stack",
            "instructions' empty x87 stack",
        ),
    ];
    let parts: Vec<String> = assumed
        .kinds
        .iter()
        .zip(kinds)
        .filter(|&(&count, _)| count > 0)
        .map(|(&count, (one, many))| format!("{count} {}", if count == 1 { one } else { many }))
        .collect();
    format!("/* Reading view: {} assumed. */\n", parts.join(", "))
}

/// The personality routines whose exception tables follow the Itanium C++
/// ABI's rule ([`binbun_decompile::itanium`]): C++'s, and C's (cleanups
/// only, `__attribute__((cleanup))`).
const ITANIUM_PERSONALITIES: [&str; 2] = ["__gxx_personality_v0", "__gcc_personality_v0"];

/// What decompilation knows about the exceptions of `image`: each landing
/// pad's action chain, where every call site unwinding to it lists the
/// same one under a personality routine following the Itanium C++ ABI's
/// rule (`__gxx_personality_v0`, `__gcc_personality_v0`)
/// (another routine's, or a damaged table's, pads stay landing pads);
/// the functions that resume unwinding: the import thunks (`thunks`:
/// entry and import name) of `_Unwind_Resume`; and the functions that
/// never throw: `nothrow`, the import thunks of the functions the type
/// library declares so ([`crate::prototypes::Imports::nothrow`]). A
/// function the image defines is not taken for one by its symbol (names
/// are not evidence of what code is: [`crate::prototypes`]).
///
/// Microsoft x64: an `__except` block's chain is its scope's filter; the
/// pads of the funclets built into their functions ([`crate::funclets`])
/// are entered for their try block's catch blocks (by type, in order,
/// selectors from 1) then the cleanup of the try's lowest state (an unwind
/// funclet, selector 0), or for a cleanup only (an unwind funclet, a
/// `__finally` block); a call to a function's exception handling data
/// (which no code calls: [`crate::funclets`]) resumes
/// unwinding.
pub fn exceptions<'n>(
    image: &Image<'_>,
    thunks: impl IntoIterator<Item = (u64, &'n str)>,
    nothrow: impl IntoIterator<Item = u64>,
) -> Exceptions {
    let unversioned = |name: &'n str| name.split('@').next().unwrap_or(name);
    let thunks: Vec<(u64, &str)> = thunks
        .into_iter()
        .map(|(entry, name)| (entry, unversioned(name)))
        .collect();
    let mut chains: BTreeMap<u64, Result<Vec<Action>, Unknown>> = BTreeMap::new();
    for table in &image.exception_tables {
        let itanium = table
            .personality
            .as_ref()
            .and_then(|routine| routine.symbol.as_deref())
            .is_some_and(|symbol| {
                let symbol = symbol.split('@').next().unwrap_or(symbol);
                ITANIUM_PERSONALITIES.contains(&symbol)
            });
        for site in &table.call_sites {
            let Some(pad) = site.landing_pad else {
                continue;
            };
            let chain = if itanium {
                actions(site).ok_or(Unknown::Damaged)
            } else {
                Err(Unknown::Personality)
            };
            chains
                .entry(pad)
                .and_modify(|known| {
                    if known.is_ok() && *known != chain {
                        *known = Err(chain.clone().err().unwrap_or(Unknown::Disagree));
                    }
                })
                .or_insert(chain);
        }
    }
    let resume = funclet_chains(image, &mut chains);
    // An `__except` block: entered for what its scope's filter takes.
    // Blocks of several scopes with different filters stay landing pads.
    for table in &image.frame_handlers {
        let HandlerTable::Scopes(scopes) = &table.table else {
            continue;
        };
        for scope in scopes {
            let ScopeAction::Except { filter, target } = scope.action else {
                continue;
            };
            let filter = match filter {
                binbun_loader::SehFilter::Constant(value) => SehFilter::Constant(value),
                binbun_loader::SehFilter::Funclet(funclet) => SehFilter::Funclet(funclet),
            };
            let chain = Ok(vec![Action {
                kind: HandlerKind::Except(filter),
                selector: 0,
            }]);
            chains
                .entry(target)
                .and_modify(|known| {
                    if known.is_ok() && *known != chain {
                        *known = Err(Unknown::Disagree);
                    }
                })
                .or_insert(chain);
        }
    }
    Exceptions {
        funclets: !image.frame_handlers.is_empty(),
        pads: chains
            .iter()
            .filter_map(|(&pad, chain)| Some((pad, chain.clone().ok()?)))
            .collect(),
        unknown: chains
            .iter()
            .filter_map(|(&pad, chain)| Some((pad, chain.clone().err()?)))
            .collect(),
        resume: thunks
            .iter()
            .filter(|&&(_, name)| name == "_Unwind_Resume")
            .map(|&(entry, _)| entry)
            .chain(resume)
            .collect(),
        nothrow: nothrow.into_iter().collect(),
    }
}

/// The action chain of call site `site`, if it is whole (a selector per
/// clause, at least one clause).
fn actions(site: &CallSite) -> Option<Vec<Action>> {
    if site.clauses.is_empty() || site.clauses.len() != site.selectors.len() {
        return None;
    }
    let catch_type = |info: &TypeInfo| CatchType {
        name: info.name.clone(),
        symbol: info.object.symbol.clone(),
        address: info.object.address,
    };
    let chain = site
        .clauses
        .iter()
        .zip(&site.selectors)
        .map(|(clause, &selector)| Action {
            kind: match clause {
                Clause::Catch(info) => HandlerKind::Catch(catch_type(info)),
                Clause::CatchAll => HandlerKind::CatchAll,
                Clause::Cleanup => HandlerKind::Cleanup,
                Clause::Filter(types) => {
                    HandlerKind::Unexpected(types.iter().map(catch_type).collect())
                }
            },
            selector,
        })
        .collect();
    Some(chain)
}

/// The action chains of the landing pads of the funclets built into their
/// functions ([`crate::funclets`]), into `chains`: a C++ try block's catch
/// blocks (selectors from 1, by type) up to its `catch (...)`
/// ([`catch_all`]: it takes every exception), or else then the unwind
/// funclet of the try's lowest state (a cleanup, 0); an unwind funclet
/// alone, a cleanup; a `__finally` block, its own kind. Returns the
/// addresses of the functions' exception handling data, a call to which
/// resumes unwinding.
fn funclet_chains(
    image: &Image<'_>,
    chains: &mut BTreeMap<u64, Result<Vec<Action>, Unknown>>,
) -> BTreeSet<u64> {
    let mut resume = BTreeSet::new();
    let alone = |chains: &mut BTreeMap<u64, Result<Vec<Action>, Unknown>>, pad, kind| {
        chains
            .entry(pad)
            .or_insert_with(|| Ok(vec![Action { kind, selector: 0 }]));
    };
    for table in &image.frame_handlers {
        resume.insert(table.data);
        let cxx = match &table.table {
            HandlerTable::Scopes(scopes) => {
                for scope in scopes {
                    if let ScopeAction::Finally(funclet) = scope.action {
                        alone(chains, funclet, HandlerKind::Finally);
                    }
                }
                continue;
            }
            HandlerTable::Cxx(cxx) => cxx,
        };
        for (state, entry) in cxx.unwind.iter().enumerate() {
            let Ok(state) = i32::try_from(state) else {
                continue;
            };
            let mut catches: Vec<&Catch> = cxx
                .tries
                .iter()
                .filter(|block| block.low == state)
                .flat_map(|block| &block.catches)
                .collect();
            let all = catches.iter().position(|catch| catch_all(catch));
            if let Some(all) = all {
                catches.truncate(all + 1);
            }
            let Some(pad) = all
                .and_then(|all| catches.get(all))
                .map(|catch| catch.funclet)
                .or(entry.action)
                .or_else(|| catches.first().map(|catch| catch.funclet))
            else {
                continue;
            };
            if catches.is_empty() {
                alone(chains, pad, HandlerKind::Cleanup);
                continue;
            }
            let mut chain: Vec<Action> = (1..)
                .zip(&catches)
                .map(|(selector, catch)| Action {
                    kind: match &catch.ty {
                        Some(info) if !catch_all(catch) => HandlerKind::Catch(CatchType {
                            name: info.name.clone(),
                            symbol: info.object.symbol.clone(),
                            address: info.object.address,
                        }),
                        Some(_) | None => HandlerKind::CatchAll,
                    },
                    selector,
                })
                .collect();
            if entry.action.is_some() && all.is_none() {
                chain.push(Action {
                    kind: HandlerKind::Cleanup,
                    selector: 0,
                });
            }
            chains.insert(pad, Ok(chain));
        }
    }
    resume
}

/// A function's L1 as decompilation starts from it ([`decompile_l1`]),
/// with what lowering reads and the counts of what made them.
#[derive(Clone, Debug)]
pub struct Prepared {
    /// The L1 function after the L1 passes.
    pub l1: Function,
    /// Its analyses, as the L1 passes left them, for lowering.
    pub facts: Facts,
    /// The counts of the L1 passes, by name.
    pub passes: Vec<(&'static str, PassStats)>,
    /// How many times each L1 analysis was computed, by type name.
    pub analyses: Vec<(&'static str, usize)>,
}

/// Function `id` of `program` (recovered from `image`) in L1, as
/// decompilation starts from it: [`lift_function_leaving`] (every computed
/// jump recovery has no table for, or resolved to code outside the
/// function, leaving the function); then the L1
/// passes under a manager (a verifier after each change, [`FUEL`] per
/// node), but those `disabled`: those leaves kept tail calls only where
/// the ABI's conditions for one hold, the others unresolved jumps
/// ([`TailJumps`]; disabled, every such jump is unresolved); under the
/// ABI's assumption that the stack and the
/// caller's return address are canonical, the branches on L0's checks
/// that they are made jumps ([`AssumeCanonical`]); then every branch
/// constant propagation over the image's read-only memory proves to go
/// one way made a jump ([`FoldBranches`]), and every branch on whether a
/// value is zero that an earlier test of the same value decides
/// ([`FoldImplied`]: `bsr` on a source tested first). So the blocks never
/// taken (a fault that cannot happen, a flag update that always does, a
/// register's old value kept for a zero source) are not decompiled.
/// Lowering, interfaces and the checks against L1 all use this
/// L1, so they agree, and lowering the analyses the passes left
/// ([`Facts::of`]).
///
/// # Errors
///
/// As for [`lift_function_leaving`], or a pass leaves L1 failing its
/// verifier.
pub fn decompile_l1(
    image: &Image<'_>,
    program: &Program,
    id: FunctionId,
    summaries: &Summaries,
    disabled: &[String],
) -> Result<Prepared, String> {
    let l1 = lift_l1(image, program, id, summaries, disabled)?;
    Ok(exact_l1(image, l1, disabled, false)?.0)
}

/// Function `id` of `program` (recovered from `image`) lifted as
/// decompilation lifts it: with the computed jumps recovery found no table
/// for, or resolved to code outside the function, leaving the function
/// ([`lift_function_leaving`]), for [`TailJumps`] to decide, unless that
/// pass is `disabled`.
///
/// # Errors
///
/// As for [`lift_function_leaving`].
pub(crate) fn lift_l1(
    image: &Image<'_>,
    program: &Program,
    id: FunctionId,
    summaries: &Summaries,
    disabled: &[String],
) -> Result<Function, String> {
    if tail_jumps(disabled) {
        lift_function_leaving(image, program, id, summaries)
    } else {
        lift_function(image, program, id, summaries)
    }
    .map_err(|e| e.0)
}

/// Whether [`TailJumps`] runs (it isn't among the passes `disabled`).
fn tail_jumps(disabled: &[String]) -> bool {
    !disabled
        .iter()
        .any(|name| name == TailJumps::default().name())
}

/// The L1 passes of [`decompile_l1`] on a function's `l1` as lifted (of
/// `image`, by [`lift_l1`]), those `disabled` not run; with what each pass
/// that changed it left if `record` ([`Manager::record`]).
///
/// # Errors
///
/// A pass leaves L1 failing its verifier.
pub(crate) fn exact_l1(
    image: &Image<'_>,
    mut l1: Function,
    disabled: &[String],
    record: bool,
) -> Result<(Prepared, Recorded<Function>), String> {
    let setting = L1Setting::new(image, l1.entry)?;
    let env = setting.env();
    let mut manager = l1_manager(&l1, disabled, record);
    if tail_jumps(disabled) {
        manager
            .run(
                &mut TailJumps::new(setting.link, setting.kept.clone()),
                &mut l1,
                &env,
            )
            .map_err(|broken| fails("L1", &broken))?;
    }
    manager
        .run(&mut AssumeCanonical, &mut l1, &env)
        .map_err(|broken| fails("L1", &broken))?;
    manager
        .run(&mut FoldBranches, &mut l1, &env)
        .map_err(|broken| fails("L1", &broken))?;
    manager
        .run(&mut FoldImplied, &mut l1, &env)
        .map_err(|broken| fails("L1", &broken))?;
    let facts = Facts::of(&mut manager, &l1, &env);
    let recorded = Recorded::of(&mut manager);
    let prepared = Prepared {
        l1,
        facts,
        passes: manager.stats().collect(),
        analyses: manager.computed().collect(),
    };
    Ok((prepared, recorded))
}

/// The manager the L1 passes of `l1` run under: [`FUEL`] per node, L1's
/// verifier after each change, the passes `disabled` not run, recording
/// if `record`.
fn l1_manager<'e>(
    l1: &Function,
    disabled: &[String],
    record: bool,
) -> Manager<Function, L1Env<'e>> {
    let mut manager =
        Manager::new(Fuel::new(FUEL.saturating_mul(l1::size(l1)))).with_verifier(l1::verify);
    for name in disabled {
        manager.disable(name);
    }
    if record {
        manager.record();
    }
    manager
}

/// The reading view's L1 ([`View::Reading`]) of a function `prepared` (of
/// `image`, by [`decompile_l1`]), the passes `disabled`: its L1 under the
/// reading view's assumptions ([`AssumeDereferenced`]: every branch on a
/// canonical-address check that guards an access goes the canonical
/// way; [`AssumeAligned`]: the stack pointer at entry is aligned as
/// AAPCS64 says, so its alignment checks pass; [`AssumeAuthenticated`],
/// where calls leave the return address in a register: a return address
/// signed with pointer authentication authenticates, as the one a call
/// left; [`AssumeFloatControls`]: the floating-point controls are what a
/// process starts with wherever the function has not loaded them;
/// [`AssumeFloatResults`]: a NaN is not told from another and a float
/// converted to an integer is in its range; [`AssumeEmptyX87`]: the x87
/// stack is empty at entry with TOP 0), then [`FoldBranches`]
/// again, with the facts lowering reads; and the checks it assumed
/// ([`Assumed`]). The interfaces stay the exact L1's.
///
/// Every run of the original L1 that takes no fault the first three
/// rule out runs the same in this L1 (a folded branch goes where it
/// went). The floating-point ones settle values too: a run under other
/// controls, or one that converts a float out of range, computes
/// something else, and a NaN this L1 gives is the operation's where the
/// original's is an operand's. L2 that agrees with this L1 agrees with
/// the original on the runs the assumptions hold for (NaNs alike): the
/// structure check and the differential compare the reading view with
/// this L1.
///
/// # Errors
///
/// A pass leaves L1 failing its verifier.
pub fn reading_l1(
    image: &Image<'_>,
    prepared: &Prepared,
    disabled: &[String],
) -> Result<(Prepared, Assumed), String> {
    let (reading, assumed, _) = reading_l1_recorded(image, prepared, disabled, false)?;
    Ok((reading, assumed))
}

/// [`reading_l1`], with what each pass that changed the L1 left if
/// `record` ([`Manager::record`]).
///
/// # Errors
///
/// As for [`reading_l1`].
pub(crate) fn reading_l1_recorded(
    image: &Image<'_>,
    prepared: &Prepared,
    disabled: &[String],
    record: bool,
) -> Result<(Prepared, Assumed, Recorded<Function>), String> {
    let mut l1 = prepared.l1.clone();
    let setting = L1Setting::new(image, l1.entry)?;
    let env = setting.env();
    let mut manager = l1_manager(&l1, disabled, record);
    let mut assume = AssumeDereferenced::default();
    manager
        .run(&mut assume, &mut l1, &env)
        .map_err(|broken| fails("L1", &broken))?;
    let mut aligned = AssumeAligned::default();
    manager
        .run(&mut aligned, &mut l1, &env)
        .map_err(|broken| fails("L1", &broken))?;
    let mut authenticated: Vec<u64> = Vec::new();
    if let Some(link) = setting.link {
        let mut authenticate = AssumeAuthenticated::new(link);
        manager
            .run(&mut authenticate, &mut l1, &env)
            .map_err(|broken| fails("L1", &broken))?;
        authenticated = authenticate.folded().to_vec();
    }
    let mut controls = AssumeFloatControls::new(setting.float_control.clone());
    manager
        .run(&mut controls, &mut l1, &env)
        .map_err(|broken| fails("L1", &broken))?;
    let mut results = AssumeFloatResults::default();
    manager
        .run(&mut results, &mut l1, &env)
        .map_err(|broken| fails("L1", &broken))?;
    let mut x87 = AssumeEmptyX87::new(setting.x87.clone());
    manager
        .run(&mut x87, &mut l1, &env)
        .map_err(|broken| fails("L1", &broken))?;
    manager
        .run(&mut FoldBranches, &mut l1, &env)
        .map_err(|broken| fails("L1", &broken))?;
    let facts = Facts::of(&mut manager, &l1, &env);
    let recorded = Recorded::of(&mut manager);
    let reading = Prepared {
        l1,
        facts,
        passes: prepared
            .passes
            .iter()
            .copied()
            .chain(manager.stats())
            .collect(),
        analyses: prepared
            .analyses
            .iter()
            .copied()
            .chain(manager.computed())
            .collect(),
    };
    let groups = [
        assume.folded(),
        aligned.folded(),
        &authenticated,
        controls.folded(),
        results.folded(),
        x87.folded(),
    ];
    let assumed = Assumed {
        sites: groups.concat(),
        kinds: groups.map(<[u64]>::len),
    };
    Ok((reading, assumed, recorded))
}

/// What the L1 passes of a function read of its image ([`L1Env`]).
struct L1Setting<'a> {
    memory: ImageMemory<'a>,
    declarations: &'static Declarations,
    stack_pointer: RegisterId,
    arguments: Vec<RegisterId>,
    return_slot: Option<i64>,
    /// The register calls leave the return address in, if not the stack.
    link: Option<RegisterId>,
    /// The registers a callee keeps, by the convention.
    kept: Vec<RegisterId>,
    thread_pointer: Option<RegisterId>,
    /// The floating-point control registers.
    float_control: Vec<Control>,
    /// The x87 stack's tag word and TOP, if the convention has the stack.
    x87: Vec<RegisterId>,
}

impl<'a> L1Setting<'a> {
    /// The setting of the function of `image` at `entry`.
    fn new(image: &'a Image<'a>, entry: u64) -> Result<Self, String> {
        let environment = ImageEnvironment::new(image, &[]).map_err(|e| e.0)?;
        let declarations = environment.declarations();
        let (return_slot, link) = match setup(image, entry)?.link {
            Link::Stack { .. } => (Some(0), None),
            Link::Register(register) => (None, Some(register)),
        };
        let convention = convention(image, declarations);
        Ok(Self {
            memory: ImageMemory::new(image),
            declarations,
            stack_pointer: environment.stack_pointer(),
            arguments: environment.arguments(),
            return_slot,
            link,
            kept: convention.callee_saved,
            thread_pointer: convention.thread_pointer,
            float_control: convention
                .float_control
                .iter()
                .map(|control| Control {
                    register: control.register,
                    status_bits: control.status_bits,
                    default: control.default,
                })
                .collect(),
            x87: convention
                .x87
                .iter()
                .flat_map(|x87| [x87.tags, x87.top])
                .collect(),
        })
    }

    fn env(&self) -> L1Env<'_> {
        L1Env {
            declarations: self.declarations,
            memory: &self.memory,
            stack_pointer: self.stack_pointer,
            arguments: &self.arguments,
            return_slot: self.return_slot,
            thread_pointer: self.thread_pointer,
        }
    }
}

/// The `prepared` L1 of a function of `image`, lowered to goto-only L2
/// ([`lowering::lower_counted`], with the analyses its passes left) under
/// the image's convention and [`setup`], the callees' `interfaces` and
/// declared `prototypes`, and the image's data; with its mapping and
/// counts.
///
/// # Errors
///
/// The image's code is not lifted, or lowering fails.
pub fn lower_function(
    image: &Image<'_>,
    prepared: &Prepared,
    interfaces: &Interfaces,
    prototypes: &dyn Prototypes,
    data: &dyn Data,
) -> Result<(Ast, Mapping, lowering::Counts), String> {
    with_context(
        image,
        prepared.l1.entry,
        interfaces,
        prototypes,
        data,
        |context| {
            lowering::lower_counted(&prepared.l1, &prepared.facts, context)
                .map_err(|e| e.to_string())
        },
    )
}

/// `f` of the lowering context of the function of `image` at `entry`
/// (the interfaces of the functions it may call, declared prototypes, the
/// image's data).
pub(crate) fn with_context<T>(
    image: &Image<'_>,
    entry: u64,
    interfaces: &Interfaces,
    prototypes: &dyn Prototypes,
    data: &dyn Data,
    f: impl FnOnce(&Context<'_>) -> Result<T, String>,
) -> Result<T, String> {
    let setup = setup(image, entry)?;
    let convention = convention(image, setup.declarations);
    let context = Context {
        fixed: &setup.fixed,
        prototypes,
        data,
        interfaces: &interfaces.interfaces,
        functions: &interfaces.functions,
        read_results: interfaces.callers.result_unread(entry).then_some(&[][..]),
        preferred_result: interfaces.callers.preferred(entry),
        dropped: interfaces.callers.dropped(entry),
        schemes: &interfaces.callers.schemes,
        specialised: interfaces.callers.specialised(entry),
        specialisations: &interfaces.callers.specialised,
        assume_futex: interfaces.callers.assume,
        ..Context::new(
            setup.declarations,
            setup.stack_pointer,
            setup.link,
            &convention,
        )
    };
    f(&context)
}

/// `l2` as a C file under `style`, functions and globals named,
/// and read-only data and instructions without semantics read, from
/// `image`; those instructions as `opaque` says.
///
/// # Errors
///
/// The printer refuses the function.
fn print(
    l2: &Ast,
    declarations: &Declarations,
    style: &Style,
    image: &dyn Printing,
    opaque: Opaque,
) -> Result<C, String> {
    let printed = binbun_print::c::function(l2, declarations, style, image, opaque)
        .map_err(|error| format!("cannot print: {error}"))?;
    Ok(C {
        text: layout::render(&printed.document, style),
        casts: printed.counts.casts,
        asm: printed.counts.asm,
        unknowns: printed.counts.unknowns,
    })
}