SymbolTable.py revision 9302
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 716657Snate@binkert.org # FIXME - warn on masking of a declaration in a previous frame 726657Snate@binkert.org self.sym_map_vec[-1][id] = sym 736657Snate@binkert.org 746657Snate@binkert.org def find(self, ident, types=None): 756657Snate@binkert.org for sym_map in reversed(self.sym_map_vec): 766657Snate@binkert.org try: 776657Snate@binkert.org symbol = sym_map[ident] 786657Snate@binkert.org except KeyError: 796657Snate@binkert.org continue 806657Snate@binkert.org 816657Snate@binkert.org if types is not None: 826794SBrad.Beckmann@amd.com if not isinstance(symbol, types): 836794SBrad.Beckmann@amd.com symbol.error("Symbol '%s' is not of types '%s'.", 849302Snilay@cs.wisc.edu symbol, types) 856657Snate@binkert.org 866657Snate@binkert.org return symbol 876657Snate@binkert.org 886657Snate@binkert.org return None 896657Snate@binkert.org 906657Snate@binkert.org def newMachComponentSym(self, symbol): 916657Snate@binkert.org # used to cheat-- that is, access components in other machines 926657Snate@binkert.org machine = self.find("current_machine", StateMachine) 936657Snate@binkert.org if machine: 946657Snate@binkert.org self.machine_components[str(machine)][str(symbol)] = symbol 956657Snate@binkert.org 966657Snate@binkert.org def newCurrentMachine(self, sym): 976657Snate@binkert.org self.registerGlobalSym(str(sym), sym) 986657Snate@binkert.org self.registerSym("current_machine", sym) 996657Snate@binkert.org self.sym_vec.append(sym) 1006657Snate@binkert.org 1016657Snate@binkert.org self.machine_components[str(sym)] = {} 1026657Snate@binkert.org 1036657Snate@binkert.org @property 1046657Snate@binkert.org def state_machine(self): 1056657Snate@binkert.org return self.find("current_machine", StateMachine) 1066657Snate@binkert.org 1076657Snate@binkert.org def pushFrame(self): 1086657Snate@binkert.org self.sym_map_vec.append({}) 1096657Snate@binkert.org 1106657Snate@binkert.org def popFrame(self): 1116657Snate@binkert.org assert len(self.sym_map_vec) > 0 1126657Snate@binkert.org self.sym_map_vec.pop() 1136657Snate@binkert.org 1146657Snate@binkert.org def registerGlobalSym(self, ident, symbol): 1156657Snate@binkert.org # Check for redeclaration (global frame only) 1166657Snate@binkert.org if ident in self.sym_map_vec[0]: 1176657Snate@binkert.org symbol.error("Symbol '%s' redeclared in global scope." % ident) 1186657Snate@binkert.org 1196657Snate@binkert.org self.sym_map_vec[0][ident] = symbol 1206657Snate@binkert.org 1216657Snate@binkert.org def getAllType(self, type): 1226657Snate@binkert.org for symbol in self.sym_vec: 1236657Snate@binkert.org if isinstance(symbol, type): 1246657Snate@binkert.org yield symbol 1256657Snate@binkert.org 1269219Spower.jg@gmail.com def writeCodeFiles(self, path, includes): 1278453Snate@binkert.org makeDir(path) 1288453Snate@binkert.org 1296999Snate@binkert.org code = self.codeFormatter() 1309219Spower.jg@gmail.com code('/** Auto generated C++ code started by $__file__:$__line__ */') 1316657Snate@binkert.org 1329219Spower.jg@gmail.com for include_path in includes: 1339219Spower.jg@gmail.com code('#include "${{include_path}}"') 1349219Spower.jg@gmail.com 1356657Snate@binkert.org for symbol in self.sym_vec: 1366657Snate@binkert.org if isinstance(symbol, Type) and not symbol.isPrimitive: 1376657Snate@binkert.org code('#include "mem/protocol/${{symbol.c_ident}}.hh"') 1386657Snate@binkert.org 1396657Snate@binkert.org code.write(path, "Types.hh") 1406657Snate@binkert.org 1416657Snate@binkert.org for symbol in self.sym_vec: 1429219Spower.jg@gmail.com symbol.writeCodeFiles(path, includes) 1436657Snate@binkert.org 1446657Snate@binkert.org def writeHTMLFiles(self, path): 1458453Snate@binkert.org makeDir(path) 1468453Snate@binkert.org 1476657Snate@binkert.org machines = list(self.getAllType(StateMachine)) 1486657Snate@binkert.org if len(machines) > 1: 1496657Snate@binkert.org name = "%s_table.html" % machines[0].ident 1506657Snate@binkert.org else: 1516657Snate@binkert.org name = "empty.html" 1526657Snate@binkert.org 1536999Snate@binkert.org code = self.codeFormatter() 1546657Snate@binkert.org code(''' 1556657Snate@binkert.org<html> 1566657Snate@binkert.org<head> 1576657Snate@binkert.org<title>$path</title> 1586657Snate@binkert.org</head> 1596657Snate@binkert.org<frameset rows="*,30"> 1606657Snate@binkert.org <frame name="Table" src="$name"> 1616657Snate@binkert.org <frame name="Status" src="empty.html"> 1626657Snate@binkert.org</frameset> 1636657Snate@binkert.org</html> 1646657Snate@binkert.org''') 1656657Snate@binkert.org code.write(path, "index.html") 1666657Snate@binkert.org 1676999Snate@binkert.org code = self.codeFormatter() 1686657Snate@binkert.org code("<HTML></HTML>") 1696657Snate@binkert.org code.write(path, "empty.html") 1706657Snate@binkert.org 1716657Snate@binkert.org for symbol in self.sym_vec: 1726657Snate@binkert.org symbol.writeHTMLFiles(path) 1736657Snate@binkert.org 1746657Snate@binkert.org__all__ = [ "SymbolTable" ] 175