fusevm 0.25.0

Language-agnostic bytecode VM with fused superinstructions and a 3-tier Cranelift JIT (linear/block/tracing)
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
#![allow(clippy::approx_constant)]
#![cfg(feature = "jit")]

use fusevm::{ChunkBuilder, JitCompiler, Op, Value};

fn jit_run(ops: &[(Op, u32)]) -> Option<Value> {
    let mut b = ChunkBuilder::new();
    for (op, line) in ops {
        b.emit(op.clone(), *line);
    }
    let chunk = b.build();
    let jit = JitCompiler::new();
    jit.try_run_linear(&chunk, &[])
}

fn jit_expect_int(ops: &[(Op, u32)], expected: i64) {
    match jit_run(ops) {
        Some(Value::Int(n)) => assert_eq!(n, expected, "expected {expected}, got {n}"),
        other => panic!("expected Some(Int({expected})), got {other:?}"),
    }
}

/// Assert the linear tier answers `Value::Bool`.
///
/// The six relational ops, `Op::LogNot`, and `Op::LoadTrue`/`Op::LoadFalse` all
/// push a `Value::Bool` in the interpreter (`cmp_int_fast` / `Op::LogNot` in
/// `src/vm.rs`) and in the AOT tier (`Kind::Bool` in `src/aot.rs`). These
/// assertions used `jit_expect_int` against `Int(0|1)`, which only the linear
/// tier ever produced — its return channel had no boolean kind, so it boxed the
/// 0/1 in the integer register as `Value::Int`.
fn jit_expect_bool(ops: &[(Op, u32)], expected: bool) {
    match jit_run(ops) {
        Some(Value::Bool(b)) => assert_eq!(b, expected, "expected {expected}, got {b}"),
        other => panic!("expected Some(Bool({expected})), got {other:?}"),
    }
}

fn jit_expect_float(ops: &[(Op, u32)], expected: f64) {
    match jit_run(ops) {
        Some(Value::Float(f)) => {
            assert!((f - expected).abs() < 1e-10, "expected {expected}, got {f}");
        }
        other => panic!("expected Some(Float({expected})), got {other:?}"),
    }
}

// ── Arithmetic ──

#[test]
fn jit_add() {
    jit_expect_int(
        &[(Op::LoadInt(40), 1), (Op::LoadInt(2), 1), (Op::Add, 1)],
        42,
    );
}

#[test]
fn jit_sub() {
    jit_expect_int(
        &[(Op::LoadInt(50), 1), (Op::LoadInt(8), 1), (Op::Sub, 1)],
        42,
    );
}

#[test]
fn jit_mul() {
    jit_expect_int(
        &[(Op::LoadInt(6), 1), (Op::LoadInt(7), 1), (Op::Mul, 1)],
        42,
    );
}

#[test]
fn jit_negate() {
    jit_expect_int(&[(Op::LoadInt(42), 1), (Op::Negate, 1)], -42);
}

#[test]
fn jit_inc_dec() {
    jit_expect_int(&[(Op::LoadInt(41), 1), (Op::Inc, 1)], 42);
    jit_expect_int(&[(Op::LoadInt(43), 1), (Op::Dec, 1)], 42);
}

#[test]
fn jit_pow() {
    // `Float(1024.0)`, not `Int(1024)`: `Op::Pow` is always-float in the
    // interpreter (`Value::Float(a.to_float().powf(b.to_float()))`) — see the
    // `jit_pow_float_zero_result_kind_preserved` case below, which already
    // states the same rule. This assertion used to read `Int`, pinning an
    // integer answer only the native tiers ever produced.
    jit_expect_float(
        &[(Op::LoadInt(2), 1), (Op::LoadInt(10), 1), (Op::Pow, 1)],
        1024.0,
    );
}

#[test]
fn jit_float_add() {
    jit_expect_float(
        &[
            (Op::LoadFloat(1.5), 1),
            (Op::LoadFloat(2.5), 1),
            (Op::Add, 1),
        ],
        4.0,
    );
}

