xqvm 0.2.1

X-Quadratic Virtual Machine — bytecode interpreter for the XQuad Toolchain
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
// Copyright (C) 2026 Postquant Labs Incorporated
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! CFG construction and forward stack-depth analysis for XQVM bytecode.
//!
//! The main entry point is [`check_stack_depth`], which:
//!
//! 1. Builds a [`Cfg`] of basic blocks (`BlockId = byte offset of first instruction`).
//! 2. Derives a [`BlockEffect`] for every block (abstract stack effect).
//! 3. Runs a forward worklist analysis with a [`DepthValue`] lattice.
//! 4. Reports the first [`VerifierError`] found.
//!
//! Unlike a linear scan, this analysis follows conditional branches so
//! underflows only reachable on non-fall-through paths are caught.
//!
//! # Correctness note
//!
//! Block effects are computed using the net stack delta from the opcode table.
//! This deliberately does not model the internal pop/push split for instructions
//! like `ADD` (pop 2, push 1, delta -1).  Any false negatives are the same as
//! those already present in a linear net-delta scan.

#[cfg(not(feature = "std"))]
use alloc::{
    collections::{BTreeSet, VecDeque},
    vec::Vec,
};

#[cfg(feature = "std")]
use std::collections::{BTreeSet, HashMap, VecDeque};

#[cfg(not(feature = "std"))]
use hashbrown::HashMap;

use crate::Program;
use crate::VerifierError;
use crate::bytecode::{Instruction, InstructionStream, JumpTable, StackEffect};
use crate::dataflow::analysis::Analysis;
use crate::dataflow::cfg::Cfg;
use crate::dataflow::direction::Forward;
use crate::dataflow::lattice::Lattice;
use crate::dataflow::solver::solve;

/// Maximum integer stack depth permitted by the VM spec.
const STACK_LIMIT: usize = 8192;

// ---------------------------------------------------------------------------
// BlockId
// ---------------------------------------------------------------------------

/// Byte offset of a basic block's first instruction.
pub type BlockId = usize;

// ---------------------------------------------------------------------------
// BlockEffect
// ---------------------------------------------------------------------------

/// Abstract stack effect of a basic block.
///
/// `min_input` is the minimum entry depth required to avoid underflow inside
/// the block (in the pre-`SCLR` phase).  `output` describes the depth at exit.
#[derive(Clone, Debug)]
pub struct BlockEffect {
    /// Minimum entry depth required to execute the block without underflow.
    pub min_input: usize,
    /// Stack depth at block exit.
    pub output: OutputKind,
}

/// How the exit depth relates to the entry depth.
#[derive(Clone, Debug)]
pub enum OutputKind {
    /// Exit depth = entry depth + `delta`.
    Delta(i32),
    /// Exit depth is fixed regardless of entry (block contains `SCLR`).
    Fixed(usize),
    /// Block always causes stack underflow regardless of entry depth.
    ///
    /// Occurs when instructions after a `SCLR` drive the absolute depth below 0.
    Underflow,
}

// ---------------------------------------------------------------------------
// DepthValue lattice
// ---------------------------------------------------------------------------

/// Abstract stack depth at a program point.
///
/// The meet-semilattice order is `Top` ≥ `Known(n)` (for all n) ≥ `Underflow`.
/// `Known` values are additionally ordered by magnitude so that
/// `meet(Known(a), Known(b)) = Known(min(a, b))` (conservative join).
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum DepthValue {
    /// No information yet (initial state for unvisited nodes).
    Top,
    /// The stack has exactly `n` items at this program point.
    Known(usize),
    /// A stack underflow was detected on at least one path to this point.
    Underflow,
}

impl Lattice for DepthValue {
    fn top() -> Self {
        Self::Top
    }

    fn meet(&self, other: &Self) -> Self {
        match (self, other) {
            (Self::Top, x) | (x, Self::Top) => x.clone(),
            (Self::Underflow, _) | (_, Self::Underflow) => Self::Underflow,
            (Self::Known(a), Self::Known(b)) => Self::Known(*a.min(b)),
        }
    }
}

