miden-assembly 0.28.0

Miden VM assembly 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
use alloc::{boxed::Box, collections::BTreeMap, format, vec::Vec};
use core::fmt::Debug;

use miden_core::{
    advice::AdviceMap,
    mast::{
        BasicBlockNode, BasicBlockNodeBuilder, CallNodeBuilder, DynNodeBuilder,
        ExternalNodeBuilder, JoinNodeBuilder, LoopNodeBuilder, MastForest, MastForestContributor,
        MastForestError, MastNode, MastNodeBuilder, MastNodeId, SplitNodeBuilder,
    },
    utils::IndexVec,
};
use miden_mast_package::debug_info::{
    DebugFunctionIdx, DebugInfoBuilder, DebugSourceAsmOp, DebugSourceInlineCall, DebugSourceNodeId,
    DebugSourceVar, PackageDebugInfo, PackageDebugInfoBuilder, SourceNode,
};

use super::{
    MastNodeRef, PendingMastNode, PendingMastNodeKind, SourceNodeRef,
    compute_operations_and_adjust_mappings,
};
use crate::diagnostics::{Diagnostic, Report, miette};

/// Result of finalizing a [`super::MastForestBuilder`].
pub(crate) struct BuiltMastForest {
    mast_forest: MastForest,
    debug_info: Box<PackageDebugInfo>,
    /// Final node IDs for builder refs retained in the finalized forest.
    node_id_by_ref: BTreeMap<MastNodeRef, MastNodeId>,
    /// Final source occurrence IDs for builder refs retained in the source graph.
    source_id_by_ref: BTreeMap<SourceNodeRef, DebugSourceNodeId>,
}

impl BuiltMastForest {
    #[cfg(test)]
    pub(crate) fn into_parts(self) -> (MastForest, BTreeMap<MastNodeRef, MastNodeId>) {
        (self.mast_forest, self.node_id_by_ref)
    }

    pub(crate) fn into_parts_with_debug_info(
        self,
    ) -> (
        MastForest,
        BTreeMap<MastNodeRef, MastNodeId>,
        Box<PackageDebugInfo>,
        BTreeMap<SourceNodeRef, DebugSourceNodeId>,
    ) {
        (self.mast_forest, self.node_id_by_ref, self.debug_info, self.source_id_by_ref)
    }
}

/// Errors raised while converting builder-owned records into a finalized [`MastForest`].
#[derive(Debug, thiserror::Error, Diagnostic)]
pub(super) enum MastForestBuilderError {
    #[error("pending {node_kind} node {node_ref} has {actual} children, expected {expected}")]
    InvalidChildCount {
        node_ref: MastNodeRef,
        node_kind: &'static str,
        expected: usize,
        actual: usize,
    },
    #[error(
        "pending {node_kind} node {node_ref} references child {child_ref} before the child was finalized"
    )]
    MissingFinalChild {
        node_ref: MastNodeRef,
        node_kind: &'static str,
        child_ref: MastNodeRef,
    },
    #[error("failed to build pending {node_kind} node {node_ref}: {source}")]
    BuildNode {
        node_ref: MastNodeRef,
        node_kind: &'static str,
        #[source]
        source: MastForestError,
    },
    #[error("failed to add finalized MAST node for pending node {node_ref}: {source}")]
    AddNode {
        node_ref: MastNodeRef,
        #[source]
        source: MastForestError,
    },
    #[error("procedure root {root_ref} was not retained in final MAST forest")]
    MissingProcedureRoot { root_ref: MastNodeRef },
    #[error(
        "source occurrence {source_ref} references child {child_ref} before the child was finalized"
    )]
    MissingFinalSourceChild {
        source_ref: SourceNodeRef,
        child_ref: SourceNodeRef,
    },
    #[error(
        "source occurrence {source_ref} references execution node {exec_ref} before it was finalized"
    )]
    MissingFinalSourceExec {
        source_ref: SourceNodeRef,
        exec_ref: MastNodeRef,
    },
    #[error("failed to add source occurrence {source_ref}: {source}")]
    AddSourceNode {
        source_ref: SourceNodeRef,
        #[source]
        source: MastForestError,
    },
    #[error("failed to finalize MAST forest: {source}")]
    FinalizeForest {
        #[source]
        source: MastForestError,
    },
}

