From 15c78df3a2794cf764a2a7881032d0db10be2651 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 18 Aug 2014 14:33:54 +0800 Subject: [PATCH] devices/runtime: new syscall API --- artiq/devices/runtime.py | 48 ++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/artiq/devices/runtime.py b/artiq/devices/runtime.py index c1f19efb4..597938931 100644 --- a/artiq/devices/runtime.py +++ b/artiq/devices/runtime.py @@ -1,19 +1,21 @@ from llvm import core as lc from llvm import target as lt +from artiq.compiler import ir_values + lt.initialize_all() -_syscalls = [ - ("rpc", "i+:i"), - ("gpio_set", "ii:v"), - ("rtio_set", "iii:v"), - ("rtio_sync", "i:v"), - ("dds_program", "ii:v"), -] +_syscalls = { + "rpc": "i+:i", + "gpio_set": "ii:n", + "rtio_set": "iii:n", + "rtio_sync": "i:n", + "dds_program": "ii:n", +} def _str_to_functype(s): _chr_to_type = { - "v": lc.Type.void, + "n": lc.Type.void, "i": lc.Type.int } assert(s[-2] == ":") @@ -31,22 +33,30 @@ def _str_to_functype(s): class LinkInterface: def set_module(self, module): + self.module = module self.var_arg_fixcount = dict() - for func_name, func_type_str in _syscalls: + for func_name, func_type_str in _syscalls.items(): var_arg_fixcount, func_type = _str_to_functype(func_type_str) if var_arg_fixcount is not None: self.var_arg_fixcount[func_name] = var_arg_fixcount - module.add_function(func_type, "__syscall_"+func_name) + self.module.add_function(func_type, "__syscall_"+func_name) - self.module = module - - def emit_syscall(self, builder, syscall_name, args): - if syscall_name in self.var_arg_fixcount: - fixcount = self.var_arg_fixcount[syscall_name] - args = args[:fixcount] \ - + [lc.Constant.int(lc.Type.int(), len(args) - fixcount)] \ - + args[fixcount:] - return builder.call(self.module.get_function_named("__syscall_"+syscall_name), args) + def syscall(self, syscall_name, args, builder): + _chr_to_value = { + "n": ir_values.VNone, + "i": ir_values.VInt + } + r = _chr_to_value[_syscalls[syscall_name][-1]]() + if builder is not None: + args = [arg.llvm_value for arg in args] + if syscall_name in self.var_arg_fixcount: + fixcount = self.var_arg_fixcount[syscall_name] + args = args[:fixcount] \ + + [lc.Constant.int(lc.Type.int(), len(args) - fixcount)] \ + + args[fixcount:] + llvm_function = self.module.get_function_named("__syscall_"+syscall_name) + r.llvm_value = builder.call(llvm_function, args) + return r class Environment(LinkInterface): def __init__(self, ref_period):