// ---------------------------------------------------------------------------
// StackDepthAnalysis
// ---------------------------------------------------------------------------

/// Forward stack-depth analysis over XQVM basic blocks.
pub struct StackDepthAnalysis<'a> {
    effects: &'a HashMap<BlockId, BlockEffect>,
}

impl Analysis for StackDepthAnalysis<'_> {
    type Value = DepthValue;
    type Node = BlockId;
    type Dir = Forward;

    fn boundary_value(&self) -> DepthValue {
        DepthValue::Known(0)
    }

    fn transfer(&self, node: &BlockId, input: &DepthValue) -> DepthValue {
        let effect = self.effects.get(node);
        debug_assert!(effect.is_some(), "every CFG node must have a BlockEffect");
        apply_effect(input.clone(), effect)
    }
}

// ---------------------------------------------------------------------------
// apply_effect  (internal)
// ---------------------------------------------------------------------------

/// Apply a [`BlockEffect`] to an entry [`DepthValue`], returning the exit value.
fn apply_effect(input: DepthValue, effect: Option<&BlockEffect>) -> DepthValue {
    let Some(effect) = effect else {
        return input;
    };
    match input {
        DepthValue::Top => DepthValue::Top,
        DepthValue::Underflow => DepthValue::Underflow,
        DepthValue::Known(depth) => {
            if depth < effect.min_input {
                return DepthValue::Underflow;
            }
            match &effect.output {
                OutputKind::Underflow => DepthValue::Underflow,
                OutputKind::Delta(d) => {
                    let out = depth.saturating_add_signed(isize::try_from(*d).unwrap_or(0));
                    DepthValue::Known(out)
                }
                OutputKind::Fixed(k) => DepthValue::Known(*k),
            }
        }
    }
}

// ---------------------------------------------------------------------------
// BlockTerminatorKind  (internal)
// ---------------------------------------------------------------------------

/// Kind of the last control-flow instruction in a basic block.
#[derive(Debug)]
enum BlockTerminatorKind {
    FallThrough,
    Jump {
        label: u16,
    },
    CondJump {
        label: u16,
    },
    /// Block ends with `RANGE` or `ITER`.
    LoopOpener,
    /// Block ends with `NEXT`.
    LoopCloser,
    Halt,
}

// ---------------------------------------------------------------------------
// LoopRegion  (internal)
// ---------------------------------------------------------------------------

/// Pre-built CFG context shared across the three CFG-based verifier phases.
///
/// Constructed once by [`build_cfg`] so that the register, uninit, and stack-depth
/// analyses can all operate on the same graph without repeating the two-pass construction.
pub(crate) struct CfgContext {
    /// The control-flow graph of basic blocks.
    pub(crate) cfg: Cfg<BlockId>,
    /// Abstract stack effect for each basic block.
    pub(crate) effects: HashMap<BlockId, BlockEffect>,
    /// Loop regions identified during CFG construction.
    pub(crate) loop_regions: Vec<LoopRegion>,
}

/// Pairing of a loop opener with the blocks it governs.
pub(crate) struct LoopRegion {
    /// Byte offset of the `RANGE`/`ITER` instruction (for error reporting).
    opener_pos: usize,
    /// Block that contains the `RANGE`/`ITER` terminator.
    opener_block: BlockId,
    /// First block of the loop body (successor of opener via fall-through).
    body_start: BlockId,
    /// First block after the loop (skip-edge target of opener).
    after_next: BlockId,
}

// ---------------------------------------------------------------------------
// compute_block_effect  (internal)
// ---------------------------------------------------------------------------

