From 58a30eeea68c4d7c81582a6de7ed82b51c832ebf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tiago=20M=C3=BCck?= Date: Mon, 9 Sep 2019 19:13:52 -0500 Subject: [PATCH] mem-ruby: allow qualifiers in SLICC functions MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit All parameters in functions defined within SLICC are const& by default (except for the implicit types, e.g. TBE). This allow us to specify if we want to pass parameters as & or const&. Default behavior is maintained. A use case is to allow refactoring of common code in actions that enqueue messages. Messages can be passed as a non-const ref. to to functions with common initialization. E.g.: void initRequestMsg(RequestMsg & out_msg) { // Common msg init code } action(sendRequest1, ...) { enqueue(...) { initRequestMsg(out_msg); // Request1 specific code } } action(sendRequest2, ...) { enqueue(...) { initRequestMsg(out_msg); // Request2 specific code } } Change-Id: Ic6a18169a661b3e36710b2a9f8a0e6bc5fce40f8 Signed-off-by: Tiago Mück Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/31259 Reviewed-by: Jason Lowe-Power Maintainer: Jason Lowe-Power Tested-by: kokoro --- src/mem/slicc/ast/FormalParamAST.py | 39 ++++++++++++++++++++++++----- src/mem/slicc/parser.py | 29 ++++++++++++++++++--- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/mem/slicc/ast/FormalParamAST.py b/src/mem/slicc/ast/FormalParamAST.py index 778b5c1aa..57f5c94e3 100644 --- a/src/mem/slicc/ast/FormalParamAST.py +++ b/src/mem/slicc/ast/FormalParamAST.py @@ -1,3 +1,15 @@ +# Copyright (c) 2020 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) 1999-2008 Mark D. Hill and David A. Wood # Copyright (c) 2009 The Hewlett-Packard Development Company # All rights reserved. @@ -29,12 +41,12 @@ from slicc.ast.AST import AST from slicc.symbols import Var class FormalParamAST(AST): - def __init__(self, slicc, type_ast, ident, default = None, pointer = False): + def __init__(self, slicc, type_ast, ident, default = None, qualifier=""): super(FormalParamAST, self).__init__(slicc) self.type_ast = type_ast self.ident = ident self.default = default - self.pointer = pointer + self.qualifier = qualifier def __repr__(self): return "[FormalParamAST: %s]" % self.ident @@ -52,11 +64,26 @@ class FormalParamAST(AST): self.pairs) self.symtab.newSymbol(v) - if self.pointer or str(type) == "TBE" or ( - # Check whether type is entry by checking the interface since - # in protocol files, entries use AbstractCacheEntry as interfaces. + # Qualifier is always a pointer for TBE table and Cache entries. + # It's expected to be left unspecified or specified as ptr. + qualifier = self.qualifier + if str(type) == "TBE" or ( "interface" in type and ( type["interface"] == "AbstractCacheEntry")): + if qualifier not in ["", "PTR"] : + self.warning("Parameter \'%s\' is always pointer. " + "%s qualifier ignored" % (self.ident, qualifier)) + qualifier = "PTR" + + # default + if qualifier == "": + qualifier = "CONST_REF" + + if qualifier == "PTR": return type, "%s* %s" % (type.c_ident, param) - else: + elif qualifier == "REF": + return type, "%s& %s" % (type.c_ident, param) + elif qualifier == "CONST_REF": return type, "const %s& %s" % (type.c_ident, param) + else: + self.error("Invalid qualifier for param \'%s\'" % self.ident) diff --git a/src/mem/slicc/parser.py b/src/mem/slicc/parser.py index 13dde9ab4..51a68d0b3 100644 --- a/src/mem/slicc/parser.py +++ b/src/mem/slicc/parser.py @@ -1,3 +1,15 @@ +# Copyright (c) 2020 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) 2009 The Hewlett-Packard Development Company # Copyright (c) 2017 Google Inc. # All rights reserved. @@ -132,7 +144,8 @@ class SLICC(Grammar): 'INCR', 'DECR', 'DOUBLE_COLON', 'SEMI', 'ASSIGN', 'DOT', - 'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ] + 'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING', + 'AMP', 'CONST' ] tokens += reserved.values() t_EQ = r'==' @@ -149,6 +162,8 @@ class SLICC(Grammar): t_PLUS = r'\+' t_DASH = r'-' t_STAR = r'\*' + t_AMP = r'&' + t_CONST = r'const' t_SLASH = r'/' t_MOD = r'%' t_DOUBLE_COLON = r'::' @@ -433,11 +448,19 @@ class SLICC(Grammar): def p_param__pointer(self, p): "param : type STAR ident" - p[0] = ast.FormalParamAST(self, p[1], p[3], None, True) + p[0] = ast.FormalParamAST(self, p[1], p[3], None, "PTR") + + def p_param__ref(self, p): + "param : type AMP ident" + p[0] = ast.FormalParamAST(self, p[1], p[3], None, "REF") + + def p_param__const_ref(self, p): + "param : CONST type AMP ident" + p[0] = ast.FormalParamAST(self, p[1], p[3], None, "CONST_REF") def p_param__pointer_default(self, p): "param : type STAR ident ASSIGN STRING" - p[0] = ast.FormalParamAST(self, p[1], p[3], p[5], True) + p[0] = ast.FormalParamAST(self, p[1], p[3], p[5], "PTR") def p_param__default_number(self, p): "param : type ident ASSIGN NUMBER" -- 2.30.2