FuncCallExprAST.py revision 6657:ef5fae93a3b2
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        # DEBUG_EXPR is strange since it takes parameters of multiple types
44        if self.proc_name == "DEBUG_EXPR":
45            # FIXME - check for number of parameters
46            code('DEBUG_SLICC(MedPrio, "$0: ", $1)',
47                 self.exprs[0].location, self.exprs[0].inline())
48
49            return self.symtab.find("void", Type)
50
51        # hack for adding comments to profileTransition
52        if self.proc_name == "APPEND_TRANSITION_COMMENT":
53            # FIXME - check for number of parameters
54            code("APPEND_TRANSITION_COMMENT($0)", self.exprs[0].inline())
55            return self.symtab.find("void", Type)
56
57        # Look up the function in the symbol table
58        func = self.symtab.find(self.proc_name, Func)
59
60        # Check the types and get the code for the parameters
61        if func is None:
62            self.error("Unrecognized function name: '%s'", self.proc_name)
63
64        if len(self.exprs) != len(func.param_types):
65            self.error("Wrong number of arguments passed to function : '%s'" +\
66                       " Expected %d, got %d", self.proc_name,
67                       len(func.param_types), len(self.exprs))
68
69        cvec = []
70        for expr,expected_type in zip(self.exprs, func.param_types):
71            # Check the types of the parameter
72            actual_type,param_code = expr.inline(True)
73            if actual_type != expected_type:
74                expr.error("Type mismatch: expected: %s actual: %s" % \
75                           (expected_type, actual_type))
76            cvec.append(param_code)
77
78        # OK, the semantics of "trigger" here is that, ports in the
79        # machine have different priorities. We always check the first
80        # port for doable transitions. If nothing/stalled, we pick one
81        # from the next port.
82        #
83        # One thing we have to be careful as the SLICC protocol
84        # writter is : If a port have two or more transitions can be
85        # picked from in one cycle, they must be independent.
86        # Otherwise, if transition A and B mean to be executed in
87        # sequential, and A get stalled, transition B can be issued
88        # erroneously. In practice, in most case, there is only one
89        # transition should be executed in one cycle for a given
90        # port. So as most of current protocols.
91
92        if self.proc_name == "trigger":
93            code('''
94{
95    Address addr = ${{cvec[1]}};
96    TransitionResult result = doTransition(${{cvec[0]}}, ${machine}_getState(addr), addr);
97
98    if (result == TransitionResult_Valid) {
99        counter++;
100        continue; // Check the first port again
101    }
102
103    if (result == TransitionResult_ResourceStall) {
104        g_eventQueue_ptr->scheduleEvent(this, 1);
105
106        // Cannot do anything with this transition, go check next doable transition (mostly likely of next port)
107    }
108}
109''')
110        elif self.proc_name == "doubleTrigger":
111            # NOTE:  Use the doubleTrigger call with extreme caution
112            # the key to double trigger is the second event triggered
113            # cannot fail becuase the first event cannot be undone
114            assert len(cvec) == 4
115            code('''
116{
117    Address addr1 = ${{cvec[1]}};
118    TransitionResult result1 =
119        doTransition(${{cvec[0]}}, ${machine}_getState(addr1), addr1);
120
121    if (result1 == TransitionResult_Valid) {
122        //this second event cannont fail because the first event
123        // already took effect
124        Address addr2 = ${{cvec[3]}};
125        TransitionResult result2 = doTransition(${{cvec[2]}}, ${machine}_getState(addr2), addr2);
126
127        // ensure the event suceeded
128        assert(result2 == TransitionResult_Valid);
129
130        counter++;
131        continue; // Check the first port again
132    }
133
134    if (result1 == TransitionResult_ResourceStall) {
135        g_eventQueue_ptr->scheduleEvent(this, 1);
136        // Cannot do anything with this transition, go check next
137        // doable transition (mostly likely of next port)
138    }
139}
140''')
141        elif self.proc_name == "error":
142            code("$0", self.exprs[0].embedError(cvec[0]))
143        elif self.proc_name == "assert":
144            error = self.exprs[0].embedError('"assert failure"')
145            code('''
146if (ASSERT_FLAG && !(${{cvec[0]}})) {
147    $error
148}
149''')
150
151        elif self.proc_name == "continueProcessing":
152            code("counter++;")
153            code("continue; // Check the first port again")
154        else:
155            # Normal function
156
157            # if the func is internal to the chip but not the machine
158            # then it can only be accessed through the chip pointer
159            internal = ""
160            if "external" not in func and not func.isInternalMachineFunc:
161                internal = "m_chip_ptr->"
162
163            params = ', '.join(str(c) for c in cvec)
164            fix = code.nofix()
165            code('(${internal}${{func.c_ident}}($params))')
166            code.fix(fix)
167
168        return func.return_type
169