wirespec-backend-c 0.4.2

C backend for wirespec
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
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
// crates/wirespec-backend-c/src/source.rs
//
// .c source file assembly: includes, parse, serialize, serialized_len implementations.

use wirespec_codec::ir::*;
use wirespec_sema::expr::{SemanticExpr, TransitionPeerKind};
use wirespec_sema::ir::{
    SemanticAction, SemanticDelegate, SemanticEnum, SemanticStateMachine, SemanticVarInt,
    VarIntEncoding,
};
use wirespec_sema::types::PrimitiveWireType;

use crate::expr::SmExprContext;
use crate::names::*;
use crate::parse_emit;
use crate::serialize_emit;

/// Resolve enum fields: change strategy from Struct to Primitive and set
/// wire_type to the enum's underlying primitive type. This allows the
/// standard primitive read/write codegen path to handle enum fields.
fn resolve_enum_fields(fields: &[CodecField], enums: &[SemanticEnum]) -> Vec<CodecField> {
    fields
        .iter()
        .map(|f| {
            if f.strategy == FieldStrategy::Struct
                && let WireType::Enum(ref name) = f.wire_type
                && let Some(e) = enums.iter().find(|e| &e.name == name)
            {
                let underlying_wt = semantic_type_to_wire_type_simple(&e.underlying_type);
                let mut f2 = f.clone();
                f2.strategy = FieldStrategy::Primitive;
                f2.wire_type = underlying_wt;
                f2.ref_type_name = None;
                // Propagate endianness from the enum's underlying type
                if let wirespec_sema::types::SemanticType::Primitive { endianness, .. } =
                    &e.underlying_type
                {
                    f2.endianness = *endianness;
                }
                return f2;
            }
            f.clone()
        })
        .collect()
}

/// Simple mapping from SemanticType (Primitive only) to WireType.
fn semantic_type_to_wire_type_simple(ty: &wirespec_sema::types::SemanticType) -> WireType {
    use wirespec_sema::types::SemanticType;
    match ty {
        SemanticType::Primitive { wire, .. } => match wire {
            PrimitiveWireType::U8 => WireType::U8,
            PrimitiveWireType::U16 => WireType::U16,
            PrimitiveWireType::U24 => WireType::U24,
            PrimitiveWireType::U32 => WireType::U32,
            PrimitiveWireType::U64 => WireType::U64,
            PrimitiveWireType::I8 => WireType::I8,
            PrimitiveWireType::I16 => WireType::I16,
            PrimitiveWireType::I32 => WireType::I32,
            PrimitiveWireType::I64 => WireType::I64,
            PrimitiveWireType::Bool => WireType::Bool,
            PrimitiveWireType::Bit => WireType::Bit,
        },
        _ => WireType::U8, // fallback for unexpected types
    }
}

/// Emit the complete .c source file content.
pub fn emit_source(module: &CodecModule, prefix: &str) -> String {
    let mut out = String::new();

    // Set the const prefix for expression generation
    crate::expr::set_const_prefix(prefix);

    // Include the generated header
    out.push_str(&format!("#include \"{prefix}.h\"\n\n"));

    // Forward declarations for static parse_cursor functions (needed when
    // type A references type B's parse_cursor but B is defined later in the file).
    for packet in &module.packets {
        let tname = c_type_name(prefix, &packet.name);
        let cursor_fn = c_func_name(prefix, &packet.name, "parse_cursor");
        let has_cksum = packet.checksum_plan.is_some()
            && packet
                .checksum_plan
                .as_ref()
                .is_some_and(|p| p.input_model == ChecksumInputModel::RecomputeWithSkippedField);
        if has_cksum {
            out.push_str(&format!(
                "static wirespec_result_t {cursor_fn}(wirespec_cursor_t *cur, {tname} *out, size_t *_cksum_offset_out);\n"
            ));
        } else {
            out.push_str(&format!(
                "static wirespec_result_t {cursor_fn}(wirespec_cursor_t *cur, {tname} *out);\n"
            ));
        }
    }
    for frame in &module.frames {
        let tname = c_type_name(prefix, &frame.name);
        let cursor_fn = c_func_name(prefix, &frame.name, "parse_cursor");
        out.push_str(&format!(
            "static wirespec_result_t {cursor_fn}(wirespec_cursor_t *cur, {tname} *out);\n"
        ));
    }
    for capsule in &module.capsules {
        let tname = c_type_name(prefix, &capsule.name);
        let cursor_fn = c_func_name(prefix, &capsule.name, "parse_cursor");
        out.push_str(&format!(
            "static wirespec_result_t {cursor_fn}(wirespec_cursor_t *cur, {tname} *out);\n"
        ));
    }
    out.push('\n');

    // VarInt functions
    for vi in &module.varints {
        emit_varint_source(&mut out, vi, prefix);
    }

    // Packets
    for packet in &module.packets {
        emit_packet_parse(&mut out, packet, prefix, &module.enums);
        emit_packet_serialize(&mut out, packet, prefix, &module.enums);
        emit_packet_serialized_len(&mut out, packet, prefix, &module.enums);
    }

    // Frames (resolve enum fields in variants)
    for frame in &module.frames {
        let frame = resolve_frame(frame, &module.enums);
        emit_frame_parse(&mut out, &frame, prefix, &module.enums);
        emit_frame_serialize(&mut out, &frame, prefix, &module.enums);
        emit_frame_serialized_len(&mut out, &frame, prefix, &module.enums);
    }

    // Capsules (resolve enum fields in header + variants)
    for capsule in &module.capsules {
        let capsule = resolve_capsule(capsule, &module.enums);
        emit_capsule_parse(&mut out, &capsule, prefix, &module.enums);
        emit_capsule_serialize(&mut out, &capsule, prefix, &module.enums);
        emit_capsule_serialized_len(&mut out, &capsule, prefix, &module.enums);
    }

    // State machines
    for sm in &module.state_machines {
        emit_sm_dispatch(&mut out, sm, prefix);
    }

    out
}

// ── Packet ──

