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