FuncCallExprAST.py revision 11049:dfb0aa3f0649
1# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
2# Copyright (c) 2009 The Hewlett-Packard Development Company
3# Copyright (c) 2013 Advanced Micro Devices, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met: redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer;
10# redistributions in binary form must reproduce the above copyright
11# notice, this list of conditions and the following disclaimer in the
12# documentation and/or other materials provided with the distribution;
13# neither the name of the copyright holders nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29from slicc.ast.ExprAST import ExprAST
30from slicc.symbols import Func, Type
31
32class FuncCallExprAST(ExprAST):
33    def __init__(self, slicc, proc_name, exprs):
34        super(FuncCallExprAST, self).__init__(slicc)
35        self.proc_name = proc_name
36        self.exprs = exprs
37
38    def __repr__(self):
39        return "[FuncCallExpr: %s %s]" % (self.proc_name, self.exprs)
40
41    def generate(self, code):
42        machine = self.state_machine
43
44        if self.proc_name == "DPRINTF":
45            # Code for inserting the location of the DPRINTF()
46            # statement in the .sm file in the statement it self.
47            # 'self.exprs[0].location' represents the location.
48            # 'format' represents the second argument of the
49            # original DPRINTF() call. It is left unmodified.
50            # str_list is used for concatenating the argument
51            # list following the format specifier. A DPRINTF()
52            # call may or may not contain any arguments following
53            # the format specifier. These two cases need to be
54            # handled differently. Hence the check whether or not
55            # the str_list is empty.
56
57            dflag = "%s" % (self.exprs[0].name)
58            machine.addDebugFlag(dflag)
59            format = "%s" % (self.exprs[1].inline())
60            format_length = len(format)
61            str_list = []
62
63            for i in range(2, len(self.exprs)):
64                str_list.append("%s" % self.exprs[i].inline())
65
66            if len(str_list) == 0:
67                code('DPRINTF($0, "$1: $2")',
68                     dflag, self.exprs[0].location, format[2:format_length-2])
69            else:
70                code('DPRINTF($0, "$1: $2", $3)',
71                     dflag,
72                     self.exprs[0].location, format[2:format_length-2],
73                     ', '.join(str_list))
74
75            return self.symtab.find("void", Type)
76
77        # hack for adding comments to profileTransition
78        if self.proc_name == "APPEND_TRANSITION_COMMENT":
79            # FIXME - check for number of parameters
80            code("APPEND_TRANSITION_COMMENT($0)", self.exprs[0].inline())
81            return self.symtab.find("void", Type)
82
83        func_name_args = self.proc_name
84
85        for expr in self.exprs:
86            actual_type,param_code = expr.inline(True)
87            func_name_args += "_" + str(actual_type.ident)
88
89        # Look up the function in the symbol table
90        func = self.symtab.find(func_name_args, Func)
91
92        # Check the types and get the code for the parameters
93        if func is None:
94            self.error("Unrecognized function name: '%s'", func_name_args)
95
96        if len(self.exprs) != len(func.param_types):
97            self.error("Wrong number of arguments passed to function : '%s'" +\
98                       " Expected %d, got %d", self.proc_name,
99                       len(func.param_types), len(self.exprs))
100
101        cvec = []
102        type_vec = []
103        for expr,expected_type in zip(self.exprs, func.param_types):
104            # Check the types of the parameter
105            actual_type,param_code = expr.inline(True)
106            if str(actual_type) != 'OOD' and \
107            str(actual_type) != str(expected_type):
108                expr.error("Type mismatch: expected: %s actual: %s" % \
109                           (expected_type, actual_type))
110            cvec.append(param_code)
111            type_vec.append(expected_type)
112
113        # OK, the semantics of "trigger" here is that, ports in the
114        # machine have different priorities. We always check the first
115        # port for doable transitions. If nothing/stalled, we pick one
116        # from the next port.
117        #
118        # One thing we have to be careful as the SLICC protocol
119        # writter is : If a port have two or more transitions can be
120        # picked from in one cycle, they must be independent.
121        # Otherwise, if transition A and B mean to be executed in
122        # sequential, and A get stalled, transition B can be issued
123        # erroneously. In practice, in most case, there is only one
124        # transition should be executed in one cycle for a given
125        # port. So as most of current protocols.
126
127        if self.proc_name == "trigger":
128            code('''
129{
130''')
131            if machine.TBEType != None and machine.EntryType != None:
132                code('''
133    TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, ${{cvec[3]}}, ${{cvec[1]}});
134''')
135            elif machine.TBEType != None:
136                code('''
137    TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, ${{cvec[1]}});
138''')
139            elif machine.EntryType != None:
140                code('''
141    TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, ${{cvec[1]}});
142''')
143            else:
144                code('''
145    TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[1]}});
146''')
147
148            code('''
149    if (result == TransitionResult_Valid) {
150        counter++;
151        continue; // Check the first port again
152    }
153
154    if (result == TransitionResult_ResourceStall ||
155        result == TransitionResult_ProtocolStall) {
156        scheduleEvent(Cycles(1));
157
158        // Cannot do anything with this transition, go check next doable transition (mostly likely of next port)
159    }
160}
161''')
162        elif self.proc_name == "error":
163            code("$0", self.exprs[0].embedError(cvec[0]))
164        elif self.proc_name == "assert":
165            error = self.exprs[0].embedError('"assert failure"')
166            code('''
167#ifndef NDEBUG
168if (!(${{cvec[0]}})) {
169    $error
170}
171#endif
172''')
173
174        elif self.proc_name == "set_cache_entry":
175            code("set_cache_entry(m_cache_entry_ptr, %s);" %(cvec[0]));
176        elif self.proc_name == "unset_cache_entry":
177            code("unset_cache_entry(m_cache_entry_ptr);");
178        elif self.proc_name == "set_tbe":
179            code("set_tbe(m_tbe_ptr, %s);" %(cvec[0]));
180        elif self.proc_name == "unset_tbe":
181            code("unset_tbe(m_tbe_ptr);");
182        elif self.proc_name == "stallPort":
183            code("scheduleEvent(Cycles(1));")
184
185        else:
186            # Normal function
187            if "external" not in func and not func.isInternalMachineFunc:
188                self.error("Invalid function")
189
190            params = ""
191            first_param = True
192
193            for (param_code, type) in zip(cvec, type_vec):
194                if first_param:
195                    params = str(param_code)
196                    first_param  = False
197                else:
198                    params += ', '
199                    params += str(param_code);
200
201            fix = code.nofix()
202            code('(${{func.c_name}}($params))')
203            code.fix(fix)
204
205        return func.return_type
206