2014-09-17 17:06:51 +08:00
|
|
|
import os
|
|
|
|
|
2014-09-05 22:18:31 +08:00
|
|
|
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
|
2014-09-17 17:06:51 +08:00
|
|
|
from artiq.transforms.unparse import Unparser
|
2014-09-05 22:18:31 +08:00
|
|
|
from artiq.py2llvm import get_runtime_binary
|
2014-05-31 00:20:13 +08:00
|
|
|
|
2014-09-05 12:03:22 +08:00
|
|
|
|
2014-09-17 17:06:51 +08:00
|
|
|
def _unparse(label, node):
|
|
|
|
print("*** Unparsing: "+label)
|
|
|
|
Unparser(node)
|
|
|
|
|
|
|
|
|
|
|
|
def _make_debug_unparse(final):
|
|
|
|
try:
|
|
|
|
env = os.environ["ARTIQ_UNPARSE"]
|
|
|
|
except KeyError:
|
|
|
|
env = ""
|
|
|
|
selected_labels = set(env.split())
|
|
|
|
if "all" in selected_labels:
|
|
|
|
return _unparse
|
|
|
|
else:
|
|
|
|
if "final" in selected_labels:
|
|
|
|
selected_labels.add(final)
|
|
|
|
|
|
|
|
def _filtered_unparse(label, node):
|
|
|
|
if label in selected_labels:
|
|
|
|
_unparse(label, node)
|
|
|
|
return _filtered_unparse
|
|
|
|
|
|
|
|
|
2014-05-31 00:20:13 +08:00
|
|
|
class Core:
|
2014-09-05 12:03:22 +08:00
|
|
|
def __init__(self, core_com, runtime_env=None):
|
|
|
|
if runtime_env is None:
|
|
|
|
runtime_env = core_com.get_runtime_env()
|
|
|
|
self.runtime_env = runtime_env
|
|
|
|
self.core_com = core_com
|
2014-07-06 04:47:54 +08:00
|
|
|
|
2014-09-05 12:03:22 +08:00
|
|
|
def run(self, k_function, k_args, k_kwargs):
|
2014-09-17 17:06:51 +08:00
|
|
|
# transform/simplify AST
|
|
|
|
_debug_unparse = _make_debug_unparse("fold_constants_2")
|
|
|
|
|
2014-09-13 19:32:21 +08:00
|
|
|
func_def, rpc_map = inline(self, k_function, k_args, k_kwargs)
|
2014-09-17 17:06:51 +08:00
|
|
|
_debug_unparse("inline", func_def)
|
|
|
|
|
2014-09-13 19:32:21 +08:00
|
|
|
lower_units(func_def, self.runtime_env.ref_period)
|
2014-09-17 17:06:51 +08:00
|
|
|
_debug_unparse("lower_units", func_def)
|
|
|
|
|
2014-09-13 19:32:21 +08:00
|
|
|
fold_constants(func_def)
|
2014-09-17 17:06:51 +08:00
|
|
|
_debug_unparse("fold_constants_1", func_def)
|
|
|
|
|
2014-09-13 19:32:21 +08:00
|
|
|
unroll_loops(func_def, 50)
|
2014-09-17 17:06:51 +08:00
|
|
|
_debug_unparse("unroll_loops", func_def)
|
|
|
|
|
2014-09-13 19:32:21 +08:00
|
|
|
interleave(func_def)
|
2014-09-17 17:06:51 +08:00
|
|
|
_debug_unparse("interleave", func_def)
|
|
|
|
|
2014-09-13 19:32:21 +08:00
|
|
|
lower_time(func_def, getattr(self.runtime_env, "initial_time", 0))
|
2014-09-17 17:06:51 +08:00
|
|
|
_debug_unparse("lower_time", func_def)
|
|
|
|
|
2014-09-13 19:32:21 +08:00
|
|
|
fold_constants(func_def)
|
2014-09-17 17:06:51 +08:00
|
|
|
_debug_unparse("fold_constants_2", func_def)
|
2014-06-17 04:56:08 +08:00
|
|
|
|
2014-09-17 17:06:51 +08:00
|
|
|
# compile to machine code and run
|
2014-09-13 19:32:21 +08:00
|
|
|
binary = get_runtime_binary(self.runtime_env, func_def)
|
2014-09-15 22:40:33 +08:00
|
|
|
self.core_com.load(binary)
|
|
|
|
self.core_com.run(func_def.name)
|
2014-09-05 12:03:22 +08:00
|
|
|
self.core_com.serve(rpc_map)
|