fn emit_packet_parse(out: &mut String, packet: &CodecPacket, prefix: &str, enums: &[SemanticEnum]) {
    let tname = c_type_name(prefix, &packet.name);
    let cursor_fn = c_func_name(prefix, &packet.name, "parse_cursor");
    let public_fn = c_func_name(prefix, &packet.name, "parse");
    let resolved_fields = resolve_enum_fields(&packet.fields, enums);

    let has_cksum = packet.checksum_plan.is_some();
    let needs_offset = has_cksum
        && packet
            .checksum_plan
            .as_ref()
            .is_some_and(|p| p.input_model == ChecksumInputModel::RecomputeWithSkippedField);

    // Static cursor-based parse
    if needs_offset {
        out.push_str(&format!(
            "static wirespec_result_t {cursor_fn}(wirespec_cursor_t *cur, {tname} *out, size_t *_cksum_offset_out) {{\n"
        ));
        out.push_str("    wirespec_result_t r;\n");
        out.push_str("    size_t _cksum_offset = 0;\n");
    } else {
        out.push_str(&format!(
            "static wirespec_result_t {cursor_fn}(wirespec_cursor_t *cur, {tname} *out) {{\n"
        ));
        out.push_str("    wirespec_result_t r;\n");
    }

    // Emit parse items, tracking checksum field offset
    if needs_offset {
        let cksum_plan = packet.checksum_plan.as_ref().unwrap();
        emit_parse_items_with_cksum(
            out,
            &resolved_fields,
            &packet.items,
            prefix,
            "    ",
            "out->",
            &cksum_plan.field_name,
        );
        out.push_str("    *_cksum_offset_out = _cksum_offset;\n");
    } else {
        parse_emit::emit_parse_items(
            out,
            &resolved_fields,
            &packet.items,
            prefix,
            "    ",
            "out->",
        );
    }

    out.push_str("    (void)r;\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");

    // Public parse function
    out.push_str(&format!(
        "wirespec_result_t {public_fn}(const uint8_t *buf, size_t len, {tname} *out, size_t *consumed) {{\n"
    ));
    out.push_str("    wirespec_cursor_t cur;\n");
    out.push_str("    wirespec_cursor_init(&cur, buf, len);\n");

    if needs_offset {
        out.push_str("    size_t _cksum_offset = 0;\n");
        out.push_str(&format!(
            "    wirespec_result_t r = {cursor_fn}(&cur, out, &_cksum_offset);\n"
        ));
    } else {
        out.push_str(&format!(
            "    wirespec_result_t r = {cursor_fn}(&cur, out);\n"
        ));
    }

    out.push_str("    if (r != WIRESPEC_OK) return r;\n");
    out.push_str("    *consumed = wirespec_cursor_consumed(&cur);\n");

    // Checksum verify after parse
    if let Some(ref plan) = packet.checksum_plan {
        emit_checksum_verify(out, plan, "    ");
    }

    out.push_str("    (void)r;\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");
}

fn emit_packet_serialize(
    out: &mut String,
    packet: &CodecPacket,
    prefix: &str,
    enums: &[SemanticEnum],
) {
    let tname = c_type_name(prefix, &packet.name);
    let fn_name = c_func_name(prefix, &packet.name, "serialize");
    let resolved_fields = resolve_enum_fields(&packet.fields, enums);

    out.push_str(&format!(
        "wirespec_result_t {fn_name}(const {tname} *val, uint8_t *buf, size_t cap, size_t *written) {{\n"
    ));
    out.push_str("    wirespec_result_t r;\n");
    out.push_str("    size_t pos = 0;\n");

    let has_cksum = packet.checksum_plan.is_some();
    if has_cksum {
        out.push_str("    size_t _cksum_offset = 0;\n");
    }

    if has_cksum {
        let cksum_plan = packet.checksum_plan.as_ref().unwrap();
        emit_serialize_items_with_cksum(
            out,
            &resolved_fields,
            &packet.items,
            prefix,
            "    ",
            &cksum_plan.field_name,
        );
    } else {
        serialize_emit::emit_serialize_items(out, &resolved_fields, &packet.items, prefix, "    ");
    }

    // Checksum compute after all fields written
    if let Some(ref plan) = packet.checksum_plan {
        emit_checksum_compute(out, plan, "    ");
    }

    out.push_str("    *written = pos;\n");
    out.push_str("    (void)r;\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");
}

fn emit_packet_serialized_len(
    out: &mut String,
    packet: &CodecPacket,
    prefix: &str,
    enums: &[SemanticEnum],
) {
    let tname = c_type_name(prefix, &packet.name);
    let fn_name = c_func_name(prefix, &packet.name, "serialized_len");
    let resolved_fields = resolve_enum_fields(&packet.fields, enums);

    out.push_str(&format!("size_t {fn_name}(const {tname} *val) {{\n"));
    out.push_str("    (void)val;\n");
    out.push_str("    size_t len = 0;\n");

    serialize_emit::emit_serialized_len_items(
        out,
        &resolved_fields,
        &packet.items,
        prefix,
        "    ",
        "val->",
    );

    out.push_str("    return len;\n");
    out.push_str("}\n\n");
}

// ── Frame ──

fn resolve_frame(frame: &CodecFrame, enums: &[SemanticEnum]) -> CodecFrame {
    let mut resolved = frame.clone();
    for variant in &mut resolved.variants {
        variant.fields = resolve_enum_fields(&variant.fields, enums);
    }
    resolved
}

fn resolve_capsule(capsule: &CodecCapsule, enums: &[SemanticEnum]) -> CodecCapsule {
    let mut resolved = capsule.clone();
    resolved.header_fields = resolve_enum_fields(&resolved.header_fields, enums);
    for variant in &mut resolved.variants {
        variant.fields = resolve_enum_fields(&variant.fields, enums);
    }
    resolved
}

fn emit_frame_parse(out: &mut String, frame: &CodecFrame, prefix: &str, _enums: &[SemanticEnum]) {
    let tname = c_type_name(prefix, &frame.name);
    let cursor_fn = c_func_name(prefix, &frame.name, "parse_cursor");
    let public_fn = c_func_name(prefix, &frame.name, "parse");

    // Static cursor-based parse
    out.push_str(&format!(
        "static wirespec_result_t {cursor_fn}(wirespec_cursor_t *cur, {tname} *out) {{\n"
    ));
    out.push_str("    wirespec_result_t r;\n");

    parse_emit::emit_frame_parse_body(out, frame, prefix, "    ");

    out.push_str("    (void)r;\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");

    // Public parse function
    out.push_str(&format!(
        "wirespec_result_t {public_fn}(const uint8_t *buf, size_t len, {tname} *out, size_t *consumed) {{\n"
    ));
    out.push_str("    wirespec_cursor_t cur;\n");
    out.push_str("    wirespec_cursor_init(&cur, buf, len);\n");
    out.push_str(&format!(
        "    wirespec_result_t r = {cursor_fn}(&cur, out);\n"
    ));
    out.push_str("    if (r != WIRESPEC_OK) return r;\n");
    out.push_str("    *consumed = wirespec_cursor_consumed(&cur);\n");
    out.push_str("    (void)r;\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");
}

fn emit_frame_serialize(
    out: &mut String,
    frame: &CodecFrame,
    prefix: &str,
    _enums: &[SemanticEnum],
) {
    let tname = c_type_name(prefix, &frame.name);
    let fn_name = c_func_name(prefix, &frame.name, "serialize");

    out.push_str(&format!(
        "wirespec_result_t {fn_name}(const {tname} *val, uint8_t *buf, size_t cap, size_t *written) {{\n"
    ));
    out.push_str("    wirespec_result_t r;\n");
    out.push_str("    size_t pos = 0;\n");

    serialize_emit::emit_frame_serialize_body(out, frame, prefix, "    ");

    out.push_str("    *written = pos;\n");
    out.push_str("    (void)r;\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");
}

