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 wrap_u64(vt, val): if vt == ValTypeI32: return int32(val) elif vt == ValTypeI64: return int64(val) elif vt == ValTypeF32: try: cov_val = struct.unpack('>f', struct.pack('>l', int64(val)))[0] except struct.error: cov_val = struct.unpack('>f', struct.pack('>L', int64(val)))[0] if math.isnan(cov_val): return float32(val) else: return float32(cov_val) elif vt == ValTypeF64: try: cov_val = struct.unpack('>d', struct.pack('>q', int64(val)))[0] except struct.error: cov_val = struct.unpack('>d', struct.pack('>Q', int64(val)))[0] if math.isnan(cov_val): return float64(val) else: return float64(cov_val) else: raise Exception("unreachable")
def i32_trunc_f64s(vm, _): val = vm.pop_f64() if math.isinf(val): raise ErrIntOverflow if math.isnan(val): raise ErrConvertToInt f = math.trunc(val) if f > __MaxInt32 or f < __MinInt32: raise ErrIntOverflow vm.push_s32(int32(f))
def get_consts(expr): if len(expr) == 0: vals = [] else: vals = [None] * len(expr) for i, instr in enumerate(expr): opcode = instr.opcode if opcode == I32Const: vals[i] = int32(instr.args) elif opcode == I64Const: vals[i] = int64(instr.args) elif opcode == F32Const: vals[i] = float32(instr.args) elif opcode == F64Const: vals[i] = float64(instr.args) else: raise Exception("TODO") return vals
def test_operand_stack(self): stack = OperandStack() stack.push_bool(True) stack.push_bool(False) stack.push_u32(1) stack.push_s32(-2) stack.push_u64(3) stack.push_s64(-4) stack.push_f32(5.5) stack.push_f64(6.5) self.assertEqual(6.5, stack.pop_f64()) self.assertEqual(float32(5.5), stack.pop_f32()) self.assertEqual(int64(-4), stack.pop_s64()) self.assertEqual(uint64(3), stack.pop_u64()) self.assertEqual(int32(-2), stack.pop_s32()) self.assertEqual(uint32(1), stack.pop_u32()) self.assertEqual(False, stack.pop_bool()) self.assertEqual(True, stack.pop_bool()) self.assertEqual(0, len(stack.slots))
def parse_i32(s: str): return int32(parse_int(s, 32))
def i64_load_32s(vm, mem_arg): val = read_u32(vm, mem_arg) vm.push_s64(int64(int32(val)))
def i32_load_16s(vm, mem_arg): val = read_u16(vm, mem_arg) vm.push_s32(int32(int16(val)))
def i32_load_8s(vm, mem_arg): val = read_u8(vm, mem_arg) vm.push_u32(int32(int8(val)))
def i64_extend_32s(vm, _): vm.push_s64(int64(int32(vm.pop_s64())))
def i32_extend_16s(vm, _): vm.push_s32(int32(int16(vm.pop_s32())))
def i32_extend_8s(vm, _): vm.push_s32(int32(int8(vm.pop_s32())))
def i32_const(vm, args): vm.push_s32(int32(args))
def pop_s32(self): return int32(self.pop_numeric())