veryl-simulator 0.20.0

A modern hardware description language
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
use crate::HashMap;
use crate::HashSet;
#[cfg(not(target_family = "wasm"))]
use crate::cranelift;
use crate::ir::context::{Context, Conv, ScopeContext};
#[cfg(not(target_family = "wasm"))]
use crate::ir::context::{JitCacheEntry, JitCachedFunc};
use crate::ir::expression::{ExpressionContext, build_dynamic_bit_select};
#[cfg(not(target_family = "wasm"))]
use crate::ir::statement::CompiledBlockStatement;
use crate::ir::statement::ProtoAssignStatement;
use crate::ir::variable::{ModuleVariableMeta, VarOffset, create_variable_meta};
use crate::ir::{Event, ProtoExpression, ProtoStatement};
use crate::simulator_error::SimulatorError;
use std::collections::VecDeque;
#[cfg(not(target_family = "wasm"))]
use std::sync::Arc;
use veryl_analyzer::ir as air;
use veryl_parser::token_range::TokenRange;

/// Collect variable offsets from statements, filtering out internal variables
/// (those that appear in both inputs and outputs) to avoid dependency cycles
/// when the compiled block is used in analyze_dependency.
#[cfg(not(target_family = "wasm"))]
type VarOffsets = Vec<VarOffset>;

/// Collect canonical (current) FF offsets written by these statements.
#[cfg(not(target_family = "wasm"))]
fn gather_ff_canonical(stmts: &[ProtoStatement]) -> Vec<isize> {
    let mut result = HashSet::default();
    for s in stmts {
        result.extend(s.gather_ff_canonical_offsets());
    }
    result.into_iter().collect()
}

#[cfg(not(target_family = "wasm"))]
fn gather_external_offsets(stmts: &[ProtoStatement]) -> (VarOffsets, VarOffsets) {
    let mut all_inputs = vec![];
    let mut all_outputs = vec![];
    for s in stmts {
        s.gather_variable_offsets(&mut all_inputs, &mut all_outputs);
    }

    let input_set: HashSet<VarOffset> = all_inputs.iter().cloned().collect();
    let output_set: HashSet<VarOffset> = all_outputs.iter().cloned().collect();
    // Remove internal variables (both read and written) from inputs only.
    // Outputs are kept so dependent blocks see the dependency edge.
    let internal: HashSet<VarOffset> = input_set.intersection(&output_set).cloned().collect();
    all_inputs.retain(|x| !internal.contains(x));
    all_inputs.dedup();
    all_outputs.dedup();

    (all_inputs, all_outputs)
}

