forked from M-Labs/nac3
1
0
Fork 0
nac3/nac3artiq/language.py

51 lines
1.3 KiB
Python
Raw Normal View History

from inspect import isclass, getmodule
2020-12-18 23:44:45 +08:00
from functools import wraps
import sys
2020-12-18 23:44:45 +08:00
import nac3artiq
2020-12-18 23:44:45 +08:00
2021-09-29 15:33:12 +08:00
import device_db
2020-12-18 23:44:45 +08:00
__all__ = ["extern", "kernel", "run_on_core"]
2020-12-18 23:44:45 +08:00
2021-09-29 15:33:12 +08:00
nac3 = nac3artiq.NAC3(device_db.device_db["core"]["arguments"]["target"])
allow_module_registration = True
2021-09-30 22:30:54 +08:00
registered_ids = set()
2021-09-24 11:39:26 +08:00
2021-09-27 10:13:03 +08:00
def extern(function):
2021-09-30 22:30:54 +08:00
global registered_ids
assert allow_module_registration
2021-09-30 22:30:54 +08:00
module = getmodule(function)
module_id = id(module)
if module_id not in registered_ids:
nac3.register_module(module)
registered_ids.add(module_id)
2021-09-24 11:39:26 +08:00
return function
2020-12-18 23:44:45 +08:00
def kernel(class_or_function):
global allow_module_registration
2021-09-30 22:30:54 +08:00
global registered_ids
2021-09-23 19:30:03 +08:00
assert allow_module_registration
2021-09-30 22:30:54 +08:00
module = getmodule(class_or_function)
module_id = id(module)
if module_id not in registered_ids:
nac3.register_module(module)
registered_ids.add(module_id)
return class_or_function
def get_defined_class(method):
return vars(sys.modules[method.__module__])[method.__qualname__.split('.')[0]]
def run_on_core(method, *args, **kwargs):
global allow_module_registration
if allow_module_registration:
nac3.analyze()
allow_module_registration = False
nac3.compile_method(id(get_defined_class(method)), method.__name__)