Exemple #1
0
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")
Exemple #2
0
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))
Exemple #3
0
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)
Exemple #4
0
def local_set(vm, args):
    """设置局部变量的值"""
    idx = uint32(args)
    val = vm.pop_u64()
    vm.set_operand(vm.local_0_idx + idx, val)
Exemple #5
0
def local_get(vm, args):
    """获取局部变量"""
    idx = uint32(args)
    val = vm.get_operand(vm.local_0_idx + idx)
    vm.push_u64(val)
Exemple #6
0
def i32_pop_cnt(vm, _):
    """统计1比特数"""
    vm.push_u32(uint32(__ones_count32(vm.pop_u32())))
Exemple #7
0
def i32_ctz(vm, _):
    """统计后置0比特数"""
    vm.push_u32(uint32(__trailing_zeros32(vm.pop_u32())))
Exemple #8
0
def i32_clz(vm, _):
    """统计前置0比特数"""
    vm.push_u32(uint32(__leading_zeros32(vm.pop_u32())))
Exemple #9
0
def i64_store_32(vm, mem_arg):
    val = vm.pop_u64()
    write_u32(vm, mem_arg, uint32(val))
Exemple #10
0
def i32_wrap_i64(vm, _):
    vm.push_u32(uint32(vm.pop_u64()))
Exemple #11
0
 def push_s32(self, val):
     self.push_numeric(uint32(val))
Exemple #12
0
 def pop_u32(self) -> uint32:
     return uint32(self.pop_numeric())
Exemple #13
0
def control_return(vm, _):
    # 找到函数最外层块的标签索引(也就是当前控制块的深度)
    _, label_idx = vm.top_call_frame()
    br(vm, uint32(label_idx))
Exemple #14
0
def i32_load_16u(vm, mem_arg):
    val = read_u16(vm, mem_arg)
    vm.push_u32(uint32(val))
Exemple #15
0
def i32_load_8u(vm, mem_arg):
    val = read_u8(vm, mem_arg)
    vm.push_u32(uint32(val))
Exemple #16
0
def global_get(vm, args):
    idx = uint32(args)
    val = vm.globals[idx].get_as_u64()
    vm.push_u64(val)
Exemple #17
0
def global_set(vm, args):
    idx = uint32(args)
    val = vm.pop_u64()
    vm.globals[idx].set_as_u64(val)
Exemple #18
0
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'))