/// Stable topological sort of comb statements using Kahn's algorithm (BFS/FIFO).
///
/// Builds Read-After-Write (RAW) dependency edges: for each variable written by
/// statement A and read by statement B (where B != A), add edge A → B.
/// Self-references (a statement that both reads and writes the same variable)
/// are skipped to avoid false cycles.
///
/// Falls back to source order if a cycle is detected.
pub(crate) fn stable_topo_sort(statements: Vec<ProtoStatement>) -> Vec<ProtoStatement> {
    let n = statements.len();
    if n <= 1 {
        return statements;
    }

    // Gather per-statement inputs and outputs (variable offsets).
    let mut stmt_inputs: Vec<Vec<VarOffset>> = Vec::with_capacity(n);
    let mut stmt_outputs: Vec<Vec<VarOffset>> = Vec::with_capacity(n);
    for s in &statements {
        let mut ins = vec![];
        let mut outs = vec![];
        s.gather_variable_offsets(&mut ins, &mut outs);
        stmt_inputs.push(ins);
        stmt_outputs.push(outs);
    }

    // Build a map: variable → set of statement indices that write it.
    // Dedup per stmt so repeated writes (e.g. multiple case arms touching
    // the same var) don't inflate the downstream in-degree count.
    let mut writers: HashMap<VarOffset, Vec<usize>> = HashMap::default();
    for (i, outs) in stmt_outputs.iter().enumerate() {
        let mut unique_outs: HashSet<VarOffset> = HashSet::default();
        for &key in outs {
            if unique_outs.insert(key) {
                writers.entry(key).or_default().push(i);
            }
        }
    }

    // Build adjacency list and in-degree count for Kahn's algorithm.
    // Edge: writer_stmt → reader_stmt (RAW dependency).
    // For variables with multiple writers (sequential reassignment from inlined
    // functions), only the most recent writer before the reader is relevant.
    let mut adj: Vec<HashSet<usize>> = vec![HashSet::default(); n];
    let mut in_degree: Vec<usize> = vec![0; n];

    for (reader_idx, ins) in stmt_inputs.iter().enumerate() {
        for key in ins {
            if let Some(writer_indices) = writers.get(key) {
                if writer_indices.len() == 1 {
                    let writer_idx = writer_indices[0];
                    if writer_idx != reader_idx && adj[writer_idx].insert(reader_idx) {
                        in_degree[reader_idx] += 1;
                    }
                } else if let Some(&writer_idx) =
                    writer_indices.iter().rev().find(|&&w| w < reader_idx)
                    && adj[writer_idx].insert(reader_idx)
                {
                    in_degree[reader_idx] += 1;
                }
            }
        }
    }

    // WAW ordering: chain consecutive writers of the same variable so that
    // bit-select assigns to a packed variable keep source order.
    // Skip when next already reaches prev (would create a cycle).
    for writer_indices in writers.values() {
        for pair in writer_indices.windows(2) {
            let (prev, next) = (pair[0], pair[1]);
            let mut reachable = false;
            let mut stack = vec![next];
            let mut visited = HashSet::default();
            while let Some(node) = stack.pop() {
                if node == prev {
                    reachable = true;
                    break;
                }
                if visited.insert(node) {
                    for &succ in &adj[node] {
                        stack.push(succ);
                    }
                }
            }
            if !reachable && adj[prev].insert(next) {
                in_degree[next] += 1;
            }
        }
    }

    // Kahn's algorithm with FIFO queue (VecDeque) for stable ordering.
    // Initialize queue with zero-in-degree nodes in source order.
    let mut queue: VecDeque<usize> = VecDeque::new();
    for (i, &deg) in in_degree.iter().enumerate() {
        if deg == 0 {
            queue.push_back(i);
        }
    }

    let mut sorted_indices: Vec<usize> = Vec::with_capacity(n);
    while let Some(idx) = queue.pop_front() {
        sorted_indices.push(idx);
        // Collect successors in index order for determinism
        let mut successors: Vec<usize> = adj[idx].iter().cloned().collect();
        successors.sort_unstable();
        for succ in successors {
            in_degree[succ] -= 1;
            if in_degree[succ] == 0 {
                queue.push_back(succ);
            }
        }
    }

    // If not all nodes were processed, a cycle was detected — fall back to source order.
    if sorted_indices.len() != n {
        return statements;
    }

    // Reconstruct statement list in sorted order.
    let mut result: Vec<Option<ProtoStatement>> = statements.into_iter().map(Some).collect();
    sorted_indices
        .into_iter()
        .map(|i| result[i].take().unwrap())
        .collect()
}

pub struct ProtoDeclaration {
    pub event_statements: HashMap<Event, Vec<ProtoStatement>>,
    pub comb_statements: Vec<ProtoStatement>,
    /// Post-comb functions: child comb-only JIT functions for pre-event eval.
    pub post_comb_fns: Vec<ProtoStatement>,
    pub child_modules: Vec<ModuleVariableMeta>,
}

