shape-vm 0.3.1

Stack-based bytecode virtual machine for the Shape programming 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
//! Linking pass: converts a content-addressed `Program` into a flat `LinkedProgram`.
//!
//! The linker topologically sorts function blobs by their dependency edges,
//! then flattens per-blob instruction/constant/string pools into merged arrays,
//! remapping operand indices so they reference the correct global positions.

use std::collections::HashMap;

use rayon::prelude::*;

use crate::bytecode::{
    BytecodeProgram, Constant, DebugInfo, Function, FunctionBlob, FunctionHash, Instruction,
    LinkedFunction, LinkedProgram, Operand, Program, SourceMap,
};
use shape_abi_v1::PermissionSet;
use shape_value::{FunctionId, StringId};

// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------

#[derive(Debug, thiserror::Error)]
pub enum LinkError {
    #[error("Missing function blob: {0}")]
    MissingBlob(FunctionHash),
    #[error("Circular dependency detected")]
    CircularDependency,
    #[error("Constant pool overflow: {0} constants exceeds u16 max")]
    ConstantPoolOverflow(usize),
    #[error("String pool overflow: {0} strings exceeds u32 max")]
    StringPoolOverflow(usize),
}

// ---------------------------------------------------------------------------
// Topological sort
// ---------------------------------------------------------------------------

/// Topologically sort blobs so that every dependency appears before
/// the blob that depends on it.  Returns blob hashes in dependency order
/// (leaves first, entry last).
fn topo_sort(program: &Program) -> Result<Vec<FunctionHash>, LinkError> {
    // States: 0 = unvisited, 1 = in-progress, 2 = done
    let mut state: HashMap<FunctionHash, u8> = HashMap::new();
    let mut order: Vec<FunctionHash> = Vec::with_capacity(program.function_store.len());

    fn visit(
        hash: FunctionHash,
        program: &Program,
        state: &mut HashMap<FunctionHash, u8>,
        order: &mut Vec<FunctionHash>,
    ) -> Result<(), LinkError> {
        match state.get(&hash).copied().unwrap_or(0) {
            2 => return Ok(()), // already done
            1 => return Err(LinkError::CircularDependency),
            _ => {}
        }
        state.insert(hash, 1); // mark in-progress

        let blob = program
            .function_store
            .get(&hash)
            .ok_or(LinkError::MissingBlob(hash))?;

        for dep in &blob.dependencies {
            // ZERO is an explicit self-recursion sentinel produced by the compiler.
            // It does not reference a separate blob in function_store.
            if *dep == FunctionHash::ZERO {
                continue;
            }
            visit(*dep, program, state, order)?;
        }

        state.insert(hash, 2); // done
        order.push(hash);
        Ok(())
    }

    // Visit all blobs reachable from the entry point.  We start from entry
    // so unreachable blobs are excluded (they could be present in the store
    // from incremental compilation).
    visit(program.entry, program, &mut state, &mut order)?;

    // Also visit any remaining blobs not reachable from entry
    // (e.g. blobs referenced only from constants or other metadata).
    let remaining: Vec<FunctionHash> = program
        .function_store
        .keys()
        .copied()
        .filter(|h| state.get(h).copied().unwrap_or(0) != 2)
        .collect();
    for hash in remaining {
        visit(hash, program, &mut state, &mut order)?;
    }

    Ok(order)
}

// ---------------------------------------------------------------------------
// Operand remapping
// ---------------------------------------------------------------------------