#[test]
fn jit_mixed_int_float() {
    jit_expect_float(
        &[(Op::LoadInt(1), 1), (Op::LoadFloat(2.5), 1), (Op::Add, 1)],
        3.5,
    );
}

#[test]
fn jit_chained_arithmetic() {
    // (2 + 3) * 4 = 20
    jit_expect_int(
        &[
            (Op::LoadInt(2), 1),
            (Op::LoadInt(3), 1),
            (Op::Add, 1),
            (Op::LoadInt(4), 1),
            (Op::Mul, 1),
        ],
        20,
    );
}

// ── Comparisons ──

#[test]
fn jit_numeric_comparisons() {
    jit_expect_bool(
        &[(Op::LoadInt(1), 1), (Op::LoadInt(2), 1), (Op::NumLt, 1)],
        true,
    );
    jit_expect_bool(
        &[(Op::LoadInt(2), 1), (Op::LoadInt(1), 1), (Op::NumLt, 1)],
        false,
    );
    jit_expect_bool(
        &[(Op::LoadInt(5), 1), (Op::LoadInt(5), 1), (Op::NumEq, 1)],
        true,
    );
    jit_expect_bool(
        &[(Op::LoadInt(5), 1), (Op::LoadInt(3), 1), (Op::NumGt, 1)],
        true,
    );
    jit_expect_bool(
        &[(Op::LoadInt(5), 1), (Op::LoadInt(5), 1), (Op::NumLe, 1)],
        true,
    );
    jit_expect_bool(
        &[(Op::LoadInt(5), 1), (Op::LoadInt(5), 1), (Op::NumGe, 1)],
        true,
    );
    jit_expect_bool(
        &[(Op::LoadInt(5), 1), (Op::LoadInt(5), 1), (Op::NumNe, 1)],
        false,
    );
}

#[test]
fn jit_spaceship() {
    jit_expect_int(
        &[(Op::LoadInt(1), 1), (Op::LoadInt(2), 1), (Op::Spaceship, 1)],
        -1,
    );
    jit_expect_int(
        &[(Op::LoadInt(5), 1), (Op::LoadInt(5), 1), (Op::Spaceship, 1)],
        0,
    );
    jit_expect_int(
        &[(Op::LoadInt(9), 1), (Op::LoadInt(3), 1), (Op::Spaceship, 1)],
        1,
    );
}

// ── Bitwise ──

#[test]
fn jit_bitwise() {
    jit_expect_int(
        &[
            (Op::LoadInt(0xFF), 1),
            (Op::LoadInt(0x0F), 1),
            (Op::BitAnd, 1),
        ],
        0x0F,
    );
    jit_expect_int(
        &[
            (Op::LoadInt(0xF0), 1),
            (Op::LoadInt(0x0F), 1),
            (Op::BitOr, 1),
        ],
        0xFF,
    );
    jit_expect_int(
        &[
            (Op::LoadInt(0xFF), 1),
            (Op::LoadInt(0xFF), 1),
            (Op::BitXor, 1),
        ],
        0,
    );
    jit_expect_int(
        &[(Op::LoadInt(1), 1), (Op::LoadInt(8), 1), (Op::Shl, 1)],
        256,
    );
    jit_expect_int(
        &[(Op::LoadInt(256), 1), (Op::LoadInt(4), 1), (Op::Shr, 1)],
        16,
    );
}

// ── Stack ops ──

#[test]
fn jit_dup() {
    jit_expect_int(&[(Op::LoadInt(21), 1), (Op::Dup, 1), (Op::Add, 1)], 42);
}

#[test]
fn jit_swap() {
    jit_expect_int(
        &[
            (Op::LoadInt(10), 1),
            (Op::LoadInt(3), 1),
            (Op::Swap, 1),
            (Op::Sub, 1),
        ],
        -7,
    );
}

// ── LogNot ──

#[test]
fn jit_lognot() {
    // `Bool`, not `Int(0|1)`: `Op::LogNot` is
    // `Value::Bool(!val.is_truthy())` in the interpreter (`src/vm.rs`).
    jit_expect_bool(&[(Op::LoadInt(0), 1), (Op::LogNot, 1)], true);
    jit_expect_bool(&[(Op::LoadInt(42), 1), (Op::LogNot, 1)], false);
}

