chipi-core 0.9.1

Core library for chipi: parser, IR, and code generation backends for instruction decoder generation
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
//! Function-pointer LUT generation. Consumes a validated definition and a
//! dispatch tree.
//!
//! The output is one or more static `[Handler; N]` arrays. There is one
//! per tree `Branch`. Each table has a small inline dispatch function next
//! to it. The result is an emulator-style lookup-table dispatch.
//!
//! ## Basic usage (one handler per instruction)
//!
//! Each instruction maps directly to `{handler_mod}::{instruction_name}`.
//!
//! ## Grouped handlers with const generics
//!
//! Multiple instructions can share a single handler via const generics. The
//! binding lists them under a `handler` block; chipi emits LUT entries that
//! supply the matching `OP_*` constant as a const-generic argument.
//!
//! ```text
//! // Generated LUT entry for addi (in the "alu" group):
//! crate::cpu::interpreter::alu::<{ OP_ADDI }>
//!
//! // User implementation:
//! pub fn alu<const OP: u32>(ctx: &mut Cpu, instr: Instruction) {
//!     match OP {
//!         OP_ADDI  => { /* ... */ }
//!         OP_ADDIS => { /* ... */ }
//!         _ => unreachable!(),
//!     }
//! }
//! ```
//!
//! ## Extra const-generic arguments via `handler_const`
//!
//! When handlers carry more than one const generic (e.g. `<const OP, const SYSTEM>`)
//! and the extra args are constant for the whole binding, set them on the
//! dispatch with `handler_const <expr>`. Each entry becomes its own
//! `{ ... }`-wrapped argument appended to every emitted handler reference:
//!
//! ```text
//! // Binding:
//! handler_const crate::system::GC
//!
//! // Generated LUT entry:
//! crate::cpu::interpreter::alu::<{ OP_ADDI }, { crate::system::GC }>
//! ```
//!
//! The compiler monomorphizes each instantiation. Runtime dispatch is one
//! indirect call into the table. There is no extra branching overhead.

use std::collections::HashMap;
use std::fmt::Write;

use crate::error::{Error, ErrorKind, Span};
use crate::tree::DecodeNode;
use crate::types::*;

struct Ctx<'a> {
    def: &'a ValidatedDef,
    handler_mod: &'a str,
    ctx_type: &'a str,
    /// Type of the second parameter passed to every handler.
    instr_type: &'a str,
    /// Expression that yields a `u32` from the local `instr` or `opcode`
    /// parameter inside a generated dispatch function.
    raw_expr: &'a str,
    uid: usize,
    /// Accumulated auxiliary items in emission order. Holds tables and
    /// dispatch functions.
    buf: String,
    /// Map from instruction name to its group handler function name. Empty
    /// means no grouping.
    groups: &'a HashMap<String, String>,
    /// Optional handler for unmatched opcodes. Replaces the default
    /// `todo!()` panic body of `_unimpl`.
    invalid_handler: Option<&'a str>,
    /// Extra const-generic arguments appended to every handler reference in
    /// the generated LUT, so handlers with more than one const generic
    /// (e.g. `<const OP, const SYSTEM>`) can be dispatched without
    /// per-instantiation wrappers.
    handler_consts: &'a [String],
}

impl<'a> Ctx<'a> {
    fn uid(&mut self) -> usize {
        let id = self.uid;
        self.uid += 1;
        id
    }

    /// Parameter name used in generated function signatures.
    /// Returns `"opcode"` for primitive integer types. Returns `"instr"`
    /// for wrapper types.
    fn param_name(&self) -> &'static str {
        if is_primitive(self.instr_type) {
            "opcode"
        } else {
            "instr"
        }
    }

    /// Return the handler expression for `instr_name` as it should appear in a
    /// static table or a direct call.
    ///
    /// - Ungrouped: `handler_mod::instr_name` (or `::<{ extra }, ...>` if
    ///   `handler_consts` is non-empty).
    /// - Grouped:   `handler_mod::group_name::<{ OP_INSTR_NAME }>` (or
    ///   `::<{ OP_INSTR_NAME }, { extra }, ...>` if `handler_consts` is set).
    fn handler_for(&self, instr_name: &str) -> String {
        let extra: Vec<String> = self.handler_consts.iter().map(|c| format!("{{ {c} }}")).collect();
        if let Some(group) = self.groups.get(instr_name) {
            let mut args = vec![format!("{{ {} }}", op_const_name(instr_name))];
            args.extend(extra);
            format!("{}::{group}::<{}>", self.handler_mod, args.join(", "))
        } else if extra.is_empty() {
            format!("{}::{}", self.handler_mod, instr_name)
        } else {
            format!("{}::{}::<{}>", self.handler_mod, instr_name, extra.join(", "))
        }
    }
}

