예제 #1
0
파일: numeric.py 프로젝트: metazool/py-wasm
def iXX_divs_op(config: Configuration) -> None:
    """
    Common logic function for the integer DIVS opcodes
    """
    instruction = cast(BinOp, config.current_instruction)
    b, a = config.pop2_u32()

    b_s = instruction.valtype.to_signed(b)
    a_s = instruction.valtype.to_signed(a)
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s, %s)", instruction.opcode.text, a_s, b_s)

    if b == 0:
        raise Trap('DIVISION BY ZERO')

    raw_result = abs(int(a_s)) // abs(int(b_s))
    _, upper_bound = instruction.valtype.signed_bounds

    if raw_result > upper_bound:
        raise Trap('UNDEFINED')

    if (a_s < 0) is not (b_s < 0):
        signed_result = instruction.valtype.signed_type(-1 * raw_result)
    else:
        signed_result = instruction.valtype.signed_type(raw_result)

    result = instruction.valtype.from_signed(signed_result)

    config.push_operand(result)
예제 #2
0
파일: control.py 프로젝트: metazool/py-wasm
def if_op(config: Configuration) -> None:
    """
    Logic function for the IF opcode
    """
    instruction = cast(If, config.current_instruction)

    if config.enable_logic_fn_logging:
        logger.debug("%s()", instruction.opcode.text)

    value = config.pop_operand()
    arity = len(instruction.result_type)

    if value:
        label = Label(
            arity=arity,
            instructions=instruction.instructions,
            is_loop=False,
        )
    else:
        label = Label(
            arity=arity,
            instructions=instruction.else_instructions,
            is_loop=False,
        )

    config.push_label(label)
예제 #3
0
파일: control.py 프로젝트: metazool/py-wasm
def _setup_function_invocation(config: Configuration,
                               function_address: FunctionAddress,
                               function_args: Tuple[TValue, ...]) -> None:
    """
    Helper function for invoking a function by the function's address.
    """
    function = config.store.funcs[function_address]

    if len(function_args) != len(function.type.params):
        raise TypeError(
            f"Wrong number of arguments. Expected {len(function.type.params)} "
            f"Got {len(function_args)}")

    if isinstance(function, FunctionInstance):
        locals = [valtype.zero for valtype in function.code.locals]
        frame = Frame(
            module=function.module,
            locals=list(function_args) + locals,
            # TODO: do we need this wrapping anymore?
            instructions=Block.wrap_with_end(function.type.results,
                                             function.code.body),
            arity=len(function.type.results),
        )
        config.push_frame(frame)
    elif isinstance(function, HostFunction):
        ret = function.hostcode(config, function_args)
        if len(ret) > 1:
            raise Exception("Invariant")
        elif ret:
            config.push_operand(ret[0])
    else:
        raise Exception("Invariant: unreachable code path")
예제 #4
0
파일: numeric.py 프로젝트: metazool/py-wasm
def iXX_trunc_usX_fXX_op(config: Configuration) -> None:
    """
    Common logic function for the TRUNC opcodes which convert a float to an
    integer
    """
    instruction = cast(Truncate, config.current_instruction)

    value = config.pop_f32()

    if config.enable_logic_fn_logging:
        logger.debug("%s(%s)", instruction.opcode.text, value)

    if numpy.isnan(value) or numpy.isinf(value):
        raise Trap(f"Truncation is undefined for {value}")
    else:
        trunc_value = int(numpy.trunc(value))

        if instruction.signed:
            s_lower_bound, s_upper_bound = instruction.valtype.signed_bounds
            if trunc_value < s_lower_bound or trunc_value > s_upper_bound:
                raise Trap(
                    f"Truncation is undefined for {value}. Result outside of s32 "
                    "range.")

            result = instruction.valtype.from_signed(
                instruction.valtype.signed_type(trunc_value))
        else:
            u_lower_bound, u_upper_bound = instruction.valtype.bounds
            if trunc_value < u_lower_bound or trunc_value > u_upper_bound:
                raise Trap(
                    f"Truncation is undefined for {value}. Result outside of s32 "
                    "range.")
            result = instruction.valtype.value(trunc_value)

        config.push_operand(result)