/// Resolve a blob-relative function dependency index to a globally linked
/// function id. Shared between `Operand::Function` and `Operand::ClosureAlloc`
/// (closure spec H5 merged opcode identity into the operand).
fn remap_fid(
    dep_idx: u16,
    blob: &FunctionBlob,
    current_function_id: usize,
    hash_to_id: &HashMap<FunctionHash, usize>,
    name_to_id: &HashMap<&str, usize>,
) -> u16 {
    if let Some(dep_hash) = blob.dependencies.get(dep_idx as usize) {
        if *dep_hash == FunctionHash::ZERO {
            // ZERO sentinel: self-recursion or mutual recursion.
            if let Some(callee_name) = blob.callee_names.get(dep_idx as usize) {
                if callee_name != &blob.name {
                    // Mutual recursion: look up the target by name.
                    if let Some(target_id) = name_to_id.get(callee_name.as_str()) {
                        *target_id as u16
                    } else {
                        // Fallback: self (shouldn't happen for valid programs).
                        current_function_id as u16
                    }
                } else {
                    // Self-recursion.
                    current_function_id as u16
                }
            } else {
                // No callee name info; assume self-recursion.
                current_function_id as u16
            }
        } else {
            hash_to_id[dep_hash] as u16
        }
    } else {
        // Defensive fallback for blobs emitted with already-global function ids.
        dep_idx
    }
}

/// Remap a single operand given the per-blob base offsets and function
/// hash-to-id mapping.
fn remap_operand(
    operand: Operand,
    const_base: usize,
    string_base: usize,
    blob: &FunctionBlob,
    current_function_id: usize,
    hash_to_id: &HashMap<FunctionHash, usize>,
    name_to_id: &HashMap<&str, usize>,
) -> Operand {
    match operand {
        Operand::Const(i) => Operand::Const((const_base + i as usize) as u16),
        Operand::Property(i) => Operand::Property((string_base + i as usize) as u16),
        Operand::Name(StringId(i)) => Operand::Name(StringId((string_base + i as usize) as u32)),
        Operand::Function(FunctionId(dep_idx)) => {
            Operand::Function(FunctionId(remap_fid(
                dep_idx,
                blob,
                current_function_id,
                hash_to_id,
                name_to_id,
            )))
        }
        Operand::ClosureAlloc { fid: FunctionId(dep_idx), escapes } => {
            Operand::ClosureAlloc {
                fid: FunctionId(remap_fid(
                    dep_idx,
                    blob,
                    current_function_id,
                    hash_to_id,
                    name_to_id,
                )),
                escapes,
            }
        }
        Operand::TypedMethodCall {
            method_id,
            arg_count,
            string_id,
            receiver_type_tag,
        } => Operand::TypedMethodCall {
            method_id,
            arg_count,
            string_id: (string_base + string_id as usize) as u16,
            receiver_type_tag,
        },
        // Unchanged operands:
        Operand::Offset(_)
        | Operand::Local(_)
        | Operand::ModuleBinding(_)
        | Operand::Builtin(_)
        | Operand::Count(_)
        | Operand::ColumnIndex(_)
        | Operand::TypedField { .. }
        | Operand::TypedObjectAlloc { .. }
        | Operand::TypedMerge { .. }
        | Operand::ColumnAccess { .. }
        | Operand::ForeignFunction(_)
        | Operand::MatrixDims { .. }
        | Operand::Width(_)
        | Operand::TypedLocal(_, _)
        | Operand::TypedModuleBinding(_, _)
        | Operand::FieldOffset(_) => operand,
    }
}

// ---------------------------------------------------------------------------
// Constant remapping
// ---------------------------------------------------------------------------