fn emit_frame_serialized_len(
    out: &mut String,
    frame: &CodecFrame,
    prefix: &str,
    _enums: &[SemanticEnum],
) {
    let tname = c_type_name(prefix, &frame.name);
    let fn_name = c_func_name(prefix, &frame.name, "serialized_len");
    let tag_type = c_frame_tag_type(prefix, &frame.name);

    out.push_str(&format!("size_t {fn_name}(const {tname} *val) {{\n"));
    out.push_str("    (void)val;\n");
    out.push_str("    size_t len = 0;\n");

    // Tag length
    match &frame.tag.wire_type {
        WireType::VarInt | WireType::ContVarInt => {
            if let Some(ref ref_name) = frame.tag.ref_type_name {
                let wire_size_fn = c_func_name(prefix, ref_name, "wire_size");
                out.push_str(&format!("    len += {wire_size_fn}(val->frame_type);\n"));
            }
        }
        _ => {
            if let Some(w) = crate::type_map::wire_type_byte_width(&frame.tag.wire_type) {
                out.push_str(&format!("    len += {w};\n"));
            }
        }
    }

    // Switch on tag for variant-specific lengths
    out.push_str("    switch (val->tag) {\n");
    for variant in &frame.variants {
        let tag_val = c_frame_tag_value(prefix, &frame.name, &variant.name);
        let vname = to_snake_case(&variant.name);
        out.push_str(&format!("    case {tag_val}:\n"));
        let val_prefix = format!("val->value.{vname}.");
        serialize_emit::emit_serialized_len_items(
            out,
            &variant.fields,
            &variant.items,
            prefix,
            "        ",
            &val_prefix,
        );
        out.push_str("        break;\n");
    }
    // Need a default case to avoid warnings
    let _ = tag_type;
    out.push_str("    default: break;\n");
    out.push_str("    }\n");

    out.push_str("    return len;\n");
    out.push_str("}\n\n");
}

// ── Capsule ──

fn emit_capsule_parse(
    out: &mut String,
    capsule: &CodecCapsule,
    prefix: &str,
    _enums: &[SemanticEnum],
) {
    let tname = c_type_name(prefix, &capsule.name);
    let cursor_fn = c_func_name(prefix, &capsule.name, "parse_cursor");
    let public_fn = c_func_name(prefix, &capsule.name, "parse");

    // Static cursor-based parse
    out.push_str(&format!(
        "static wirespec_result_t {cursor_fn}(wirespec_cursor_t *cur, {tname} *out) {{\n"
    ));
    out.push_str("    wirespec_result_t r;\n");

    parse_emit::emit_capsule_parse_body(out, capsule, prefix, "    ");

    out.push_str("    (void)r;\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");

    // Public parse function
    out.push_str(&format!(
        "wirespec_result_t {public_fn}(const uint8_t *buf, size_t len, {tname} *out, size_t *consumed) {{\n"
    ));
    out.push_str("    wirespec_cursor_t cur;\n");
    out.push_str("    wirespec_cursor_init(&cur, buf, len);\n");
    out.push_str(&format!(
        "    wirespec_result_t r = {cursor_fn}(&cur, out);\n"
    ));
    out.push_str("    if (r != WIRESPEC_OK) return r;\n");
    out.push_str("    *consumed = wirespec_cursor_consumed(&cur);\n");
    out.push_str("    (void)r;\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");
}

fn emit_capsule_serialize(
    out: &mut String,
    capsule: &CodecCapsule,
    prefix: &str,
    _enums: &[SemanticEnum],
) {
    let tname = c_type_name(prefix, &capsule.name);
    let fn_name = c_func_name(prefix, &capsule.name, "serialize");

    out.push_str(&format!(
        "wirespec_result_t {fn_name}(const {tname} *val, uint8_t *buf, size_t cap, size_t *written) {{\n"
    ));
    out.push_str("    wirespec_result_t r;\n");
    out.push_str("    size_t pos = 0;\n");

    // Serialize header fields
    serialize_emit::emit_serialize_items(
        out,
        &capsule.header_fields,
        &capsule.header_items,
        prefix,
        "    ",
    );

    // Serialize payload variants
    out.push_str("    switch (val->tag) {\n");
    for variant in &capsule.variants {
        let tag_val = c_frame_tag_value(prefix, &capsule.name, &variant.name);
        let vname = to_snake_case(&variant.name);
        out.push_str(&format!("    case {tag_val}: {{\n"));

        let val_prefix = format!("val->value.{vname}.");
        serialize_emit::emit_variant_serialize_items_public(
            out,
            &variant.fields,
            &variant.items,
            prefix,
            "        ",
            &val_prefix,
        );

        out.push_str("        break;\n");
        out.push_str("    }\n");
    }
    out.push_str("    default: break;\n");
    out.push_str("    }\n");

    out.push_str("    *written = pos;\n");
    out.push_str("    (void)r;\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");
}

fn emit_capsule_serialized_len(
    out: &mut String,
    capsule: &CodecCapsule,
    prefix: &str,
    _enums: &[SemanticEnum],
) {
    let tname = c_type_name(prefix, &capsule.name);
    let fn_name = c_func_name(prefix, &capsule.name, "serialized_len");

    out.push_str(&format!("size_t {fn_name}(const {tname} *val) {{\n"));
    out.push_str("    (void)val;\n");
    out.push_str("    size_t len = 0;\n");

    // Header field lengths
    serialize_emit::emit_serialized_len_items(
        out,
        &capsule.header_fields,
        &capsule.header_items,
        prefix,
        "    ",
        "val->",
    );

    // Payload variant lengths
    out.push_str("    switch (val->tag) {\n");
    for variant in &capsule.variants {
        let tag_val = c_frame_tag_value(prefix, &capsule.name, &variant.name);
        let vname = to_snake_case(&variant.name);
        out.push_str(&format!("    case {tag_val}:\n"));
        let val_prefix = format!("val->value.{vname}.");
        serialize_emit::emit_serialized_len_items(
            out,
            &variant.fields,
            &variant.items,
            prefix,
            "        ",
            &val_prefix,
        );
        out.push_str("        break;\n");
    }
    out.push_str("    default: break;\n");
    out.push_str("    }\n");

    out.push_str("    return len;\n");
    out.push_str("}\n\n");
}

// ── State machine dispatch ──