예제 #5
0
파일: memory.py 프로젝트: metazool/py-wasm
def store_op(config: Configuration) -> None:
    """
    Logic function for the various *STORE* memory opcodes.
    """
    instruction = cast(MemoryOp, config.current_instruction)
    if config.enable_logic_fn_logging:
        logger.debug("%s()", instruction.opcode.text)

    memarg = instruction.memarg

    memory_address = config.frame_module.memory_addrs[0]
    mem = config.store.mems[memory_address]

    value = config.pop_operand()

    base_offset = config.pop_u32()
    memory_location = numpy.uint32(base_offset + memarg.offset)

    # TODO: update this section to use the `ValType.pack_bytes` API
    if instruction.valtype.is_integer_type:
        wrapped_value = instruction.memory_bit_size.wrap_type(value)
        encoded_value = wrapped_value.tobytes()
    elif instruction.valtype.is_float_type:
        encoded_value = value.tobytes()
    else:
        raise Exception("Invariant")

    assert len(encoded_value) == instruction.memory_bit_size.value // 8
    mem.write(memory_location, encoded_value)
예제 #6
0
def drop_op(config: Configuration) -> None:
    """
    Logic functin for the DROP opcode.
    """
    if config.enable_logic_fn_logging:
        logger.debug("%s()", config.current_instruction.opcode.text)

    config.pop_operand()
예제 #7
0
파일: numeric.py 프로젝트: metazool/py-wasm
def const_op(config: Configuration) -> None:
    """
    Common logic function for the various CONST opcodes.
    """
    instruction = cast(TConst, config.current_instruction)
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s)", instruction.opcode.text, instruction)

    config.push_operand(instruction.value)
예제 #8
0
파일: numeric.py 프로젝트: metazool/py-wasm
def ixor_op(config: Configuration) -> None:
    """
    Common logic function for the integer XOR opcodes
    """
    b, a = config.pop2_u64()
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s, %s)", config.current_instruction.opcode.text, a,
                     b)

    config.push_operand(a ^ b)
예제 #9
0
파일: numeric.py 프로젝트: metazool/py-wasm
def f32demote_op(config: Configuration) -> None:
    """
    Logic function for the F32_DEMOTE_F64 opcode
    """
    value = config.pop_f64()

    if config.enable_logic_fn_logging:
        logger.debug("%s(%s)", config.current_instruction.opcode.text, value)

    config.push_operand(numpy.float32(value))
예제 #10
0
파일: numeric.py 프로젝트: metazool/py-wasm
def iwrap64_op(config: Configuration) -> None:
    """
    Logic function for the I32_WRAP_I64 opcode
    """
    value = config.pop_u64()

    if config.enable_logic_fn_logging:
        logger.debug("%s(%s)", config.current_instruction.opcode.text, value)

    config.push_operand(numpy.uint32(value))
예제 #11
0
def get_local_op(config: Configuration) -> None:
    """
    Logic functin for the GET_LOCAL opcode.
    """
    instruction = cast(LocalOp, config.current_instruction)
    if config.enable_logic_fn_logging:
        logger.debug("%s()", instruction.opcode.text)

    value = config.frame_locals[instruction.local_idx]
    config.push_operand(value)
예제 #12
0
파일: numeric.py 프로젝트: metazool/py-wasm
def fsqrt_op(config: Configuration) -> None:
    """
    Common logic function for the float SQRT opcodes
    """
    instruction = cast(UnOp, config.current_instruction)

    value = config.pop_f64()

    if config.enable_logic_fn_logging:
        logger.debug("%s(%s)", instruction.opcode.text, value)

    if numpy.isnan(value):
        with allow_invalid():
            config.push_operand(numpy.sqrt(value))
    elif value == 0:
        # TODO: this block and the subsequent _is_negative block are in
        # inverted order in the spec, indicating that the proper response here
        # should potentially be `nan` for the case of `sqrt(-0.0)` but the spec
        # tests assert that is not correct.
        # f32.wasm: line 2419
        config.push_operand(value)
    elif _is_negative(value):
        config.push_operand(instruction.valtype.nan)
    else:
        config.push_operand(numpy.sqrt(value))
예제 #13
0
파일: numeric.py 프로젝트: metazool/py-wasm
def iXX_mul_op(config: Configuration) -> None:
    """
    Common logic function for the integer MUL opcodes
    """
    b, a = config.pop2_u64()
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s, %s)", config.current_instruction.opcode.text, a,
                     b)

    with allow_overflow():
        config.push_operand(a * b)
