1- use alloc:: vec:: Vec ;
1+ use alloc:: { sync :: Arc , vec:: Vec } ;
22
33use miden_core:: { mast:: MastNodeId , program:: Program } ;
4- use miden_mast_package:: debug_info:: DebugSourceNodeId ;
4+ use miden_mast_package:: debug_info:: { DebugSourceNodeId , PackageDebugInfo } ;
55
66/// A hint for the initial size of the continuation stack.
77const CONTINUATION_STACK_SIZE_HINT : usize = 64 ;
@@ -55,7 +55,10 @@ pub enum Continuation<F> {
5555 ///
5656 /// When we encounter an `ExternalNode`, we enter the corresponding MAST forest directly, and
5757 /// push an `EnterForest` continuation to restore the previous forest when done.
58- EnterForest ( F ) ,
58+ EnterForest {
59+ forest : F ,
60+ package_debug_info : Option < Arc < PackageDebugInfo > > ,
61+ } ,
5962}
6063
6164impl < F > Continuation < F > {
@@ -82,7 +85,23 @@ impl<F> Continuation<F> {
8285 | Respan { node_id : _, batch_index : _ }
8386 | FinishBasicBlock ( _) => true ,
8487
85- EnterForest ( _) => false ,
88+ EnterForest { .. } => false ,
89+ }
90+ }
91+
92+ #[ cfg( any( test, feature = "testing" ) ) ]
93+ pub ( crate ) fn exec_node ( & self ) -> Option < MastNodeId > {
94+ match self {
95+ Self :: StartNode ( node_id)
96+ | Self :: FinishJoin ( node_id)
97+ | Self :: FinishSplit ( node_id)
98+ | Self :: FinishLoop ( node_id)
99+ | Self :: FinishCall ( node_id)
100+ | Self :: FinishDyn ( node_id)
101+ | Self :: ResumeBasicBlock { node_id, .. }
102+ | Self :: Respan { node_id, .. }
103+ | Self :: FinishBasicBlock ( node_id) => Some ( * node_id) ,
104+ Self :: EnterForest { .. } => None ,
86105 }
87106 }
88107}
@@ -123,12 +142,19 @@ impl<F> ContinuationStack<F> {
123142 pub ( crate ) fn new_with_source_node_id (
124143 program : & Program ,
125144 source_node_id : DebugSourceNodeId ,
145+ ) -> Self {
146+ Self :: new_with_optional_source_node_id ( program, Some ( source_node_id) )
147+ }
148+
149+ pub ( crate ) fn new_with_optional_source_node_id (
150+ program : & Program ,
151+ source_node_id : Option < DebugSourceNodeId > ,
126152 ) -> Self {
127153 let mut stack = Vec :: with_capacity ( CONTINUATION_STACK_SIZE_HINT ) ;
128154 stack. push ( Continuation :: StartNode ( program. entrypoint ( ) ) ) ;
129155
130156 let mut source_node_ids = Vec :: with_capacity ( CONTINUATION_STACK_SIZE_HINT ) ;
131- source_node_ids. push ( Some ( source_node_id) ) ;
157+ source_node_ids. push ( source_node_id) ;
132158
133159 Self {
134160 stack,
@@ -159,7 +185,15 @@ impl<F> ContinuationStack<F> {
159185 /// # Arguments
160186 /// * `forest` - The MAST forest to enter
161187 pub fn push_enter_forest ( & mut self , forest : F ) {
162- self . stack . push ( Continuation :: EnterForest ( forest) ) ;
188+ self . push_enter_forest_with_package_debug_info ( forest, None ) ;
189+ }
190+
191+ pub ( crate ) fn push_enter_forest_with_package_debug_info (
192+ & mut self ,
193+ forest : F ,
194+ package_debug_info : Option < Arc < PackageDebugInfo > > ,
195+ ) {
196+ self . stack . push ( Continuation :: EnterForest { forest, package_debug_info } ) ;
163197 self . push_source_node_id ( None ) ;
164198 }
165199
@@ -232,6 +266,19 @@ impl<F> ContinuationStack<F> {
232266 }
233267 }
234268
269+ #[ cfg( any( test, feature = "testing" ) ) ]
270+ pub ( crate ) fn start_tracking_source_nodes (
271+ & mut self ,
272+ next_source_node_id : Option < DebugSourceNodeId > ,
273+ ) {
274+ let mut source_node_ids = Vec :: with_capacity ( self . stack . len ( ) ) ;
275+ source_node_ids. resize ( self . stack . len ( ) , None ) ;
276+ if let Some ( source_node_id) = source_node_ids. last_mut ( ) {
277+ * source_node_id = next_source_node_id;
278+ }
279+ self . source_node_ids = Some ( source_node_ids) ;
280+ }
281+
235282 // PUBLIC ACCESSORS
236283 // --------------------------------------------------------------------------------------------
237284
@@ -260,6 +307,10 @@ impl<F> ContinuationStack<F> {
260307 Some ( ( continuation, source_node_id) )
261308 }
262309
310+ pub ( crate ) fn tracks_source_nodes ( & self ) -> bool {
311+ self . source_node_ids . is_some ( )
312+ }
313+
263314 /// Returns an iterator over the continuations on the stack that will execute in the next clock
264315 /// cycle.
265316 ///
@@ -324,12 +375,15 @@ mod tests {
324375 // Push an incrementing continuation first (bottom of stack)
325376 stack. push_continuation ( Continuation :: StartNode ( MastNodeId :: new_unchecked ( 0 ) ) ) ;
326377 // Push a non-incrementing continuation on top
327- stack. push_continuation ( Continuation :: EnterForest ( Arc :: new ( MastForest :: new ( ) ) ) ) ;
378+ stack. push_continuation ( Continuation :: EnterForest {
379+ forest : Arc :: new ( MastForest :: new ( ) ) ,
380+ package_debug_info : None ,
381+ } ) ;
328382
329383 let result: Vec < _ > = stack. iter_continuations_for_next_clock ( ) . collect ( ) ;
330384 // Should return: EnterForest (non-incrementing), then StartNode (first incrementing)
331385 assert_eq ! ( result. len( ) , 2 ) ;
332- assert ! ( matches!( result[ 0 ] , Continuation :: EnterForest ( _ ) ) ) ;
386+ assert ! ( matches!( result[ 0 ] , Continuation :: EnterForest { .. } ) ) ;
333387 assert ! ( matches!( result[ 1 ] , Continuation :: StartNode ( _) ) ) ;
334388 }
335389
@@ -339,14 +393,20 @@ mod tests {
339393 // Push an incrementing continuation first (bottom of stack)
340394 stack. push_continuation ( Continuation :: StartNode ( MastNodeId :: new_unchecked ( 0 ) ) ) ;
341395 // Push two non-incrementing continuations on top
342- stack. push_continuation ( Continuation :: EnterForest ( Arc :: new ( MastForest :: new ( ) ) ) ) ;
343- stack. push_continuation ( Continuation :: EnterForest ( Arc :: new ( MastForest :: new ( ) ) ) ) ;
396+ stack. push_continuation ( Continuation :: EnterForest {
397+ forest : Arc :: new ( MastForest :: new ( ) ) ,
398+ package_debug_info : None ,
399+ } ) ;
400+ stack. push_continuation ( Continuation :: EnterForest {
401+ forest : Arc :: new ( MastForest :: new ( ) ) ,
402+ package_debug_info : None ,
403+ } ) ;
344404
345405 let result: Vec < _ > = stack. iter_continuations_for_next_clock ( ) . collect ( ) ;
346406 // Should return: EnterForest, EnterForest, StartNode
347407 assert_eq ! ( result. len( ) , 3 ) ;
348- assert ! ( matches!( result[ 0 ] , Continuation :: EnterForest ( _ ) ) ) ;
349- assert ! ( matches!( result[ 1 ] , Continuation :: EnterForest ( _ ) ) ) ;
408+ assert ! ( matches!( result[ 0 ] , Continuation :: EnterForest { .. } ) ) ;
409+ assert ! ( matches!( result[ 1 ] , Continuation :: EnterForest { .. } ) ) ;
350410 assert ! ( matches!( result[ 2 ] , Continuation :: StartNode ( _) ) ) ;
351411 }
352412}
0 commit comments