/// Remap function references inside `Constant::Function(idx)`.
/// These reference dependency indices within the blob, not global function IDs,
/// so they need the same treatment as `Operand::Function`.
fn remap_constant(
    constant: &Constant,
    blob: &FunctionBlob,
    current_function_id: usize,
    hash_to_id: &HashMap<FunctionHash, usize>,
    name_to_id: &HashMap<&str, usize>,
) -> Constant {
    match constant {
        Constant::Function(dep_idx) => {
            let dep_idx = *dep_idx as usize;
            if dep_idx < blob.dependencies.len() {
                let dep_hash = blob.dependencies[dep_idx];
                if dep_hash == FunctionHash::ZERO {
                    // ZERO sentinel: self-recursion or mutual recursion.
                    if let Some(callee_name) = blob.callee_names.get(dep_idx) {
                        if callee_name != &blob.name {
                            // Mutual recursion: look up the target by name.
                            if let Some(target_id) = name_to_id.get(callee_name.as_str()) {
                                Constant::Function(*target_id as u16)
                            } else {
                                Constant::Function(current_function_id as u16)
                            }
                        } else {
                            Constant::Function(current_function_id as u16)
                        }
                    } else {
                        Constant::Function(current_function_id as u16)
                    }
                } else {
                    let linked_id = hash_to_id[&dep_hash];
                    Constant::Function(linked_id as u16)
                }
            } else {
                // dep_idx doesn't map to a dependency — keep as-is.
                constant.clone()
            }
        }
        other => other.clone(),
    }
}

// ---------------------------------------------------------------------------
// Public API: link
// ---------------------------------------------------------------------------

/// Threshold for switching from sequential to parallel remap.
/// Below this count, the overhead of Rayon's thread pool is not worth it.
const PARALLEL_THRESHOLD: usize = 50;

/// Per-blob offset information computed in Pass 1.
struct BlobOffsets {
    instruction_base: usize,
    const_base: usize,
    string_base: usize,
}