// ── Slot ops ──

#[test]
fn jit_slot_get_set() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::GetSlot(0), 1);
    b.emit(Op::GetSlot(1), 1);
    b.emit(Op::Add, 1);
    let chunk = b.build();
    let jit = JitCompiler::new();
    let slots: Vec<i64> = vec![100, 200];
    match jit.try_run_linear(&chunk, &slots) {
        Some(Value::Int(300)) => {}
        other => panic!("expected Int(300), got {other:?}"),
    }
}

#[test]
fn jit_pre_inc_slot() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::PreIncSlot(0), 1);
    let chunk = b.build();
    let jit = JitCompiler::new();
    let slots: Vec<i64> = vec![41];
    match jit.try_run_linear(&chunk, &slots) {
        Some(Value::Int(42)) => {}
        other => panic!("expected Int(42), got {other:?}"),
    }
}

// ── Constants ──

#[test]
fn jit_load_const_int() {
    let mut b = ChunkBuilder::new();
    let c = b.add_constant(Value::Int(42));
    b.emit(Op::LoadConst(c), 1);
    let chunk = b.build();
    let jit = JitCompiler::new();
    match jit.try_run_linear(&chunk, &[]) {
        Some(Value::Int(42)) => {}
        other => panic!("expected Int(42), got {other:?}"),
    }
}

#[test]
fn jit_load_const_float() {
    let mut b = ChunkBuilder::new();
    let c = b.add_constant(Value::Float(3.14));
    b.emit(Op::LoadConst(c), 1);
    let chunk = b.build();
    let jit = JitCompiler::new();
    match jit.try_run_linear(&chunk, &[]) {
        Some(Value::Float(f)) => assert!((f - 3.14).abs() < 1e-10),
        other => panic!("expected Float(3.14), got {other:?}"),
    }
}

// ── Eligibility ──

#[test]
fn jit_ineligible_string_ops() {
    // Concat is not JIT-eligible in linear mode
    let result = jit_run(&[(Op::LoadInt(1), 1), (Op::LoadInt(2), 1), (Op::Concat, 1)]);
    assert!(result.is_none());
}

#[test]
fn jit_ineligible_exec() {
    let result = jit_run(&[(Op::Exec(0), 1)]);
    assert!(result.is_none());
}

// ── Cache hit (run twice, second should be cached) ──

#[test]
fn jit_cache_hit() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(21), 1);
    b.emit(Op::LoadInt(21), 1);
    b.emit(Op::Add, 1);
    let chunk = b.build();
    let jit = JitCompiler::new();
    let first = jit.try_run_linear(&chunk, &[]);
    let second = jit.try_run_linear(&chunk, &[]);
    assert_eq!(format!("{first:?}"), format!("{second:?}"),);
}

// ── Complex expression ──

#[test]
fn jit_complex_expression() {
    // ((10 + 20) * 3 - 5) = 85
    jit_expect_int(
        &[
            (Op::LoadInt(10), 1),
            (Op::LoadInt(20), 1),
            (Op::Add, 1),
            (Op::LoadInt(3), 1),
            (Op::Mul, 1),
            (Op::LoadInt(5), 1),
            (Op::Sub, 1),
        ],
        85,
    );
}

#[test]
fn jit_bool_constants() {
    // `Op::LoadTrue` pushes `Value::Bool(true)`, not `Int(1)`.
    jit_expect_bool(&[(Op::LoadTrue, 1)], true);
    jit_expect_bool(&[(Op::LoadFalse, 1)], false);
}

#[test]
fn jit_float_spaceship() {
    jit_expect_int(
        &[
            (Op::LoadFloat(1.5), 1),
            (Op::LoadFloat(2.5), 1),
            (Op::Spaceship, 1),
        ],
        -1,
    );
}

#[test]
fn jit_float_negate() {
    jit_expect_float(&[(Op::LoadFloat(3.14), 1), (Op::Negate, 1)], -3.14);
}