/// Generate the Rust source for a function-pointer LUT.
///
/// `handler_mod`: module path where handlers live. Example:
/// `"crate::cpu::interpreter"`.
///
/// `ctx_type`: context type passed to every handler. Example:
/// `"crate::gekko::Gekko"`.
///
/// `groups`: map from instruction name to group handler name. An empty map
/// gives one handler per instruction.
///
/// `instr_type`: type of the second handler parameter. Pass `None` to
/// derive from the spec's `width`. The auto type is `u8`, `u16`, or `u32`.
/// Pass `Some("crate::cpu::Instruction")` to use a wrapper type.
///
/// `raw_expr`: expression that yields the underlying integer from the
/// `instr` local. Ignored when `instr_type` is `None`. Defaults to
/// `"instr.0"` for wrapper types.
///
/// `invalid_handler`: handler called for unmatched opcodes. `None` falls
/// back to a `todo!()` panic.
///
/// The generated file contains:
///
/// - `pub const OP_*: u32`. One constant per instruction. Usable as a
///   const-generic argument.
/// - `pub type Handler = fn(&mut Ctx, InstrType);`
/// - Static dispatch tables `_T0`, `_T1`, etc. One per bit-range level.
/// - `pub fn dispatch(ctx: &mut Ctx, instr: InstrType)`.
pub fn generate_lut_code(
    def: &ValidatedDef,
    tree: &DecodeNode,
    handler_mod: &str,
    ctx_type: &str,
    groups: &HashMap<String, String>,
    instr_type: Option<&str>,
    raw_expr: Option<&str>,
    dispatch: crate::Dispatch,
    invalid_handler: Option<&str>,
    handler_consts: &[String],
) -> Result<String, Vec<Error>> {
    let instr_type = instr_type.unwrap_or_else(|| width_to_type(def.config.width));
    let raw_expr = raw_expr.unwrap_or_else(|| {
        if is_primitive(instr_type) {
            "opcode"
        } else {
            "instr.0"
        }
    });

    let mut ctx = Ctx {
        def,
        handler_mod,
        ctx_type,
        instr_type,
        raw_expr,
        uid: 0,
        buf: String::new(),
        groups,
        invalid_handler,
        handler_consts,
    };

    let ct = ctx_type;
    let it = instr_type;
    let pn = ctx.param_name();
    let re = raw_expr;

    let mut out = String::new();
    writeln!(
        out,
        "// Auto-generated by https://github.com/ioncodes/chipi"
    )
    .unwrap();
    writeln!(out, "// Do not edit.").unwrap();
    writeln!(out).unwrap();

    // OP_* constants
    writeln!(
        out,
        "// Per-instruction constants. Use as const-generic arguments:"
    )
    .unwrap();
    writeln!(
        out,
        "// `fn alu<const OP: u32>(ctx, instr) {{ match OP {{ OP_ADDI => ... }} }}`"
    )
    .unwrap();
    for (i, instr) in def.instructions.iter().enumerate() {
        writeln!(out, "pub const {}: u32 = {i};", op_const_name(&instr.name)).unwrap();
    }
    writeln!(out).unwrap();

    match dispatch {
        crate::Dispatch::FnPtrLut => {
            let root = emit_node(tree, &mut ctx);

            writeln!(out, "pub type Handler = fn(&mut {ct}, {it});").unwrap();
            writeln!(out).unwrap();
            writeln!(out, "#[cold]").unwrap();
            writeln!(out, "#[inline(never)]").unwrap();
            writeln!(out, "fn _unimpl(_ctx: &mut {ct}, {pn}: {it}) {{").unwrap();
            if let Some(handler) = invalid_handler {
                writeln!(out, "    {handler}(_ctx, {pn})").unwrap();
            } else {
                writeln!(out, "    todo!(\"unimplemented opcode {{:#010x}}\", {re})").unwrap();
            }
            writeln!(out, "}}").unwrap();
            writeln!(out).unwrap();
            out.push_str(&ctx.buf);
            writeln!(out, "/// Dispatch an instruction word to its handler.").unwrap();
            writeln!(out, "#[inline(always)]").unwrap();
            writeln!(out, "pub fn dispatch(ctx: &mut {ct}, {pn}: {it}) {{").unwrap();
            writeln!(out, "    {root}(ctx, {pn});").unwrap();
            writeln!(out, "}}").unwrap();
        }
        crate::Dispatch::JumpTable => {
            writeln!(out, "/// Dispatch an instruction word to its handler.").unwrap();
            writeln!(out, "#[inline(always)]").unwrap();
            writeln!(out, "pub fn dispatch(ctx: &mut {ct}, {pn}: {it}) {{").unwrap();
            emit_jump_table_node(&mut out, tree, &mut ctx, 1);
            writeln!(out, "}}").unwrap();
        }
        crate::Dispatch::FlatLut => {
            emit_flat_lut(
                &mut out,
                FlatTargetSource::TopLevel(def),
                &mut ctx,
                "dispatch",
                ct,
                it,
                pn,
                re,
            )?;
        }
        crate::Dispatch::FlatMatch => {
            emit_flat_match(
                &mut out,
                FlatTargetSource::TopLevel(def),
                &mut ctx,
                "dispatch",
                ct,
                it,
                pn,
                re,
            )?;
        }
    }

    // Generate instr_size() for variable-length decoders
    if needs_variable_length(def) {
        writeln!(out).unwrap();
        writeln!(
            out,
            "/// Returns the size of the instruction in units (words)."
        )
        .unwrap();
        writeln!(out, "#[inline(always)]").unwrap();
        writeln!(out, "pub fn instr_size({pn}: {it}) -> u32 {{").unwrap();
        emit_size_node(&mut out, tree, def, &re, 1);
        writeln!(out, "}}").unwrap();
    }

    Ok(out)
}

