FuncCallExprAST.py (7793:f6cbeb8712d3) FuncCallExprAST.py (7839:9e556fb25900)
1# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
2# Copyright (c) 2009 The Hewlett-Packard Development Company
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
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:
64 code('DPRINTF(RubySlicc, "$0: $1")',
65 self.exprs[0].location, format[2:format_length-2])
66 else:
67 code('DPRINTF(RubySlicc, "$0: $1", $2)',
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 = []
1# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
2# Copyright (c) 2009 The Hewlett-Packard Development Company
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
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:
64 code('DPRINTF(RubySlicc, "$0: $1")',
65 self.exprs[0].location, format[2:format_length-2])
66 else:
67 code('DPRINTF(RubySlicc, "$0: $1", $2)',
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 = []
92 for expr,expected_type in zip(self.exprs, func.param_types):
93 # Check the types of the parameter
94 actual_type,param_code = expr.inline(True)
95 if actual_type != expected_type:
96 expr.error("Type mismatch: expected: %s actual: %s" % \
97 (expected_type, actual_type))
98 cvec.append(param_code)
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 actual_type != expected_type:
97 expr.error("Type mismatch: expected: %s actual: %s" % \
98 (expected_type, actual_type))
99 cvec.append(param_code)
100 type_vec.append(expected_type)
99
100 # OK, the semantics of "trigger" here is that, ports in the
101 # machine have different priorities. We always check the first
102 # port for doable transitions. If nothing/stalled, we pick one
103 # from the next port.
104 #
105 # One thing we have to be careful as the SLICC protocol
106 # writter is : If a port have two or more transitions can be
107 # picked from in one cycle, they must be independent.
108 # Otherwise, if transition A and B mean to be executed in
109 # sequential, and A get stalled, transition B can be issued
110 # erroneously. In practice, in most case, there is only one
111 # transition should be executed in one cycle for a given
112 # port. So as most of current protocols.
113
114 if self.proc_name == "trigger":
115 code('''
116{
117 Address addr = ${{cvec[1]}};
101
102 # OK, the semantics of "trigger" here is that, ports in the
103 # machine have different priorities. We always check the first
104 # port for doable transitions. If nothing/stalled, we pick one
105 # from the next port.
106 #
107 # One thing we have to be careful as the SLICC protocol
108 # writter is : If a port have two or more transitions can be
109 # picked from in one cycle, they must be independent.
110 # Otherwise, if transition A and B mean to be executed in
111 # sequential, and A get stalled, transition B can be issued
112 # erroneously. In practice, in most case, there is only one
113 # transition should be executed in one cycle for a given
114 # port. So as most of current protocols.
115
116 if self.proc_name == "trigger":
117 code('''
118{
119 Address addr = ${{cvec[1]}};
118 TransitionResult result = doTransition(${{cvec[0]}}, ${machine}_getState(addr), addr);
120''')
121 if machine.TBEType != None and machine.EntryType != None:
122 code('''
123 TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, ${{cvec[3]}}, addr);
124''')
125 elif machine.TBEType != None:
126 code('''
127 TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, addr);
128''')
129 elif machine.EntryType != None:
130 code('''
131 TransitionResult result = doTransition(${{cvec[0]}}, ${{cvec[2]}}, addr);
132''')
133 else:
134 code('''
135 TransitionResult result = doTransition(${{cvec[0]}}, addr);
136''')
119
137
138 code('''
120 if (result == TransitionResult_Valid) {
121 counter++;
122 continue; // Check the first port again
123 }
124
125 if (result == TransitionResult_ResourceStall) {
126 g_eventQueue_ptr->scheduleEvent(this, 1);
127
128 // Cannot do anything with this transition, go check next doable transition (mostly likely of next port)
129 }
130}
131''')
132 elif self.proc_name == "doubleTrigger":
133 # NOTE: Use the doubleTrigger call with extreme caution
134 # the key to double trigger is the second event triggered
135 # cannot fail becuase the first event cannot be undone
136 assert len(cvec) == 4
137 code('''
138{
139 Address addr1 = ${{cvec[1]}};
140 TransitionResult result1 =
141 doTransition(${{cvec[0]}}, ${machine}_getState(addr1), addr1);
142
143 if (result1 == TransitionResult_Valid) {
144 //this second event cannont fail because the first event
145 // already took effect
146 Address addr2 = ${{cvec[3]}};
147 TransitionResult result2 = doTransition(${{cvec[2]}}, ${machine}_getState(addr2), addr2);
148
149 // ensure the event suceeded
150 assert(result2 == TransitionResult_Valid);
151
152 counter++;
153 continue; // Check the first port again
154 }
155
156 if (result1 == TransitionResult_ResourceStall) {
157 g_eventQueue_ptr->scheduleEvent(this, 1);
158 // Cannot do anything with this transition, go check next
159 // doable transition (mostly likely of next port)
160 }
161}
162''')
163 elif self.proc_name == "error":
164 code("$0", self.exprs[0].embedError(cvec[0]))
165 elif self.proc_name == "assert":
166 error = self.exprs[0].embedError('"assert failure"')
167 code('''
168#ifndef NDEBUG
169if (!(${{cvec[0]}})) {
170 $error
171}
172#endif
173''')
174
175 elif self.proc_name == "continueProcessing":
176 code("counter++;")
177 code("continue; // Check the first port again")
139 if (result == TransitionResult_Valid) {
140 counter++;
141 continue; // Check the first port again
142 }
143
144 if (result == TransitionResult_ResourceStall) {
145 g_eventQueue_ptr->scheduleEvent(this, 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 == "doubleTrigger":
152 # NOTE: Use the doubleTrigger call with extreme caution
153 # the key to double trigger is the second event triggered
154 # cannot fail becuase the first event cannot be undone
155 assert len(cvec) == 4
156 code('''
157{
158 Address addr1 = ${{cvec[1]}};
159 TransitionResult result1 =
160 doTransition(${{cvec[0]}}, ${machine}_getState(addr1), addr1);
161
162 if (result1 == TransitionResult_Valid) {
163 //this second event cannont fail because the first event
164 // already took effect
165 Address addr2 = ${{cvec[3]}};
166 TransitionResult result2 = doTransition(${{cvec[2]}}, ${machine}_getState(addr2), addr2);
167
168 // ensure the event suceeded
169 assert(result2 == TransitionResult_Valid);
170
171 counter++;
172 continue; // Check the first port again
173 }
174
175 if (result1 == TransitionResult_ResourceStall) {
176 g_eventQueue_ptr->scheduleEvent(this, 1);
177 // Cannot do anything with this transition, go check next
178 // doable transition (mostly likely of next port)
179 }
180}
181''')
182 elif self.proc_name == "error":
183 code("$0", self.exprs[0].embedError(cvec[0]))
184 elif self.proc_name == "assert":
185 error = self.exprs[0].embedError('"assert failure"')
186 code('''
187#ifndef NDEBUG
188if (!(${{cvec[0]}})) {
189 $error
190}
191#endif
192''')
193
194 elif self.proc_name == "continueProcessing":
195 code("counter++;")
196 code("continue; // Check the first port again")
197
198 elif self.proc_name == "set_cache_entry":
199 code("set_cache_entry(m_cache_entry_ptr, %s);" %(cvec[0]));
200 elif self.proc_name == "unset_cache_entry":
201 code("unset_cache_entry(m_cache_entry_ptr);");
202 elif self.proc_name == "set_tbe":
203 code("set_tbe(m_tbe_ptr, %s);" %(cvec[0]));
204 elif self.proc_name == "unset_tbe":
205 code("unset_tbe(m_tbe_ptr);");
206
178 else:
179 # Normal function
180
181 # if the func is internal to the chip but not the machine
182 # then it can only be accessed through the chip pointer
183 internal = ""
184 if "external" not in func and not func.isInternalMachineFunc:
185 internal = "m_chip_ptr->"
186
207 else:
208 # Normal function
209
210 # if the func is internal to the chip but not the machine
211 # then it can only be accessed through the chip pointer
212 internal = ""
213 if "external" not in func and not func.isInternalMachineFunc:
214 internal = "m_chip_ptr->"
215
187 params = ', '.join(str(c) for c in cvec)
216 params = ""
217 first_param = True
218
219 for (param_code, type) in zip(cvec, type_vec):
220 if str(type) == "TBE" or ("interface" in type and
221 type["interface"] == "AbstractCacheEntry"):
222
223 if first_param:
224 params = str(param_code).replace('*','')
225 first_param = False
226 else:
227 params += ', '
228 params += str(param_code).replace('*','');
229 else:
230 if first_param:
231 params = str(param_code)
232 first_param = False
233 else:
234 params += ', '
235 params += str(param_code);
236
188 fix = code.nofix()
189 code('(${internal}${{func.c_ident}}($params))')
190 code.fix(fix)
191
192 return func.return_type
237 fix = code.nofix()
238 code('(${internal}${{func.c_ident}}($params))')
239 code.fix(fix)
240
241 return func.return_type