pd-vm 0.22.5

RustScript bytecode compiler and VM
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
#![allow(dead_code)]
use crate::builtins::BuiltinFunction;
use crate::bytecode::{Value, ValueType, VmMap};
use crate::vm::{HostCallExecOutcome, NumericValue, Vm, VmError, VmResult, logical_shr_i64};
use std::sync::Arc;
use std::sync::{Mutex, OnceLock};

pub(crate) const STATUS_CONTINUE: i32 = 0;
pub(crate) const STATUS_HALTED: i32 = 1;
pub(crate) const STATUS_TRACE_EXIT: i32 = 2;
pub(crate) const STATUS_YIELDED: i32 = 3;
pub(crate) const STATUS_WAITING: i32 = 4;
pub(crate) const STATUS_OUT_OF_FUEL: i32 = 5;
pub(crate) const STATUS_LINKED_CONTINUE: i32 = 6;
pub(crate) const STATUS_ERROR: i32 = -1;

pub(crate) const OP_LDC: i64 = 1;
pub(crate) const OP_ADD: i64 = 2;
pub(crate) const OP_SUB: i64 = 3;
pub(crate) const OP_MUL: i64 = 4;
pub(crate) const OP_DIV: i64 = 5;
pub(crate) const OP_MOD: i64 = 6;
pub(crate) const OP_SHL: i64 = 7;
pub(crate) const OP_SHR: i64 = 8;
pub(crate) const OP_LSHR: i64 = 9;
pub(crate) const OP_AND: i64 = 10;
pub(crate) const OP_OR: i64 = 11;
pub(crate) const OP_NOT: i64 = 12;
pub(crate) const OP_NEG: i64 = 13;
pub(crate) const OP_CEQ: i64 = 14;
pub(crate) const OP_CLT: i64 = 15;
pub(crate) const OP_CGT: i64 = 16;
pub(crate) const OP_POP: i64 = 17;
pub(crate) const OP_DUP: i64 = 18;
pub(crate) const OP_LDLOC: i64 = 19;
pub(crate) const OP_STLOC: i64 = 20;
pub(crate) const OP_CALL: i64 = 21;
pub(crate) const OP_GUARD_FALSE: i64 = 22;
pub(crate) const OP_JUMP: i64 = 23;
pub(crate) const OP_BUILTIN_CALL: i64 = 24;
pub(crate) const OP_GUARD_TRUE: i64 = 25;
pub(crate) const OP_LOOP_IF_FALSE: i64 = 26;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum NativeInterruptMode {
    Fuel,
    Epoch,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct NativeInterruptSettings {
    pub(crate) mode: NativeInterruptMode,
    pub(crate) check_interval: u32,
}

impl NativeInterruptSettings {
    pub(crate) const fn fuel(check_interval: u32) -> Self {
        Self {
            mode: NativeInterruptMode::Fuel,
            check_interval,
        }
    }

    pub(crate) const fn epoch(check_interval: u32) -> Self {
        Self {
            mode: NativeInterruptMode::Epoch,
            check_interval,
        }
    }
}

static GENERIC_BRIDGE_ERROR: OnceLock<Mutex<Option<VmError>>> = OnceLock::new();

fn generic_bridge_error_cell() -> &'static Mutex<Option<VmError>> {
    GENERIC_BRIDGE_ERROR.get_or_init(|| Mutex::new(None))
}

pub(crate) fn store_bridge_error(error: VmError) {
    if let Ok(mut guard) = generic_bridge_error_cell().lock() {
        *guard = Some(error);
    }
}

pub(crate) fn clear_bridge_error() {
    if let Ok(mut guard) = generic_bridge_error_cell().lock() {
        *guard = None;
    }
}

pub(crate) fn take_bridge_error() -> Option<VmError> {
    if let Ok(mut guard) = generic_bridge_error_cell().lock() {
        return guard.take();
    }
    None
}