/// Simulate `code[block_start..block_end]` to derive a [`BlockEffect`] and the
/// [`BlockTerminatorKind`] with the absolute byte offset of the terminator.
///
/// Returns `(effect, kind, terminator_abs_offset)`.
fn compute_block_effect(
    code: &[u8],
    block_start: usize,
    block_end: usize,
) -> (BlockEffect, BlockTerminatorKind, usize) {
    let default = (
        BlockEffect {
            min_input: 0,
            output: OutputKind::Delta(0),
        },
        BlockTerminatorKind::FallThrough,
        block_start,
    );

    let block_code = match code.get(block_start..block_end) {
        Some(s) if !s.is_empty() => s,
        _ => return default,
    };

    // `depth` is the running stack depth relative to entry (starts at 0).
    // After `SCLR`, `depth` is absolute (reset to 0) and `fixed` is set.
    let mut depth: i32 = 0;
    let mut min_input: usize = 0;
    let mut fixed: bool = false;
    let mut static_underflow: bool = false;

    let mut kind = BlockTerminatorKind::FallThrough;
    let mut term_pos: usize = block_start;

    let mut stream = InstructionStream::new(block_code);
    while let Some(item) = stream.next_instruction() {
        let Ok((rel_pos, _, instr)) = item else { break };
        let abs_pos = block_start + rel_pos;

        match instr.stack_effect() {
            StackEffect::Reset => {
                if !fixed && depth < 0 {
                    min_input = min_input.max(depth.unsigned_abs() as usize);
                }
                fixed = true;
                static_underflow = false;
                depth = 0;
            }
            StackEffect::Delta(d) => {
                let d = i32::from(d);
                depth += d;
                if !fixed {
                    if depth < 0 {
                        min_input = min_input.max(depth.unsigned_abs() as usize);
                    }
                } else if depth < 0 {
                    static_underflow = true;
                }
            }
        }

        let new_kind = match &instr {
            Instruction::Jump1 { label } => Some(BlockTerminatorKind::Jump {
                label: u16::from(*label),
            }),
            Instruction::Jump2 { label } => Some(BlockTerminatorKind::Jump { label: *label }),
            Instruction::JumpI1 { label } => Some(BlockTerminatorKind::CondJump {
                label: u16::from(*label),
            }),
            Instruction::JumpI2 { label } => Some(BlockTerminatorKind::CondJump { label: *label }),
            Instruction::Halt {} => Some(BlockTerminatorKind::Halt),
            Instruction::Range {} | Instruction::Iter { .. } => {
                Some(BlockTerminatorKind::LoopOpener)
            }
            Instruction::Next {} => Some(BlockTerminatorKind::LoopCloser),
            _ => None,
        };

        if let Some(k) = new_kind {
            kind = k;
            term_pos = abs_pos;
        }
    }

    let output = if static_underflow {
        OutputKind::Underflow
    } else if fixed {
        OutputKind::Fixed(usize::try_from(depth).unwrap_or(0))
    } else {
        OutputKind::Delta(depth)
    };

    (BlockEffect { min_input, output }, kind, term_pos)
}

// ---------------------------------------------------------------------------
// simulate_body  (internal)
// ---------------------------------------------------------------------------

/// One-pass BFS simulation of a loop body for [`LoopStackImbalance`] detection.
///
/// Starting from `body_start` with `entry` depth, propagates through the body
/// blocks (skipping the back-edge to `body_start` and the skip-edge to
/// `after_next`).  Returns the depth at the `NEXT` block's output.
///
/// Because back-edges are skipped, each block is visited at most once per
/// depth-change, so the simulation always terminates.
///
/// [`LoopStackImbalance`]: VerifierError::LoopStackImbalance
fn simulate_body(
    body_start: BlockId,
    entry: DepthValue,
    after_next: BlockId,
    effects: &HashMap<BlockId, BlockEffect>,
    cfg: &Cfg<BlockId>,
) -> DepthValue {
    let mut depth_in: HashMap<BlockId, DepthValue> = HashMap::new();
    let mut worklist: VecDeque<BlockId> = VecDeque::new();
    let _ = depth_in.insert(body_start, entry);
    worklist.push_back(body_start);

    let mut exit_depth = DepthValue::Top;

    while let Some(block) = worklist.pop_front() {
        let d_in = depth_in.get(&block).cloned().unwrap_or(DepthValue::Top);
        let d_out = apply_effect(d_in, effects.get(&block));

        // A block with a successor == body_start is the NEXT block.
        if cfg.successors(&block).contains(&body_start) {
            exit_depth = exit_depth.meet(&d_out);
        }

        for &succ in cfg.successors(&block) {
            if succ == body_start || succ == after_next {
                continue;
            }
            let current = depth_in.get(&succ).cloned().unwrap_or(DepthValue::Top);
            let merged = current.meet(&d_out);
            if merged != current {
                let _ = depth_in.insert(succ, merged);
                worklist.push_back(succ);
            }
        }
    }

    exit_depth
}

