2015-07-03 00:35:35 +08:00
|
|
|
"""
|
|
|
|
The :class:`Module` class encapsulates a single Python
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
from pythonparser import source, diagnostic, parse_buffer
|
2015-07-03 02:54:31 +08:00
|
|
|
from . import prelude, types, transforms, validators
|
2015-07-03 00:35:35 +08:00
|
|
|
|
|
|
|
class Module:
|
2015-07-03 02:28:26 +08:00
|
|
|
def __init__(self, source_buffer, engine=None):
|
|
|
|
if engine is None:
|
|
|
|
engine = diagnostic.Engine(all_errors_are_fatal=True)
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
self.name, _ = os.path.splitext(os.path.basename(source_buffer.name))
|
2015-07-14 11:44:16 +08:00
|
|
|
|
2015-07-03 00:35:35 +08:00
|
|
|
asttyped_rewriter = transforms.ASTTypedRewriter(engine=engine)
|
|
|
|
inferencer = transforms.Inferencer(engine=engine)
|
2015-07-03 00:49:52 +08:00
|
|
|
int_monomorphizer = transforms.IntMonomorphizer(engine=engine)
|
2015-07-03 02:54:31 +08:00
|
|
|
monomorphism_validator = validators.MonomorphismValidator(engine=engine)
|
2015-07-03 03:55:08 +08:00
|
|
|
escape_validator = validators.EscapeValidator(engine=engine)
|
2015-07-21 09:54:34 +08:00
|
|
|
artiq_ir_generator = transforms.ARTIQIRGenerator(engine=engine, module_name=self.name)
|
2015-07-19 16:44:51 +08:00
|
|
|
dead_code_eliminator = transforms.DeadCodeEliminator(engine=engine)
|
|
|
|
local_access_validator = validators.LocalAccessValidator(engine=engine)
|
2015-07-21 09:54:34 +08:00
|
|
|
llvm_ir_generator = transforms.LLVMIRGenerator(engine=engine, module_name=self.name)
|
2015-07-19 16:44:51 +08:00
|
|
|
|
|
|
|
self.parsetree, self.comments = parse_buffer(source_buffer, engine=engine)
|
|
|
|
self.typedtree = asttyped_rewriter.visit(self.parsetree)
|
2015-07-03 00:49:52 +08:00
|
|
|
self.globals = asttyped_rewriter.globals
|
2015-07-19 16:44:51 +08:00
|
|
|
inferencer.visit(self.typedtree)
|
|
|
|
int_monomorphizer.visit(self.typedtree)
|
|
|
|
inferencer.visit(self.typedtree)
|
|
|
|
monomorphism_validator.visit(self.typedtree)
|
|
|
|
escape_validator.visit(self.typedtree)
|
2015-07-21 09:54:34 +08:00
|
|
|
self.artiq_ir = artiq_ir_generator.visit(self.typedtree)
|
|
|
|
dead_code_eliminator.process(self.artiq_ir)
|
2015-07-25 10:37:37 +08:00
|
|
|
# local_access_validator.process(self.artiq_ir)
|
2015-07-22 09:13:04 +08:00
|
|
|
self.llvm_ir = llvm_ir_generator.process(self.artiq_ir)
|
2015-07-03 00:49:52 +08:00
|
|
|
|
2015-07-03 00:35:35 +08:00
|
|
|
@classmethod
|
2015-07-03 03:38:55 +08:00
|
|
|
def from_string(cls, source_string, name="input.py", first_line=1, engine=None):
|
|
|
|
return cls(source.Buffer(source_string + "\n", name, first_line), engine=engine)
|
2015-07-03 00:35:35 +08:00
|
|
|
|
|
|
|
@classmethod
|
2015-07-03 03:38:55 +08:00
|
|
|
def from_filename(cls, filename, engine=None):
|
2015-07-03 00:35:35 +08:00
|
|
|
with open(filename) as f:
|
2015-07-03 03:38:55 +08:00
|
|
|
return cls(source.Buffer(f.read(), filename, 1), engine=engine)
|
2015-07-03 00:35:35 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
printer = types.TypePrinter()
|
|
|
|
globals = ["%s: %s" % (var, printer.name(self.globals[var])) for var in self.globals]
|
|
|
|
return "<artiq.compiler.Module %s {\n %s\n}>" % (repr(self.name), ",\n ".join(globals))
|