/// Generate handler stub functions for every instruction.
///
/// `group_to_instrs` maps a group handler name to the instructions it
/// covers. An empty map gives one stub per instruction.
///
/// `lut_mod` is the Rust module path where the generated `OP_*` constants
/// live. Example: `"crate::cpu::lut"`. Required when groups are non-empty.
/// The const-generic stubs need this to reference the constants.
///
/// `instr_type` is the type of the second parameter. Pass `None` to derive
/// from the spec's `width`.
///
/// Run this once to bootstrap an interpreter module.
pub fn generate_stubs_code(
    def: &ValidatedDef,
    ctx_type: &str,
    group_to_instrs: &HashMap<String, Vec<String>>,
    lut_mod: Option<&str>,
    instr_type: Option<&str>,
) -> String {
    let instr_type = instr_type.unwrap_or_else(|| width_to_type(def.config.width));

    // Reverse map: instr_name -> group fn name
    let instr_to_group: HashMap<&str, &str> = group_to_instrs
        .iter()
        .flat_map(|(g, v)| v.iter().map(move |i| (i.as_str(), g.as_str())))
        .collect();

    let it = instr_type;
    let pn = if is_primitive(instr_type) {
        "_opcode"
    } else {
        "_instr"
    };

    let mut out = String::new();
    writeln!(
        out,
        "// Handler stubs. Implement each function and remove the todo!()"
    )
    .unwrap();
    writeln!(out, "#![allow(unused_variables)]").unwrap();
    writeln!(out).unwrap();

    // If there are groups and we know where the OP constants live, import them.
    if !group_to_instrs.is_empty() {
        if let Some(lut) = lut_mod {
            writeln!(out, "use {lut}::*;").unwrap();
            writeln!(out).unwrap();
        }
    }

    // Emit one const-generic stub per group, with a match arm per instruction.
    let mut emitted_groups: Vec<&str> = group_to_instrs.keys().map(|s| s.as_str()).collect();
    emitted_groups.sort();
    for group in emitted_groups {
        let instrs = &group_to_instrs[group];
        writeln!(
            out,
            "pub fn {group}<const OP: u32>(_ctx: &mut {ctx_type}, {pn}: {it}) {{"
        )
        .unwrap();
        writeln!(out, "    match OP {{").unwrap();
        for instr in instrs {
            writeln!(
                out,
                "        {} => todo!(\"{instr}\"),",
                op_const_name(instr)
            )
            .unwrap();
        }
        writeln!(out, "        _ => unreachable!(),").unwrap();
        writeln!(out, "    }}").unwrap();
        writeln!(out, "}}").unwrap();
        writeln!(out).unwrap();
    }

    // Emit individual stubs for ungrouped instructions.
    for instr in &def.instructions {
        if instr_to_group.contains_key(instr.name.as_str()) {
            continue;
        }
        writeln!(
            out,
            "pub fn {}(_ctx: &mut {ctx_type}, {pn}: {it}) {{ todo!(\"{}\") }}",
            instr.name, instr.name,
        )
        .unwrap();
    }

    out
}

fn emit_node(node: &DecodeNode, ctx: &mut Ctx) -> String {
    match node {
        DecodeNode::Fail => "_unimpl".to_string(),

        DecodeNode::Leaf { instruction_index } => {
            ctx.handler_for(&ctx.def.instructions[*instruction_index].name)
        }

        DecodeNode::PriorityLeaves { candidates } => {
            let id = ctx.uid();
            let fn_name = format!("_priority_{id}");
            // Clone these strings before the loop so we can mutably borrow ctx.buf below.
            let ct = ctx.ctx_type.to_string();
            let it = ctx.instr_type.to_string();
            let pn = ctx.param_name();
            let re = ctx.raw_expr.to_string();

            let mut body = String::new();
            writeln!(body, "#[inline(always)]").unwrap();
            writeln!(body, "fn {fn_name}(ctx: &mut {ct}, {pn}: {it}) {{").unwrap();

            let mut has_open_branch = false;
            for (i, &idx) in candidates.iter().enumerate() {
                let handler = ctx.handler_for(&ctx.def.instructions[idx].name);
                let guard = full_guard_expr(&ctx.def.instructions[idx], &re);

                match (i, guard) {
                    (0, Some(g)) => {
                        writeln!(body, "    if {g} {{").unwrap();
                        writeln!(body, "        {handler}(ctx, {pn});").unwrap();
                        has_open_branch = true;
                    }
                    (_, Some(g)) => {
                        writeln!(body, "    }} else if {g} {{").unwrap();
                        writeln!(body, "        {handler}(ctx, {pn});").unwrap();
                    }
                    (0, None) => {
                        writeln!(body, "    {handler}(ctx, {pn});").unwrap();
                        has_open_branch = false;
                        break;
                    }
                    (_, None) => {
                        writeln!(body, "    }} else {{").unwrap();
                        writeln!(body, "        {handler}(ctx, {pn});").unwrap();
                        writeln!(body, "    }}").unwrap();
                        has_open_branch = false;
                        break;
                    }
                }
            }
            if has_open_branch {
                writeln!(body, "    }}").unwrap();
            }
            writeln!(body, "}}\n").unwrap();

            ctx.buf.push_str(&body);
            fn_name
        }

        DecodeNode::Branch {
            range,
            arms,
            default,
        } => {
            let id = ctx.uid();
            let table = format!("_T{id}");
            let dispatch = format!("_d{id}");
            let size = 1usize << range.width();
            // Clone these strings before emitting so we can mutably borrow ctx.buf below.
            let ct = ctx.ctx_type.to_string();
            let it = ctx.instr_type.to_string();
            let pn = ctx.param_name();
            let re = ctx.raw_expr.to_string();

            let default_handler = emit_node(default, ctx);
            let mut entries: Vec<String> = vec![default_handler; size];
            for (value, child) in arms {
                let handler = emit_node(child, ctx);
                let idx = *value as usize;
                if idx < size {
                    entries[idx] = handler;
                }
            }

            writeln!(ctx.buf, "static {table}: [Handler; {size}] = [").unwrap();
            for (i, entry) in entries.iter().enumerate() {
                writeln!(ctx.buf, "    {entry}, // {i:#x}").unwrap();
            }
            writeln!(ctx.buf, "];\n").unwrap();

            let extract = range_extract_expr(range, &re);
            writeln!(ctx.buf, "#[inline(always)]").unwrap();
            writeln!(ctx.buf, "fn {dispatch}(ctx: &mut {ct}, {pn}: {it}) {{").unwrap();
            writeln!(ctx.buf, "    {table}[({extract}) as usize](ctx, {pn});").unwrap();
            writeln!(ctx.buf, "}}\n").unwrap();

            dispatch
        }
    }
}