// ---------------------------------------------------------------------------
// build_cfg
// ---------------------------------------------------------------------------

/// Build a [`CfgContext`] from bytecode.
///
/// Two-pass construction:
///
/// 1. Collect *leaders* (block-entry byte offsets) and pair every `RANGE`/`ITER`
///    with its matching `NEXT` to get loop skip-edges and back-edges.
/// 2. For each block `[leader_i, leader_{i+1})`, compute the [`BlockEffect`]
///    and emit outgoing CFG edges based on the terminator instruction.
///
/// Returns `None` for empty bytecode.  Call after structural verification to
/// guarantee well-formed input (balanced loops, valid jump targets).
#[expect(
    clippy::too_many_lines,
    reason = "two-pass CFG construction; splitting the passes into separate functions would obscure the algorithm"
)]
pub(crate) fn build_cfg(code: &[u8], jump_table: &JumpTable) -> Option<CfgContext> {
    if code.is_empty() {
        return None;
    }

    // ------------------------------------------------------------------
    // Pass 1 -- collect leaders and loop pairing.
    //
    // opener_info : RANGE/ITER abs_pos → (body_start, after_next)
    // next_back   : NEXT abs_pos       → body_start (back-edge target)
    // ------------------------------------------------------------------
    let mut leaders: BTreeSet<usize> = BTreeSet::new();
    let _ = leaders.insert(0);

    let mut opener_info: HashMap<usize, (usize, usize)> = HashMap::new();
    let mut next_back: HashMap<usize, usize> = HashMap::new();
    let mut loop_stack: Vec<(usize, usize)> = Vec::new(); // (opener_pos, body_start)
    let mut halt_block: Option<usize> = None;
    let mut cur_block: usize = 0;

    let mut stream = InstructionStream::new(code);
    while let Some(item) = stream.next_instruction() {
        let Ok((pos, _, instr)) = item else { break };
        let after = stream.pos();

        if leaders.contains(&pos) {
            cur_block = pos;
        }

        match &instr {
            Instruction::Target {} => {
                let _ = leaders.insert(pos);
            }
            Instruction::Jump1 { .. }
            | Instruction::Jump2 { .. }
            | Instruction::JumpI1 { .. }
            | Instruction::JumpI2 { .. } => {
                let _ = leaders.insert(after);
            }
            Instruction::Range {} | Instruction::Iter { .. } => {
                let _ = leaders.insert(after);
                loop_stack.push((pos, after));
            }
            Instruction::Next {} => {
                let _ = leaders.insert(after);
                if let Some((opener_pos, body_start)) = loop_stack.pop() {
                    let _ = next_back.insert(pos, body_start);
                    let _ = opener_info.insert(opener_pos, (body_start, after));
                }
            }
            Instruction::Halt {} => {
                halt_block = Some(cur_block);
            }
            _ => {}
        }
    }

    let leaders_sorted: Vec<usize> = leaders.into_iter().collect();
    let exit_block = halt_block.unwrap_or_else(|| *leaders_sorted.last().unwrap_or(&0));

    // ------------------------------------------------------------------
    // Pass 2 -- compute effects, build edges, collect loop regions.
    // ------------------------------------------------------------------
    let mut effects: HashMap<BlockId, BlockEffect> = HashMap::new();
    let mut cfg_builder = Cfg::builder(0usize, exit_block);
    let mut loop_regions: Vec<LoopRegion> = Vec::new();

    for (idx, &block_start) in leaders_sorted.iter().enumerate() {
        let block_end = leaders_sorted.get(idx + 1).copied().unwrap_or(code.len());

        let (effect, term_kind, term_pos) = compute_block_effect(code, block_start, block_end);
        let _ = effects.insert(block_start, effect);

        match term_kind {
            BlockTerminatorKind::FallThrough => {
                if block_end < code.len() {
                    cfg_builder = cfg_builder.edge(block_start, block_end);
                }
            }
            BlockTerminatorKind::Jump { label } => {
                if let Some(target) = jump_table.get(label) {
                    cfg_builder = cfg_builder.edge(block_start, target);
                }
            }
            BlockTerminatorKind::CondJump { label } => {
                if let Some(target) = jump_table.get(label) {
                    cfg_builder = cfg_builder.edge(block_start, target);
                }
                if block_end < code.len() {
                    cfg_builder = cfg_builder.edge(block_start, block_end);
                }
            }
            BlockTerminatorKind::LoopOpener => {
                if let Some(&(body_start, after_next)) = opener_info.get(&term_pos) {
                    cfg_builder = cfg_builder.edge(block_start, body_start);
                    cfg_builder = cfg_builder.edge(block_start, after_next);
                    loop_regions.push(LoopRegion {
                        opener_pos: term_pos,
                        opener_block: block_start,
                        body_start,
                        after_next,
                    });
                } else {
                    // Unmatched loop opener (shouldn't happen after Phase 1).
                    cfg_builder = cfg_builder.edge(block_start, block_end);
                }
            }
            BlockTerminatorKind::LoopCloser => {
                if let Some(&body_start) = next_back.get(&term_pos) {
                    cfg_builder = cfg_builder.edge(block_start, body_start);
                }
                if block_end < code.len() {
                    cfg_builder = cfg_builder.edge(block_start, block_end);
                }
            }
            BlockTerminatorKind::Halt => {}
        }
    }

    Some(CfgContext {
        cfg: cfg_builder.build(),
        effects,
        loop_regions,
    })
}

