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
310972Sdavid.hashe@amd.com# Copyright (c) 2013 Advanced Micro Devices, Inc.
46657Snate@binkert.org# All rights reserved.
56657Snate@binkert.org#
66657Snate@binkert.org# Redistribution and use in source and binary forms, with or without
76657Snate@binkert.org# modification, are permitted provided that the following conditions are
86657Snate@binkert.org# met: redistributions of source code must retain the above copyright
96657Snate@binkert.org# notice, this list of conditions and the following disclaimer;
106657Snate@binkert.org# redistributions in binary form must reproduce the above copyright
116657Snate@binkert.org# notice, this list of conditions and the following disclaimer in the
126657Snate@binkert.org# documentation and/or other materials provided with the distribution;
136657Snate@binkert.org# neither the name of the copyright holders nor the names of its
146657Snate@binkert.org# contributors may be used to endorse or promote products derived from
156657Snate@binkert.org# this software without specific prior written permission.
166657Snate@binkert.org#
176657Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186657Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196657Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206657Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216657Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226657Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236657Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246657Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256657Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266657Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276657Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286657Snate@binkert.org
296657Snate@binkert.orgfrom slicc.ast.ExprAST import ExprAST
306657Snate@binkert.orgfrom slicc.symbols import Func, Type
316657Snate@binkert.org
326657Snate@binkert.orgclass FuncCallExprAST(ExprAST):
336657Snate@binkert.org    def __init__(self, slicc, proc_name, exprs):
346657Snate@binkert.org        super(FuncCallExprAST, self).__init__(slicc)
356657Snate@binkert.org        self.proc_name = proc_name
366657Snate@binkert.org        self.exprs = exprs
376657Snate@binkert.org
386657Snate@binkert.org    def __repr__(self):
396657Snate@binkert.org        return "[FuncCallExpr: %s %s]" % (self.proc_name, self.exprs)
406657Snate@binkert.org
416657Snate@binkert.org    def generate(self, code):
426657Snate@binkert.org        machine = self.state_machine
436657Snate@binkert.org
447780Snilay@cs.wisc.edu        if self.proc_name == "DPRINTF":
457780Snilay@cs.wisc.edu            # Code for inserting the location of the DPRINTF()
467780Snilay@cs.wisc.edu            # statement in the .sm file in the statement it self.
477780Snilay@cs.wisc.edu            # 'self.exprs[0].location' represents the location.
487780Snilay@cs.wisc.edu            # 'format' represents the second argument of the
497780Snilay@cs.wisc.edu            # original DPRINTF() call. It is left unmodified.
507780Snilay@cs.wisc.edu            # str_list is used for concatenating the argument
517780Snilay@cs.wisc.edu            # list following the format specifier. A DPRINTF()
527780Snilay@cs.wisc.edu            # call may or may not contain any arguments following
537780Snilay@cs.wisc.edu            # the format specifier. These two cases need to be
547780Snilay@cs.wisc.edu            # handled differently. Hence the check whether or not
557780Snilay@cs.wisc.edu            # the str_list is empty.
567780Snilay@cs.wisc.edu
5710972Sdavid.hashe@amd.com            dflag = "%s" % (self.exprs[0].name)
5810972Sdavid.hashe@amd.com            machine.addDebugFlag(dflag)
597780Snilay@cs.wisc.edu            format = "%s" % (self.exprs[1].inline())
607780Snilay@cs.wisc.edu            format_length = len(format)
617780Snilay@cs.wisc.edu            str_list = []
627780Snilay@cs.wisc.edu
637780Snilay@cs.wisc.edu            for i in range(2, len(self.exprs)):
647780Snilay@cs.wisc.edu                str_list.append("%s" % self.exprs[i].inline())
657780Snilay@cs.wisc.edu
667780Snilay@cs.wisc.edu            if len(str_list) == 0:
6710972Sdavid.hashe@amd.com                code('DPRINTF($0, "$1: $2")',
6810972Sdavid.hashe@amd.com                     dflag, self.exprs[0].location, format[2:format_length-2])
697780Snilay@cs.wisc.edu            else:
7010972Sdavid.hashe@amd.com                code('DPRINTF($0, "$1: $2", $3)',
7110972Sdavid.hashe@amd.com                     dflag,
727780Snilay@cs.wisc.edu                     self.exprs[0].location, format[2:format_length-2],
737780Snilay@cs.wisc.edu                     ', '.join(str_list))
746657Snate@binkert.org
756657Snate@binkert.org            return self.symtab.find("void", Type)
766657Snate@binkert.org
7712662Sjohnathan.alsop@amd.com        if self.proc_name == "DPRINTFN":
7812662Sjohnathan.alsop@amd.com            format = "%s" % (self.exprs[0].inline())
7912662Sjohnathan.alsop@amd.com            format_length = len(format)
8012662Sjohnathan.alsop@amd.com            str_list = []
8112662Sjohnathan.alsop@amd.com
8212662Sjohnathan.alsop@amd.com            for i in range(1, len(self.exprs)):
8312662Sjohnathan.alsop@amd.com                str_list.append("%s" % self.exprs[i].inline())
8412662Sjohnathan.alsop@amd.com
8512662Sjohnathan.alsop@amd.com            if len(str_list) == 0:
8612662Sjohnathan.alsop@amd.com                code('DPRINTFN("$0: $1")',
8712662Sjohnathan.alsop@amd.com                     self.exprs[0].location, format[2:format_length-2])
8812662Sjohnathan.alsop@amd.com            else:
8912662Sjohnathan.alsop@amd.com                code('DPRINTFN("$0: $1", $2)',
9012662Sjohnathan.alsop@amd.com                     self.exprs[0].location, format[2:format_length-2],
9112662Sjohnathan.alsop@amd.com                     ', '.join(str_list))
9212662Sjohnathan.alsop@amd.com
9312662Sjohnathan.alsop@amd.com            return self.symtab.find("void", Type)
9412662Sjohnathan.alsop@amd.com
956657Snate@binkert.org        # hack for adding comments to profileTransition
966657Snate@binkert.org        if self.proc_name == "APPEND_TRANSITION_COMMENT":
976657Snate@binkert.org            # FIXME - check for number of parameters
986657Snate@binkert.org            code("APPEND_TRANSITION_COMMENT($0)", self.exprs[0].inline())
996657Snate@binkert.org            return self.symtab.find("void", Type)
1006657Snate@binkert.org
10110984SBrad.Beckmann@amd.com        func_name_args = self.proc_name
10210984SBrad.Beckmann@amd.com
10310984SBrad.Beckmann@amd.com        for expr in self.exprs:
10410984SBrad.Beckmann@amd.com            actual_type,param_code = expr.inline(True)
10510984SBrad.Beckmann@amd.com            func_name_args += "_" + str(actual_type.ident)
10610984SBrad.Beckmann@amd.com
1076657Snate@binkert.org        # Look up the function in the symbol table
10810984SBrad.Beckmann@amd.com        func = self.symtab.find(func_name_args, Func)
1096657Snate@binkert.org
1106657Snate@binkert.org        # Check the types and get the code for the parameters
1116657Snate@binkert.org        if func is None:
11210984SBrad.Beckmann@amd.com            self.error("Unrecognized function name: '%s'", func_name_args)
1136657Snate@binkert.org
11411062Snilay@cs.wisc.edu        cvec, type_vec = func.checkArguments(self.exprs)
1156657Snate@binkert.org
1166657Snate@binkert.org        # OK, the semantics of "trigger" here is that, ports in the
1176657Snate@binkert.org        # machine have different priorities. We always check the first
1186657Snate@binkert.org        # port for doable transitions. If nothing/stalled, we pick one
1196657Snate@binkert.org        # from the next port.
1206657Snate@binkert.org        #
1216657Snate@binkert.org        # One thing we have to be careful as the SLICC protocol
1226657Snate@binkert.org        # writter is : If a port have two or more transitions can be
1236657Snate@binkert.org        # picked from in one cycle, they must be independent.
1246657Snate@binkert.org        # Otherwise, if transition A and B mean to be executed in
1256657Snate@binkert.org        # sequential, and A get stalled, transition B can be issued
1266657Snate@binkert.org        # erroneously. In practice, in most case, there is only one
1276657Snate@binkert.org        # transition should be executed in one cycle for a given
1286657Snate@binkert.org        # port. So as most of current protocols.
1296657Snate@binkert.org
1306657Snate@binkert.org        if self.proc_name == "trigger":
1316657Snate@binkert.org            code('''
1326657Snate@binkert.org{
1337839Snilay@cs.wisc.edu''')
1347839Snilay@cs.wisc.edu            if machine.TBEType != None and machine.EntryType != None:
1357839Snilay@cs.wisc.edu                code('''
13610009Snilay@cs.wisc.edu    TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, ${{cvec[3]}}, ${{cvec[1]}});
1377839Snilay@cs.wisc.edu''')
1387839Snilay@cs.wisc.edu            elif machine.TBEType != None:
1397839Snilay@cs.wisc.edu                code('''
14010009Snilay@cs.wisc.edu    TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, ${{cvec[1]}});
1417839Snilay@cs.wisc.edu''')
1427839Snilay@cs.wisc.edu            elif machine.EntryType != None:
1437839Snilay@cs.wisc.edu                code('''
14410009Snilay@cs.wisc.edu    TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, ${{cvec[1]}});
1457839Snilay@cs.wisc.edu''')
1467839Snilay@cs.wisc.edu            else:
1477839Snilay@cs.wisc.edu                code('''
14810009Snilay@cs.wisc.edu    TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[1]}});
1497839Snilay@cs.wisc.edu''')
1506657Snate@binkert.org
1517839Snilay@cs.wisc.edu            code('''
1526657Snate@binkert.org    if (result == TransitionResult_Valid) {
1536657Snate@binkert.org        counter++;
1546657Snate@binkert.org        continue; // Check the first port again
1556657Snate@binkert.org    }
1566657Snate@binkert.org
15710981SBrad.Beckmann@amd.com    if (result == TransitionResult_ResourceStall ||
15810981SBrad.Beckmann@amd.com        result == TransitionResult_ProtocolStall) {
1599499Snilay@cs.wisc.edu        scheduleEvent(Cycles(1));
1606657Snate@binkert.org
1616657Snate@binkert.org        // Cannot do anything with this transition, go check next doable transition (mostly likely of next port)
1626657Snate@binkert.org    }
1636657Snate@binkert.org}
1646657Snate@binkert.org''')
1656657Snate@binkert.org        elif self.proc_name == "error":
1666657Snate@binkert.org            code("$0", self.exprs[0].embedError(cvec[0]))
1676657Snate@binkert.org        elif self.proc_name == "assert":
1686657Snate@binkert.org            error = self.exprs[0].embedError('"assert failure"')
1696657Snate@binkert.org            code('''
1707793SBrad.Beckmann@amd.com#ifndef NDEBUG
1717793SBrad.Beckmann@amd.comif (!(${{cvec[0]}})) {
1726657Snate@binkert.org    $error
1736657Snate@binkert.org}
1747793SBrad.Beckmann@amd.com#endif
1756657Snate@binkert.org''')
1766657Snate@binkert.org
1777839Snilay@cs.wisc.edu        elif self.proc_name == "set_cache_entry":
1787839Snilay@cs.wisc.edu            code("set_cache_entry(m_cache_entry_ptr, %s);" %(cvec[0]));
1797839Snilay@cs.wisc.edu        elif self.proc_name == "unset_cache_entry":
1807839Snilay@cs.wisc.edu            code("unset_cache_entry(m_cache_entry_ptr);");
1817839Snilay@cs.wisc.edu        elif self.proc_name == "set_tbe":
1827839Snilay@cs.wisc.edu            code("set_tbe(m_tbe_ptr, %s);" %(cvec[0]));
1837839Snilay@cs.wisc.edu        elif self.proc_name == "unset_tbe":
1847839Snilay@cs.wisc.edu            code("unset_tbe(m_tbe_ptr);");
18510981SBrad.Beckmann@amd.com        elif self.proc_name == "stallPort":
18610981SBrad.Beckmann@amd.com            code("scheduleEvent(Cycles(1));")
1877839Snilay@cs.wisc.edu
1886657Snate@binkert.org        else:
1896657Snate@binkert.org            # Normal function
1906657Snate@binkert.org            if "external" not in func and not func.isInternalMachineFunc:
1919271Snilay@cs.wisc.edu                self.error("Invalid function")
1926657Snate@binkert.org
1937839Snilay@cs.wisc.edu            params = ""
1947839Snilay@cs.wisc.edu            first_param = True
1957839Snilay@cs.wisc.edu
1967839Snilay@cs.wisc.edu            for (param_code, type) in zip(cvec, type_vec):
1978192SLisa.Hsu@amd.com                if first_param:
1988192SLisa.Hsu@amd.com                    params = str(param_code)
1998192SLisa.Hsu@amd.com                    first_param  = False
2008192SLisa.Hsu@amd.com                else:
2018192SLisa.Hsu@amd.com                    params += ', '
2028192SLisa.Hsu@amd.com                    params += str(param_code);
2037839Snilay@cs.wisc.edu
2046657Snate@binkert.org            fix = code.nofix()
20510984SBrad.Beckmann@amd.com            code('(${{func.c_name}}($params))')
2066657Snate@binkert.org            code.fix(fix)
2076657Snate@binkert.org
2086657Snate@binkert.org        return func.return_type
209