/// Emit a decode tree node as nested match statements for the JumpTable strategy.
fn emit_jump_table_node(out: &mut String, node: &DecodeNode, ctx: &mut Ctx, indent: usize) {
    let pad = "    ".repeat(indent);
    let pn = ctx.param_name();
    // Clone raw_expr so we can pass `ctx` mutably to recursive calls below.
    let re = ctx.raw_expr.to_string();

    match node {
        DecodeNode::Fail => {
            if let Some(handler) = ctx.invalid_handler {
                writeln!(out, "{pad}{handler}(ctx, {pn});").unwrap();
            } else {
                writeln!(
                    out,
                    "{pad}todo!(\"unimplemented opcode {{:#010x}}\", {re});"
                )
                .unwrap();
            }
        }
        DecodeNode::Leaf { instruction_index } => {
            let handler = ctx.handler_for(&ctx.def.instructions[*instruction_index].name);
            writeln!(out, "{pad}{handler}(ctx, {pn});").unwrap();
        }
        DecodeNode::PriorityLeaves { candidates } => {
            for (i, &idx) in candidates.iter().enumerate() {
                let handler = ctx.handler_for(&ctx.def.instructions[idx].name);
                let guard = full_guard_expr(&ctx.def.instructions[idx], &re);

                match (i, guard) {
                    (0, Some(g)) => {
                        writeln!(out, "{pad}if {g} {{").unwrap();
                        writeln!(out, "{pad}    {handler}(ctx, {pn});").unwrap();
                    }
                    (_, Some(g)) => {
                        writeln!(out, "{pad}}} else if {g} {{").unwrap();
                        writeln!(out, "{pad}    {handler}(ctx, {pn});").unwrap();
                    }
                    (0, None) => {
                        writeln!(out, "{pad}{handler}(ctx, {pn});").unwrap();
                        return;
                    }
                    (_, None) => {
                        writeln!(out, "{pad}}} else {{").unwrap();
                        writeln!(out, "{pad}    {handler}(ctx, {pn});").unwrap();
                        writeln!(out, "{pad}}}").unwrap();
                        return;
                    }
                }
            }
            // Close the last if branch
            writeln!(out, "{pad}}}").unwrap();
        }
        DecodeNode::Branch {
            range,
            arms,
            default,
        } => {
            let extract = range_extract_expr(range, &re);
            writeln!(out, "{pad}match ({extract}) as usize {{").unwrap();

            for (value, child) in arms {
                writeln!(out, "{pad}    {value:#x} => {{").unwrap();
                emit_jump_table_node(out, child, ctx, indent + 2);
                writeln!(out, "{pad}    }}").unwrap();
            }

            writeln!(out, "{pad}    _ => {{").unwrap();
            emit_jump_table_node(out, default, ctx, indent + 2);
            writeln!(out, "{pad}    }}").unwrap();
            writeln!(out, "{pad}}}").unwrap();
        }
    }
}

/// Map a decoder `width` to the corresponding Rust unsigned integer type.
fn width_to_type(width: u32) -> &'static str {
    match width {
        8 => "u8",
        16 => "u16",
        _ => "u32",
    }
}

/// Returns `true` for the Rust primitive unsigned integer types that chipi may
/// emit automatically. Used to decide the parameter name (`opcode` vs `instr`)
/// and whether a raw-extraction expression is needed.
fn is_primitive(t: &str) -> bool {
    matches!(t, "u8" | "u16" | "u32")
}

/// Convert an instruction name to its `OP_*` constant name.
///
/// e.g. `"addi"` -> `"OP_ADDI"`, `"ps_add."` -> `"OP_PS_ADD_DOT"`
/// Build a handler reference in the form expected by the LUT or a direct call:
///
/// - Ungrouped, no extra consts: `handler_mod::instr_name`
/// - Ungrouped, with consts:     `handler_mod::instr_name::<{ extra }, ...>`
/// - Grouped, no extra consts:   `handler_mod::group::<{ OP_INSTR }>`
/// - Grouped, with consts:       `handler_mod::group::<{ OP_INSTR }, { extra }, ...>`
///
/// `handler_consts` lets handlers carry extra const generics (e.g. a system
/// id) without per-binding wrapper modules.
pub fn build_handler_ref(
    handler_mod: &str,
    instr_name: &str,
    groups: &HashMap<String, String>,
    handler_consts: &[String],
) -> String {
    let extra: Vec<String> = handler_consts.iter().map(|c| format!("{{ {c} }}")).collect();
    if let Some(group) = groups.get(instr_name) {
        let mut args = vec![format!("{{ {} }}", op_const_name(instr_name))];
        args.extend(extra);
        format!("{}::{group}::<{}>", handler_mod, args.join(", "))
    } else if extra.is_empty() {
        format!("{}::{}", handler_mod, instr_name)
    } else {
        format!("{}::{}::<{}>", handler_mod, instr_name, extra.join(", "))
    }
}