/// Stateful finalization helper for converting live builder records into a [`MastForest`].
///
/// Methods are called in finalization order:
/// 1. materialize live nodes;
/// 2. finalize source/debug occurrence rows from builder-local source records;
/// 3. assemble the final forest.
///
/// The helper is private to finalization; keeping the order documented is sufficient because it is
/// not exposed as a reusable API.
pub(super) struct MastForestFinalizer {
    nodes: IndexVec<MastNodeId, MastNode>,
    node_id_by_ref: BTreeMap<MastNodeRef, MastNodeId>,
}

impl MastForestFinalizer {
    pub(super) fn new() -> Self {
        Self {
            nodes: IndexVec::new(),
            node_id_by_ref: BTreeMap::new(),
        }
    }

    pub(super) fn materialize_live_nodes(
        &mut self,
        live_node_refs: &[MastNodeRef],
        pending_nodes: &IndexVec<MastNodeRef, PendingMastNode>,
    ) -> Result<(), Report> {
        for &node_ref in live_node_refs {
            let pending_node = &pending_nodes[node_ref];
            let builder =
                build_pending_node_with_final_ids(pending_node, node_ref, &self.node_id_by_ref)
                    .map_err(Report::new)?;

            let final_node_id =
                MastNodeId::new_unchecked(self.nodes.len().try_into().map_err(|_| {
                    Report::new(MastForestBuilderError::FinalizeForest {
                        source: MastForestError::TooManyNodes,
                    })
                })?);
            let node = builder.build_linked().map_err(|source| {
                Report::new(MastForestBuilderError::BuildNode {
                    node_ref,
                    node_kind: pending_node.kind.name(),
                    source,
                })
            })?;
            let inserted_node_id = self.nodes.push(node).map_err(|_| {
                Report::new(MastForestBuilderError::AddNode {
                    node_ref,
                    source: MastForestError::TooManyNodes,
                })
            })?;
            debug_assert_eq!(inserted_node_id, final_node_id);
            self.node_id_by_ref.insert(node_ref, final_node_id);
        }

        Ok(())
    }

    pub(super) fn into_built_forest(
        self,
        procedure_root_refs: &[MastNodeRef],
        advice_map: AdviceMap,
        debug_info: DebugInfoBuilder<MastNodeRef, SourceNodeRef>,
    ) -> Result<BuiltMastForest, Report> {
        let Self { nodes, mut node_id_by_ref } = self;

        let mut roots = Vec::with_capacity(procedure_root_refs.len());
        for &root_ref in procedure_root_refs {
            let root_id = *node_id_by_ref.get(&root_ref).ok_or_else(|| {
                Report::new(MastForestBuilderError::MissingProcedureRoot { root_ref })
            })?;
            roots.push(root_id);
        }

        let (mast_forest, final_id_remapping) =
            MastForest::from_raw_parts_with_id_map(nodes, roots, advice_map)
                .map_err(|source| Report::new(MastForestBuilderError::FinalizeForest { source }))?;

        // Source/debug references were recorded against builder-local node IDs. Rewrite them to
        // the finalized dense IDs before constructing the debug graph.
        for node_id in node_id_by_ref.values_mut() {
            *node_id = final_id_remapping.get(*node_id).ok_or_else(|| {
                Report::new(MastForestBuilderError::FinalizeForest {
                    source: MastForestError::NodeIdOverflow(*node_id, final_id_remapping.len()),
                })
            })?;
        }

        let (debug_info, source_id_by_ref) =
            Self::finalize_source_graph(&mast_forest, &node_id_by_ref, debug_info)?;

        Ok(BuiltMastForest {
            mast_forest,
            debug_info,
            node_id_by_ref,
            source_id_by_ref,
        })
    }

