aver-lang 0.17.2

VM and transpiler for Aver, a statically-typed language designed for AI-assisted development
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
//! Scenario runners — compile entry once per target, run N+warmup
//! iterations timing each. Three targets:
//!
//! - `vm` — in-process `vm::compile_program_with_modules` + `VM::run`.
//! - `wasm-local` — legacy `aver compile --target wasm` (NaN-boxed
//!   wasm32 + wasip1 bridge); wasmtime Engine + Module built once,
//!   each iteration creates a fresh Store + Instance and invokes
//!   `_start`. Mirrors the `cargo bench` shape so VM/WASM numbers
//!   are directly comparable.
//! - `wasm-gc` — `aver compile --target wasm-gc` (engine GC + tail
//!   calls, no NaN-boxing); same in-process wasmtime harness with
//!   `wasm_gc` / `wasm_tail_call` / `wasm_function_references`
//!   enabled. Console / Time imports get bench-mode no-op stubs.
//! - `rust` — `aver compile --target rust` + `cargo build --release`
//!   produces a native binary; each iteration spawns it once. Includes
//!   process spawn overhead (~1-2 ms on macOS) — for programs that
//!   take <1 ms in pure compute the spawn dominates, just like in
//!   the cargo bench measurements.

use std::process::Command;
use std::time::Instant;

use crate::ast::TopLevel;
use crate::bench::manifest::{BenchTarget, Manifest};
use crate::bench::report::{BackendInfo, BenchReport, HostInfo, IterationStats, ScenarioMetadata};
use crate::ir::{PipelineConfig, PipelineStage, TypecheckMode};
use crate::nan_value::Arena;
use crate::source::parse_source;
use crate::vm;

#[derive(Debug)]
pub enum RunError {
    Read(String),
    Parse(String),
    Typecheck(String),
    Compile(String),
    Runtime(String),
    Setup(String),
}

impl std::fmt::Display for RunError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Read(m)
            | Self::Parse(m)
            | Self::Typecheck(m)
            | Self::Compile(m)
            | Self::Runtime(m)
            | Self::Setup(m) => f.write_str(m),
        }
    }
}

/// Run `manifest` against the requested target. Dispatches to the
/// per-target runner; the report shape is identical across targets so
/// downstream tools (`--compare`, NDJSON consumers) don't care which
/// backend produced the numbers.
pub fn run_scenario(manifest: &Manifest, target: BenchTarget) -> Result<BenchReport, RunError> {
    match target {
        BenchTarget::Vm => run_vm(manifest),
        BenchTarget::WasmLocal => run_wasm_local(manifest),
        BenchTarget::WasmGc => run_wasm_gc(manifest),
        BenchTarget::Rust => run_rust(manifest),
    }
}

// ── VM target ──────────────────────────────────────────────────────────

fn run_vm(manifest: &Manifest) -> Result<BenchReport, RunError> {
    let entry_str = manifest.entry.to_string_lossy().into_owned();
    let module_root = manifest
        .entry
        .parent()
        .map(|p| p.to_string_lossy().into_owned())
        .unwrap_or_default();

    let source = std::fs::read_to_string(&manifest.entry)
        .map_err(|e| RunError::Read(format!("{}: {}", entry_str, e)))?;
    let mut items: Vec<TopLevel> = parse_source(&source).map_err(RunError::Parse)?;

    let passes_applied = std::cell::RefCell::new(Vec::<String>::new());
    let pipeline_result = crate::ir::pipeline::run(
        &mut items,
        PipelineConfig {
            typecheck: Some(TypecheckMode::Full {
                base_dir: Some(&module_root),
            }),
            on_after_pass: Some(Box::new(|stage: PipelineStage, _| {
                passes_applied.borrow_mut().push(stage.name().to_string());
            })),
            ..Default::default()
        },
    );
    let tc_result = pipeline_result.typecheck.expect("typecheck was requested");
    if !tc_result.errors.is_empty() {
        let msg = tc_result
            .errors
            .iter()
            .map(|err| format!("error[{}:{}]: {}", err.line, err.col, err.message))
            .collect::<Vec<_>>()
            .join("\n");
        return Err(RunError::Typecheck(msg));
    }

    let mut arena = Arena::new();
    vm::register_service_types(&mut arena);
    let (code, globals) = vm::compile_program_with_modules(
        &items,
        &mut arena,
        Some(&module_root),
        &entry_str,
        pipeline_result.analysis.as_ref(),
    )
    .map_err(|e| RunError::Compile(format!("VM compile: {}", e)))?;

    let mut samples: Vec<f64> = Vec::with_capacity(manifest.iterations);

    for _ in 0..manifest.warmup {
        run_one_vm(&code, &globals, &arena, &manifest.args)?;
    }
    let mut last_response_bytes: Option<usize> = None;
    for _ in 0..manifest.iterations {
        let t = Instant::now();
        let bytes = run_one_vm(&code, &globals, &arena, &manifest.args)?;
        samples.push(t.elapsed().as_secs_f64() * 1000.0);
        last_response_bytes = bytes;
    }

    let policy = crate::ir::NeutralAllocPolicy;
    let visible_allocs = crate::ir::count_alloc_sites_in_program(&items, &policy);
    let mut report = build_report(
        manifest,
        BenchTarget::Vm,
        &samples,
        passes_applied.into_inner(),
        Some(visible_allocs),
    );
    report.response_bytes = last_response_bytes;
    Ok(report)
}

