SymbolTable.py (10985:d87a25259254) SymbolTable.py (11283:4cc8b312f026)
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 makeDir
29
30from slicc.generate import html
31from slicc.symbols.StateMachine import StateMachine
32from slicc.symbols.Type import Type
33from slicc.util import Location
34
35class SymbolTable(object):
36 def __init__(self, slicc):
37 self.slicc = slicc
38
39 self.sym_vec = []
40 self.sym_map_vec = [ {} ]
41 self.machine_components = {}
42
43 pairs = {}
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 makeDir
29
30from slicc.generate import html
31from slicc.symbols.StateMachine import StateMachine
32from slicc.symbols.Type import Type
33from slicc.util import Location
34
35class SymbolTable(object):
36 def __init__(self, slicc):
37 self.slicc = slicc
38
39 self.sym_vec = []
40 self.sym_map_vec = [ {} ]
41 self.machine_components = {}
42
43 pairs = {}
44 pairs["enumeration"] = "yes"
45 location = Location("init", 0, no_warning=not slicc.verbose)
46 MachineType = Type(self, "MachineType", location, pairs)
47 self.newSymbol(MachineType)
48
49 pairs = {}
50 pairs["primitive"] = "yes"
51 pairs["external"] = "yes"
52 location = Location("init", 0, no_warning=not slicc.verbose)
53 void = Type(self, "void", location, pairs)
54 self.newSymbol(void)
55
56 def __repr__(self):
57 return "[SymbolTable]" # FIXME
58
59 def codeFormatter(self, *args, **kwargs):
60 return self.slicc.codeFormatter(*args, **kwargs)
61
62 def newSymbol(self, sym):
63 self.registerSym(str(sym), sym)
64 self.sym_vec.append(sym)
65
66 def registerSym(self, id, sym):
67 # Check for redeclaration (in the current frame only)
68 if id in self.sym_map_vec[-1]:
69 sym.error("Symbol '%s' redeclared in same scope.", id)
70
71 for sym_map in self.sym_map_vec:
72 if id in sym_map:
73 if type(sym_map[id]) != type(sym):
74 sym.error("Conflicting declaration of Symbol '%s'", id)
75
76 # FIXME - warn on masking of a declaration in a previous frame
77 self.sym_map_vec[-1][id] = sym
78
79 def find(self, ident, types=None):
80 for sym_map in reversed(self.sym_map_vec):
81 try:
82 symbol = sym_map[ident]
83 except KeyError:
84 continue
85
86 if types is not None:
87 if not isinstance(symbol, types):
88 continue # there could be a name clash with other symbol
89 # so rather than producing an error, keep trying
90
91 return symbol
92
93 return None
94
95 def newMachComponentSym(self, symbol):
96 # used to cheat-- that is, access components in other machines
97 machine = self.find("current_machine", StateMachine)
98 if machine:
99 self.machine_components[str(machine)][str(symbol)] = symbol
100
101 def newCurrentMachine(self, sym):
102 self.registerGlobalSym(str(sym), sym)
103 self.registerSym("current_machine", sym)
104 self.sym_vec.append(sym)
105
106 self.machine_components[str(sym)] = {}
107
108 @property
109 def state_machine(self):
110 return self.find("current_machine", StateMachine)
111
112 def pushFrame(self):
113 self.sym_map_vec.append({})
114
115 def popFrame(self):
116 assert len(self.sym_map_vec) > 0
117 self.sym_map_vec.pop()
118
119 def registerGlobalSym(self, ident, symbol):
120 # Check for redeclaration (global frame only)
121 if ident in self.sym_map_vec[0]:
122 symbol.error("Symbol '%s' redeclared in global scope." % ident)
123
124 self.sym_map_vec[0][ident] = symbol
125
126 def getAllType(self, type):
127 for symbol in self.sym_vec:
128 if isinstance(symbol, type):
129 yield symbol
130
131 def writeCodeFiles(self, path, includes):
132 makeDir(path)
133
134 code = self.codeFormatter()
135 code('/** Auto generated C++ code started by $__file__:$__line__ */')
136
137 for include_path in includes:
138 code('#include "${{include_path}}"')
139
140 for symbol in self.sym_vec:
141 if isinstance(symbol, Type) and not symbol.isPrimitive:
142 code('#include "mem/protocol/${{symbol.c_ident}}.hh"')
143
144 code.write(path, "Types.hh")
145
146 for symbol in self.sym_vec:
147 symbol.writeCodeFiles(path, includes)
148
149 def writeHTMLFiles(self, path):
150 makeDir(path)
151
152 machines = list(self.getAllType(StateMachine))
153 if len(machines) > 1:
154 name = "%s_table.html" % machines[0].ident
155 else:
156 name = "empty.html"
157
158 code = self.codeFormatter()
159 code('''
160<html>
161<head>
162<title>$path</title>
163</head>
164<frameset rows="*,30">
165 <frame name="Table" src="$name">
166 <frame name="Status" src="empty.html">
167</frameset>
168</html>
169''')
170 code.write(path, "index.html")
171
172 code = self.codeFormatter()
173 code("<HTML></HTML>")
174 code.write(path, "empty.html")
175
176 for symbol in self.sym_vec:
177 symbol.writeHTMLFiles(path)
178
179__all__ = [ "SymbolTable" ]
44 pairs["primitive"] = "yes"
45 pairs["external"] = "yes"
46 location = Location("init", 0, no_warning=not slicc.verbose)
47 void = Type(self, "void", location, pairs)
48 self.newSymbol(void)
49
50 def __repr__(self):
51 return "[SymbolTable]" # FIXME
52
53 def codeFormatter(self, *args, **kwargs):
54 return self.slicc.codeFormatter(*args, **kwargs)
55
56 def newSymbol(self, sym):
57 self.registerSym(str(sym), sym)
58 self.sym_vec.append(sym)
59
60 def registerSym(self, id, sym):
61 # Check for redeclaration (in the current frame only)
62 if id in self.sym_map_vec[-1]:
63 sym.error("Symbol '%s' redeclared in same scope.", id)
64
65 for sym_map in self.sym_map_vec:
66 if id in sym_map:
67 if type(sym_map[id]) != type(sym):
68 sym.error("Conflicting declaration of Symbol '%s'", id)
69
70 # FIXME - warn on masking of a declaration in a previous frame
71 self.sym_map_vec[-1][id] = sym
72
73 def find(self, ident, types=None):
74 for sym_map in reversed(self.sym_map_vec):
75 try:
76 symbol = sym_map[ident]
77 except KeyError:
78 continue
79
80 if types is not None:
81 if not isinstance(symbol, types):
82 continue # there could be a name clash with other symbol
83 # so rather than producing an error, keep trying
84
85 return symbol
86
87 return None
88
89 def newMachComponentSym(self, symbol):
90 # used to cheat-- that is, access components in other machines
91 machine = self.find("current_machine", StateMachine)
92 if machine:
93 self.machine_components[str(machine)][str(symbol)] = symbol
94
95 def newCurrentMachine(self, sym):
96 self.registerGlobalSym(str(sym), sym)
97 self.registerSym("current_machine", sym)
98 self.sym_vec.append(sym)
99
100 self.machine_components[str(sym)] = {}
101
102 @property
103 def state_machine(self):
104 return self.find("current_machine", StateMachine)
105
106 def pushFrame(self):
107 self.sym_map_vec.append({})
108
109 def popFrame(self):
110 assert len(self.sym_map_vec) > 0
111 self.sym_map_vec.pop()
112
113 def registerGlobalSym(self, ident, symbol):
114 # Check for redeclaration (global frame only)
115 if ident in self.sym_map_vec[0]:
116 symbol.error("Symbol '%s' redeclared in global scope." % ident)
117
118 self.sym_map_vec[0][ident] = symbol
119
120 def getAllType(self, type):
121 for symbol in self.sym_vec:
122 if isinstance(symbol, type):
123 yield symbol
124
125 def writeCodeFiles(self, path, includes):
126 makeDir(path)
127
128 code = self.codeFormatter()
129 code('/** Auto generated C++ code started by $__file__:$__line__ */')
130
131 for include_path in includes:
132 code('#include "${{include_path}}"')
133
134 for symbol in self.sym_vec:
135 if isinstance(symbol, Type) and not symbol.isPrimitive:
136 code('#include "mem/protocol/${{symbol.c_ident}}.hh"')
137
138 code.write(path, "Types.hh")
139
140 for symbol in self.sym_vec:
141 symbol.writeCodeFiles(path, includes)
142
143 def writeHTMLFiles(self, path):
144 makeDir(path)
145
146 machines = list(self.getAllType(StateMachine))
147 if len(machines) > 1:
148 name = "%s_table.html" % machines[0].ident
149 else:
150 name = "empty.html"
151
152 code = self.codeFormatter()
153 code('''
154<html>
155<head>
156<title>$path</title>
157</head>
158<frameset rows="*,30">
159 <frame name="Table" src="$name">
160 <frame name="Status" src="empty.html">
161</frameset>
162</html>
163''')
164 code.write(path, "index.html")
165
166 code = self.codeFormatter()
167 code("<HTML></HTML>")
168 code.write(path, "empty.html")
169
170 for symbol in self.sym_vec:
171 symbol.writeHTMLFiles(path)
172
173__all__ = [ "SymbolTable" ]