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