fn emit_sm_dispatch(out: &mut String, sm: &SemanticStateMachine, prefix: &str) {
    let sm_snake = to_snake_case(&sm.name);
    let sm_upper = sm_snake.to_uppercase();
    let prefix_upper = prefix.to_uppercase();

    let sm_type = format!("{prefix}_{sm_snake}_t");
    let event_type = format!("{prefix}_{sm_snake}_event_t");
    let dispatch_fn = format!("{prefix}_{sm_snake}_dispatch");

    out.push_str(&format!(
        "wirespec_result_t {dispatch_fn}(\n    {sm_type} *sm,\n    const {event_type} *event)\n{{\n"
    ));
    out.push_str(&format!(
        "    {prefix}_{sm_snake}_state_tag_t src_tag = sm->tag;\n\n"
    ));

    // Separate transitions into specific (non-wildcard) and wildcard
    let specific_transitions: Vec<_> = sm
        .transitions
        .iter()
        .filter(|t| t.src_state_name != "*")
        .collect();
    let wildcard_transitions: Vec<_> = sm
        .transitions
        .iter()
        .filter(|t| t.src_state_name == "*")
        .collect();

    // Group specific transitions by source state
    let mut state_groups: std::collections::BTreeMap<String, Vec<_>> =
        std::collections::BTreeMap::new();
    for t in &specific_transitions {
        state_groups
            .entry(t.src_state_name.clone())
            .or_default()
            .push(*t);
    }

    // Emit specific state transitions
    let mut first_state = true;
    for (src_state_name, transitions) in &state_groups {
        let src_upper = to_snake_case(src_state_name).to_uppercase();
        let src_snake = to_snake_case(src_state_name);
        let if_kw = if first_state { "if" } else { "} else if" };
        out.push_str(&format!(
            "    {if_kw} (src_tag == {prefix_upper}_{sm_upper}_{src_upper}) {{\n"
        ));

        let mut first_event = true;
        for t in transitions {
            let event_upper = to_snake_case(&t.event_name).to_uppercase();
            let dst_snake = to_snake_case(&t.dst_state_name);
            let dst_upper = dst_snake.to_uppercase();

            let ev_if_kw = if first_event { "if" } else { "} else if" };
            out.push_str(&format!(
                "        {ev_if_kw} (event->tag == {prefix_upper}_{sm_upper}_EVENT_{event_upper}) {{\n"
            ));

            let expr_ctx = SmExprContext {
                src_state_snake: &src_snake,
                dst_state_snake: &dst_snake,
                event_snake: &to_snake_case(&t.event_name),
                sm: Some(sm),
                prefix,
            };

            // Guard check (with special handling for All expressions)
            if let Some(ref guard) = t.guard {
                emit_guard(out, guard, &expr_ctx, "            ");
            }

            // Build destination state
            out.push_str(&format!("            {sm_type} dst;\n"));
            out.push_str(&format!(
                "            dst.tag = {prefix_upper}_{sm_upper}_{dst_upper};\n"
            ));

            // Actions: assign fields (with special handling for Fill)
            for action in &t.actions {
                emit_action(out, action, &expr_ctx, "            ");
            }

            // Delegate: child SM dispatch
            if let Some(ref delegate) = t.delegate {
                emit_delegate(
                    out,
                    delegate,
                    sm,
                    &expr_ctx,
                    prefix,
                    &src_snake,
                    "            ",
                );
            }

            out.push_str("            *sm = dst;\n");
            out.push_str("            return WIRESPEC_OK;\n");

            first_event = false;
        }
        if !first_event {
            out.push_str("        }\n");
        }

        first_state = false;
    }

    // Wildcard transitions: apply to all non-terminal states as a fallback
    if !wildcard_transitions.is_empty() {
        // If we had specific states, close the else-if chain first
        if !first_state {
            out.push_str("    }\n\n");
            out.push_str("    /* Wildcard transitions */\n");
        }

        for t in &wildcard_transitions {
            let event_upper = to_snake_case(&t.event_name).to_uppercase();
            let dst_snake = to_snake_case(&t.dst_state_name);
            let dst_upper = dst_snake.to_uppercase();

            out.push_str(&format!(
                "    if (event->tag == {prefix_upper}_{sm_upper}_EVENT_{event_upper}) {{\n"
            ));

            // Wildcard transitions apply to any non-terminal state.
            // Since the src state is dynamic, we can't dereference specific src fields
            // without a switch. For wildcards with actions, we'd need per-state handling.
            // For simple wildcards (no actions referencing src), just set the tag.
            out.push_str(&format!("        {sm_type} dst;\n"));
            out.push_str(&format!(
                "        dst.tag = {prefix_upper}_{sm_upper}_{dst_upper};\n"
            ));

            for action in &t.actions {
                // For wildcard actions, use empty src context
                let expr_ctx = SmExprContext {
                    src_state_snake: "_wildcard",
                    dst_state_snake: &dst_snake,
                    event_snake: &to_snake_case(&t.event_name),
                    sm: Some(sm),
                    prefix,
                };
                emit_action(out, action, &expr_ctx, "        ");
            }

            out.push_str("        *sm = dst;\n");
            out.push_str("        return WIRESPEC_OK;\n");
            out.push_str("    }\n");
        }
    } else if !first_state {
        // Close the final else-if brace for specific states
        out.push_str("    }\n");
    }

    out.push_str("\n    return WIRESPEC_ERR_INVALID_STATE;\n");
    out.push_str("}\n\n");
}

// ── SM helper: emit guard ──

/// Emit a guard check. If the guard is an `All` expression, emit it as a
/// statement-level block instead of an inline expression.
fn emit_guard(out: &mut String, guard: &SemanticExpr, ctx: &SmExprContext, indent: &str) {
    match guard {
        SemanticExpr::All {
            collection,
            sm_name,
            state_name,
            ..
        } => {
            let sm_snake = to_snake_case(sm_name);
            let state_snake = to_snake_case(state_name);
            let prefix_upper = ctx.prefix.to_uppercase();
            let sm_upper = sm_snake.to_uppercase();
            let state_upper = state_snake.to_uppercase();
            let tag = format!("{prefix_upper}_{sm_upper}_{state_upper}");

            match collection.as_ref() {
                SemanticExpr::Slice { base, start, end } => {
                    let base_c = crate::expr::sema_expr_to_c(base, ctx);
                    let start_c = crate::expr::sema_expr_to_c(start, ctx);
                    let end_c = crate::expr::sema_expr_to_c(end, ctx);
                    out.push_str(&format!("{indent}{{\n"));
                    out.push_str(&format!("{indent}    bool _all_ok = true;\n"));
                    out.push_str(&format!(
                        "{indent}    for (size_t _aj = (size_t)({start_c}); _aj < (size_t)({end_c}); _aj++) {{\n"
                    ));
                    out.push_str(&format!(
                        "{indent}        if ({base_c}[_aj].tag != {tag}) {{ _all_ok = false; break; }}\n"
                    ));
                    out.push_str(&format!("{indent}    }}\n"));
                    out.push_str(&format!(
                        "{indent}    if (!_all_ok) return WIRESPEC_ERR_CONSTRAINT;\n"
                    ));
                    out.push_str(&format!("{indent}}}\n"));
                }
                _ => {
                    let coll_c = crate::expr::sema_expr_to_c(collection, ctx);
                    out.push_str(&format!(
                        "{indent}/* all() guard: non-slice collection ({coll_c}) — treating as no-op */\n"
                    ));
                }
            }
        }
        _ => {
            let guard_c = crate::expr::sema_expr_to_c(guard, ctx);
            out.push_str(&format!(
                "{indent}if (!({guard_c})) return WIRESPEC_ERR_INVALID_STATE;\n"
            ));
        }
    }
}

// ── SM helper: emit action ──

