Exemplo n.º 1
0
    def simplify(self):
        self.a = self.a.simplify()
        self.b = self.b.simplify()

        if isinstance(self.a, constant):
            if self.a.value == 0:
                return constant(0, self.a.type)

            if self.a.value == 1:
                return self.b

            if self.a.value == -1:
                return expr_uminus(self.b).simplify()

            pass

        if isinstance(self.b, constant):
            if self.b.value == 0:
                return constant(0, self.b.type)

            if self.b.value == 1:
                return self.a

            if self.b.value == -1:
                return expr_uminus(self.a).simplify()

            pass

        if isinstance(self.a, constant) and isinstance(self.b, constant):
            return constant(self.a.value + self.b.value, self.a.type)

        return self
Exemplo n.º 2
0
    def simplify(self):
        self.a = self.a.simplify()
        self.b = self.b.simplify()

        if isinstance(self.a, constant):
            if self.a.value == 0:
                return constant(0, self.a.type)

            if self.a.value == 1:
                return self.b

            if self.a.value == -1:
                return expr_uminus(self.b).simplify()

            pass

        if isinstance(self.b, constant):
            if self.b.value == 0:
                return constant(0, self.b.type)

            if self.b.value == 1:
                return self.a

            if self.b.value == -1:
                return expr_uminus(self.a).simplify()

            pass

        if isinstance(self.a, constant) and isinstance(self.b, constant):
            return constant(self.a.value + self.b.value, self.a.type)

        return self
Exemplo n.º 3
0
Arquivo: codegen.py Projeto: andyv/sse
def test_unary(operator):
    tn = type_node(type_int4)

    v1 = variable('v1', tn)
    v2 = variable('v2', tn)

    from ir_nodes import reg_b, reg_c

    ebx = reg_b.get_subreg(tn)
    ecx = reg_c.get_subreg(tn)

    mem1 = memory(0)
    mem1.name = '(mem1)'

    mem2 = memory(1)
    mem2.name = '(mem2)'

    const = constant('10', type_int4)

    op = operator(v2)
    assign = expr_assign(v1, op)
    assign.prev = assign.next = None

    def single(case0, a, b, live):
        v1.register = a
        v2.register = b

        assign.last_used = []
        annot = ''

        if live:
            assign.last_used.append(v2)
            annot = '   (%s dead)' % b.name
            pass

        reverse, insn_list = classify_cmp(v1, v2)
        print 'Case %d   %s = - %s    %s' % (case0, a, b, annot)

        case, x, y = classify_unary(assign)

        repl = { '@t': get_temp_reg(assign.var.type), 'op': assign.value.arith_op,
                 '@1': str(x), '@2': str(y) }

        insn_list = [ replace_insn(i, repl) for i in unary_seq[case] ]
        for insn in insn_list:
            print '       ' + insn
            pass

        print
        return

    single(1, ebx, ebx, 0)
    single(2, ebx, ecx, 0)
    single(3, ebx, mem1, 0)
    single(4, mem1, ebx, 0)
    single(5, mem1, ebx, 1)
    single(6, mem1, mem1, 0)
    single(7, mem1, mem2, 0)

    raise SystemExit
Exemplo n.º 4
0
    def simplify(self):
        self.a = self.a.simplify()
        self.b = self.b.simplify()

        if isinstance(self.a, constant) and isinstance(self.b, constant):
            a = self.value.a
            b = self.value.b

            if self.op == '==':
                rc = 1 if a == b else 0

            elif self.op == '<':
                rc = 1 if a < b else 0

            elif self.op == '<=':
                rc = 1 if a <= b else 0

            elif self.op == '>':
                rc = 1 if a > b else 0

            elif self.op == '<=':
                rc = 1 if a >= b else 0

            else:
                raise SystemExit, 'expr_compare.simplify(): Bad operator'

            return constant(rc, type_node(type_int4, 0))

        return self
Exemplo n.º 5
0
    def simplify(self):
        self.a = self.a.simplify()
        self.b = self.b.simplify()

        if isinstance(self.a, constant) and isinstance(self.b, constant):
            a = self.value.a
            b = self.value.b

            if self.op == '==':
                rc = 1 if a == b else 0

            elif self.op == '<':
                rc = 1 if a < b else 0

            elif self.op == '<=':
                rc = 1 if a <= b else 0

            elif self.op == '>':
                rc = 1 if a > b else 0

            elif self.op == '<=':
                rc = 1 if a >= b else 0

            else:
                raise SystemExit, 'expr_compare.simplify(): Bad operator'

            return constant(rc, type_node(type_int4, 0))

        return self
