SymbolTable.py revision 9219
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        # FIXME - warn on masking of a declaration in a previous frame
72        self.sym_map_vec[-1][id] = sym
73
74    def find(self, ident, types=None):
75        for sym_map in reversed(self.sym_map_vec):
76            try:
77                symbol = sym_map[ident]
78            except KeyError:
79                continue
80
81            if types is not None:
82                if not isinstance(symbol, types):
83                    symbol.error("Symbol '%s' is not of types '%s'.",
84                                 symbol,
85                                 types)
86
87            return symbol
88
89        return None
90
91    def newMachComponentSym(self, symbol):
92        # used to cheat-- that is, access components in other machines
93        machine = self.find("current_machine", StateMachine)
94        if machine:
95            self.machine_components[str(machine)][str(symbol)] = symbol
96
97    def newCurrentMachine(self, sym):
98        self.registerGlobalSym(str(sym), sym)
99        self.registerSym("current_machine", sym)
100        self.sym_vec.append(sym)
101
102        self.machine_components[str(sym)] = {}
103
104    @property
105    def state_machine(self):
106        return self.find("current_machine", StateMachine)
107
108    def pushFrame(self):
109        self.sym_map_vec.append({})
110
111    def popFrame(self):
112        assert len(self.sym_map_vec) > 0
113        self.sym_map_vec.pop()
114
115    def registerGlobalSym(self, ident, symbol):
116        # Check for redeclaration (global frame only)
117        if ident in self.sym_map_vec[0]:
118            symbol.error("Symbol '%s' redeclared in global scope." % ident)
119
120        self.sym_map_vec[0][ident] = symbol
121
122    def getAllType(self, type):
123        for symbol in self.sym_vec:
124            if isinstance(symbol, type):
125                yield symbol
126
127    def writeCodeFiles(self, path, includes):
128        makeDir(path)
129
130        code = self.codeFormatter()
131        code('/** Auto generated C++ code started by $__file__:$__line__ */')
132
133        for include_path in includes:
134            code('#include "${{include_path}}"')
135
136        for symbol in self.sym_vec:
137            if isinstance(symbol, Type) and not symbol.isPrimitive:
138                code('#include "mem/protocol/${{symbol.c_ident}}.hh"')
139
140        code.write(path, "Types.hh")
141
142        for symbol in self.sym_vec:
143            symbol.writeCodeFiles(path, includes)
144
145    def writeHTMLFiles(self, path):
146        makeDir(path)
147
148        machines = list(self.getAllType(StateMachine))
149        if len(machines) > 1:
150            name = "%s_table.html" % machines[0].ident
151        else:
152            name = "empty.html"
153
154        code = self.codeFormatter()
155        code('''
156<html>
157<head>
158<title>$path</title>
159</head>
160<frameset rows="*,30">
161    <frame name="Table" src="$name">
162    <frame name="Status" src="empty.html">
163</frameset>
164</html>
165''')
166        code.write(path, "index.html")
167
168        code = self.codeFormatter()
169        code("<HTML></HTML>")
170        code.write(path, "empty.html")
171
172        for symbol in self.sym_vec:
173            symbol.writeHTMLFiles(path)
174
175__all__ = [ "SymbolTable" ]
176