fn arc_repr_word<T>(value: &Arc<T>) -> usize {
    debug_assert_eq!(std::mem::size_of::<Arc<T>>(), std::mem::size_of::<usize>());
    unsafe { *(value as *const Arc<T> as *const usize) }
}

fn arc_into_repr_ptr<T>(value: Arc<T>) -> *mut u8 {
    let ptr = arc_repr_word(&value) as *mut u8;
    std::mem::forget(value);
    ptr
}

unsafe fn arc_from_repr_ptr<T>(ptr: *mut u8) -> Arc<T> {
    debug_assert_eq!(
        std::mem::size_of::<Arc<T>>(),
        std::mem::size_of::<*mut u8>()
    );
    unsafe { std::mem::transmute_copy(&ptr) }
}

fn run_step<F>(vm: *mut Vm, helper_name: &str, f: F) -> i32
where
    F: FnOnce(&mut Vm) -> VmResult<i32>,
{
    let Some(vm_ref) = (unsafe { vm.as_mut() }) else {
        store_bridge_error(VmError::JitNative(format!(
            "native {helper_name} helper received null vm pointer"
        )));
        return STATUS_ERROR;
    };

    match f(vm_ref) {
        Ok(status) => status,
        Err(err) => {
            store_bridge_error(err);
            STATUS_ERROR
        }
    }
}

fn bridge_name_for_op(op: i64) -> Option<&'static str> {
    match op {
        OP_LDC => Some("ldc"),
        OP_ADD => Some("add"),
        OP_SUB => Some("sub"),
        OP_MUL => Some("mul"),
        OP_DIV => Some("div"),
        OP_MOD => Some("mod"),
        OP_SHL => Some("shl"),
        OP_SHR => Some("shr"),
        OP_LSHR => Some("lshr"),
        OP_AND => Some("and"),
        OP_OR => Some("or"),
        OP_NOT => Some("not"),
        OP_NEG => Some("neg"),
        OP_CEQ => Some("ceq"),
        OP_CLT => Some("clt"),
        OP_CGT => Some("cgt"),
        OP_POP => Some("pop"),
        OP_DUP => Some("dup"),
        OP_LDLOC => Some("ldloc"),
        OP_STLOC => Some("stloc"),
        OP_CALL => Some("call"),
        OP_BUILTIN_CALL => Some("builtin_call"),
        OP_GUARD_FALSE => Some("guard_false"),
        OP_GUARD_TRUE => Some("guard_true"),
        OP_LOOP_IF_FALSE => Some("loop_if_false"),
        OP_JUMP => Some("jump_ip"),
        _ => None,
    }
}

pub(crate) fn helper_entry_address() -> usize {
    pd_vm_native_step as *const () as usize
}

pub(crate) fn interrupt_helper_entry_address() -> usize {
    pd_vm_native_interrupt_tick as *const () as usize
}

pub(crate) fn aot_call_boundary_interrupt_entry_address() -> usize {
    pd_vm_native_aot_call_boundary_interrupt as *const () as usize
}

pub(crate) fn alloc_byte_buffer_entry_address() -> usize {
    pd_vm_native_alloc_byte_buffer as *const () as usize
}

pub(crate) fn alloc_value_buffer_entry_address() -> usize {
    pd_vm_native_alloc_value_buffer as *const () as usize
}

pub(crate) fn shared_string_from_buffer_entry_address() -> usize {
    pd_vm_native_shared_string_from_buffer as *const () as usize
}

pub(crate) fn shared_bytes_from_buffer_entry_address() -> usize {
    pd_vm_native_shared_bytes_from_buffer as *const () as usize
}

pub(crate) fn shared_array_from_buffer_entry_address() -> usize {
    pd_vm_native_shared_array_from_buffer as *const () as usize
}

pub(crate) fn copy_bytes_entry_address() -> usize {
    pd_vm_native_copy_bytes as *const () as usize
}

pub(crate) fn zero_bytes_entry_address() -> usize {
    pd_vm_native_zero_bytes as *const () as usize
}

pub(crate) fn clone_value_to_slot_entry_address() -> usize {
    pd_vm_native_clone_value_to_slot as *const () as usize
}

