Exemple #1
0
    def __call__(self, building_block, target):
        """

        :param building_block:
        :param target:

        """

        reg = [
            reg for reg in target.registers.values()
            if reg.name == self._register_name
        ][0]

        value = self._value

        if (reg in building_block.context.reserved_registers
                and self._reserve):
            raise MicroprobeCodeGenerationError("Register '%s' already"
                                                " reserved" % str(reg))

        if reg in target.control_registers and self._force_control is False:
            raise MicroprobeCodeGenerationError(
                "Register '%s' in Target definition control"
                " registers" % str(reg))

        if callable(value):
            value = value()

        if reg.used_for_float_arithmetic:
            value = ieee_float_to_int64(float(value))

        if (target.wrapper.direct_initialization_support
                and not self._force_code):
            target.wrapper.register_direct_init(reg, value)
        else:
            building_block.add_init(
                target.set_register(reg, value, building_block.context))

        building_block.context.set_register_value(reg, value)

        if (self._reserve is not False
                and reg not in building_block.context.reserved_registers):
            building_block.context.add_reserved_registers([reg])
Exemple #2
0
    def __call__(self, building_block, target):
        """

        :param building_block:
        :param target:

        """

        variable_to_declare = False
        addresses_set = []
        context = building_block.context

        for bbl in building_block.cfg.bbls:
            for instr in bbl.instrs:

                # if not instr.hfp_exponent_overflow_exception:
                #    continue

                is_float = False
                for operand in instr.operands():

                    if operand.type.float and operand.is_input:
                        is_float = True

                if not is_float:
                    continue

                for memoperand in instr.memory_operands():

                    if memoperand.address is None:
                        continue

                    if memoperand.address in addresses_set:
                        continue

                    if memoperand.is_load:

                        if variable_to_declare is False:

                            variable_to_declare = True

                            reg2 = target.get_register_for_address_arithmetic(
                                context)

                            instrs = target.set_register(
                                reg2, ieee_float_to_int64(self._value),
                                context)

                            building_block.add_init(instrs)

                            context.set_register_value(
                                reg2, ieee_float_to_int64(self._value))

                            context.add_reserved_registers([reg2])

                            reg1 = target.get_register_for_address_arithmetic(
                                context)

                            context.add_reserved_registers([reg1])

                        instrs = target.set_register_to_address(
                            reg1, memoperand.address, context)

                        context.set_register_value(reg1, memoperand.address)

                        instrs += target.store_integer(reg2,
                                                       memoperand.address, 64,
                                                       context)

                        context.set_memory_value(
                            MemoryValue(memoperand.address, self._value, 8))

                        addresses_set.append(memoperand.address)

                        building_block.add_init(instrs)

                        LOG.debug("Instruction: %s: %s", instr.address, instr)
                        LOG.debug("Address: %s", memoperand.address)

        if variable_to_declare:
            context.remove_reserved_registers([reg1, reg2])
