-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
vm.rs
2006 lines (1655 loc) · 56.8 KB
/
vm.rs
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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::sync::{Arc, Mutex};
use std::sync::atomic::{Ordering, AtomicU64};
use std::mem::{transmute, size_of, align_of};
use std::collections::{HashSet, HashMap};
use std::thread;
use std::ffi::CStr;
use crate::host::*;
use crate::program::Program;
/// Instruction opcodes
/// Note: commonly used upcodes should be in the [0, 127] range (one byte)
/// less frequently used opcodes can take multiple bytes if necessary.
#[allow(non_camel_case_types)]
#[derive(PartialEq, Copy, Clone, Debug)]
#[repr(u8)]
pub enum Op
{
// Halt execution and produce an error
// Panic is zero so that jumping to uninitialized memory causes panic
panic = 0,
// No-op (useful for code patching or patch points)
nop,
// Debugger breakpoint.
// This instruction must be just one byte so it can be patched anywhere.
breakpoint,
// Push common constants (0, 1, 2)
push_0,
push_1,
push_2,
// Push zero n times (e.g. initialize locals)
// push_0n <n:u8>
push_0n,
// push_i8 <imm:i8> (sign-extended)
push_i8,
// push_u32 <imm:u32>
push_u32,
// push_u64 <imm:u64>
push_u64,
// Stack manipulation
pop,
dup,
swap,
// Push the nth-value (indexed from the stack top) on top of the stack
// getn 0 is equivalent to dup
// getn <idx:u8>
getn,
// Pop the stack top and set the nth stack slot from the top to this value
// setn 0 is equivalent to removing the value below the current stack top
// setn <idx:u8>
setn,
// Get the argument count for the current stack frame
get_argc,
// Get the function argument at a given index
// get_arg <idx:u8>
get_arg,
// Get a variadic argument with a dynamic index variable
// get_arg (idx)
get_var_arg,
// Set the function argument at a given index
// set_arg <idx:u8> (value)
set_arg,
// Get the local variable at a given stack slot index
// The index is relative to the base of the stack frame
// get_local <idx:u8>
get_local,
// Set the local variable at a given stack slot index
// The index is relative to the base of the stack frame
// set_local <idx:u8> (value)
set_local,
// 32-bit bitwise operations
and_u32,
or_u32,
xor_u32,
not_u32,
lshift_u32,
rshift_u32,
rshift_i32,
// 32-bit integer arithmetic
add_u32,
sub_u32,
mul_u32,
div_u32,
mod_u32,
div_i32,
mod_i32,
// 32-bit integer comparisons
eq_u32,
ne_u32,
lt_u32,
le_u32,
gt_u32,
ge_u32,
lt_i32,
le_i32,
gt_i32,
ge_i32,
// 64-bit bitwise operations
and_u64,
or_u64,
xor_u64,
not_u64,
lshift_u64,
rshift_u64,
rshift_i64,
// 64-bit integer arithmetic
add_u64,
sub_u64,
mul_u64,
div_u64,
mod_u64,
div_i64,
mod_i64,
// TODO: arithmetic with overflow
// These instructions probably shouldn't jump directly,
// as this would add more branch instructions to the
// instruction set.
// We don't need to worry about compactness.
// add_u64_ovf,
// sub_u64_ovf,
// mul_i64_ovf, // produces two 64-bit words of output
// 64-bit integer comparisons
eq_u64,
ne_u64,
lt_u64,
le_u64,
gt_u64,
ge_u64,
lt_i64,
le_i64,
gt_i64,
ge_i64,
// Integer sign extension
sx_i8_i32,
sx_i8_i64,
sx_i16_i32,
sx_i16_i64,
sx_i32_i64,
// Truncation instructions
trunc_u8,
trunc_u16,
trunc_u32,
// 32-bit floating-point arithmetic
add_f32,
sub_f32,
mul_f32,
div_f32,
// Floating-point math functions
sin_f32,
cos_f32,
tan_f32,
asin_f32,
acos_f32,
atan_f32,
pow_f32,
sqrt_f32,
// 32-bit floating-point comparison instructions
eq_f32,
ne_f32,
lt_f32,
le_f32,
gt_f32,
ge_f32,
// Int/float conversion
i32_to_f32,
i64_to_f32,
f32_to_i32,
// Load a value at a given adress
// store (addr)
load_u8,
load_u16,
load_u32,
load_u64,
// Store a value at a given adress
// store (addr) (value)
store_u8,
store_u16,
store_u32,
store_u64,
/*
// TODO:
// Load from heap at fixed address
// This is used for reading global variables
// The address is multiplied by the data size (x 4 or x8)
// If we save 24 bits for the offset, then that gives us quite a lot
load_global_u64 <addr:u24>
*/
// Atomic load with acquire semantics
// atomic_load (addr)
atomic_load_u64,
// Atomic store with release semantics
// atomic_store (addr) (value)
atomic_store_u64,
// Compare-and-swap
// Uses acquire semantics on success, relaxed on failure.
// Store has relaxed semantics.
// This instruction can be used to implement spin locks.
// atomic_cas (addr) (cmp-val) (store-val)
// Pushes the value found at the memory address
atomic_cas_u64,
// Set thread-local variable
// thread_set <idx:u8> (val)
thread_set,
// Get thread-local variable
// thread_get <idx:u8>
thread_get,
// NOTE: may want to wait for this because it's not RISC,
// but it could help reduce code flag
// NOTE: should this insn have a jump offset built in?
// - no, for consistency, let jz/jnz handle that
// Test flag bits (logical and) with a constant
// This can be used for tag bit tests (e.g. fixnum test)
// Do we want to test just one specific bit, bit_idx:u8?
// test_bit_z <bit_idx:u8>
// test_bit_nz <bit_idx:u8>
// TODO: we should probably have 8-bit offset versions of jump insns
// However, this can wait. Premature optimization.
// jmp_8, jz_8, jnz_8
// Jump to pc offset
// jmp <offset:i32>
jmp,
// Jump to pc offset if stack top is zero
// jz <offset:i32>
jz,
// Jump to pc offset if stack top is not zero
// jnz <offset:i32>
jnz,
// Call a function using the call stack
// call <offset:i32> <num_args:u8> (arg0, arg1, ..., argN)
call,
// Call a function pointer passed as argument
// call <num_args:u8> (arg0, arg1, ..., argN, f_ptr)
call_fp,
// Call into a host function
// For example, to set up a device or to allocate more memory
// syscall <syscall_idx:u16> (arg0, arg1, ..., argN)
syscall,
// Return to caller function or end thread
// ret (value)
ret,
// NOTE: last opcode must have value < 255
// Currently, every opcode is just one byte long,
// and we hope to keep it that way, but the value
// 255 is reserved for future 16-bit opcode extensions.
OP_EXT = 255,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Value(u64);
impl Value
{
pub fn is_null(&self) -> bool {
let Value(val) = *self;
val == 0
}
pub fn as_u8(&self) -> u8 {
let Value(val) = *self;
val as u8
}
pub fn as_u16(&self) -> u16 {
let Value(val) = *self;
val as u16
}
pub fn as_u32(&self) -> u32 {
let Value(val) = *self;
val as u32
}
pub fn as_u64(&self) -> u64 {
let Value(val) = *self;
val as u64
}
pub fn as_usize(&self) -> usize {
let Value(val) = *self;
val as usize
}
pub fn as_i8(&self) -> i8 {
let Value(val) = *self;
val as i8
}
pub fn as_i16(&self) -> i16 {
let Value(val) = *self;
val as i16
}
pub fn as_i32(&self) -> i32 {
let Value(val) = *self;
val as i32
}
pub fn as_i64(&self) -> i64 {
let Value(val) = *self;
val as i64
}
pub fn as_f32(&self) -> f32 {
let Value(val) = *self;
let val = val as i32;
unsafe { transmute(val) }
}
}
impl From<bool> for Value {
fn from(val: bool) -> Self {
Value(if val { 1 } else { 0 })
}
}
impl From<u8> for Value {
fn from(val: u8) -> Self {
Value(val as u64)
}
}
impl From<u16> for Value {
fn from(val: u16) -> Self {
Value(val as u64)
}
}
impl From<u32> for Value {
fn from(val: u32) -> Self {
Value(val as u64)
}
}
impl From<u64> for Value {
fn from(val: u64) -> Self {
Value(val as u64)
}
}
impl From<usize> for Value {
fn from(val: usize) -> Self {
Value(val as u64)
}
}
impl From<i8> for Value {
fn from(val: i8) -> Self {
Value((val as i64) as u64)
}
}
impl From<i32> for Value {
fn from(val: i32) -> Self {
Value(val as u64)
}
}
impl From<i64> for Value {
fn from(val: i64) -> Self {
Value(val as u64)
}
}
impl From<f32> for Value {
fn from(val: f32) -> Self {
let val: u32 = unsafe { transmute(val) };
Value(val as u64)
}
}
struct StackFrame
{
// Previous base pointer at the time of call
prev_bp: usize,
// Return address
ret_addr: usize,
// Argument count
argc: usize,
}
struct MemBlock
{
// Underlying memory block
mem_block: *mut u8,
// Total size of the mapped memory block
mapping_size: usize,
// System page size
page_size: usize,
// Currently visible/accessible size
// This is a box because we need a pointer
// To access this value from threads using MemView
cur_size: Box<usize>,
}
impl MemBlock
{
pub fn new() -> MemBlock
{
// Try to allocate a very large block first (512GB)
let start_size: usize = 512 * 1024 * 1024 * 1024;
let mut alloc_size = start_size;
let mut mem_block;
// Try to allocate a contiguous block of memory that is
// as large as possible
loop {
// PROT_NONE means the data cannot be accessed yet
mem_block = unsafe {libc::mmap(
std::ptr::null_mut(),
alloc_size,
libc::PROT_NONE,
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
-1,
0
)};
if mem_block != libc::MAP_FAILED {
break;
}
println!("mmap failed, trying again");
// Try again with a smaller alloc size
alloc_size /= 2;
assert!(alloc_size > 1);
}
assert!(alloc_size >= 1024);
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
assert!(page_size % 8 == 0);
MemBlock {
mem_block: unsafe { transmute(mem_block) },
mapping_size: alloc_size,
page_size,
cur_size: Box::new(0),
}
}
/// Grow to a new size in bytes
/// This operation is a no-op if the existing size
/// is greater or equal to the requested size
///
/// Note: this operation must be guarded by the VM
pub fn grow(&mut self, mut new_size: usize) -> usize
{
// Round up to a page size multiple
let rem = new_size % self.page_size;
if rem != 0 {
new_size += self.page_size - rem;
}
assert!(new_size % self.page_size == 0);
let cur_size = *self.cur_size;
// Growing the memory block, need to map as read | write
if new_size <= cur_size {
return cur_size;
}
// Compute the address from which to mmap
let map_addr = unsafe { transmute(self.mem_block.add(cur_size)) };
let map_size = new_size - cur_size;
assert!(map_size % self.page_size == 0);
let mem_block = unsafe {libc::mmap(
map_addr,
map_size,
libc::PROT_WRITE | libc::PROT_READ,
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_FIXED,
-1,
0
)};
if mem_block == libc::MAP_FAILED {
panic!();
}
// Update the currently accessible size
*self.cur_size = new_size;
new_size
}
// Create a new thread-local view on this memory block
fn new_view(&self) -> MemView
{
MemView {
mem_block: self.mem_block,
cur_size: &*self.cur_size,
}
}
}
struct MemView
{
// Underlying memory block
mem_block: *mut u8,
// Pointer to size variable from parent MemBlock
cur_size: *const usize,
}
unsafe impl Send for MemView {}
impl MemView
{
pub fn size_bytes(&self) -> usize
{
unsafe { *self.cur_size }
}
/// Get a mutable pointer to an address/offset
pub fn get_ptr_mut<T>(&mut self, addr: usize, num_elems: usize) -> *mut T
{
// Check that the address is within bounds
let cur_size = unsafe { *self.cur_size };
if addr + std::mem::size_of::<T>() * num_elems > cur_size {
panic!("attempting to access memory slice past end of heap");
}
// Check that the address is aligned
if addr & (align_of::<T>() - 1) != 0 {
panic!(
"attempting to access data of type {} at unaligned address {}",
std::any::type_name::<T>(),
addr
);
}
unsafe {
let ptr: *mut u8 = self.mem_block.add(addr);
transmute::<*mut u8 , *mut T>(ptr)
}
}
/// Get a constant pointer to an address/offset
pub fn get_ptr<T>(&self, addr: usize, num_elems: usize) -> *const T
{
// Check that the address is within bounds
let cur_size = unsafe { *self.cur_size };
if addr + std::mem::size_of::<T>() * num_elems > cur_size {
panic!("attempting to access memory slice past end of heap");
}
// Check that the address is aligned
if addr & (size_of::<T>() - 1) != 0 {
panic!(
"attempting to access data of type {} at unaligned address",
std::any::type_name::<T>()
);
}
unsafe {
let ptr: *mut u8 = self.mem_block.add(addr);
transmute::<*mut u8 , *const T>(ptr)
}
}
/// Get a mutable slice inside this memory block
pub fn get_slice_mut<T>(&mut self, addr: usize, num_elems: usize) -> &mut [T]
{
unsafe {
let start_ptr = self.get_ptr_mut(addr, num_elems);
std::slice::from_raw_parts_mut(start_ptr, num_elems)
}
}
/// Read a value at the current PC and then increment the PC
pub fn read_pc<T>(&self, pc: &mut usize) -> T where T: Copy
{
// Check that the address is within bounds
let cur_size = unsafe { *self.cur_size };
if *pc + std::mem::size_of::<T>() > cur_size {
// TODO: output name of type being read
panic!("pc outside of bounds of code space");
}
unsafe {
let val_ptr = transmute::<*const u8 , *const T>(self.mem_block.add(*pc));
*pc += size_of::<T>();
std::ptr::read_unaligned(val_ptr)
}
}
}
pub struct Thread
{
// Thread id
pub id: u64,
// Parent VM
pub vm: Arc<Mutex<VM>>,
// Code memory block
code: MemView,
// Heap memory block
heap: MemView,
// Value stack
stack: Vec<Value>,
// List of stack frames (activation records)
frames: Vec<StackFrame>,
// Thread-local variables
locals: Vec<Value>,
}
impl Thread
{
fn new(tid: u64, vm: Arc<Mutex<VM>>, code: MemView, heap: MemView) -> Self
{
Self {
id: tid,
vm,
code,
heap,
stack: Vec::default(),
frames: Vec::default(),
locals: Vec::default(),
}
}
pub fn push<T>(&mut self, val: T) where Value: From<T>
{
self.stack.push(Value::from(val));
}
pub fn pop(&mut self) -> Value
{
match self.stack.pop() {
Some(val) => val,
None => panic!("tried to pop when the stack is empty")
}
}
/// Get the current size of the heap in bytes
pub fn heap_size(&self) -> usize
{
self.heap.size_bytes()
}
/// Get a mutable pointer to an address/offset in the heap
pub fn get_heap_ptr_mut<T>(&mut self, addr: usize, num_elems: usize) -> *mut T
{
self.heap.get_ptr_mut(addr, num_elems)
}
/// Get a mutable slice to access a memory region in the heap
pub fn get_heap_slice_mut<T>(&mut self, addr: usize, num_elems: usize) -> &mut [T]
{
self.heap.get_slice_mut(addr, num_elems)
}
/// Read an UTF-8 string at a given address in the heap into a Rust string
pub fn get_heap_str(&self, str_ptr: usize) -> &str
{
// Verify that there is a null-terminator for this string
// within the bounds of the heap
let mut str_len = 0;
loop
{
let char_ptr = str_ptr + str_len;
if char_ptr >= self.heap.size_bytes() {
panic!("string is not properly null-terminated");
}
let byte_ptr: *const u8 = self.heap.get_ptr(char_ptr, 1);
if unsafe { *byte_ptr } == 0 {
break;
}
str_len += 1;
}
// Convert the string to a Rust string
let char_ptr = self.heap.get_ptr(str_ptr, str_len);
let c_str = unsafe { CStr::from_ptr(char_ptr as *const i8) };
let rust_str = c_str.to_str().unwrap();
rust_str
}
/// Call a function at a given address
pub fn call(&mut self, callee_pc: u64, args: &[Value]) -> Value
{
assert!(self.stack.len() == 0);
assert!(self.frames.len() == 0);
// Push a new stack frame
self.frames.push(StackFrame {
prev_bp: usize::MAX,
ret_addr: usize::MAX,
argc: args.len(),
});
// Push the arguments on the stack
for arg in args {
self.stack.push(*arg);
}
// The base pointer will point at the first local
let mut bp = self.stack.len();
let mut pc = callee_pc as usize;
// For each instruction to execute
loop
{
let op = self.code.read_pc::<Op>(&mut pc);
//dbg!(op);
match op
{
Op::panic => panic!("execution error, encountered panic opcode"),
Op::nop => continue,
Op::pop => {
self.pop();
}
Op::getn => {
let n = self.code.read_pc::<u8>(&mut pc) as usize;
let val = self.stack[self.stack.len() - (1 + n)];
self.push(val);
}
Op::setn => {
let n = self.code.read_pc::<u8>(&mut pc) as usize;
let val = self.pop();
let len = self.stack.len();
self.stack[len - (1 + n)] = val;
}
Op::dup => {
let val = self.pop();
self.push(val);
self.push(val);
}
Op::swap => {
let a = self.pop();
let b = self.pop();
self.push(a);
self.push(b);
}
Op::get_arg => {
let idx = self.code.read_pc::<u8>(&mut pc) as usize;
let argc = self.frames[self.frames.len() - 1].argc;
if idx >= argc {
panic!("invalid index in get_arg, idx={}, argc={}", idx, argc);
}
// Last argument is at bp - 1 (if there are arguments)
let stack_idx = (bp - argc) + idx;
self.push(self.stack[stack_idx]);
}
Op::get_var_arg => {
let idx = self.pop().as_usize();
let argc = self.frames[self.frames.len() - 1].argc;
if idx >= argc {
panic!("invalid index in get_arg, idx={}, argc={}", idx, argc);
}
// Last argument is at bp - 1 (if there are arguments)
let stack_idx = (bp - argc) + idx;
self.push(self.stack[stack_idx]);
}
Op::set_arg => {
let idx = self.code.read_pc::<u8>(&mut pc) as usize;
let argc = self.frames[self.frames.len() - 1].argc;
if idx >= argc {
panic!("invalid index in set_arg, idx={}, argc={}", idx, argc);
}
// Last argument is at bp - 1 (if there are arguments)
let stack_idx = (bp - argc) + idx;
let val = self.pop();
self.stack[stack_idx] = val;
}
Op::get_local => {
let idx = self.code.read_pc::<u8>(&mut pc) as usize;
if bp + idx >= self.stack.len() {
panic!("invalid index {} in get_local", idx);
}
self.push(self.stack[bp + idx]);
}
Op::set_local => {
let idx = self.code.read_pc::<u8>(&mut pc) as usize;
let val = self.pop();
if bp + idx >= self.stack.len() {
panic!("invalid index in set_local");
}
self.stack[bp + idx] = val;
}
Op::push_0 => {
self.push(0);
}
Op::push_1 => {
self.push(1);
}
Op::push_2 => {
self.push(2);
}
Op::push_0n => {
let n = self.code.read_pc::<u8>(&mut pc);
self.stack.resize(self.stack.len() + n as usize, Value::from(0));
}
Op::push_i8 => {
let val = self.code.read_pc::<i8>(&mut pc);
self.push(val);
}
Op::push_u32 => {
let val = self.code.read_pc::<u32>(&mut pc);
self.push(val);
}
Op::push_u64 => {
let val = self.code.read_pc::<u64>(&mut pc);
self.push(val);
}
Op::and_u32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(v0.as_u32() & v1.as_u32());
}
Op::or_u32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(v0.as_u32() | v1.as_u32());
}
Op::xor_u32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(v0.as_u32() ^ v1.as_u32());
}
Op::not_u32 => {
let v0 = self.pop();
self.push(!v0.as_u32());
}
Op::lshift_u32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(
v0.as_u32().wrapping_shl(v1.as_u32())
);
}
Op::rshift_u32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(
v0.as_u32().wrapping_shr(v1.as_u32())
);
}
Op::rshift_i32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(
v0.as_i32().wrapping_shr(v1.as_u32())
);
}
Op::add_u32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(
v0.as_u32().wrapping_add(v1.as_u32())
);
}
Op::sub_u32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(
v0.as_u32().wrapping_sub(v1.as_u32())
);
}
Op::mul_u32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(
v0.as_u32().wrapping_mul(v1.as_u32())
);
}
// Division by zero will cause a panic (this is intentional)
Op::div_u32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(
v0.as_u32() / v1.as_u32()
);
}
// Division by zero will cause a panic (this is intentional)
Op::mod_u32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(
v0.as_u32() % v1.as_u32()
);
}
// Division by zero will cause a panic (this is intentional)
Op::div_i32 => {
let v1 = self.pop();
let v0 = self.pop();
self.push(
v0.as_i32() / v1.as_i32()
);
}
// Division by zero will cause a panic (this is intentional)
Op::mod_i32 => {
let v1 = self.pop();