    fn finalize_source_graph(
        mast_forest: &MastForest,
        node_id_by_ref: &BTreeMap<MastNodeRef, MastNodeId>,
        debug_info: DebugInfoBuilder<MastNodeRef, SourceNodeRef>,
    ) -> Result<(Box<PackageDebugInfo>, BTreeMap<SourceNodeRef, DebugSourceNodeId>), Report> {
        let source_debug_info = debug_info.build();
        let mut debug_info = PackageDebugInfoBuilder::default();
        let tables = source_debug_info
            .merge_tables_into(&mut debug_info)
            .map_err(|error| Report::msg(format!("{error}")))?;
        let live_source_refs = source_debug_info
            .nodes()
            .iter()
            .enumerate()
            .filter_map(|(index, source_node)| {
                node_id_by_ref
                    .contains_key(&source_node.exec_node)
                    .then_some(SourceNodeRef::from(index as u32))
            })
            .collect::<Vec<_>>();

        let mut source_id_by_ref = BTreeMap::new();
        for &source_ref in &live_source_refs {
            source_id_by_ref
                .insert(source_ref, DebugSourceNodeId::from(source_id_by_ref.len() as u32));
        }

        for &source_ref in &live_source_refs {
            let pending_source_node = &source_debug_info[source_ref];
            let exec_node =
                *node_id_by_ref.get(&pending_source_node.exec_node).ok_or_else(|| {
                    Report::new(MastForestBuilderError::MissingFinalSourceExec {
                        source_ref,
                        exec_ref: pending_source_node.exec_node,
                    })
                })?;
            let children = pending_source_node
                .children
                .iter()
                .map(|child_ref| {
                    source_id_by_ref.get(child_ref).copied().ok_or_else(|| {
                        Report::new(MastForestBuilderError::MissingFinalSourceChild {
                            source_ref,
                            child_ref: *child_ref,
                        })
                    })
                })
                .collect::<Result<Vec<_>, Report>>()?;
            let node = &mast_forest[exec_node];
            let (_, asm_op_indices) = compute_operations_and_adjust_mappings(
                node,
                pending_source_node
                    .asm_ops
                    .iter()
                    .map(|asm_op| asm_op.op_idx as usize)
                    .collect(),
            );
            let asm_ops = pending_source_node
                .asm_ops
                .iter()
                .zip(asm_op_indices)
                .map(|(asm_op, op_idx)| {
                    let location_idx = asm_op
                        .location_idx
                        .into_option()
                        .map(|index| remapped(tables.location(index), index, "location"))
                        .transpose()?;
                    let context_name_idx = remapped(
                        tables.string(asm_op.context_name_idx),
                        asm_op.context_name_idx,
                        "string",
                    )?;
                    let op_name_idx =
                        remapped(tables.string(asm_op.op_name_idx), asm_op.op_name_idx, "string")?;
                    Ok(DebugSourceAsmOp::new(
                        u32::try_from(op_idx).unwrap(),
                        location_idx,
                        context_name_idx,
                        op_name_idx,
                        asm_op.num_cycles,
                    ))
                })
                .collect::<Result<Vec<_>, Report>>()?;
            let (_, debug_var_indices) = compute_operations_and_adjust_mappings(
                node,
                pending_source_node.debug_vars.iter().map(|dv| dv.op_idx as usize).collect(),
            );
            let debug_vars = pending_source_node
                .debug_vars
                .iter()
                .zip(debug_var_indices)
                .map(|(debug_var, op_idx)| {
                    let location_idx = debug_var
                        .location_idx
                        .map(|index| remapped(tables.location(index), index, "location"))
                        .transpose()?;
                    let name_idx =
                        remapped(tables.string(debug_var.name_idx), debug_var.name_idx, "string")?;
                    let type_id = debug_var
                        .type_id
                        .map(|index| remapped(tables.ty(index), index, "type"))
                        .transpose()?;
                    Ok(DebugSourceVar {
                        op_idx: u32::try_from(op_idx).unwrap(),
                        name_idx,
                        type_id,
                        arg_idx: debug_var.arg_idx,
                        location_idx,
                        value_location: debug_var.value_location.clone(),
                    })
                })
                .collect::<Result<Vec<_>, Report>>()?;
            let (op_start, op_end) = adjust_source_op_range(
                node,
                pending_source_node.op_start as usize,
                pending_source_node.op_end as usize,
            );
            let inserted_id = debug_info
                .add_node(SourceNode {
                    exec_node,
                    children,
                    op_start: u32::try_from(op_start).unwrap(),
                    op_end: u32::try_from(op_end).unwrap(),
                    asm_ops,
                    debug_vars,
                    inline_calls: vec![],
                })
                .map_err(|_| {
                    Report::new(MastForestBuilderError::AddSourceNode {
                        source_ref,
                        source: MastForestError::TooManyNodes,
                    })
                })?;
            debug_assert_eq!(inserted_id, source_id_by_ref[&source_ref]);
        }

        for source_ref in source_debug_info.roots() {
            let Some(source_id) = source_id_by_ref.get(source_ref).copied() else {
                continue;
            };
            debug_info.add_root(source_id);
        }

        for &source_ref in &live_source_refs {
            let pending_source_node = &source_debug_info[source_ref];
            if pending_source_node.inline_calls.is_empty() {
                continue;
            }

            let source_id = source_id_by_ref[&source_ref];
            let exec_node = debug_info[source_id].exec_node;
            let (_, inline_call_indices) = compute_operations_and_adjust_mappings(
                &mast_forest[exec_node],
                pending_source_node
                    .inline_calls
                    .iter()
                    .map(|inline_call| inline_call.op_idx as usize)
                    .collect(),
            );
            let inline_calls = pending_source_node
                .inline_calls
                .iter()
                .zip(inline_call_indices)
                .map(|(inline_call, op_idx)| {
                    Ok(DebugSourceInlineCall {
                        op_idx: u32::try_from(op_idx).unwrap(),
                        callee_idx: remapped(
                            tables.function(inline_call.callee_idx),
                            inline_call.callee_idx,
                            "function",
                        )?,
                        loc_idx: remapped(
                            tables.location(inline_call.loc_idx),
                            inline_call.loc_idx,
                            "location",
                        )?,
                    })
                })
                .collect::<Result<Vec<_>, Report>>()?;
            debug_info[source_id].inline_calls = inline_calls;
        }

        for (index, function) in source_debug_info.functions().iter().enumerate() {
            let Some(function_source_node) = function.source_node.into_option() else {
                continue;
            };
            let Some(source_id) = source_id_by_ref.get(&function_source_node) else {
                continue;
            };
            let index = tables
                .function(DebugFunctionIdx::from(index as u32))
                .expect("expected function to have been remapped");
            debug_info.set_function_source_node(index, *source_id);
        }

        Ok((debug_info.build(), source_id_by_ref))
    }
}