예제 #14
0
def get_global_op(config: Configuration) -> None:
    """
    Logic functin for the GET_GLOBAL opcode.
    """
    instruction = cast(GlobalOp, config.current_instruction)
    if config.enable_logic_fn_logging:
        logger.debug("%s()", instruction.opcode.text)

    global_address = config.frame_module.global_addrs[instruction.global_idx]
    global_ = config.store.globals[global_address]
    config.push_operand(global_.value)
예제 #15
0
파일: memory.py 프로젝트: metazool/py-wasm
def memory_size_op(config: Configuration) -> None:
    """
    Logic function for the MEMORY_SIZE opcode
    """
    if config.enable_logic_fn_logging:
        logger.debug("%s()", config.current_instruction.opcode.text)

    memory_address = config.frame_module.memory_addrs[0]
    mem = config.store.mems[memory_address]
    size = numpy.uint32(len(mem.data) // constants.PAGE_SIZE_64K)
    config.push_operand(size)
예제 #16
0
파일: numeric.py 프로젝트: metazool/py-wasm
def ieqz_op(config: Configuration) -> None:
    """
    Common logic function for the integer EQZ opcodes
    """
    value = config.pop_operand()
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s)", config.current_instruction.opcode.text, value)

    if value == 0:
        config.push_operand(constants.U32_ONE)
    else:
        config.push_operand(constants.U32_ZERO)
예제 #17
0
파일: numeric.py 프로젝트: metazool/py-wasm
def fadd_op(config: Configuration) -> None:
    """
    Common logic function for the float ADD opcodes
    """
    b, a = config.pop2_f64()

    if config.enable_logic_fn_logging:
        logger.debug("%s(%s, %s)", config.current_instruction.opcode.text, a,
                     b)

    with allow_multiple(over=True, invalid=True):
        config.push_operand(a + b)
예제 #18
0
파일: numeric.py 프로젝트: metazool/py-wasm
def idivu_op(config: Configuration) -> None:
    """
    Common logic function for the integer DIVU opcodes
    """
    b, a = config.pop2_u32()
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s, %s)", config.current_instruction.opcode.text, a,
                     b)

    if b == 0:
        raise Trap('DIVISION BY ZERO')
    config.push_operand(a // b)
예제 #19
0
def select_op(config: Configuration) -> None:
    """
    Logic functin for the SELECT opcode.
    """
    if config.enable_logic_fn_logging:
        logger.debug("%s()", config.current_instruction.opcode.text)

    a, b, c = config.pop3_operands()

    if a:
        config.push_operand(c)
    else:
        config.push_operand(b)
예제 #20
0
파일: numeric.py 프로젝트: metazool/py-wasm
def XXX_reinterpret_XXX_op(config: Configuration) -> None:
    """
    Common logic function for the REINTERPRET opcodes
    """
    instruction = cast(Convert, config.current_instruction)

    value = config.pop_f32()

    if config.enable_logic_fn_logging:
        logger.debug("%s(%s)", instruction.opcode.text, value)

    config.push_operand(
        numpy.frombuffer(value.data, instruction.valtype.value)[0])
예제 #21
0
파일: numeric.py 프로젝트: metazool/py-wasm
def ne_op(config: Configuration) -> None:
    """
    Common logic function for all NE opcodes
    """
    b, a = config.pop2_operands()
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s, %s)", config.current_instruction.opcode.text, a,
                     b)

    if a == b:
        config.push_operand(constants.U32_ZERO)
    else:
        config.push_operand(constants.U32_ONE)
예제 #22
0
파일: numeric.py 프로젝트: metazool/py-wasm
def iXX_shl_op(config: Configuration) -> None:
    """
    Common logic function for the integer SHL opcodes
    """
    instruction = cast(BinOp, config.current_instruction)
    b, a = config.pop2_u64()
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s, %s)", instruction.opcode.text, a, b)

    shift_amount = int(b % instruction.valtype.bit_size.value)
    raw_result = int(a) << shift_amount
    mod = instruction.valtype.mod
    config.push_operand(instruction.valtype.value(raw_result % mod))
예제 #23
0
파일: numeric.py 프로젝트: metazool/py-wasm
def fneg_op(config: Configuration) -> None:
    """
    Common logic function for the float NEG opcodes
    """
    instruction = cast(UnOp, config.current_instruction)

    value = config.pop_f64()

    if config.enable_logic_fn_logging:
        logger.debug("%s(%s)", instruction.opcode.text, value)

    negated_value = _negate_float(value)

    config.push_operand(negated_value)