pub(crate) fn init_null_value_slot_entry_address() -> usize {
    pd_vm_native_init_null_value_slot as *const () as usize
}

pub(crate) fn clear_value_slot_entry_address() -> usize {
    pd_vm_native_clear_value_slot as *const () as usize
}

pub(crate) fn value_eq_entry_address() -> usize {
    pd_vm_native_value_eq as *const () as usize
}

pub(crate) fn write_heap_value_to_slot_entry_address() -> usize {
    pd_vm_native_write_heap_value_to_slot as *const () as usize
}

pub(crate) fn restore_exit_state_entry_address() -> usize {
    pd_vm_native_restore_exit_state as *const () as usize
}

pub(crate) fn map_has_entry_address() -> usize {
    pd_vm_native_map_has as *const () as usize
}

pub(crate) fn map_get_entry_address() -> usize {
    pd_vm_native_map_get as *const () as usize
}

pub(crate) fn helper_entry_offset() -> i32 {
    i32::try_from(std::mem::offset_of!(Vm, native_helper_fn))
        .expect("Vm::native_helper_fn offset must fit i32")
}

pub(crate) fn interrupt_helper_entry_offset() -> i32 {
    i32::try_from(std::mem::offset_of!(Vm, native_interrupt_helper_fn))
        .expect("Vm::native_interrupt_helper_fn offset must fit i32")
}

pub(crate) extern "C" fn pd_vm_native_interrupt_tick(vm: *mut Vm) -> i32 {
    let Some(vm_ref) = (unsafe { vm.as_mut() }) else {
        store_bridge_error(VmError::JitNative(
            "native interrupt helper received null vm pointer".to_string(),
        ));
        return STATUS_ERROR;
    };

    match vm_ref.charge_interrupt_tick() {
        Ok(()) => STATUS_CONTINUE,
        Err(VmError::OutOfFuel { .. } | VmError::EpochDeadlineReached { .. }) => STATUS_OUT_OF_FUEL,
        Err(err) => {
            store_bridge_error(err);
            STATUS_ERROR
        }
    }
}

pub(crate) extern "C" fn pd_vm_native_aot_call_boundary_interrupt(vm: *mut Vm) -> i32 {
    let Some(vm_ref) = (unsafe { vm.as_mut() }) else {
        store_bridge_error(VmError::JitNative(
            "native aot call-boundary interrupt helper received null vm pointer".to_string(),
        ));
        return STATUS_ERROR;
    };

    match vm_ref.charge_aot_call_boundary_interrupt() {
        Ok(()) => STATUS_CONTINUE,
        Err(VmError::OutOfFuel { .. } | VmError::EpochDeadlineReached { .. }) => STATUS_OUT_OF_FUEL,
        Err(err) => {
            store_bridge_error(err);
            STATUS_ERROR
        }
    }
}

pub(crate) extern "C" fn pd_vm_native_alloc_byte_buffer(cap: usize) -> *mut u8 {
    let mut buffer = Vec::<u8>::with_capacity(cap);
    let ptr = buffer.as_mut_ptr();
    std::mem::forget(buffer);
    ptr
}

pub(crate) extern "C" fn pd_vm_native_alloc_value_buffer(cap: usize) -> *mut Value {
    let mut buffer = Vec::<Value>::with_capacity(cap);
    let ptr = buffer.as_mut_ptr();
    std::mem::forget(buffer);
    ptr
}

pub(crate) extern "C" fn pd_vm_native_shared_string_from_buffer(
    ptr: *mut u8,
    len: usize,
    cap: usize,
) -> *mut u8 {
    let bytes = unsafe { Vec::<u8>::from_raw_parts(ptr, len, cap) };
    let text = unsafe { String::from_utf8_unchecked(bytes) };
    arc_into_repr_ptr(Arc::new(text))
}