// ---------------------------------------------------------------------------
// check_stack_depth
// ---------------------------------------------------------------------------

/// Run the CFG-based forward stack-depth analysis using a pre-built [`CfgContext`].
///
/// Consumes `ctx` so that the caller can construct it once and pass it in
/// without cloning.  The check order is:
///
/// 1. [`VerifierError::LoopStackImbalance`] -- a loop body has non-zero net
///    stack effect (detected via a one-pass BFS that ignores back-edges).
/// 2. [`VerifierError::StackDepthMismatch`] -- two predecessors of a join
///    point carry different stack depths.
/// 3. [`VerifierError::StackUnderflow`] -- a reachable block has insufficient
///    entry depth or a static post-`SCLR` underflow.
/// 4. [`VerifierError::StackOverflowRisk`] -- depth exceeds 8 192.
///
/// # Errors
///
/// Returns the first [`VerifierError`] found, in the order described above.
pub(crate) fn check_stack_depth_with_context(
    cfg: &Cfg<BlockId>,
    effects: &HashMap<BlockId, BlockEffect>,
    loop_regions: &[LoopRegion],
) -> Result<(), VerifierError> {
    let analysis = StackDepthAnalysis { effects };
    let result = solve(&analysis, cfg);

    // --- LoopStackImbalance check (before underflow scan) ---
    //
    // The worklist analysis uses min-meet on back-edges, which causes
    // loop-body imbalances to converge rather than diverge.  A separate
    // one-pass BFS (simulate_body) detects them correctly.
    for region in loop_regions {
        let entry = result
            .after(&region.opener_block)
            .cloned()
            .unwrap_or(DepthValue::Top);
        let exit = simulate_body(
            region.body_start,
            entry.clone(),
            region.after_next,
            analysis.effects,
            cfg,
        );
        if let (DepthValue::Known(e), DepthValue::Known(x)) = (&entry, &exit)
            && e != x
        {
            return Err(VerifierError::LoopStackImbalance {
                opener: region.opener_pos,
                entry: *e,
                exit: *x,
            });
        }
    }

    // --- StackDepthMismatch check ---
    //
    // For every block with two or more reachable predecessors, collect the
    // exit depth of each predecessor and verify they are all equal.  A
    // discrepancy means the stack is in an undefined state at the join point.
    // Blocks are visited in program order (ascending byte offset) so the
    // error reported is deterministic.
    //
    // `Top` predecessors are unreachable paths; they are excluded from the
    // comparison because they contribute no real stack frame.
    let mut sorted_join_blocks: Vec<BlockId> = cfg.nodes().to_vec();
    sorted_join_blocks.sort_unstable();

    for &block in &sorted_join_blocks {
        // Skip single-predecessor and entry blocks (no join to check).
        let preds = cfg.predecessors(&block);
        if preds.len() < 2 {
            continue;
        }
        // Skip unreachable blocks (their before-state is Top or missing).
        match result.before(&block) {
            None | Some(DepthValue::Top) => continue,
            _ => {}
        }
        // Collect known exit depths from reachable predecessors.
        let mut known = preds.iter().filter_map(|p| {
            if let Some(DepthValue::Known(d)) = result.after(p) {
                Some(*d)
            } else {
                None
            }
        });
        let Some(first) = known.next() else { continue };
        for other in known {
            if other != first {
                return Err(VerifierError::StackDepthMismatch {
                    target_offset: block,
                    depth_a: first,
                    depth_b: other,
                });
            }
        }
    }

    // --- Underflow / overflow scan ---
    for &node in cfg.nodes() {
        let before = result.before(&node).cloned().unwrap_or(DepthValue::Top);
        let after = result.after(&node).cloned().unwrap_or(DepthValue::Top);

        if before == DepthValue::Top {
            continue;
        }

        match &before {
            DepthValue::Underflow => {
                return Err(VerifierError::StackUnderflow { offset: node });
            }
            DepthValue::Known(d) if *d > STACK_LIMIT => {
                return Err(VerifierError::StackOverflowRisk {
                    offset: node,
                    depth: *d,
                });
            }
            _ => {}
        }

        match &after {
            DepthValue::Underflow => {
                return Err(VerifierError::StackUnderflow { offset: node });
            }
            DepthValue::Known(d) if *d > STACK_LIMIT => {
                return Err(VerifierError::StackOverflowRisk {
                    offset: node,
                    depth: *d,
                });
            }
            _ => {}
        }
    }

    Ok(())
}

