parser.py revision 10165
16657Snate@binkert.org# Copyright (c) 2009 The Hewlett-Packard Development Company
26657Snate@binkert.org# All rights reserved.
36657Snate@binkert.org#
46657Snate@binkert.org# Redistribution and use in source and binary forms, with or without
56657Snate@binkert.org# modification, are permitted provided that the following conditions are
66657Snate@binkert.org# met: redistributions of source code must retain the above copyright
76657Snate@binkert.org# notice, this list of conditions and the following disclaimer;
86657Snate@binkert.org# redistributions in binary form must reproduce the above copyright
96657Snate@binkert.org# notice, this list of conditions and the following disclaimer in the
106657Snate@binkert.org# documentation and/or other materials provided with the distribution;
116657Snate@binkert.org# neither the name of the copyright holders nor the names of its
126657Snate@binkert.org# contributors may be used to endorse or promote products derived from
136657Snate@binkert.org# this software without specific prior written permission.
146657Snate@binkert.org#
156657Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
166657Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
176657Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
186657Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
196657Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
206657Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
216657Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226657Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236657Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246657Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
256657Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266657Snate@binkert.org#
276657Snate@binkert.org# Authors: Nathan Binkert
286657Snate@binkert.org
296657Snate@binkert.orgimport os.path
306657Snate@binkert.orgimport re
316657Snate@binkert.orgimport sys
326657Snate@binkert.org
336999Snate@binkert.orgfrom m5.util import code_formatter
348452Snate@binkert.orgfrom m5.util.grammar import Grammar, ParseError
356657Snate@binkert.org
366657Snate@binkert.orgimport slicc.ast as ast
376657Snate@binkert.orgimport slicc.util as util
386657Snate@binkert.orgfrom slicc.symbols import SymbolTable
396657Snate@binkert.org
406657Snate@binkert.orgclass SLICC(Grammar):
419219Spower.jg@gmail.com    def __init__(self, filename, base_dir, verbose=False, traceback=False, **kwargs):
428454Snate@binkert.org        self.protocol = None
438454Snate@binkert.org        self.traceback = traceback
448453Snate@binkert.org        self.verbose = verbose
456999Snate@binkert.org        self.symtab = SymbolTable(self)
469219Spower.jg@gmail.com        self.base_dir = base_dir
476999Snate@binkert.org
488454Snate@binkert.org        try:
498454Snate@binkert.org            self.decl_list = self.parse_file(filename, **kwargs)
508454Snate@binkert.org        except ParseError, e:
518454Snate@binkert.org            if not self.traceback:
528454Snate@binkert.org                sys.exit(str(e))
538454Snate@binkert.org            raise
548454Snate@binkert.org
558453Snate@binkert.org    def currentLocation(self):
568453Snate@binkert.org        return util.Location(self.current_source, self.current_line,
578453Snate@binkert.org                             no_warning=not self.verbose)
588453Snate@binkert.org
596999Snate@binkert.org    def codeFormatter(self, *args, **kwargs):
606999Snate@binkert.org        code = code_formatter(*args, **kwargs)
616999Snate@binkert.org        code['protocol'] = self.protocol
626999Snate@binkert.org        return code
636657Snate@binkert.org
648453Snate@binkert.org    def process(self):
658454Snate@binkert.org        self.decl_list.findMachines()
668454Snate@binkert.org        self.decl_list.generate()
676657Snate@binkert.org
689219Spower.jg@gmail.com    def writeCodeFiles(self, code_path, includes):
699219Spower.jg@gmail.com        self.symtab.writeCodeFiles(code_path, includes)
706657Snate@binkert.org
718453Snate@binkert.org    def writeHTMLFiles(self, html_path):
728453Snate@binkert.org        self.symtab.writeHTMLFiles(html_path)
736657Snate@binkert.org
746657Snate@binkert.org    def files(self):
756714Ssteve.reinhardt@amd.com        f = set([
766714Ssteve.reinhardt@amd.com            'MachineType.cc',
776657Snate@binkert.org            'MachineType.hh',
786657Snate@binkert.org            'Types.hh' ])
796657Snate@binkert.org
808454Snate@binkert.org        f |= self.decl_list.files()
816657Snate@binkert.org
826714Ssteve.reinhardt@amd.com        return f
836657Snate@binkert.org
846657Snate@binkert.org    t_ignore = '\t '
856657Snate@binkert.org
866657Snate@binkert.org    # C or C++ comment (ignore)
876657Snate@binkert.org    def t_c_comment(self, t):
886657Snate@binkert.org        r'/\*(.|\n)*?\*/'
896657Snate@binkert.org        t.lexer.lineno += t.value.count('\n')
906657Snate@binkert.org
916657Snate@binkert.org    def t_cpp_comment(self, t):
926657Snate@binkert.org        r'//.*'
936657Snate@binkert.org
946657Snate@binkert.org    # Define a rule so we can track line numbers
956657Snate@binkert.org    def t_newline(self, t):
966657Snate@binkert.org        r'\n+'
976657Snate@binkert.org        t.lexer.lineno += len(t.value)
986657Snate@binkert.org
996657Snate@binkert.org    reserved = {
1008454Snate@binkert.org        'protocol' : 'PROTOCOL',
1018454Snate@binkert.org        'include' : 'INCLUDE',
1026657Snate@binkert.org        'global' : 'GLOBAL',
1036657Snate@binkert.org        'machine' : 'MACHINE',
1046657Snate@binkert.org        'in_port' : 'IN_PORT',
1056657Snate@binkert.org        'out_port' : 'OUT_PORT',
1066657Snate@binkert.org        'action' : 'ACTION',
1076657Snate@binkert.org        'transition' : 'TRANS',
1086657Snate@binkert.org        'structure' : 'STRUCT',
1096657Snate@binkert.org        'external_type' : 'EXTERN_TYPE',
1106657Snate@binkert.org        'enumeration' : 'ENUM',
1118086SBrad.Beckmann@amd.com        'state_declaration' : 'STATE_DECL',
1126657Snate@binkert.org        'peek' : 'PEEK',
1137567SBrad.Beckmann@amd.com        'stall_and_wait' : 'STALL_AND_WAIT',
1146657Snate@binkert.org        'enqueue' : 'ENQUEUE',
1156657Snate@binkert.org        'check_allocate' : 'CHECK_ALLOCATE',
1166657Snate@binkert.org        'check_stop_slots' : 'CHECK_STOP_SLOTS',
1176882SBrad.Beckmann@amd.com        'static_cast' : 'STATIC_CAST',
1186657Snate@binkert.org        'if' : 'IF',
1197839Snilay@cs.wisc.edu        'is_valid' : 'IS_VALID',
1207839Snilay@cs.wisc.edu        'is_invalid' : 'IS_INVALID',
1216657Snate@binkert.org        'else' : 'ELSE',
1226657Snate@binkert.org        'return' : 'RETURN',
1236657Snate@binkert.org        'void' : 'VOID',
1246657Snate@binkert.org        'new' : 'NEW',
1257839Snilay@cs.wisc.edu        'OOD' : 'OOD',
1266657Snate@binkert.org    }
1276657Snate@binkert.org
1286657Snate@binkert.org    literals = ':[]{}(),='
1296657Snate@binkert.org
1306657Snate@binkert.org    tokens = [ 'EQ', 'NE', 'LT', 'GT', 'LE', 'GE',
1316657Snate@binkert.org               'LEFTSHIFT', 'RIGHTSHIFT',
1326657Snate@binkert.org               'NOT', 'AND', 'OR',
1336657Snate@binkert.org               'PLUS', 'DASH', 'STAR', 'SLASH',
1349692Snilay@cs.wisc.edu               'INCR', 'DECR',
1356657Snate@binkert.org               'DOUBLE_COLON', 'SEMI',
1366657Snate@binkert.org               'ASSIGN', 'DOT',
1376657Snate@binkert.org               'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ]
1386657Snate@binkert.org    tokens += reserved.values()
1396657Snate@binkert.org
1406657Snate@binkert.org    t_EQ = r'=='
1416657Snate@binkert.org    t_NE = r'!='
1426657Snate@binkert.org    t_LT = r'<'
1436657Snate@binkert.org    t_GT = r'>'
1446657Snate@binkert.org    t_LE = r'<='
1456657Snate@binkert.org    t_GE = r'>='
1466657Snate@binkert.org    t_LEFTSHIFT = r'<<'
1476657Snate@binkert.org    t_RIGHTSHIFT = r'>>'
1486657Snate@binkert.org    t_NOT = r'!'
1496657Snate@binkert.org    t_AND = r'&&'
1506657Snate@binkert.org    t_OR = r'\|\|'
1516657Snate@binkert.org    t_PLUS = r'\+'
1526657Snate@binkert.org    t_DASH = r'-'
1536657Snate@binkert.org    t_STAR = r'\*'
1546657Snate@binkert.org    t_SLASH = r'/'
1556657Snate@binkert.org    t_DOUBLE_COLON = r'::'
1566657Snate@binkert.org    t_SEMI = r';'
1576657Snate@binkert.org    t_ASSIGN = r':='
1586657Snate@binkert.org    t_DOT = r'\.'
1599692Snilay@cs.wisc.edu    t_INCR = r'\+\+'
1609692Snilay@cs.wisc.edu    t_DECR = r'--'
1616657Snate@binkert.org
1626657Snate@binkert.org    precedence = (
1639692Snilay@cs.wisc.edu        ('left', 'INCR', 'DECR'),
1646657Snate@binkert.org        ('left', 'AND', 'OR'),
1656657Snate@binkert.org        ('left', 'EQ', 'NE'),
1666657Snate@binkert.org        ('left', 'LT', 'GT', 'LE', 'GE'),
1676657Snate@binkert.org        ('left', 'RIGHTSHIFT', 'LEFTSHIFT'),
1686657Snate@binkert.org        ('left', 'PLUS', 'DASH'),
1696657Snate@binkert.org        ('left', 'STAR', 'SLASH'),
1706657Snate@binkert.org        ('right', 'NOT', 'UMINUS'),
1716657Snate@binkert.org    )
1726657Snate@binkert.org
1736657Snate@binkert.org    def t_IDENT(self, t):
1746657Snate@binkert.org        r'[a-zA-Z_][a-zA-Z_0-9]*'
1756657Snate@binkert.org        if t.value == 'true':
1766657Snate@binkert.org            t.type = 'LIT_BOOL'
1776657Snate@binkert.org            t.value = True
1786657Snate@binkert.org            return t
1796657Snate@binkert.org
1806657Snate@binkert.org        if t.value == 'false':
1816657Snate@binkert.org            t.type = 'LIT_BOOL'
1826657Snate@binkert.org            t.value = False
1836657Snate@binkert.org            return t
1846657Snate@binkert.org
1856657Snate@binkert.org        # Check for reserved words
1866657Snate@binkert.org        t.type = self.reserved.get(t.value, 'IDENT')
1876657Snate@binkert.org        return t
1886657Snate@binkert.org
1896657Snate@binkert.org    def t_FLOATNUMBER(self, t):
1906657Snate@binkert.org        '[0-9]+[.][0-9]+'
1916657Snate@binkert.org        try:
1926657Snate@binkert.org            t.value = float(t.value)
1936657Snate@binkert.org        except ValueError:
1948452Snate@binkert.org            raise ParseError("Illegal float", t)
1956657Snate@binkert.org        return t
1966657Snate@binkert.org
1976657Snate@binkert.org    def t_NUMBER(self, t):
1986657Snate@binkert.org        r'[0-9]+'
1996657Snate@binkert.org        try:
2006657Snate@binkert.org            t.value = int(t.value)
2016657Snate@binkert.org        except ValueError:
2028452Snate@binkert.org            raise ParseError("Illegal number", t)
2036657Snate@binkert.org        return t
2046657Snate@binkert.org
2056657Snate@binkert.org    def t_STRING1(self, t):
2066657Snate@binkert.org        r'\"[^"\n]*\"'
2076657Snate@binkert.org        t.type = 'STRING'
2086657Snate@binkert.org        t.value = t.value[1:-1]
2096657Snate@binkert.org        return t
2106657Snate@binkert.org
2116657Snate@binkert.org    def t_STRING2(self, t):
2126657Snate@binkert.org        r"\'[^'\n]*\'"
2136657Snate@binkert.org        t.type = 'STRING'
2146657Snate@binkert.org        t.value = t.value[1:-1]
2156657Snate@binkert.org        return t
2166657Snate@binkert.org
2176657Snate@binkert.org    def p_file(self, p):
2186657Snate@binkert.org        "file : decls"
2196657Snate@binkert.org        p[0] = p[1]
2206657Snate@binkert.org
2216657Snate@binkert.org    def p_empty(self, p):
2226657Snate@binkert.org        "empty :"
2236657Snate@binkert.org
2246657Snate@binkert.org    def p_decls(self, p):
2256657Snate@binkert.org        "decls : declsx"
2266657Snate@binkert.org        p[0] = ast.DeclListAST(self, p[1])
2276657Snate@binkert.org
2286657Snate@binkert.org    def p_declsx__list(self, p):
2296657Snate@binkert.org        "declsx : decl declsx"
2308454Snate@binkert.org        if isinstance(p[1], ast.DeclListAST):
2318454Snate@binkert.org            decls = p[1].decls
2328454Snate@binkert.org        elif p[1] is None:
2338454Snate@binkert.org            decls = []
2348454Snate@binkert.org        else:
2358454Snate@binkert.org            decls = [ p[1] ]
2368454Snate@binkert.org        p[0] = decls + p[2]
2376657Snate@binkert.org
2386657Snate@binkert.org    def p_declsx__none(self, p):
2396657Snate@binkert.org        "declsx : empty"
2406657Snate@binkert.org        p[0] = []
2416657Snate@binkert.org
2428454Snate@binkert.org    def p_decl__protocol(self, p):
2438454Snate@binkert.org        "decl : PROTOCOL STRING SEMI"
2448454Snate@binkert.org        if self.protocol:
2458454Snate@binkert.org            msg = "Protocol can only be set once! Error at %s:%s\n" % \
2468454Snate@binkert.org                (self.current_source, self.current_line)
2478454Snate@binkert.org            raise ParseError(msg)
2488454Snate@binkert.org        self.protocol = p[2]
2498454Snate@binkert.org        p[0] = None
2508454Snate@binkert.org
2518454Snate@binkert.org    def p_decl__include(self, p):
2528454Snate@binkert.org        "decl : INCLUDE STRING SEMI"
2538454Snate@binkert.org        dirname = os.path.dirname(self.current_source)
2549219Spower.jg@gmail.com        if os.path.exists(os.path.join(dirname, p[2])):
2559219Spower.jg@gmail.com            filename = os.path.join(dirname, p[2])
2569219Spower.jg@gmail.com        else:
2579219Spower.jg@gmail.com            filename = os.path.join(self.base_dir, p[2])
2588454Snate@binkert.org        p[0] = self.parse_file(filename)
2598454Snate@binkert.org
2609773Snilay@cs.wisc.edu    def p_decl__machine0(self, p):
2619773Snilay@cs.wisc.edu        "decl : MACHINE '(' idents ')' ':' params '{' decls '}'"
2629773Snilay@cs.wisc.edu        p[0] = ast.MachineAST(self, p[3], [], p[7], p[9])
2639773Snilay@cs.wisc.edu
2649773Snilay@cs.wisc.edu    def p_decl__machine1(self, p):
2659773Snilay@cs.wisc.edu        "decl : MACHINE '(' idents pairs ')' ':' params '{' decls '}'"
2666657Snate@binkert.org        p[0] = ast.MachineAST(self, p[3], p[4], p[7], p[9])
2676657Snate@binkert.org
2686657Snate@binkert.org    def p_decl__action(self, p):
2696657Snate@binkert.org        "decl : ACTION '(' ident pairs ')' statements"
2706657Snate@binkert.org        p[0] = ast.ActionDeclAST(self, p[3], p[4], p[6])
2716657Snate@binkert.org
2726657Snate@binkert.org    def p_decl__in_port(self, p):
2736657Snate@binkert.org        "decl : IN_PORT '(' ident ',' type ',' var pairs ')' statements"
2746657Snate@binkert.org        p[0] = ast.InPortDeclAST(self, p[3], p[5], p[7], p[8], p[10])
2756657Snate@binkert.org
2766657Snate@binkert.org    def p_decl__out_port(self, p):
2776657Snate@binkert.org        "decl : OUT_PORT '(' ident ',' type ',' var pairs ')' SEMI"
2786657Snate@binkert.org        p[0] = ast.OutPortDeclAST(self, p[3], p[5], p[7], p[8])
2796657Snate@binkert.org
2806657Snate@binkert.org    def p_decl__trans0(self, p):
28110165Snilay@cs.wisc.edu        "decl : TRANS '(' idents ',' idents ',' ident ')' idents"
28210165Snilay@cs.wisc.edu        p[0] = ast.TransitionDeclAST(self, [], p[3], p[5], p[7], p[9])
2836657Snate@binkert.org
2846657Snate@binkert.org    def p_decl__trans1(self, p):
28510165Snilay@cs.wisc.edu        "decl : TRANS '(' idents ',' idents ')' idents"
28610165Snilay@cs.wisc.edu        p[0] = ast.TransitionDeclAST(self, [], p[3], p[5], None, p[7])
2879104Shestness@cs.utexas.edu
2889104Shestness@cs.utexas.edu    def p_decl__trans2(self, p):
28910165Snilay@cs.wisc.edu        "decl : TRANS '(' idents ',' idents ',' ident ')' idents idents"
29010165Snilay@cs.wisc.edu        p[0] = ast.TransitionDeclAST(self, p[9], p[3], p[5], p[7], p[10])
2919104Shestness@cs.utexas.edu
2929104Shestness@cs.utexas.edu    def p_decl__trans3(self, p):
29310165Snilay@cs.wisc.edu        "decl : TRANS '(' idents ',' idents ')' idents idents"
29410165Snilay@cs.wisc.edu        p[0] = ast.TransitionDeclAST(self, p[7], p[3], p[5], None, p[8])
2956657Snate@binkert.org
2966657Snate@binkert.org    def p_decl__extern0(self, p):
2976657Snate@binkert.org        "decl : EXTERN_TYPE '(' type pairs ')' SEMI"
2986657Snate@binkert.org        p[4]["external"] = "yes"
2996657Snate@binkert.org        p[0] = ast.TypeDeclAST(self, p[3], p[4], [])
3006657Snate@binkert.org
3016657Snate@binkert.org    def p_decl__global(self, p):
3026657Snate@binkert.org        "decl : GLOBAL '(' type pairs ')' '{' type_members '}'"
3036657Snate@binkert.org        p[4]["global"] = "yes"
3046657Snate@binkert.org        p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
3056657Snate@binkert.org
3066657Snate@binkert.org    def p_decl__struct(self, p):
3076657Snate@binkert.org        "decl : STRUCT '(' type pairs ')' '{' type_members '}'"
3086657Snate@binkert.org        p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
3096657Snate@binkert.org
3106657Snate@binkert.org    def p_decl__enum(self, p):
3116657Snate@binkert.org        "decl : ENUM '(' type pairs ')' '{' type_enums   '}'"
3126657Snate@binkert.org        p[4]["enumeration"] = "yes"
3136657Snate@binkert.org        p[0] = ast.EnumDeclAST(self, p[3], p[4], p[7])
3146657Snate@binkert.org
3158086SBrad.Beckmann@amd.com    def p_decl__state_decl(self, p):
3168086SBrad.Beckmann@amd.com        "decl : STATE_DECL '(' type pairs ')' '{' type_states   '}'"
3178086SBrad.Beckmann@amd.com        p[4]["enumeration"] = "yes"
3188086SBrad.Beckmann@amd.com        p[4]["state_decl"] = "yes"
3198086SBrad.Beckmann@amd.com        p[0] = ast.StateDeclAST(self, p[3], p[4], p[7])
3208086SBrad.Beckmann@amd.com
3216657Snate@binkert.org    def p_decl__object(self, p):
3226657Snate@binkert.org        "decl : type ident pairs SEMI"
3236657Snate@binkert.org        p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3])
3246657Snate@binkert.org
3259298Snilay@cs.wisc.edu    # Function definition and declaration
3266657Snate@binkert.org    def p_decl__func_decl(self, p):
3279298Snilay@cs.wisc.edu        "decl : func_decl"
3289298Snilay@cs.wisc.edu        p[0] = p[1]
3299298Snilay@cs.wisc.edu
3309298Snilay@cs.wisc.edu    def p_func_decl__0(self, p):
3319298Snilay@cs.wisc.edu        """func_decl :  void ident '(' params ')' pairs SEMI
3326657Snate@binkert.org                | type ident '(' params ')' pairs SEMI"""
3336657Snate@binkert.org        p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
3346657Snate@binkert.org
3356657Snate@binkert.org    def p_decl__func_def(self, p):
3369298Snilay@cs.wisc.edu        "decl : func_def"
3379298Snilay@cs.wisc.edu        p[0] = p[1]
3389298Snilay@cs.wisc.edu
3399298Snilay@cs.wisc.edu    def p_func_def__0(self, p):
3409298Snilay@cs.wisc.edu        """func_def : void ident '(' params ')' pairs statements
3419298Snilay@cs.wisc.edu            | type ident '(' params ')' pairs statements"""
3426657Snate@binkert.org        p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
3436657Snate@binkert.org
3446657Snate@binkert.org    # Type fields
3456657Snate@binkert.org    def p_type_members__list(self, p):
3466657Snate@binkert.org        "type_members : type_member type_members"
3476657Snate@binkert.org        p[0] = [ p[1] ] + p[2]
3486657Snate@binkert.org
3496657Snate@binkert.org    def p_type_members__empty(self, p):
3506657Snate@binkert.org        "type_members : empty"
3516657Snate@binkert.org        p[0] = []
3526657Snate@binkert.org
3538156Snilay@cs.wisc.edu    def p_type_method__0(self, p):
3548156Snilay@cs.wisc.edu        "type_member : type_or_void ident '(' types ')' pairs SEMI"
3558156Snilay@cs.wisc.edu        p[0] = ast.TypeFieldMethodAST(self, p[1], p[2], p[4], p[6])
3568156Snilay@cs.wisc.edu
3579298Snilay@cs.wisc.edu    def p_type_method__1(self, p):
3589298Snilay@cs.wisc.edu        "type_member : type_or_void ident '(' params ')' pairs statements"
3599298Snilay@cs.wisc.edu        p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
3609298Snilay@cs.wisc.edu
3616657Snate@binkert.org    def p_type_member__1(self, p):
3628156Snilay@cs.wisc.edu        "type_member : type_or_void ident pairs SEMI"
3636657Snate@binkert.org        p[0] = ast.TypeFieldMemberAST(self, p[1], p[2], p[3], None)
3646657Snate@binkert.org
3656657Snate@binkert.org    def p_type_member__2(self, p):
3668156Snilay@cs.wisc.edu        "type_member : type_or_void ident ASSIGN expr SEMI"
3676657Snate@binkert.org        p[0] = ast.TypeFieldMemberAST(self, p[1], p[2],
3686657Snate@binkert.org                                      ast.PairListAST(self), p[4])
3696657Snate@binkert.org
3706657Snate@binkert.org    # Enum fields
3716657Snate@binkert.org    def p_type_enums__list(self, p):
3726657Snate@binkert.org        "type_enums : type_enum type_enums"
3736657Snate@binkert.org        p[0] = [ p[1] ] + p[2]
3746657Snate@binkert.org
3756657Snate@binkert.org    def p_type_enums__empty(self, p):
3766657Snate@binkert.org        "type_enums : empty"
3776657Snate@binkert.org        p[0] = []
3786657Snate@binkert.org
3796657Snate@binkert.org    def p_type_enum(self, p):
3806657Snate@binkert.org        "type_enum : ident pairs SEMI"
3816657Snate@binkert.org        p[0] = ast.TypeFieldEnumAST(self, p[1], p[2])
3826657Snate@binkert.org
3838086SBrad.Beckmann@amd.com    # States
3848086SBrad.Beckmann@amd.com    def p_type_states__list(self, p):
3858086SBrad.Beckmann@amd.com        "type_states : type_state type_states"
3868086SBrad.Beckmann@amd.com        p[0] = [ p[1] ] + p[2]
3878086SBrad.Beckmann@amd.com
3888086SBrad.Beckmann@amd.com    def p_type_states__empty(self, p):
3898086SBrad.Beckmann@amd.com        "type_states : empty"
3908086SBrad.Beckmann@amd.com        p[0] = []
3918086SBrad.Beckmann@amd.com
3928086SBrad.Beckmann@amd.com    def p_type_state(self, p):
3938086SBrad.Beckmann@amd.com        "type_state : ident ',' enumeration pairs SEMI"
3948086SBrad.Beckmann@amd.com        p[0] = ast.TypeFieldStateAST(self, p[1], p[3], p[4])
3958086SBrad.Beckmann@amd.com
3966657Snate@binkert.org    # Type
3976657Snate@binkert.org    def p_types__multiple(self, p):
3986657Snate@binkert.org        "types : type ',' types"
3996657Snate@binkert.org        p[0] = [ p[1] ] + p[3]
4006657Snate@binkert.org
4016657Snate@binkert.org    def p_types__one(self, p):
4026657Snate@binkert.org        "types : type"
4036657Snate@binkert.org        p[0] = [ p[1] ]
4046657Snate@binkert.org
4056657Snate@binkert.org    def p_types__empty(self, p):
4066657Snate@binkert.org        "types : empty"
4076657Snate@binkert.org        p[0] = []
4086657Snate@binkert.org
4097055Snate@binkert.org    def p_typestr__multi(self, p):
4107055Snate@binkert.org        "typestr : typestr DOUBLE_COLON ident"
4117055Snate@binkert.org        p[0] = '%s::%s' % (p[1], p[3])
4127055Snate@binkert.org
4137055Snate@binkert.org    def p_typestr__single(self, p):
4147055Snate@binkert.org        "typestr : ident"
4157055Snate@binkert.org        p[0] = p[1]
4167055Snate@binkert.org
4177055Snate@binkert.org    def p_type__one(self, p):
4187055Snate@binkert.org        "type : typestr"
4196657Snate@binkert.org        p[0] = ast.TypeAST(self, p[1])
4206657Snate@binkert.org
4216657Snate@binkert.org    def p_void(self, p):
4226657Snate@binkert.org        "void : VOID"
4236657Snate@binkert.org        p[0] = ast.TypeAST(self, p[1])
4246657Snate@binkert.org
4256657Snate@binkert.org    def p_type_or_void(self, p):
4266657Snate@binkert.org        """type_or_void : type
4276657Snate@binkert.org                        | void"""
4286657Snate@binkert.org        p[0] = p[1]
4296657Snate@binkert.org
4306657Snate@binkert.org    # Formal Param
4316657Snate@binkert.org    def p_params__many(self, p):
4326657Snate@binkert.org        "params : param ',' params"
4336657Snate@binkert.org        p[0] = [ p[1] ] + p[3]
4346657Snate@binkert.org
4356657Snate@binkert.org    def p_params__one(self, p):
4366657Snate@binkert.org        "params : param"
4376657Snate@binkert.org        p[0] = [ p[1] ]
4386657Snate@binkert.org
4396657Snate@binkert.org    def p_params__none(self, p):
4406657Snate@binkert.org        "params : empty"
4416657Snate@binkert.org        p[0] = []
4426657Snate@binkert.org
4436657Snate@binkert.org    def p_param(self, p):
4446657Snate@binkert.org        "param : type ident"
4456657Snate@binkert.org        p[0] = ast.FormalParamAST(self, p[1], p[2])
4466657Snate@binkert.org
4476882SBrad.Beckmann@amd.com    def p_param__pointer(self, p):
4486882SBrad.Beckmann@amd.com        "param : type STAR ident"
4496882SBrad.Beckmann@amd.com        p[0] = ast.FormalParamAST(self, p[1], p[3], None, True)
4506882SBrad.Beckmann@amd.com
4516907SBrad.Beckmann@amd.com    def p_param__pointer_default(self, p):
4526907SBrad.Beckmann@amd.com        "param : type STAR ident '=' STRING"
4536907SBrad.Beckmann@amd.com        p[0] = ast.FormalParamAST(self, p[1], p[3], p[5], True)
4546907SBrad.Beckmann@amd.com
4556907SBrad.Beckmann@amd.com    def p_param__default_number(self, p):
4566877Ssteve.reinhardt@amd.com        "param : type ident '=' NUMBER"
4576877Ssteve.reinhardt@amd.com        p[0] = ast.FormalParamAST(self, p[1], p[2], p[4])
4586877Ssteve.reinhardt@amd.com
4596907SBrad.Beckmann@amd.com    def p_param__default_bool(self, p):
4606907SBrad.Beckmann@amd.com        "param : type ident '=' LIT_BOOL"
4616907SBrad.Beckmann@amd.com        p[0] = ast.FormalParamAST(self, p[1], p[2], p[4])
4626907SBrad.Beckmann@amd.com
4636907SBrad.Beckmann@amd.com    def p_param__default_string(self, p):
4646907SBrad.Beckmann@amd.com        "param : type ident '=' STRING"
4656907SBrad.Beckmann@amd.com        p[0] = ast.FormalParamAST(self, p[1], p[2], p[4])
4666907SBrad.Beckmann@amd.com
4676657Snate@binkert.org    # Idents and lists
4686657Snate@binkert.org    def p_idents__braced(self, p):
4696657Snate@binkert.org        "idents : '{' identx '}'"
4706657Snate@binkert.org        p[0] = p[2]
4716657Snate@binkert.org
4726657Snate@binkert.org    def p_idents__bare(self, p):
4736657Snate@binkert.org        "idents : ident"
4746657Snate@binkert.org        p[0] = [ p[1] ]
4756657Snate@binkert.org
4766657Snate@binkert.org    def p_identx__multiple_1(self, p):
4776657Snate@binkert.org        """identx : ident SEMI identx
4786657Snate@binkert.org                  | ident ',' identx"""
4796657Snate@binkert.org        p[0] = [ p[1] ] + p[3]
4806657Snate@binkert.org
4816657Snate@binkert.org    def p_identx__multiple_2(self, p):
4826657Snate@binkert.org        "identx : ident identx"
4836657Snate@binkert.org        p[0] = [ p[1] ] + p[2]
4846657Snate@binkert.org
4856657Snate@binkert.org    def p_identx__single(self, p):
4866657Snate@binkert.org        "identx : empty"
4876657Snate@binkert.org        p[0] = [ ]
4886657Snate@binkert.org
4896657Snate@binkert.org    def p_ident(self, p):
4906657Snate@binkert.org        "ident : IDENT"
4916657Snate@binkert.org        p[0] = p[1]
4926657Snate@binkert.org
4936657Snate@binkert.org    # Pair and pair lists
4946657Snate@binkert.org    def p_pairs__list(self, p):
4956657Snate@binkert.org        "pairs : ',' pairsx"
4966657Snate@binkert.org        p[0] = p[2]
4976657Snate@binkert.org
4986657Snate@binkert.org    def p_pairs__empty(self, p):
4996657Snate@binkert.org        "pairs : empty"
5006657Snate@binkert.org        p[0] = ast.PairListAST(self)
5016657Snate@binkert.org
5026657Snate@binkert.org    def p_pairsx__many(self, p):
5036657Snate@binkert.org        "pairsx : pair ',' pairsx"
5046657Snate@binkert.org        p[0] = p[3]
5056657Snate@binkert.org        p[0].addPair(p[1])
5066657Snate@binkert.org
5076657Snate@binkert.org    def p_pairsx__one(self, p):
5086657Snate@binkert.org        "pairsx : pair"
5096657Snate@binkert.org        p[0] = ast.PairListAST(self)
5106657Snate@binkert.org        p[0].addPair(p[1])
5116657Snate@binkert.org
5126657Snate@binkert.org    def p_pair__assign(self, p):
5136657Snate@binkert.org        """pair : ident '=' STRING
5147567SBrad.Beckmann@amd.com                | ident '=' ident
5157567SBrad.Beckmann@amd.com                | ident '=' NUMBER"""
5166657Snate@binkert.org        p[0] = ast.PairAST(self, p[1], p[3])
5176657Snate@binkert.org
5186657Snate@binkert.org    def p_pair__literal(self, p):
5196657Snate@binkert.org        "pair : STRING"
5206657Snate@binkert.org        p[0] = ast.PairAST(self, "short", p[1])
5216657Snate@binkert.org
5226657Snate@binkert.org    # Below are the rules for action descriptions
5236657Snate@binkert.org    def p_statements__inner(self, p):
5246657Snate@binkert.org        "statements : '{' statements_inner '}'"
5256657Snate@binkert.org        p[0] = ast.StatementListAST(self, p[2])
5266657Snate@binkert.org
5276657Snate@binkert.org    def p_statements__none(self, p):
5286657Snate@binkert.org        "statements : '{' '}'"
5296657Snate@binkert.org        p[0] = ast.StatementListAST(self, [])
5306657Snate@binkert.org
5316657Snate@binkert.org    def p_statements_inner__many(self, p):
5326657Snate@binkert.org        "statements_inner : statement statements_inner"
5336657Snate@binkert.org        p[0] = [ p[1] ] + p[2]
5346657Snate@binkert.org
5356657Snate@binkert.org    def p_statements_inner__one(self, p):
5366657Snate@binkert.org        "statements_inner : statement"
5376657Snate@binkert.org        p[0] = [ p[1] ]
5386657Snate@binkert.org
5396657Snate@binkert.org    def p_exprs__multiple(self, p):
5406657Snate@binkert.org        "exprs : expr ',' exprs"
5416657Snate@binkert.org        p[0] = [ p[1] ] + p[3]
5426657Snate@binkert.org
5436657Snate@binkert.org    def p_exprs__one(self, p):
5446657Snate@binkert.org        "exprs : expr"
5456657Snate@binkert.org        p[0] = [ p[1] ]
5466657Snate@binkert.org
5476657Snate@binkert.org    def p_exprs__empty(self, p):
5486657Snate@binkert.org        "exprs : empty"""
5496657Snate@binkert.org        p[0] = []
5506657Snate@binkert.org
5516657Snate@binkert.org    def p_statement__expression(self, p):
5526657Snate@binkert.org        "statement : expr SEMI"
5536657Snate@binkert.org        p[0] = ast.ExprStatementAST(self, p[1])
5546657Snate@binkert.org
5556657Snate@binkert.org    def p_statement__assign(self, p):
5566657Snate@binkert.org        "statement : expr ASSIGN expr SEMI"
5576657Snate@binkert.org        p[0] = ast.AssignStatementAST(self, p[1], p[3])
5586657Snate@binkert.org
5596657Snate@binkert.org    def p_statement__enqueue(self, p):
56010155Snilay@cs.wisc.edu        "statement : ENQUEUE '(' var ',' type ')' statements"
56110155Snilay@cs.wisc.edu        p[0] = ast.EnqueueStatementAST(self, p[3], p[5], None, p[7])
56210155Snilay@cs.wisc.edu
56310155Snilay@cs.wisc.edu    def p_statement__enqueue_latency(self, p):
56410155Snilay@cs.wisc.edu        "statement : ENQUEUE '(' var ',' type ',' expr ')' statements"
56510155Snilay@cs.wisc.edu        p[0] = ast.EnqueueStatementAST(self, p[3], p[5], p[7], p[9])
5666657Snate@binkert.org
5677567SBrad.Beckmann@amd.com    def p_statement__stall_and_wait(self, p):
5687567SBrad.Beckmann@amd.com        "statement : STALL_AND_WAIT '(' var ',' var ')' SEMI"
5697567SBrad.Beckmann@amd.com        p[0] = ast.StallAndWaitStatementAST(self, p[3], p[5])
5707567SBrad.Beckmann@amd.com
5716657Snate@binkert.org    def p_statement__peek(self, p):
5726863Sdrh5@cs.wisc.edu        "statement : PEEK '(' var ',' type pairs ')' statements"
5736863Sdrh5@cs.wisc.edu        p[0] = ast.PeekStatementAST(self, p[3], p[5], p[6], p[8], "peek")
5746657Snate@binkert.org
5756657Snate@binkert.org    def p_statement__check_allocate(self, p):
5766657Snate@binkert.org        "statement : CHECK_ALLOCATE '(' var ')' SEMI"
5776657Snate@binkert.org        p[0] = ast.CheckAllocateStatementAST(self, p[3])
5786657Snate@binkert.org
5796657Snate@binkert.org    def p_statement__check_stop(self, p):
5806657Snate@binkert.org        "statement : CHECK_STOP_SLOTS '(' var ',' STRING ',' STRING ')' SEMI"
5816657Snate@binkert.org        p[0] = ast.CheckStopStatementAST(self, p[3], p[5], p[7])
5826657Snate@binkert.org
5836657Snate@binkert.org    def p_statement__return(self, p):
5846657Snate@binkert.org        "statement : RETURN expr SEMI"
5856657Snate@binkert.org        p[0] = ast.ReturnStatementAST(self, p[2])
5866657Snate@binkert.org
5876657Snate@binkert.org    def p_statement__if(self, p):
5886657Snate@binkert.org        "statement : if_statement"
5896657Snate@binkert.org        p[0] = p[1]
5906657Snate@binkert.org
5916657Snate@binkert.org    def p_if_statement__if(self, p):
5926657Snate@binkert.org        "if_statement : IF '(' expr ')' statements"
5936657Snate@binkert.org        p[0] = ast.IfStatementAST(self, p[3], p[5], None)
5946657Snate@binkert.org
5956657Snate@binkert.org    def p_if_statement__if_else(self, p):
5966657Snate@binkert.org        "if_statement : IF '(' expr ')' statements ELSE statements"
5976657Snate@binkert.org        p[0] = ast.IfStatementAST(self, p[3], p[5], p[7])
5986657Snate@binkert.org
5996657Snate@binkert.org    def p_statement__if_else_if(self, p):
6006657Snate@binkert.org        "if_statement : IF '(' expr ')' statements ELSE if_statement"
6016657Snate@binkert.org        p[0] = ast.IfStatementAST(self, p[3], p[5],
6026657Snate@binkert.org                                  ast.StatementListAST(self, p[7]))
6036657Snate@binkert.org
60410155Snilay@cs.wisc.edu    def p_expr__static_cast(self, p):
60510155Snilay@cs.wisc.edu        "aexpr : STATIC_CAST '(' type ',' expr ')'"
60610155Snilay@cs.wisc.edu        p[0] = ast.StaticCastAST(self, p[3], "ref", p[5])
60710155Snilay@cs.wisc.edu
60810155Snilay@cs.wisc.edu    def p_expr__static_cast_ptr(self, p):
60910155Snilay@cs.wisc.edu        "aexpr : STATIC_CAST '(' type ',' STRING ',' expr ')'"
61010155Snilay@cs.wisc.edu        p[0] = ast.StaticCastAST(self, p[3], p[5], p[7])
61110155Snilay@cs.wisc.edu
6126657Snate@binkert.org    def p_expr__var(self, p):
6136657Snate@binkert.org        "aexpr : var"
6146657Snate@binkert.org        p[0] = p[1]
6156657Snate@binkert.org
6167839Snilay@cs.wisc.edu    def p_expr__localvar(self, p):
6177839Snilay@cs.wisc.edu        "aexpr : type ident"
6187839Snilay@cs.wisc.edu        p[0] = ast.LocalVariableAST(self, p[1], p[2])
6197839Snilay@cs.wisc.edu
6206657Snate@binkert.org    def p_expr__literal(self, p):
6216657Snate@binkert.org        "aexpr : literal"
6226657Snate@binkert.org        p[0] = p[1]
6236657Snate@binkert.org
6246657Snate@binkert.org    def p_expr__enumeration(self, p):
6256657Snate@binkert.org        "aexpr : enumeration"
6266657Snate@binkert.org        p[0] = p[1]
6276657Snate@binkert.org
6286657Snate@binkert.org    def p_expr__func_call(self, p):
6296657Snate@binkert.org        "aexpr : ident '(' exprs ')'"
6306657Snate@binkert.org        p[0] = ast.FuncCallExprAST(self, p[1], p[3])
6316657Snate@binkert.org
6326657Snate@binkert.org    def p_expr__new(self, p):
6336657Snate@binkert.org        "aexpr : NEW type"
6346657Snate@binkert.org        p[0] = ast.NewExprAST(self, p[2])
6356657Snate@binkert.org
6367839Snilay@cs.wisc.edu    def p_expr__null(self, p):
6377839Snilay@cs.wisc.edu        "aexpr : OOD"
6387839Snilay@cs.wisc.edu        p[0] = ast.OodAST(self)
6397839Snilay@cs.wisc.edu
6406657Snate@binkert.org    def p_expr__member(self, p):
6416657Snate@binkert.org        "aexpr : aexpr DOT ident"
6426657Snate@binkert.org        p[0] = ast.MemberExprAST(self, p[1], p[3])
6436657Snate@binkert.org
6446657Snate@binkert.org    def p_expr__member_method_call(self, p):
6456657Snate@binkert.org        "aexpr : aexpr DOT ident '(' exprs ')'"
6466657Snate@binkert.org        p[0] = ast.MemberMethodCallExprAST(self, p[1], p[3], p[5])
6476657Snate@binkert.org
6486657Snate@binkert.org    def p_expr__member_method_call_lookup(self, p):
6496657Snate@binkert.org        "aexpr : aexpr '[' exprs ']'"
6506657Snate@binkert.org        p[0] = ast.MemberMethodCallExprAST(self, p[1], "lookup", p[3])
6516657Snate@binkert.org
6526657Snate@binkert.org    def p_expr__class_method_call(self, p):
6536657Snate@binkert.org        "aexpr : type DOUBLE_COLON ident '(' exprs ')'"
6546657Snate@binkert.org        p[0] = ast.ClassMethodCallExprAST(self, p[1], p[3], p[5])
6556657Snate@binkert.org
6566657Snate@binkert.org    def p_expr__aexpr(self, p):
6576657Snate@binkert.org        "expr : aexpr"
6586657Snate@binkert.org        p[0] = p[1]
6596657Snate@binkert.org
6606657Snate@binkert.org    def p_expr__binary_op(self, p):
6616657Snate@binkert.org        """expr : expr STAR  expr
6626657Snate@binkert.org                | expr SLASH expr
6636657Snate@binkert.org                | expr PLUS  expr
6646657Snate@binkert.org                | expr DASH  expr
6656657Snate@binkert.org                | expr LT    expr
6666657Snate@binkert.org                | expr GT    expr
6676657Snate@binkert.org                | expr LE    expr
6686657Snate@binkert.org                | expr GE    expr
6696657Snate@binkert.org                | expr EQ    expr
6706657Snate@binkert.org                | expr NE    expr
6716657Snate@binkert.org                | expr AND   expr
6726657Snate@binkert.org                | expr OR    expr
6736657Snate@binkert.org                | expr RIGHTSHIFT expr
6746657Snate@binkert.org                | expr LEFTSHIFT  expr"""
6756657Snate@binkert.org        p[0] = ast.InfixOperatorExprAST(self, p[1], p[2], p[3])
6766657Snate@binkert.org
6776657Snate@binkert.org    # FIXME - unary not
6786657Snate@binkert.org    def p_expr__unary_op(self, p):
6796657Snate@binkert.org        """expr : NOT expr
6809692Snilay@cs.wisc.edu                | INCR expr
6819692Snilay@cs.wisc.edu                | DECR expr
6826657Snate@binkert.org                | DASH expr %prec UMINUS"""
6839692Snilay@cs.wisc.edu        p[0] = ast.PrefixOperatorExprAST(self, p[1], p[2])
6846657Snate@binkert.org
6856657Snate@binkert.org    def p_expr__parens(self, p):
6866657Snate@binkert.org        "aexpr : '(' expr ')'"
6876657Snate@binkert.org        p[0] = p[2]
6886657Snate@binkert.org
6897839Snilay@cs.wisc.edu    def p_expr__is_valid_ptr(self, p):
6907839Snilay@cs.wisc.edu        "aexpr : IS_VALID '(' var ')'"
6917839Snilay@cs.wisc.edu        p[0] = ast.IsValidPtrExprAST(self, p[3], True)
6927839Snilay@cs.wisc.edu
6937839Snilay@cs.wisc.edu    def p_expr__is_invalid_ptr(self, p):
6947839Snilay@cs.wisc.edu        "aexpr : IS_INVALID '(' var ')'"
6957839Snilay@cs.wisc.edu        p[0] = ast.IsValidPtrExprAST(self, p[3], False)
6967839Snilay@cs.wisc.edu
6976657Snate@binkert.org    def p_literal__string(self, p):
6986657Snate@binkert.org        "literal : STRING"
6997055Snate@binkert.org        p[0] = ast.LiteralExprAST(self, p[1], "std::string")
7006657Snate@binkert.org
7016657Snate@binkert.org    def p_literal__number(self, p):
7026657Snate@binkert.org        "literal : NUMBER"
7036657Snate@binkert.org        p[0] = ast.LiteralExprAST(self, p[1], "int")
7046657Snate@binkert.org
7056657Snate@binkert.org    def p_literal__float(self, p):
7066657Snate@binkert.org        "literal : FLOATNUMBER"
7076657Snate@binkert.org        p[0] = ast.LiteralExprAST(self, p[1], "int")
7086657Snate@binkert.org
7096657Snate@binkert.org    def p_literal__bool(self, p):
7106657Snate@binkert.org        "literal : LIT_BOOL"
7116657Snate@binkert.org        p[0] = ast.LiteralExprAST(self, p[1], "bool")
7126657Snate@binkert.org
7136657Snate@binkert.org    def p_enumeration(self, p):
7146657Snate@binkert.org        "enumeration : ident ':' ident"
7156657Snate@binkert.org        p[0] = ast.EnumExprAST(self, ast.TypeAST(self, p[1]), p[3])
7166657Snate@binkert.org
7176657Snate@binkert.org    def p_var(self, p):
7186657Snate@binkert.org        "var : ident"
7196657Snate@binkert.org        p[0] = ast.VarExprAST(self, p[1])
720