pub(crate) extern "C" fn pd_vm_native_shared_bytes_from_buffer(
    ptr: *mut u8,
    len: usize,
    cap: usize,
) -> *mut u8 {
    let bytes = unsafe { Vec::<u8>::from_raw_parts(ptr, len, cap) };
    arc_into_repr_ptr(Arc::new(bytes))
}

pub(crate) extern "C" fn pd_vm_native_shared_array_from_buffer(
    ptr: *mut Value,
    len: usize,
    cap: usize,
) -> *mut u8 {
    let values = unsafe { Vec::<Value>::from_raw_parts(ptr, len, cap) };
    arc_into_repr_ptr(Arc::new(values))
}

pub(crate) extern "C" fn pd_vm_native_copy_bytes(dst: *mut u8, src: *const u8, len: usize) {
    unsafe {
        std::ptr::copy_nonoverlapping(src, dst, len);
    }
}

pub(crate) extern "C" fn pd_vm_native_zero_bytes(dst: *mut u8, len: usize) {
    unsafe {
        std::ptr::write_bytes(dst, 0, len);
    }
}

unsafe fn clone_arc_from_repr_ptr<T>(ptr: *mut u8) -> Arc<T> {
    let arc = unsafe { arc_from_repr_ptr::<T>(ptr) };
    let cloned = arc.clone();
    std::mem::forget(arc);
    cloned
}

pub(crate) extern "C" fn pd_vm_native_clone_value_to_slot(
    dst: *mut Value,
    src: *const Value,
) -> i32 {
    if dst.is_null() || src.is_null() {
        store_bridge_error(VmError::JitNative(
            "native clone-value helper received null slot pointer".to_string(),
        ));
        return STATUS_ERROR;
    }

    unsafe {
        std::ptr::write(dst, (*src).clone());
    }
    STATUS_CONTINUE
}

pub(crate) extern "C" fn pd_vm_native_init_null_value_slot(dst: *mut Value) -> i32 {
    if dst.is_null() {
        store_bridge_error(VmError::JitNative(
            "native init-null-slot helper received null pointer".to_string(),
        ));
        return STATUS_ERROR;
    }

    unsafe {
        std::ptr::write(dst, Value::Null);
    }
    STATUS_CONTINUE
}

pub(crate) extern "C" fn pd_vm_native_clear_value_slot(dst: *mut Value) -> i32 {
    if dst.is_null() {
        store_bridge_error(VmError::JitNative(
            "native clear-slot helper received null pointer".to_string(),
        ));
        return STATUS_ERROR;
    }

    unsafe {
        let old = std::mem::replace(&mut *dst, Value::Null);
        drop(old);
    }
    STATUS_CONTINUE
}

pub(crate) extern "C" fn pd_vm_native_value_eq(lhs: *const Value, rhs: *const Value) -> i32 {
    if lhs.is_null() || rhs.is_null() {
        store_bridge_error(VmError::JitNative(
            "native value-eq helper received null pointer".to_string(),
        ));
        return STATUS_ERROR;
    }

    i32::from(unsafe { *lhs == *rhs })
}

pub(crate) extern "C" fn pd_vm_native_write_heap_value_to_slot(
    dst: *mut Value,
    repr_ptr: *mut u8,
    tag: i64,
) -> i32 {
    if dst.is_null() || repr_ptr.is_null() {
        store_bridge_error(VmError::JitNative(
            "native box-heap helper received null pointer".to_string(),
        ));
        return STATUS_ERROR;
    }

    let value = match tag {
        x if x == ValueType::String as i64 => {
            Value::String(unsafe { clone_arc_from_repr_ptr::<String>(repr_ptr) })
        }
        x if x == ValueType::Bytes as i64 => {
            Value::Bytes(unsafe { clone_arc_from_repr_ptr::<Vec<u8>>(repr_ptr) })
        }
        x if x == ValueType::Array as i64 => {
            Value::Array(unsafe { clone_arc_from_repr_ptr::<Vec<Value>>(repr_ptr) })
        }
        x if x == ValueType::Map as i64 => {
            Value::Map(unsafe { clone_arc_from_repr_ptr::<VmMap>(repr_ptr) })
        }
        _ => {
            store_bridge_error(VmError::JitNative(format!(
                "native box-heap helper received unsupported ValueType tag {tag}"
            )));
            return STATUS_ERROR;
        }
    };

    unsafe {
        std::ptr::write(dst, value);
    }
    STATUS_CONTINUE
}