/// Run the CFG-based forward stack-depth analysis on `program`.
///
/// Builds a `CfgContext` from `program` and delegates to
/// `check_stack_depth_with_context`.  Returns `Ok(())` for empty programs.
///
/// Callers that run multiple CFG-based analyses should prefer building the
/// context once via `build_cfg` and calling
/// `check_stack_depth_with_context` directly.
///
/// # Errors
///
/// Returns the first [`VerifierError`] found.
pub fn check_stack_depth(program: &Program) -> Result<(), VerifierError> {
    let code = program.code();
    if code.is_empty() {
        return Ok(());
    }
    let Some(ctx) = build_cfg(code, program.jump_table()) else {
        return Ok(());
    };
    check_stack_depth_with_context(&ctx.cfg, &ctx.effects, &ctx.loop_regions)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn prog(build: impl FnOnce(&mut InstructionBuilder)) -> Program {
        let mut b = InstructionBuilder::new();
        build(&mut b);
        b.build().unwrap()
    }

    // --- basic valid programs ---

    #[test]
    fn empty_program_ok() {
        assert!(check_stack_depth(&Program::new(vec![])).is_ok());
    }

    #[test]
    fn halt_only_ok() {
        let p = prog(|b| {
            let _ = b.emit_halt();
        });
        assert!(check_stack_depth(&p).is_ok());
    }

    #[test]
    fn push_halt_ok() {
        let p = prog(|b| {
            let _ = b.emit_push(42).emit_halt();
        });
        assert!(check_stack_depth(&p).is_ok());
    }

    #[test]
    fn push_pop_halt_ok() {
        let p = prog(|b| {
            let _ = b.emit_push(1).emit_pop().emit_halt();
        });
        assert!(check_stack_depth(&p).is_ok());
    }

    // --- underflow detection ---

    #[test]
    fn pop_on_empty_stack_underflow() {
        let p = prog(|b| {
            let _ = b.emit_pop().emit_halt();
        });
        let err = check_stack_depth(&p).unwrap_err();
        assert_eq!(err.variant_name(), "StackUnderflow");
    }

    #[test]
    fn static_underflow_after_sclr() {
        // SCLR resets depth to 0; subsequent POP underflows unconditionally.
        let p = prog(|b| {
            let _ = b.emit_sclr().emit_pop().emit_halt();
        });
        let err = check_stack_depth(&p).unwrap_err();
        assert_eq!(err.variant_name(), "StackUnderflow");
    }

    #[test]
    fn sclr_then_push_ok() {
        // SCLR followed by a push is fine.
        let p = prog(|b| {
            let _ = b.emit_sclr().emit_push(1).emit_halt();
        });
        assert!(check_stack_depth(&p).is_ok());
    }

    // --- CFG path coverage: catches what a linear scan misses ---
    //
    // Program structure:
    //   PUSH1(1)          ; depth 1
    //   JUMPI1(target)    ; pops condition → depth 0; taken path jumps to TARGET
    //   PUSH1(99)         ; fall-through: depth 1
    //   TARGET            ; join point: taken arrives with depth 0, fall-through with depth 1
    //   POP               ; ...
    //   HALT
    //
    // The linear scan walks: 1 → JUMPI1 (depth 0) → PUSH1 (depth 1) → TARGET (1)
    // → POP (0) → HALT.  It misses that the taken path arrives with depth 0 != 1.
    // The CFG analysis detects the mismatch (StackDepthMismatch), which is the
    // root cause of what the linear scan would have eventually reported as
    // StackUnderflow on the POP instruction.

    #[test]
    fn cond_branch_depth_mismatch_caught() {
        let mut b = InstructionBuilder::new();
        let target = b.label();
        let _ = b
            .emit_push(1)
            .emit_jump_if(target) // taken: depth 0; fall-through: depth 0 → +1 below
            .emit_push(99) // fall-through only: depth 1 at TARGET
            .place(target)
            .unwrap()
            .emit_pop()
            .emit_halt();
        let p = b.build().unwrap();
        let err = check_stack_depth(&p).unwrap_err();
        // StackDepthMismatch fires before StackUnderflow: depth 0 vs 1 at TARGET.
        assert_eq!(err.variant_name(), "StackDepthMismatch");
    }

    // Both paths must arrive at the join with exactly the same depth.
    // Taken path: PUSH1(1)[+1], JUMPI1[-1] → depth 0 at TARGET.
    // Fall-through: same block as taken, then PUSH1(2)[+1], POP[-1] → depth 0 at TARGET.
    // meet(0, 0) = 0 → no mismatch; HALT at depth 0 → ok.
    #[test]
    fn cond_branch_both_paths_same_depth_ok() {
        let mut b = InstructionBuilder::new();
        let target = b.label();
        let _ = b
            .emit_push(1)
            .emit_jump_if(target) // taken: depth 0 → TARGET
            .emit_push(2) // fall-through: depth 1
            .emit_pop() // depth 0 → TARGET
            .place(target)
            .unwrap()
            .emit_halt();
        let p = b.build().unwrap();
        assert!(check_stack_depth(&p).is_ok());
    }

    #[test]
    fn stack_depth_mismatch_at_join() {
        // Minimal StackDepthMismatch case: JUMPI1 taken brings depth 0,
        // but the fall-through block pushes one extra item before TARGET.
        // PUSH1(5); JUMPI1(0); PUSH1(7); TARGET(0); HALT
        //   Taken:        depth 0 at TARGET.
        //   Fall-through: depth 1 at TARGET.
        //   → StackDepthMismatch.
        let mut b = InstructionBuilder::new();
        let target = b.label();
        let _ = b
            .emit_push(5)
            .emit_jump_if(target) // taken → TARGET with depth 0
            .emit_push(7) // fall-through: depth 1 → TARGET
            .place(target)
            .unwrap()
            .emit_halt();
        let p = b.build().unwrap();
        let err = check_stack_depth(&p).unwrap_err();
        assert_eq!(err.variant_name(), "StackDepthMismatch");
    }

    // --- loop ---

    #[test]
    fn stack_neutral_loop_ok() {
        // PUSH start=0, count=3; loop body pushes and pops (net 0); HALT.
        let p = prog(|b| {
            let _ = b
                .emit_push(0) // start
                .emit_push(3) // count
                .emit_range() // pops 2; depth 0 entering body
                .emit_push(1) // push
                .emit_pop() // pop (net 0 body)
                .emit_next()
                .emit_halt();
        });
        assert!(check_stack_depth(&p).is_ok());
    }

    #[test]
    fn loop_body_push_causes_imbalance() {
        let p = prog(|b| {
            let _ = b
                .emit_push(0)
                .emit_push(3)
                .emit_range()
                .emit_push(99) // unmatched push
                .emit_next()
                .emit_halt();
        });
        let err = check_stack_depth(&p).unwrap_err();
        assert_eq!(err.variant_name(), "LoopStackImbalance");
    }

    #[test]
    fn sclr_inside_loop_body_causes_imbalance() {
        // Entry depth after RANGE = 1; SCLR resets to 0; NEXT sees 0 != 1.
        let p = prog(|b| {
            let _ = b
                .emit_push(1) // extra item -- depth 1 after RANGE's -2
                .emit_push(0)
                .emit_push(3)
                .emit_range()
                .emit_sclr() // resets absolute depth to 0
                .emit_next()
                .emit_halt();
        });
        let err = check_stack_depth(&p).unwrap_err();
        assert_eq!(err.variant_name(), "LoopStackImbalance");
    }

    #[test]
    fn loop_body_pop_causes_imbalance() {
        // Entry depth after RANGE = 1; POP gives 0; NEXT sees 0 != 1.
        let p = prog(|b| {
            let _ = b
                .emit_push(1) // extra item
                .emit_push(0)
                .emit_push(3)
                .emit_range() // pops 2, depth = 1
                .emit_pop() // depth = 0
                .emit_next() // expects 1, got 0
                .emit_halt();
        });
        let err = check_stack_depth(&p).unwrap_err();
        assert_eq!(err.variant_name(), "LoopStackImbalance");
    }

    // --- depth_value lattice ---

    #[test]
    fn depth_value_meet_identity() {
        let x = DepthValue::Known(5);
        assert_eq!(DepthValue::Top.meet(&x), x);
        assert_eq!(x.meet(&DepthValue::Top), x);
    }

    #[test]
    fn depth_value_meet_known_takes_min() {
        let a = DepthValue::Known(3);
        let b = DepthValue::Known(7);
        assert_eq!(a.meet(&b), DepthValue::Known(3));
    }

    #[test]
    fn depth_value_underflow_absorbs() {
        let k = DepthValue::Known(10);
        assert_eq!(DepthValue::Underflow.meet(&k), DepthValue::Underflow);
        assert_eq!(k.meet(&DepthValue::Underflow), DepthValue::Underflow);
    }
}