From 9db8627081d234b3e54ccfe0c3a84765081b3b3f Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 16 Jun 2014 22:56:08 +0200 Subject: [PATCH] compiler: generate RPC calls --- artiq/compiler/inline.py | 12 ++++++++---- artiq/devices/core.py | 15 ++++++++++++++- examples/compiler_test.py | 8 ++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/artiq/compiler/inline.py b/artiq/compiler/inline.py index 7303e54be..9c45ef484 100644 --- a/artiq/compiler/inline.py +++ b/artiq/compiler/inline.py @@ -35,6 +35,7 @@ class _ReferenceManager: self.to_inlined = dict() # inlined_name -> use_count self.use_count = dict() + self.rpc_map = defaultdict(lambda: len(self.rpc_map)) # reserved names self.use_count["Quantity"] = 1 @@ -126,9 +127,12 @@ class _ReferenceReplacer(ast.NodeTransformer): elif hasattr(func, "k_function_info"): print(func.k_function_info) # TODO: inline called kernel - - self.generic_visit(node) - return node + return node + else: + args = [ast.Str("rpc"), ast.Num(self.rm.rpc_map[func])] + args += [self.visit(arg) for arg in node.args] + return ast.Call(func=ast.Name("syscall", ast.Load()), + args=args, keywords=[], starargs=None, kwargs=None) class _ListReadOnlyParams(ast.NodeVisitor): def visit_FunctionDef(self, node): @@ -180,4 +184,4 @@ def inline(k_function, k_args, k_kwargs, rm=None): funcdef.body[0:0] = param_init - return funcdef.body + return funcdef.body, rm.rpc_map diff --git a/artiq/devices/core.py b/artiq/devices/core.py index 7b7e192f5..8c7fd30eb 100644 --- a/artiq/devices/core.py +++ b/artiq/devices/core.py @@ -1,7 +1,20 @@ +from operator import itemgetter + from artiq.compiler.inline import inline from artiq.compiler.unparse import Unparser class Core: def run(self, k_function, k_args, k_kwargs): - stmts = inline(k_function, k_args, k_kwargs) + stmts, rpc_map = inline(k_function, k_args, k_kwargs) + + print("=========================") + print(" Inlined") + print("=========================") Unparser(stmts) + + print("") + print("=========================") + print(" RPC map") + print("=========================") + for rpc_func, rpc_num in sorted(rpc_map.items(), key=itemgetter(1)): + print("{:3} -> {}".format(rpc_num, str(rpc_func))) diff --git a/examples/compiler_test.py b/examples/compiler_test.py index 5f418cf75..504cd433a 100644 --- a/examples/compiler_test.py +++ b/examples/compiler_test.py @@ -6,10 +6,17 @@ my_range = range class CompilerTest(Experiment): channels = "core a b A B" + def print_done(self): + print("Done!") + + def print_iter(self, n): + print("Iteration: {}".format(n)) + @kernel def run(self, n, t2): t2 += 1*us for i in my_range(n): + self.print_iter(i) with parallel: with sequential: self.a.pulse(100*MHz, 20*us) @@ -17,6 +24,7 @@ class CompilerTest(Experiment): with sequential: self.A.pulse(100*MHz, 10*us) self.B.pulse(100*MHz, t2) + self.print_done() if __name__ == "__main__": from artiq.devices import core, core_dds