Esempio n. 1
0
    def check(self, value):
        """

        :param value:

        """

        if isinstance(value, six.integer_types):
            cvalue = value
        elif isinstance(value, Address):
            # Warning!
            return
        else:
            if not isinstance(value[0], Address) or \
                    not isinstance(value[1], Address):
                raise MicroprobeValueError("Invalid operand value '%s'."
                                           " Any Address?" % (value))
            cvalue = value[0] - value[1]

        if cvalue > (self._maxdispl << self._shift) or \
           cvalue < (self._mindispl << self._shift) or \
           self._in_except_ranges(cvalue):
            raise MicroprobeValueError(
                "Invalid operand value '%d' "
                "not within the"
                " allowed range (%d, %d) and exceptions"
                " '%s' " %
                (cvalue, self._mindispl, self._maxdispl, self._except))
Esempio n. 2
0
def get_wrapper(name):
    """Return a wrapper object with name *name*.

    Look for the registered :class:`~.Wrapper` objects and return and instance
    of the one with name equal *name*.

    :param name: Wrapper name
    :type name: :class:`~.str`
    :return: A wrapper instance
    :rtype: :class:`~.Wrapper`
    """

    global _INIT  # pylint: disable=global-statement
    if _INIT:
        _INIT = False
        _import_default_wrappers()

    if MICROPROBE_RC['debugwrapper']:
        name = "DebugBinaryDouble"

    for elem in get_all_subclasses(microprobe.code.wrapper.Wrapper):
        if elem.__name__ == name:
            return elem

    raise MicroprobeValueError(
        "Unknown wrapper '%s'. Available wrappers are: %s. " % (
            name, [
                elem.__name__
                for elem in get_all_subclasses(
                    microprobe.code.wrapper.Wrapper
                )
            ]
        )
    )
Esempio n. 3
0
    def __init__(self, minimize=False, value=None, dd=0, relax=False):
        """

        :param minimize:  (Default value = False)
        :param value:  (Default value = None)
        :param dd:  (Default value = 1)

        """
        super(DefaultRegisterAllocationPass, self).__init__()
        self._min = minimize
        if self._min:
            raise NotImplementedError("Feature changed need to be"
                                      " reimplemented")

        if isinstance(dd, six.integer_types):
            self._dd = lambda: dd  # Dependency distance
        elif isinstance(dd, float):
            self._dd = microprobe.utils.distrib.discrete_average(dd)
        elif callable(dd):
            self._dd = dd
        else:
            raise MicroprobeValueError("Invalid parameter")

        self._relax = relax

        if value is not None:
            self._immediate = value
            if value == "zero":
                self._immediate = 0
        else:
            self._immediate = "random"
        self._description = "Default register allocation (required always to" \
                            " set any remaining operand). minimize=%s, " \
                            "value=%s, dd=%s" % (minimize, value, dd)
Esempio n. 4
0
    def check(self, value):
        """Check if a value is valid for the operand.

        :param value: value of the operand.
        :type value: :class:`~.str`, :class:`~.Register` or
            :class:`int`
        :raise microprobe.exceptions.MicroprobeValueError: if
            the value is not allowed for the operand
        """
        if not self.__contains__(value):
            raise MicroprobeValueError("Invalid operand value %s not in %s" %
                                       (value, list(self.values())))
Esempio n. 5
0
def pstdev(data):
    """Calculates the population standard deviation"""
    ldata = len(data)
    if ldata < 2:
        raise MicroprobeValueError(
            "variance requires at least two data points")

    data_ave = average(data)
    squared_sum = sum((x - data_ave)**2 for x in data)

    pvar = squared_sum / (ldata)
    return pvar**0.5
Esempio n. 6
0
    def check(self, value):
        """

        :param value:

        """

        if not isinstance(value, six.integer_types):
            raise MicroprobeValueError("Invalid operand value: '%s'. Integer"
                                       " required and '%s' provided" %
                                       (value, type(value)))

        value = value >> self._shift
        if value <= self._max and value >= self._min \
                and (value - self._min) % self._step == 0 \
                and value not in self._novalues:

            return True

        else:

            raise MicroprobeValueError("Invalid operand value: %d (max: %d,"
                                       " min: %d)" %
                                       (value, self._max, self._min))
