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 makeDir
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 location
= Location("init", 0, no_warning
=not slicc
.verbose
)
46 MachineType
= Type(self
, "MachineType", location
, pairs
)
47 self
.newSymbol(MachineType
)
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
)
57 return "[SymbolTable]" # FIXME
59 def codeFormatter(self
, *args
, **kwargs
):
60 return self
.slicc
.codeFormatter(*args
, **kwargs
)
62 def newSymbol(self
, sym
):
63 self
.registerSym(str(sym
), sym
)
64 self
.sym_vec
.append(sym
)
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)
71 for sym_map
in self
.sym_map_vec
:
73 if type(self
.sym_map_vec
[0][id]) != type(sym
):
74 sym
.error("Conflicting declaration of Symbol '%s'", id)
76 # FIXME - warn on masking of a declaration in a previous frame
77 self
.sym_map_vec
[-1][id] = sym
79 def find(self
, ident
, types
=None):
80 for sym_map
in reversed(self
.sym_map_vec
):
82 symbol
= sym_map
[ident
]
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
95 def newMachComponentSym(self
, symbol
):
96 # used to cheat-- that is, access components in other machines
97 machine
= self
.find("current_machine", StateMachine
)
99 self
.machine_components
[str(machine
)][str(symbol
)] = symbol
101 def newCurrentMachine(self
, sym
):
102 self
.registerGlobalSym(str(sym
), sym
)
103 self
.registerSym("current_machine", sym
)
104 self
.sym_vec
.append(sym
)
106 self
.machine_components
[str(sym
)] = {}
109 def state_machine(self
):
110 return self
.find("current_machine", StateMachine
)
113 self
.sym_map_vec
.append({})
116 assert len(self
.sym_map_vec
) > 0
117 self
.sym_map_vec
.pop()
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
)
124 self
.sym_map_vec
[0][ident
] = symbol
126 def getAllType(self
, type):
127 for symbol
in self
.sym_vec
:
128 if isinstance(symbol
, type):
131 def writeCodeFiles(self
, path
, includes
):
134 code
= self
.codeFormatter()
135 code('/** Auto generated C++ code started by $__file__:$__line__ */')
137 for include_path
in includes
:
138 code('#include "${{include_path}}"')
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"')
144 code
.write(path
, "Types.hh")
146 for symbol
in self
.sym_vec
:
147 symbol
.writeCodeFiles(path
, includes
)
149 def writeHTMLFiles(self
, path
):
152 machines
= list(self
.getAllType(StateMachine
))
153 if len(machines
) > 1:
154 name
= "%s_table.html" % machines
[0].ident
158 code
= self
.codeFormatter()
164 <frameset rows="*,30">
165 <frame name="Table" src="$name">
166 <frame name="Status" src="empty.html">
170 code
.write(path
, "index.html")
172 code
= self
.codeFormatter()
173 code("<HTML></HTML>")
174 code
.write(path
, "empty.html")
176 for symbol
in self
.sym_vec
:
177 symbol
.writeHTMLFiles(path
)
179 __all__
= [ "SymbolTable" ]