impl Conv<&air::Declaration> for ProtoDeclaration {
    fn conv(context: &mut Context, src: &air::Declaration) -> Result<Self, SimulatorError> {
        match src {
            air::Declaration::Comb(x) => {
                let mut comb_statements = vec![];
                for stmt in &x.statements {
                    let stmts: Vec<ProtoStatement> = Conv::conv(context, stmt)?;
                    comb_statements.extend(stmts);
                }
                let comb_statements = if comb_statements.len() > 1 {
                    vec![ProtoStatement::SequentialBlock(comb_statements)]
                } else {
                    comb_statements
                };
                Ok(ProtoDeclaration {
                    event_statements: HashMap::default(),
                    comb_statements,
                    post_comb_fns: vec![],
                    child_modules: vec![],
                })
            }
            air::Declaration::Ff(x) => {
                let mut statements = vec![];
                for stmt in &x.statements {
                    let stmts: Vec<ProtoStatement> = Conv::conv(context, stmt)?;
                    statements.extend(stmts);
                }

                let clock_event = Event::Clock(x.clock.id);
                let mut event_statements: HashMap<Event, Vec<ProtoStatement>> = HashMap::default();

                if let Some(reset) = &x.reset {
                    let reset_event = Event::Reset(reset.id);
                    let head = statements.remove(0);
                    let (true_side, false_side) = head.split_if_reset().unwrap();
                    event_statements.insert(reset_event, true_side);
                    event_statements.insert(clock_event, false_side);
                } else {
                    event_statements.insert(clock_event, statements);
                }

                Ok(ProtoDeclaration {
                    event_statements,
                    comb_statements: vec![],
                    post_comb_fns: vec![],
                    child_modules: vec![],
                })
            }
            air::Declaration::Inst(x) => Conv::conv(context, x.as_ref()),
            air::Declaration::Initial(x) => {
                context.in_initial = true;
                let mut initial_statements = vec![];
                let mut conv_err = None;
                for stmt in &x.statements {
                    match Conv::conv(context, stmt) {
                        Ok(stmts) => {
                            let stmts: Vec<ProtoStatement> = stmts;
                            initial_statements.extend(stmts);
                        }
                        Err(e) => {
                            conv_err = Some(e);
                            break;
                        }
                    }
                }
                context.in_initial = false;
                if let Some(e) = conv_err {
                    return Err(e);
                }
                let mut event_statements = HashMap::default();
                event_statements.insert(Event::Initial, initial_statements);
                Ok(ProtoDeclaration {
                    event_statements,
                    comb_statements: vec![],
                    post_comb_fns: vec![],
                    child_modules: vec![],
                })
            }
            air::Declaration::Final(x) => {
                let mut final_statements = vec![];
                for stmt in &x.statements {
                    let stmts: Vec<ProtoStatement> = Conv::conv(context, stmt)?;
                    final_statements.extend(stmts);
                }
                let mut event_statements = HashMap::default();
                event_statements.insert(Event::Final, final_statements);
                Ok(ProtoDeclaration {
                    event_statements,
                    comb_statements: vec![],
                    post_comb_fns: vec![],
                    child_modules: vec![],
                })
            }
            air::Declaration::Unsupported(token) => {
                Err(SimulatorError::unsupported_description(token))
            }
            air::Declaration::Null => Ok(ProtoDeclaration {
                event_statements: HashMap::default(),
                comb_statements: vec![],
                post_comb_fns: vec![],
                child_modules: vec![],
            }),
        }
    }
}