#[test]
fn jit_mod_int() {
    // 17 % 5 = 2 — but only if constant non-zero divisor
    jit_expect_int(
        &[(Op::LoadInt(17), 1), (Op::LoadInt(5), 1), (Op::Mod, 1)],
        2,
    );
}

// ── Float-kind preservation (zero-valued and integral-valued float constants) ──
//
// The interpreter keeps `Value::Float` for float operands regardless of the
// value; the JIT must agree. Regression tests for the elisprs bug where an
// integral-valued `LoadFloat` constant (including ±0.0) was lowered as
// `JitTy::Int`, so `LoadFloat(-0.0); Negate` came back as `Int(0)` from the
// JIT tier while the first (interpreted) evaluation returned `Float(0.0)`.
// Bit-exact assertions so ±0.0 sign is pinned too.

fn jit_expect_float_bits(ops: &[(Op, u32)], expected: f64) {
    match jit_run(ops) {
        Some(Value::Float(f)) => assert_eq!(
            f.to_bits(),
            expected.to_bits(),
            "expected Float({expected:?}), got Float({f:?})"
        ),
        other => panic!("expected Some(Float({expected:?})), got {other:?}"),
    }
}

#[test]
fn jit_negate_neg_zero_float_kind_preserved() {
    // (- -0.0) → +0.0, Float — never Int(0).
    jit_expect_float_bits(&[(Op::LoadFloat(-0.0), 1), (Op::Negate, 1)], 0.0);
}

#[test]
fn jit_negate_pos_zero_float_kind_preserved() {
    // (- 0.0) → -0.0, Float — never Int(0).
    jit_expect_float_bits(&[(Op::LoadFloat(0.0), 1), (Op::Negate, 1)], -0.0);
}

#[test]
fn jit_sub_neg_zero_float_kind_preserved() {
    // (- -0.0 0) → -0.0, Float (Int operand promotes, kind stays Float).
    jit_expect_float_bits(
        &[(Op::LoadFloat(-0.0), 1), (Op::LoadInt(0), 1), (Op::Sub, 1)],
        -0.0,
    );
}

#[test]
fn jit_negate_integral_float_kind_preserved() {
    // (- 2.0) → -2.0, Float — an integral-valued float is still a float.
    jit_expect_float_bits(&[(Op::LoadFloat(2.0), 1), (Op::Negate, 1)], -2.0);
}

#[test]
fn jit_add_float_zero_result_kind_preserved() {
    // 1.5 + -1.5 → +0.0, Float.
    jit_expect_float_bits(
        &[
            (Op::LoadFloat(1.5), 1),
            (Op::LoadFloat(-1.5), 1),
            (Op::Add, 1),
        ],
        0.0,
    );
}

#[test]
fn jit_mul_float_zero_result_kind_preserved() {
    // 0.0 * 5.0 → +0.0, Float.
    jit_expect_float_bits(
        &[
            (Op::LoadFloat(0.0), 1),
            (Op::LoadFloat(5.0), 1),
            (Op::Mul, 1),
        ],
        0.0,
    );
}

#[test]
fn jit_div_float_zero_result_kind_preserved() {
    // 0.0 / 2.0 → +0.0, Float (not the exact-int-division fast path).
    jit_expect_float_bits(
        &[
            (Op::LoadFloat(0.0), 1),
            (Op::LoadFloat(2.0), 1),
            (Op::Div, 1),
        ],
        0.0,
    );
}

#[test]
fn jit_mod_float_zero_result_kind_preserved() {
    // 4.0 % 2.0 → +0.0, Float (interpreter uses the float path for Floats).
    jit_expect_float_bits(
        &[
            (Op::LoadFloat(4.0), 1),
            (Op::LoadFloat(2.0), 1),
            (Op::Mod, 1),
        ],
        0.0,
    );
}

#[test]
fn jit_pow_float_zero_result_kind_preserved() {
    // 0.0 ** 3.0 → +0.0, Float (interpreter Pow is always float).
    jit_expect_float_bits(
        &[
            (Op::LoadFloat(0.0), 1),
            (Op::LoadFloat(3.0), 1),
            (Op::Pow, 1),
        ],
        0.0,
    );
}