Exemplo n.º 6
0
    def simplify(self):
        self.a = self.a.simplify()
        self.b = self.b.simplify()

        if isinstance(self.a, constant):
            return self.b if self.a.value == 0 else constant(1, self.a.type)

        return self
Exemplo n.º 7
0
    def simplify(self):
        self.a = self.a.simplify()
        self.b = self.b.simplify()

        if isinstance(self.a, constant) and isinstance(self.b, constant):
            return constant(self.a.value | self.b.value, self.a.type)

        return self
Exemplo n.º 8
0
    def simplify(self):
        self.a = self.a.simplify()
        self.b = self.b.simplify()

        if isinstance(self.a, constant):
            return self.b if self.a.value == 0 else constant(1, self.a.type)

        return self
Exemplo n.º 9
0
    def simplify(self):
        self.a = self.a.simplify()
        self.b = self.b.simplify()

        if isinstance(self.a, constant) and isinstance(self.b, constant):
            return constant(self.a.value | self.b.value, self.a.type)

        return self
Exemplo n.º 10
0
    def simplify(self):
        self.a = self.a.simplify()
        self.b = self.b.simplify()

        if isinstance(self.a, constant) and self.a.value == 0:
            return expr_uminus(self.b).simplify()

        if isinstance(self.b, constant) and self.b.value == 0:
            return self.a

        if isinstance(self.a, constant) and isinstance(self.b, constant):
            return constant(self.a.value - self.b.value, self.a.type)

        return self
Exemplo n.º 11
0
    def simplify(self):
        self.a = self.a.simplify()
        self.b = self.b.simplify()

        if isinstance(self.a, constant) and self.a.value == 0:
            return expr_uminus(self.b).simplify()

        if isinstance(self.b, constant) and self.b.value == 0:
            return self.a

        if isinstance(self.a, constant) and isinstance(self.b, constant):
            return constant(self.a.value - self.b.value, self.a.type)

        return self
Exemplo n.º 12
0
Arquivo: codegen.py Projeto: andyv/sse
def predicate_insn(st):
    if isinstance(st, expr_compare):
        reverse, insn_list = classify_cmp(st.a, st.b)
        signed = signed_type(st.a.type) or signed_type(st.b.type)
        cond = st

    else:
        reverse, insn_list = classify_cmp(st, constant('0', st.type))
        cond = expr_not_equal
        signed = True
        pass

    if reverse:
        cond = opposite_cond[cond.__class__]
        pass

    return cond, signed, insn_list
Exemplo n.º 13
0
def predicate_insn(st):
    if isinstance(st, expr_compare):
        reverse, insn_list = classify_cmp(st.a, st.b)
        signed = signed_type(st.a.type) or signed_type(st.b.type)
        cond = st

    else:
        reverse, insn_list = classify_cmp(st, constant('0', st.type))
        cond = expr_not_equal
        signed = True
        pass

    if reverse:
        cond = opposite_cond[cond.__class__]
        pass

    return cond, signed, insn_list
Exemplo n.º 14
0
Arquivo: codegen.py Projeto: andyv/sse
def test_cmp():
    tn = type_node(type_int4)

    v1 = variable('v1', tn)
    v2 = variable('v2', tn)

    from ir_nodes import reg_b, reg_c

    ebx = reg_b.get_subreg(tn)
    ecx = reg_c.get_subreg(tn)

    mem1 = memory(0)
    mem1.name = '(mem1)'

    mem2 = memory(1)
    mem2.name = '(mem2)'

    const = constant('10', type_int4)

    def single(case0, a, b):
        v1.register = a
        v2.register = b

        reverse, insn_list = classify_cmp(v1, v2)
        print 'Case', case0, '   compare', a, ' to ', b, '    reverse =', reverse

        for insn in insn_list:
            print '       ' + insn
            pass

        print
        return

    single(1, ebx, ecx)
    single(2, ebx, mem1)
    single(3, ebx, const)
    single(4, mem1, ebx)
    single(5, mem1, mem2)
    single(6, mem1, const)
    single(7, const, ebx)
    single(8, const, mem1)

    raise SystemExit
Exemplo n.º 15
0
def test_cmp():
    tn = type_node(type_int4)

    v1 = variable('v1', tn)
    v2 = variable('v2', tn)

    from ir_nodes import reg_b, reg_c

    ebx = reg_b.get_subreg(tn)
    ecx = reg_c.get_subreg(tn)

    mem1 = memory(0)
    mem1.name = '(mem1)'

    mem2 = memory(1)
    mem2.name = '(mem2)'

    const = constant('10', type_int4)

    def single(case0, a, b):
        v1.register = a
        v2.register = b

        reverse, insn_list = classify_cmp(v1, v2)
        print 'Case', case0, '   compare', a, ' to ', b, '    reverse =', reverse

        for insn in insn_list:
            print '       ' + insn
            pass

        print
        return

    single(1, ebx, ecx)
    single(2, ebx, mem1)
    single(3, ebx, const)
    single(4, mem1, ebx)
    single(5, mem1, mem2)
    single(6, mem1, const)
    single(7, const, ebx)
    single(8, const, mem1)

    raise SystemExit