pub(crate) extern "C" fn pd_vm_native_restore_exit_state(
    vm: *mut Vm,
    stack_src: *const Value,
    stack_len: usize,
    locals_src: *const Value,
    locals_len: usize,
    ip: usize,
) -> i32 {
    run_step(vm, "restore_exit_state", |vm| {
        if locals_len != vm.locals.len() {
            return Err(VmError::JitNative(format!(
                "native exit restore locals length mismatch: expected {}, got {}",
                vm.locals.len(),
                locals_len
            )));
        }
        if stack_len != 0 && stack_src.is_null() {
            return Err(VmError::JitNative(
                "native exit restore received null stack buffer".to_string(),
            ));
        }
        if locals_len != 0 && locals_src.is_null() {
            return Err(VmError::JitNative(
                "native exit restore received null locals buffer".to_string(),
            ));
        }

        vm.clear_stack_with_drop_contract();
        vm.stack.reserve(stack_len);
        for index in 0..stack_len {
            let value = unsafe { std::ptr::read(stack_src.add(index)) };
            vm.stack.push(value);
        }

        for index in 0..locals_len {
            let local_index = u8::try_from(index).map_err(|_| {
                VmError::JitNative("native exit restore local index out of range".to_string())
            })?;
            let value = unsafe { std::ptr::read(locals_src.add(index)) };
            vm.store_local_with_drop_contract(local_index, value)?;
        }

        vm.jump_to(ip)?;
        Ok(STATUS_CONTINUE)
    })
}

pub(crate) extern "C" fn pd_vm_native_map_has(repr_ptr: *mut u8, key: *const Value) -> i32 {
    if repr_ptr.is_null() || key.is_null() {
        store_bridge_error(VmError::JitNative(
            "native map-has helper received null pointer".to_string(),
        ));
        return STATUS_ERROR;
    }

    let entries = unsafe { arc_from_repr_ptr::<VmMap>(repr_ptr) };
    let present = entries.get(unsafe { &*key }).is_some();
    std::mem::forget(entries);
    i32::from(present)
}

pub(crate) extern "C" fn pd_vm_native_map_get(
    dst: *mut Value,
    repr_ptr: *mut u8,
    key: *const Value,
) -> i32 {
    if dst.is_null() || repr_ptr.is_null() || key.is_null() {
        store_bridge_error(VmError::JitNative(
            "native map-get helper received null pointer".to_string(),
        ));
        return STATUS_ERROR;
    }

    let entries = unsafe { arc_from_repr_ptr::<VmMap>(repr_ptr) };
    let Some(value) = entries.get(unsafe { &*key }) else {
        std::mem::forget(entries);
        return 0;
    };
    unsafe {
        std::ptr::write(dst, value.clone());
    }
    std::mem::forget(entries);
    1
}

