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