impl Conv<&air::InstDeclaration> for ProtoDeclaration {
    fn conv(context: &mut Context, src: &air::InstDeclaration) -> Result<Self, SimulatorError> {
        let air::Component::Module(child_module) = src.component.as_ref() else {
            panic!("InstDeclaration for non-Module component");
        };

        let mut child_analyzer_context = veryl_analyzer::conv::Context::default();
        child_analyzer_context.variables = child_module.variables.clone();
        child_analyzer_context.functions = child_module.functions.clone();
        let mut child_ff_table = air::FfTable::default();
        child_module.gather_ff(&mut child_analyzer_context, &mut child_ff_table);
        child_ff_table.update_is_ff();
        if context.config.disable_ff_opt {
            child_ff_table.force_all_ff();
        }

        // Comb-to-FF hoist: clone child declarations and mutate them —
        // move comb-side `let` writes into the consuming FF block, then
        // rebuild the FfTable on the hoisted form.
        let mut hoisted_child_decls = child_module.declarations.clone();
        {
            let plans = veryl_analyzer::ir::comb_to_ff_hoist::plan_hoists(
                &hoisted_child_decls,
                &child_ff_table,
                &child_module.variables,
            );
            veryl_analyzer::ir::comb_to_ff_hoist::apply_hoists(
                &mut hoisted_child_decls,
                &plans,
                &child_module.variables,
            );
            child_ff_table = air::FfTable::default();
            for (i, x) in hoisted_child_decls.iter().enumerate() {
                x.gather_ff(&mut child_analyzer_context, &mut child_ff_table, i);
            }
            child_ff_table.update_is_ff();
            if context.config.disable_ff_opt {
                child_ff_table.force_all_ff();
            }
        }
        let child_decls: &[air::Declaration] = &hoisted_child_decls;

        let ff_start = context.ff_total_bytes as isize;
        let comb_start = context.comb_total_bytes as isize;
        let (mut child_variable_meta, child_ff_count, child_comb_count) = create_variable_meta(
            &child_module.variables,
            &child_ff_table,
            context.config.use_4state,
            ff_start,
            comb_start,
        )
        .unwrap();

        context.ff_total_bytes += child_ff_count;
        context.comb_total_bytes += child_comb_count;

        // Alias child input/output port storage to the parent slot it's
        // wired to, eliminating the inter-module copy at settle_comb.
        let alias_enabled = std::env::var("VERYL_DISABLE_PORT_ALIAS").ok().as_deref() != Some("1");
        let mut aliased_input_ids: HashSet<air::VarId> = HashSet::default();
        if alias_enabled {
            for input in &src.inputs {
                let air::Expression::Term(factor) = &input.expr else {
                    continue;
                };
                let air::Factor::Variable(parent_id, idx, sel, _) = factor.as_ref() else {
                    continue;
                };
                if !idx.0.is_empty() || !sel.is_empty() {
                    continue;
                }
                // Reading parent storage directly during an event would
                // race the parent's own NBA commit; reject when the
                // parent has any always_ff writer.
                let parent_scope = context.scope();
                let parent_has_ff_writer = parent_scope
                    .ff_table
                    .table
                    .iter()
                    .any(|((vid, _), entry)| *vid == *parent_id && entry.assigned.is_some());
                if parent_has_ff_writer {
                    continue;
                }
                let parent_meta = match parent_scope.variable_meta.get(parent_id) {
                    Some(m) => m.clone(),
                    None => continue,
                };
                let child_meta = match child_variable_meta.get(&input.id) {
                    Some(m) => m,
                    None => continue,
                };
                if parent_meta.elements.len() != child_meta.elements.len()
                    || parent_meta.width != child_meta.width
                    || parent_meta.native_bytes != child_meta.native_bytes
                {
                    continue;
                }
                let entry = child_variable_meta.get_mut(&input.id).unwrap();
                for (i, parent_elem) in parent_meta.elements.iter().enumerate() {
                    entry.elements[i].current = parent_elem.current;
                    entry.elements[i].next_offset = parent_elem.next_offset;
                }
                // Drop initial_values so fill_buffers_recursive doesn't
                // overwrite parent storage with the child port's default.
                entry.initial_values.clear();
                aliased_input_ids.insert(input.id);
            }
        }

        // Output ports are aliased comb→comb only.  An FF on either side
        // has a separate next-slot write that would not reach the comb
        // slot the alias targets.
        let mut aliased_output_ids: HashSet<air::VarId> = HashSet::default();
        let mut already_aliased_parent_ids: HashSet<air::VarId> = HashSet::default();
        if alias_enabled {
            for output in &src.outputs {
                let Some(parent_dst) = output.dst.first() else {
                    continue;
                };
                if !parent_dst.index.0.is_empty() || !parent_dst.select.is_empty() {
                    continue;
                }
                let child_output_is_ff = child_ff_table
                    .table
                    .iter()
                    .any(|((vid, _), entry)| *vid == output.id && entry.assigned.is_some());
                if child_output_is_ff {
                    continue;
                }
                let parent_scope = context.scope();
                let parent_has_ff_writer = parent_scope
                    .ff_table
                    .table
                    .iter()
                    .any(|((vid, _), entry)| *vid == parent_dst.id && entry.assigned.is_some());
                if parent_has_ff_writer {
                    continue;
                }
                let parent_meta = match parent_scope.variable_meta.get(&parent_dst.id) {
                    Some(m) => m.clone(),
                    None => continue,
                };
                if parent_meta.elements.iter().any(|e| e.is_ff()) {
                    continue;
                }
                let child_meta = match child_variable_meta.get(&output.id) {
                    Some(m) => m,
                    None => continue,
                };
                if parent_meta.elements.len() != child_meta.elements.len()
                    || parent_meta.width != child_meta.width
                    || parent_meta.native_bytes != child_meta.native_bytes
                {
                    continue;
                }
                if already_aliased_parent_ids.contains(&parent_dst.id) {
                    continue;
                }
                let entry = child_variable_meta.get_mut(&output.id).unwrap();
                for (i, parent_elem) in parent_meta.elements.iter().enumerate() {
                    entry.elements[i].current = parent_elem.current;
                    entry.elements[i].next_offset = parent_elem.next_offset;
                }
                entry.initial_values.clear();
                aliased_output_ids.insert(output.id);
                already_aliased_parent_ids.insert(parent_dst.id);
            }
        }

        let child_scope = ScopeContext {
            variable_meta: child_variable_meta.clone(),
            analyzer_context: child_analyzer_context,
            ff_table: child_ff_table.clone(),
        };
        context.scope_contexts.push(child_scope);

        let mut all_event_statements: HashMap<Event, Vec<ProtoStatement>> = HashMap::default();
        let mut all_comb_statements: Vec<ProtoStatement> = vec![];
        let mut all_post_comb_fns: Vec<ProtoStatement> = vec![];
        let mut all_child_modules: Vec<ModuleVariableMeta> = vec![];

        for decl in child_decls {
            let proto_decl: ProtoDeclaration = Conv::conv(context, decl)?;

            for (event, mut stmts) in proto_decl.event_statements {
                all_event_statements
                    .entry(event)
                    .and_modify(|v| v.append(&mut stmts))
                    .or_insert(stmts);
            }
            all_comb_statements.append(&mut proto_decl.comb_statements.clone());
            all_post_comb_fns.extend(proto_decl.post_comb_fns);
            all_child_modules.extend(proto_decl.child_modules);
        }

        context.scope_contexts.pop();

        // JIT cache: reuse compiled code across instances of the same module type.
        // ff_start and comb_start are already byte offsets.
        //
        // The pre-JIT originals are preserved inside each CompiledBlock's
        // `original_stmts` field, and `analyze_dependency` Phase 2 expands
        // CBs from there when fine-grained ordering is needed.  Avoiding
        // a parallel copy outside the CB is what keeps the parent's
        // `unified` list free of duplicate ProtoStatements (which would
        // otherwise show up as false 2-stmt SCCs).
        #[cfg(not(target_family = "wasm"))]
        if context.config.use_jit {
            let ff_start_bytes = ff_start;
            let comb_start_bytes = comb_start;
            let component_key: *const air::Component = Arc::as_ptr(&src.component);

            // When input-port aliasing is on, the compiled chunk bakes
            // parent-specific offsets, so the cache (keyed only by the
            // child component) cannot be shared across instances.
            let cache_lookup = if alias_enabled {
                None
            } else {
                context.jit_cache.get(&component_key)
            };
            if let Some(cache_entry) = cache_lookup {
                // Cache hit: replace internal logic with CompiledBlocks using delta
                let ff_delta = ff_start_bytes - cache_entry.ref_ff_start_bytes;
                let comb_delta = comb_start_bytes - cache_entry.ref_comb_start_bytes;

                let adjust = |offsets: &[VarOffset]| -> Vec<VarOffset> {
                    offsets
                        .iter()
                        .map(|off| off.adjust(ff_delta, comb_delta))
                        .collect()
                };

                let adjust_stmts = |stmts: &[ProtoStatement]| -> Vec<ProtoStatement> {
                    let mut adjusted = stmts.to_vec();
                    for s in &mut adjusted {
                        s.adjust_offsets(ff_delta, comb_delta);
                    }
                    adjusted
                };

                for (event, stmts) in all_event_statements.iter_mut() {
                    if let Some(cached) = cache_entry.event_funcs.get(event) {
                        let adjusted_canonical: Vec<isize> = cached
                            .ff_canonical_offsets
                            .iter()
                            .map(|off| off + ff_delta)
                            .collect();
                        *stmts = vec![ProtoStatement::CompiledBlock(CompiledBlockStatement {
                            func: cached.func,
                            ff_delta_bytes: ff_delta,
                            comb_delta_bytes: comb_delta,
                            input_offsets: adjust(&cached.input_offsets),
                            output_offsets: adjust(&cached.output_offsets),
                            ff_canonical_offsets: adjusted_canonical,
                            stmt_deps: vec![],
                            original_stmts: adjust_stmts(&cached.original_stmts),
                        })];
                    }
                }

                if let Some(cached) = &cache_entry.comb_func {
                    let adjusted_deps: Vec<_> = cached
                        .stmt_deps
                        .iter()
                        .map(|(ins, outs)| (adjust(ins), adjust(outs)))
                        .collect();
                    all_comb_statements =
                        vec![ProtoStatement::CompiledBlock(CompiledBlockStatement {
                            func: cached.func,
                            ff_delta_bytes: ff_delta,
                            comb_delta_bytes: comb_delta,
                            input_offsets: adjust(&cached.input_offsets),
                            output_offsets: adjust(&cached.output_offsets),
                            ff_canonical_offsets: vec![],
                            stmt_deps: adjusted_deps,
                            original_stmts: adjust_stmts(&cached.original_stmts),
                        })];
                }
            } else {
                // Cache miss: compile events individually
                let mut event_funcs = HashMap::default();
                for (event, stmts) in all_event_statements.iter_mut() {
                    if stmts.iter().all(|s| s.can_build_binary())
                        && !stmts.is_empty()
                        && let Some(func) = cranelift::build_binary(context, stmts.clone())
                    {
                        // Event blocks use NBA semantics, so a variable
                        // that is both read and written is not purely
                        // internal; keep all inputs so sort_ff_event sees
                        // the dependency.
                        let mut all_inputs = vec![];
                        let mut all_outputs = vec![];
                        for s in stmts.iter() {
                            s.gather_variable_offsets(&mut all_inputs, &mut all_outputs);
                        }
                        all_inputs.dedup();
                        all_outputs.dedup();
                        let (input_offsets, output_offsets) = (all_inputs, all_outputs);
                        let ff_canonical = gather_ff_canonical(stmts);

                        let event_original = stmts.clone();
                        event_funcs.insert(
                            event.clone(),
                            JitCachedFunc {
                                func,
                                input_offsets: input_offsets.clone(),
                                output_offsets: output_offsets.clone(),
                                ff_canonical_offsets: ff_canonical.clone(),
                                stmt_deps: vec![],
                                original_stmts: event_original.clone(),
                            },
                        );

                        *stmts = vec![ProtoStatement::CompiledBlock(CompiledBlockStatement {
                            func,
                            ff_delta_bytes: 0,
                            comb_delta_bytes: 0,
                            input_offsets,
                            output_offsets,
                            ff_canonical_offsets: ff_canonical,
                            stmt_deps: vec![],
                            original_stmts: event_original,
                        })];
                    }
                }

                // Compile comb individually
                let all_can_build = all_comb_statements.iter().all(|s| s.can_build_binary());
                let comb_func = if all_can_build && !all_comb_statements.is_empty() {
                    // Sort statements topologically (RAW dependencies) so that
                    // output port connections run before assigns that read them.
                    let sorted_comb_for_func = stable_topo_sort(all_comb_statements.clone());

                    if let Some(func) =
                        cranelift::build_binary(context, sorted_comb_for_func.clone())
                    {
                        let (input_offsets, output_offsets) =
                            gather_external_offsets(&sorted_comb_for_func);

                        let stmt_deps: Vec<_> = sorted_comb_for_func
                            .iter()
                            .map(|s| {
                                let mut ins = vec![];
                                let mut outs = vec![];
                                s.gather_variable_offsets(&mut ins, &mut outs);
                                (ins, outs)
                            })
                            .collect();

                        let original_stmts = sorted_comb_for_func.clone();
                        all_comb_statements =
                            vec![ProtoStatement::CompiledBlock(CompiledBlockStatement {
                                func,
                                ff_delta_bytes: 0,
                                comb_delta_bytes: 0,
                                input_offsets: input_offsets.clone(),
                                output_offsets: output_offsets.clone(),
                                ff_canonical_offsets: vec![],
                                stmt_deps: stmt_deps.clone(),
                                original_stmts,
                            })];

                        Some(JitCachedFunc {
                            func,
                            input_offsets,
                            output_offsets,
                            ff_canonical_offsets: vec![],
                            stmt_deps,
                            original_stmts: sorted_comb_for_func,
                        })
                    } else {
                        None
                    }
                } else {
                    None
                };

                if !alias_enabled {
                    context.jit_cache.insert(
                        component_key,
                        JitCacheEntry {
                            ref_ff_start_bytes: ff_start_bytes,
                            ref_comb_start_bytes: comb_start_bytes,
                            event_funcs,
                            comb_func,
                        },
                    );
                }
            }
        }

        // Input ports: parent expr → child port var
        for input in &src.inputs {
            if aliased_input_ids.contains(&input.id) {
                continue;
            }
            let child_meta = child_variable_meta.get(&input.id).unwrap();

            // Array port with a simple variable expression: expand per-element
            if child_meta.elements.len() > 1
                && let air::Expression::Term(factor) = &input.expr
                && let air::Factor::Variable(parent_id, index, select, _) = factor.as_ref()
                && index.0.is_empty()
                && select.is_empty()
            {
                let parent_scope = context.scope();
                let parent_meta = parent_scope.variable_meta.get(parent_id).unwrap();
                for i in 0..child_meta.elements.len() {
                    let child_element = &child_meta.elements[i];
                    let parent_element = &parent_meta.elements[i];
                    let parent_expr = ProtoExpression::Variable {
                        var_offset: parent_element.current,
                        select: None,
                        dynamic_select: None,
                        width: child_meta.width,
                        var_full_width: child_meta.width,
                        expr_context: ExpressionContext {
                            width: child_meta.width,
                            signed: false,
                        },
                    };
                    all_comb_statements.push(ProtoStatement::Assign(ProtoAssignStatement {
                        dst: child_element.current,
                        dst_width: child_meta.width,
                        select: None,
                        dynamic_select: None,
                        rhs_select: None,
                        expr: parent_expr,
                        dst_ff_current_offset: 0, // not FF
                        token: TokenRange::default(),
                    }));
                }
                continue;
            }

            let proto_expr: ProtoExpression = Conv::conv(context, &input.expr)?;
            let element = &child_meta.elements[0];
            all_comb_statements.push(ProtoStatement::Assign(ProtoAssignStatement {
                dst: element.current,
                dst_width: child_meta.width,
                select: None,
                dynamic_select: None,
                rhs_select: None,
                expr: proto_expr.clone(),
                dst_ff_current_offset: 0, // not FF
                token: TokenRange::default(),
            }));
        }

        // Output ports: child port var → parent dst
        for output in &src.outputs {
            if aliased_output_ids.contains(&output.id) {
                continue;
            }
            if let Some(parent_dst) = output.dst.first() {
                let child_meta = child_variable_meta.get(&output.id).unwrap();

                let (
                    parent_index,
                    parent_select,
                    parent_width,
                    parent_need_dynamic,
                    parent_width_shape,
                    parent_kind_width,
                ) = {
                    let parent_scope = context.scope();
                    let parent_meta = parent_scope.variable_meta.get(&parent_dst.id).unwrap();
                    let parent_index = parent_dst
                        .index
                        .eval_value(&mut parent_scope.analyzer_context)
                        .unwrap();

                    let select = if !parent_dst.select.is_empty() {
                        parent_dst.select.eval_value(
                            &mut parent_scope.analyzer_context,
                            &parent_dst.comptime.r#type,
                            false,
                        )
                    } else {
                        None
                    };
                    let need_dynamic =
                        !parent_dst.select.is_empty() && !parent_dst.select.is_const();
                    let select = if need_dynamic { None } else { select };
                    let width = parent_meta.width;
                    let width_shape = parent_meta.r#type.width().clone();
                    let kind_width = parent_meta.r#type.kind.width().unwrap_or(1);
                    (
                        parent_index,
                        select,
                        width,
                        need_dynamic,
                        width_shape,
                        kind_width,
                    )
                };

                let parent_dynamic_select = if parent_need_dynamic {
                    Some(build_dynamic_bit_select(
                        context,
                        &parent_width_shape,
                        &parent_dst.select,
                        parent_kind_width,
                    )?)
                } else {
                    None
                };

                let parent_scope = context.scope();
                let parent_meta = parent_scope.variable_meta.get(&parent_dst.id).unwrap();

                // Determine which parent elements to connect.
                // When the parent destination has no index and the variable is an
                // array, connect each element individually (array-to-array port).
                let parent_element_indices: Vec<usize> = if let Some(idx) =
                    parent_meta.r#type.array.calc_index(&parent_index)
                {
                    vec![idx]
                } else if parent_index.is_empty() && !parent_meta.r#type.array.is_empty() {
                    (0..parent_meta.elements.len()).collect()
                } else {
                    panic!(
                        "calc_index failed for output port destination (index {:?}, array {:?})",
                        parent_index, parent_meta.r#type.array,
                    );
                };

                for (elem_idx, &parent_elem_idx) in parent_element_indices.iter().enumerate() {
                    let child_element = &child_meta.elements[elem_idx];
                    let parent_element = &parent_meta.elements[parent_elem_idx];

                    let child_expr = ProtoExpression::Variable {
                        var_offset: child_element.current,
                        select: None,
                        dynamic_select: None,
                        width: child_meta.width,
                        var_full_width: child_meta.width,
                        expr_context: ExpressionContext {
                            width: child_meta.width,
                            signed: false,
                        },
                    };

                    let dst_var = if parent_element.is_ff() {
                        VarOffset::Ff(parent_element.next_offset)
                    } else {
                        VarOffset::Comb(parent_element.current_offset())
                    };

                    let stmt = ProtoStatement::Assign(ProtoAssignStatement {
                        dst: dst_var,
                        dst_width: parent_width,
                        select: parent_select,
                        dynamic_select: parent_dynamic_select.clone(),
                        rhs_select: None,
                        expr: child_expr,
                        dst_ff_current_offset: parent_element.current_offset(),
                        token: TokenRange::default(),
                    });

                    all_comb_statements.push(stmt);
                }
            }
        }

