def trunc_sat(vm, args): if args == 0: # i32.trunc_sat_f32_s v = __trunc_sat_s(vm.pop_f32(), 32) vm.push_s32(int32(v)) elif args == 1: # i32.trunc_sat_f32_u v = __trunc_sat_u(vm.pop_f32(), 32) vm.push_u32(uint32(v)) elif args == 2: # i32.trunc_sat_f64_s v = __trunc_sat_s(vm.pop_f64(), 32) vm.push_s32(int32(v)) elif args == 3: # i32.trunc_sat_f64_u v = __trunc_sat_u(vm.pop_f64(), 32) vm.push_u32(uint32(v)) elif args == 4: # i64.trunc_sat_f32_s v = __trunc_sat_s(vm.pop_f32(), 64) vm.push_s64(v) elif args == 5: # i64.trunc_sat_f32_u v = __trunc_sat_u(vm.pop_f32(), 64) vm.push_u64(v) elif args == 6: # i64.trunc_sat_f64_s v = __trunc_sat_s(vm.pop_f64(), 64) vm.push_s64(v) elif args == 7: # i64.trunc_sat_f64_u v = __trunc_sat_u(vm.pop_f64(), 64) vm.push_u64(v) else: raise Exception("unreachable")
def i32_trunc_f64u(vm, _): f = math.trunc(vm.pop_f64()) if f > __MaxUint32 or f < 0: raise ErrIntOverflow if math.isnan(f): raise ErrConvertToInt vm.push_u32(uint32(f))
def call(vm, args): idx = uint32(args) imported_func_count = len(vm.module.import_sec) if idx < imported_func_count: # hack! call_assert_func(vm, args) else: call_internal_func(vm, idx - imported_func_count)
def global_get(vm, args): idx = uint32(args) val = vm.globals[idx].get_as_u64() vm.push_u64(val)
def local_tee(vm, args): """用重定向操作符>把某个命令的输出重定向到文件里""" idx = uint32(args) val = vm.pop_u64() vm.push_u64(val) vm.set_operand(vm.local_0_idx + idx, val)
def local_set(vm, args): """设置局部变量的值""" idx = uint32(args) val = vm.pop_u64() vm.set_operand(vm.local_0_idx + idx, val)
def local_get(vm, args): """获取局部变量""" idx = uint32(args) val = vm.get_operand(vm.local_0_idx + idx) vm.push_u64(val)
def i32_pop_cnt(vm, _): """统计1比特数""" vm.push_u32(uint32(__ones_count32(vm.pop_u32())))
def i32_ctz(vm, _): """统计后置0比特数""" vm.push_u32(uint32(__trailing_zeros32(vm.pop_u32())))
def push_s32(self, val): self.push_u64(uint32(val))
def i32_wrap_i64(vm, _): vm.push_u32(uint32(vm.pop_u64()))
def i32_load_16u(vm, mem_arg): val = read_u16(vm, mem_arg) vm.push_u32(uint32(val))
def i32_load_8u(vm, mem_arg): val = read_u8(vm, mem_arg) vm.push_u32(uint32(val))
def i64_store_32(vm, mem_arg): val = vm.pop_u64() write_u32(vm, mem_arg, uint32(val))
def read_u32(vm, mem_arg): buf = [0x00] * 4 offset = get_offset(vm, mem_arg) buf = vm.memory.read(offset, buf) return uint32(int.from_bytes(bytearray(buf), byteorder='little'))
def global_set(vm, args): idx = uint32(args) val = vm.pop_u64() vm.globals[idx].set_as_u64(val)
def i32_clz(vm, _): """统计前置0比特数""" vm.push_u32(uint32(__leading_zeros32(vm.pop_u32())))
def pop_u32(self) -> uint32: return uint32(self.pop_u64())