From 4f26b6281d260f1d4750bf2d5b8e4407f9f094ad Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 25 Sep 2014 12:57:26 +0800 Subject: [PATCH] host: support runtime device exceptions --- artiq/devices/corecom_serial.py | 13 +++++++++---- artiq/devices/runtime_exceptions.py | 15 +++++++++++++++ artiq/language/core.py | 2 ++ artiq/transforms/inline.py | 3 +-- 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 artiq/devices/runtime_exceptions.py diff --git a/artiq/devices/corecom_serial.py b/artiq/devices/corecom_serial.py index 9b0d349af..7fef4a461 100644 --- a/artiq/devices/corecom_serial.py +++ b/artiq/devices/corecom_serial.py @@ -6,7 +6,9 @@ from enum import Enum import logging from artiq.language import units +from artiq.language import core as core_language from artiq.devices.runtime import Environment +from artiq.devices import runtime_exceptions logger = logging.getLogger(__name__) @@ -142,7 +144,7 @@ class CoreCom: _write_exactly(self.port, struct.pack("B", ord(c))) logger.debug("running kernel: {}".format(kname)) - def serve(self, rpc_map, exception_map): + def serve(self, rpc_map, user_exception_map): while True: msg = self._get_device_msg() if msg == _D2HMsgType.RPC_REQUEST: @@ -160,9 +162,12 @@ class CoreCom: logger.debug("rpc service: {} ({}) == {}".format( rpc_num, args, r)) elif msg == _D2HMsgType.KERNEL_EXCEPTION: - (exception_num, ) = struct.unpack(">l", - _read_exactly(self.port, 4)) - raise exception_map[exception_num] + (eid, ) = struct.unpack(">l", _read_exactly(self.port, 4)) + if eid < core_language.first_user_eid: + exception = runtime_exceptions.exception_map[eid] + else: + exception = user_exception_map[eid] + raise exception elif msg == _D2HMsgType.KERNEL_FINISHED: return else: diff --git a/artiq/devices/runtime_exceptions.py b/artiq/devices/runtime_exceptions.py new file mode 100644 index 000000000..d01ef5bbf --- /dev/null +++ b/artiq/devices/runtime_exceptions.py @@ -0,0 +1,15 @@ +class RuntimeException(Exception): + pass + + +class OutOfMemory(RuntimeException): + eid = 0 + + +class RTIOUnderflow(RuntimeException): + eid = 1 + + +exception_map = {e.eid: e for e in globals().values() + if isinstance(e, RuntimeException.__class__) + and hasattr(e, "eid")} diff --git a/artiq/language/core.py b/artiq/language/core.py index 75da83f16..e607521a3 100644 --- a/artiq/language/core.py +++ b/artiq/language/core.py @@ -330,6 +330,8 @@ def syscall(*args): _encoded_exceptions = dict() +first_user_eid = 1024 + def EncodedException(eid): """Represents exceptions on the core device, which are identified diff --git a/artiq/transforms/inline.py b/artiq/transforms/inline.py index 1f3a63d9c..1cb1cac08 100644 --- a/artiq/transforms/inline.py +++ b/artiq/transforms/inline.py @@ -41,8 +41,7 @@ class _ReferenceManager: # inlined_name -> use_count self.use_count = dict() self.rpc_mapper = _HostObjectMapper() - # exceptions 0-1023 are for runtime - self.exception_mapper = _HostObjectMapper(1024) + self.exception_mapper = _HostObjectMapper(core_language.first_user_eid) self.kernel_attr_init = [] # reserved names