예제 #24
0
파일: numeric.py 프로젝트: metazool/py-wasm
def ipopcnt_op(config: Configuration) -> None:
    """
    Common logic function for the integer POPCNT opcodes
    """
    instruction = cast(UnOp, config.current_instruction)
    value = config.pop_operand()
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s)", instruction.opcode.text, value)

    if value == 0:
        config.push_operand(instruction.valtype.zero)
    else:
        config.push_operand(
            instruction.valtype.value(bin(int(value)).count('1')))
예제 #25
0
파일: control.py 프로젝트: metazool/py-wasm
def loop_op(config: Configuration) -> None:
    """
    Logic function for the LOOP opcode
    """
    instruction = cast(Loop, config.current_instruction)

    if config.enable_logic_fn_logging:
        logger.debug("%s()", instruction.opcode.text)

    label = Label(
        arity=0,
        instructions=instruction.instructions,
        is_loop=True,
    )
    config.push_label(label)
예제 #26
0
파일: control.py 프로젝트: metazool/py-wasm
def block_op(config: Configuration) -> None:
    """
    Logic function for the BLOCK opcode
    """
    block = cast(Block, config.current_instruction)

    if config.enable_logic_fn_logging:
        logger.debug("%s()", block.opcode.text)

    label = Label(
        arity=len(block.result_type),
        instructions=block.instructions,
        is_loop=False,
    )
    config.push_label(label)
예제 #27
0
파일: numeric.py 프로젝트: metazool/py-wasm
def iXX_ges_op(config: Configuration) -> None:
    """
    Common logic function for the integer GES opcodes
    """
    instruction = cast(RelOp, config.current_instruction)
    b, a = config.pop2_u64()
    b_s = instruction.valtype.to_signed(b)
    a_s = instruction.valtype.to_signed(a)
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s, %s)", instruction.opcode.text, a_s, b_s)

    if a_s >= b_s:
        config.push_operand(constants.U32_ONE)
    else:
        config.push_operand(constants.U32_ZERO)
예제 #28
0
파일: numeric.py 프로젝트: metazool/py-wasm
def iXX_clz_op(config: Configuration) -> None:
    """
    Common logic function for the integer CLZ opcodes
    """
    instruction = cast(TestOp, config.current_instruction)
    value = config.pop_u64()
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s)", instruction.opcode.text, value)

    bit_size = instruction.valtype.bit_size.value
    if value == 0:
        config.push_operand(instruction.valtype.value(bit_size))
    else:
        config.push_operand(
            instruction.valtype.value(bit_size - int(value).bit_length()))
예제 #29
0
파일: control.py 프로젝트: metazool/py-wasm
def call_indirect_op(config: Configuration) -> None:
    """
    Logic function for the CALL_INDIRECT opcode
    """
    instruction = cast(CallIndirect, config.current_instruction)

    if config.enable_logic_fn_logging:
        logger.debug("%s()", instruction.opcode.text)

    table_address = config.frame_module.table_addrs[0]
    table = config.store.tables[table_address]
    function_type = config.frame_module.types[instruction.type_idx]

    element_idx = config.pop_u32()

    if len(table.elem) <= element_idx:
        raise Trap(
            "Element index out of table range: {element_idx} > {len(table.elem)}"
        )

    function_address = table.elem[element_idx]

    if function_address is None:
        raise Trap("Table element at index {element_idx} is empty")

    function = config.store.funcs[int(function_address)]

    if function.type != function_type:
        raise Trap(
            "Function type mismatch.  Expected {function_type}.  Got {function.type}"
        )

    _setup_call(config, function_address)
예제 #30
0
파일: numeric.py 프로젝트: metazool/py-wasm
def iXX_ctz_op(config: Configuration) -> None:
    """
    Common logic function for the integer CTZ opcodes
    """
    instruction = cast(TestOp, config.current_instruction)
    value = config.pop_u64()
    if config.enable_logic_fn_logging:
        logger.debug("%s(%s)", instruction.opcode.text, value)

    if value == 0:
        config.push_operand(
            instruction.valtype.value(instruction.valtype.bit_size.value))
    else:
        as_bin = bin(int(value))
        _, _, zeros = as_bin.rpartition('1')
        config.push_operand(instruction.valtype.value(len(zeros)))