fn remapped<K, V>(mapped: Option<V>, index: K, table: &str) -> Result<V, Report>
where
    K: Debug,
{
    mapped.ok_or_else(|| {
        Report::msg(format!("debug info references missing {table} index {index:?}"))
    })
}

fn adjust_source_op_range(node: &MastNode, op_start: usize, op_end: usize) -> (usize, usize) {
    if op_start == op_end {
        return (op_start, op_end);
    }

    match node {
        MastNode::Block(block) => {
            let adjusted = BasicBlockNode::adjust_asm_op_indices(
                vec![op_start, op_end - 1],
                block.op_batches(),
            );
            (adjusted[0], adjusted[1] + 1)
        },
        _ => (op_start, op_end),
    }
}

fn build_pending_node_with_final_ids(
    pending_node: &PendingMastNode,
    node_ref: MastNodeRef,
    final_node_id_by_ref: &BTreeMap<MastNodeRef, MastNodeId>,
) -> Result<MastNodeBuilder, MastForestBuilderError> {
    let builder = match &pending_node.kind {
        PendingMastNodeKind::BasicBlock { op_batches } => {
            ensure_child_count(node_ref, pending_node, 0)?;
            MastNodeBuilder::BasicBlock(BasicBlockNodeBuilder::from_op_batches_preserving_digest(
                op_batches.clone(),
                pending_node.digest,
            ))
        },
        PendingMastNodeKind::Join => {
            ensure_child_count(node_ref, pending_node, 2)?;
            let children = final_child_ids::<2>(node_ref, pending_node, final_node_id_by_ref)?;
            MastNodeBuilder::Join(JoinNodeBuilder::new(children).with_digest(pending_node.digest))
        },
        PendingMastNodeKind::Split => {
            ensure_child_count(node_ref, pending_node, 2)?;
            let branches = final_child_ids::<2>(node_ref, pending_node, final_node_id_by_ref)?;
            MastNodeBuilder::Split(SplitNodeBuilder::new(branches).with_digest(pending_node.digest))
        },
        PendingMastNodeKind::Loop => {
            ensure_child_count(node_ref, pending_node, 1)?;
            let [body] = final_child_ids::<1>(node_ref, pending_node, final_node_id_by_ref)?;
            MastNodeBuilder::Loop(LoopNodeBuilder::new(body).with_digest(pending_node.digest))
        },
        PendingMastNodeKind::Call { is_syscall } => {
            ensure_child_count(node_ref, pending_node, 1)?;
            let [callee] = final_child_ids::<1>(node_ref, pending_node, final_node_id_by_ref)?;
            let builder = if *is_syscall {
                CallNodeBuilder::new_syscall(callee)
            } else {
                CallNodeBuilder::new(callee)
            };
            MastNodeBuilder::Call(builder.with_digest(pending_node.digest))
        },
        PendingMastNodeKind::Dyn { is_dyncall } => {
            ensure_child_count(node_ref, pending_node, 0)?;
            let builder = if *is_dyncall {
                DynNodeBuilder::new_dyncall()
            } else {
                DynNodeBuilder::new_dyn()
            };
            MastNodeBuilder::Dyn(builder.with_digest(pending_node.digest))
        },
        PendingMastNodeKind::External => {
            ensure_child_count(node_ref, pending_node, 0)?;
            MastNodeBuilder::External(ExternalNodeBuilder::new(pending_node.digest))
        },
    };

    Ok(builder)
}