/// Emit an action assignment. If the value is a `Fill` expression, emit a
/// for-loop instead of a simple assignment.
fn emit_action(out: &mut String, action: &SemanticAction, ctx: &SmExprContext, indent: &str) {
    match &action.value {
        SemanticExpr::Fill { value, count } => {
            // Fill count is validated at compile time by sema — bounds check not needed here.
            let target_c = crate::expr::sema_expr_to_c(&action.target, ctx);
            let value_c = crate::expr::sema_expr_to_c(value, ctx);
            let count_c = crate::expr::sema_expr_to_c(count, ctx);
            out.push_str(&format!(
                "{indent}/* fill: count validated at compile time by sema */\n"
            ));
            out.push_str(&format!(
                "{indent}for (size_t _fi = 0; _fi < (size_t)({count_c}); _fi++) {{\n"
            ));
            out.push_str(&format!("{indent}    {target_c}[_fi] = {value_c};\n"));
            out.push_str(&format!("{indent}}}\n"));
        }
        _ => {
            let target_c = crate::expr::sema_expr_to_c(&action.target, ctx);
            let value_c = crate::expr::sema_expr_to_c(&action.value, ctx);
            let op = &action.op;
            // Check if the target is an array field — C can't assign arrays
            // directly, so use memcpy instead.
            let is_array_field =
                if let SemanticExpr::TransitionPeerRef { reference } = &action.target {
                    if let Some(field_name) = reference.path.first() {
                        ctx.sm.is_some_and(|sm| {
                            sm.states.iter().any(|s| {
                                s.fields.iter().any(|f| {
                                    &f.name == field_name
                                        && matches!(
                                            &f.ty,
                                            wirespec_sema::types::SemanticType::Array { .. }
                                        )
                                })
                            })
                        })
                    } else {
                        false
                    }
                } else {
                    false
                };
            if is_array_field {
                out.push_str(&format!(
                    "{indent}memcpy({target_c}, {value_c}, sizeof({target_c}));\n"
                ));
            } else {
                out.push_str(&format!("{indent}{target_c} {op} {value_c};\n"));
            }
        }
    }
}

// ── SM helper: emit delegate (child dispatch) ──

/// Extract the child field name from a delegate target expression.
/// Delegate target is typically `TransitionPeerRef { peer: Src, path: ["child_field"] }`
/// or a `Subscript` over such a ref (e.g., `src.paths[id]`).
fn extract_delegate_child_field(target: &SemanticExpr) -> Option<String> {
    match target {
        SemanticExpr::TransitionPeerRef { reference } => {
            if reference.peer == TransitionPeerKind::Src && !reference.path.is_empty() {
                Some(reference.path[0].clone())
            } else {
                None
            }
        }
        SemanticExpr::Subscript { base, .. } => extract_delegate_child_field(base),
        _ => None,
    }
}

/// Emit delegate child dispatch code. This auto-copies dst from sm,
/// dispatches to the child SM, and re-dispatches the parent if the child's
/// state changed.
fn emit_delegate(
    out: &mut String,
    delegate: &SemanticDelegate,
    sm: &SemanticStateMachine,
    _ctx: &SmExprContext,
    prefix: &str,
    src_state_snake: &str,
    indent: &str,
) {
    // Extract the child field name from TransitionPeerRef or Subscript(TransitionPeerRef, index).
    let field_name = extract_delegate_child_field(&delegate.target);
    let is_indexed = matches!(delegate.target, SemanticExpr::Subscript { .. });

    // Look up the child SM type from the source state's field definitions
    let child_sm_name = field_name.as_ref().and_then(|fname| {
        sm.states
            .iter()
            .find(|s| to_snake_case(&s.name) == src_state_snake)
            .and_then(|state| state.fields.iter().find(|f| &f.name == fname))
            .and_then(|f| f.child_sm_name.clone())
    });

    if let (Some(field_name), Some(child_sm)) = (&field_name, &child_sm_name) {
        let child_snake = to_snake_case(child_sm);
        let _child_type = format!("{prefix}_{child_snake}_t");
        let child_event_type = format!("{prefix}_{child_snake}_event_t");
        let child_dispatch_fn = format!("{prefix}_{child_snake}_dispatch");
        let child_tag_type = format!("{prefix}_{child_snake}_state_tag_t");
        let field_snake = to_snake_case(field_name);

        let prefix_upper = prefix.to_uppercase();
        let sm_snake = to_snake_case(&sm.name);
        let sm_upper = sm_snake.to_uppercase();

        out.push_str(&format!(
            "{indent}/* delegate: child dispatch to {child_sm} */\n"
        ));
        out.push_str(&format!("{indent}dst = *sm;\n"));

        // Build child event: the delegate event_name is the parent event
        // parameter that carries the child event tag value (a u8), so cast it
        // to the child event tag type.
        let delegate_param_snake = to_snake_case(&delegate.event_name);
        let child_event_tag_type = format!("{prefix}_{child_snake}_event_tag_t");
        let parent_event_snake = to_snake_case(_ctx.event_snake);
        out.push_str(&format!("{indent}{child_event_type} _child_ev;\n"));
        out.push_str(&format!(
            "{indent}_child_ev.tag = ({child_event_tag_type})event->{parent_event_snake}.{delegate_param_snake};\n"
        ));

        if is_indexed {
            // Indexed delegate: src.field[index] <- ev
            let index_expr = if let SemanticExpr::Subscript { index, .. } = &delegate.target {
                crate::expr::sema_expr_to_c(index, _ctx)
            } else {
                "0".to_string()
            };

            // Extract the array size from the field's type for bounds checking.
            let array_size: Option<i64> = sm
                .states
                .iter()
                .find(|s| to_snake_case(&s.name) == src_state_snake)
                .and_then(|state| state.fields.iter().find(|f| &f.name == field_name))
                .and_then(|f| {
                    if let wirespec_sema::types::SemanticType::Array {
                        count_expr: Some(count),
                        ..
                    } = &f.ty
                    {
                        if let wirespec_sema::expr::SemanticExpr::Literal {
                            value: wirespec_sema::expr::SemanticLiteral::Int(n),
                        } = count.as_ref()
                        {
                            Some(*n)
                        } else {
                            None
                        }
                    } else {
                        None
                    }
                });

            let child_ref = format!("dst.{src_state_snake}.{field_snake}[_idx]");

            out.push_str(&format!("{indent}{{\n"));
            out.push_str(&format!(
                "{indent}    size_t _idx = (size_t)({index_expr});\n"
            ));

            // Emit bounds check before indexing into the array.
            if let Some(size) = array_size {
                out.push_str(&format!(
                    "{indent}    if (_idx >= {size}) return WIRESPEC_ERR_BOUNDS;\n"
                ));
            }
            out.push_str(&format!(
                "{indent}    {child_tag_type} _old_tag = {child_ref}.tag;\n"
            ));
            out.push_str(&format!(
                "{indent}    wirespec_result_t _rc = {child_dispatch_fn}(\n{indent}        &{child_ref}, &_child_ev);\n"
            ));
            out.push_str(&format!(
                "{indent}    if (_rc != WIRESPEC_OK) return _rc;\n"
            ));

            if !sm.has_child_state_changed {
                out.push_str(&format!("{indent}    (void)_old_tag;\n"));
            }
            if sm.has_child_state_changed {
                out.push_str(&format!(
                    "{indent}    if ({child_ref}.tag != _old_tag) {{\n"
                ));
                let event_type = format!("{prefix}_{sm_snake}_event_t");
                out.push_str(&format!("{indent}        {event_type} _csc_ev;\n"));
                out.push_str(&format!(
                    "{indent}        _csc_ev.tag = {prefix_upper}_{sm_upper}_EVENT_CHILD_STATE_CHANGED;\n"
                ));
                out.push_str(&format!("{indent}        *sm = dst;\n"));
                out.push_str(&format!(
                    "{indent}        wirespec_result_t _csc_rc = {prefix}_{sm_snake}_dispatch(sm, &_csc_ev);\n"
                ));
                out.push_str(&format!(
                    "{indent}        if (_csc_rc != WIRESPEC_OK && _csc_rc != WIRESPEC_ERR_INVALID_STATE)\n"
                ));
                out.push_str(&format!("{indent}            return _csc_rc;\n"));
                out.push_str(&format!("{indent}        return WIRESPEC_OK;\n"));
                out.push_str(&format!("{indent}    }}\n"));
            }
            out.push_str(&format!("{indent}}}\n"));
        } else {
            // Simple delegate: src.field <- ev
            out.push_str(&format!(
                "{indent}{child_tag_type} _old_tag = dst.{src_state_snake}.{field_snake}.tag;\n"
            ));

            // Dispatch to child
            out.push_str(&format!(
                "{indent}wirespec_result_t _rc = {child_dispatch_fn}(\n{indent}    &dst.{src_state_snake}.{field_snake}, &_child_ev);\n"
            ));
            out.push_str(&format!("{indent}if (_rc != WIRESPEC_OK) return _rc;\n"));

            // Re-dispatch if child tag changed and parent handles child_state_changed
            if !sm.has_child_state_changed {
                out.push_str(&format!("{indent}(void)_old_tag;\n"));
            }
            if sm.has_child_state_changed {
                out.push_str(&format!(
                    "{indent}if (dst.{src_state_snake}.{field_snake}.tag != _old_tag) {{\n"
                ));
                let event_type = format!("{prefix}_{sm_snake}_event_t");
                out.push_str(&format!("{indent}    {event_type} _csc_ev;\n"));
                out.push_str(&format!(
                    "{indent}    _csc_ev.tag = {prefix_upper}_{sm_upper}_EVENT_CHILD_STATE_CHANGED;\n"
                ));
                out.push_str(&format!("{indent}    *sm = dst;\n"));
                out.push_str(&format!(
                    "{indent}    wirespec_result_t _csc_rc = {prefix}_{sm_snake}_dispatch(sm, &_csc_ev);\n"
                ));
                out.push_str(&format!(
                    "{indent}    if (_csc_rc != WIRESPEC_OK && _csc_rc != WIRESPEC_ERR_INVALID_STATE)\n"
                ));
                out.push_str(&format!("{indent}        return _csc_rc;\n"));
                out.push_str(&format!("{indent}    return WIRESPEC_OK;\n"));
                out.push_str(&format!("{indent}}}\n"));
            }
        }
    } else {
        // This branch should be unreachable: sema guarantees child_sm_name is resolved
        // for all delegate targets. If we reach here, it's a codegen bug.
        unreachable!("child SM type not resolved — sema should guarantee this");
    }
}