Exemplo n.º 16
0
Arquivo: codegen.py Projeto: andyv/sse
def test_binary(operator, seq_dict):
    tn = type_node(type_int4)

    v1 = variable('v1', tn)
    v2 = variable('v2', tn)
    v3 = variable('v3', tn)

    from ir_nodes import reg_b, reg_c, reg_d

    ebx = reg_b.get_subreg(tn)                          
    ecx = reg_c.get_subreg(tn)
    edx = reg_d.get_subreg(tn)

    mem1 = memory(0)
    mem1.name = '(mem1)'

    mem2 = memory(1)
    mem2.name = '(mem2)'

    mem3 = memory(2)
    mem3.name = '(mem3)'

    const = constant('10', type_int4)

    op = operator(v2, v3)
    assign = expr_assign(v1, op)
    assign.prev = assign.next = None

    def single(case_0, a, b, c, last_used):
        v1.register = a
        v2.register = b
        v3.register = c
        
        assign.last_used = []
        annot = ''

        if last_used & 2:
            assign.last_used.append(v2)
            annot += '    (%s dead)' % b.name
            pass

        if last_used & 1:
            assign.last_used.append(v3)
            annot += '    (%s dead)' % c.name
            pass

        print 'Case %d      %s = %s %s %s%s' % (case_0, a, b, operator.op, c, annot)

        case = classify_binary(assign)

        if case_0 != case:
            msg = 'test_binary(): Bad cases: %d/%d' % ( case_0, case )
            raise SystemExit, msg       

        repl = { '@1': assign.var, '@2': assign.value.a, '@3': assign.value.b,
                 'op': operator.arith_op, '@t': get_temp_reg(assign.value.type) }

        r = [ replace_insn(i, repl) for i in seq_dict[case] ]
        for insn in r:
            print '    ' + insn
            pass

        print
        return

# See classify_binary() for the case enumeration

    single(1, ebx, ebx, ecx, 0)
    single(2, ebx, ecx, ebx, 0)
    single(3, ebx, ecx, edx, 0)
    single(4, ebx, ebx, mem1, 0)
    single(5, ebx, ecx, mem1, 0)
    single(6, ebx, ebx, const, 0)
    single(7, ebx, ecx, const, 0)
    single(8, ebx, mem1, ebx, 0)
    single(9, ebx, mem1, ecx, 0)
    single(10, ebx, mem1, mem2, 0)
    single(11, ebx, mem1, const, 0)
    single(12, ebx, const, ebx, 0)
    single(13, ebx, const, ecx, 0)
    single(14, ebx, const, mem1, 0)

    single(15, mem1, ebx, ecx, 2)
    single(16, mem1, ebx, ecx, 1)
    single(17, mem1, ebx, ecx, 0)
    single(18, mem1, ebx, mem1, 0)
    single(19, mem1, ebx, mem2, 2)
    single(20, mem1, ebx, mem2, 0)
    single(21, mem1, ebx, const, 2)
    single(22, mem1, ebx, const, 0)

    single(23, mem1, mem1, ebx, 0)
    single(24, mem1, mem2, ebx, 1)
    single(25, mem1, mem2, ebx, 0)
    single(26, mem1, mem1, mem2, 0)
    single(27, mem1, mem2, mem1, 0)
    single(28, mem1, mem2, mem3, 0)
    single(29, mem1, mem2, const, 0)

    single(30, mem1, const, ebx, 1)
    single(31, mem1, const, ebx, 0)
    single(32, mem1, const, mem1, 0)
    single(33, mem1, const, mem2, 0)

    raise SystemExit