fn ensure_child_count(
    node_ref: MastNodeRef,
    pending_node: &PendingMastNode,
    expected: usize,
) -> Result<(), MastForestBuilderError> {
    let actual = pending_node.child_refs.len();
    if actual == expected {
        Ok(())
    } else {
        Err(MastForestBuilderError::InvalidChildCount {
            node_ref,
            node_kind: pending_node.kind.name(),
            expected,
            actual,
        })
    }
}

fn final_child_ids<const N: usize>(
    node_ref: MastNodeRef,
    pending_node: &PendingMastNode,
    final_node_id_by_ref: &BTreeMap<MastNodeRef, MastNodeId>,
) -> Result<[MastNodeId; N], MastForestBuilderError> {
    let node_kind = pending_node.kind.name();
    pending_node
        .child_refs
        .iter()
        .map(|child_ref| {
            final_node_id_by_ref.get(child_ref).copied().ok_or(
                MastForestBuilderError::MissingFinalChild {
                    node_ref,
                    node_kind,
                    child_ref: *child_ref,
                },
            )
        })
        .collect::<Result<Vec<_>, MastForestBuilderError>>()?
        .try_into()
        .map_err(|values: Vec<_>| MastForestBuilderError::InvalidChildCount {
            node_ref,
            node_kind,
            expected: N,
            actual: values.len(),
        })
}