/// Run one bench iteration and return the byte count of `main`'s
/// rendered return value. Aver `main` is conventionally `() -> T` for
/// some `T`; we serialise the resulting `NanValue` through `aver_display`
/// (the same code path `Console.print` uses) and count UTF-8 bytes.
/// `Unit` returns `Some(0)`. `None` is reserved for cases where the
/// value isn't displayable — none of the bench scenarios hit that path
/// today.
fn run_one_vm(
    code: &vm::CodeStore,
    globals: &[crate::nan_value::NanValue],
    arena: &Arena,
    args: &[String],
) -> Result<Option<usize>, RunError> {
    let mut machine = vm::VM::new(code.clone(), globals.to_vec(), arena.clone());
    machine.set_silent_console(true);
    machine.set_cli_args(args.to_vec());
    use crate::nan_value::NanValueConvert;
    let result = machine
        .run()
        .map_err(|e| RunError::Runtime(format!("{}", e)))?;
    // Render `main`'s return value through the same `aver_display` path
    // `Console.print` uses, so `response_bytes` matches what the user
    // would see if their program piped `main` through `print`. The
    // VM's arena is borrowed read-only for the conversion.
    let value = result.to_value(&machine.arena);
    let bytes = crate::value::aver_display(&value).map(|s| s.len());
    Ok(bytes)
}

// ── WASM target ────────────────────────────────────────────────────────