// ── VarInt ──

fn emit_varint_source(out: &mut String, vi: &SemanticVarInt, prefix: &str) {
    match vi.encoding {
        VarIntEncoding::PrefixMatch => emit_varint_prefix_match(out, vi, prefix),
        VarIntEncoding::ContinuationBit => emit_varint_continuation_bit(out, vi, prefix),
    }
}

fn emit_varint_prefix_match(out: &mut String, vi: &SemanticVarInt, prefix: &str) {
    let snake = to_snake_case(&vi.name);
    let tname = format!("{prefix}_{snake}_t");
    let parse_fn = format!("{prefix}_{snake}_parse");
    let serialize_fn = format!("{prefix}_{snake}_serialize");
    let wire_size_fn = format!("{prefix}_{snake}_wire_size");
    let prefix_bits = vi.prefix_bits.unwrap_or(2);

    // ── Parse ──
    out.push_str(&format!(
        "wirespec_result_t\n{parse_fn}(const uint8_t *buf, size_t len,\n    {tname} *out, size_t *consumed)\n{{\n"
    ));
    out.push_str("    if (len < 1) return WIRESPEC_ERR_SHORT_BUFFER;\n\n");
    out.push_str(&format!(
        "    uint8_t prefix = buf[0] >> {};\n",
        8 - prefix_bits
    ));
    out.push_str("    switch (prefix) {\n");

    for branch in &vi.branches {
        out.push_str(&format!("    case {}: {{\n", branch.prefix_value));
        let total = branch.total_bytes as usize;
        if total > 1 {
            out.push_str(&format!(
                "        if (len < {total}) return WIRESPEC_ERR_SHORT_BUFFER;\n"
            ));
        }
        // First byte: mask off prefix bits to get value bits
        let first_mask = (1u64 << (8 - prefix_bits)) - 1;
        if total == 1 {
            out.push_str(&format!("        *out = buf[0] & 0x{first_mask:X};\n"));
        } else {
            // Multi-byte: assemble value from all bytes
            out.push_str(&format!(
                "        *out = ((uint64_t)(buf[0] & 0x{first_mask:X}) << {})\n",
                (total - 1) * 8
            ));
            for i in 1..total {
                let shift = (total - 1 - i) * 8;
                if i == total - 1 {
                    out.push_str(&format!("             | (uint64_t)buf[{i}];\n"));
                } else {
                    out.push_str(&format!("             | ((uint64_t)buf[{i}] << {shift})\n"));
                }
            }
        }
        out.push_str(&format!("        *consumed = {total};\n"));

        // Strict noncanonical check
        if vi.strict && branch.prefix_value > 0 {
            let prev_branch = &vi.branches[(branch.prefix_value as usize) - 1];
            out.push_str(&format!(
                "        if (*out <= {}ULL) return WIRESPEC_ERR_NONCANONICAL;\n",
                prev_branch.max_value
            ));
        }

        out.push_str("        break;\n");
        out.push_str("    }\n");
    }

    out.push_str("    default:\n");
    out.push_str("        return WIRESPEC_ERR_INVALID_TAG;\n");
    out.push_str("    }\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");

    // ── Serialize ──
    out.push_str(&format!(
        "wirespec_result_t\n{serialize_fn}({tname} val,\n    uint8_t *buf, size_t cap, size_t *written)\n{{\n"
    ));

    for (i, branch) in vi.branches.iter().enumerate() {
        let total = branch.total_bytes as usize;
        let prefix_marker = branch.prefix_value << (8 - prefix_bits);
        if i == 0 {
            out.push_str(&format!("    if (val <= {}ULL) {{\n", branch.max_value));
        } else {
            out.push_str(&format!(
                "    }} else if (val <= {}ULL) {{\n",
                branch.max_value
            ));
        }

        out.push_str(&format!(
            "        if (cap < {total}) return WIRESPEC_ERR_SHORT_BUFFER;\n"
        ));

        if total == 1 {
            if prefix_marker == 0 {
                out.push_str("        buf[0] = (uint8_t)val;\n");
            } else {
                out.push_str(&format!(
                    "        buf[0] = 0x{prefix_marker:02X} | (uint8_t)val;\n"
                ));
            }
        } else {
            let top_shift = (total - 1) * 8;
            if prefix_marker == 0 {
                out.push_str(&format!(
                    "        buf[0] = (uint8_t)(val >> {top_shift});\n"
                ));
            } else {
                out.push_str(&format!(
                    "        buf[0] = 0x{prefix_marker:02X} | (uint8_t)(val >> {top_shift});\n"
                ));
            }
            for j in 1..total {
                let shift = (total - 1 - j) * 8;
                if shift == 0 {
                    out.push_str(&format!("        buf[{j}] = (uint8_t)val;\n"));
                } else {
                    out.push_str(&format!("        buf[{j}] = (uint8_t)(val >> {shift});\n"));
                }
            }
        }

        out.push_str(&format!("        *written = {total};\n"));
    }

    out.push_str("    } else {\n");
    out.push_str("        return WIRESPEC_ERR_OVERFLOW;\n");
    out.push_str("    }\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");

    // ── Wire size ──
    out.push_str(&format!("size_t\n{wire_size_fn}({tname} val)\n{{\n"));

    for (i, branch) in vi.branches.iter().enumerate() {
        let total = branch.total_bytes;
        if i < vi.branches.len() - 1 {
            out.push_str(&format!(
                "    if (val <= {}ULL) return {total};\n",
                branch.max_value
            ));
        } else {
            out.push_str(&format!("    return {total};\n"));
        }
    }

    out.push_str("}\n\n");

    // ── parse_cursor (static inline helper) ──
    let parse_cursor_fn = format!("{prefix}_{snake}_parse_cursor");
    out.push_str(&format!(
        "static inline wirespec_result_t {parse_cursor_fn}(\n    wirespec_cursor_t *cur, {tname} *out)\n{{\n"
    ));
    out.push_str("    size_t consumed = 0;\n");
    out.push_str(&format!(
        "    wirespec_result_t r = {parse_fn}(\n        cur->base + cur->pos, wirespec_cursor_remaining(cur),\n        out, &consumed);\n"
    ));
    out.push_str("    if (r != WIRESPEC_OK) return r;\n");
    out.push_str("    cur->pos += consumed;\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");
}

