InPortDeclAST.py (9820:2f9aecba2362) InPortDeclAST.py (9996:150338b8ba12)
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):
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")
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")
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 machine = symtab.state_machine
55 if machine is None:
56 self.error("InPort declaration not part of a machine.")
57
58 code = self.slicc.codeFormatter()
59 queue_type = self.var_expr.generate(code)
60 if not queue_type.isInPort:
61 self.error("The inport queue's type must have the 'inport' " + \
62 "attribute. Type '%s' does not have this attribute.",
63 queue_type)
64
65 type = self.queue_type.type
66 in_port = Var(self.symtab, self.ident, self.location, type, str(code),
67 self.pairs)
68 symtab.newSymbol(in_port)
69
70 symtab.pushFrame()
71 param_types = []
72
73 # Check for Event
74 type = symtab.find("Event", Type)
75 if type is None:
76 self.error("in_port decls require 'Event' enumeration defined")
77 param_types.append(type)
78
79 # Check for Address
80 type = symtab.find("Address", Type)
81 if type is None:
82 self.error("in_port decls require 'Address' type to be defined")
83
84 param_types.append(type)
85
86 if machine.EntryType != None:
87 param_types.append(machine.EntryType)
88 if machine.TBEType != None:
89 param_types.append(machine.TBEType)
90
91 # Add the trigger method - FIXME, this is a bit dirty
92 pairs = { "external" : "yes" }
93 func = Func(self.symtab, "trigger", self.location, void_type,
94 param_types, [], "", pairs)
95 symtab.newSymbol(func)
96
97 param_types = []
98 # Check for Event2
99 type = symtab.find("Event", Type)
100 if type is None:
101 self.error("in_port decls require 'Event' enumeration")
102
103 param_types.append(type)
104
105 # Check for Address2
106 type = symtab.find("Address", Type)
107 if type is None:
108 self.error("in_port decls require 'Address' type to be defined")
109
110 param_types.append(type)
111
112 if self.statements is not None:
113 rcode = self.slicc.codeFormatter()
114 rcode.indent()
115 rcode.indent()
116 self.statements.generate(rcode, None)
117 in_port["c_code_in_port"] = str(rcode)
118
119 symtab.popFrame()
120
121 # Add port to state machine
122 machine.addInPort(in_port)
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 in_port = Var(self.symtab, self.ident, self.location, type, str(code),
62 self.pairs)
63 symtab.newSymbol(in_port)
64
65 symtab.pushFrame()
66 param_types = []
67
68 # Check for Event
69 type = symtab.find("Event", Type)
70 if type is None:
71 self.error("in_port decls require 'Event' enumeration defined")
72 param_types.append(type)
73
74 # Check for Address
75 type = symtab.find("Address", Type)
76 if type is None:
77 self.error("in_port decls require 'Address' type to be defined")
78
79 param_types.append(type)
80
81 if machine.EntryType != None:
82 param_types.append(machine.EntryType)
83 if machine.TBEType != None:
84 param_types.append(machine.TBEType)
85
86 # Add the trigger method - FIXME, this is a bit dirty
87 pairs = { "external" : "yes" }
88 func = Func(self.symtab, "trigger", self.location, void_type,
89 param_types, [], "", pairs)
90 symtab.newSymbol(func)
91
92 param_types = []
93 # Check for Event2
94 type = symtab.find("Event", Type)
95 if type is None:
96 self.error("in_port decls require 'Event' enumeration")
97
98 param_types.append(type)
99
100 # Check for Address2
101 type = symtab.find("Address", Type)
102 if type is None:
103 self.error("in_port decls require 'Address' type to be defined")
104
105 param_types.append(type)
106
107 if self.statements is not None:
108 rcode = self.slicc.codeFormatter()
109 rcode.indent()
110 rcode.indent()
111 self.statements.generate(rcode, None)
112 in_port["c_code_in_port"] = str(rcode)
113
114 symtab.popFrame()
115
116 # Add port to state machine
117 machine.addInPort(in_port)
123
124 # Include max_rank to be used by StateMachine.py
125 in_port["max_port_rank"] = InPortDeclAST.max_port_rank