From f7f027001efeb63fed490bb72a540f541a65aea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=81=AB=E7=84=9A=20=E5=AF=8C=E8=89=AF?= Date: Wed, 23 Nov 2022 12:10:32 +0800 Subject: [PATCH] compiler: insert new lines into long synthesized code (#1986) --- artiq/compiler/embedding.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/artiq/compiler/embedding.py b/artiq/compiler/embedding.py index 359584d6d..8eb5cfae2 100644 --- a/artiq/compiler/embedding.py +++ b/artiq/compiler/embedding.py @@ -169,6 +169,7 @@ class EmbeddingMap: class ASTSynthesizer: def __init__(self, embedding_map, value_map, quote_function=None, expanded_from=None): self.source = "" + self.source_last_new_line = 0 self.source_buffer = source.Buffer(self.source, "") self.embedding_map = embedding_map self.value_map = value_map @@ -187,6 +188,14 @@ class ASTSynthesizer: return source.Range(self.source_buffer, range_from, range_to, expanded_from=self.expanded_from) + def _add_iterable(self, fragment): + # Since DILocation points on the beginning of the piece of source + # we don't care if the fragment's end will overflow LLVM's limit. + if len(self.source) - self.source_last_new_line >= 2**16: + fragment = "\\\n" + fragment + self.source_last_new_line = len(self.source) + 2 + return self._add(fragment) + def fast_quote_list(self, value): elts = [None] * len(value) is_T = False @@ -245,7 +254,7 @@ class ASTSynthesizer: for index, elt in enumerate(value): elts[index] = self.quote(elt) if index < len(value) - 1: - self._add(", ") + self._add_iterable(", ") return elts def quote(self, value): @@ -296,28 +305,28 @@ class ASTSynthesizer: loc=self._add(repr(value))) elif isinstance(value, str): return asttyped.StrT(s=value, ctx=None, type=builtins.TStr(), - loc=self._add(repr(value))) + loc=self._add_iterable(repr(value))) elif isinstance(value, bytes): return asttyped.StrT(s=value, ctx=None, type=builtins.TBytes(), - loc=self._add(repr(value))) + loc=self._add_iterable(repr(value))) elif isinstance(value, bytearray): - quote_loc = self._add('`') - repr_loc = self._add(repr(value)) - unquote_loc = self._add('`') + quote_loc = self._add_iterable('`') + repr_loc = self._add_iterable(repr(value)) + unquote_loc = self._add_iterable('`') loc = quote_loc.join(unquote_loc) return asttyped.QuoteT(value=value, type=builtins.TByteArray(), loc=loc) elif isinstance(value, list): - begin_loc = self._add("[") + begin_loc = self._add_iterable("[") elts = self.fast_quote_list(value) - end_loc = self._add("]") + end_loc = self._add_iterable("]") return asttyped.ListT(elts=elts, ctx=None, type=builtins.TList(), begin_loc=begin_loc, end_loc=end_loc, loc=begin_loc.join(end_loc)) elif isinstance(value, tuple): - begin_loc = self._add("(") + begin_loc = self._add_iterable("(") elts = self.fast_quote_list(value) - end_loc = self._add(")") + end_loc = self._add_iterable(")") return asttyped.TupleT(elts=elts, ctx=None, type=types.TTuple([e.type for e in elts]), begin_loc=begin_loc, end_loc=end_loc,