2015-07-03 00:35:35 +08:00
|
|
|
"""
|
2015-08-06 13:24:41 +08:00
|
|
|
The :class:`Module` class encapsulates a single Python module,
|
|
|
|
which corresponds to a single ARTIQ translation unit (one LLVM
|
|
|
|
bitcode file and one object file, unless LTO is used).
|
|
|
|
A :class:`Module` can be created from a typed AST.
|
|
|
|
|
|
|
|
The :class:`Source` class parses a single source file or
|
|
|
|
string and infers types for it using a trivial :module:`prelude`.
|
2015-07-03 00:35:35 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
from pythonparser import source, diagnostic, parse_buffer
|
2015-10-09 05:29:31 +08:00
|
|
|
from . import prelude, types, transforms, analyses, validators
|
2015-07-03 00:35:35 +08:00
|
|
|
|
2015-08-06 13:24:41 +08:00
|
|
|
class Source:
|
2015-07-03 02:28:26 +08:00
|
|
|
def __init__(self, source_buffer, engine=None):
|
|
|
|
if engine is None:
|
2015-08-06 13:24:41 +08:00
|
|
|
self.engine = diagnostic.Engine(all_errors_are_fatal=True)
|
|
|
|
else:
|
|
|
|
self.engine = engine
|
2015-07-03 02:28:26 +08:00
|
|
|
|
2015-08-26 12:56:01 +08:00
|
|
|
self.object_map = None
|
|
|
|
|
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-08-06 13:24:41 +08:00
|
|
|
asttyped_rewriter = transforms.ASTTypedRewriter(engine=engine,
|
2015-08-07 19:21:53 +08:00
|
|
|
prelude=prelude.globals())
|
2015-07-03 00:35:35 +08:00
|
|
|
inferencer = transforms.Inferencer(engine=engine)
|
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)
|
2015-08-06 13:24:41 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
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)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_filename(cls, filename, engine=None):
|
|
|
|
with open(filename) as f:
|
|
|
|
return cls(source.Buffer(f.read(), filename, 1), engine=engine)
|
|
|
|
|
|
|
|
class Module:
|
2015-08-31 23:59:33 +08:00
|
|
|
def __init__(self, src, ref_period=1e-6):
|
2015-08-09 07:17:19 +08:00
|
|
|
self.engine = src.engine
|
2015-08-26 12:56:01 +08:00
|
|
|
self.object_map = src.object_map
|
2015-08-09 07:17:19 +08:00
|
|
|
|
|
|
|
int_monomorphizer = transforms.IntMonomorphizer(engine=self.engine)
|
|
|
|
inferencer = transforms.Inferencer(engine=self.engine)
|
|
|
|
monomorphism_validator = validators.MonomorphismValidator(engine=self.engine)
|
|
|
|
escape_validator = validators.EscapeValidator(engine=self.engine)
|
2015-09-30 23:41:14 +08:00
|
|
|
iodelay_estimator = transforms.IODelayEstimator(engine=self.engine,
|
|
|
|
ref_period=ref_period)
|
2015-08-09 07:17:19 +08:00
|
|
|
artiq_ir_generator = transforms.ARTIQIRGenerator(engine=self.engine,
|
2015-08-31 23:59:33 +08:00
|
|
|
module_name=src.name,
|
|
|
|
ref_period=ref_period)
|
2015-08-09 07:17:19 +08:00
|
|
|
dead_code_eliminator = transforms.DeadCodeEliminator(engine=self.engine)
|
|
|
|
local_access_validator = validators.LocalAccessValidator(engine=self.engine)
|
2015-10-09 05:29:31 +08:00
|
|
|
devirtualization = analyses.Devirtualization()
|
2015-11-17 10:22:20 +08:00
|
|
|
interleaver = transforms.Interleaver(engine=self.engine)
|
2015-08-06 13:24:41 +08:00
|
|
|
|
|
|
|
self.name = src.name
|
|
|
|
self.globals = src.globals
|
|
|
|
int_monomorphizer.visit(src.typedtree)
|
|
|
|
inferencer.visit(src.typedtree)
|
|
|
|
monomorphism_validator.visit(src.typedtree)
|
|
|
|
escape_validator.visit(src.typedtree)
|
2015-09-03 07:46:09 +08:00
|
|
|
iodelay_estimator.visit_fixpoint(src.typedtree)
|
2015-10-09 05:29:31 +08:00
|
|
|
devirtualization.visit(src.typedtree)
|
2015-08-06 13:24:41 +08:00
|
|
|
self.artiq_ir = artiq_ir_generator.visit(src.typedtree)
|
2015-10-09 06:32:27 +08:00
|
|
|
artiq_ir_generator.annotate_calls(devirtualization)
|
2015-07-21 09:54:34 +08:00
|
|
|
dead_code_eliminator.process(self.artiq_ir)
|
2015-08-23 04:31:09 +08:00
|
|
|
local_access_validator.process(self.artiq_ir)
|
2015-11-17 10:22:20 +08:00
|
|
|
interleaver.process(self.artiq_ir)
|
2015-07-30 01:35:16 +08:00
|
|
|
|
|
|
|
def build_llvm_ir(self, target):
|
|
|
|
"""Compile the module to LLVM IR for the specified target."""
|
2015-08-09 07:17:19 +08:00
|
|
|
llvm_ir_generator = transforms.LLVMIRGenerator(engine=self.engine,
|
2015-08-26 12:56:01 +08:00
|
|
|
module_name=self.name, target=target,
|
|
|
|
object_map=self.object_map)
|
2015-07-30 01:35:16 +08:00
|
|
|
return llvm_ir_generator.process(self.artiq_ir)
|
|
|
|
|
|
|
|
def entry_point(self):
|
|
|
|
"""Return the name of the function that is the entry point of this module."""
|
2015-08-07 18:20:29 +08:00
|
|
|
if self.name != "":
|
|
|
|
return self.name + ".__modinit__"
|
|
|
|
else:
|
|
|
|
return "__modinit__"
|
2015-07-03 00:49:52 +08:00
|
|
|
|
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))
|