devices/runtime: new syscall API

This commit is contained in:
Sebastien Bourdeauducq 2014-08-18 14:33:54 +08:00
parent 65566ec710
commit 15c78df3a2
1 changed files with 29 additions and 19 deletions

View File

@ -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):