pub fn op_const_name(name: &str) -> String {
    let sanitised = name.to_uppercase().replace('.', "_DOT").replace('-', "_");
    format!("OP_{sanitised}")
}

fn full_guard_expr(instr: &ValidatedInstruction, raw_expr: &str) -> Option<String> {
    let mut mask: u32 = 0;
    let mut value: u32 = 0;
    for (unit, hw_bit, bit) in instr.fixed_bits() {
        if unit != 0 || bit == Bit::Wildcard {
            continue;
        }
        mask |= 1 << hw_bit;
        if bit == Bit::One {
            value |= 1 << hw_bit;
        }
    }
    if mask == 0 {
        None
    } else {
        Some(format!("{raw_expr} & {mask:#010x} == {value:#010x}"))
    }
}

fn range_extract_expr(range: &BitRange, raw_expr: &str) -> String {
    let width = range.width();
    let shift = range.end;
    let mask = (1u32 << width) - 1;
    if shift == 0 {
        format!("{raw_expr} & {mask:#x}")
    } else {
        format!("({raw_expr} >> {shift}) & {mask:#x}")
    }
}

/// Generate a flat dispatch function for a sub-decoder.
///
/// Produces `pub fn dispatch_{snake_name}(ctx: &mut Ctx, val: u8)` that
/// decodes the extension bits and calls the appropriate handler.
pub fn generate_subdecoder_dispatch(
    _def: &ValidatedDef,
    sd: &ValidatedSubDecoder,
    handler_mod: &str,
    ctx_type: &str,
    groups: &HashMap<String, String>,
    instr_type: Option<&str>,
    handler_consts: &[String],
) -> String {
    let snake_name = sd.name.chars().fold(String::new(), |mut acc, c| {
        if c.is_uppercase() && !acc.is_empty() {
            acc.push('_');
        }
        acc.push(c.to_ascii_lowercase());
        acc
    });
    let dispatch_fn = format!("dispatch_{snake_name}");
    let lut_size = 1usize << sd.width;

    let mut out = String::new();

    // OP_EXT_* constants
    writeln!(out, "// Sub-decoder constants for {}", sd.name).unwrap();
    for (i, instr) in sd.instructions.iter().enumerate() {
        writeln!(out, "pub const {}: u32 = {i};", op_const_name(&instr.name)).unwrap();
    }
    writeln!(out).unwrap();

    // Build dispatch table: value -> instruction index
    // Prefer more specific matches (more fixed bits)
    let mut dispatch_table: Vec<Option<usize>> = vec![None; lut_size];
    let mut specificity: Vec<u32> = vec![0; lut_size];

    for (instr_idx, instr) in sd.instructions.iter().enumerate() {
        // Count fixed (non-wildcard) bits
        let fixed_count: u32 = instr
            .segments
            .iter()
            .map(|seg| {
                if let Segment::Fixed { pattern, .. } = seg {
                    pattern
                        .iter()
                        .filter(|b| matches!(b, Bit::Zero | Bit::One))
                        .count() as u32
                } else {
                    0
                }
            })
            .sum();

        for val in 0..lut_size {
            let matches = instr.segments.iter().all(|seg| {
                if let Segment::Fixed {
                    ranges, pattern, ..
                } = seg
                {
                    let mut bit_idx = 0;
                    for range in ranges {
                        for i in 0..range.width() as usize {
                            if bit_idx < pattern.len() {
                                let hw_bit = range.start - i as u32;
                                let bit_val = (val >> hw_bit) & 1;
                                match pattern[bit_idx] {
                                    Bit::Zero if bit_val != 0 => return false,
                                    Bit::One if bit_val != 1 => return false,
                                    _ => {}
                                }
                                bit_idx += 1;
                            }
                        }
                    }
                    true
                } else {
                    true
                }
            });
            if matches && (dispatch_table[val].is_none() || fixed_count > specificity[val]) {
                dispatch_table[val] = Some(instr_idx);
                specificity[val] = fixed_count;
            }
        }
    }

    // Emit jump table dispatch
    let param_type = width_to_type(sd.width);
    let (param_name, param_type_str, raw_expr) = if let Some(it) = instr_type {
        ("instr", it.to_string(), "instr.0".to_string())
    } else {
        ("val", param_type.to_string(), "val".to_string())
    };
    writeln!(out, "/// Dispatch a sub-decoder extension opcode.").unwrap();
    writeln!(out, "#[inline(always)]").unwrap();
    writeln!(
        out,
        "pub fn {dispatch_fn}(ctx: &mut {ctx_type}, {param_name}: {param_type_str}) {{"
    )
    .unwrap();
    writeln!(out, "    match {raw_expr} {{").unwrap();

    let mut i = 0;
    while i < lut_size {
        let current = dispatch_table[i];
        let start = i;
        while i < lut_size && dispatch_table[i] == current {
            i += 1;
        }
        let end = i - 1;

        let pattern = if start == end {
            format!("{:#x}", start)
        } else {
            format!("{:#x}..={:#x}", start, end)
        };

        match current {
            Some(idx) => {
                let instr_name = &sd.instructions[idx].name;
                let handler = build_handler_ref(handler_mod, instr_name, groups, handler_consts);
                writeln!(out, "        {pattern} => {handler}(ctx, {param_name}),").unwrap();
            }
            None => {
                writeln!(out, "        {pattern} => {{}},").unwrap();
            }
        }
    }

    writeln!(out, "    }}").unwrap();
    writeln!(out, "}}").unwrap();

    out
}