pub(crate) extern "C" fn pd_vm_native_step(vm: *mut Vm, op: i64, a: i64, b: i64, c: i64) -> i32 {
    run_step(vm, "step", |vm| {
        if op == OP_BUILTIN_CALL {
            let bridge_name = u16::try_from(a)
                .ok()
                .and_then(BuiltinFunction::from_call_index)
                .map(BuiltinFunction::name)
                .unwrap_or("builtin_call");
            vm.record_native_bridge_hit(bridge_name);
        } else if let Some(name) = bridge_name_for_op(op) {
            vm.record_native_bridge_hit(name);
        }

        match op {
            OP_LDC => {
                let index = u32::try_from(a)
                    .map_err(|_| VmError::JitNative("ldc index out of range".to_string()))?;
                let value = vm
                    .program
                    .constants
                    .get(index as usize)
                    .cloned()
                    .ok_or(VmError::InvalidConstant(index))?;
                vm.stack.push(value);
                Ok(STATUS_CONTINUE)
            }
            OP_ADD => {
                vm.binary_add_op()?;
                Ok(STATUS_CONTINUE)
            }
            OP_SUB => {
                vm.binary_numeric_op(
                    |lhs, rhs| Ok(lhs.wrapping_sub(rhs)),
                    |lhs, rhs| Ok(lhs - rhs),
                )?;
                Ok(STATUS_CONTINUE)
            }
            OP_MUL => {
                vm.binary_numeric_op(
                    |lhs, rhs| Ok(lhs.wrapping_mul(rhs)),
                    |lhs, rhs| Ok(lhs * rhs),
                )?;
                Ok(STATUS_CONTINUE)
            }
            OP_DIV => {
                vm.binary_numeric_op(crate::vm::checked_int_div, |lhs, rhs| Ok(lhs / rhs))?;
                Ok(STATUS_CONTINUE)
            }
            OP_MOD => {
                vm.binary_numeric_op(crate::vm::checked_int_rem, |lhs, rhs| Ok(lhs % rhs))?;
                Ok(STATUS_CONTINUE)
            }
            OP_SHL => {
                let rhs = vm.pop_shift_amount()?;
                let lhs = vm.pop_int()?;
                vm.stack
                    .push(crate::bytecode::Value::Int(lhs.wrapping_shl(rhs)));
                Ok(STATUS_CONTINUE)
            }
            OP_SHR => {
                let rhs = vm.pop_shift_amount()?;
                let lhs = vm.pop_int()?;
                vm.stack
                    .push(crate::bytecode::Value::Int(lhs.wrapping_shr(rhs)));
                Ok(STATUS_CONTINUE)
            }
            OP_LSHR => {
                let rhs = vm.pop_shift_amount()?;
                let lhs = vm.pop_int()?;
                vm.stack
                    .push(crate::bytecode::Value::Int(logical_shr_i64(lhs, rhs)));
                Ok(STATUS_CONTINUE)
            }
            OP_AND => {
                let rhs = vm.pop_bool()?;
                let lhs = vm.pop_bool()?;
                vm.stack.push(crate::bytecode::Value::Bool(lhs && rhs));
                Ok(STATUS_CONTINUE)
            }
            OP_OR => {
                let rhs = vm.pop_bool()?;
                let lhs = vm.pop_bool()?;
                vm.stack.push(crate::bytecode::Value::Bool(lhs || rhs));
                Ok(STATUS_CONTINUE)
            }
            OP_NOT => {
                vm.unary_not_op()?;
                Ok(STATUS_CONTINUE)
            }
            OP_NEG => {
                let value = vm.pop_numeric()?;
                match value {
                    NumericValue::Int(value) => vm
                        .stack
                        .push(crate::bytecode::Value::Int(value.wrapping_neg())),
                    NumericValue::Float(value) => {
                        vm.stack.push(crate::bytecode::Value::Float(-value))
                    }
                }
                Ok(STATUS_CONTINUE)
            }
            OP_CEQ => {
                let rhs = vm.pop_value()?;
                let lhs = vm.pop_value()?;
                vm.stack.push(crate::bytecode::Value::Bool(lhs == rhs));
                Ok(STATUS_CONTINUE)
            }
            OP_CLT => {
                vm.compare_numeric_op(|lhs, rhs| lhs < rhs, |lhs, rhs| lhs < rhs)?;
                Ok(STATUS_CONTINUE)
            }
            OP_CGT => {
                vm.compare_numeric_op(|lhs, rhs| lhs > rhs, |lhs, rhs| lhs > rhs)?;
                Ok(STATUS_CONTINUE)
            }
            OP_POP => {
                vm.pop_value()?;
                Ok(STATUS_CONTINUE)
            }
            OP_DUP => {
                let value = vm.peek_value()?.clone();
                vm.stack.push(value);
                Ok(STATUS_CONTINUE)
            }
            OP_LDLOC => {
                let index = u8::try_from(a)
                    .map_err(|_| VmError::JitNative("ldloc index out of range".to_string()))?;
                let value = vm
                    .locals
                    .get(index as usize)
                    .cloned()
                    .ok_or(VmError::InvalidLocal(index))?;
                vm.stack.push(value);
                Ok(STATUS_CONTINUE)
            }
            OP_STLOC => {
                let index = u8::try_from(a)
                    .map_err(|_| VmError::JitNative("stloc index out of range".to_string()))?;
                let value = vm.pop_value()?;
                vm.store_local_with_drop_contract(index, value)?;
                Ok(STATUS_CONTINUE)
            }
            OP_CALL => {
                let index = u16::try_from(a)
                    .map_err(|_| VmError::JitNative("call index out of range".to_string()))?;
                let argc = u8::try_from(b)
                    .map_err(|_| VmError::JitNative("call argc out of range".to_string()))?;
                let call_ip = usize::try_from(c)
                    .map_err(|_| VmError::JitNative("call ip out of range".to_string()))?;
                match vm.execute_host_call(index, argc, call_ip)? {
                    HostCallExecOutcome::Returned => Ok(STATUS_CONTINUE),
                    HostCallExecOutcome::Halted => Ok(STATUS_HALTED),
                    HostCallExecOutcome::Yielded => Ok(STATUS_YIELDED),
                    HostCallExecOutcome::Pending(_) => Ok(STATUS_WAITING),
                }
            }
            OP_BUILTIN_CALL => {
                let index = u16::try_from(a).map_err(|_| {
                    VmError::JitNative("builtin call index out of range".to_string())
                })?;
                let argc = u8::try_from(b).map_err(|_| {
                    VmError::JitNative("builtin call argc out of range".to_string())
                })?;
                let call_ip = usize::try_from(c)
                    .map_err(|_| VmError::JitNative("builtin call ip out of range".to_string()))?;
                match vm.execute_host_call(index, argc, call_ip)? {
                    HostCallExecOutcome::Returned => Ok(STATUS_CONTINUE),
                    HostCallExecOutcome::Halted => Ok(STATUS_HALTED),
                    HostCallExecOutcome::Yielded => Ok(STATUS_YIELDED),
                    HostCallExecOutcome::Pending(_) => Ok(STATUS_WAITING),
                }
            }
            OP_GUARD_FALSE => {
                let exit_ip = usize::try_from(a)
                    .map_err(|_| VmError::JitNative("guard exit ip out of range".to_string()))?;
                let condition = vm.pop_bool()?;
                if !condition {
                    vm.jump_to(exit_ip)?;
                    return Ok(STATUS_TRACE_EXIT);
                }
                Ok(STATUS_CONTINUE)
            }
            OP_GUARD_TRUE => {
                let exit_ip = usize::try_from(a)
                    .map_err(|_| VmError::JitNative("guard exit ip out of range".to_string()))?;
                let condition = vm.pop_bool()?;
                if condition {
                    vm.jump_to(exit_ip)?;
                    return Ok(STATUS_TRACE_EXIT);
                }
                Ok(STATUS_CONTINUE)
            }
            OP_LOOP_IF_FALSE => {
                let exit_ip = usize::try_from(a)
                    .map_err(|_| VmError::JitNative("guard exit ip out of range".to_string()))?;
                let condition = vm.pop_bool()?;
                if condition {
                    vm.jump_to(exit_ip)?;
                    return Ok(STATUS_TRACE_EXIT);
                }
                Ok(STATUS_CONTINUE)
            }
            OP_JUMP => {
                let target_ip = usize::try_from(a)
                    .map_err(|_| VmError::JitNative("jump target out of range".to_string()))?;
                vm.jump_to(target_ip)?;
                Ok(STATUS_TRACE_EXIT)
            }
            _ => Err(VmError::JitNative(format!(
                "native step helper received unsupported op id {op}"
            ))),
        }
    })
}