Exemple #3
0
    def declare_global_var(self, var):
        """

        :param var:

        """

        section_name = ".data.microprobe.data.%s" % var.name

        section_address = var.address.displacement

        if var.address.base_address == "code":
            section_address += 0
        elif var.address.base_address == "data":
            section_address += 0
        else:
            section_address = None
            # raise NotImplementedError(str(var.address))

        # make sure we always have the same state
        random = Random()
        random.seed(10)

        if var.array():
            typesize = var.size // var.elems
        else:
            typesize = var.size

        str_fmt = "%%0%dx" % (typesize * 2)

        myvalue = var.value
        if myvalue is None:
            if var.array():
                myvalue = [random.randint(0, (2**(typesize*8)-1))
                           for elem in range(0, var.elems)]
            else:
                myvalue = random.randint(0, (2**(typesize*8)-1))

        if not var.array:
            if not isinstance(myvalue, list):
                value = myvalue
            else:
                LOG.warning(
                    "Multiple initial values specified for a "
                    "single element variable. Var: '%s'", var
                )
                value = myvalue[0]
            values = [value]
        else:
            elems = var.size // typesize
            if not isinstance(myvalue, list):
                values = [myvalue] * elems
            else:
                values = (myvalue * ((len(myvalue) // elems) + 1))[
                         0:elems]

        for idx, value in enumerate(values):
            if callable(value):
                value = value()
            if isinstance(value, float):
                values[idx] = ieee_float_to_int64(value)
                assert var.type in ["float", "double"]
            elif isinstance(value, six.integer_types):
                values[idx] = value
            else:
                raise MicroprobeCodeGenerationError(
                    "Unable to initialize variable var: '%s' "
                    "to value '%s'" % (myvalue, type(myvalue))
                )

        value_str = [str_fmt % value for value in values]
        value_str = "".join(value_str)
        value_str = ["0x%s" % value_str[i:i+2]
                     for i in range(0, len(value_str), 2)]
        value_str = ".byte %s" % ",".join(value_str)

        astr = []
        if False:
            if var.array():

                value = var.value
                if var.value is not None:
                    value = [0]

                valuestr = ""

                if not isinstance(value, list):
                    value = [value]

                get_value = getnextf(itertools.cycle(value))

                for dummy_idx in range(var.elems):
                    value = get_value()
                    if callable(value):
                        value = value()
                    valuestr = "%s%s," % (valuestr, value)

            else:

                value = var.value
                if var.value is not None:
                    value = 0

                valuestr = ".byte 0x%x" % value

        if section_address is not None:
            astr.append("# MICROPROBE LD @    %s 0x%X : { *(%s) }  " %
                        (section_name, section_address, section_name))
        else:
            astr.append("# MICROPROBE LD @    %s : { *(%s) }  " %
                        (section_name, section_name))
        astr.append(".section %s" % section_name)
        astr.append(".global %s" % var.name)
        astr.append("%s:" % var.name)
        astr.append("%s\n" % value_str)

        return "\n".join(astr)
Exemple #4
0
    def __call__(self, building_block, target):
        """

        :param building_block:
        :param target:

        """

        if not self._skip_unknown:
            for register_name in self._reg_dict:
                if register_name not in list(target.registers.keys()):
                    raise MicroprobeCodeGenerationError(
                        "Unknown register name: '%s'. Unable to set it" %
                        register_name)

        if self._warn_unknown:
            for register_name in self._reg_dict:
                if register_name not in list(target.registers.keys()):
                    print_warning(
                        "Unknown register name: '%s'. Unable to set it" %
                        register_name)

        for reg in target.registers.values():

            value = None
            elemsize = None

            if reg.name in self._reg_dict:
                value = self._reg_dict[reg.name]
                self._reg_dict.pop(reg.name)

            if (reg in building_block.context.reserved_registers
                    and not self._force_reserved):
                LOG.debug("Skip reserved - %s", reg)
                continue
            elif reg in target.control_registers:
                LOG.debug("Skip control - %s", reg)
                continue

            if value is None:
                if reg.used_for_vector_arithmetic:
                    if self._vect_value is not None:
                        value = self._vect_value
                        elemsize = self._vect_elemsize
                    else:
                        LOG.debug("Skip no vector default value provided - %s",
                                  reg)
                        continue
                elif reg.used_for_float_arithmetic:
                    if self._fp_value is not None:
                        value = self._fp_value
                    else:
                        LOG.debug("Skip no float default value provided - %s",
                                  reg)
                        continue
                else:
                    if self._value is not None:
                        value = self._value
                    else:
                        LOG.debug("Skip no default value provided - %s", reg)
                        continue

                while callable(value):
                    value = value()

                if reg.used_for_float_arithmetic:

                    value = ieee_float_to_int64(float(value))

                elif reg.used_for_vector_arithmetic:

                    if isinstance(value, float):
                        if elemsize != 64:
                            raise MicroprobeCodeGenerationError(
                                "Unable to initialize '%s' to '%s'. Only 64bit"
                                " vector element initialization is supported" %
                                (reg.name, (value, elemsize)))
                        value = ieee_float_to_int64(float(value))
                        value = "%d_%d" % (value, elemsize)
                    else:
                        value = "%d_%d" % (value, elemsize)

            LOG.debug("Set '%s' to '0x%x'", reg, value)

            if (target.wrapper.direct_initialization_support
                    and not self._force_code):

                try:
                    target.wrapper.register_direct_init(reg, value)
                except MicroprobeCodeGenerationError:
                    building_block.add_init(
                        target.set_register(reg, value,
                                            building_block.context))
                except MicroprobeDuplicatedValueError:
                    LOG.debug("Skip already set - %s", reg)
            else:
                building_block.add_init(
                    target.set_register(reg, value, building_block.context))

            building_block.context.set_register_value(reg, value)