/// Link a content-addressed `Program` into a flat `LinkedProgram`.
///
/// The linker:
/// 1. Topologically sorts function blobs by dependencies.
/// 2. **Pass 1 (sequential):** Computes cumulative base offsets for each blob
///    and builds the `hash_to_id` reverse index.
/// 3. **Pass 2 (parallel for >50 functions):** Each blob independently remaps
///    its instructions/constants/strings into pre-allocated output arrays at
///    non-overlapping offsets.
/// 4. Builds a `LinkedFunction` table and merged debug info.
pub fn link(program: &Program) -> Result<LinkedProgram, LinkError> {
    let sorted = topo_sort(program)?;

    // Resolve sorted hashes to blob references up-front.
    let blobs: Vec<&FunctionBlob> = sorted
        .iter()
        .map(|h| {
            program
                .function_store
                .get(h)
                .ok_or(LinkError::MissingBlob(*h))
        })
        .collect::<Result<Vec<_>, _>>()?;

    // ------------------------------------------------------------------
    // Pass 1 (sequential): compute base offsets and hash_to_id
    // ------------------------------------------------------------------
    let mut offsets: Vec<BlobOffsets> = Vec::with_capacity(blobs.len());
    let mut hash_to_id: HashMap<FunctionHash, usize> = HashMap::with_capacity(blobs.len());
    let mut name_to_id: HashMap<&str, usize> = HashMap::with_capacity(blobs.len());

    let mut total_instructions: usize = 0;
    let mut total_constants: usize = 0;
    let mut total_strings: usize = 0;

    for (i, blob) in blobs.iter().enumerate() {
        offsets.push(BlobOffsets {
            instruction_base: total_instructions,
            const_base: total_constants,
            string_base: total_strings,
        });
        hash_to_id.insert(blob.content_hash, i);
        name_to_id.insert(&blob.name, i);

        total_instructions += blob.instructions.len();
        total_constants += blob.constants.len();
        total_strings += blob.strings.len();
    }

    // Overflow checks on totals.
    if total_constants > u16::MAX as usize + 1 {
        return Err(LinkError::ConstantPoolOverflow(total_constants));
    }
    if total_strings > u32::MAX as usize + 1 {
        return Err(LinkError::StringPoolOverflow(total_strings));
    }

    // Compute transitive union of all required permissions across all blobs.
    let total_required_permissions = blobs.iter().fold(PermissionSet::pure(), |acc, blob| {
        acc.union(&blob.required_permissions)
    });

    // ------------------------------------------------------------------
    // Pass 2: remap and write into pre-allocated arrays
    // ------------------------------------------------------------------
    let use_parallel = blobs.len() > PARALLEL_THRESHOLD;

    // Pre-allocate output arrays with exact sizes.
    let mut instructions: Vec<Instruction> = Vec::with_capacity(total_instructions);
    let mut constants: Vec<Constant> = Vec::with_capacity(total_constants);
    let mut strings: Vec<String> = Vec::with_capacity(total_strings);

    if use_parallel {
        // SAFETY: We write to non-overlapping regions of the output arrays.
        // Each blob writes to [base..base+len) which is disjoint from all
        // other blobs because the bases are cumulative sums of prior sizes.
        // We use `set_len` after all writes to make the Vecs aware of the data.

        // Extend vecs to their full capacity with uninitialized-safe defaults.
        // For Instructions (Copy type), use zeroed memory via MaybeUninit logic.
        // For Constant/String (non-Copy), we must use a different strategy:
        // collect per-blob results in parallel, then write sequentially.

        // Strategy: parallel map each blob to its (remapped_instructions,
        // remapped_constants, cloned_strings, source_map_entries), then
        // write them into the pre-allocated arrays sequentially (memcpy-fast).
        struct BlobResult {
            instructions: Vec<Instruction>,
            constants: Vec<Constant>,
            strings: Vec<String>,
            source_map: Vec<(usize, u16, u32)>,
        }

        let results: Vec<BlobResult> = blobs
            .par_iter()
            .zip(offsets.par_iter())
            .enumerate()
            .map(|(function_id, (blob, off))| {
                let remapped_instrs: Vec<Instruction> = blob
                    .instructions
                    .iter()
                    .map(|instr| {
                        let remapped_operand = instr.operand.map(|op| {
                            remap_operand(
                                op,
                                off.const_base,
                                off.string_base,
                                blob,
                                function_id,
                                &hash_to_id,
                                &name_to_id,
                            )
                        });
                        Instruction {
                            opcode: instr.opcode,
                            operand: remapped_operand,
                        }
                    })
                    .collect();

                let remapped_consts: Vec<Constant> = blob
                    .constants
                    .iter()
                    .map(|c| remap_constant(c, blob, function_id, &hash_to_id, &name_to_id))
                    .collect();

                let cloned_strings: Vec<String> = blob.strings.clone();

                let source_entries: Vec<(usize, u16, u32)> = blob
                    .source_map
                    .iter()
                    .map(|&(local_offset, file_id, line)| {
                        (off.instruction_base + local_offset, file_id as u16, line)
                    })
                    .collect();

                BlobResult {
                    instructions: remapped_instrs,
                    constants: remapped_consts,
                    strings: cloned_strings,
                    source_map: source_entries,
                }
            })
            .collect();

        // Now write results into the pre-allocated arrays (sequential, but
        // this is just memcpy/move of contiguous data -- very fast).
        let mut merged_line_numbers: Vec<(usize, u16, u32)> = Vec::new();
        for result in results {
            instructions.extend(result.instructions);
            constants.extend(result.constants);
            strings.extend(result.strings);
            merged_line_numbers.extend(result.source_map);
        }

        merged_line_numbers.sort_by_key(|&(offset, _, _)| offset);

        let functions: Vec<LinkedFunction> = blobs
            .iter()
            .zip(offsets.iter())
            .map(|(blob, off)| LinkedFunction {
                blob_hash: blob.content_hash,
                entry_point: off.instruction_base,
                body_length: blob.instructions.len(),
                name: blob.name.clone(),
                arity: blob.arity,
                param_names: blob.param_names.clone(),
                locals_count: blob.locals_count,
                is_closure: blob.is_closure,
                captures_count: blob.captures_count,
                is_async: blob.is_async,
                ref_params: blob.ref_params.clone(),
                ref_mutates: blob.ref_mutates.clone(),
                mutable_captures: blob.mutable_captures.clone(),
                frame_descriptor: blob.frame_descriptor.clone(),
            })
            .collect();

        let debug_info = DebugInfo {
            source_map: SourceMap {
                files: program.debug_info.source_map.files.clone(),
                source_texts: program.debug_info.source_map.source_texts.clone(),
            },
            line_numbers: merged_line_numbers,
            variable_names: program.debug_info.variable_names.clone(),
            source_text: String::new(),
        };

        return Ok(LinkedProgram {
            entry: program.entry,
            instructions,
            constants,
            strings,
            functions,
            hash_to_id,
            debug_info,
            data_schema: program.data_schema.clone(),
            module_binding_names: program.module_binding_names.clone(),
            top_level_locals_count: program.top_level_locals_count,
            top_level_local_storage_hints: program.top_level_local_storage_hints.clone(),
            type_schema_registry: program.type_schema_registry.clone(),
            module_binding_storage_hints: program.module_binding_storage_hints.clone(),
            function_local_storage_hints: program.function_local_storage_hints.clone(),
            top_level_frame: program.top_level_frame.clone(),
            top_level_local_concrete_types: program.top_level_local_concrete_types.clone(),
            function_local_concrete_types: program.function_local_concrete_types.clone(),
            function_return_concrete_types: program.function_return_concrete_types.clone(),
            monomorphized_method_call_sites:
                program.monomorphized_method_call_sites.clone(),
            // cluster-2-cw-IB-class-b: propagate the value-call return-
            // ConcreteType side-table through the parallel-link path so
            // the conduit producer can stamp value-call destinations
            // identically to the sequential path below.
            value_call_return_concrete_types:
                program.value_call_return_concrete_types.clone(),
            // W10 jit-call-method-user-trait-fix (2026-05-17): propagate
            // the operator-trait-dispatch side-table through the
            // parallel-link path so the JIT consumer can re-emit
            // user-type binary/unary operators as method-call IR.
            operator_trait_dispatch_sites:
                program.operator_trait_dispatch_sites.clone(),
            trait_method_symbols: program.trait_method_symbols.clone(),
            foreign_functions: program.foreign_functions.clone(),
            native_struct_layouts: program.native_struct_layouts.clone(),
            total_required_permissions: total_required_permissions.clone(),
            closure_function_layouts: remap_closure_function_layouts(
                program,
                &blobs,
            ),
            trait_vtables: program.trait_vtables.clone(),
            has_imported_const_inline: program.has_imported_const_inline,
            has_w17_marshal_residual: program.has_w17_marshal_residual,
        });
    }

    // ------------------------------------------------------------------
    // Sequential path (≤ PARALLEL_THRESHOLD functions)
    // ------------------------------------------------------------------
    let mut merged_line_numbers: Vec<(usize, u16, u32)> = Vec::new();

    for (function_id, (blob, off)) in blobs.iter().zip(offsets.iter()).enumerate() {
        // Remap and copy instructions.
        for instr in &blob.instructions {
            let remapped_operand = instr.operand.map(|op| {
                remap_operand(
                    op,
                    off.const_base,
                    off.string_base,
                    blob,
                    function_id,
                    &hash_to_id,
                    &name_to_id,
                )
            });
            instructions.push(Instruction {
                opcode: instr.opcode,
                operand: remapped_operand,
            });
        }

        // Merge constants (remap Constant::Function).
        for c in &blob.constants {
            constants.push(remap_constant(
                c,
                blob,
                function_id,
                &hash_to_id,
                &name_to_id,
            ));
        }

        // Merge strings.
        strings.extend(blob.strings.iter().cloned());

        // Merge source map entries.
        for &(local_offset, file_id, line) in &blob.source_map {
            let global_offset = off.instruction_base + local_offset;
            merged_line_numbers.push((global_offset, file_id as u16, line));
        }
    }

    // Sort line numbers by instruction offset for correct binary-search lookup.
    merged_line_numbers.sort_by_key(|&(offset, _, _)| offset);

    let functions: Vec<LinkedFunction> = blobs
        .iter()
        .zip(offsets.iter())
        .map(|(blob, off)| LinkedFunction {
            blob_hash: blob.content_hash,
            entry_point: off.instruction_base,
            body_length: blob.instructions.len(),
            name: blob.name.clone(),
            arity: blob.arity,
            param_names: blob.param_names.clone(),
            locals_count: blob.locals_count,
            is_closure: blob.is_closure,
            captures_count: blob.captures_count,
            is_async: blob.is_async,
            ref_params: blob.ref_params.clone(),
            ref_mutates: blob.ref_mutates.clone(),
            mutable_captures: blob.mutable_captures.clone(),
            frame_descriptor: blob.frame_descriptor.clone(),
        })
        .collect();

    let debug_info = DebugInfo {
        source_map: SourceMap {
            files: program.debug_info.source_map.files.clone(),
            source_texts: program.debug_info.source_map.source_texts.clone(),
        },
        line_numbers: merged_line_numbers,
        variable_names: program.debug_info.variable_names.clone(),
        source_text: String::new(),
    };

    Ok(LinkedProgram {
        entry: program.entry,
        instructions,
        constants,
        strings,
        functions,
        hash_to_id,
        debug_info,
        data_schema: program.data_schema.clone(),
        module_binding_names: program.module_binding_names.clone(),
        top_level_locals_count: program.top_level_locals_count,
        top_level_local_storage_hints: program.top_level_local_storage_hints.clone(),
        type_schema_registry: program.type_schema_registry.clone(),
        module_binding_storage_hints: program.module_binding_storage_hints.clone(),
        function_local_storage_hints: program.function_local_storage_hints.clone(),
        top_level_frame: program.top_level_frame.clone(),
        top_level_local_concrete_types: program.top_level_local_concrete_types.clone(),
        function_local_concrete_types: program.function_local_concrete_types.clone(),
        function_return_concrete_types: program.function_return_concrete_types.clone(),
        monomorphized_method_call_sites:
            program.monomorphized_method_call_sites.clone(),
        // cluster-2-cw-IB-class-b: propagate the value-call return-
        // ConcreteType side-table through the sequential-link path so
        // the conduit producer's value-call destination stamping fires
        // on linked programs.
        value_call_return_concrete_types:
            program.value_call_return_concrete_types.clone(),
        // W10 jit-call-method-user-trait-fix (2026-05-17): propagate
        // the operator-trait-dispatch side-table through the
        // sequential-link path so the JIT consumer's user-type operator
        // dispatch fires on linked programs.
        operator_trait_dispatch_sites:
            program.operator_trait_dispatch_sites.clone(),
        trait_method_symbols: program.trait_method_symbols.clone(),
        foreign_functions: program.foreign_functions.clone(),
        native_struct_layouts: program.native_struct_layouts.clone(),
        total_required_permissions,
        closure_function_layouts: remap_closure_function_layouts(program, &blobs),
        trait_vtables: program.trait_vtables.clone(),
        has_imported_const_inline: program.has_imported_const_inline,
        has_w17_marshal_residual: program.has_w17_marshal_residual,
    })
}

