From c5c38c6376025895316df4fbf2570ed2d43bc4d6 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sun, 7 Sep 2014 15:09:38 +0800 Subject: [PATCH] py2llvm: remove unnecessary indirection for unary operators --- artiq/py2llvm/ast_body.py | 34 ++++++++++++---------------------- artiq/py2llvm/values.py | 16 ---------------- 2 files changed, 12 insertions(+), 38 deletions(-) diff --git a/artiq/py2llvm/ast_body.py b/artiq/py2llvm/ast_body.py index c14b2dfdd..b0617ddda 100644 --- a/artiq/py2llvm/ast_body.py +++ b/artiq/py2llvm/ast_body.py @@ -54,13 +54,13 @@ class Visitor: def _visit_expr_UnaryOp(self, node): ast_unops = { - ast.Invert: values.operators.inv, - ast.Not: values.operators.not_, - ast.UAdd: values.operators.pos, - ast.USub: values.operators.neg + ast.Invert: "o_inv", + ast.Not: "o_not", + ast.UAdd: "o_pos", + ast.USub: "o_neg" } - return ast_unops[type(node.op)](self.visit_expression(node.operand), - self.builder) + value = self.visit_expression(node.operand) + return getattr(value, ast_unops[type(node.op)])(self.builder) def _visit_expr_BinOp(self, node): ast_binops = { @@ -104,17 +104,10 @@ class Visitor: return r def _visit_expr_Call(self, node): - ast_unfuns = { - "bool": values.operators.bool, - "int": values.operators.int, - "int64": values.operators.int64, - "round": values.operators.round, - "round64": values.operators.round64, - } fn = node.func.id - if fn in ast_unfuns: - return ast_unfuns[fn](self.visit_expression(node.args[0]), - self.builder) + if fn in {"bool", "int", "int64", "round", "round64"}: + value = self.visit_expression(node.args[0]) + return getattr(value, "o_"+fn)(self.builder) elif fn == "Fraction": r = fractions.VFraction() if self.builder is not None: @@ -168,8 +161,7 @@ class Visitor: else_block = function.append_basic_block("i_else") merge_block = function.append_basic_block("i_merge") - condition = values.operators.bool(self.visit_expression(node.test), - self.builder) + condition = self.visit_expression(node.test).o_bool(self.builder) self.builder.cbranch(condition.get_ssa_value(self.builder), then_block, else_block) @@ -191,16 +183,14 @@ class Visitor: else_block = function.append_basic_block("w_else") merge_block = function.append_basic_block("w_merge") - condition = values.operators.bool( - self.visit_expression(node.test), self.builder) + condition = self.visit_expression(node.test).o_bool(self.builder) self.builder.cbranch( condition.get_ssa_value(self.builder), body_block, else_block) self.builder.position_at_end(body_block) self.visit_statements(node.body) if not is_terminated(self.builder.basic_block): - condition = values.operators.bool( - self.visit_expression(node.test), self.builder) + condition = self.visit_expression(node.test).o_bool(self.builder) self.builder.cbranch( condition.get_ssa_value(self.builder), body_block, merge_block) diff --git a/artiq/py2llvm/values.py b/artiq/py2llvm/values.py index 8adbd72a7..711b0cdb9 100644 --- a/artiq/py2llvm/values.py +++ b/artiq/py2llvm/values.py @@ -40,18 +40,6 @@ class VGeneric: return self.o_roundx(64, builder) -def _make_unary_operator(op_name): - def op(x, builder): - try: - opf = getattr(x, "o_"+op_name) - except AttributeError: - raise TypeError( - "Unsupported operand type for {}: {}" - .format(op_name, type(x).__name__)) - return opf(builder) - return op - - def _make_binary_operator(op_name): def op(l, r, builder): try: @@ -77,10 +65,6 @@ def _make_binary_operator(op_name): def _make_operators(): d = dict() - for op_name in ("bool", "int", "int64", "round", "round64", - "inv", "pos", "neg"): - d[op_name] = _make_unary_operator(op_name) - d["not_"] = _make_unary_operator("not") for op_name in ("add", "sub", "mul", "truediv", "floordiv", "mod", "pow", "lshift", "rshift", "xor",