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    def __init__(self, slicc, ident, msg_type, var_expr, pairs, statements):
34        super(InPortDeclAST, self).__init__(slicc, pairs)
35
36        self.ident = ident
37        self.msg_type = msg_type
38        self.var_expr = var_expr
39        self.statements = statements
40        self.queue_type = TypeAST(slicc, "InPort")
41
42    def __repr__(self):
43        return "[InPortDecl: %s]" % self.ident
44
45    def generate(self):
46        symtab = self.symtab
47        void_type = symtab.find("void", Type)
48
49        machine = symtab.state_machine
50        if machine is None:
51            self.error("InPort declaration not part of a machine.")
52
53        code = self.slicc.codeFormatter()
54        queue_type = self.var_expr.generate(code)
55        if not queue_type.isInPort:
56            self.error("The inport queue's type must have the 'inport' " + \
57                       "attribute.  Type '%s' does not have this attribute.",
58                       queue_type)
59
60        type = self.queue_type.type
61        self.pairs["buffer_expr"] = self.var_expr
62        in_port = Var(self.symtab, self.ident, self.location, type, str(code),
63                      self.pairs, machine)
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("Addr", Type)
77        if type is None:
78            self.error("in_port decls require 'Addr' type to be defined")
79
80        param_types.append(type)
81
82        if machine.EntryType != None:
83            param_types.append(machine.EntryType)
84        if machine.TBEType != None:
85            param_types.append(machine.TBEType)
86
87        # Add the trigger method - FIXME, this is a bit dirty
88        pairs = { "external" : "yes" }
89        trigger_func_name = "trigger"
90        for param in param_types:
91            trigger_func_name += "_" + param.ident
92        func = Func(self.symtab, trigger_func_name, "trigger", self.location,
93                    void_type, param_types, [], "", pairs)
94        symtab.newSymbol(func)
95
96        # Add the stallPort method - this hacks reschedules the controller
97        # for stalled messages that don't trigger events
98        func = Func(self.symtab, "stallPort", "stallPort", self.location,
99                    void_type, [], [], "", pairs)
100        symtab.newSymbol(func)
101
102        param_types = []
103        # Check for Event2
104        type = symtab.find("Event", Type)
105        if type is None:
106            self.error("in_port decls require 'Event' enumeration")
107
108        param_types.append(type)
109
110        # Check for Address2
111        type = symtab.find("Addr", Type)
112        if type is None:
113            self.error("in_port decls require 'Addr' type to be defined")
114
115        param_types.append(type)
116
117        if self.statements is not None:
118            rcode = self.slicc.codeFormatter()
119            rcode.indent()
120            rcode.indent()
121            self.statements.generate(rcode, None)
122            in_port["c_code_in_port"] = str(rcode)
123
124        symtab.popFrame()
125
126        # Add port to state machine
127        machine.addInPort(in_port)
128