1 # Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
2 # Copyright (c) 2009 The Hewlett-Packard Development Company
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.
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.
28 from m5
.util
import code_formatter
30 from slicc
.generate
import html
31 from slicc
.symbols
.StateMachine
import StateMachine
32 from slicc
.symbols
.Type
import Type
33 from slicc
.util
import Location
35 class SymbolTable(object):
36 def __init__(self
, slicc
):
40 self
.sym_map_vec
= [ {} ]
41 self
.machine_components
= {}
44 pairs
["enumeration"] = "yes"
45 MachineType
= Type(self
, "MachineType", Location("init", 0), pairs
)
46 self
.newSymbol(MachineType
)
49 pairs
["primitive"] = "yes"
50 pairs
["external"] = "yes"
51 void
= Type(self
, "void", Location("init", 0), pairs
)
55 return "[SymbolTable]" # FIXME
57 def codeFormatter(self
, *args
, **kwargs
):
58 return self
.slicc
.codeFormatter(*args
, **kwargs
)
60 def newSymbol(self
, sym
):
61 self
.registerSym(str(sym
), sym
)
62 self
.sym_vec
.append(sym
)
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)
69 # FIXME - warn on masking of a declaration in a previous frame
70 self
.sym_map_vec
[-1][id] = sym
72 def find(self
, ident
, types
=None):
73 for sym_map
in reversed(self
.sym_map_vec
):
75 symbol
= sym_map
[ident
]
80 if not isinstance(symbol
, types
):
81 symbol
.error("Symbol '%s' is not of types '%s'.",
89 def newMachComponentSym(self
, symbol
):
90 # used to cheat-- that is, access components in other machines
91 machine
= self
.find("current_machine", StateMachine
)
93 self
.machine_components
[str(machine
)][str(symbol
)] = symbol
95 def newCurrentMachine(self
, sym
):
96 self
.registerGlobalSym(str(sym
), sym
)
97 self
.registerSym("current_machine", sym
)
98 self
.sym_vec
.append(sym
)
100 self
.machine_components
[str(sym
)] = {}
103 def state_machine(self
):
104 return self
.find("current_machine", StateMachine
)
107 self
.sym_map_vec
.append({})
110 assert len(self
.sym_map_vec
) > 0
111 self
.sym_map_vec
.pop()
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
)
118 self
.sym_map_vec
[0][ident
] = symbol
120 def getAllType(self
, type):
121 for symbol
in self
.sym_vec
:
122 if isinstance(symbol
, type):
125 def writeCodeFiles(self
, path
):
126 code
= self
.codeFormatter()
128 /** Auto generated C++ code started by $__file__:$__line__ */
130 #include "mem/ruby/slicc_interface/RubySlicc_includes.hh"
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"')
136 code
.write(path
, "Types.hh")
138 for symbol
in self
.sym_vec
:
139 symbol
.writeCodeFiles(path
)
141 def writeHTMLFiles(self
, path
):
142 machines
= list(self
.getAllType(StateMachine
))
143 if len(machines
) > 1:
144 name
= "%s_table.html" % machines
[0].ident
148 code
= self
.codeFormatter()
154 <frameset rows="*,30">
155 <frame name="Table" src="$name">
156 <frame name="Status" src="empty.html">
160 code
.write(path
, "index.html")
162 code
= self
.codeFormatter()
163 code("<HTML></HTML>")
164 code
.write(path
, "empty.html")
166 for symbol
in self
.sym_vec
:
167 symbol
.writeHTMLFiles(path
)
169 __all__
= [ "SymbolTable" ]