fn emit_varint_continuation_bit(out: &mut String, vi: &SemanticVarInt, prefix: &str) {
    let snake = to_snake_case(&vi.name);
    let tname = format!("{prefix}_{snake}_t");
    let parse_fn = format!("{prefix}_{snake}_parse");
    let serialize_fn = format!("{prefix}_{snake}_serialize");
    let wire_size_fn = format!("{prefix}_{snake}_wire_size");

    let value_bits = vi.value_bits_per_byte.unwrap_or(7) as u32;
    let max_bytes = vi.max_bytes as u32;
    let cont_mask: u8 = 0x80; // MSB continuation bit
    let value_mask: u8 = (1u16 << value_bits) as u8 - 1;

    // ── Parse ──
    out.push_str(&format!(
        "wirespec_result_t\n{parse_fn}(const uint8_t *buf, size_t len,\n    {tname} *out, size_t *consumed)\n{{\n"
    ));
    out.push_str("    uint64_t result = 0;\n");
    out.push_str(&format!(
        "    for (size_t i = 0; i < {max_bytes}; i++) {{\n"
    ));
    out.push_str("        if (i >= len) return WIRESPEC_ERR_SHORT_BUFFER;\n");
    out.push_str("        uint8_t byte = buf[i];\n");

    match vi.byte_order {
        wirespec_sema::types::Endianness::Little => {
            out.push_str(&format!(
                "        result |= (uint64_t)(byte & 0x{value_mask:02X}) << (i * {value_bits});\n"
            ));
        }
        wirespec_sema::types::Endianness::Big => {
            out.push_str(&format!(
                "        result = (result << {value_bits}) | (uint64_t)(byte & 0x{value_mask:02X});\n"
            ));
        }
    }

    out.push_str(&format!(
        "        if ((byte & 0x{cont_mask:02X}) == 0) {{\n"
    ));
    out.push_str("            *out = result;\n");
    out.push_str("            *consumed = i + 1;\n");
    out.push_str("            return WIRESPEC_OK;\n");
    out.push_str("        }\n");
    out.push_str("    }\n");
    out.push_str("    return WIRESPEC_ERR_OVERFLOW;\n");
    out.push_str("}\n\n");

    // ── Serialize ──
    out.push_str(&format!(
        "wirespec_result_t\n{serialize_fn}({tname} val,\n    uint8_t *buf, size_t cap, size_t *written)\n{{\n"
    ));

    match vi.byte_order {
        wirespec_sema::types::Endianness::Little => {
            out.push_str("    size_t i = 0;\n");
            out.push_str("    do {\n");
            out.push_str("        if (i >= cap) return WIRESPEC_ERR_SHORT_BUFFER;\n");
            out.push_str(&format!(
                "        if (i >= {max_bytes}) return WIRESPEC_ERR_OVERFLOW;\n"
            ));
            out.push_str(&format!(
                "        buf[i] = (uint8_t)(val & 0x{value_mask:02X});\n"
            ));
            out.push_str(&format!("        val >>= {value_bits};\n"));
            out.push_str("        if (val > 0) {\n");
            out.push_str(&format!("            buf[i] |= 0x{cont_mask:02X};\n"));
            out.push_str("        }\n");
            out.push_str("        i++;\n");
            out.push_str("    } while (val > 0);\n");
            out.push_str("    *written = i;\n");
        }
        wirespec_sema::types::Endianness::Big => {
            out.push_str(&format!("    uint8_t tmp[{max_bytes}];\n"));
            out.push_str("    size_t n = 0;\n");
            out.push_str("    do {\n");
            out.push_str(&format!(
                "        if (n >= {max_bytes}) return WIRESPEC_ERR_OVERFLOW;\n"
            ));
            out.push_str(&format!(
                "        tmp[n++] = (uint8_t)(val & 0x{value_mask:02X});\n"
            ));
            out.push_str(&format!("        val >>= {value_bits};\n"));
            out.push_str("    } while (val > 0);\n");
            out.push_str("    if (n > cap) return WIRESPEC_ERR_SHORT_BUFFER;\n");
            out.push_str("    for (size_t i = 0; i < n; i++) {\n");
            out.push_str("        buf[i] = tmp[n - 1 - i];\n");
            out.push_str("        if (i < n - 1) {\n");
            out.push_str(&format!("            buf[i] |= 0x{cont_mask:02X};\n"));
            out.push_str("        }\n");
            out.push_str("    }\n");
            out.push_str("    *written = n;\n");
        }
    }

    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");

    // ── Wire size ──
    out.push_str(&format!("size_t\n{wire_size_fn}({tname} val)\n{{\n"));
    out.push_str("    size_t n = 1;\n");
    out.push_str(&format!(
        "    while (val >> {value_bits}) {{ n++; val >>= {value_bits}; }}\n"
    ));
    out.push_str(&format!("    if (n > {max_bytes}) n = {max_bytes};\n"));
    out.push_str("    return n;\n");
    out.push_str("}\n\n");

    // ── parse_cursor (static inline helper) ──
    let parse_cursor_fn = format!("{prefix}_{snake}_parse_cursor");
    out.push_str(&format!(
        "static inline wirespec_result_t {parse_cursor_fn}(\n    wirespec_cursor_t *cur, {tname} *out)\n{{\n"
    ));
    out.push_str("    size_t consumed = 0;\n");
    out.push_str(&format!(
        "    wirespec_result_t r = {parse_fn}(\n        cur->base + cur->pos, wirespec_cursor_remaining(cur),\n        out, &consumed);\n"
    ));
    out.push_str("    if (r != WIRESPEC_OK) return r;\n");
    out.push_str("    cur->pos += consumed;\n");
    out.push_str("    return WIRESPEC_OK;\n");
    out.push_str("}\n\n");
}