        // Remap child event keys (clock/reset) to parent VarIds via input port connections
        let mut child_to_parent_var: HashMap<air::VarId, air::VarId> = HashMap::default();
        for input in &src.inputs {
            if let air::Expression::Term(factor) = &input.expr
                && let air::Factor::Variable(parent_var_id, _, _, _) = factor.as_ref()
            {
                child_to_parent_var.insert(input.id, *parent_var_id);
            }
        }

        let mut remapped_events: HashMap<Event, Vec<ProtoStatement>> = HashMap::default();
        for (event, stmts) in all_event_statements {
            let new_event = match &event {
                Event::Clock(child_id) => {
                    if let Some(parent_id) = child_to_parent_var.get(child_id) {
                        Event::Clock(*parent_id)
                    } else {
                        event.clone()
                    }
                }
                Event::Reset(child_id) => {
                    if let Some(parent_id) = child_to_parent_var.get(child_id) {
                        Event::Reset(*parent_id)
                    } else {
                        event.clone()
                    }
                }
                _ => event.clone(),
            };
            remapped_events
                .entry(new_event)
                .and_modify(|v| v.extend(stmts.clone()))
                .or_insert(stmts);
        }

        let child_module_meta = ModuleVariableMeta {
            name: src.name,
            variable_meta: child_variable_meta,
            children: all_child_modules,
        };

        Ok(ProtoDeclaration {
            event_statements: remapped_events,
            comb_statements: all_comb_statements,
            post_comb_fns: all_post_comb_fns,
            child_modules: vec![child_module_meta],
        })
    }
}