Beispiel #1
0
        def __init__(self, *args, **kwargs):
            from peachpy.x86_64.function import active_function
            from peachpy.x86_64.registers import GeneralPurposeRegister, MMXRegister, XMMRegister, YMMRegister
            from peachpy.util import is_int16, is_int32
            from peachpy.x86_64.abi import goasm_amd64_abi, goasm_amd64p32_abi

            origin = kwargs.get("origin")
            prototype = kwargs.get("prototype")
            if origin is None and prototype is None and peachpy.x86_64.options.get_debug_level() > 0:
                origin = inspect.stack()
            super(STORE.RESULT, self).__init__("STORE.RESULT", origin=origin)
            self.operands = tuple(map(check_operand, args))
            self.out_regs = (False,)
            self.in_regs = (True,)
            if len(self.operands) != 1:
                raise SyntaxError("Instruction \"STORE.RESULT\" requires 1 operand")

            target_function = active_function
            destination_offset = None
            if target_function is None:
                target_function = kwargs.get("target_function")
                assert target_function.abi in {goasm_amd64_abi, goasm_amd64p32_abi}
                destination_offset = target_function.result_offset
            if target_function.result_type is None:
                raise ValueError("STORE.RESULT can't be used with void functions")
            self.destination_type = target_function.result_type
            self.destination_size = self.destination_type.size
            # Will be updated during ABI binding (ABIFunction._lower_pseudoinstructions)
            self.destination_offset = destination_offset

            if isinstance(self.operands[0], GeneralPurposeRegister):
                if self.operands[0].size != self.destination_size:
                    raise ValueError("Can not store result in register %s: size mismatch with return type %s"
                                     % (str(self.operands[0]), str(self.destination_type)))
            elif isinstance(self.operands[0], MMXRegister):
                if self.destination_size not in {4, 8}:
                    raise ValueError("Can not store result in register %s: size mismatch with return type %s"
                                     % (str(self.operands[0]), str(self.destination_type)))
            elif isinstance(self.operands[0], XMMRegister):
                if self.destination_size not in {4, 8}:
                    raise ValueError("Can not store result in register %s: size mismatch with return type %s"
                                     % (str(self.operands[0]), str(self.destination_type)))
            elif isinstance(self.operands[0], YMMRegister):
                raise ValueError("Can not store result in register %s: unsupported register type")
            elif is_int32(self.operands[0]):
                if not self.destination_type.is_integer:
                    raise ValueError("Can not store integer result %d: type mismatch with result type %s"
                                     % (self.operands[0], str(self.destination_type)))
                if is_int16(self.operands[0]) and self.destination_size < 2:
                    raise ValueError("Can not store integer result %d: size mismatch with result type %s"
                                     % (self.operands[0], str(self.destination_type)))
                if is_int32(self.operands[0]) and self.destination_size < 4:
                    raise ValueError("Can not store integer result %d: size mismatch with result type %s"
                                     % (self.operands[0], str(self.destination_type)))

            if peachpy.stream.active_stream is not None:
                peachpy.stream.active_stream.add_instruction(self)
Beispiel #2
0
def format_operand_type(operand):
    """Returns string representation of the operand type in assembly language"""
    from peachpy.x86_64.registers import GeneralPurposeRegister64, GeneralPurposeRegister32, GeneralPurposeRegister16,\
        GeneralPurposeRegister8, MMXRegister, XMMRegister, YMMRegister,\
        al, ax, eax, rax, cl, xmm0
    from peachpy.x86_64.pseudo import Label
    from peachpy.util import is_int64, is_int32, is_int16, is_int8
    if is_int8(operand):
        return "imm8"
    elif is_int16(operand):
        return "imm16"
    elif is_int32(operand):
        return "imm32"
    elif is_int64(operand):
        return "imm64"
    elif al == operand:
        return "al"
    elif ax == operand:
        return "ax"
    elif eax == operand:
        return "eax"
    elif rax == operand:
        return "rax"
    elif cl == operand:
        return "cl"
    elif xmm0 == operand:
        return "xmm0"
    elif isinstance(operand, GeneralPurposeRegister64):
        return "r64"
    elif isinstance(operand, GeneralPurposeRegister32):
        return "r32"
    elif isinstance(operand, GeneralPurposeRegister16):
        return "r16"
    elif isinstance(operand, GeneralPurposeRegister8):
        return "r8"
    elif isinstance(operand, MMXRegister):
        return "mm"
    elif isinstance(operand, XMMRegister):
        return "xmm"
    elif isinstance(operand, YMMRegister):
        return "ymm"
    elif isinstance(operand, MemoryOperand):
        if operand.size is None:
            return "m"
        else:
            return "m" + str(operand.size)
    elif isinstance(operand, Label):
        return "rel"
    else:
        return operand.__class__.__name__
