FuncCallExprAST.py revision 8192
16657Snate@binkert.org# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 26657Snate@binkert.org# Copyright (c) 2009 The Hewlett-Packard Development Company 36657Snate@binkert.org# All rights reserved. 46657Snate@binkert.org# 56657Snate@binkert.org# Redistribution and use in source and binary forms, with or without 66657Snate@binkert.org# modification, are permitted provided that the following conditions are 76657Snate@binkert.org# met: redistributions of source code must retain the above copyright 86657Snate@binkert.org# notice, this list of conditions and the following disclaimer; 96657Snate@binkert.org# redistributions in binary form must reproduce the above copyright 106657Snate@binkert.org# notice, this list of conditions and the following disclaimer in the 116657Snate@binkert.org# documentation and/or other materials provided with the distribution; 126657Snate@binkert.org# neither the name of the copyright holders nor the names of its 136657Snate@binkert.org# contributors may be used to endorse or promote products derived from 146657Snate@binkert.org# this software without specific prior written permission. 156657Snate@binkert.org# 166657Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 176657Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 186657Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 196657Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 206657Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 216657Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 226657Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 236657Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 246657Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 256657Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 266657Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 276657Snate@binkert.org 286657Snate@binkert.orgfrom slicc.ast.ExprAST import ExprAST 296657Snate@binkert.orgfrom slicc.symbols import Func, Type 306657Snate@binkert.org 316657Snate@binkert.orgclass FuncCallExprAST(ExprAST): 326657Snate@binkert.org def __init__(self, slicc, proc_name, exprs): 336657Snate@binkert.org super(FuncCallExprAST, self).__init__(slicc) 346657Snate@binkert.org self.proc_name = proc_name 356657Snate@binkert.org self.exprs = exprs 366657Snate@binkert.org 376657Snate@binkert.org def __repr__(self): 386657Snate@binkert.org return "[FuncCallExpr: %s %s]" % (self.proc_name, self.exprs) 396657Snate@binkert.org 406657Snate@binkert.org def generate(self, code): 416657Snate@binkert.org machine = self.state_machine 426657Snate@binkert.org 437780Snilay@cs.wisc.edu if self.proc_name == "DPRINTF": 447780Snilay@cs.wisc.edu # Code for inserting the location of the DPRINTF() 457780Snilay@cs.wisc.edu # statement in the .sm file in the statement it self. 467780Snilay@cs.wisc.edu # 'self.exprs[0].location' represents the location. 477780Snilay@cs.wisc.edu # 'format' represents the second argument of the 487780Snilay@cs.wisc.edu # original DPRINTF() call. It is left unmodified. 497780Snilay@cs.wisc.edu # str_list is used for concatenating the argument 507780Snilay@cs.wisc.edu # list following the format specifier. A DPRINTF() 517780Snilay@cs.wisc.edu # call may or may not contain any arguments following 527780Snilay@cs.wisc.edu # the format specifier. These two cases need to be 537780Snilay@cs.wisc.edu # handled differently. Hence the check whether or not 547780Snilay@cs.wisc.edu # the str_list is empty. 557780Snilay@cs.wisc.edu 567780Snilay@cs.wisc.edu format = "%s" % (self.exprs[1].inline()) 577780Snilay@cs.wisc.edu format_length = len(format) 587780Snilay@cs.wisc.edu str_list = [] 597780Snilay@cs.wisc.edu 607780Snilay@cs.wisc.edu for i in range(2, len(self.exprs)): 617780Snilay@cs.wisc.edu str_list.append("%s" % self.exprs[i].inline()) 627780Snilay@cs.wisc.edu 637780Snilay@cs.wisc.edu if len(str_list) == 0: 647780Snilay@cs.wisc.edu code('DPRINTF(RubySlicc, "$0: $1")', 657780Snilay@cs.wisc.edu self.exprs[0].location, format[2:format_length-2]) 667780Snilay@cs.wisc.edu else: 677780Snilay@cs.wisc.edu code('DPRINTF(RubySlicc, "$0: $1", $2)', 687780Snilay@cs.wisc.edu self.exprs[0].location, format[2:format_length-2], 697780Snilay@cs.wisc.edu ', '.join(str_list)) 706657Snate@binkert.org 716657Snate@binkert.org return self.symtab.find("void", Type) 726657Snate@binkert.org 736657Snate@binkert.org # hack for adding comments to profileTransition 746657Snate@binkert.org if self.proc_name == "APPEND_TRANSITION_COMMENT": 756657Snate@binkert.org # FIXME - check for number of parameters 766657Snate@binkert.org code("APPEND_TRANSITION_COMMENT($0)", self.exprs[0].inline()) 776657Snate@binkert.org return self.symtab.find("void", Type) 786657Snate@binkert.org 796657Snate@binkert.org # Look up the function in the symbol table 806657Snate@binkert.org func = self.symtab.find(self.proc_name, Func) 816657Snate@binkert.org 826657Snate@binkert.org # Check the types and get the code for the parameters 836657Snate@binkert.org if func is None: 846657Snate@binkert.org self.error("Unrecognized function name: '%s'", self.proc_name) 856657Snate@binkert.org 866657Snate@binkert.org if len(self.exprs) != len(func.param_types): 876657Snate@binkert.org self.error("Wrong number of arguments passed to function : '%s'" +\ 886657Snate@binkert.org " Expected %d, got %d", self.proc_name, 896657Snate@binkert.org len(func.param_types), len(self.exprs)) 906657Snate@binkert.org 916657Snate@binkert.org cvec = [] 927839Snilay@cs.wisc.edu type_vec = [] 936657Snate@binkert.org for expr,expected_type in zip(self.exprs, func.param_types): 946657Snate@binkert.org # Check the types of the parameter 956657Snate@binkert.org actual_type,param_code = expr.inline(True) 966657Snate@binkert.org if actual_type != expected_type: 976657Snate@binkert.org expr.error("Type mismatch: expected: %s actual: %s" % \ 986657Snate@binkert.org (expected_type, actual_type)) 996657Snate@binkert.org cvec.append(param_code) 1007839Snilay@cs.wisc.edu type_vec.append(expected_type) 1016657Snate@binkert.org 1026657Snate@binkert.org # OK, the semantics of "trigger" here is that, ports in the 1036657Snate@binkert.org # machine have different priorities. We always check the first 1046657Snate@binkert.org # port for doable transitions. If nothing/stalled, we pick one 1056657Snate@binkert.org # from the next port. 1066657Snate@binkert.org # 1076657Snate@binkert.org # One thing we have to be careful as the SLICC protocol 1086657Snate@binkert.org # writter is : If a port have two or more transitions can be 1096657Snate@binkert.org # picked from in one cycle, they must be independent. 1106657Snate@binkert.org # Otherwise, if transition A and B mean to be executed in 1116657Snate@binkert.org # sequential, and A get stalled, transition B can be issued 1126657Snate@binkert.org # erroneously. In practice, in most case, there is only one 1136657Snate@binkert.org # transition should be executed in one cycle for a given 1146657Snate@binkert.org # port. So as most of current protocols. 1156657Snate@binkert.org 1166657Snate@binkert.org if self.proc_name == "trigger": 1176657Snate@binkert.org code(''' 1186657Snate@binkert.org{ 1196657Snate@binkert.org Address addr = ${{cvec[1]}}; 1207839Snilay@cs.wisc.edu''') 1217839Snilay@cs.wisc.edu if machine.TBEType != None and machine.EntryType != None: 1227839Snilay@cs.wisc.edu code(''' 1237839Snilay@cs.wisc.edu TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, ${{cvec[3]}}, addr); 1247839Snilay@cs.wisc.edu''') 1257839Snilay@cs.wisc.edu elif machine.TBEType != None: 1267839Snilay@cs.wisc.edu code(''' 1277839Snilay@cs.wisc.edu TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, addr); 1287839Snilay@cs.wisc.edu''') 1297839Snilay@cs.wisc.edu elif machine.EntryType != None: 1307839Snilay@cs.wisc.edu code(''' 1317839Snilay@cs.wisc.edu TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, addr); 1327839Snilay@cs.wisc.edu''') 1337839Snilay@cs.wisc.edu else: 1347839Snilay@cs.wisc.edu code(''' 1357839Snilay@cs.wisc.edu TransitionResult result = doTransition(${{cvec[0]}}, addr); 1367839Snilay@cs.wisc.edu''') 1376657Snate@binkert.org 1387839Snilay@cs.wisc.edu code(''' 1396657Snate@binkert.org if (result == TransitionResult_Valid) { 1406657Snate@binkert.org counter++; 1416657Snate@binkert.org continue; // Check the first port again 1426657Snate@binkert.org } 1436657Snate@binkert.org 1446657Snate@binkert.org if (result == TransitionResult_ResourceStall) { 1456657Snate@binkert.org g_eventQueue_ptr->scheduleEvent(this, 1); 1466657Snate@binkert.org 1476657Snate@binkert.org // Cannot do anything with this transition, go check next doable transition (mostly likely of next port) 1486657Snate@binkert.org } 1496657Snate@binkert.org} 1506657Snate@binkert.org''') 1516657Snate@binkert.org elif self.proc_name == "doubleTrigger": 1526657Snate@binkert.org # NOTE: Use the doubleTrigger call with extreme caution 1536657Snate@binkert.org # the key to double trigger is the second event triggered 1546657Snate@binkert.org # cannot fail becuase the first event cannot be undone 1556657Snate@binkert.org assert len(cvec) == 4 1566657Snate@binkert.org code(''' 1576657Snate@binkert.org{ 1586657Snate@binkert.org Address addr1 = ${{cvec[1]}}; 1596657Snate@binkert.org TransitionResult result1 = 1606657Snate@binkert.org doTransition(${{cvec[0]}}, ${machine}_getState(addr1), addr1); 1616657Snate@binkert.org 1626657Snate@binkert.org if (result1 == TransitionResult_Valid) { 1636657Snate@binkert.org //this second event cannont fail because the first event 1646657Snate@binkert.org // already took effect 1656657Snate@binkert.org Address addr2 = ${{cvec[3]}}; 1666657Snate@binkert.org TransitionResult result2 = doTransition(${{cvec[2]}}, ${machine}_getState(addr2), addr2); 1676657Snate@binkert.org 1686657Snate@binkert.org // ensure the event suceeded 1696657Snate@binkert.org assert(result2 == TransitionResult_Valid); 1706657Snate@binkert.org 1716657Snate@binkert.org counter++; 1726657Snate@binkert.org continue; // Check the first port again 1736657Snate@binkert.org } 1746657Snate@binkert.org 1756657Snate@binkert.org if (result1 == TransitionResult_ResourceStall) { 1766657Snate@binkert.org g_eventQueue_ptr->scheduleEvent(this, 1); 1776657Snate@binkert.org // Cannot do anything with this transition, go check next 1786657Snate@binkert.org // doable transition (mostly likely of next port) 1796657Snate@binkert.org } 1806657Snate@binkert.org} 1816657Snate@binkert.org''') 1826657Snate@binkert.org elif self.proc_name == "error": 1836657Snate@binkert.org code("$0", self.exprs[0].embedError(cvec[0])) 1846657Snate@binkert.org elif self.proc_name == "assert": 1856657Snate@binkert.org error = self.exprs[0].embedError('"assert failure"') 1866657Snate@binkert.org code(''' 1877793SBrad.Beckmann@amd.com#ifndef NDEBUG 1887793SBrad.Beckmann@amd.comif (!(${{cvec[0]}})) { 1896657Snate@binkert.org $error 1906657Snate@binkert.org} 1917793SBrad.Beckmann@amd.com#endif 1926657Snate@binkert.org''') 1936657Snate@binkert.org 1946657Snate@binkert.org elif self.proc_name == "continueProcessing": 1956657Snate@binkert.org code("counter++;") 1966657Snate@binkert.org code("continue; // Check the first port again") 1977839Snilay@cs.wisc.edu 1987839Snilay@cs.wisc.edu elif self.proc_name == "set_cache_entry": 1997839Snilay@cs.wisc.edu code("set_cache_entry(m_cache_entry_ptr, %s);" %(cvec[0])); 2007839Snilay@cs.wisc.edu elif self.proc_name == "unset_cache_entry": 2017839Snilay@cs.wisc.edu code("unset_cache_entry(m_cache_entry_ptr);"); 2027839Snilay@cs.wisc.edu elif self.proc_name == "set_tbe": 2037839Snilay@cs.wisc.edu code("set_tbe(m_tbe_ptr, %s);" %(cvec[0])); 2047839Snilay@cs.wisc.edu elif self.proc_name == "unset_tbe": 2057839Snilay@cs.wisc.edu code("unset_tbe(m_tbe_ptr);"); 2067839Snilay@cs.wisc.edu 2076657Snate@binkert.org else: 2086657Snate@binkert.org # Normal function 2096657Snate@binkert.org 2106657Snate@binkert.org # if the func is internal to the chip but not the machine 2116657Snate@binkert.org # then it can only be accessed through the chip pointer 2126657Snate@binkert.org internal = "" 2136657Snate@binkert.org if "external" not in func and not func.isInternalMachineFunc: 2146657Snate@binkert.org internal = "m_chip_ptr->" 2156657Snate@binkert.org 2167839Snilay@cs.wisc.edu params = "" 2177839Snilay@cs.wisc.edu first_param = True 2187839Snilay@cs.wisc.edu 2197839Snilay@cs.wisc.edu for (param_code, type) in zip(cvec, type_vec): 2208192SLisa.Hsu@amd.com if first_param: 2218192SLisa.Hsu@amd.com params = str(param_code) 2228192SLisa.Hsu@amd.com first_param = False 2238192SLisa.Hsu@amd.com else: 2248192SLisa.Hsu@amd.com params += ', ' 2258192SLisa.Hsu@amd.com params += str(param_code); 2267839Snilay@cs.wisc.edu 2276657Snate@binkert.org fix = code.nofix() 2286657Snate@binkert.org code('(${internal}${{func.c_ident}}($params))') 2296657Snate@binkert.org code.fix(fix) 2306657Snate@binkert.org 2316657Snate@binkert.org return func.return_type 232