/// Check if any instruction in the decoder requires multiple units.
fn needs_variable_length(def: &ValidatedDef) -> bool {
    def.instructions.iter().any(|i| i.unit_count() > 1)
}

/// Emit a decode tree node that returns the instruction size in units.
fn emit_size_node(
    out: &mut String,
    node: &DecodeNode,
    def: &ValidatedDef,
    raw_expr: &str,
    indent: usize,
) {
    let pad = "    ".repeat(indent);

    match node {
        DecodeNode::Fail => {
            writeln!(out, "{pad}1").unwrap();
        }
        DecodeNode::Leaf { instruction_index } => {
            let size = def.instructions[*instruction_index].unit_count();
            writeln!(out, "{pad}{size}").unwrap();
        }
        DecodeNode::PriorityLeaves { candidates } => {
            for (i, &idx) in candidates.iter().enumerate() {
                let size = def.instructions[idx].unit_count();
                let guard = full_guard_expr(&def.instructions[idx], raw_expr);

                match (i, guard) {
                    (0, Some(g)) => {
                        writeln!(out, "{pad}if {g} {{").unwrap();
                        writeln!(out, "{pad}    {size}").unwrap();
                    }
                    (_, Some(g)) => {
                        writeln!(out, "{pad}}} else if {g} {{").unwrap();
                        writeln!(out, "{pad}    {size}").unwrap();
                    }
                    (0, None) => {
                        writeln!(out, "{pad}{size}").unwrap();
                        return;
                    }
                    (_, None) => {
                        writeln!(out, "{pad}}} else {{").unwrap();
                        writeln!(out, "{pad}    {size}").unwrap();
                        writeln!(out, "{pad}}}").unwrap();
                        return;
                    }
                }
            }
            writeln!(out, "{pad}}}").unwrap();
        }
        DecodeNode::Branch {
            range,
            arms,
            default,
        } => {
            let extract = range_extract_expr(range, raw_expr);
            writeln!(out, "{pad}match ({extract}) as usize {{").unwrap();

            for (value, child) in arms {
                writeln!(out, "{pad}    {value:#x} => {{").unwrap();
                emit_size_node(out, child, def, raw_expr, indent + 2);
                writeln!(out, "{pad}    }}").unwrap();
            }

            writeln!(out, "{pad}    _ => {{").unwrap();
            emit_size_node(out, default, def, raw_expr, indent + 2);
            writeln!(out, "{pad}    }}").unwrap();
            writeln!(out, "{pad}}}").unwrap();
        }
    }
}

// ---------------------------------------------------------------------------
// Flat dispatch (flat_lut, flat_match)
// ---------------------------------------------------------------------------