Exemplo n.º 17
0
def test_unary(operator):
    tn = type_node(type_int4)

    v1 = variable('v1', tn)
    v2 = variable('v2', tn)

    from ir_nodes import reg_b, reg_c

    ebx = reg_b.get_subreg(tn)
    ecx = reg_c.get_subreg(tn)

    mem1 = memory(0)
    mem1.name = '(mem1)'

    mem2 = memory(1)
    mem2.name = '(mem2)'

    const = constant('10', type_int4)

    op = operator(v2)
    assign = expr_assign(v1, op)
    assign.prev = assign.next = None

    def single(case0, a, b, live):
        v1.register = a
        v2.register = b

        assign.last_used = []
        annot = ''

        if live:
            assign.last_used.append(v2)
            annot = '   (%s dead)' % b.name
            pass

        reverse, insn_list = classify_cmp(v1, v2)
        print 'Case %d   %s = - %s    %s' % (case0, a, b, annot)

        case, x, y = classify_unary(assign)

        repl = {
            '@t': get_temp_reg(assign.var.type),
            'op': assign.value.arith_op,
            '@1': str(x),
            '@2': str(y)
        }

        insn_list = [replace_insn(i, repl) for i in unary_seq[case]]
        for insn in insn_list:
            print '       ' + insn
            pass

        print
        return

    single(1, ebx, ebx, 0)
    single(2, ebx, ecx, 0)
    single(3, ebx, mem1, 0)
    single(4, mem1, ebx, 0)
    single(5, mem1, ebx, 1)
    single(6, mem1, mem1, 0)
    single(7, mem1, mem2, 0)

    raise SystemExit
Exemplo n.º 18
0
def test_binary(operator, seq_dict):
    tn = type_node(type_int4)

    v1 = variable('v1', tn)
    v2 = variable('v2', tn)
    v3 = variable('v3', tn)

    from ir_nodes import reg_b, reg_c, reg_d

    ebx = reg_b.get_subreg(tn)
    ecx = reg_c.get_subreg(tn)
    edx = reg_d.get_subreg(tn)

    mem1 = memory(0)
    mem1.name = '(mem1)'

    mem2 = memory(1)
    mem2.name = '(mem2)'

    mem3 = memory(2)
    mem3.name = '(mem3)'

    const = constant('10', type_int4)

    op = operator(v2, v3)
    assign = expr_assign(v1, op)
    assign.prev = assign.next = None

    def single(case_0, a, b, c, last_used):
        v1.register = a
        v2.register = b
        v3.register = c

        assign.last_used = []
        annot = ''

        if last_used & 2:
            assign.last_used.append(v2)
            annot += '    (%s dead)' % b.name
            pass

        if last_used & 1:
            assign.last_used.append(v3)
            annot += '    (%s dead)' % c.name
            pass

        print 'Case %d      %s = %s %s %s%s' % (case_0, a, b, operator.op, c,
                                                annot)

        case = classify_binary(assign)

        if case_0 != case:
            msg = 'test_binary(): Bad cases: %d/%d' % (case_0, case)
            raise SystemExit, msg

        repl = {
            '@1': assign.var,
            '@2': assign.value.a,
            '@3': assign.value.b,
            'op': operator.arith_op,
            '@t': get_temp_reg(assign.value.type)
        }

        r = [replace_insn(i, repl) for i in seq_dict[case]]
        for insn in r:
            print '    ' + insn
            pass

        print
        return


# See classify_binary() for the case enumeration

    single(1, ebx, ebx, ecx, 0)
    single(2, ebx, ecx, ebx, 0)
    single(3, ebx, ecx, edx, 0)
    single(4, ebx, ebx, mem1, 0)
    single(5, ebx, ecx, mem1, 0)
    single(6, ebx, ebx, const, 0)
    single(7, ebx, ecx, const, 0)
    single(8, ebx, mem1, ebx, 0)
    single(9, ebx, mem1, ecx, 0)
    single(10, ebx, mem1, mem2, 0)
    single(11, ebx, mem1, const, 0)
    single(12, ebx, const, ebx, 0)
    single(13, ebx, const, ecx, 0)
    single(14, ebx, const, mem1, 0)

    single(15, mem1, ebx, ecx, 2)
    single(16, mem1, ebx, ecx, 1)
    single(17, mem1, ebx, ecx, 0)
    single(18, mem1, ebx, mem1, 0)
    single(19, mem1, ebx, mem2, 2)
    single(20, mem1, ebx, mem2, 0)
    single(21, mem1, ebx, const, 2)
    single(22, mem1, ebx, const, 0)

    single(23, mem1, mem1, ebx, 0)
    single(24, mem1, mem2, ebx, 1)
    single(25, mem1, mem2, ebx, 0)
    single(26, mem1, mem1, mem2, 0)
    single(27, mem1, mem2, mem1, 0)
    single(28, mem1, mem2, mem3, 0)
    single(29, mem1, mem2, const, 0)

    single(30, mem1, const, ebx, 1)
    single(31, mem1, const, ebx, 0)
    single(32, mem1, const, mem1, 0)
    single(33, mem1, const, mem2, 0)

    raise SystemExit