Exemplo n.º 1
0
    def __str__(self):
        ''' Returns a smtlib representation of the current state '''
        result = ''
        translator = TranslatorSmtlib(use_bindings=True)
        for expression in self.constraints:
            translator.visit(expression)

        for d in self.declarations:
            result += d.declaration + '\n'

        for name, exp, smtlib in translator.bindings:
            if isinstance(exp, BitVec):
                result += '(declare-fun %s () (_ BitVec %d))'%(name, exp.size)
            elif isinstance(exp, Bool):
                result += '(declare-fun %s () Bool)' % name
            elif isinstance(exp, Array):
                result += '(declare-fun %s () (Array (_ BitVec %d) (_ BitVec 8)))' % (name, exp.index_bits)
            else:
                raise Exception("Type not supported %r", exp)
            result += '(assert (= %s %s))\n' % (name, smtlib)

        r = translator.pop()
        while r is not None:
            result += '(assert %s)\n' % r
            r = translator.pop()

        return result

        buf = ''
        for d in self.declarations:
            buf += d.declaration + '\n'
        for a in self.constraints:
            buf += '(assert %s)\n' % translate_to_smtlib(a, use_bindings=True)
        return buf
Exemplo n.º 2
0
    def related_to(self, expression):
        if isinstance(expression, BoolConstant) and expression.value is True:
            return str(self)

        N = len(self.constraints)
        related_variables = get_variables(expression)
        related_constraints = set()
        remaining_constraints = set(self.constraints)

        added = True
        while added:
            added = False
            logger.debug('Related variables %r', map(lambda x: x.name, related_variables))
            for constraint in list(remaining_constraints):
                variables = get_variables(constraint)
                if related_variables & variables:
                    remaining_constraints.remove(constraint)
                    related_constraints.add(constraint)
                    related_variables |= variables
                    added = True
                    
        result = ''
        for var in related_variables:
            result += var.declaration + '\n'
#        for prop in related_constraints:
#            result += '(assert %s)\n' % translate_to_smtlib(prop, bindings=True)

        translator = TranslatorSmtlib(use_bindings=True)
        for expression in related_constraints:
            translator.visit(expression)

        for name, exp, smtlib in translator.bindings:
            if isinstance(exp, BitVec):
                result += '(declare-fun %s () (_ BitVec %d))'%(name, exp.size)
            elif isinstance(exp, Bool):
                result += '(declare-fun %s () Bool)' % name
            elif isinstance(exp, Array):
                result += '(declare-fun %s () (Array (_ BitVec %d) (_ BitVec 8)))' % (name, exp.index_bits)
            else:
                raise Exception("Type not supported %r", exp)
            result += '(assert (= %s %s))\n' % (name, smtlib)

        r = translator.pop()
        while r is not None:
            result += '(assert %s)\n' % r
            r = translator.pop()

        logger.debug('Reduced %d constraints!!', N - len(related_constraints))

        return result
Exemplo n.º 3
0
    def __str__(self):
        ''' Returns a smtlib representation of the current state '''
        result = ''
        translator = TranslatorSmtlib(use_bindings=True)
        for expression in self.constraints:
            translator.visit(expression)

        for d in self.declarations:
            result += d.declaration + '\n'

        for name, exp, smtlib in translator.bindings:
            if isinstance(exp, BitVec):
                result += '(declare-fun %s () (_ BitVec %d))'%(name, exp.size)
            elif isinstance(exp, Bool):
                result += '(declare-fun %s () Bool)' % name
            elif isinstance(exp, Array):
                result += '(declare-fun %s () (Array (_ BitVec %d) (_ BitVec 8)))' % (name, exp.index_bits)
            else:
                raise Exception("Type not supported %r", exp)
            result += '(assert (= %s %s))\n' % (name, smtlib)

        r = translator.pop()
        while r is not None:
            result += '(assert %s)\n' % r
            r = translator.pop()

        return result

        buf = ''
        for d in self.declarations:
            buf += d.declaration + '\n'
        for a in self.constraints:
            buf += '(assert %s)\n' % translate_to_smtlib(a, use_bindings=True)
        return buf
