parser.py revision 11283
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.generate() 666657Snate@binkert.org 679219Spower.jg@gmail.com def writeCodeFiles(self, code_path, includes): 689219Spower.jg@gmail.com self.symtab.writeCodeFiles(code_path, includes) 696657Snate@binkert.org 708453Snate@binkert.org def writeHTMLFiles(self, html_path): 718453Snate@binkert.org self.symtab.writeHTMLFiles(html_path) 726657Snate@binkert.org 736657Snate@binkert.org def files(self): 7411283Santhony.gutierrez@amd.com f = set(['Types.hh']) 756657Snate@binkert.org 768454Snate@binkert.org f |= self.decl_list.files() 776657Snate@binkert.org 786714Ssteve.reinhardt@amd.com return f 796657Snate@binkert.org 806657Snate@binkert.org t_ignore = '\t ' 816657Snate@binkert.org 826657Snate@binkert.org # C or C++ comment (ignore) 836657Snate@binkert.org def t_c_comment(self, t): 846657Snate@binkert.org r'/\*(.|\n)*?\*/' 856657Snate@binkert.org t.lexer.lineno += t.value.count('\n') 866657Snate@binkert.org 876657Snate@binkert.org def t_cpp_comment(self, t): 886657Snate@binkert.org r'//.*' 896657Snate@binkert.org 906657Snate@binkert.org # Define a rule so we can track line numbers 916657Snate@binkert.org def t_newline(self, t): 926657Snate@binkert.org r'\n+' 936657Snate@binkert.org t.lexer.lineno += len(t.value) 946657Snate@binkert.org 956657Snate@binkert.org reserved = { 968454Snate@binkert.org 'protocol' : 'PROTOCOL', 978454Snate@binkert.org 'include' : 'INCLUDE', 986657Snate@binkert.org 'global' : 'GLOBAL', 996657Snate@binkert.org 'machine' : 'MACHINE', 1006657Snate@binkert.org 'in_port' : 'IN_PORT', 1016657Snate@binkert.org 'out_port' : 'OUT_PORT', 1026657Snate@binkert.org 'action' : 'ACTION', 1036657Snate@binkert.org 'transition' : 'TRANS', 1046657Snate@binkert.org 'structure' : 'STRUCT', 1056657Snate@binkert.org 'external_type' : 'EXTERN_TYPE', 1066657Snate@binkert.org 'enumeration' : 'ENUM', 1078086SBrad.Beckmann@amd.com 'state_declaration' : 'STATE_DECL', 1086657Snate@binkert.org 'peek' : 'PEEK', 1097567SBrad.Beckmann@amd.com 'stall_and_wait' : 'STALL_AND_WAIT', 1106657Snate@binkert.org 'enqueue' : 'ENQUEUE', 1116657Snate@binkert.org 'check_allocate' : 'CHECK_ALLOCATE', 11210981SBrad.Beckmann@amd.com 'check_next_cycle' : 'CHECK_NEXT_CYCLE', 1136657Snate@binkert.org 'check_stop_slots' : 'CHECK_STOP_SLOTS', 1146882SBrad.Beckmann@amd.com 'static_cast' : 'STATIC_CAST', 1156657Snate@binkert.org 'if' : 'IF', 1167839Snilay@cs.wisc.edu 'is_valid' : 'IS_VALID', 1177839Snilay@cs.wisc.edu 'is_invalid' : 'IS_INVALID', 1186657Snate@binkert.org 'else' : 'ELSE', 1196657Snate@binkert.org 'return' : 'RETURN', 1206657Snate@binkert.org 'void' : 'VOID', 1216657Snate@binkert.org 'new' : 'NEW', 1227839Snilay@cs.wisc.edu 'OOD' : 'OOD', 1236657Snate@binkert.org } 1246657Snate@binkert.org 1256657Snate@binkert.org literals = ':[]{}(),=' 1266657Snate@binkert.org 1276657Snate@binkert.org tokens = [ 'EQ', 'NE', 'LT', 'GT', 'LE', 'GE', 1286657Snate@binkert.org 'LEFTSHIFT', 'RIGHTSHIFT', 1296657Snate@binkert.org 'NOT', 'AND', 'OR', 1306657Snate@binkert.org 'PLUS', 'DASH', 'STAR', 'SLASH', 1319692Snilay@cs.wisc.edu 'INCR', 'DECR', 1326657Snate@binkert.org 'DOUBLE_COLON', 'SEMI', 1336657Snate@binkert.org 'ASSIGN', 'DOT', 1346657Snate@binkert.org 'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ] 1356657Snate@binkert.org tokens += reserved.values() 1366657Snate@binkert.org 1376657Snate@binkert.org t_EQ = r'==' 1386657Snate@binkert.org t_NE = r'!=' 1396657Snate@binkert.org t_LT = r'<' 1406657Snate@binkert.org t_GT = r'>' 1416657Snate@binkert.org t_LE = r'<=' 1426657Snate@binkert.org t_GE = r'>=' 1436657Snate@binkert.org t_LEFTSHIFT = r'<<' 1446657Snate@binkert.org t_RIGHTSHIFT = r'>>' 1456657Snate@binkert.org t_NOT = r'!' 1466657Snate@binkert.org t_AND = r'&&' 1476657Snate@binkert.org t_OR = r'\|\|' 1486657Snate@binkert.org t_PLUS = r'\+' 1496657Snate@binkert.org t_DASH = r'-' 1506657Snate@binkert.org t_STAR = r'\*' 1516657Snate@binkert.org t_SLASH = r'/' 1526657Snate@binkert.org t_DOUBLE_COLON = r'::' 1536657Snate@binkert.org t_SEMI = r';' 1546657Snate@binkert.org t_ASSIGN = r':=' 1556657Snate@binkert.org t_DOT = r'\.' 1569692Snilay@cs.wisc.edu t_INCR = r'\+\+' 1579692Snilay@cs.wisc.edu t_DECR = r'--' 1586657Snate@binkert.org 1596657Snate@binkert.org precedence = ( 1609692Snilay@cs.wisc.edu ('left', 'INCR', 'DECR'), 1616657Snate@binkert.org ('left', 'AND', 'OR'), 1626657Snate@binkert.org ('left', 'EQ', 'NE'), 1636657Snate@binkert.org ('left', 'LT', 'GT', 'LE', 'GE'), 1646657Snate@binkert.org ('left', 'RIGHTSHIFT', 'LEFTSHIFT'), 1656657Snate@binkert.org ('left', 'PLUS', 'DASH'), 1666657Snate@binkert.org ('left', 'STAR', 'SLASH'), 1676657Snate@binkert.org ('right', 'NOT', 'UMINUS'), 1686657Snate@binkert.org ) 1696657Snate@binkert.org 1706657Snate@binkert.org def t_IDENT(self, t): 1716657Snate@binkert.org r'[a-zA-Z_][a-zA-Z_0-9]*' 1726657Snate@binkert.org if t.value == 'true': 1736657Snate@binkert.org t.type = 'LIT_BOOL' 1746657Snate@binkert.org t.value = True 1756657Snate@binkert.org return t 1766657Snate@binkert.org 1776657Snate@binkert.org if t.value == 'false': 1786657Snate@binkert.org t.type = 'LIT_BOOL' 1796657Snate@binkert.org t.value = False 1806657Snate@binkert.org return t 1816657Snate@binkert.org 1826657Snate@binkert.org # Check for reserved words 1836657Snate@binkert.org t.type = self.reserved.get(t.value, 'IDENT') 1846657Snate@binkert.org return t 1856657Snate@binkert.org 1866657Snate@binkert.org def t_FLOATNUMBER(self, t): 1876657Snate@binkert.org '[0-9]+[.][0-9]+' 1886657Snate@binkert.org try: 1896657Snate@binkert.org t.value = float(t.value) 1906657Snate@binkert.org except ValueError: 1918452Snate@binkert.org raise ParseError("Illegal float", t) 1926657Snate@binkert.org return t 1936657Snate@binkert.org 1946657Snate@binkert.org def t_NUMBER(self, t): 1956657Snate@binkert.org r'[0-9]+' 1966657Snate@binkert.org try: 1976657Snate@binkert.org t.value = int(t.value) 1986657Snate@binkert.org except ValueError: 1998452Snate@binkert.org raise ParseError("Illegal number", t) 2006657Snate@binkert.org return t 2016657Snate@binkert.org 2026657Snate@binkert.org def t_STRING1(self, t): 2036657Snate@binkert.org r'\"[^"\n]*\"' 2046657Snate@binkert.org t.type = 'STRING' 2056657Snate@binkert.org t.value = t.value[1:-1] 2066657Snate@binkert.org return t 2076657Snate@binkert.org 2086657Snate@binkert.org def t_STRING2(self, t): 2096657Snate@binkert.org r"\'[^'\n]*\'" 2106657Snate@binkert.org t.type = 'STRING' 2116657Snate@binkert.org t.value = t.value[1:-1] 2126657Snate@binkert.org return t 2136657Snate@binkert.org 2146657Snate@binkert.org def p_file(self, p): 2156657Snate@binkert.org "file : decls" 2166657Snate@binkert.org p[0] = p[1] 2176657Snate@binkert.org 2186657Snate@binkert.org def p_empty(self, p): 2196657Snate@binkert.org "empty :" 2206657Snate@binkert.org 2216657Snate@binkert.org def p_decls(self, p): 2226657Snate@binkert.org "decls : declsx" 2236657Snate@binkert.org p[0] = ast.DeclListAST(self, p[1]) 2246657Snate@binkert.org 2256657Snate@binkert.org def p_declsx__list(self, p): 2266657Snate@binkert.org "declsx : decl declsx" 2278454Snate@binkert.org if isinstance(p[1], ast.DeclListAST): 2288454Snate@binkert.org decls = p[1].decls 2298454Snate@binkert.org elif p[1] is None: 2308454Snate@binkert.org decls = [] 2318454Snate@binkert.org else: 2328454Snate@binkert.org decls = [ p[1] ] 2338454Snate@binkert.org p[0] = decls + p[2] 2346657Snate@binkert.org 2356657Snate@binkert.org def p_declsx__none(self, p): 2366657Snate@binkert.org "declsx : empty" 2376657Snate@binkert.org p[0] = [] 2386657Snate@binkert.org 2398454Snate@binkert.org def p_decl__protocol(self, p): 2408454Snate@binkert.org "decl : PROTOCOL STRING SEMI" 2418454Snate@binkert.org if self.protocol: 2428454Snate@binkert.org msg = "Protocol can only be set once! Error at %s:%s\n" % \ 2438454Snate@binkert.org (self.current_source, self.current_line) 2448454Snate@binkert.org raise ParseError(msg) 2458454Snate@binkert.org self.protocol = p[2] 2468454Snate@binkert.org p[0] = None 2478454Snate@binkert.org 2488454Snate@binkert.org def p_decl__include(self, p): 2498454Snate@binkert.org "decl : INCLUDE STRING SEMI" 2508454Snate@binkert.org dirname = os.path.dirname(self.current_source) 2519219Spower.jg@gmail.com if os.path.exists(os.path.join(dirname, p[2])): 2529219Spower.jg@gmail.com filename = os.path.join(dirname, p[2]) 2539219Spower.jg@gmail.com else: 2549219Spower.jg@gmail.com filename = os.path.join(self.base_dir, p[2]) 2558454Snate@binkert.org p[0] = self.parse_file(filename) 2568454Snate@binkert.org 2579773Snilay@cs.wisc.edu def p_decl__machine0(self, p): 25811283Santhony.gutierrez@amd.com "decl : MACHINE '(' enumeration ')' ':' obj_decls '{' decls '}'" 2599773Snilay@cs.wisc.edu p[0] = ast.MachineAST(self, p[3], [], p[7], p[9]) 2609773Snilay@cs.wisc.edu 2619773Snilay@cs.wisc.edu def p_decl__machine1(self, p): 26211283Santhony.gutierrez@amd.com "decl : MACHINE '(' enumeration pairs ')' ':' obj_decls '{' decls '}'" 2636657Snate@binkert.org p[0] = ast.MachineAST(self, p[3], p[4], p[7], p[9]) 2646657Snate@binkert.org 2656657Snate@binkert.org def p_decl__action(self, p): 2666657Snate@binkert.org "decl : ACTION '(' ident pairs ')' statements" 2676657Snate@binkert.org p[0] = ast.ActionDeclAST(self, p[3], p[4], p[6]) 2686657Snate@binkert.org 2696657Snate@binkert.org def p_decl__in_port(self, p): 2706657Snate@binkert.org "decl : IN_PORT '(' ident ',' type ',' var pairs ')' statements" 2716657Snate@binkert.org p[0] = ast.InPortDeclAST(self, p[3], p[5], p[7], p[8], p[10]) 2726657Snate@binkert.org 2736657Snate@binkert.org def p_decl__out_port(self, p): 2746657Snate@binkert.org "decl : OUT_PORT '(' ident ',' type ',' var pairs ')' SEMI" 2756657Snate@binkert.org p[0] = ast.OutPortDeclAST(self, p[3], p[5], p[7], p[8]) 2766657Snate@binkert.org 2776657Snate@binkert.org def p_decl__trans0(self, p): 27810964Sdavid.hashe@amd.com "decl : TRANS '(' idents ',' idents ',' ident_or_star ')' idents" 27910165Snilay@cs.wisc.edu p[0] = ast.TransitionDeclAST(self, [], p[3], p[5], p[7], p[9]) 2806657Snate@binkert.org 2816657Snate@binkert.org def p_decl__trans1(self, p): 28210165Snilay@cs.wisc.edu "decl : TRANS '(' idents ',' idents ')' idents" 28310165Snilay@cs.wisc.edu p[0] = ast.TransitionDeclAST(self, [], p[3], p[5], None, p[7]) 2849104Shestness@cs.utexas.edu 2859104Shestness@cs.utexas.edu def p_decl__trans2(self, p): 28610964Sdavid.hashe@amd.com "decl : TRANS '(' idents ',' idents ',' ident_or_star ')' idents idents" 28710165Snilay@cs.wisc.edu p[0] = ast.TransitionDeclAST(self, p[9], p[3], p[5], p[7], p[10]) 2889104Shestness@cs.utexas.edu 2899104Shestness@cs.utexas.edu def p_decl__trans3(self, p): 29010165Snilay@cs.wisc.edu "decl : TRANS '(' idents ',' idents ')' idents idents" 29110165Snilay@cs.wisc.edu p[0] = ast.TransitionDeclAST(self, p[7], p[3], p[5], None, p[8]) 2926657Snate@binkert.org 2936657Snate@binkert.org def p_decl__extern0(self, p): 2946657Snate@binkert.org "decl : EXTERN_TYPE '(' type pairs ')' SEMI" 2956657Snate@binkert.org p[4]["external"] = "yes" 2966657Snate@binkert.org p[0] = ast.TypeDeclAST(self, p[3], p[4], []) 2976657Snate@binkert.org 2986657Snate@binkert.org def p_decl__global(self, p): 2996657Snate@binkert.org "decl : GLOBAL '(' type pairs ')' '{' type_members '}'" 3006657Snate@binkert.org p[4]["global"] = "yes" 3016657Snate@binkert.org p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7]) 3026657Snate@binkert.org 3036657Snate@binkert.org def p_decl__struct(self, p): 3046657Snate@binkert.org "decl : STRUCT '(' type pairs ')' '{' type_members '}'" 3056657Snate@binkert.org p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7]) 3066657Snate@binkert.org 3076657Snate@binkert.org def p_decl__enum(self, p): 3086657Snate@binkert.org "decl : ENUM '(' type pairs ')' '{' type_enums '}'" 3096657Snate@binkert.org p[4]["enumeration"] = "yes" 3106657Snate@binkert.org p[0] = ast.EnumDeclAST(self, p[3], p[4], p[7]) 3116657Snate@binkert.org 3128086SBrad.Beckmann@amd.com def p_decl__state_decl(self, p): 3138086SBrad.Beckmann@amd.com "decl : STATE_DECL '(' type pairs ')' '{' type_states '}'" 3148086SBrad.Beckmann@amd.com p[4]["enumeration"] = "yes" 3158086SBrad.Beckmann@amd.com p[4]["state_decl"] = "yes" 3168086SBrad.Beckmann@amd.com p[0] = ast.StateDeclAST(self, p[3], p[4], p[7]) 3178086SBrad.Beckmann@amd.com 31810307Snilay@cs.wisc.edu # Type fields 31910308Snilay@cs.wisc.edu def p_obj_decls__list(self, p): 32010308Snilay@cs.wisc.edu "obj_decls : obj_decl obj_decls" 32110308Snilay@cs.wisc.edu p[0] = [ p[1] ] + p[2] 32210308Snilay@cs.wisc.edu 32310308Snilay@cs.wisc.edu def p_obj_decls__empty(self, p): 32410308Snilay@cs.wisc.edu "obj_decls : empty" 32510308Snilay@cs.wisc.edu p[0] = [] 32610308Snilay@cs.wisc.edu 32710307Snilay@cs.wisc.edu def p_type_members__list(self, p): 32810307Snilay@cs.wisc.edu "type_members : type_member type_members" 32910307Snilay@cs.wisc.edu p[0] = [ p[1] ] + p[2] 33010307Snilay@cs.wisc.edu 33110307Snilay@cs.wisc.edu def p_type_members__empty(self, p): 33210307Snilay@cs.wisc.edu "type_members : empty" 33310307Snilay@cs.wisc.edu p[0] = [] 33410307Snilay@cs.wisc.edu 33510307Snilay@cs.wisc.edu def p_type_member__0(self, p): 33610307Snilay@cs.wisc.edu """type_member : obj_decl 33710307Snilay@cs.wisc.edu | func_decl 33810307Snilay@cs.wisc.edu | func_def""" 33910307Snilay@cs.wisc.edu p[0] = p[1] 34010307Snilay@cs.wisc.edu 34110307Snilay@cs.wisc.edu # Member / Variable declarations 34210307Snilay@cs.wisc.edu def p_decl__obj_decl(self, p): 34310307Snilay@cs.wisc.edu "decl : obj_decl" 34410307Snilay@cs.wisc.edu p[0] = p[1] 34510307Snilay@cs.wisc.edu 34610307Snilay@cs.wisc.edu def p_obj_decl__0(self, p): 34710307Snilay@cs.wisc.edu "obj_decl : type ident pairs SEMI" 34810308Snilay@cs.wisc.edu p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3], None, False) 34910307Snilay@cs.wisc.edu 35010307Snilay@cs.wisc.edu def p_obj_decl__1(self, p): 35110307Snilay@cs.wisc.edu "obj_decl : type STAR ident pairs SEMI" 35210308Snilay@cs.wisc.edu p[0] = ast.ObjDeclAST(self, p[1], p[3], p[4], None, True) 35310307Snilay@cs.wisc.edu 35410307Snilay@cs.wisc.edu def p_obj_decl__2(self, p): 35510307Snilay@cs.wisc.edu "obj_decl : type ident ASSIGN expr SEMI" 35610308Snilay@cs.wisc.edu p[0] = ast.ObjDeclAST(self, p[1], p[2], ast.PairListAST(self), p[4], 35710308Snilay@cs.wisc.edu False) 35810307Snilay@cs.wisc.edu 35910307Snilay@cs.wisc.edu def p_obj_decl__3(self, p): 36010307Snilay@cs.wisc.edu "obj_decl : type STAR ident ASSIGN expr SEMI" 36110308Snilay@cs.wisc.edu p[0] = ast.ObjDeclAST(self, p[1], p[3], ast.PairListAST(self), p[5], 36210308Snilay@cs.wisc.edu True) 3636657Snate@binkert.org 3649298Snilay@cs.wisc.edu # Function definition and declaration 3656657Snate@binkert.org def p_decl__func_decl(self, p): 3669298Snilay@cs.wisc.edu "decl : func_decl" 3679298Snilay@cs.wisc.edu p[0] = p[1] 3689298Snilay@cs.wisc.edu 3699298Snilay@cs.wisc.edu def p_func_decl__0(self, p): 3709298Snilay@cs.wisc.edu """func_decl : void ident '(' params ')' pairs SEMI 3716657Snate@binkert.org | type ident '(' params ')' pairs SEMI""" 3726657Snate@binkert.org p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None) 3736657Snate@binkert.org 37410307Snilay@cs.wisc.edu def p_func_decl__1(self, p): 37510307Snilay@cs.wisc.edu """func_decl : void ident '(' types ')' pairs SEMI 37610307Snilay@cs.wisc.edu | type ident '(' types ')' pairs SEMI""" 37710307Snilay@cs.wisc.edu p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None) 37810307Snilay@cs.wisc.edu 3796657Snate@binkert.org def p_decl__func_def(self, p): 3809298Snilay@cs.wisc.edu "decl : func_def" 3819298Snilay@cs.wisc.edu p[0] = p[1] 3829298Snilay@cs.wisc.edu 3839298Snilay@cs.wisc.edu def p_func_def__0(self, p): 3849298Snilay@cs.wisc.edu """func_def : void ident '(' params ')' pairs statements 3859298Snilay@cs.wisc.edu | type ident '(' params ')' pairs statements""" 3866657Snate@binkert.org p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7]) 3876657Snate@binkert.org 3886657Snate@binkert.org # Enum fields 3896657Snate@binkert.org def p_type_enums__list(self, p): 3906657Snate@binkert.org "type_enums : type_enum type_enums" 3916657Snate@binkert.org p[0] = [ p[1] ] + p[2] 3926657Snate@binkert.org 3936657Snate@binkert.org def p_type_enums__empty(self, p): 3946657Snate@binkert.org "type_enums : empty" 3956657Snate@binkert.org p[0] = [] 3966657Snate@binkert.org 3976657Snate@binkert.org def p_type_enum(self, p): 3986657Snate@binkert.org "type_enum : ident pairs SEMI" 3996657Snate@binkert.org p[0] = ast.TypeFieldEnumAST(self, p[1], p[2]) 4006657Snate@binkert.org 4018086SBrad.Beckmann@amd.com # States 4028086SBrad.Beckmann@amd.com def p_type_states__list(self, p): 4038086SBrad.Beckmann@amd.com "type_states : type_state type_states" 4048086SBrad.Beckmann@amd.com p[0] = [ p[1] ] + p[2] 4058086SBrad.Beckmann@amd.com 4068086SBrad.Beckmann@amd.com def p_type_states__empty(self, p): 4078086SBrad.Beckmann@amd.com "type_states : empty" 4088086SBrad.Beckmann@amd.com p[0] = [] 4098086SBrad.Beckmann@amd.com 4108086SBrad.Beckmann@amd.com def p_type_state(self, p): 4118086SBrad.Beckmann@amd.com "type_state : ident ',' enumeration pairs SEMI" 4128086SBrad.Beckmann@amd.com p[0] = ast.TypeFieldStateAST(self, p[1], p[3], p[4]) 4138086SBrad.Beckmann@amd.com 4146657Snate@binkert.org # Formal Param 4156657Snate@binkert.org def p_params__many(self, p): 4166657Snate@binkert.org "params : param ',' params" 4176657Snate@binkert.org p[0] = [ p[1] ] + p[3] 4186657Snate@binkert.org 4196657Snate@binkert.org def p_params__one(self, p): 4206657Snate@binkert.org "params : param" 4216657Snate@binkert.org p[0] = [ p[1] ] 4226657Snate@binkert.org 4236657Snate@binkert.org def p_params__none(self, p): 4246657Snate@binkert.org "params : empty" 4256657Snate@binkert.org p[0] = [] 4266657Snate@binkert.org 4276657Snate@binkert.org def p_param(self, p): 4286657Snate@binkert.org "param : type ident" 4296657Snate@binkert.org p[0] = ast.FormalParamAST(self, p[1], p[2]) 4306657Snate@binkert.org 4316882SBrad.Beckmann@amd.com def p_param__pointer(self, p): 4326882SBrad.Beckmann@amd.com "param : type STAR ident" 4336882SBrad.Beckmann@amd.com p[0] = ast.FormalParamAST(self, p[1], p[3], None, True) 4346882SBrad.Beckmann@amd.com 4356907SBrad.Beckmann@amd.com def p_param__pointer_default(self, p): 43610308Snilay@cs.wisc.edu "param : type STAR ident ASSIGN STRING" 4376907SBrad.Beckmann@amd.com p[0] = ast.FormalParamAST(self, p[1], p[3], p[5], True) 4386907SBrad.Beckmann@amd.com 4396907SBrad.Beckmann@amd.com def p_param__default_number(self, p): 44010308Snilay@cs.wisc.edu "param : type ident ASSIGN NUMBER" 4416877Ssteve.reinhardt@amd.com p[0] = ast.FormalParamAST(self, p[1], p[2], p[4]) 4426877Ssteve.reinhardt@amd.com 4436907SBrad.Beckmann@amd.com def p_param__default_bool(self, p): 44410308Snilay@cs.wisc.edu "param : type ident ASSIGN LIT_BOOL" 4456907SBrad.Beckmann@amd.com p[0] = ast.FormalParamAST(self, p[1], p[2], p[4]) 4466907SBrad.Beckmann@amd.com 4476907SBrad.Beckmann@amd.com def p_param__default_string(self, p): 44810308Snilay@cs.wisc.edu "param : type ident ASSIGN STRING" 4496907SBrad.Beckmann@amd.com p[0] = ast.FormalParamAST(self, p[1], p[2], p[4]) 4506907SBrad.Beckmann@amd.com 45110307Snilay@cs.wisc.edu # Type 45210307Snilay@cs.wisc.edu def p_types__multiple(self, p): 45310307Snilay@cs.wisc.edu "types : type ',' types" 45410307Snilay@cs.wisc.edu p[0] = [ p[1] ] + p[3] 45510307Snilay@cs.wisc.edu 45610307Snilay@cs.wisc.edu def p_types__one(self, p): 45710307Snilay@cs.wisc.edu "types : type" 45810307Snilay@cs.wisc.edu p[0] = [ p[1] ] 45910307Snilay@cs.wisc.edu 46010307Snilay@cs.wisc.edu def p_types__empty(self, p): 46110307Snilay@cs.wisc.edu "types : empty" 46210307Snilay@cs.wisc.edu p[0] = [] 46310307Snilay@cs.wisc.edu 46410307Snilay@cs.wisc.edu def p_typestr__multi(self, p): 46510307Snilay@cs.wisc.edu "typestr : typestr DOUBLE_COLON ident" 46610307Snilay@cs.wisc.edu p[0] = '%s::%s' % (p[1], p[3]) 46710307Snilay@cs.wisc.edu 46810307Snilay@cs.wisc.edu def p_typestr__single(self, p): 46910307Snilay@cs.wisc.edu "typestr : ident" 47010307Snilay@cs.wisc.edu p[0] = p[1] 47110307Snilay@cs.wisc.edu 47210307Snilay@cs.wisc.edu def p_type__one(self, p): 47310307Snilay@cs.wisc.edu "type : typestr" 47410307Snilay@cs.wisc.edu p[0] = ast.TypeAST(self, p[1]) 47510307Snilay@cs.wisc.edu 47610307Snilay@cs.wisc.edu def p_void(self, p): 47710307Snilay@cs.wisc.edu "void : VOID" 47810307Snilay@cs.wisc.edu p[0] = ast.TypeAST(self, p[1]) 47910307Snilay@cs.wisc.edu 4806657Snate@binkert.org # Idents and lists 4816657Snate@binkert.org def p_idents__braced(self, p): 4826657Snate@binkert.org "idents : '{' identx '}'" 4836657Snate@binkert.org p[0] = p[2] 4846657Snate@binkert.org 4856657Snate@binkert.org def p_idents__bare(self, p): 4866657Snate@binkert.org "idents : ident" 4876657Snate@binkert.org p[0] = [ p[1] ] 4886657Snate@binkert.org 4896657Snate@binkert.org def p_identx__multiple_1(self, p): 4906657Snate@binkert.org """identx : ident SEMI identx 4916657Snate@binkert.org | ident ',' identx""" 4926657Snate@binkert.org p[0] = [ p[1] ] + p[3] 4936657Snate@binkert.org 4946657Snate@binkert.org def p_identx__multiple_2(self, p): 4956657Snate@binkert.org "identx : ident identx" 4966657Snate@binkert.org p[0] = [ p[1] ] + p[2] 4976657Snate@binkert.org 4986657Snate@binkert.org def p_identx__single(self, p): 4996657Snate@binkert.org "identx : empty" 5006657Snate@binkert.org p[0] = [ ] 5016657Snate@binkert.org 5026657Snate@binkert.org def p_ident(self, p): 5036657Snate@binkert.org "ident : IDENT" 5046657Snate@binkert.org p[0] = p[1] 5056657Snate@binkert.org 50610964Sdavid.hashe@amd.com def p_ident_or_star(self, p): 50710964Sdavid.hashe@amd.com """ident_or_star : ident 50810964Sdavid.hashe@amd.com | STAR""" 50910964Sdavid.hashe@amd.com p[0] = p[1] 51010964Sdavid.hashe@amd.com 5116657Snate@binkert.org # Pair and pair lists 5126657Snate@binkert.org def p_pairs__list(self, p): 5136657Snate@binkert.org "pairs : ',' pairsx" 5146657Snate@binkert.org p[0] = p[2] 5156657Snate@binkert.org 5166657Snate@binkert.org def p_pairs__empty(self, p): 5176657Snate@binkert.org "pairs : empty" 5186657Snate@binkert.org p[0] = ast.PairListAST(self) 5196657Snate@binkert.org 5206657Snate@binkert.org def p_pairsx__many(self, p): 5216657Snate@binkert.org "pairsx : pair ',' pairsx" 5226657Snate@binkert.org p[0] = p[3] 5236657Snate@binkert.org p[0].addPair(p[1]) 5246657Snate@binkert.org 5256657Snate@binkert.org def p_pairsx__one(self, p): 5266657Snate@binkert.org "pairsx : pair" 5276657Snate@binkert.org p[0] = ast.PairListAST(self) 5286657Snate@binkert.org p[0].addPair(p[1]) 5296657Snate@binkert.org 5306657Snate@binkert.org def p_pair__assign(self, p): 5316657Snate@binkert.org """pair : ident '=' STRING 5327567SBrad.Beckmann@amd.com | ident '=' ident 5337567SBrad.Beckmann@amd.com | ident '=' NUMBER""" 5346657Snate@binkert.org p[0] = ast.PairAST(self, p[1], p[3]) 5356657Snate@binkert.org 5366657Snate@binkert.org def p_pair__literal(self, p): 5376657Snate@binkert.org "pair : STRING" 5386657Snate@binkert.org p[0] = ast.PairAST(self, "short", p[1]) 5396657Snate@binkert.org 5406657Snate@binkert.org # Below are the rules for action descriptions 5416657Snate@binkert.org def p_statements__inner(self, p): 5426657Snate@binkert.org "statements : '{' statements_inner '}'" 5436657Snate@binkert.org p[0] = ast.StatementListAST(self, p[2]) 5446657Snate@binkert.org 5456657Snate@binkert.org def p_statements__none(self, p): 5466657Snate@binkert.org "statements : '{' '}'" 5476657Snate@binkert.org p[0] = ast.StatementListAST(self, []) 5486657Snate@binkert.org 5496657Snate@binkert.org def p_statements_inner__many(self, p): 5506657Snate@binkert.org "statements_inner : statement statements_inner" 5516657Snate@binkert.org p[0] = [ p[1] ] + p[2] 5526657Snate@binkert.org 5536657Snate@binkert.org def p_statements_inner__one(self, p): 5546657Snate@binkert.org "statements_inner : statement" 5556657Snate@binkert.org p[0] = [ p[1] ] 5566657Snate@binkert.org 5576657Snate@binkert.org def p_exprs__multiple(self, p): 5586657Snate@binkert.org "exprs : expr ',' exprs" 5596657Snate@binkert.org p[0] = [ p[1] ] + p[3] 5606657Snate@binkert.org 5616657Snate@binkert.org def p_exprs__one(self, p): 5626657Snate@binkert.org "exprs : expr" 5636657Snate@binkert.org p[0] = [ p[1] ] 5646657Snate@binkert.org 5656657Snate@binkert.org def p_exprs__empty(self, p): 5666657Snate@binkert.org "exprs : empty""" 5676657Snate@binkert.org p[0] = [] 5686657Snate@binkert.org 5696657Snate@binkert.org def p_statement__expression(self, p): 5706657Snate@binkert.org "statement : expr SEMI" 5716657Snate@binkert.org p[0] = ast.ExprStatementAST(self, p[1]) 5726657Snate@binkert.org 5736657Snate@binkert.org def p_statement__assign(self, p): 5746657Snate@binkert.org "statement : expr ASSIGN expr SEMI" 5756657Snate@binkert.org p[0] = ast.AssignStatementAST(self, p[1], p[3]) 5766657Snate@binkert.org 5776657Snate@binkert.org def p_statement__enqueue(self, p): 57810155Snilay@cs.wisc.edu "statement : ENQUEUE '(' var ',' type ')' statements" 57910155Snilay@cs.wisc.edu p[0] = ast.EnqueueStatementAST(self, p[3], p[5], None, p[7]) 58010155Snilay@cs.wisc.edu 58110155Snilay@cs.wisc.edu def p_statement__enqueue_latency(self, p): 58210155Snilay@cs.wisc.edu "statement : ENQUEUE '(' var ',' type ',' expr ')' statements" 58310155Snilay@cs.wisc.edu p[0] = ast.EnqueueStatementAST(self, p[3], p[5], p[7], p[9]) 5846657Snate@binkert.org 5857567SBrad.Beckmann@amd.com def p_statement__stall_and_wait(self, p): 5867567SBrad.Beckmann@amd.com "statement : STALL_AND_WAIT '(' var ',' var ')' SEMI" 5877567SBrad.Beckmann@amd.com p[0] = ast.StallAndWaitStatementAST(self, p[3], p[5]) 5887567SBrad.Beckmann@amd.com 5896657Snate@binkert.org def p_statement__peek(self, p): 5906863Sdrh5@cs.wisc.edu "statement : PEEK '(' var ',' type pairs ')' statements" 5916863Sdrh5@cs.wisc.edu p[0] = ast.PeekStatementAST(self, p[3], p[5], p[6], p[8], "peek") 5926657Snate@binkert.org 5936657Snate@binkert.org def p_statement__check_allocate(self, p): 5946657Snate@binkert.org "statement : CHECK_ALLOCATE '(' var ')' SEMI" 5956657Snate@binkert.org p[0] = ast.CheckAllocateStatementAST(self, p[3]) 5966657Snate@binkert.org 59710981SBrad.Beckmann@amd.com def p_statement__check_next_cycle(self, p): 59810981SBrad.Beckmann@amd.com "statement : CHECK_NEXT_CYCLE '(' ')' SEMI" 59910981SBrad.Beckmann@amd.com p[0] = ast.CheckNextCycleAST(self) 60010981SBrad.Beckmann@amd.com 6016657Snate@binkert.org def p_statement__check_stop(self, p): 6026657Snate@binkert.org "statement : CHECK_STOP_SLOTS '(' var ',' STRING ',' STRING ')' SEMI" 6036657Snate@binkert.org p[0] = ast.CheckStopStatementAST(self, p[3], p[5], p[7]) 6046657Snate@binkert.org 6056657Snate@binkert.org def p_statement__return(self, p): 6066657Snate@binkert.org "statement : RETURN expr SEMI" 6076657Snate@binkert.org p[0] = ast.ReturnStatementAST(self, p[2]) 6086657Snate@binkert.org 6096657Snate@binkert.org def p_statement__if(self, p): 6106657Snate@binkert.org "statement : if_statement" 6116657Snate@binkert.org p[0] = p[1] 6126657Snate@binkert.org 6136657Snate@binkert.org def p_if_statement__if(self, p): 6146657Snate@binkert.org "if_statement : IF '(' expr ')' statements" 6156657Snate@binkert.org p[0] = ast.IfStatementAST(self, p[3], p[5], None) 6166657Snate@binkert.org 6176657Snate@binkert.org def p_if_statement__if_else(self, p): 6186657Snate@binkert.org "if_statement : IF '(' expr ')' statements ELSE statements" 6196657Snate@binkert.org p[0] = ast.IfStatementAST(self, p[3], p[5], p[7]) 6206657Snate@binkert.org 6216657Snate@binkert.org def p_statement__if_else_if(self, p): 6226657Snate@binkert.org "if_statement : IF '(' expr ')' statements ELSE if_statement" 6236657Snate@binkert.org p[0] = ast.IfStatementAST(self, p[3], p[5], 6246657Snate@binkert.org ast.StatementListAST(self, p[7])) 6256657Snate@binkert.org 62610155Snilay@cs.wisc.edu def p_expr__static_cast(self, p): 62710155Snilay@cs.wisc.edu "aexpr : STATIC_CAST '(' type ',' expr ')'" 62810155Snilay@cs.wisc.edu p[0] = ast.StaticCastAST(self, p[3], "ref", p[5]) 62910155Snilay@cs.wisc.edu 63010155Snilay@cs.wisc.edu def p_expr__static_cast_ptr(self, p): 63110155Snilay@cs.wisc.edu "aexpr : STATIC_CAST '(' type ',' STRING ',' expr ')'" 63210155Snilay@cs.wisc.edu p[0] = ast.StaticCastAST(self, p[3], p[5], p[7]) 63310155Snilay@cs.wisc.edu 6346657Snate@binkert.org def p_expr__var(self, p): 6356657Snate@binkert.org "aexpr : var" 6366657Snate@binkert.org p[0] = p[1] 6376657Snate@binkert.org 6387839Snilay@cs.wisc.edu def p_expr__localvar(self, p): 6397839Snilay@cs.wisc.edu "aexpr : type ident" 6407839Snilay@cs.wisc.edu p[0] = ast.LocalVariableAST(self, p[1], p[2]) 6417839Snilay@cs.wisc.edu 6426657Snate@binkert.org def p_expr__literal(self, p): 6436657Snate@binkert.org "aexpr : literal" 6446657Snate@binkert.org p[0] = p[1] 6456657Snate@binkert.org 6466657Snate@binkert.org def p_expr__enumeration(self, p): 6476657Snate@binkert.org "aexpr : enumeration" 6486657Snate@binkert.org p[0] = p[1] 6496657Snate@binkert.org 6506657Snate@binkert.org def p_expr__func_call(self, p): 6516657Snate@binkert.org "aexpr : ident '(' exprs ')'" 6526657Snate@binkert.org p[0] = ast.FuncCallExprAST(self, p[1], p[3]) 6536657Snate@binkert.org 6546657Snate@binkert.org def p_expr__new(self, p): 6556657Snate@binkert.org "aexpr : NEW type" 6566657Snate@binkert.org p[0] = ast.NewExprAST(self, p[2]) 6576657Snate@binkert.org 6587839Snilay@cs.wisc.edu def p_expr__null(self, p): 6597839Snilay@cs.wisc.edu "aexpr : OOD" 6607839Snilay@cs.wisc.edu p[0] = ast.OodAST(self) 6617839Snilay@cs.wisc.edu 6626657Snate@binkert.org def p_expr__member(self, p): 6636657Snate@binkert.org "aexpr : aexpr DOT ident" 6646657Snate@binkert.org p[0] = ast.MemberExprAST(self, p[1], p[3]) 6656657Snate@binkert.org 6666657Snate@binkert.org def p_expr__member_method_call(self, p): 6676657Snate@binkert.org "aexpr : aexpr DOT ident '(' exprs ')'" 66811062Snilay@cs.wisc.edu p[0] = ast.MemberMethodCallExprAST(self, p[1], 66911062Snilay@cs.wisc.edu ast.FuncCallExprAST(self, p[3], p[5])) 67011049Snilay@cs.wisc.edu 67111049Snilay@cs.wisc.edu def p_expr__member_method_call_lookup(self, p): 67211049Snilay@cs.wisc.edu "aexpr : aexpr '[' exprs ']'" 67311062Snilay@cs.wisc.edu p[0] = ast.MemberMethodCallExprAST(self, p[1], 67411062Snilay@cs.wisc.edu ast.FuncCallExprAST(self, "lookup", p[3])) 6756657Snate@binkert.org 6766657Snate@binkert.org def p_expr__class_method_call(self, p): 6776657Snate@binkert.org "aexpr : type DOUBLE_COLON ident '(' exprs ')'" 67811062Snilay@cs.wisc.edu p[0] = ast.ClassMethodCallExprAST(self, p[1], 67911062Snilay@cs.wisc.edu ast.FuncCallExprAST(self, p[3], p[5])) 6806657Snate@binkert.org 6816657Snate@binkert.org def p_expr__aexpr(self, p): 6826657Snate@binkert.org "expr : aexpr" 6836657Snate@binkert.org p[0] = p[1] 6846657Snate@binkert.org 6856657Snate@binkert.org def p_expr__binary_op(self, p): 6866657Snate@binkert.org """expr : expr STAR expr 6876657Snate@binkert.org | expr SLASH expr 6886657Snate@binkert.org | expr PLUS expr 6896657Snate@binkert.org | expr DASH expr 6906657Snate@binkert.org | expr LT expr 6916657Snate@binkert.org | expr GT expr 6926657Snate@binkert.org | expr LE expr 6936657Snate@binkert.org | expr GE expr 6946657Snate@binkert.org | expr EQ expr 6956657Snate@binkert.org | expr NE expr 6966657Snate@binkert.org | expr AND expr 6976657Snate@binkert.org | expr OR expr 6986657Snate@binkert.org | expr RIGHTSHIFT expr 6996657Snate@binkert.org | expr LEFTSHIFT expr""" 7006657Snate@binkert.org p[0] = ast.InfixOperatorExprAST(self, p[1], p[2], p[3]) 7016657Snate@binkert.org 7026657Snate@binkert.org # FIXME - unary not 7036657Snate@binkert.org def p_expr__unary_op(self, p): 7046657Snate@binkert.org """expr : NOT expr 7059692Snilay@cs.wisc.edu | INCR expr 7069692Snilay@cs.wisc.edu | DECR expr 7076657Snate@binkert.org | DASH expr %prec UMINUS""" 7089692Snilay@cs.wisc.edu p[0] = ast.PrefixOperatorExprAST(self, p[1], p[2]) 7096657Snate@binkert.org 7106657Snate@binkert.org def p_expr__parens(self, p): 7116657Snate@binkert.org "aexpr : '(' expr ')'" 7126657Snate@binkert.org p[0] = p[2] 7136657Snate@binkert.org 7147839Snilay@cs.wisc.edu def p_expr__is_valid_ptr(self, p): 7157839Snilay@cs.wisc.edu "aexpr : IS_VALID '(' var ')'" 7167839Snilay@cs.wisc.edu p[0] = ast.IsValidPtrExprAST(self, p[3], True) 7177839Snilay@cs.wisc.edu 7187839Snilay@cs.wisc.edu def p_expr__is_invalid_ptr(self, p): 7197839Snilay@cs.wisc.edu "aexpr : IS_INVALID '(' var ')'" 7207839Snilay@cs.wisc.edu p[0] = ast.IsValidPtrExprAST(self, p[3], False) 7217839Snilay@cs.wisc.edu 7226657Snate@binkert.org def p_literal__string(self, p): 7236657Snate@binkert.org "literal : STRING" 7247055Snate@binkert.org p[0] = ast.LiteralExprAST(self, p[1], "std::string") 7256657Snate@binkert.org 7266657Snate@binkert.org def p_literal__number(self, p): 7276657Snate@binkert.org "literal : NUMBER" 7286657Snate@binkert.org p[0] = ast.LiteralExprAST(self, p[1], "int") 7296657Snate@binkert.org 7306657Snate@binkert.org def p_literal__float(self, p): 7316657Snate@binkert.org "literal : FLOATNUMBER" 7326657Snate@binkert.org p[0] = ast.LiteralExprAST(self, p[1], "int") 7336657Snate@binkert.org 7346657Snate@binkert.org def p_literal__bool(self, p): 7356657Snate@binkert.org "literal : LIT_BOOL" 7366657Snate@binkert.org p[0] = ast.LiteralExprAST(self, p[1], "bool") 7376657Snate@binkert.org 7386657Snate@binkert.org def p_enumeration(self, p): 7396657Snate@binkert.org "enumeration : ident ':' ident" 7406657Snate@binkert.org p[0] = ast.EnumExprAST(self, ast.TypeAST(self, p[1]), p[3]) 7416657Snate@binkert.org 7426657Snate@binkert.org def p_var(self, p): 7436657Snate@binkert.org "var : ident" 7446657Snate@binkert.org p[0] = ast.VarExprAST(self, p[1]) 745