From 9d437626953fa01583e8944edf298c2b19d97cab Mon Sep 17 00:00:00 2001 From: pca006132 Date: Tue, 25 Jan 2022 15:15:57 +0800 Subject: [PATCH] test: fixed lit tests Note that because we changed exception representation from using string names as exception identifier into using integer IDs, we need to initialize the embedding map in order to allocate the integer IDs. Also, we can no longer print the exception names and messages from the kernel, we will need the host to map exception IDs to names, and may need the host to map string IDs to actual strings (messages can be static strings in the firmware, or strings stored in the host only). We now check for exception IDs for lit tests, which are fixed because we preallocated all builtin exceptions. --- artiq/compiler/embedding.py | 21 ++++++++++++------- artiq/compiler/module.py | 4 ++-- .../compiler/transforms/artiq_ir_generator.py | 7 ++++--- artiq/test/lit/exceptions/catch_all.py | 4 ++-- artiq/test/lit/exceptions/catch_multi.py | 4 ++-- artiq/test/lit/exceptions/reraise.py | 2 +- artiq/test/lit/exceptions/reraise_update.py | 2 +- artiq/test/lit/exceptions/uncaught.py | 2 +- 8 files changed, 27 insertions(+), 19 deletions(-) diff --git a/artiq/compiler/embedding.py b/artiq/compiler/embedding.py index 2a6ce62c5..359584d6d 100644 --- a/artiq/compiler/embedding.py +++ b/artiq/compiler/embedding.py @@ -50,9 +50,22 @@ class EmbeddingMap: self.str_forward_map = {} self.str_reverse_map = {} + self.preallocate_runtime_exception_names(["RuntimeError", + "RTIOUnderflow", + "RTIOOverflow", + "RTIODestinationUnreachable", + "DMAError", + "I2CError", + "CacheError", + "SPIError", + "0:ZeroDivisionError", + "0:IndexError"]) + def preallocate_runtime_exception_names(self, names): for i, name in enumerate(names): - exn_id = self.store_str("0:artiq.coredevice.exceptions." + name) + if ":" not in name: + name = "0:artiq.coredevice.exceptions." + name + exn_id = self.store_str(name) assert exn_id == i def store_str(self, s): @@ -754,12 +767,6 @@ class Stitcher: self.functions = {} self.embedding_map = EmbeddingMap() - self.embedding_map.preallocate_runtime_exception_names(["runtimeerror", - "RTIOUnderflow", - "RTIOOverflow", - "RTIODestinationUnreachable", - "DMAError", - "I2CError"]) self.value_map = defaultdict(lambda: []) self.definitely_changed = False diff --git a/artiq/compiler/module.py b/artiq/compiler/module.py index 7f027ba2f..f3bc35cde 100644 --- a/artiq/compiler/module.py +++ b/artiq/compiler/module.py @@ -10,7 +10,7 @@ string and infers types for it using a trivial :module:`prelude`. import os from pythonparser import source, diagnostic, parse_buffer -from . import prelude, types, transforms, analyses, validators +from . import prelude, types, transforms, analyses, validators, embedding class Source: def __init__(self, source_buffer, engine=None): @@ -18,7 +18,7 @@ class Source: self.engine = diagnostic.Engine(all_errors_are_fatal=True) else: self.engine = engine - self.embedding_map = None + self.embedding_map = embedding.EmbeddingMap() self.name, _ = os.path.splitext(os.path.basename(source_buffer.name)) asttyped_rewriter = transforms.ASTTypedRewriter(engine=engine, diff --git a/artiq/compiler/transforms/artiq_ir_generator.py b/artiq/compiler/transforms/artiq_ir_generator.py index bd69ca79c..40f50718a 100644 --- a/artiq/compiler/transforms/artiq_ir_generator.py +++ b/artiq/compiler/transforms/artiq_ir_generator.py @@ -2814,14 +2814,15 @@ class ARTIQIRGenerator(algorithm.Visitor): format_string += ")" elif builtins.is_exception(value.type): + # message may not be an actual string... + # so we cannot really print it name = self.append(ir.GetAttr(value, "#__name__")) - message = self.append(ir.GetAttr(value, "#__message__")) param1 = self.append(ir.GetAttr(value, "#__param0__")) param2 = self.append(ir.GetAttr(value, "#__param1__")) param3 = self.append(ir.GetAttr(value, "#__param2__")) - format_string += "%.*s(%.*s, %lld, %lld, %lld)" - args += [name, message, param1, param2, param3] + format_string += "%ld(%lld, %lld, %lld)" + args += [name, param1, param2, param3] else: assert False diff --git a/artiq/test/lit/exceptions/catch_all.py b/artiq/test/lit/exceptions/catch_all.py index 1417f5f31..1b4c38b8e 100644 --- a/artiq/test/lit/exceptions/catch_all.py +++ b/artiq/test/lit/exceptions/catch_all.py @@ -8,7 +8,7 @@ def catch(f): except Exception as e: print(e) -# CHECK-L: ZeroDivisionError +# CHECK-L: 8(0, 0, 0) catch(lambda: 1/0) -# CHECK-L: IndexError +# CHECK-L: 9(10, 1, 0) catch(lambda: [1.0][10]) diff --git a/artiq/test/lit/exceptions/catch_multi.py b/artiq/test/lit/exceptions/catch_multi.py index 472086660..dbd8bcdec 100644 --- a/artiq/test/lit/exceptions/catch_multi.py +++ b/artiq/test/lit/exceptions/catch_multi.py @@ -10,7 +10,7 @@ def catch(f): except IndexError as ie: print(ie) -# CHECK-L: ZeroDivisionError +# CHECK-L: 8(0, 0, 0) catch(lambda: 1/0) -# CHECK-L: IndexError +# CHECK-L: 9(10, 1, 0) catch(lambda: [1.0][10]) diff --git a/artiq/test/lit/exceptions/reraise.py b/artiq/test/lit/exceptions/reraise.py index 911b22322..fd5eabd4a 100644 --- a/artiq/test/lit/exceptions/reraise.py +++ b/artiq/test/lit/exceptions/reraise.py @@ -3,7 +3,7 @@ # REQUIRES: exceptions def f(): - # CHECK-L: Uncaught 0:ZeroDivisionError + # CHECK-L: Uncaught 8 # CHECK-L: at input.py:${LINE:+1}: 1/0 diff --git a/artiq/test/lit/exceptions/reraise_update.py b/artiq/test/lit/exceptions/reraise_update.py index 11bd30639..aac8cdae1 100644 --- a/artiq/test/lit/exceptions/reraise_update.py +++ b/artiq/test/lit/exceptions/reraise_update.py @@ -9,7 +9,7 @@ def g(): try: f() except Exception as e: - # CHECK-L: Uncaught 0:ZeroDivisionError + # CHECK-L: Uncaught 8 # CHECK-L: at input.py:${LINE:+1}: raise e diff --git a/artiq/test/lit/exceptions/uncaught.py b/artiq/test/lit/exceptions/uncaught.py index 13b83ec22..1d44d4b2a 100644 --- a/artiq/test/lit/exceptions/uncaught.py +++ b/artiq/test/lit/exceptions/uncaught.py @@ -2,6 +2,6 @@ # RUN: OutputCheck %s --file-to-check=%t # REQUIRES: exceptions -# CHECK-L: Uncaught 0:ZeroDivisionError: cannot divide by zero (0, 0, 0) +# CHECK-L: Uncaught 8: cannot divide by zero (0, 0, 0) # CHECK-L: at input.py:${LINE:+1}: 1/0