Exemplo n.º 4
0
    def __str__(self):
        ''' Returns a smtlib representation of the current state '''
        result = ''
        translator = TranslatorSmtlib()
        for expression in self.constraints:
            translator.visit(simplify(expression))

        # band aid hack around the fact that we are double declaring stuff :( :(
        tmp = set()
        for d in self.declarations:
            tmp.add(d.declaration)
        for d in tmp:
            result += d + '\n'

        for name, exp, smtlib in translator.bindings:
            if isinstance(exp, BitVec):
                result += '(declare-fun %s () (_ BitVec %d))' % (name, exp.size)
            elif isinstance(exp, Bool):
                result += '(declare-fun %s () Bool)' % name
            elif isinstance(exp, Array):
                result += '(declare-fun %s () (Array (_ BitVec %d) (_ BitVec %d)))' % (name, exp.index_bits, exp.value_bits)
            else:
                raise Exception("Type not supported %r", exp)
            result += '(assert (= %s %s))\n' % (name, smtlib)

        constraint_str = translator.pop()
        while constraint_str is not None:
            if constraint_str != 'true':
                result += '(assert %s)\n' % constraint_str
            constraint_str = translator.pop()

        return self.to_string()
Exemplo n.º 5
0
    def related_to(self, expression):
        if isinstance(expression, BoolConstant) and expression.value is True:
            return str(self)

        N = len(self.constraints)
        related_variables = get_variables(expression)
        related_constraints = set()
        remaining_constraints = set(self.constraints)

        added = True
        while added:
            added = False
            logger.debug('Related variables %r',
                         map(lambda x: x.name, related_variables))
            for constraint in list(remaining_constraints):
                variables = get_variables(constraint)
                if related_variables & variables:
                    remaining_constraints.remove(constraint)
                    related_constraints.add(constraint)
                    related_variables |= variables
                    added = True

        result = ''
        for var in related_variables:
            result += var.declaration + '\n'
#        for prop in related_constraints:
#            result += '(assert %s)\n' % translate_to_smtlib(prop, bindings=True)

        translator = TranslatorSmtlib(use_bindings=True)
        for expression in related_constraints:
            translator.visit(expression)

        for name, exp, smtlib in translator.bindings:
            if isinstance(exp, BitVec):
                result += '(declare-fun %s () (_ BitVec %d))' % (name,
                                                                 exp.size)
            elif isinstance(exp, Bool):
                result += '(declare-fun %s () Bool)' % name
            elif isinstance(exp, Array):
                result += '(declare-fun %s () (Array (_ BitVec %d) (_ BitVec %d)))' % (
                    name, exp.index_bits, exp.value_bits)
            else:
                raise Exception("Type not supported %r", exp)
            result += '(assert (= %s %s))\n' % (name, smtlib)

        constraint_str = translator.pop()
        while constraint_str is not None:
            if constraint_str != 'true':
                result += '(assert %s)\n' % constraint_str
            constraint_str = translator.pop()

        logger.debug('Reduced %d constraints!!', N - len(related_constraints))

        return result
Exemplo n.º 6
0
    def to_string(self, related_to=None, replace_constants=False):
        replace_constants = True
        related_variables, related_constraints = self.__get_related(related_to)

        if replace_constants:
            constant_bindings = {}
            for expression in self.constraints:
                if isinstance(expression, BoolEq) and \
                   isinstance(expression.operands[0], Variable) and \
                   isinstance(expression.operands[1], Constant):
                    constant_bindings[expression.operands[0]] = expression.operands[1]

        tmp = set()
        result = ''
        for var in related_variables:
            # FIXME
            # band aid hack around the fact that we are double declaring stuff :( :(
            if var.declaration in tmp:
                #logger.warning("Variable '%s' was copied twice somewhere", var.name)
                continue
            tmp.add(var.declaration)
            result += var.declaration + '\n'

        translator = TranslatorSmtlib(use_bindings=True)
        for constraint in related_constraints:
            if replace_constants:
                constraint = simplify(replace(constraint, constant_bindings))
            translator.visit(constraint)

        for name, exp, smtlib in translator.bindings:
            if isinstance(exp, BitVec):
                result += '(declare-fun %s () (_ BitVec %d))' % (name, exp.size)
            elif isinstance(exp, Bool):
                result += '(declare-fun %s () Bool)' % name
            elif isinstance(exp, Array):
                result += '(declare-fun %s () (Array (_ BitVec %d) (_ BitVec %d)))' % (name, exp.index_bits, exp.value_bits)
            else:
                raise Exception("Type not supported %r", exp)
            result += '(assert (= %s %s))\n' % (name, smtlib)

        constraint_str = translator.pop()
        while constraint_str is not None:
            if constraint_str != 'true':
                result += '(assert %s)\n' % constraint_str
            constraint_str = translator.pop()
        return result