InPortDeclAST.py revision 7567:238f99c9f441
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.DeclAST import DeclAST
29from slicc.ast.TypeAST import TypeAST
30from slicc.symbols import Func, Type, Var
31
32class InPortDeclAST(DeclAST):
33    max_port_rank = 0
34
35    def __init__(self, slicc, ident, msg_type, var_expr, pairs, statements):
36        super(InPortDeclAST, self).__init__(slicc, pairs)
37
38        self.ident = ident
39        self.msg_type = msg_type
40        self.var_expr = var_expr
41        self.statements = statements
42        self.queue_type = TypeAST(slicc, "InPort")
43        if self.pairs.has_key("rank"):
44            InPortDeclAST.max_port_rank = max(self.pairs["rank"],
45                                              InPortDeclAST.max_port_rank)
46
47    def __repr__(self):
48        return "[InPortDecl: %s]" % self.ident
49
50    def generate(self):
51        symtab = self.symtab
52        void_type = symtab.find("void", Type)
53
54        code = self.slicc.codeFormatter()
55        queue_type = self.var_expr.generate(code)
56        if not queue_type.isInPort:
57            self.error("The inport queue's type must have the 'inport' " + \
58                       "attribute.  Type '%s' does not have this attribute.",
59                       queue_type)
60
61        type = self.queue_type.type
62        in_port = Var(self.symtab, self.ident, self.location, type, str(code),
63                      self.pairs)
64        symtab.newSymbol(in_port)
65
66        symtab.pushFrame()
67        param_types = []
68
69        # Check for Event
70        type = symtab.find("Event", Type)
71        if type is None:
72            self.error("in_port decls require 'Event' enumeration defined")
73        param_types.append(type)
74
75        # Check for Address
76        type = symtab.find("Address", Type)
77        if type is None:
78            self.error("in_port decls require 'Address' type to be defined")
79
80        param_types.append(type)
81
82        # Add the trigger method - FIXME, this is a bit dirty
83        pairs = { "external" : "yes" }
84        func = Func(self.symtab, "trigger", self.location, void_type,
85                    param_types, [], "", pairs, None)
86        symtab.newSymbol(func)
87
88        param_types = []
89        # Check for Event2
90        type = symtab.find("Event", Type)
91        if type is None:
92            self.error("in_port decls require 'Event' enumeration")
93
94        param_types.append(type)
95
96        # Check for Address2
97        type = symtab.find("Address", Type)
98        if type is None:
99            self.error("in_port decls require 'Address' type to be defined")
100
101        param_types.append(type)
102
103        # Add the doubleTrigger method - this hack supports tiggering
104        # two simulateous events
105        #
106        # The key is that the second transistion cannot fail because
107        # the first event cannot be undone therefore you must do some
108        # checks before calling double trigger to ensure that won't
109        # happen
110        func = Func(self.symtab, "doubleTrigger", self.location, void_type,
111                    param_types, [], "", pairs, None)
112        symtab.newSymbol(func)
113
114        # Add the continueProcessing method - this hack supports
115        # messages that don't trigger events
116        func = Func(self.symtab, "continueProcessing", self.location,
117                    void_type, [], [], "", pairs, None)
118        symtab.newSymbol(func)
119
120        if self.statements is not None:
121            rcode = self.slicc.codeFormatter()
122            rcode.indent()
123            rcode.indent()
124            self.statements.generate(rcode, None)
125            in_port["c_code_in_port"] = str(rcode)
126        symtab.popFrame()
127
128        # Add port to state machine
129        machine = symtab.state_machine
130        if machine is None:
131            self.error("InPort declaration not part of a machine.")
132
133        machine.addInPort(in_port)
134
135        # Include max_rank to be used by StateMachine.py
136        in_port["max_port_rank"] = InPortDeclAST.max_port_rank
137