From a647e1104dfc2a292b6b20b2b3fc559ffeb8df1c Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Fri, 5 Sep 2014 22:18:31 +0800 Subject: [PATCH] compiler: split into transforms and py2llvm --- artiq/devices/core.py | 14 ++-- artiq/devices/runtime.py | 8 +- artiq/{compiler/ir.py => py2llvm/__init__.py} | 12 +-- .../ir_ast_body.py => py2llvm/ast_body.py} | 74 +++++++++---------- .../infer_types.py} | 2 +- .../ir_values.py => py2llvm/values.py} | 0 artiq/transforms/__init__.py | 0 .../fold_constants.py | 2 +- artiq/{compiler => transforms}/inline.py | 2 +- artiq/{compiler => transforms}/interleave.py | 2 +- artiq/{compiler => transforms}/lower_time.py | 2 +- artiq/{compiler => transforms}/lower_units.py | 2 +- artiq/{compiler => transforms}/tools.py | 0 artiq/{compiler => transforms}/unparse.py | 0 .../{compiler => transforms}/unroll_loops.py | 2 +- 15 files changed, 61 insertions(+), 61 deletions(-) rename artiq/{compiler/ir.py => py2llvm/__init__.py} (73%) rename artiq/{compiler/ir_ast_body.py => py2llvm/ast_body.py} (77%) rename artiq/{compiler/ir_infer_types.py => py2llvm/infer_types.py} (97%) rename artiq/{compiler/ir_values.py => py2llvm/values.py} (100%) create mode 100644 artiq/transforms/__init__.py rename artiq/{compiler => transforms}/fold_constants.py (98%) rename artiq/{compiler => transforms}/inline.py (99%) rename artiq/{compiler => transforms}/interleave.py (99%) rename artiq/{compiler => transforms}/lower_time.py (97%) rename artiq/{compiler => transforms}/lower_units.py (96%) rename artiq/{compiler => transforms}/tools.py (100%) rename artiq/{compiler => transforms}/unparse.py (100%) rename artiq/{compiler => transforms}/unroll_loops.py (96%) diff --git a/artiq/devices/core.py b/artiq/devices/core.py index 6691a7e7f..7e197afd7 100644 --- a/artiq/devices/core.py +++ b/artiq/devices/core.py @@ -1,10 +1,10 @@ -from artiq.compiler.inline import inline -from artiq.compiler.lower_units import lower_units -from artiq.compiler.fold_constants import fold_constants -from artiq.compiler.unroll_loops import unroll_loops -from artiq.compiler.interleave import interleave -from artiq.compiler.lower_time import lower_time -from artiq.compiler.ir import get_runtime_binary +from artiq.transforms.inline import inline +from artiq.transforms.lower_units import lower_units +from artiq.transforms.fold_constants import fold_constants +from artiq.transforms.unroll_loops import unroll_loops +from artiq.transforms.interleave import interleave +from artiq.transforms.lower_time import lower_time +from artiq.py2llvm import get_runtime_binary class Core: diff --git a/artiq/devices/runtime.py b/artiq/devices/runtime.py index 7f4f46d0e..cfc0beb71 100644 --- a/artiq/devices/runtime.py +++ b/artiq/devices/runtime.py @@ -1,7 +1,7 @@ from llvm import core as lc from llvm import target as lt -from artiq.compiler import ir_values +from artiq.py2llvm import values lt.initialize_all() @@ -21,9 +21,9 @@ _chr_to_type = { } _chr_to_value = { - "n": lambda: ir_values.VNone(), - "i": lambda: ir_values.VInt(), - "I": lambda: ir_values.VInt(64) + "n": lambda: values.VNone(), + "i": lambda: values.VInt(), + "I": lambda: values.VInt(64) } diff --git a/artiq/compiler/ir.py b/artiq/py2llvm/__init__.py similarity index 73% rename from artiq/compiler/ir.py rename to artiq/py2llvm/__init__.py index 6dd997f26..ba273cb8c 100644 --- a/artiq/compiler/ir.py +++ b/artiq/py2llvm/__init__.py @@ -1,19 +1,19 @@ from llvm import core as lc from llvm import passes as lp -from artiq.compiler import ir_infer_types, ir_ast_body, ir_values +from artiq.py2llvm import infer_types, ast_body, values -def compile_function(module, env, funcdef): +def _compile_function(module, env, funcdef): function_type = lc.Type.function(lc.Type.void(), []) function = module.add_function(function_type, funcdef.name) bb = function.append_basic_block("entry") builder = lc.Builder.new(bb) - ns = ir_infer_types.infer_types(env, funcdef) + ns = infer_types.infer_types(env, funcdef) for k, v in ns.items(): v.alloca(builder, k) - visitor = ir_ast_body.Visitor(env, ns, builder) + visitor = ast_body.Visitor(env, ns, builder) visitor.visit_statements(funcdef.body) builder.ret_void() @@ -21,9 +21,9 @@ def compile_function(module, env, funcdef): def get_runtime_binary(env, funcdef): module = lc.Module.new("main") env.init_module(module) - ir_values.init_module(module) + values.init_module(module) - compile_function(module, env, funcdef) + _compile_function(module, env, funcdef) pass_manager = lp.PassManager.new() pass_manager.add(lp.PASS_MEM2REG) diff --git a/artiq/compiler/ir_ast_body.py b/artiq/py2llvm/ast_body.py similarity index 77% rename from artiq/compiler/ir_ast_body.py rename to artiq/py2llvm/ast_body.py index b4dc901f7..2e3833c77 100644 --- a/artiq/compiler/ir_ast_body.py +++ b/artiq/py2llvm/ast_body.py @@ -1,6 +1,6 @@ import ast -from artiq.compiler import ir_values +from artiq.py2llvm import values class Visitor: @@ -29,9 +29,9 @@ class Visitor: def _visit_expr_NameConstant(self, node): v = node.value if v is None: - r = ir_values.VNone() + r = values.VNone() elif isinstance(v, bool): - r = ir_values.VBool() + r = values.VBool() else: raise NotImplementedError if self.builder is not None: @@ -42,9 +42,9 @@ class Visitor: n = node.n if isinstance(n, int): if abs(n) < 2**31: - r = ir_values.VInt() + r = values.VInt() else: - r = ir_values.VInt(64) + r = values.VInt(64) else: raise NotImplementedError if self.builder is not None: @@ -53,28 +53,28 @@ class Visitor: def _visit_expr_UnaryOp(self, node): ast_unops = { - ast.Invert: ir_values.operators.inv, - ast.Not: ir_values.operators.not_, - ast.UAdd: ir_values.operators.pos, - ast.USub: ir_values.operators.neg + ast.Invert: values.operators.inv, + ast.Not: values.operators.not_, + ast.UAdd: values.operators.pos, + ast.USub: values.operators.neg } return ast_unops[type(node.op)](self.visit_expression(node.operand), self.builder) def _visit_expr_BinOp(self, node): ast_binops = { - ast.Add: ir_values.operators.add, - ast.Sub: ir_values.operators.sub, - ast.Mult: ir_values.operators.mul, - ast.Div: ir_values.operators.truediv, - ast.FloorDiv: ir_values.operators.floordiv, - ast.Mod: ir_values.operators.mod, - ast.Pow: ir_values.operators.pow, - ast.LShift: ir_values.operators.lshift, - ast.RShift: ir_values.operators.rshift, - ast.BitOr: ir_values.operators.or_, - ast.BitXor: ir_values.operators.xor, - ast.BitAnd: ir_values.operators.and_ + ast.Add: values.operators.add, + ast.Sub: values.operators.sub, + ast.Mult: values.operators.mul, + ast.Div: values.operators.truediv, + ast.FloorDiv: values.operators.floordiv, + ast.Mod: values.operators.mod, + ast.Pow: values.operators.pow, + ast.LShift: values.operators.lshift, + ast.RShift: values.operators.rshift, + ast.BitOr: values.operators.or_, + ast.BitXor: values.operators.xor, + ast.BitAnd: values.operators.and_ } return ast_binops[type(node.op)](self.visit_expression(node.left), self.visit_expression(node.right), @@ -82,12 +82,12 @@ class Visitor: def _visit_expr_Compare(self, node): ast_cmps = { - ast.Eq: ir_values.operators.eq, - ast.NotEq: ir_values.operators.ne, - ast.Lt: ir_values.operators.lt, - ast.LtE: ir_values.operators.le, - ast.Gt: ir_values.operators.gt, - ast.GtE: ir_values.operators.ge + ast.Eq: values.operators.eq, + ast.NotEq: values.operators.ne, + ast.Lt: values.operators.lt, + ast.LtE: values.operators.le, + ast.Gt: values.operators.gt, + ast.GtE: values.operators.ge } comparisons = [] old_comparator = self.visit_expression(node.left) @@ -99,23 +99,23 @@ class Visitor: old_comparator = comparator r = comparisons[0] for comparison in comparisons[1:]: - r = ir_values.operators.and_(r, comparison) + r = values.operators.and_(r, comparison) return r def _visit_expr_Call(self, node): ast_unfuns = { - "bool": ir_values.operators.bool, - "int": ir_values.operators.int, - "int64": ir_values.operators.int64, - "round": ir_values.operators.round, - "round64": ir_values.operators.round64, + "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) elif fn == "Fraction": - r = ir_values.VFraction() + r = values.VFraction() if self.builder is not None: numerator = self.visit_expression(node.args[0]) denominator = self.visit_expression(node.args[1]) @@ -164,7 +164,7 @@ class Visitor: else_block = function.append_basic_block("i_else") merge_block = function.append_basic_block("i_merge") - condition = ir_values.operators.bool(self.visit_expression(node.test), + condition = values.operators.bool(self.visit_expression(node.test), self.builder) self.builder.cbranch(condition.get_ssa_value(self.builder), then_block, else_block) @@ -185,14 +185,14 @@ class Visitor: else_block = function.append_basic_block("w_else") merge_block = function.append_basic_block("w_merge") - condition = ir_values.operators.bool( + condition = values.operators.bool( self.visit_expression(node.test), 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) - condition = ir_values.operators.bool( + condition = values.operators.bool( self.visit_expression(node.test), self.builder) self.builder.cbranch( condition.get_ssa_value(self.builder), body_block, merge_block) diff --git a/artiq/compiler/ir_infer_types.py b/artiq/py2llvm/infer_types.py similarity index 97% rename from artiq/compiler/ir_infer_types.py rename to artiq/py2llvm/infer_types.py index 7d385a106..2057d4eaa 100644 --- a/artiq/compiler/ir_infer_types.py +++ b/artiq/py2llvm/infer_types.py @@ -2,7 +2,7 @@ import ast from operator import itemgetter from copy import deepcopy -from artiq.compiler.ir_ast_body import Visitor +from artiq.py2llvm.ast_body import Visitor class _TypeScanner(ast.NodeVisitor): diff --git a/artiq/compiler/ir_values.py b/artiq/py2llvm/values.py similarity index 100% rename from artiq/compiler/ir_values.py rename to artiq/py2llvm/values.py diff --git a/artiq/transforms/__init__.py b/artiq/transforms/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/artiq/compiler/fold_constants.py b/artiq/transforms/fold_constants.py similarity index 98% rename from artiq/compiler/fold_constants.py rename to artiq/transforms/fold_constants.py index ddebbbf71..6ee230061 100644 --- a/artiq/compiler/fold_constants.py +++ b/artiq/transforms/fold_constants.py @@ -1,7 +1,7 @@ import ast import operator -from artiq.compiler.tools import * +from artiq.transforms.tools import * from artiq.language.core import int64, round64 diff --git a/artiq/compiler/inline.py b/artiq/transforms/inline.py similarity index 99% rename from artiq/compiler/inline.py rename to artiq/transforms/inline.py index 3e5af0b70..8dc7807a2 100644 --- a/artiq/compiler/inline.py +++ b/artiq/transforms/inline.py @@ -4,7 +4,7 @@ import inspect import textwrap import ast -from artiq.compiler.tools import eval_ast, value_to_ast +from artiq.transforms.tools import eval_ast, value_to_ast from artiq.language import core as core_language from artiq.language import units diff --git a/artiq/compiler/interleave.py b/artiq/transforms/interleave.py similarity index 99% rename from artiq/compiler/interleave.py rename to artiq/transforms/interleave.py index 1494e734b..d8ab192c2 100644 --- a/artiq/compiler/interleave.py +++ b/artiq/transforms/interleave.py @@ -1,7 +1,7 @@ import ast import types -from artiq.compiler.tools import * +from artiq.transforms.tools import * # -1 statement duration could not be pre-determined diff --git a/artiq/compiler/lower_time.py b/artiq/transforms/lower_time.py similarity index 97% rename from artiq/compiler/lower_time.py rename to artiq/transforms/lower_time.py index f4e47d985..e4edbf982 100644 --- a/artiq/compiler/lower_time.py +++ b/artiq/transforms/lower_time.py @@ -1,6 +1,6 @@ import ast -from artiq.compiler.tools import value_to_ast +from artiq.transforms.tools import value_to_ast from artiq.language.core import int64 diff --git a/artiq/compiler/lower_units.py b/artiq/transforms/lower_units.py similarity index 96% rename from artiq/compiler/lower_units.py rename to artiq/transforms/lower_units.py index 2b3001568..fa2262a00 100644 --- a/artiq/compiler/lower_units.py +++ b/artiq/transforms/lower_units.py @@ -1,6 +1,6 @@ import ast -from artiq.compiler.tools import value_to_ast +from artiq.transforms.tools import value_to_ast from artiq.language import units diff --git a/artiq/compiler/tools.py b/artiq/transforms/tools.py similarity index 100% rename from artiq/compiler/tools.py rename to artiq/transforms/tools.py diff --git a/artiq/compiler/unparse.py b/artiq/transforms/unparse.py similarity index 100% rename from artiq/compiler/unparse.py rename to artiq/transforms/unparse.py diff --git a/artiq/compiler/unroll_loops.py b/artiq/transforms/unroll_loops.py similarity index 96% rename from artiq/compiler/unroll_loops.py rename to artiq/transforms/unroll_loops.py index 632385f37..d000429ee 100644 --- a/artiq/compiler/unroll_loops.py +++ b/artiq/transforms/unroll_loops.py @@ -1,6 +1,6 @@ import ast -from artiq.compiler.tools import eval_ast, value_to_ast +from artiq.transforms.tools import eval_ast, value_to_ast def _count_stmts(node):