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["primitive"] = "yes" 456657Snate@binkert.org pairs["external"] = "yes" 468453Snate@binkert.org location = Location("init", 0, no_warning=not slicc.verbose) 478453Snate@binkert.org void = Type(self, "void", location, pairs) 486657Snate@binkert.org self.newSymbol(void) 496657Snate@binkert.org 506657Snate@binkert.org def __repr__(self): 516657Snate@binkert.org return "[SymbolTable]" # FIXME 526657Snate@binkert.org 536999Snate@binkert.org def codeFormatter(self, *args, **kwargs): 546999Snate@binkert.org return self.slicc.codeFormatter(*args, **kwargs) 556999Snate@binkert.org 566657Snate@binkert.org def newSymbol(self, sym): 576657Snate@binkert.org self.registerSym(str(sym), sym) 586657Snate@binkert.org self.sym_vec.append(sym) 596657Snate@binkert.org 606657Snate@binkert.org def registerSym(self, id, sym): 616657Snate@binkert.org # Check for redeclaration (in the current frame only) 626657Snate@binkert.org if id in self.sym_map_vec[-1]: 636657Snate@binkert.org sym.error("Symbol '%s' redeclared in same scope.", id) 646657Snate@binkert.org 659767Slena@cs.wisc.edu for sym_map in self.sym_map_vec: 669767Slena@cs.wisc.edu if id in sym_map: 6710985SBrad.Beckmann@amd.com if type(sym_map[id]) != type(sym): 689767Slena@cs.wisc.edu sym.error("Conflicting declaration of Symbol '%s'", id) 699767Slena@cs.wisc.edu 706657Snate@binkert.org # FIXME - warn on masking of a declaration in a previous frame 716657Snate@binkert.org self.sym_map_vec[-1][id] = sym 726657Snate@binkert.org 736657Snate@binkert.org def find(self, ident, types=None): 746657Snate@binkert.org for sym_map in reversed(self.sym_map_vec): 756657Snate@binkert.org try: 766657Snate@binkert.org symbol = sym_map[ident] 776657Snate@binkert.org except KeyError: 786657Snate@binkert.org continue 796657Snate@binkert.org 806657Snate@binkert.org if types is not None: 816794SBrad.Beckmann@amd.com if not isinstance(symbol, types): 8210953Sdavid.hashe@amd.com continue # there could be a name clash with other symbol 8310953Sdavid.hashe@amd.com # so rather than producing an error, keep trying 846657Snate@binkert.org 856657Snate@binkert.org return symbol 866657Snate@binkert.org 876657Snate@binkert.org return None 886657Snate@binkert.org 896657Snate@binkert.org def newMachComponentSym(self, symbol): 906657Snate@binkert.org # used to cheat-- that is, access components in other machines 916657Snate@binkert.org machine = self.find("current_machine", StateMachine) 926657Snate@binkert.org if machine: 936657Snate@binkert.org self.machine_components[str(machine)][str(symbol)] = symbol 946657Snate@binkert.org 956657Snate@binkert.org def newCurrentMachine(self, sym): 966657Snate@binkert.org self.registerGlobalSym(str(sym), sym) 976657Snate@binkert.org self.registerSym("current_machine", sym) 986657Snate@binkert.org self.sym_vec.append(sym) 996657Snate@binkert.org 1006657Snate@binkert.org self.machine_components[str(sym)] = {} 1016657Snate@binkert.org 1026657Snate@binkert.org @property 1036657Snate@binkert.org def state_machine(self): 1046657Snate@binkert.org return self.find("current_machine", StateMachine) 1056657Snate@binkert.org 1066657Snate@binkert.org def pushFrame(self): 1076657Snate@binkert.org self.sym_map_vec.append({}) 1086657Snate@binkert.org 1096657Snate@binkert.org def popFrame(self): 1106657Snate@binkert.org assert len(self.sym_map_vec) > 0 1116657Snate@binkert.org self.sym_map_vec.pop() 1126657Snate@binkert.org 1136657Snate@binkert.org def registerGlobalSym(self, ident, symbol): 1146657Snate@binkert.org # Check for redeclaration (global frame only) 1156657Snate@binkert.org if ident in self.sym_map_vec[0]: 1166657Snate@binkert.org symbol.error("Symbol '%s' redeclared in global scope." % ident) 1176657Snate@binkert.org 1186657Snate@binkert.org self.sym_map_vec[0][ident] = symbol 1196657Snate@binkert.org 1206657Snate@binkert.org def getAllType(self, type): 1216657Snate@binkert.org for symbol in self.sym_vec: 1226657Snate@binkert.org if isinstance(symbol, type): 1236657Snate@binkert.org yield symbol 1246657Snate@binkert.org 1259219Spower.jg@gmail.com def writeCodeFiles(self, path, includes): 1268453Snate@binkert.org makeDir(path) 1278453Snate@binkert.org 1286999Snate@binkert.org code = self.codeFormatter() 1299219Spower.jg@gmail.com code('/** Auto generated C++ code started by $__file__:$__line__ */') 1306657Snate@binkert.org 1319219Spower.jg@gmail.com for include_path in includes: 1329219Spower.jg@gmail.com code('#include "${{include_path}}"') 1339219Spower.jg@gmail.com 1346657Snate@binkert.org for symbol in self.sym_vec: 1356657Snate@binkert.org if isinstance(symbol, Type) and not symbol.isPrimitive: 13614184Sgabeblack@google.com code('#include "mem/ruby/protocol/${{symbol.c_ident}}.hh"') 1376657Snate@binkert.org 1386657Snate@binkert.org code.write(path, "Types.hh") 1396657Snate@binkert.org 1406657Snate@binkert.org for symbol in self.sym_vec: 1419219Spower.jg@gmail.com symbol.writeCodeFiles(path, includes) 1426657Snate@binkert.org 1436657Snate@binkert.org def writeHTMLFiles(self, path): 1448453Snate@binkert.org makeDir(path) 1458453Snate@binkert.org 1466657Snate@binkert.org machines = list(self.getAllType(StateMachine)) 1476657Snate@binkert.org if len(machines) > 1: 1486657Snate@binkert.org name = "%s_table.html" % machines[0].ident 1496657Snate@binkert.org else: 1506657Snate@binkert.org name = "empty.html" 1516657Snate@binkert.org 1526999Snate@binkert.org code = self.codeFormatter() 1536657Snate@binkert.org code(''' 1546657Snate@binkert.org<html> 1556657Snate@binkert.org<head> 1566657Snate@binkert.org<title>$path</title> 1576657Snate@binkert.org</head> 1586657Snate@binkert.org<frameset rows="*,30"> 1596657Snate@binkert.org <frame name="Table" src="$name"> 1606657Snate@binkert.org <frame name="Status" src="empty.html"> 1616657Snate@binkert.org</frameset> 1626657Snate@binkert.org</html> 1636657Snate@binkert.org''') 1646657Snate@binkert.org code.write(path, "index.html") 1656657Snate@binkert.org 1666999Snate@binkert.org code = self.codeFormatter() 1676657Snate@binkert.org code("<HTML></HTML>") 1686657Snate@binkert.org code.write(path, "empty.html") 1696657Snate@binkert.org 1706657Snate@binkert.org for symbol in self.sym_vec: 1716657Snate@binkert.org symbol.writeHTMLFiles(path) 1726657Snate@binkert.org 1736657Snate@binkert.org__all__ = [ "SymbolTable" ] 174