SymbolTable.py revision 9767
16657Snate@binkert.org# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
26657Snate@binkert.org# Copyright (c) 2009 The Hewlett-Packard Development Company
36657Snate@binkert.org# All rights reserved.
46657Snate@binkert.org#
56657Snate@binkert.org# Redistribution and use in source and binary forms, with or without
66657Snate@binkert.org# modification, are permitted provided that the following conditions are
76657Snate@binkert.org# met: redistributions of source code must retain the above copyright
86657Snate@binkert.org# notice, this list of conditions and the following disclaimer;
96657Snate@binkert.org# redistributions in binary form must reproduce the above copyright
106657Snate@binkert.org# notice, this list of conditions and the following disclaimer in the
116657Snate@binkert.org# documentation and/or other materials provided with the distribution;
126657Snate@binkert.org# neither the name of the copyright holders nor the names of its
136657Snate@binkert.org# contributors may be used to endorse or promote products derived from
146657Snate@binkert.org# this software without specific prior written permission.
156657Snate@binkert.org#
166657Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176657Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186657Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196657Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206657Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216657Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226657Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236657Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246657Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256657Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266657Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276657Snate@binkert.org
288453Snate@binkert.orgfrom m5.util import makeDir
296657Snate@binkert.org
306657Snate@binkert.orgfrom slicc.generate import html
316657Snate@binkert.orgfrom slicc.symbols.StateMachine import StateMachine
326657Snate@binkert.orgfrom slicc.symbols.Type import Type
336657Snate@binkert.orgfrom slicc.util import Location
346657Snate@binkert.org
356657Snate@binkert.orgclass SymbolTable(object):
366999Snate@binkert.org    def __init__(self, slicc):
376999Snate@binkert.org        self.slicc = slicc
386999Snate@binkert.org
396657Snate@binkert.org        self.sym_vec = []
406657Snate@binkert.org        self.sym_map_vec = [ {} ]
416657Snate@binkert.org        self.machine_components = {}
426657Snate@binkert.org
436657Snate@binkert.org        pairs = {}
446657Snate@binkert.org        pairs["enumeration"] = "yes"
458453Snate@binkert.org        location = Location("init", 0, no_warning=not slicc.verbose)
468453Snate@binkert.org        MachineType = Type(self, "MachineType", location, pairs)
476657Snate@binkert.org        self.newSymbol(MachineType)
486657Snate@binkert.org
496657Snate@binkert.org        pairs = {}
506657Snate@binkert.org        pairs["primitive"] = "yes"
516657Snate@binkert.org        pairs["external"] = "yes"
528453Snate@binkert.org        location = Location("init", 0, no_warning=not slicc.verbose)
538453Snate@binkert.org        void = Type(self, "void", location, pairs)
546657Snate@binkert.org        self.newSymbol(void)
556657Snate@binkert.org
566657Snate@binkert.org    def __repr__(self):
576657Snate@binkert.org        return "[SymbolTable]" # FIXME
586657Snate@binkert.org
596999Snate@binkert.org    def codeFormatter(self, *args, **kwargs):
606999Snate@binkert.org        return self.slicc.codeFormatter(*args, **kwargs)
616999Snate@binkert.org
626657Snate@binkert.org    def newSymbol(self, sym):
636657Snate@binkert.org        self.registerSym(str(sym), sym)
646657Snate@binkert.org        self.sym_vec.append(sym)
656657Snate@binkert.org
666657Snate@binkert.org    def registerSym(self, id, sym):
676657Snate@binkert.org        # Check for redeclaration (in the current frame only)
686657Snate@binkert.org        if id in self.sym_map_vec[-1]:
696657Snate@binkert.org            sym.error("Symbol '%s' redeclared in same scope.", id)
706657Snate@binkert.org
719767Slena@cs.wisc.edu        for sym_map in self.sym_map_vec:
729767Slena@cs.wisc.edu            if id in sym_map:
739767Slena@cs.wisc.edu                if type(self.sym_map_vec[0][id]) != type(sym):
749767Slena@cs.wisc.edu                    sym.error("Conflicting declaration of Symbol '%s'", id)
759767Slena@cs.wisc.edu
766657Snate@binkert.org        # FIXME - warn on masking of a declaration in a previous frame
776657Snate@binkert.org        self.sym_map_vec[-1][id] = sym
786657Snate@binkert.org
796657Snate@binkert.org    def find(self, ident, types=None):
806657Snate@binkert.org        for sym_map in reversed(self.sym_map_vec):
816657Snate@binkert.org            try:
826657Snate@binkert.org                symbol = sym_map[ident]
836657Snate@binkert.org            except KeyError:
846657Snate@binkert.org                continue
856657Snate@binkert.org
866657Snate@binkert.org            if types is not None:
876794SBrad.Beckmann@amd.com                if not isinstance(symbol, types):
886794SBrad.Beckmann@amd.com                    symbol.error("Symbol '%s' is not of types '%s'.",
899302Snilay@cs.wisc.edu                                 symbol, types)
906657Snate@binkert.org
916657Snate@binkert.org            return symbol
926657Snate@binkert.org
936657Snate@binkert.org        return None
946657Snate@binkert.org
956657Snate@binkert.org    def newMachComponentSym(self, symbol):
966657Snate@binkert.org        # used to cheat-- that is, access components in other machines
976657Snate@binkert.org        machine = self.find("current_machine", StateMachine)
986657Snate@binkert.org        if machine:
996657Snate@binkert.org            self.machine_components[str(machine)][str(symbol)] = symbol
1006657Snate@binkert.org
1016657Snate@binkert.org    def newCurrentMachine(self, sym):
1026657Snate@binkert.org        self.registerGlobalSym(str(sym), sym)
1036657Snate@binkert.org        self.registerSym("current_machine", sym)
1046657Snate@binkert.org        self.sym_vec.append(sym)
1056657Snate@binkert.org
1066657Snate@binkert.org        self.machine_components[str(sym)] = {}
1076657Snate@binkert.org
1086657Snate@binkert.org    @property
1096657Snate@binkert.org    def state_machine(self):
1106657Snate@binkert.org        return self.find("current_machine", StateMachine)
1116657Snate@binkert.org
1126657Snate@binkert.org    def pushFrame(self):
1136657Snate@binkert.org        self.sym_map_vec.append({})
1146657Snate@binkert.org
1156657Snate@binkert.org    def popFrame(self):
1166657Snate@binkert.org        assert len(self.sym_map_vec) > 0
1176657Snate@binkert.org        self.sym_map_vec.pop()
1186657Snate@binkert.org
1196657Snate@binkert.org    def registerGlobalSym(self, ident, symbol):
1206657Snate@binkert.org        # Check for redeclaration (global frame only)
1216657Snate@binkert.org        if ident in self.sym_map_vec[0]:
1226657Snate@binkert.org            symbol.error("Symbol '%s' redeclared in global scope." % ident)
1236657Snate@binkert.org
1246657Snate@binkert.org        self.sym_map_vec[0][ident] = symbol
1256657Snate@binkert.org
1266657Snate@binkert.org    def getAllType(self, type):
1276657Snate@binkert.org        for symbol in self.sym_vec:
1286657Snate@binkert.org            if isinstance(symbol, type):
1296657Snate@binkert.org                yield symbol
1306657Snate@binkert.org
1319219Spower.jg@gmail.com    def writeCodeFiles(self, path, includes):
1328453Snate@binkert.org        makeDir(path)
1338453Snate@binkert.org
1346999Snate@binkert.org        code = self.codeFormatter()
1359219Spower.jg@gmail.com        code('/** Auto generated C++ code started by $__file__:$__line__ */')
1366657Snate@binkert.org
1379219Spower.jg@gmail.com        for include_path in includes:
1389219Spower.jg@gmail.com            code('#include "${{include_path}}"')
1399219Spower.jg@gmail.com
1406657Snate@binkert.org        for symbol in self.sym_vec:
1416657Snate@binkert.org            if isinstance(symbol, Type) and not symbol.isPrimitive:
1426657Snate@binkert.org                code('#include "mem/protocol/${{symbol.c_ident}}.hh"')
1436657Snate@binkert.org
1446657Snate@binkert.org        code.write(path, "Types.hh")
1456657Snate@binkert.org
1466657Snate@binkert.org        for symbol in self.sym_vec:
1479219Spower.jg@gmail.com            symbol.writeCodeFiles(path, includes)
1486657Snate@binkert.org
1496657Snate@binkert.org    def writeHTMLFiles(self, path):
1508453Snate@binkert.org        makeDir(path)
1518453Snate@binkert.org
1526657Snate@binkert.org        machines = list(self.getAllType(StateMachine))
1536657Snate@binkert.org        if len(machines) > 1:
1546657Snate@binkert.org            name = "%s_table.html" % machines[0].ident
1556657Snate@binkert.org        else:
1566657Snate@binkert.org            name = "empty.html"
1576657Snate@binkert.org
1586999Snate@binkert.org        code = self.codeFormatter()
1596657Snate@binkert.org        code('''
1606657Snate@binkert.org<html>
1616657Snate@binkert.org<head>
1626657Snate@binkert.org<title>$path</title>
1636657Snate@binkert.org</head>
1646657Snate@binkert.org<frameset rows="*,30">
1656657Snate@binkert.org    <frame name="Table" src="$name">
1666657Snate@binkert.org    <frame name="Status" src="empty.html">
1676657Snate@binkert.org</frameset>
1686657Snate@binkert.org</html>
1696657Snate@binkert.org''')
1706657Snate@binkert.org        code.write(path, "index.html")
1716657Snate@binkert.org
1726999Snate@binkert.org        code = self.codeFormatter()
1736657Snate@binkert.org        code("<HTML></HTML>")
1746657Snate@binkert.org        code.write(path, "empty.html")
1756657Snate@binkert.org
1766657Snate@binkert.org        for symbol in self.sym_vec:
1776657Snate@binkert.org            symbol.writeHTMLFiles(path)
1786657Snate@binkert.org
1796657Snate@binkert.org__all__ = [ "SymbolTable" ]
180