/// Source of instructions for flat-dispatch enumeration.
/// `TopLevel` considers only unit-0 segments. `Sub` considers all segments.
pub(crate) enum FlatTargetSource<'a> {
    TopLevel(&'a ValidatedDef),
    Sub(&'a ValidatedSubDecoder),
}

impl<'a> FlatTargetSource<'a> {
    fn width(&self) -> u32 {
        match self {
            FlatTargetSource::TopLevel(d) => d.config.width,
            FlatTargetSource::Sub(s) => s.width,
        }
    }

    fn instruction_count(&self) -> usize {
        match self {
            FlatTargetSource::TopLevel(d) => d.instructions.len(),
            FlatTargetSource::Sub(s) => s.instructions.len(),
        }
    }

    fn instr_name(&self, idx: usize) -> &str {
        match self {
            FlatTargetSource::TopLevel(d) => &d.instructions[idx].name,
            FlatTargetSource::Sub(s) => &s.instructions[idx].name,
        }
    }

    /// Whether instruction `idx` matches raw value `raw`.
    /// Top-level decoders look only at unit-0 segments. Variable-length
    /// instructions still flat-dispatch on their first word.
    fn matches(&self, idx: usize, raw: u64) -> bool {
        let segments = match self {
            FlatTargetSource::TopLevel(d) => &d.instructions[idx].segments,
            FlatTargetSource::Sub(s) => &s.instructions[idx].segments,
        };
        let only_unit_zero = matches!(self, FlatTargetSource::TopLevel(_));

        for seg in segments {
            if let Segment::Fixed {
                ranges, pattern, ..
            } = seg
            {
                let mut bit_idx = 0;
                for range in ranges {
                    let in_unit_0 = range.unit == 0;
                    let range_width = range.width() as usize;
                    if only_unit_zero && !in_unit_0 {
                        bit_idx += range_width;
                        continue;
                    }
                    for i in 0..range_width {
                        if bit_idx >= pattern.len() {
                            break;
                        }
                        let hw_bit = range.start - i as u32;
                        let bit_val = (raw >> hw_bit) & 1;
                        match pattern[bit_idx] {
                            Bit::Zero if bit_val != 0 => return false,
                            Bit::One if bit_val != 1 => return false,
                            _ => {}
                        }
                        bit_idx += 1;
                    }
                }
            }
        }
        true
    }
}

/// Build a `Vec<String>` of length `2^width`. Each entry is the resolved
/// handler expression for that raw value. Returns ambiguity errors if any
/// value resolves to two or more distinct handler strings.
fn build_flat_handler_table(
    src: &FlatTargetSource,
    handler_for: &dyn Fn(&str) -> String,
    invalid_handler: &str,
    span: &Span,
) -> Result<Vec<String>, Vec<Error>> {
    let width = src.width();
    let n: u64 = 1u64 << width;
    let mut table: Vec<String> = vec![invalid_handler.to_string(); n as usize];
    let mut errors: Vec<Error> = Vec::new();

    let n_instrs = src.instruction_count();

    for raw in 0..n {
        let mut matched: Vec<usize> = Vec::new();
        for idx in 0..n_instrs {
            if src.matches(idx, raw) {
                matched.push(idx);
            }
        }
        if matched.is_empty() {
            // already invalid_handler
        } else if matched.len() == 1 {
            table[raw as usize] = handler_for(src.instr_name(matched[0]));
        } else {
            let handlers: Vec<String> = matched
                .iter()
                .map(|i| handler_for(src.instr_name(*i)))
                .collect();
            let first = &handlers[0];
            if handlers.iter().all(|h| h == first) {
                table[raw as usize] = first.clone();
            } else {
                let pairs: Vec<(String, String)> = matched
                    .iter()
                    .zip(handlers.iter())
                    .map(|(i, h)| (src.instr_name(*i).to_string(), h.clone()))
                    .collect();
                errors.push(Error::new(
                    ErrorKind::FlatDispatchAmbiguous {
                        raw,
                        matches: pairs,
                    },
                    span.clone(),
                ));
            }
        }
    }

    if errors.is_empty() {
        Ok(table)
    } else {
        Err(errors)
    }
}

#[allow(clippy::too_many_arguments)]
fn emit_flat_lut(
    out: &mut String,
    src: FlatTargetSource,
    ctx: &mut Ctx,
    fn_name: &str,
    ct: &str,
    it: &str,
    pn: &str,
    re: &str,
) -> Result<(), Vec<Error>> {
    let invalid = ctx
        .invalid_handler
        .map(|s| s.to_string())
        .unwrap_or_else(|| "_unimpl".to_string());

    let handler_for_owned = |name: &str| ctx.handler_for(name);
    let span = Span::new("<flat_lut>", 0, 0, 0);
    let table = build_flat_handler_table(&src, &handler_for_owned, &invalid, &span)?;

    let n = table.len();
    writeln!(out, "pub type Handler = fn(&mut {ct}, {it});").unwrap();
    writeln!(out).unwrap();
    if ctx.invalid_handler.is_none() {
        writeln!(out, "#[cold]").unwrap();
        writeln!(out, "#[inline(never)]").unwrap();
        writeln!(out, "fn _unimpl(_ctx: &mut {ct}, {pn}: {it}) {{").unwrap();
        writeln!(out, "    todo!(\"unimplemented opcode {{:#010x}}\", {re})").unwrap();
        writeln!(out, "}}").unwrap();
        writeln!(out).unwrap();
    }
    writeln!(out, "static DISPATCH: [Handler; {n}] = [").unwrap();
    for (i, entry) in table.iter().enumerate() {
        writeln!(out, "    {entry}, // {i:#x}").unwrap();
    }
    writeln!(out, "];").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "/// Dispatch via a flat lookup table.").unwrap();
    writeln!(out, "#[inline(always)]").unwrap();
    writeln!(out, "pub fn {fn_name}(ctx: &mut {ct}, {pn}: {it}) {{").unwrap();
    writeln!(out, "    let key = ({re}) as usize;").unwrap();
    writeln!(out, "    DISPATCH[key](ctx, {pn});").unwrap();
    writeln!(out, "}}").unwrap();

    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn emit_flat_match(
    out: &mut String,
    src: FlatTargetSource,
    ctx: &mut Ctx,
    fn_name: &str,
    ct: &str,
    it: &str,
    pn: &str,
    re: &str,
) -> Result<(), Vec<Error>> {
    let invalid = ctx
        .invalid_handler
        .map(|s| s.to_string())
        .unwrap_or_else(|| "_unimpl".to_string());

    let handler_for_owned = |name: &str| ctx.handler_for(name);
    let span = Span::new("<flat_match>", 0, 0, 0);
    let table = build_flat_handler_table(&src, &handler_for_owned, &invalid, &span)?;

    if ctx.invalid_handler.is_none() {
        writeln!(out, "#[cold]").unwrap();
        writeln!(out, "#[inline(never)]").unwrap();
        writeln!(out, "fn _unimpl(_ctx: &mut {ct}, {pn}: {it}) {{").unwrap();
        writeln!(out, "    todo!(\"unimplemented opcode {{:#010x}}\", {re})").unwrap();
        writeln!(out, "}}").unwrap();
        writeln!(out).unwrap();
    }
    writeln!(out, "/// Dispatch via a compressed match.").unwrap();
    writeln!(out, "#[inline(always)]").unwrap();
    writeln!(out, "pub fn {fn_name}(ctx: &mut {ct}, {pn}: {it}) {{").unwrap();
    writeln!(out, "    match ({re}) as u64 {{").unwrap();

    let mut i = 0usize;
    let n = table.len();
    while i < n {
        let current = &table[i];
        let start = i;
        while i < n && table[i] == *current {
            i += 1;
        }
        let end = i - 1;
        let pattern = if start == end {
            format!("{:#x}", start)
        } else {
            format!("{:#x}..={:#x}", start, end)
        };
        writeln!(out, "        {pattern} => {current}(ctx, {pn}),").unwrap();
    }

    writeln!(out, "        _ => {invalid}(ctx, {pn}),").unwrap();
    writeln!(out, "    }}").unwrap();
    writeln!(out, "}}").unwrap();
    Ok(())
}

/// Generate a flat dispatch function for a sub-decoder.
/// Uses the explicit `flat_lut` or `flat_match` strategy.
/// Errors on raw-value ambiguity.
#[allow(clippy::too_many_arguments)]
pub fn generate_subdecoder_flat_dispatch(
    sd: &ValidatedSubDecoder,
    handler_mod: &str,
    ctx_type: &str,
    groups: &HashMap<String, String>,
    instr_type: Option<&str>,
    invalid_handler: Option<&str>,
    strategy: crate::Dispatch,
    handler_consts: &[String],
) -> Result<String, Vec<Error>> {
    let snake_name = sd.name.chars().fold(String::new(), |mut acc, c| {
        if c.is_uppercase() && !acc.is_empty() {
            acc.push('_');
        }
        acc.push(c.to_ascii_lowercase());
        acc
    });
    let dispatch_fn = format!("dispatch_{snake_name}");

    let param_type = width_to_type(sd.width);
    let (param_name, param_type_str, raw_expr_str) = if let Some(it) = instr_type {
        ("instr", it.to_string(), "instr.0".to_string())
    } else {
        ("val", param_type.to_string(), "val".to_string())
    };

    // Build a temporary Ctx-like structure for handler resolution. We
    // emulate Ctx::handler_for using the local `groups` map and `handler_mod`.
    let resolve =
        |name: &str| -> String { build_handler_ref(handler_mod, name, groups, handler_consts) };
    let invalid = invalid_handler
        .map(|s| s.to_string())
        .unwrap_or_else(|| format!("{}::invalid", handler_mod));

    let span = Span::new("<flat_subdispatch>", 0, 0, 0);
    let table = build_flat_handler_table(&FlatTargetSource::Sub(sd), &resolve, &invalid, &span)?;

    let mut out = String::new();

    // OP_* constants for the sub-decoder's instructions
    writeln!(out, "// Sub-decoder constants for {}", sd.name).unwrap();
    for (i, instr) in sd.instructions.iter().enumerate() {
        writeln!(out, "pub const {}: u32 = {i};", op_const_name(&instr.name)).unwrap();
    }
    writeln!(out).unwrap();

    match strategy {
        crate::Dispatch::FlatLut => {
            let n = table.len();
            writeln!(
                out,
                "pub type SubHandler{name} = fn(&mut {ctx_type}, {param_type_str});",
                name = sd.name,
            )
            .unwrap();
            writeln!(out).unwrap();
            writeln!(
                out,
                "static DISPATCH_{up}: [SubHandler{name}; {n}] = [",
                up = sd.name.to_uppercase(),
                name = sd.name,
            )
            .unwrap();
            for (i, entry) in table.iter().enumerate() {
                writeln!(out, "    {entry}, // {i:#x}").unwrap();
            }
            writeln!(out, "];").unwrap();
            writeln!(out).unwrap();
            writeln!(out, "/// Dispatch a sub-decoder extension opcode.").unwrap();
            writeln!(out, "#[inline(always)]").unwrap();
            writeln!(
                out,
                "pub fn {dispatch_fn}(ctx: &mut {ctx_type}, {param_name}: {param_type_str}) {{"
            )
            .unwrap();
            writeln!(
                out,
                "    DISPATCH_{up}[({raw_expr_str}) as usize](ctx, {param_name});",
                up = sd.name.to_uppercase()
            )
            .unwrap();
            writeln!(out, "}}").unwrap();
        }
        crate::Dispatch::FlatMatch => {
            writeln!(out, "/// Dispatch a sub-decoder extension opcode.").unwrap();
            writeln!(out, "#[inline(always)]").unwrap();
            writeln!(
                out,
                "pub fn {dispatch_fn}(ctx: &mut {ctx_type}, {param_name}: {param_type_str}) {{"
            )
            .unwrap();
            writeln!(out, "    match {raw_expr_str} {{").unwrap();

            let mut i = 0usize;
            let n = table.len();
            while i < n {
                let current = &table[i];
                let start = i;
                while i < n && table[i] == *current {
                    i += 1;
                }
                let end = i - 1;
                let pattern = if start == end {
                    format!("{:#x}", start)
                } else {
                    format!("{:#x}..={:#x}", start, end)
                };
                writeln!(out, "        {pattern} => {current}(ctx, {param_name}),").unwrap();
            }
            writeln!(out, "        _ => {invalid}(ctx, {param_name}),").unwrap();
            writeln!(out, "    }}").unwrap();
            writeln!(out, "}}").unwrap();
        }
        _ => {
            return Err(vec![Error::new(
                ErrorKind::InvalidStrategy(format!("{:?}", strategy)),
                span,
            )]);
        }
    }

    Ok(out)
}