Esempio n. 7
0
    def __init__(self, size, instructions=None):
        """

        :param size:

        """
        if size < 1:
            raise MicroprobeValueError("I can not create a Bbl with %d size" %
                                       size)

        if instructions is None:
            instructions = []

        self._copy_id = 0

        self._incstep = 10000
        self._instrs = [None] * max(self._incstep, (size + 1) * 10)
        self._pointer = 10

        self._instdic = {}

        self._address = None
        self._displacement = 0

        if MICROPROBE_RC["verbose"]:
            progress = Progress(size, "Initializing BBL:")

        if not instructions:
            for idx in range(0, size):
                # self._check_size()
                instr = microprobe.code.ins.Instruction()
                self._instrs[self._pointer] = instr
                self._instdic[instr] = self._pointer
                self._pointer += 10
                if MICROPROBE_RC["verbose"]:
                    progress()
        else:
            for idx in range(0, size):
                self._check_size()
                if idx < len(instructions):
                    self._instrs[self._pointer] = instructions[idx]
                else:
                    self._instrs[self._pointer] = \
                        microprobe.code.ins.Instruction()
                self._instdic[self._instrs[self._pointer]] = self._pointer
                self._pointer += 10
                if MICROPROBE_RC["verbose"]:
                    progress()
Esempio n. 8
0
def average(array, weights=None):
    """

    :param array:
    :type array:
    :param weights:
    :type weights:
    """

    if weights is None:
        weights = [1.0] * len(array)

    if len(weights) != len(array):
        raise MicroprobeValueError(
            "Length of weights not compatible with length of the array")

    ave_val = (sum([array[idx] * weights[idx]
                    for idx in range(0, len(array))]) * 1.0) / sum(weights)

    return ave_val
Esempio n. 9
0
def _shift_and_fix_variables(
    variables, offset, addresses, min_address, big_endian,
        ):
    """Shift variables and their contents."""
    svariables = []
    for variable in variables:

        variable = variable.copy()
        variable.address = variable.address + offset
        if variable.var_type.upper() in ["CHAR", "UINT8_T"]:
            cbytes = 8
            for idx in range(0, variable.num_elements):
                value = variable.init_value[idx:idx + cbytes]

                # Assuming 64-bit addresses
                if len(value) < cbytes:
                    continue

                # Assuming addresses are 8 byte aligned in memory
                if (variable.address + idx) % 8 != 0:
                    continue

                # Get value in bit/little endian
                if big_endian:
                    value_str = "".join([
                        "%02X" % val for val in value
                    ])
                else:
                    value_str = "".join([
                        "%02X" % val for val in reversed(value)
                    ])
                value = int(value_str, 16)

                if not (addresses[0] <= value <= addresses[1] and
                        value > min_address):
                    continue

                distance = min((abs(value - addr) for addr in addresses[2]))
                if distance > _FIX_ADDRESS_TOLERANCE:
                    continue

                nvalue = value + offset
                fmt_str = "%%0%dX" % (2 * cbytes)
                nvalue = fmt_str % nvalue

                print_info(
                    "Shifting pointer in variable: '%s' at"
                    " address '0x%016X' from '0x%s' to value "
                    "'0x%s" %
                    (variable.name, variable.address +
                     idx, value_str, nvalue),
                )

                if len(nvalue) > (2 * cbytes):
                    raise MicroprobeValueError(
                        "Overflow during variable shifting",
                    )
                if big_endian:
                    nvalue = [
                        int(nvalue[idx2:idx2 + 2], 16)
                        for idx2 in range(0, 2 * cbytes, 2)
                    ]
                else:
                    nvalue = [
                        int(nvalue[idx2:idx2 + 2], 16)
                        for idx2 in reversed(range(0, 2 * cbytes, 2))
                    ]

                variable.init_value[idx:idx + cbytes] = nvalue
        else:
            raise NotImplementedError(
                "Unable to shift variable "
                "type '%s'" % variable.var_type
            )

        svariables.append(variable)

    return svariables