Gabe Black has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/35279 )
Change subject: arch: Split utility methods/variables out of the ISA parser.
......................................................................
arch: Split utility methods/variables out of the ISA parser.
Change-Id: Ifbff4bc6633cd11f98b02ba1291a91c3ad189285
---
M src/arch/isa_parser/isa_parser.py
A src/arch/isa_parser/util.py
2 files changed, 140 insertions(+), 99 deletions(-)
diff --git a/src/arch/isa_parser/isa_parser.py
b/src/arch/isa_parser/isa_parser.py
index c147caf..2461e29 100755
--- a/src/arch/isa_parser/isa_parser.py
+++ b/src/arch/isa_parser/isa_parser.py
@@ -47,60 +47,10 @@
from m5.util.grammar import Grammar
from .operand_types import *
+from .util import *
debug=False
-###################
-# Utility functions
-
-#
-# Indent every line in string 's' by two spaces
-# (except preprocessor directives).
-# Used to make nested code blocks look pretty.
-#
-def indent(s):
- return re.sub(r'(?m)^(?!#)', ' ', s)
-
-#
-# Munge a somewhat arbitrarily formatted piece of Python code
-# (e.g. from a format 'let' block) into something whose indentation
-# will get by the Python parser.
-#
-# The two keys here are that Python will give a syntax error if
-# there's any whitespace at the beginning of the first line, and that
-# all lines at the same lexical nesting level must have identical
-# indentation. Unfortunately the way code literals work, an entire
-# let block tends to have some initial indentation. Rather than
-# trying to figure out what that is and strip it off, we prepend 'if
-# 1:' to make the let code the nested block inside the if (and have
-# the parser automatically deal with the indentation for us).
-#
-# We don't want to do this if (1) the code block is empty or (2) the
-# first line of the block doesn't have any whitespace at the front.
-
-def fixPythonIndentation(s):
- # get rid of blank lines first
- s = re.sub(r'(?m)^\s*\n', '', s);
- if (s != '' and re.match(r'[ \t]', s[0])):
- s = 'if 1:\n' + s
- return s
-
-class ISAParserError(Exception):
- """Exception class for parser errors"""
- def __init__(self, first, second=None):
- if second is None:
- self.lineno = 0
- self.string = first
- else:
- self.lineno = first
- self.string = second
-
- def __str__(self):
- return self.string
-
-def error(*args):
- raise ISAParserError(*args)
-
####################
# Template objects.
#
@@ -763,54 +713,6 @@
else:
self.fp_enable_check = ''
-##############
-# Stack: a simple stack object. Used for both formats (formatStack)
-# and default cases (defaultStack). Simply wraps a list to give more
-# stack-like syntax and enable initialization with an argument list
-# (as opposed to an argument that's a list).
-
-class Stack(list):
- def __init__(self, *items):
- list.__init__(self, items)
-
- def push(self, item):
- self.append(item);
-
- def top(self):
- return self[-1]
-
-# Format a file include stack backtrace as a string
-def backtrace(filename_stack):
- fmt = "In file included from %s:"
- return "\n".join([fmt % f for f in filename_stack])
-
-
-#######################
-#
-# LineTracker: track filenames along with line numbers in PLY lineno fields
-# PLY explicitly doesn't do anything with 'lineno' except propagate
-# it. This class lets us tie filenames with the line numbers with a
-# minimum of disruption to existing increment code.
-#
-
-class LineTracker(object):
- def __init__(self, filename, lineno=1):
- self.filename = filename
- self.lineno = lineno
-
- # Overload '+=' for increments. We need to create a new object on
- # each update else every token ends up referencing the same
- # constantly incrementing instance.
- def __iadd__(self, incr):
- return LineTracker(self.filename, self.lineno + incr)
-
- def __str__(self):
- return "%s:%d" % (self.filename, self.lineno)
-
- # In case there are places where someone really expects a number
- def __int__(self):
- return self.lineno
-
#######################
#
diff --git a/src/arch/isa_parser/util.py b/src/arch/isa_parser/util.py
new file mode 100755
index 0000000..378cb30
--- /dev/null
+++ b/src/arch/isa_parser/util.py
@@ -0,0 +1,139 @@
+# Copyright (c) 2014, 2016, 2018-2019 ARM Limited
+# All rights reserved
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder. You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
+# Copyright (c) 2003-2005 The Regents of The University of Michigan
+# Copyright (c) 2013,2015 Advanced Micro Devices, Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import re
+
+###################
+# Utility functions
+
+#
+# Indent every line in string 's' by two spaces
+# (except preprocessor directives).
+# Used to make nested code blocks look pretty.
+#
+def indent(s):
+ return re.sub(r'(?m)^(?!#)', ' ', s)
+
+#
+# Munge a somewhat arbitrarily formatted piece of Python code
+# (e.g. from a format 'let' block) into something whose indentation
+# will get by the Python parser.
+#
+# The two keys here are that Python will give a syntax error if
+# there's any whitespace at the beginning of the first line, and that
+# all lines at the same lexical nesting level must have identical
+# indentation. Unfortunately the way code literals work, an entire
+# let block tends to have some initial indentation. Rather than
+# trying to figure out what that is and strip it off, we prepend 'if
+# 1:' to make the let code the nested block inside the if (and have
+# the parser automatically deal with the indentation for us).
+#
+# We don't want to do this if (1) the code block is empty or (2) the
+# first line of the block doesn't have any whitespace at the front.
+
+def fixPythonIndentation(s):
+ # get rid of blank lines first
+ s = re.sub(r'(?m)^\s*\n', '', s);
+ if (s != '' and re.match(r'[ \t]', s[0])):
+ s = 'if 1:\n' + s
+ return s
+
+class ISAParserError(Exception):
+ """Exception class for parser errors"""
+ def __init__(self, first, second=None):
+ if second is None:
+ self.lineno = 0
+ self.string = first
+ else:
+ self.lineno = first
+ self.string = second
+
+ def __str__(self):
+ return self.string
+
+def error(*args):
+ raise ISAParserError(*args)
+
+##############
+# Stack: a simple stack object. Used for both formats (formatStack)
+# and default cases (defaultStack). Simply wraps a list to give more
+# stack-like syntax and enable initialization with an argument list
+# (as opposed to an argument that's a list).
+
+class Stack(list):
+ def __init__(self, *items):
+ list.__init__(self, items)
+
+ def push(self, item):
+ self.append(item);
+
+ def top(self):
+ return self[-1]
+
+# Format a file include stack backtrace as a string
+def backtrace(filename_stack):
+ fmt = "In file included from %s:"
+ return "\n".join([fmt % f for f in filename_stack])
+
+
+#######################
+#
+# LineTracker: track filenames along with line numbers in PLY lineno fields
+# PLY explicitly doesn't do anything with 'lineno' except propagate
+# it. This class lets us tie filenames with the line numbers with a
+# minimum of disruption to existing increment code.
+#
+
+class LineTracker(object):
+ def __init__(self, filename, lineno=1):
+ self.filename = filename
+ self.lineno = lineno
+
+ # Overload '+=' for increments. We need to create a new object on
+ # each update else every token ends up referencing the same
+ # constantly incrementing instance.
+ def __iadd__(self, incr):
+ return LineTracker(self.filename, self.lineno + incr)
+
+ def __str__(self):
+ return "%s:%d" % (self.filename, self.lineno)
+
+ # In case there are places where someone really expects a number
+ def __int__(self):
+ return self.lineno
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/35279
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings
Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Ifbff4bc6633cd11f98b02ba1291a91c3ad189285
Gerrit-Change-Number: 35279
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s