/// Closure spec §14.6 (H6.5): remap the program's name-keyed
/// `closure_function_layouts_by_name` into a post-link-id-indexed Vec.
/// The result is a `Vec<Option<Arc<ClosureLayout>>>` whose position
/// matches `LinkedProgram.functions`'s ordering (i.e. the same index
/// the VM passes as `func_id` to `op_make_closure`).
fn remap_closure_function_layouts(
    program: &Program,
    blobs: &[&FunctionBlob],
) -> Vec<Option<std::sync::Arc<shape_value::v2::closure_layout::ClosureLayout>>> {
    if program.closure_function_layouts_by_name.is_empty() {
        return Vec::new();
    }
    blobs
        .iter()
        .map(|blob| {
            program
                .closure_function_layouts_by_name
                .get(&blob.name)
                .cloned()
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Public API: linked_to_bytecode_program
// ---------------------------------------------------------------------------

/// Convert a `LinkedProgram` back to the legacy `BytecodeProgram` format
/// for backward compatibility with the existing VM executor.
pub fn linked_to_bytecode_program(linked: &LinkedProgram) -> BytecodeProgram {
    let functions: Vec<Function> = linked
        .functions
        .iter()
        .map(|lf| Function {
            name: lf.name.clone(),
            arity: lf.arity,
            param_names: lf.param_names.clone(),
            locals_count: lf.locals_count,
            entry_point: lf.entry_point,
            body_length: lf.body_length,
            is_closure: lf.is_closure,
            captures_count: lf.captures_count,
            is_async: lf.is_async,
            ref_params: lf.ref_params.clone(),
            ref_mutates: lf.ref_mutates.clone(),
            mutable_captures: lf.mutable_captures.clone(),
            frame_descriptor: lf.frame_descriptor.clone(),
            osr_entry_points: Vec::new(),
            mir_data: None,
        })
        .collect();

    BytecodeProgram {
        instructions: linked.instructions.clone(),
        constants: linked.constants.clone(),
        strings: linked.strings.clone(),
        functions,
        has_imported_const_inline: linked.has_imported_const_inline,
        has_w17_marshal_residual: linked.has_w17_marshal_residual,
        debug_info: linked.debug_info.clone(),
        data_schema: linked.data_schema.clone(),
        module_binding_names: linked.module_binding_names.clone(),
        top_level_locals_count: linked.top_level_locals_count,
        top_level_local_storage_hints: linked.top_level_local_storage_hints.clone(),
        type_schema_registry: linked.type_schema_registry.clone(),
        module_binding_storage_hints: linked.module_binding_storage_hints.clone(),
        function_local_storage_hints: linked.function_local_storage_hints.clone(),
        top_level_frame: linked.top_level_frame.clone(),
        top_level_local_concrete_types: linked.top_level_local_concrete_types.clone(),
        function_local_concrete_types: linked.function_local_concrete_types.clone(),
        function_return_concrete_types: linked.function_return_concrete_types.clone(),
        monomorphized_method_call_sites:
            linked.monomorphized_method_call_sites.clone(),
        // cluster-2-cw-IB-class-b: propagate the value-call return-
        // ConcreteType side-table through the LinkedProgram →
        // BytecodeProgram round-trip; the conduit producer consumes
        // this on the post-link BytecodeProgram surface.
        value_call_return_concrete_types:
            linked.value_call_return_concrete_types.clone(),
        // W10 jit-call-method-user-trait-fix (2026-05-17): propagate
        // the operator-trait-dispatch side-table through the
        // LinkedProgram → BytecodeProgram round-trip; the JIT consumer
        // reads this on the post-link BytecodeProgram surface.
        operator_trait_dispatch_sites:
            linked.operator_trait_dispatch_sites.clone(),
        top_level_mir: None,
        compiled_annotations: HashMap::new(),
        trait_method_symbols: linked.trait_method_symbols.clone(),
        expanded_function_defs: HashMap::new(),
        string_index: HashMap::new(),
        foreign_functions: linked.foreign_functions.clone(),
        native_struct_layouts: linked.native_struct_layouts.clone(),
        content_addressed: None,
        function_blob_hashes: linked
            .functions
            .iter()
            .map(|lf| {
                if lf.blob_hash == FunctionHash::ZERO {
                    None
                } else {
                    Some(lf.blob_hash)
                }
            })
            .collect(),
        monomorphization_keys: Vec::new(),
        closure_function_layouts: linked.closure_function_layouts.clone(),
        trait_vtables: linked.trait_vtables.clone(),
    }
}

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

#[cfg(test)]
#[path = "linker_tests.rs"]
mod tests;