Type.py revision 8086
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 286999Snate@binkert.orgfrom m5.util import orderdict 296657Snate@binkert.org 306657Snate@binkert.orgfrom slicc.util import PairContainer 316657Snate@binkert.orgfrom slicc.symbols.Symbol import Symbol 326657Snate@binkert.org 336657Snate@binkert.orgclass DataMember(PairContainer): 346657Snate@binkert.org def __init__(self, ident, type, pairs, init_code): 356657Snate@binkert.org super(DataMember, self).__init__(pairs) 366657Snate@binkert.org self.ident = ident 376657Snate@binkert.org self.type = type 386657Snate@binkert.org self.init_code = init_code 396657Snate@binkert.org 406657Snate@binkert.orgclass Enumeration(PairContainer): 416657Snate@binkert.org def __init__(self, ident, pairs): 426657Snate@binkert.org super(Enumeration, self).__init__(pairs) 436657Snate@binkert.org self.ident = ident 446657Snate@binkert.org 456657Snate@binkert.orgclass Method(object): 466657Snate@binkert.org def __init__(self, return_type, param_types): 476657Snate@binkert.org self.return_type = return_type 486657Snate@binkert.org self.param_types = param_types 496657Snate@binkert.org 506657Snate@binkert.orgclass Type(Symbol): 516657Snate@binkert.org def __init__(self, table, ident, location, pairs, machine=None): 526657Snate@binkert.org super(Type, self).__init__(table, ident, location, pairs) 536657Snate@binkert.org self.c_ident = ident 546882SBrad.Beckmann@amd.com self.abstract_ident = "" 556657Snate@binkert.org if machine: 566657Snate@binkert.org if self.isExternal or self.isPrimitive: 576657Snate@binkert.org if "external_name" in self: 586657Snate@binkert.org self.c_ident = self["external_name"] 596657Snate@binkert.org else: 606657Snate@binkert.org # Append with machine name 616657Snate@binkert.org self.c_ident = "%s_%s" % (machine, ident) 626657Snate@binkert.org 636657Snate@binkert.org self.pairs.setdefault("desc", "No description avaliable") 646657Snate@binkert.org 656657Snate@binkert.org # check for interface that this Type implements 666657Snate@binkert.org if "interface" in self: 676657Snate@binkert.org interface = self["interface"] 686657Snate@binkert.org if interface in ("Message", "NetworkMessage"): 696657Snate@binkert.org self["message"] = "yes" 706657Snate@binkert.org if interface == "NetworkMessage": 716657Snate@binkert.org self["networkmessage"] = "yes" 726657Snate@binkert.org 736657Snate@binkert.org # FIXME - all of the following id comparisons are fragile hacks 746657Snate@binkert.org if self.ident in ("CacheMemory", "NewCacheMemory", 756657Snate@binkert.org "TLCCacheMemory", "DNUCACacheMemory", 766657Snate@binkert.org "DNUCABankCacheMemory", "L2BankCacheMemory", 776657Snate@binkert.org "CompressedCacheMemory", "PrefetchCacheMemory"): 786657Snate@binkert.org self["cache"] = "yes" 796657Snate@binkert.org 806657Snate@binkert.org if self.ident in ("TBETable", "DNUCATBETable", "DNUCAStopTable"): 816657Snate@binkert.org self["tbe"] = "yes" 826657Snate@binkert.org 836657Snate@binkert.org if self.ident == "NewTBETable": 846657Snate@binkert.org self["newtbe"] = "yes" 856657Snate@binkert.org 866657Snate@binkert.org if self.ident == "TimerTable": 876657Snate@binkert.org self["timer"] = "yes" 886657Snate@binkert.org 896657Snate@binkert.org if self.ident == "DirectoryMemory": 906657Snate@binkert.org self["dir"] = "yes" 916657Snate@binkert.org 926657Snate@binkert.org if self.ident == "PersistentTable": 936657Snate@binkert.org self["persistent"] = "yes" 946657Snate@binkert.org 956657Snate@binkert.org if self.ident == "Prefetcher": 966657Snate@binkert.org self["prefetcher"] = "yes" 976657Snate@binkert.org 986657Snate@binkert.org if self.ident == "DNUCA_Movement": 996657Snate@binkert.org self["mover"] = "yes" 1006657Snate@binkert.org 1016657Snate@binkert.org self.isMachineType = (ident == "MachineType") 1026657Snate@binkert.org 1038086SBrad.Beckmann@amd.com self.isStateDecl = ("state_decl" in self) 1048086SBrad.Beckmann@amd.com self.statePermPairs = [] 1058086SBrad.Beckmann@amd.com 1066657Snate@binkert.org self.data_members = orderdict() 1076657Snate@binkert.org 1086657Snate@binkert.org # Methods 1096657Snate@binkert.org self.methods = {} 1106657Snate@binkert.org 1116657Snate@binkert.org # Enums 1126657Snate@binkert.org self.enums = orderdict() 1136657Snate@binkert.org 1146657Snate@binkert.org @property 1156657Snate@binkert.org def isPrimitive(self): 1166657Snate@binkert.org return "primitive" in self 1176657Snate@binkert.org @property 1186657Snate@binkert.org def isNetworkMessage(self): 1196657Snate@binkert.org return "networkmessage" in self 1206657Snate@binkert.org @property 1216657Snate@binkert.org def isMessage(self): 1226657Snate@binkert.org return "message" in self 1236657Snate@binkert.org @property 1246657Snate@binkert.org def isBuffer(self): 1256657Snate@binkert.org return "buffer" in self 1266657Snate@binkert.org @property 1276657Snate@binkert.org def isInPort(self): 1286657Snate@binkert.org return "inport" in self 1296657Snate@binkert.org @property 1306657Snate@binkert.org def isOutPort(self): 1316657Snate@binkert.org return "outport" in self 1326657Snate@binkert.org @property 1336657Snate@binkert.org def isEnumeration(self): 1346657Snate@binkert.org return "enumeration" in self 1356657Snate@binkert.org @property 1366657Snate@binkert.org def isExternal(self): 1376657Snate@binkert.org return "external" in self 1386657Snate@binkert.org @property 1396657Snate@binkert.org def isGlobal(self): 1406657Snate@binkert.org return "global" in self 1416657Snate@binkert.org @property 1426657Snate@binkert.org def isInterface(self): 1436657Snate@binkert.org return "interface" in self 1446657Snate@binkert.org 1456657Snate@binkert.org # Return false on error 1466657Snate@binkert.org def dataMemberAdd(self, ident, type, pairs, init_code): 1476657Snate@binkert.org if ident in self.data_members: 1486657Snate@binkert.org return False 1496657Snate@binkert.org 1506657Snate@binkert.org member = DataMember(ident, type, pairs, init_code) 1516657Snate@binkert.org self.data_members[ident] = member 1526657Snate@binkert.org 1536657Snate@binkert.org return True 1546657Snate@binkert.org 1556657Snate@binkert.org def dataMemberType(self, ident): 1566657Snate@binkert.org return self.data_members[ident].type 1576657Snate@binkert.org 1586657Snate@binkert.org def methodId(self, name, param_type_vec): 1596657Snate@binkert.org return '_'.join([name] + [ pt.c_ident for pt in param_type_vec ]) 1606657Snate@binkert.org 1616882SBrad.Beckmann@amd.com def methodIdAbstract(self, name, param_type_vec): 1626882SBrad.Beckmann@amd.com return '_'.join([name] + [ pt.abstract_ident for pt in param_type_vec ]) 1636882SBrad.Beckmann@amd.com 1648086SBrad.Beckmann@amd.com def statePermPairAdd(self, state_name, perm_name): 1658086SBrad.Beckmann@amd.com self.statePermPairs.append([state_name, perm_name]) 1668086SBrad.Beckmann@amd.com 1676657Snate@binkert.org def methodAdd(self, name, return_type, param_type_vec): 1686657Snate@binkert.org ident = self.methodId(name, param_type_vec) 1696657Snate@binkert.org if ident in self.methods: 1706657Snate@binkert.org return False 1716657Snate@binkert.org 1726657Snate@binkert.org self.methods[ident] = Method(return_type, param_type_vec) 1736657Snate@binkert.org return True 1746657Snate@binkert.org 1756657Snate@binkert.org def enumAdd(self, ident, pairs): 1766657Snate@binkert.org if ident in self.enums: 1776657Snate@binkert.org return False 1786657Snate@binkert.org 1796657Snate@binkert.org self.enums[ident] = Enumeration(ident, pairs) 1806657Snate@binkert.org 1816657Snate@binkert.org # Add default 1826657Snate@binkert.org if "default" not in self: 1836657Snate@binkert.org self["default"] = "%s_NUM" % self.c_ident 1846657Snate@binkert.org 1856657Snate@binkert.org return True 1866657Snate@binkert.org 1876657Snate@binkert.org def writeCodeFiles(self, path): 1886657Snate@binkert.org if self.isExternal: 1896657Snate@binkert.org # Do nothing 1906657Snate@binkert.org pass 1916657Snate@binkert.org elif self.isEnumeration: 1926657Snate@binkert.org self.printEnumHH(path) 1936657Snate@binkert.org self.printEnumCC(path) 1946657Snate@binkert.org else: 1956657Snate@binkert.org # User defined structs and messages 1966657Snate@binkert.org self.printTypeHH(path) 1976657Snate@binkert.org self.printTypeCC(path) 1986657Snate@binkert.org 1996657Snate@binkert.org def printTypeHH(self, path): 2006999Snate@binkert.org code = self.symtab.codeFormatter() 2016657Snate@binkert.org code(''' 2026657Snate@binkert.org/** \\file ${{self.c_ident}}.hh 2036657Snate@binkert.org * 2046657Snate@binkert.org * 2056657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__ 2066657Snate@binkert.org */ 2076657Snate@binkert.org 2087007Snate@binkert.org#ifndef __${{self.c_ident}}_HH__ 2097007Snate@binkert.org#define __${{self.c_ident}}_HH__ 2106657Snate@binkert.org 2117002Snate@binkert.org#include <iostream> 2127002Snate@binkert.org 2136657Snate@binkert.org#include "mem/ruby/common/Global.hh" 2146657Snate@binkert.org''') 2156657Snate@binkert.org 2166657Snate@binkert.org for dm in self.data_members.values(): 2176657Snate@binkert.org if not dm.type.isPrimitive: 2186657Snate@binkert.org code('#include "mem/protocol/$0.hh"', dm.type.c_ident) 2196657Snate@binkert.org 2206657Snate@binkert.org parent = "" 2216657Snate@binkert.org if "interface" in self: 2226657Snate@binkert.org code('#include "mem/protocol/$0.hh"', self["interface"]) 2236657Snate@binkert.org parent = " : public %s" % self["interface"] 2246657Snate@binkert.org 2256657Snate@binkert.org code(''' 2267007Snate@binkert.org$klass ${{self.c_ident}}$parent 2277007Snate@binkert.org{ 2286657Snate@binkert.org public: 2296657Snate@binkert.org ${{self.c_ident}}() 2307007Snate@binkert.org { 2316657Snate@binkert.org''', klass="class") 2326657Snate@binkert.org 2336657Snate@binkert.org code.indent() 2346657Snate@binkert.org if not self.isGlobal: 2356657Snate@binkert.org code.indent() 2366657Snate@binkert.org for dm in self.data_members.values(): 2376657Snate@binkert.org ident = dm.ident 2386657Snate@binkert.org if "default" in dm: 2396657Snate@binkert.org # look for default value 2406657Snate@binkert.org code('m_$ident = ${{dm["default"]}}; // default for this field') 2416657Snate@binkert.org elif "default" in dm.type: 2426657Snate@binkert.org # Look for the type default 2436657Snate@binkert.org tid = dm.type.c_ident 2446657Snate@binkert.org code('m_$ident = ${{dm.type["default"]}}; // default value of $tid') 2456657Snate@binkert.org else: 2466657Snate@binkert.org code('// m_$ident has no default') 2476657Snate@binkert.org code.dedent() 2486657Snate@binkert.org code('}') 2496657Snate@binkert.org 2507453Snate@binkert.org # ******** Copy constructor ******** 2517453Snate@binkert.org if not self.isGlobal: 2527453Snate@binkert.org code('${{self.c_ident}}(const ${{self.c_ident}}&other)') 2537453Snate@binkert.org 2547453Snate@binkert.org # Call superclass constructor 2557453Snate@binkert.org if "interface" in self: 2567453Snate@binkert.org code(' : ${{self["interface"]}}(other)') 2577453Snate@binkert.org 2587453Snate@binkert.org code('{') 2597453Snate@binkert.org code.indent() 2607453Snate@binkert.org 2617453Snate@binkert.org for dm in self.data_members.values(): 2627453Snate@binkert.org code('m_${{dm.ident}} = other.m_${{dm.ident}};') 2637453Snate@binkert.org 2647453Snate@binkert.org if self.isMessage: 2657453Snate@binkert.org code('proc_id = other.proc_id;') 2667453Snate@binkert.org 2677453Snate@binkert.org code.dedent() 2687453Snate@binkert.org code('}') 2697453Snate@binkert.org 2706657Snate@binkert.org # ******** Full init constructor ******** 2716657Snate@binkert.org if not self.isGlobal: 2726657Snate@binkert.org params = [ 'const %s& local_%s' % (dm.type.c_ident, dm.ident) \ 2736657Snate@binkert.org for dm in self.data_members.itervalues() ] 2746657Snate@binkert.org 2756657Snate@binkert.org if self.isMessage: 2766657Snate@binkert.org params.append('const unsigned local_proc_id') 2776657Snate@binkert.org 2786657Snate@binkert.org params = ', '.join(params) 2796657Snate@binkert.org code('${{self.c_ident}}($params)') 2806657Snate@binkert.org 2816657Snate@binkert.org # Call superclass constructor 2826657Snate@binkert.org if "interface" in self: 2836657Snate@binkert.org code(' : ${{self["interface"]}}()') 2846657Snate@binkert.org 2856657Snate@binkert.org code('{') 2866657Snate@binkert.org code.indent() 2876657Snate@binkert.org for dm in self.data_members.values(): 2886657Snate@binkert.org code('m_${{dm.ident}} = local_${{dm.ident}};') 2896657Snate@binkert.org if "nextLineCallHack" in dm: 2906657Snate@binkert.org code('m_${{dm.ident}}${{dm["nextLineCallHack"]}};') 2916657Snate@binkert.org 2926657Snate@binkert.org if self.isMessage: 2936657Snate@binkert.org code('proc_id = local_proc_id;') 2946657Snate@binkert.org 2956657Snate@binkert.org code.dedent() 2966657Snate@binkert.org code('}') 2976657Snate@binkert.org 2987453Snate@binkert.org # create a static factory method and a clone member 2997453Snate@binkert.org code(''' 3007453Snate@binkert.orgstatic ${{self.c_ident}}* 3017007Snate@binkert.orgcreate() 3027007Snate@binkert.org{ 3036657Snate@binkert.org return new ${{self.c_ident}}(); 3046657Snate@binkert.org} 3056657Snate@binkert.org 3067453Snate@binkert.org${{self.c_ident}}* 3077007Snate@binkert.orgclone() const 3087007Snate@binkert.org{ 3097453Snate@binkert.org return new ${{self.c_ident}}(*this); 3107007Snate@binkert.org} 3116657Snate@binkert.org''') 3126657Snate@binkert.org 3136657Snate@binkert.org if not self.isGlobal: 3146657Snate@binkert.org # const Get methods for each field 3156657Snate@binkert.org code('// Const accessors methods for each field') 3166657Snate@binkert.org for dm in self.data_members.values(): 3176657Snate@binkert.org code(''' 3186657Snate@binkert.org/** \\brief Const accessor method for ${{dm.ident}} field. 3196657Snate@binkert.org * \\return ${{dm.ident}} field 3206657Snate@binkert.org */ 3217007Snate@binkert.orgconst ${{dm.type.c_ident}}& 3227007Snate@binkert.orgget${{dm.ident}}() const 3237007Snate@binkert.org{ 3247007Snate@binkert.org return m_${{dm.ident}}; 3257007Snate@binkert.org} 3266657Snate@binkert.org''') 3276657Snate@binkert.org 3286657Snate@binkert.org # Non-const Get methods for each field 3296657Snate@binkert.org code('// Non const Accessors methods for each field') 3306657Snate@binkert.org for dm in self.data_members.values(): 3316657Snate@binkert.org code(''' 3326657Snate@binkert.org/** \\brief Non-const accessor method for ${{dm.ident}} field. 3336657Snate@binkert.org * \\return ${{dm.ident}} field 3346657Snate@binkert.org */ 3357007Snate@binkert.org${{dm.type.c_ident}}& 3367007Snate@binkert.orgget${{dm.ident}}() 3377007Snate@binkert.org{ 3387007Snate@binkert.org return m_${{dm.ident}}; 3397007Snate@binkert.org} 3406657Snate@binkert.org''') 3416657Snate@binkert.org 3426657Snate@binkert.org #Set methods for each field 3436657Snate@binkert.org code('// Mutator methods for each field') 3446657Snate@binkert.org for dm in self.data_members.values(): 3456657Snate@binkert.org code(''' 3466657Snate@binkert.org/** \\brief Mutator method for ${{dm.ident}} field */ 3477007Snate@binkert.orgvoid 3487007Snate@binkert.orgset${{dm.ident}}(const ${{dm.type.c_ident}}& local_${{dm.ident}}) 3497007Snate@binkert.org{ 3507007Snate@binkert.org m_${{dm.ident}} = local_${{dm.ident}}; 3517007Snate@binkert.org} 3526657Snate@binkert.org''') 3536657Snate@binkert.org 3547002Snate@binkert.org code('void print(std::ostream& out) const;') 3556657Snate@binkert.org code.dedent() 3566657Snate@binkert.org code(' //private:') 3576657Snate@binkert.org code.indent() 3586657Snate@binkert.org 3596657Snate@binkert.org # Data members for each field 3606657Snate@binkert.org for dm in self.data_members.values(): 3616657Snate@binkert.org if "abstract" not in dm: 3626657Snate@binkert.org const = "" 3636657Snate@binkert.org init = "" 3646657Snate@binkert.org 3656657Snate@binkert.org # global structure 3666657Snate@binkert.org if self.isGlobal: 3676657Snate@binkert.org const = "static const " 3686657Snate@binkert.org 3696657Snate@binkert.org # init value 3706657Snate@binkert.org if dm.init_code: 3716657Snate@binkert.org # only global structure can have init value here 3726657Snate@binkert.org assert self.isGlobal 3736657Snate@binkert.org init = " = %s" % (dm.init_code) 3746657Snate@binkert.org 3756657Snate@binkert.org if "desc" in dm: 3767007Snate@binkert.org code('/** ${{dm["desc"]}} */') 3776657Snate@binkert.org 3787007Snate@binkert.org code('$const${{dm.type.c_ident}} m_${{dm.ident}}$init;') 3796657Snate@binkert.org 3806657Snate@binkert.org if self.isMessage: 3816657Snate@binkert.org code('unsigned proc_id;') 3826657Snate@binkert.org 3836657Snate@binkert.org code.dedent() 3846657Snate@binkert.org code('};') 3856657Snate@binkert.org 3866657Snate@binkert.org code(''' 3877055Snate@binkert.orginline std::ostream& 3887007Snate@binkert.orgoperator<<(std::ostream& out, const ${{self.c_ident}}& obj) 3896657Snate@binkert.org{ 3906657Snate@binkert.org obj.print(out); 3917002Snate@binkert.org out << std::flush; 3926657Snate@binkert.org return out; 3936657Snate@binkert.org} 3946657Snate@binkert.org 3957007Snate@binkert.org#endif // __${{self.c_ident}}_HH__ 3966657Snate@binkert.org''') 3976657Snate@binkert.org 3986657Snate@binkert.org code.write(path, "%s.hh" % self.c_ident) 3996657Snate@binkert.org 4006657Snate@binkert.org def printTypeCC(self, path): 4016999Snate@binkert.org code = self.symtab.codeFormatter() 4026657Snate@binkert.org 4036657Snate@binkert.org code(''' 4046657Snate@binkert.org/** \\file ${{self.c_ident}}.cc 4056657Snate@binkert.org * 4066657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__ 4076657Snate@binkert.org */ 4086657Snate@binkert.org 4097002Snate@binkert.org#include <iostream> 4107002Snate@binkert.org 4116657Snate@binkert.org#include "mem/protocol/${{self.c_ident}}.hh" 4127002Snate@binkert.org 4137002Snate@binkert.orgusing namespace std; 4146657Snate@binkert.org''') 4156657Snate@binkert.org 4166657Snate@binkert.org code(''' 4176657Snate@binkert.org/** \\brief Print the state of this object */ 4187007Snate@binkert.orgvoid 4197007Snate@binkert.org${{self.c_ident}}::print(ostream& out) const 4206657Snate@binkert.org{ 4216657Snate@binkert.org out << "[${{self.c_ident}}: "; 4226657Snate@binkert.org''') 4236657Snate@binkert.org 4246657Snate@binkert.org # For each field 4256657Snate@binkert.org code.indent() 4266657Snate@binkert.org for dm in self.data_members.values(): 4276657Snate@binkert.org code('out << "${{dm.ident}} = " << m_${{dm.ident}} << " ";''') 4286657Snate@binkert.org 4296657Snate@binkert.org if self.isMessage: 4306657Snate@binkert.org code('out << "Time = " << getTime() << " ";') 4316657Snate@binkert.org code.dedent() 4326657Snate@binkert.org 4336657Snate@binkert.org # Trailer 4346657Snate@binkert.org code(''' 4356657Snate@binkert.org out << "]"; 4366657Snate@binkert.org}''') 4376657Snate@binkert.org 4386657Snate@binkert.org code.write(path, "%s.cc" % self.c_ident) 4396657Snate@binkert.org 4406657Snate@binkert.org def printEnumHH(self, path): 4416999Snate@binkert.org code = self.symtab.codeFormatter() 4426657Snate@binkert.org code(''' 4436657Snate@binkert.org/** \\file ${{self.c_ident}}.hh 4446657Snate@binkert.org * 4456657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__ 4466657Snate@binkert.org */ 4477007Snate@binkert.org 4487007Snate@binkert.org#ifndef __${{self.c_ident}}_HH__ 4497007Snate@binkert.org#define __${{self.c_ident}}_HH__ 4506657Snate@binkert.org 4517002Snate@binkert.org#include <iostream> 4527002Snate@binkert.org#include <string> 4537002Snate@binkert.org 4546657Snate@binkert.org#include "mem/ruby/common/Global.hh" 4558086SBrad.Beckmann@amd.com''') 4568086SBrad.Beckmann@amd.com if self.isStateDecl: 4578086SBrad.Beckmann@amd.com code('#include "mem/protocol/AccessPermission.hh"') 4588086SBrad.Beckmann@amd.com 4598086SBrad.Beckmann@amd.com code(''' 4606657Snate@binkert.org 4617007Snate@binkert.org// Class definition 4626657Snate@binkert.org/** \\enum ${{self.c_ident}} 4636657Snate@binkert.org * \\brief ${{self.desc}} 4646657Snate@binkert.org */ 4656657Snate@binkert.orgenum ${{self.c_ident}} { 4666657Snate@binkert.org ${{self.c_ident}}_FIRST, 4676657Snate@binkert.org''') 4686657Snate@binkert.org 4696657Snate@binkert.org code.indent() 4706657Snate@binkert.org # For each field 4716657Snate@binkert.org for i,(ident,enum) in enumerate(self.enums.iteritems()): 4726657Snate@binkert.org desc = enum.get("desc", "No description avaliable") 4736862Sdrh5@cs.wisc.edu if i == 0: 4746862Sdrh5@cs.wisc.edu init = ' = %s_FIRST' % self.c_ident 4756862Sdrh5@cs.wisc.edu else: 4766862Sdrh5@cs.wisc.edu init = '' 4776657Snate@binkert.org code('${{self.c_ident}}_${{enum.ident}}$init, /**< $desc */') 4786657Snate@binkert.org code.dedent() 4796657Snate@binkert.org code(''' 4806657Snate@binkert.org ${{self.c_ident}}_NUM 4816657Snate@binkert.org}; 4827007Snate@binkert.org 4837007Snate@binkert.org// Code to convert from a string to the enumeration 4847002Snate@binkert.org${{self.c_ident}} string_to_${{self.c_ident}}(const std::string& str); 4857007Snate@binkert.org 4867007Snate@binkert.org// Code to convert state to a string 4877002Snate@binkert.orgstd::string ${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj); 4887007Snate@binkert.org 4897007Snate@binkert.org// Code to increment an enumeration type 4906657Snate@binkert.org${{self.c_ident}} &operator++(${{self.c_ident}} &e); 4916657Snate@binkert.org''') 4926657Snate@binkert.org 4936657Snate@binkert.org # MachineType hack used to set the base component id for each Machine 4946657Snate@binkert.org if self.isMachineType: 4956657Snate@binkert.org code(''' 4966657Snate@binkert.orgint ${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj); 4976657Snate@binkert.orgMachineType ${{self.c_ident}}_from_base_level(int); 4986657Snate@binkert.orgint ${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj); 4996657Snate@binkert.orgint ${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj); 5006657Snate@binkert.org''') 5016657Snate@binkert.org 5026657Snate@binkert.org for enum in self.enums.itervalues(): 5036657Snate@binkert.org code('#define MACHINETYPE_${{enum.ident}} 1') 5046657Snate@binkert.org 5058086SBrad.Beckmann@amd.com if self.isStateDecl: 5068086SBrad.Beckmann@amd.com code(''' 5078086SBrad.Beckmann@amd.com 5088086SBrad.Beckmann@amd.com// Code to convert the current state to an access permission 5098086SBrad.Beckmann@amd.comAccessPermission ${{self.c_ident}}_to_permission(const ${{self.c_ident}}& obj); 5108086SBrad.Beckmann@amd.com 5118086SBrad.Beckmann@amd.com''') 5128086SBrad.Beckmann@amd.com 5136657Snate@binkert.org # Trailer 5146657Snate@binkert.org code(''' 5157002Snate@binkert.orgstd::ostream& operator<<(std::ostream& out, const ${{self.c_ident}}& obj); 5166657Snate@binkert.org 5177007Snate@binkert.org#endif // __${{self.c_ident}}_HH__ 5186657Snate@binkert.org''') 5196657Snate@binkert.org 5206657Snate@binkert.org code.write(path, "%s.hh" % self.c_ident) 5216657Snate@binkert.org 5226657Snate@binkert.org def printEnumCC(self, path): 5236999Snate@binkert.org code = self.symtab.codeFormatter() 5246657Snate@binkert.org code(''' 5256657Snate@binkert.org/** \\file ${{self.c_ident}}.hh 5266657Snate@binkert.org * 5276657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__ 5286657Snate@binkert.org */ 5296657Snate@binkert.org 5307832Snate@binkert.org#include <cassert> 5317002Snate@binkert.org#include <iostream> 5327002Snate@binkert.org#include <string> 5337002Snate@binkert.org 5347805Snilay@cs.wisc.edu#include "base/misc.hh" 5356657Snate@binkert.org#include "mem/protocol/${{self.c_ident}}.hh" 5366657Snate@binkert.org 5377002Snate@binkert.orgusing namespace std; 5387002Snate@binkert.org 5396657Snate@binkert.org''') 5406657Snate@binkert.org 5418086SBrad.Beckmann@amd.com if self.isStateDecl: 5428086SBrad.Beckmann@amd.com code(''' 5438086SBrad.Beckmann@amd.com// Code to convert the current state to an access permission 5448086SBrad.Beckmann@amd.comAccessPermission ${{self.c_ident}}_to_permission(const ${{self.c_ident}}& obj) 5458086SBrad.Beckmann@amd.com{ 5468086SBrad.Beckmann@amd.com switch(obj) { 5478086SBrad.Beckmann@amd.com''') 5488086SBrad.Beckmann@amd.com # For each case 5498086SBrad.Beckmann@amd.com code.indent() 5508086SBrad.Beckmann@amd.com for statePerm in self.statePermPairs: 5518086SBrad.Beckmann@amd.com code(' case ${{self.c_ident}}_${{statePerm[0]}}:') 5528086SBrad.Beckmann@amd.com code(' return AccessPermission_${{statePerm[1]}};') 5538086SBrad.Beckmann@amd.com code.dedent() 5548086SBrad.Beckmann@amd.com code (''' 5558086SBrad.Beckmann@amd.com default: 5568086SBrad.Beckmann@amd.com panic("Unknown state access permission converstion for ${{self.c_ident}}"); 5578086SBrad.Beckmann@amd.com } 5588086SBrad.Beckmann@amd.com} 5598086SBrad.Beckmann@amd.com 5608086SBrad.Beckmann@amd.com''') 5618086SBrad.Beckmann@amd.com 5626657Snate@binkert.org if self.isMachineType: 5636657Snate@binkert.org for enum in self.enums.itervalues(): 5646657Snate@binkert.org code('#include "mem/protocol/${{enum.ident}}_Controller.hh"') 5656657Snate@binkert.org 5666657Snate@binkert.org code(''' 5677007Snate@binkert.org// Code for output operator 5687007Snate@binkert.orgostream& 5697007Snate@binkert.orgoperator<<(ostream& out, const ${{self.c_ident}}& obj) 5706657Snate@binkert.org{ 5716657Snate@binkert.org out << ${{self.c_ident}}_to_string(obj); 5726657Snate@binkert.org out << flush; 5736657Snate@binkert.org return out; 5746657Snate@binkert.org} 5756657Snate@binkert.org 5767007Snate@binkert.org// Code to convert state to a string 5777007Snate@binkert.orgstring 5787007Snate@binkert.org${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj) 5796657Snate@binkert.org{ 5806657Snate@binkert.org switch(obj) { 5816657Snate@binkert.org''') 5826657Snate@binkert.org 5836657Snate@binkert.org # For each field 5846657Snate@binkert.org code.indent() 5856657Snate@binkert.org for enum in self.enums.itervalues(): 5866657Snate@binkert.org code(' case ${{self.c_ident}}_${{enum.ident}}:') 5876657Snate@binkert.org code(' return "${{enum.ident}}";') 5886657Snate@binkert.org code.dedent() 5896657Snate@binkert.org 5906657Snate@binkert.org # Trailer 5916657Snate@binkert.org code(''' 5926657Snate@binkert.org default: 5937805Snilay@cs.wisc.edu panic("Invalid range for type ${{self.c_ident}}"); 5946657Snate@binkert.org } 5956657Snate@binkert.org} 5966657Snate@binkert.org 5977007Snate@binkert.org// Code to convert from a string to the enumeration 5987007Snate@binkert.org${{self.c_ident}} 5997007Snate@binkert.orgstring_to_${{self.c_ident}}(const string& str) 6006657Snate@binkert.org{ 6016657Snate@binkert.org''') 6026657Snate@binkert.org 6036657Snate@binkert.org # For each field 6047007Snate@binkert.org start = "" 6056657Snate@binkert.org code.indent() 6066657Snate@binkert.org for enum in self.enums.itervalues(): 6076657Snate@binkert.org code('${start}if (str == "${{enum.ident}}") {') 6086657Snate@binkert.org code(' return ${{self.c_ident}}_${{enum.ident}};') 6097007Snate@binkert.org start = "} else " 6106657Snate@binkert.org code.dedent() 6116657Snate@binkert.org 6126657Snate@binkert.org code(''' 6136657Snate@binkert.org } else { 6147805Snilay@cs.wisc.edu panic("Invalid string conversion for %s, type ${{self.c_ident}}", str); 6156657Snate@binkert.org } 6166657Snate@binkert.org} 6176657Snate@binkert.org 6187007Snate@binkert.org// Code to increment an enumeration type 6197007Snate@binkert.org${{self.c_ident}}& 6207007Snate@binkert.orgoperator++(${{self.c_ident}}& e) 6217007Snate@binkert.org{ 6226657Snate@binkert.org assert(e < ${{self.c_ident}}_NUM); 6236657Snate@binkert.org return e = ${{self.c_ident}}(e+1); 6246657Snate@binkert.org} 6256657Snate@binkert.org''') 6266657Snate@binkert.org 6276657Snate@binkert.org # MachineType hack used to set the base level and number of 6286657Snate@binkert.org # components for each Machine 6296657Snate@binkert.org if self.isMachineType: 6306657Snate@binkert.org code(''' 6317007Snate@binkert.org/** \\brief returns the base vector index for each machine type to be 6327007Snate@binkert.org * used by NetDest 6336657Snate@binkert.org * 6346657Snate@binkert.org * \\return the base vector index for each machine type to be used by NetDest 6356657Snate@binkert.org * \\see NetDest.hh 6366657Snate@binkert.org */ 6377007Snate@binkert.orgint 6387007Snate@binkert.org${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj) 6396657Snate@binkert.org{ 6406657Snate@binkert.org switch(obj) { 6416657Snate@binkert.org''') 6426657Snate@binkert.org 6436657Snate@binkert.org # For each field 6446657Snate@binkert.org code.indent() 6456657Snate@binkert.org for i,enum in enumerate(self.enums.itervalues()): 6466657Snate@binkert.org code(' case ${{self.c_ident}}_${{enum.ident}}:') 6476657Snate@binkert.org code(' return $i;') 6486657Snate@binkert.org code.dedent() 6496657Snate@binkert.org 6506657Snate@binkert.org # total num 6516657Snate@binkert.org code(''' 6526657Snate@binkert.org case ${{self.c_ident}}_NUM: 6536657Snate@binkert.org return ${{len(self.enums)}}; 6546657Snate@binkert.org 6556657Snate@binkert.org default: 6567805Snilay@cs.wisc.edu panic("Invalid range for type ${{self.c_ident}}"); 6576657Snate@binkert.org } 6586657Snate@binkert.org} 6596657Snate@binkert.org 6606657Snate@binkert.org/** \\brief returns the machine type for each base vector index used by NetDest 6616657Snate@binkert.org * 6627007Snate@binkert.org * \\return the MachineType 6636657Snate@binkert.org */ 6647007Snate@binkert.orgMachineType 6657007Snate@binkert.org${{self.c_ident}}_from_base_level(int type) 6666657Snate@binkert.org{ 6676657Snate@binkert.org switch(type) { 6686657Snate@binkert.org''') 6696657Snate@binkert.org 6706657Snate@binkert.org # For each field 6716657Snate@binkert.org code.indent() 6726657Snate@binkert.org for i,enum in enumerate(self.enums.itervalues()): 6736657Snate@binkert.org code(' case $i:') 6746657Snate@binkert.org code(' return ${{self.c_ident}}_${{enum.ident}};') 6756657Snate@binkert.org code.dedent() 6766657Snate@binkert.org 6776657Snate@binkert.org # Trailer 6786657Snate@binkert.org code(''' 6796657Snate@binkert.org default: 6807805Snilay@cs.wisc.edu panic("Invalid range for type ${{self.c_ident}}"); 6816657Snate@binkert.org } 6826657Snate@binkert.org} 6836657Snate@binkert.org 6846657Snate@binkert.org/** \\brief The return value indicates the number of components created 6856657Snate@binkert.org * before a particular machine\'s components 6866657Snate@binkert.org * 6876657Snate@binkert.org * \\return the base number of components for each machine 6886657Snate@binkert.org */ 6897007Snate@binkert.orgint 6907007Snate@binkert.org${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj) 6916657Snate@binkert.org{ 6926657Snate@binkert.org int base = 0; 6936657Snate@binkert.org switch(obj) { 6946657Snate@binkert.org''') 6956657Snate@binkert.org 6966657Snate@binkert.org # For each field 6976657Snate@binkert.org code.indent() 6986657Snate@binkert.org code(' case ${{self.c_ident}}_NUM:') 6996657Snate@binkert.org for enum in reversed(self.enums.values()): 7006657Snate@binkert.org code(' base += ${{enum.ident}}_Controller::getNumControllers();') 7016657Snate@binkert.org code(' case ${{self.c_ident}}_${{enum.ident}}:') 7026657Snate@binkert.org code(' break;') 7036657Snate@binkert.org code.dedent() 7046657Snate@binkert.org 7056657Snate@binkert.org code(''' 7066657Snate@binkert.org default: 7077805Snilay@cs.wisc.edu panic("Invalid range for type ${{self.c_ident}}"); 7086657Snate@binkert.org } 7096657Snate@binkert.org 7106657Snate@binkert.org return base; 7116657Snate@binkert.org} 7126657Snate@binkert.org 7136657Snate@binkert.org/** \\brief returns the total number of components for each machine 7146657Snate@binkert.org * \\return the total number of components for each machine 7156657Snate@binkert.org */ 7167007Snate@binkert.orgint 7177007Snate@binkert.org${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj) 7186657Snate@binkert.org{ 7196657Snate@binkert.org switch(obj) { 7206657Snate@binkert.org''') 7216657Snate@binkert.org 7226657Snate@binkert.org # For each field 7236657Snate@binkert.org for enum in self.enums.itervalues(): 7246657Snate@binkert.org code(''' 7256657Snate@binkert.org case ${{self.c_ident}}_${{enum.ident}}: 7266657Snate@binkert.org return ${{enum.ident}}_Controller::getNumControllers(); 7276657Snate@binkert.org''') 7286657Snate@binkert.org 7296657Snate@binkert.org # total num 7306657Snate@binkert.org code(''' 7316657Snate@binkert.org case ${{self.c_ident}}_NUM: 7326657Snate@binkert.org default: 7337805Snilay@cs.wisc.edu panic("Invalid range for type ${{self.c_ident}}"); 7346657Snate@binkert.org } 7356657Snate@binkert.org} 7366657Snate@binkert.org''') 7376657Snate@binkert.org 7386657Snate@binkert.org # Write the file 7396657Snate@binkert.org code.write(path, "%s.cc" % self.c_ident) 7406657Snate@binkert.org 7416657Snate@binkert.org__all__ = [ "Type" ] 742