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
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
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
def __init__(self, *args): self.a, self.b = args # Keep constants typeless. if isinstance(self.a, constant) and isinstance(self.b, constant): return if isinstance(self.a, constant): self.type = self.b.type return if isinstance(self.b, constant): self.type = self.a.type return ta = self.a.type.basic_type tb = self.b.type.basic_type try: self.type = type_node(self.type_map[self.op][ta, tb], 0) except KeyError: msg = 'Operator "%s" cannot use arguments %s/%s' % \ (self.op, ta.name, tb.name) raise parse_error, msg if self.type.basic_type != ta: self.a = expr_type_conv(self.type, self.a) pass if self.type.basic_type != tb: self.b = expr_type_conv(self.type, self.b) pass return
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
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
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