#[cfg(feature = "wasm")]
fn run_wasm_local(manifest: &Manifest) -> Result<BenchReport, RunError> {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU64, Ordering};
    use wasmtime::{Caller, Engine, Linker, Module, Store};

    // Compile the entry to a standalone WASI-bundled `.wasm` once. We
    // shell out to the same `aver compile --target wasm --bridge wasip1`
    // path the CLI uses so the produced bytes are identical to what
    // `aver compile` writes for users — bench measures the production
    // artifact, not a special bench-only build.
    let temp = tempfile::tempdir()
        .map_err(|e| RunError::Setup(format!("create wasm bench tempdir: {}", e)))?;
    let out_dir = temp.path().join("out");
    let aver_bin = std::env::current_exe()
        .map_err(|e| RunError::Setup(format!("locate current aver binary: {}", e)))?;

    let mut compile = Command::new(&aver_bin);
    compile
        .arg("compile")
        .arg(&manifest.entry)
        .arg("--target")
        .arg("wasm")
        .arg("--bridge")
        .arg("wasip1")
        .arg("--name")
        .arg(&manifest.name)
        .arg("-o")
        .arg(&out_dir);
    if let Some(root) = manifest.entry.parent() {
        compile.arg("--module-root").arg(root);
    }
    let status = compile
        .status()
        .map_err(|e| RunError::Setup(format!("spawn aver compile --target wasm: {}", e)))?;
    if !status.success() {
        return Err(RunError::Compile(format!(
            "aver compile --target wasm exited with {}",
            status
        )));
    }

    let wasm_path = out_dir.join(format!("{}.wasm", manifest.name));
    let bytes = std::fs::read(&wasm_path)
        .map_err(|e| RunError::Setup(format!("read {}: {}", wasm_path.display(), e)))?;
    let engine = Engine::default();
    let module = Module::new(&engine, &bytes)
        .map_err(|e| RunError::Setup(format!("wasmtime compile module: {}", e)))?;

    let run_one = |module: &Module, engine: &Engine| -> Result<u64, RunError> {
        let mut store = Store::new(engine, ());
        let mut linker = Linker::new(engine);
        // Aver's wasip1 bridge declares the full wasi_snapshot_preview1
        // import set unconditionally. Bench programs that don't actually
        // touch the host (no fs, no rand) get no-op stubs returning
        // errno 0. The exception is `fd_write`: we read the iovec list
        // from guest memory, sum the byte lengths, write the total back
        // to `nwritten`, and accumulate the count into a per-iteration
        // counter so `BenchReport.response_bytes` can report what the
        // guest tried to write.
        let ws = "wasi_snapshot_preview1";
        let bytes_written = Arc::new(AtomicU64::new(0));
        let bw = bytes_written.clone();
        linker
            .func_wrap(
                ws,
                "fd_write",
                move |mut caller: Caller<'_, ()>,
                      _fd: i32,
                      iovs_ptr: i32,
                      iovs_len: i32,
                      nwritten_ptr: i32|
                      -> i32 {
                    let Some(memory) = caller.get_export("memory").and_then(|e| e.into_memory())
                    else {
                        return 0;
                    };
                    let mut total: u32 = 0;
                    let mut iov_buf = [0u8; 8];
                    for i in 0..iovs_len {
                        let off = (iovs_ptr as usize).saturating_add((i as usize) * 8);
                        if memory.read(&caller, off, &mut iov_buf).is_err() {
                            break;
                        }
                        let len = u32::from_le_bytes(iov_buf[4..8].try_into().unwrap());
                        total = total.saturating_add(len);
                    }
                    let _ = memory.write(&mut caller, nwritten_ptr as usize, &total.to_le_bytes());
                    bw.fetch_add(total as u64, Ordering::Relaxed);
                    0
                },
            )
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "fd_read",
                    |_: Caller<'_, ()>, _: i32, _: i32, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| l.func_wrap(ws, "fd_close", |_: Caller<'_, ()>, _: i32| -> i32 { 0 }))
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "fd_seek",
                    |_: Caller<'_, ()>, _: i32, _: i64, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "fd_fdstat_get",
                    |_: Caller<'_, ()>, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "fd_prestat_get",
                    |_: Caller<'_, ()>, _: i32, _: i32| -> i32 { 8 },
                )
            }) // BADF — no preopens
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "fd_prestat_dir_name",
                    |_: Caller<'_, ()>, _: i32, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "path_open",
                    |_: Caller<'_, ()>,
                     _: i32,
                     _: i32,
                     _: i32,
                     _: i32,
                     _: i32,
                     _: i64,
                     _: i64,
                     _: i32,
                     _: i32|
                     -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "path_filestat_get",
                    |_: Caller<'_, ()>, _: i32, _: i32, _: i32, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "path_remove_directory",
                    |_: Caller<'_, ()>, _: i32, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "path_unlink_file",
                    |_: Caller<'_, ()>, _: i32, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "path_create_directory",
                    |_: Caller<'_, ()>, _: i32, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "path_rename",
                    |_: Caller<'_, ()>, _: i32, _: i32, _: i32, _: i32, _: i32, _: i32| -> i32 {
                        0
                    },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "fd_filestat_get",
                    |_: Caller<'_, ()>, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "fd_readdir",
                    |_: Caller<'_, ()>, _: i32, _: i32, _: i32, _: i64, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "args_sizes_get",
                    |_: Caller<'_, ()>, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(ws, "args_get", |_: Caller<'_, ()>, _: i32, _: i32| -> i32 {
                    0
                })
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "environ_sizes_get",
                    |_: Caller<'_, ()>, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "environ_get",
                    |_: Caller<'_, ()>, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "clock_time_get",
                    |_: Caller<'_, ()>, _: i32, _: i64, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| {
                l.func_wrap(
                    ws,
                    "random_get",
                    |_: Caller<'_, ()>, _: i32, _: i32| -> i32 { 0 },
                )
            })
            .and_then(|l| l.func_wrap(ws, "proc_exit", |_: Caller<'_, ()>, _: i32| {}))
            .and_then(|l| l.func_wrap(ws, "sched_yield", |_: Caller<'_, ()>| -> i32 { 0 }))
            .map_err(|e| RunError::Setup(format!("stub wasi imports: {}", e)))?;
        let instance = linker
            .instantiate(&mut store, module)
            .map_err(|e| RunError::Runtime(format!("instantiate: {}", e)))?;
        let start = instance
            .get_typed_func::<(), ()>(&mut store, "_start")
            .map_err(|e| RunError::Runtime(format!("_start export: {}", e)))?;
        start
            .call(&mut store, ())
            .map_err(|e| RunError::Runtime(format!("invoke _start: {}", e)))?;
        Ok(bytes_written.load(Ordering::Relaxed))
    };

    let mut samples: Vec<f64> = Vec::with_capacity(manifest.iterations);
    for _ in 0..manifest.warmup {
        run_one(&module, &engine)?;
    }
    let mut last_bytes: u64 = 0;
    for _ in 0..manifest.iterations {
        let t = Instant::now();
        last_bytes = run_one(&module, &engine)?;
        samples.push(t.elapsed().as_secs_f64() * 1000.0);
    }

    // Pipeline stages aren't observable through the spawned compile;
    // record the canonical full-pipeline label so the JSON shape stays
    // consistent across targets.
    let passes = canonical_passes();
    let mut report = build_report(
        manifest,
        BenchTarget::WasmLocal,
        &samples,
        passes,
        compute_visible_allocs(manifest),
    );
    // wasm-local response_bytes counts what the guest actually tried to
    // write through `fd_write` (sum of iovec lengths). Differs from the
    // VM target's "rendered return value" semantics — same-target
    // baselines still gate cleanly because we never compare across
    // targets.
    report.response_bytes = Some(last_bytes as usize);
    Ok(report)
}

#[cfg(not(feature = "wasm"))]
fn run_wasm_local(_manifest: &Manifest) -> Result<BenchReport, RunError> {
    Err(RunError::Setup(
        "wasm-local target requires the `wasm` feature; rebuild with `cargo build --features wasm`"
            .to_string(),
    ))
}

// ── wasm-gc target (0.15.3 probe) ──────────────────────────────────────

#[cfg(feature = "wasm")]
fn run_wasm_gc(manifest: &Manifest) -> Result<BenchReport, RunError> {
    use wasmtime::{Config, Engine, Module, Store};

    // Compile once. We shell out to `aver compile --target=wasm-gc`
    // so the produced bytes are identical to what users get — bench
    // measures the production codegen output, not a special path.
    let temp = tempfile::tempdir()
        .map_err(|e| RunError::Setup(format!("create wasm-gc bench tempdir: {}", e)))?;
    let out_dir = temp.path().join("out");
    let aver_bin = std::env::current_exe()
        .map_err(|e| RunError::Setup(format!("locate current aver binary: {}", e)))?;

    let mut compile = Command::new(&aver_bin);
    compile
        .arg("compile")
        .arg(&manifest.entry)
        .arg("--target")
        .arg("wasm-gc")
        .arg("--name")
        .arg(&manifest.name)
        .arg("-o")
        .arg(&out_dir);
    if let Some(root) = manifest.entry.parent() {
        compile.arg("--module-root").arg(root);
    }
    let status = compile
        .status()
        .map_err(|e| RunError::Setup(format!("spawn aver compile --target=wasm-gc: {}", e)))?;
    if !status.success() {
        return Err(RunError::Compile(format!(
            "aver compile --target=wasm-gc exited with {}",
            status
        )));
    }

    let wasm_path = out_dir.join(format!("{}.wasm", manifest.name));
    let bytes = std::fs::read(&wasm_path)
        .map_err(|e| RunError::Setup(format!("read {}: {}", wasm_path.display(), e)))?;

    // Engine config: enable GC + tail calls. wasmtime defaults vary
    // by version; pin them on so the bench doesn't depend on cwd or
    // env vars. Multi-value, bulk memory, etc. are commonly default-
    // on but we set them explicitly to match the CLI's `--wasm gc
    // --wasm tail-call` shape.
    let mut config = Config::new();
    // Match the wasmtime CLI's `--wasm gc --wasm tail-call` runtime
    // configuration. `wasm_gc` requires the cranelift backend to know
    // about gc; `wasm_function_references` is a transitive dep of GC;
    // `wasm_tail_call` enables `return_call(_indirect)` execution.
    // `cranelift_opt_level(Speed)` matches the CLI's default which
    // turns on the codegen paths that handle ref.cast properly.
    config.wasm_gc(true);
    config.wasm_tail_call(true);
    config.wasm_function_references(true);
    config.wasm_reference_types(true);
    config.wasm_multi_value(true);
    config.wasm_bulk_memory(true);
    config.cranelift_opt_level(wasmtime::OptLevel::Speed);
    // Be generous with wasm stack — tail-call return_call should
    // not need extra room, but if cranelift generates a regular
    // call we don't want to fail at 10K iterations. Default is
    // 1MB; bump to 8MB.
    config.max_wasm_stack(8 * 1024 * 1024);
    let engine = Engine::new(&config)
        .map_err(|e| RunError::Setup(format!("wasmtime engine config: {}", e)))?;
    let module = Module::new(&engine, &bytes)
        .map_err(|e| RunError::Setup(format!("wasmtime compile module: {}", e)))?;

    let run_one = |module: &Module, engine: &Engine| -> Result<String, RunError> {
        let mut store = Store::new(engine, ());
        // wasm-gc effects: stub `aver/*` imports so bench mode runs
        // silently. `Console.print(s)` becomes a no-op that drops
        // the String ref. Real hosts (browser, workerd, wasmtime+wasi)
        // wire these to actual stdout / stderr.
        let mut linker = wasmtime::Linker::new(engine);
        // Match the wasm-gc import shape from our codegen: each
        // Console method takes `(ref null $string, i32 caller_fn_idx)`
        // — the i32 was added in 0.16 for per-fn replay attribution.
        // The most permissive wasmtime type for the string ref is
        // `(ref null any)`. Bench-mode stubs drop both args.
        let console_print_ty = wasmtime::FuncType::new(
            engine,
            [
                wasmtime::ValType::Ref(wasmtime::RefType::new(true, wasmtime::HeapType::Any)),
                wasmtime::ValType::I32,
            ],
            std::iter::empty(),
        );
        linker
            .func_new(
                "aver",
                "console_print",
                console_print_ty.clone(),
                |_caller, _params, _results| Ok(()),
            )
            .map_err(|e| RunError::Setup(format!("stub aver/console_print: {}", e)))?;
        for fname in ["console_error", "console_warn"] {
            linker
                .func_new(
                    "aver",
                    fname,
                    console_print_ty.clone(),
                    |_caller, _params, _results| Ok(()),
                )
                .map_err(|e| RunError::Setup(format!("stub aver/{fname}: {e}")))?;
        }
        // Time.unixMs — bench mode uses a fixed value to keep runs
        // deterministic; the production host wires this to the real
        // `Date.now()` / `clock_gettime` equivalent.
        // `time_unix_ms(caller_fn_idx: i32) -> i64` — same per-fn idx
        // tagging as `console_print`. Drop the arg, return 0.
        let time_unix_ms_ty = wasmtime::FuncType::new(
            engine,
            std::iter::once(wasmtime::ValType::I32),
            std::iter::once(wasmtime::ValType::I64),
        );
        linker
            .func_new(
                "aver",
                "time_unix_ms",
                time_unix_ms_ty,
                |_caller, _params, results| {
                    results[0] = wasmtime::Val::I64(0);
                    Ok(())
                },
            )
            .map_err(|e| RunError::Setup(format!("stub aver/time_unix_ms: {}", e)))?;
        let instance = linker
            .instantiate(&mut store, module)
            .map_err(|e| RunError::Runtime(format!("instantiate: {}", e)))?;
        // Try `main: () -> i64` first (Int return), then `() -> f64`
        // (Float), then `() -> ()` (Unit). The order matches the
        // most common bench shapes.
        if let Ok(f) = instance.get_typed_func::<(), i64>(&mut store, "main") {
            let v = f
                .call(&mut store, ())
                .map_err(|e| RunError::Runtime(format!("invoke main: {}", e)))?;
            return Ok(v.to_string());
        }
        if let Ok(f) = instance.get_typed_func::<(), f64>(&mut store, "main") {
            let v = f
                .call(&mut store, ())
                .map_err(|e| RunError::Runtime(format!("invoke main: {}", e)))?;
            return Ok(format!("{v}"));
        }
        if let Ok(f) = instance.get_typed_func::<(), ()>(&mut store, "main") {
            f.call(&mut store, ())
                .map_err(|e| RunError::Runtime(format!("invoke main: {}", e)))?;
            return Ok(String::new());
        }
        // Reference returns (e.g. `main: () -> String`, where `String`
        // is `(ref null $string_array)`): fall back to a dynamic call.
        // Bench mode only needs the timing; rendering the byte content
        // would require introspecting the array element-by-element via
        // the wasmtime GC API. Report `[ref]` as the rendered result.
        if let Some(f) = instance.get_func(&mut store, "main") {
            let n_results = f.ty(&store).results().len();
            let mut out: Vec<wasmtime::Val> = (0..n_results)
                .map(|_| wasmtime::Val::AnyRef(None))
                .collect();
            f.call(&mut store, &[], &mut out)
                .map_err(|e| RunError::Runtime(format!("invoke main: {}", e)))?;
            return Ok(String::from("[ref]"));
        }
        Err(RunError::Runtime("main export must be a function".into()))
    };

    let mut samples: Vec<f64> = Vec::with_capacity(manifest.iterations);
    for _ in 0..manifest.warmup {
        run_one(&module, &engine)?;
    }
    let mut last_result = String::new();
    for _ in 0..manifest.iterations {
        let t = Instant::now();
        last_result = run_one(&module, &engine)?;
        samples.push(t.elapsed().as_secs_f64() * 1000.0);
    }

    let passes = canonical_passes();
    let mut report = build_report(
        manifest,
        BenchTarget::WasmGc,
        &samples,
        passes,
        compute_visible_allocs(manifest),
    );
    // wasm-gc invokes `main` directly. Use the same "rendered return
    // value" semantic as the VM target: count bytes of the decimal
    // representation.
    report.response_bytes = Some(last_result.len());
    Ok(report)
}

#[cfg(not(feature = "wasm"))]
fn run_wasm_gc(_manifest: &Manifest) -> Result<BenchReport, RunError> {
    Err(RunError::Setup(
        "wasm-gc target requires the `wasm` feature; rebuild with `cargo build --features wasm`"
            .to_string(),
    ))
}

// ── Rust target ────────────────────────────────────────────────────────

fn run_rust(manifest: &Manifest) -> Result<BenchReport, RunError> {
    // Compile to a native Rust binary once, then spawn it per iteration.
    // Spawn cost (1-2 ms on macOS) is part of what's measured — the same
    // shape the cargo bench reports, so the numbers stay comparable.
    let temp = tempfile::tempdir()
        .map_err(|e| RunError::Setup(format!("create rust bench tempdir: {}", e)))?;
    let out_dir = temp.path().join("out");
    let aver_bin = std::env::current_exe()
        .map_err(|e| RunError::Setup(format!("locate current aver binary: {}", e)))?;

    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
        .map(std::path::PathBuf::from)
        .ok();
    let mut compile_cmd = Command::new(&aver_bin);
    compile_cmd
        .arg("compile")
        .arg(&manifest.entry)
        .arg("--name")
        .arg(&manifest.name)
        .arg("-o")
        .arg(&out_dir);
    if let Some(root) = manifest.entry.parent() {
        compile_cmd.arg("--module-root").arg(root);
    }
    if let Some(root) = manifest_dir.as_ref() {
        compile_cmd.env("AVER_RUNTIME_PATH", root.join("aver-rt"));
    }
    let status = compile_cmd
        .status()
        .map_err(|e| RunError::Setup(format!("spawn aver compile --target rust: {}", e)))?;
    if !status.success() {
        return Err(RunError::Compile(format!(
            "aver compile (rust) exited with {}",
            status
        )));
    }

    let status = Command::new("cargo")
        .arg("build")
        .arg("--release")
        .current_dir(&out_dir)
        .status()
        .map_err(|e| RunError::Setup(format!("spawn cargo build: {}", e)))?;
    if !status.success() {
        return Err(RunError::Compile(format!(
            "cargo build (rust) exited with {}",
            status
        )));
    }

    let binary = out_dir.join("target/release").join(&manifest.name);
    if !binary.exists() {
        return Err(RunError::Setup(format!(
            "rust target binary not found at {}",
            binary.display()
        )));
    }

    // In-process bench loop: one spawn, the generated binary calls
    // `aver_generated::entry::main` N times under `AVER_BENCH_ITER` and
    // emits one `__bench_iter_ms__: <ms>` line per iter on stderr. The
    // alternative — spawn the binary N times from here — bottoms out at
    // ~2–3 ms macOS process-spawn cost and reports noise on anything
    // under 1 ms (`fib`, `factorial`, `record`). The codegen-side
    // dispatch is gated on the env var, so production builds pay one
    // env-var read at process start and nothing else.
    let output = Command::new(&binary)
        .args(&manifest.args)
        .env("AVER_BENCH_ITER", manifest.iterations.to_string())
        .env("AVER_BENCH_WARMUP", manifest.warmup.to_string())
        .output()
        .map_err(|e| RunError::Runtime(format!("spawn {}: {}", binary.display(), e)))?;
    if !output.status.success() {
        return Err(RunError::Runtime(format!(
            "{} exited with {}: {}",
            binary.display(),
            output.status,
            String::from_utf8_lossy(&output.stderr)
        )));
    }
    let stderr_text = String::from_utf8_lossy(&output.stderr);
    let mut samples: Vec<f64> = Vec::with_capacity(manifest.iterations);
    for line in stderr_text.lines() {
        if let Some(rest) = line.strip_prefix("__bench_iter_ms__: ")
            && let Ok(ms) = rest.trim().parse::<f64>()
        {
            samples.push(ms);
        }
    }
    if samples.is_empty() {
        return Err(RunError::Runtime(format!(
            "rust target produced no `__bench_iter_ms__` lines (stderr: {})",
            &stderr_text[..stderr_text.len().min(200)]
        )));
    }
    let last_bytes = output.stdout.len();

    let passes = canonical_passes();
    let mut report = build_report(
        manifest,
        BenchTarget::Rust,
        &samples,
        passes,
        compute_visible_allocs(manifest),
    );
    // Rust target captures actual stdout from the spawned binary.
    // Same "actual bytes printed" semantics as wasm-local; differs
    // from the VM target's "rendered return value" semantics, but
    // baselines compare same-target only.
    report.response_bytes = Some(last_bytes);
    Ok(report)
}

// ── Shared helpers ─────────────────────────────────────────────────────

fn canonical_passes() -> Vec<String> {
    [
        "tco",
        "typecheck",
        "interp_lower",
        "buffer_build",
        "resolve",
        "last_use",
        "analyze",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect()
}

fn build_report(
    manifest: &Manifest,
    target: BenchTarget,
    samples: &[f64],
    passes_applied: Vec<String>,
    compiler_visible_allocs: Option<usize>,
) -> BenchReport {
    let stats = IterationStats::from_samples(samples);
    BenchReport {
        scenario: ScenarioMetadata {
            name: manifest.name.clone(),
            entry: manifest.entry.to_string_lossy().into_owned(),
            target: target.name().to_string(),
            iterations_count: manifest.iterations,
            warmup_count: manifest.warmup,
        },
        backend: BackendInfo::for_target(target),
        host: HostInfo::capture(),
        iterations: stats,
        response_bytes: None,
        expected_match: None,
        passes_applied,
        compiler_visible_allocs,
    }
}

/// Parse + run pipeline + count IR-level alloc sites. Same numbers
/// across `vm` / `wasm-local` / `rust` since the policy is target-stable
/// (`NeutralAllocPolicy`). `None` only when parse/typecheck fails — in
/// that case the runner already returned an error before calling this,
/// so in practice the field is always populated for successful runs.
fn compute_visible_allocs(manifest: &Manifest) -> Option<usize> {
    let source = std::fs::read_to_string(&manifest.entry).ok()?;
    let mut items: Vec<TopLevel> = parse_source(&source).ok()?;
    let module_root = manifest
        .entry
        .parent()
        .map(|p| p.to_string_lossy().into_owned())
        .unwrap_or_default();
    let res = crate::ir::pipeline::run(
        &mut items,
        PipelineConfig {
            typecheck: Some(TypecheckMode::Full {
                base_dir: Some(&module_root),
            }),
            ..Default::default()
        },
    );
    if let Some(tc) = &res.typecheck
        && !tc.errors.is_empty()
    {
        return None;
    }
    let policy = crate::ir::NeutralAllocPolicy;
    Some(crate::ir::count_alloc_sites_in_program(&items, &policy))
}