// ── Checksum helpers ──

/// Emit parse items with checksum field offset tracking.
fn emit_parse_items_with_cksum(
    out: &mut String,
    fields: &[CodecField],
    items: &[CodecItem],
    prefix: &str,
    indent: &str,
    struct_prefix: &str,
    cksum_field_name: &str,
) {
    for item in items {
        if let CodecItem::Field { field_id } = item
            && let Some(f) = fields.iter().find(|f| &f.field_id == field_id)
            && f.name == cksum_field_name
        {
            out.push_str(&format!(
                "{indent}_cksum_offset = wirespec_cursor_consumed(cur);\n"
            ));
        }
        let single_items = [item.clone()];
        parse_emit::emit_parse_items(out, fields, &single_items, prefix, indent, struct_prefix);
    }
}

/// Emit serialize items with checksum field offset tracking.
fn emit_serialize_items_with_cksum(
    out: &mut String,
    fields: &[CodecField],
    items: &[CodecItem],
    prefix: &str,
    indent: &str,
    cksum_field_name: &str,
) {
    for item in items {
        if let CodecItem::Field { field_id } = item
            && let Some(f) = fields.iter().find(|f| &f.field_id == field_id)
            && f.name == cksum_field_name
        {
            out.push_str(&format!("{indent}_cksum_offset = pos;\n"));
        }
        let single_items = [item.clone()];
        serialize_emit::emit_serialize_items(out, fields, &single_items, prefix, indent);
    }
}

/// Emit checksum verification in the parse wrapper.
fn emit_checksum_verify(out: &mut String, plan: &ChecksumPlan, indent: &str) {
    use wirespec_sema::checksum_catalog;

    let algo = &plan.algorithm_id;
    let spec = checksum_catalog::lookup(algo);

    match spec.map(|s| s.verify_mode) {
        Some(checksum_catalog::ChecksumVerifyMode::ZeroSum) => {
            let width = plan.field_width_bytes;
            let ctype = if width == 2 { "uint16_t" } else { "uint32_t" };
            out.push_str(&format!(
                "{indent}{{\n{indent}    {ctype} _cksum = wirespec_{algo}_checksum(buf, *consumed);\n"
            ));
            out.push_str(&format!(
                "{indent}    if (_cksum != 0) return WIRESPEC_ERR_CHECKSUM;\n"
            ));
            out.push_str(&format!("{indent}}}\n"));
        }
        Some(checksum_catalog::ChecksumVerifyMode::RecomputeCompare) => {
            let width = plan.field_width_bytes;
            let ctype = if width == 2 { "uint16_t" } else { "uint32_t" };
            out.push_str(&format!(
                "{indent}{{\n{indent}    {ctype} _computed = wirespec_{algo}_verify(\n"
            ));
            out.push_str(&format!(
                "{indent}        buf, *consumed, _cksum_offset, {width});\n"
            ));
            out.push_str(&format!(
                "{indent}    if (out->{} != _computed)\n",
                plan.field_name
            ));
            out.push_str(&format!("{indent}        return WIRESPEC_ERR_CHECKSUM;\n"));
            out.push_str(&format!("{indent}}}\n"));
        }
        None => {
            unreachable!("unknown checksum algorithm: {algo}");
        }
    }
}

/// Emit checksum compute+patch in the serialize function.
fn emit_checksum_compute(out: &mut String, plan: &ChecksumPlan, indent: &str) {
    use wirespec_sema::checksum_catalog;

    let algo = &plan.algorithm_id;
    let spec = checksum_catalog::lookup(algo);

    out.push_str(&format!(
        "{indent}/* @checksum({algo}): auto-compute and patch */\n"
    ));

    match spec.map(|s| s.verify_mode) {
        Some(checksum_catalog::ChecksumVerifyMode::ZeroSum) => {
            out.push_str(&format!(
                "{indent}wirespec_{algo}_checksum_compute(buf, pos, _cksum_offset);\n"
            ));
        }
        Some(checksum_catalog::ChecksumVerifyMode::RecomputeCompare) => {
            let width = plan.field_width_bytes;
            let ctype = if width == 2 { "uint16_t" } else { "uint32_t" };
            out.push_str(&format!("{indent}{{\n"));
            out.push_str(&format!(
                "{indent}    {ctype} _val = wirespec_{algo}_compute(buf, pos, _cksum_offset);\n"
            ));
            // Write bytes big-endian from MSB to LSB
            for i in 0..width {
                let shift = (width - 1 - i) * 8;
                out.push_str(&format!(
                    "{indent}    buf[_cksum_offset + {i}] = (uint8_t)((_val >> {shift}) & 0xFF);\n"
                ));
            }
            out.push_str(&format!("{indent}}}\n"));
        }
        None => {
            unreachable!("unknown checksum algorithm: {algo}");
        }
    }
}

/// Generate a libFuzzer fuzz harness that tests the round-trip property:
/// parse -> serialize -> re-parse -> re-serialize must be stable.
///
/// The harness targets the first frame, capsule, or packet in the module
/// (in that priority order). Returns `None` if the module has no parseable types.
pub fn emit_fuzz_source(module: &CodecModule, prefix: &str) -> Option<String> {
    // Select fuzz target: first frame > first capsule > first packet
    let type_snake = if let Some(f) = module.frames.first() {
        to_snake_case(&f.name)
    } else if let Some(c) = module.capsules.first() {
        to_snake_case(&c.name)
    } else if let Some(p) = module.packets.first() {
        to_snake_case(&p.name)
    } else {
        return None;
    };

    let tname = format!("{prefix}_{type_snake}_t");
    let parse_fn = format!("{prefix}_{type_snake}_parse");
    let serialize_fn = format!("{prefix}_{type_snake}_serialize");

    Some(format!(
        r#"/* Auto-generated fuzz harness -- DO NOT EDIT */
#include "{prefix}.h"
#include <stdint.h>
#include <stddef.h>
#include <string.h>

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {{
    /* Phase 1: Parse fuzz input */
    {tname} obj;
    size_t consumed;
    wirespec_result_t r = {parse_fn}(data, size, &obj, &consumed);
    if (r != WIRESPEC_OK) return 0;

    /* Phase 2: Serialize back */
    uint8_t buf[65536];
    size_t written;
    r = {serialize_fn}(&obj, buf, sizeof(buf), &written);
    if (r != WIRESPEC_OK) return 0;

    /* Phase 3: Re-parse our own output -- must succeed */
    {tname} obj2;
    size_t consumed2;
    r = {parse_fn}(buf, written, &obj2, &consumed2);
    if (r != WIRESPEC_OK) __builtin_trap();
    if (consumed2 != written) __builtin_trap();

    /* Phase 4: Re-serialize -- must be identical */
    uint8_t buf2[65536];
    size_t written2;
    r = {serialize_fn}(&obj2, buf2, sizeof(buf2), &written2);
    if (r != WIRESPEC_OK) __builtin_trap();
    if (written != written2 || memcmp(buf, buf2, written) != 0) __builtin_trap();

    return 0;
}}
"#
    ))
}