Beispiel #3
0
def format_operand_type(operand):
    """Returns string representation of the operand type in assembly language"""
    from peachpy.x86_64.registers import GeneralPurposeRegister64, GeneralPurposeRegister32, GeneralPurposeRegister16,\
        GeneralPurposeRegister8, MMXRegister, XMMRegister, YMMRegister,\
        al, ax, eax, rax, cl, xmm0
    from peachpy.x86_64.pseudo import Label
    from peachpy.util import is_int64, is_int32, is_int16, is_int8
    if is_int8(operand):
        return "imm8"
    elif is_int16(operand):
        return "imm16"
    elif is_int32(operand):
        return "imm32"
    elif is_int64(operand):
        return "imm64"
    elif al == operand:
        return "al"
    elif ax == operand:
        return "ax"
    elif eax == operand:
        return "eax"
    elif rax == operand:
        return "rax"
    elif cl == operand:
        return "cl"
    elif xmm0 == operand:
        return "xmm0"
    elif isinstance(operand, GeneralPurposeRegister64):
        return "r64"
    elif isinstance(operand, GeneralPurposeRegister32):
        return "r32"
    elif isinstance(operand, GeneralPurposeRegister16):
        return "r16"
    elif isinstance(operand, GeneralPurposeRegister8):
        return "r8"
    elif isinstance(operand, MMXRegister):
        return "mm"
    elif isinstance(operand, XMMRegister):
        return "xmm"
    elif isinstance(operand, YMMRegister):
        return "ymm"
    elif isinstance(operand, MemoryOperand):
        if operand.size is None:
            return "m"
        else:
            return "m" + str(operand.size)
    elif isinstance(operand, Label):
        return "rel"
    else:
        return operand.__class__.__name__
Beispiel #4
0
 def _uint32xN(name, n, *args):
     from peachpy.util import is_int, is_int32
     assert is_int(n)
     args = [arg for arg in args if arg is not None]
     if len(args) == 0:
         raise ValueError("At least one constant value must be specified")
     if len(args) != 1 and len(args) != n:
         raise ValueError("Either 1 or %d values must be specified" % n)
     for i, number in enumerate(args):
         if not is_int(number):
             raise TypeError("The value %s is not an integer" % str(number))
         if not is_int32(number):
             raise ValueError("The number %d is not a 32-bit integer" % number)
         if number < 0:
             args[i] += 0x100000000
     if len(args) == 1:
         args = [args[0]] * n
     return Constant(4 * n, n, tuple(args), uint32_t, name)
Beispiel #5
0
 def _uint32xN(name, n, *args):
     from peachpy.util import is_int, is_int32
     assert is_int(n)
     args = [arg for arg in args if arg is not None]
     if len(args) == 0:
         raise ValueError("At least one constant value must be specified")
     if len(args) != 1 and len(args) != n:
         raise ValueError("Either 1 or %d values must be specified" % n)
     for i, number in enumerate(args):
         if not is_int(number):
             raise TypeError("The value %s is not an integer" % str(number))
         if not is_int32(number):
             raise ValueError("The number %d is not a 32-bit integer" % number)
         if number < 0:
             args[i] += 0x100000000
     if len(args) == 1:
         args = [args[0]] * n
     return Constant(4 * n, n, tuple(args), uint32_t, name)
Beispiel #6
0
        def __init__(self, *args, **kwargs):
            from peachpy.x86_64.function import active_function
            from peachpy.x86_64.registers import GeneralPurposeRegister, MMXRegister, XMMRegister, YMMRegister
            from peachpy.util import is_int16, is_int32
            from peachpy.x86_64.abi import goasm_amd64_abi, goasm_amd64p32_abi

            origin = kwargs.get("origin")
            prototype = kwargs.get("prototype")
            if origin is None and prototype is None and peachpy.x86_64.options.get_debug_level(
            ) > 0:
                origin = inspect.stack()
            super(STORE.RESULT, self).__init__("STORE.RESULT", origin=origin)
            self.operands = tuple(map(check_operand, args))
            self.out_regs = (False, )
            self.in_regs = (True, )
            if len(self.operands) != 1:
                raise SyntaxError(
                    "Instruction \"STORE.RESULT\" requires 1 operand")

            target_function = active_function
            destination_offset = None
            if target_function is None:
                target_function = kwargs.get("target_function")
                assert target_function.abi in {
                    goasm_amd64_abi, goasm_amd64p32_abi
                }
                destination_offset = target_function.result_offset
            if target_function.result_type is None:
                raise ValueError(
                    "STORE.RESULT can't be used with void functions")
            self.destination_type = target_function.result_type
            self.destination_size = self.destination_type.size
            # Will be updated during ABI binding (ABIFunction._lower_pseudoinstructions)
            self.destination_offset = destination_offset

            if isinstance(self.operands[0], GeneralPurposeRegister):
                if self.operands[0].size != self.destination_size:
                    raise ValueError(
                        "Can not store result in register %s: size mismatch with return type %s"
                        % (str(self.operands[0]), str(self.destination_type)))
            elif isinstance(self.operands[0], MMXRegister):
                if self.destination_size not in {4, 8}:
                    raise ValueError(
                        "Can not store result in register %s: size mismatch with return type %s"
                        % (str(self.operands[0]), str(self.destination_type)))
            elif isinstance(self.operands[0], XMMRegister):
                if self.destination_size not in {4, 8}:
                    raise ValueError(
                        "Can not store result in register %s: size mismatch with return type %s"
                        % (str(self.operands[0]), str(self.destination_type)))
            elif isinstance(self.operands[0], YMMRegister):
                raise ValueError(
                    "Can not store result in register %s: unsupported register type"
                )
            elif is_int32(self.operands[0]):
                if not self.destination_type.is_integer:
                    raise ValueError(
                        "Can not store integer result %d: type mismatch with result type %s"
                        % (self.operands[0], str(self.destination_type)))
                if is_int16(self.operands[0]) and self.destination_size < 2:
                    raise ValueError(
                        "Can not store integer result %d: size mismatch with result type %s"
                        % (self.operands[0], str(self.destination_type)))
                if is_int32(self.operands[0]) and self.destination_size < 4:
                    raise ValueError(
                        "Can not store integer result %d: size mismatch with result type %s"
                        % (self.operands[0], str(self.destination_type)))

            if peachpy.stream.active_stream is not None:
                peachpy.stream.active_stream.add_instruction(self)