Type.py revision 9298
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 = {}
1109298Snilay@cs.wisc.edu        self.functions = {}
1116657Snate@binkert.org
1126657Snate@binkert.org        # Enums
1136657Snate@binkert.org        self.enums = orderdict()
1146657Snate@binkert.org
1156657Snate@binkert.org    @property
1166657Snate@binkert.org    def isPrimitive(self):
1176657Snate@binkert.org        return "primitive" in self
1186657Snate@binkert.org    @property
1196657Snate@binkert.org    def isNetworkMessage(self):
1206657Snate@binkert.org        return "networkmessage" in self
1216657Snate@binkert.org    @property
1226657Snate@binkert.org    def isMessage(self):
1236657Snate@binkert.org        return "message" in self
1246657Snate@binkert.org    @property
1256657Snate@binkert.org    def isBuffer(self):
1266657Snate@binkert.org        return "buffer" in self
1276657Snate@binkert.org    @property
1286657Snate@binkert.org    def isInPort(self):
1296657Snate@binkert.org        return "inport" in self
1306657Snate@binkert.org    @property
1316657Snate@binkert.org    def isOutPort(self):
1326657Snate@binkert.org        return "outport" in self
1336657Snate@binkert.org    @property
1346657Snate@binkert.org    def isEnumeration(self):
1356657Snate@binkert.org        return "enumeration" in self
1366657Snate@binkert.org    @property
1376657Snate@binkert.org    def isExternal(self):
1386657Snate@binkert.org        return "external" in self
1396657Snate@binkert.org    @property
1406657Snate@binkert.org    def isGlobal(self):
1416657Snate@binkert.org        return "global" in self
1426657Snate@binkert.org    @property
1436657Snate@binkert.org    def isInterface(self):
1446657Snate@binkert.org        return "interface" in self
1456657Snate@binkert.org
1466657Snate@binkert.org    # Return false on error
1479298Snilay@cs.wisc.edu    def addDataMember(self, ident, type, pairs, init_code):
1486657Snate@binkert.org        if ident in self.data_members:
1496657Snate@binkert.org            return False
1506657Snate@binkert.org
1516657Snate@binkert.org        member = DataMember(ident, type, pairs, init_code)
1526657Snate@binkert.org        self.data_members[ident] = member
1536657Snate@binkert.org
1546657Snate@binkert.org        return True
1556657Snate@binkert.org
1566657Snate@binkert.org    def dataMemberType(self, ident):
1576657Snate@binkert.org        return self.data_members[ident].type
1586657Snate@binkert.org
1596657Snate@binkert.org    def methodId(self, name, param_type_vec):
1606657Snate@binkert.org        return '_'.join([name] + [ pt.c_ident for pt in param_type_vec ])
1616657Snate@binkert.org
1626882SBrad.Beckmann@amd.com    def methodIdAbstract(self, name, param_type_vec):
1636882SBrad.Beckmann@amd.com        return '_'.join([name] + [ pt.abstract_ident for pt in param_type_vec ])
1646882SBrad.Beckmann@amd.com
1658086SBrad.Beckmann@amd.com    def statePermPairAdd(self, state_name, perm_name):
1668086SBrad.Beckmann@amd.com        self.statePermPairs.append([state_name, perm_name])
1678086SBrad.Beckmann@amd.com
1689298Snilay@cs.wisc.edu    def addMethod(self, name, return_type, param_type_vec):
1696657Snate@binkert.org        ident = self.methodId(name, param_type_vec)
1706657Snate@binkert.org        if ident in self.methods:
1716657Snate@binkert.org            return False
1726657Snate@binkert.org
1736657Snate@binkert.org        self.methods[ident] = Method(return_type, param_type_vec)
1746657Snate@binkert.org        return True
1756657Snate@binkert.org
1769298Snilay@cs.wisc.edu    # Ideally either this function or the one above should exist. But
1779298Snilay@cs.wisc.edu    # methods and functions have different structures right now.
1789298Snilay@cs.wisc.edu    # Hence, these are different, at least for the time being.
1799298Snilay@cs.wisc.edu    def addFunc(self, func):
1809298Snilay@cs.wisc.edu        ident = self.methodId(func.ident, func.param_types)
1819298Snilay@cs.wisc.edu        if ident in self.functions:
1829298Snilay@cs.wisc.edu            return False
1839298Snilay@cs.wisc.edu
1849298Snilay@cs.wisc.edu        self.functions[ident] = func
1859298Snilay@cs.wisc.edu        return True
1869298Snilay@cs.wisc.edu
1879298Snilay@cs.wisc.edu    def addEnum(self, ident, pairs):
1886657Snate@binkert.org        if ident in self.enums:
1896657Snate@binkert.org            return False
1906657Snate@binkert.org
1916657Snate@binkert.org        self.enums[ident] = Enumeration(ident, pairs)
1926657Snate@binkert.org
1936657Snate@binkert.org        # Add default
1946657Snate@binkert.org        if "default" not in self:
1956657Snate@binkert.org            self["default"] = "%s_NUM" % self.c_ident
1966657Snate@binkert.org
1976657Snate@binkert.org        return True
1986657Snate@binkert.org
1999219Spower.jg@gmail.com    def writeCodeFiles(self, path, includes):
2006657Snate@binkert.org        if self.isExternal:
2016657Snate@binkert.org            # Do nothing
2026657Snate@binkert.org            pass
2036657Snate@binkert.org        elif self.isEnumeration:
2046657Snate@binkert.org            self.printEnumHH(path)
2056657Snate@binkert.org            self.printEnumCC(path)
2066657Snate@binkert.org        else:
2076657Snate@binkert.org            # User defined structs and messages
2086657Snate@binkert.org            self.printTypeHH(path)
2096657Snate@binkert.org            self.printTypeCC(path)
2106657Snate@binkert.org
2116657Snate@binkert.org    def printTypeHH(self, path):
2126999Snate@binkert.org        code = self.symtab.codeFormatter()
2136657Snate@binkert.org        code('''
2146657Snate@binkert.org/** \\file ${{self.c_ident}}.hh
2156657Snate@binkert.org *
2166657Snate@binkert.org *
2176657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
2186657Snate@binkert.org */
2196657Snate@binkert.org
2207007Snate@binkert.org#ifndef __${{self.c_ident}}_HH__
2217007Snate@binkert.org#define __${{self.c_ident}}_HH__
2226657Snate@binkert.org
2237002Snate@binkert.org#include <iostream>
2247002Snate@binkert.org
2256657Snate@binkert.org#include "mem/ruby/common/Global.hh"
2266657Snate@binkert.org''')
2276657Snate@binkert.org
2286657Snate@binkert.org        for dm in self.data_members.values():
2296657Snate@binkert.org            if not dm.type.isPrimitive:
2306657Snate@binkert.org                code('#include "mem/protocol/$0.hh"', dm.type.c_ident)
2316657Snate@binkert.org
2326657Snate@binkert.org        parent = ""
2336657Snate@binkert.org        if "interface" in self:
2346657Snate@binkert.org            code('#include "mem/protocol/$0.hh"', self["interface"])
2356657Snate@binkert.org            parent = " :  public %s" % self["interface"]
2366657Snate@binkert.org
2376657Snate@binkert.org        code('''
2387007Snate@binkert.org$klass ${{self.c_ident}}$parent
2397007Snate@binkert.org{
2406657Snate@binkert.org  public:
2416657Snate@binkert.org    ${{self.c_ident}}()
2427007Snate@binkert.org    {
2436657Snate@binkert.org''', klass="class")
2446657Snate@binkert.org
2456657Snate@binkert.org        code.indent()
2466657Snate@binkert.org        if not self.isGlobal:
2476657Snate@binkert.org            code.indent()
2486657Snate@binkert.org            for dm in self.data_members.values():
2496657Snate@binkert.org                ident = dm.ident
2506657Snate@binkert.org                if "default" in dm:
2516657Snate@binkert.org                    # look for default value
2526657Snate@binkert.org                    code('m_$ident = ${{dm["default"]}}; // default for this field')
2536657Snate@binkert.org                elif "default" in dm.type:
2546657Snate@binkert.org                    # Look for the type default
2556657Snate@binkert.org                    tid = dm.type.c_ident
2566657Snate@binkert.org                    code('m_$ident = ${{dm.type["default"]}}; // default value of $tid')
2576657Snate@binkert.org                else:
2586657Snate@binkert.org                    code('// m_$ident has no default')
2596657Snate@binkert.org            code.dedent()
2606657Snate@binkert.org        code('}')
2616657Snate@binkert.org
2627453Snate@binkert.org        # ******** Copy constructor ********
2637453Snate@binkert.org        if not self.isGlobal:
2647453Snate@binkert.org            code('${{self.c_ident}}(const ${{self.c_ident}}&other)')
2657453Snate@binkert.org
2667453Snate@binkert.org            # Call superclass constructor
2677453Snate@binkert.org            if "interface" in self:
2687453Snate@binkert.org                code('    : ${{self["interface"]}}(other)')
2697453Snate@binkert.org
2707453Snate@binkert.org            code('{')
2717453Snate@binkert.org            code.indent()
2727453Snate@binkert.org
2737453Snate@binkert.org            for dm in self.data_members.values():
2747453Snate@binkert.org                code('m_${{dm.ident}} = other.m_${{dm.ident}};')
2757453Snate@binkert.org
2767453Snate@binkert.org            code.dedent()
2777453Snate@binkert.org            code('}')
2787453Snate@binkert.org
2796657Snate@binkert.org        # ******** Full init constructor ********
2806657Snate@binkert.org        if not self.isGlobal:
2816657Snate@binkert.org            params = [ 'const %s& local_%s' % (dm.type.c_ident, dm.ident) \
2826657Snate@binkert.org                       for dm in self.data_members.itervalues() ]
2836657Snate@binkert.org
2846657Snate@binkert.org            params = ', '.join(params)
2856657Snate@binkert.org            code('${{self.c_ident}}($params)')
2866657Snate@binkert.org
2876657Snate@binkert.org            # Call superclass constructor
2886657Snate@binkert.org            if "interface" in self:
2896657Snate@binkert.org                code('    : ${{self["interface"]}}()')
2906657Snate@binkert.org
2916657Snate@binkert.org            code('{')
2926657Snate@binkert.org            code.indent()
2936657Snate@binkert.org            for dm in self.data_members.values():
2946657Snate@binkert.org                code('m_${{dm.ident}} = local_${{dm.ident}};')
2956657Snate@binkert.org                if "nextLineCallHack" in dm:
2966657Snate@binkert.org                    code('m_${{dm.ident}}${{dm["nextLineCallHack"]}};')
2976657Snate@binkert.org
2986657Snate@binkert.org            code.dedent()
2996657Snate@binkert.org            code('}')
3006657Snate@binkert.org
3017453Snate@binkert.org        # create a static factory method and a clone member
3027453Snate@binkert.org        code('''
3037453Snate@binkert.orgstatic ${{self.c_ident}}*
3047007Snate@binkert.orgcreate()
3057007Snate@binkert.org{
3066657Snate@binkert.org    return new ${{self.c_ident}}();
3076657Snate@binkert.org}
3086657Snate@binkert.org
3097453Snate@binkert.org${{self.c_ident}}*
3107007Snate@binkert.orgclone() const
3117007Snate@binkert.org{
3127453Snate@binkert.org     return new ${{self.c_ident}}(*this);
3137007Snate@binkert.org}
3146657Snate@binkert.org''')
3156657Snate@binkert.org
3166657Snate@binkert.org        if not self.isGlobal:
3176657Snate@binkert.org            # const Get methods for each field
3186657Snate@binkert.org            code('// Const accessors methods for each field')
3196657Snate@binkert.org            for dm in self.data_members.values():
3206657Snate@binkert.org                code('''
3216657Snate@binkert.org/** \\brief Const accessor method for ${{dm.ident}} field.
3226657Snate@binkert.org *  \\return ${{dm.ident}} field
3236657Snate@binkert.org */
3247007Snate@binkert.orgconst ${{dm.type.c_ident}}&
3257007Snate@binkert.orgget${{dm.ident}}() const
3267007Snate@binkert.org{
3277007Snate@binkert.org    return m_${{dm.ident}};
3287007Snate@binkert.org}
3296657Snate@binkert.org''')
3306657Snate@binkert.org
3316657Snate@binkert.org            # Non-const Get methods for each field
3326657Snate@binkert.org            code('// Non const Accessors methods for each field')
3336657Snate@binkert.org            for dm in self.data_members.values():
3346657Snate@binkert.org                code('''
3356657Snate@binkert.org/** \\brief Non-const accessor method for ${{dm.ident}} field.
3366657Snate@binkert.org *  \\return ${{dm.ident}} field
3376657Snate@binkert.org */
3387007Snate@binkert.org${{dm.type.c_ident}}&
3397007Snate@binkert.orgget${{dm.ident}}()
3407007Snate@binkert.org{
3417007Snate@binkert.org    return m_${{dm.ident}};
3427007Snate@binkert.org}
3436657Snate@binkert.org''')
3446657Snate@binkert.org
3456657Snate@binkert.org            #Set methods for each field
3466657Snate@binkert.org            code('// Mutator methods for each field')
3476657Snate@binkert.org            for dm in self.data_members.values():
3486657Snate@binkert.org                code('''
3496657Snate@binkert.org/** \\brief Mutator method for ${{dm.ident}} field */
3507007Snate@binkert.orgvoid
3517007Snate@binkert.orgset${{dm.ident}}(const ${{dm.type.c_ident}}& local_${{dm.ident}})
3527007Snate@binkert.org{
3537007Snate@binkert.org    m_${{dm.ident}} = local_${{dm.ident}};
3547007Snate@binkert.org}
3556657Snate@binkert.org''')
3566657Snate@binkert.org
3577002Snate@binkert.org        code('void print(std::ostream& out) const;')
3586657Snate@binkert.org        code.dedent()
3596657Snate@binkert.org        code('  //private:')
3606657Snate@binkert.org        code.indent()
3616657Snate@binkert.org
3626657Snate@binkert.org        # Data members for each field
3636657Snate@binkert.org        for dm in self.data_members.values():
3646657Snate@binkert.org            if "abstract" not in dm:
3656657Snate@binkert.org                const = ""
3666657Snate@binkert.org                init = ""
3676657Snate@binkert.org
3686657Snate@binkert.org                # global structure
3696657Snate@binkert.org                if self.isGlobal:
3706657Snate@binkert.org                    const = "static const "
3716657Snate@binkert.org
3726657Snate@binkert.org                # init value
3736657Snate@binkert.org                if dm.init_code:
3746657Snate@binkert.org                    # only global structure can have init value here
3756657Snate@binkert.org                    assert self.isGlobal
3766657Snate@binkert.org                    init = " = %s" % (dm.init_code)
3776657Snate@binkert.org
3786657Snate@binkert.org                if "desc" in dm:
3797007Snate@binkert.org                    code('/** ${{dm["desc"]}} */')
3806657Snate@binkert.org
3817007Snate@binkert.org                code('$const${{dm.type.c_ident}} m_${{dm.ident}}$init;')
3826657Snate@binkert.org
3839298Snilay@cs.wisc.edu        # Prototypes for functions defined for the Type
3849298Snilay@cs.wisc.edu        for item in self.functions:
3859298Snilay@cs.wisc.edu            proto = self.functions[item].prototype
3869298Snilay@cs.wisc.edu            if proto:
3879298Snilay@cs.wisc.edu                code('$proto')
3889298Snilay@cs.wisc.edu
3896657Snate@binkert.org        code.dedent()
3906657Snate@binkert.org        code('};')
3916657Snate@binkert.org
3926657Snate@binkert.org        code('''
3937055Snate@binkert.orginline std::ostream&
3947007Snate@binkert.orgoperator<<(std::ostream& out, const ${{self.c_ident}}& obj)
3956657Snate@binkert.org{
3966657Snate@binkert.org    obj.print(out);
3977002Snate@binkert.org    out << std::flush;
3986657Snate@binkert.org    return out;
3996657Snate@binkert.org}
4006657Snate@binkert.org
4017007Snate@binkert.org#endif // __${{self.c_ident}}_HH__
4026657Snate@binkert.org''')
4036657Snate@binkert.org
4046657Snate@binkert.org        code.write(path, "%s.hh" % self.c_ident)
4056657Snate@binkert.org
4066657Snate@binkert.org    def printTypeCC(self, path):
4076999Snate@binkert.org        code = self.symtab.codeFormatter()
4086657Snate@binkert.org
4096657Snate@binkert.org        code('''
4106657Snate@binkert.org/** \\file ${{self.c_ident}}.cc
4116657Snate@binkert.org *
4126657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
4136657Snate@binkert.org */
4146657Snate@binkert.org
4157002Snate@binkert.org#include <iostream>
4167002Snate@binkert.org
4176657Snate@binkert.org#include "mem/protocol/${{self.c_ident}}.hh"
4187002Snate@binkert.org
4197002Snate@binkert.orgusing namespace std;
4206657Snate@binkert.org''')
4216657Snate@binkert.org
4226657Snate@binkert.org        code('''
4236657Snate@binkert.org/** \\brief Print the state of this object */
4247007Snate@binkert.orgvoid
4257007Snate@binkert.org${{self.c_ident}}::print(ostream& out) const
4266657Snate@binkert.org{
4276657Snate@binkert.org    out << "[${{self.c_ident}}: ";
4286657Snate@binkert.org''')
4296657Snate@binkert.org
4306657Snate@binkert.org        # For each field
4316657Snate@binkert.org        code.indent()
4326657Snate@binkert.org        for dm in self.data_members.values():
4336657Snate@binkert.org            code('out << "${{dm.ident}} = " << m_${{dm.ident}} << " ";''')
4346657Snate@binkert.org
4356657Snate@binkert.org        if self.isMessage:
4369206Snilay@cs.wisc.edu            code('out << "Time = " << g_system_ptr->clockPeriod() * getTime() << " ";')
4376657Snate@binkert.org        code.dedent()
4386657Snate@binkert.org
4396657Snate@binkert.org        # Trailer
4406657Snate@binkert.org        code('''
4416657Snate@binkert.org    out << "]";
4426657Snate@binkert.org}''')
4436657Snate@binkert.org
4449298Snilay@cs.wisc.edu        # print the code for the functions in the type
4459298Snilay@cs.wisc.edu        for item in self.functions:
4469298Snilay@cs.wisc.edu            code(self.functions[item].generateCode())
4479298Snilay@cs.wisc.edu
4486657Snate@binkert.org        code.write(path, "%s.cc" % self.c_ident)
4496657Snate@binkert.org
4506657Snate@binkert.org    def printEnumHH(self, path):
4516999Snate@binkert.org        code = self.symtab.codeFormatter()
4526657Snate@binkert.org        code('''
4536657Snate@binkert.org/** \\file ${{self.c_ident}}.hh
4546657Snate@binkert.org *
4556657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
4566657Snate@binkert.org */
4577007Snate@binkert.org
4587007Snate@binkert.org#ifndef __${{self.c_ident}}_HH__
4597007Snate@binkert.org#define __${{self.c_ident}}_HH__
4606657Snate@binkert.org
4617002Snate@binkert.org#include <iostream>
4627002Snate@binkert.org#include <string>
4637002Snate@binkert.org
4648086SBrad.Beckmann@amd.com''')
4658086SBrad.Beckmann@amd.com        if self.isStateDecl:
4668086SBrad.Beckmann@amd.com            code('#include "mem/protocol/AccessPermission.hh"')
4678086SBrad.Beckmann@amd.com
4688602Snilay@cs.wisc.edu        if self.isMachineType:
4698602Snilay@cs.wisc.edu            code('#include "base/misc.hh"')
4708602Snilay@cs.wisc.edu            code('#include "mem/protocol/GenericMachineType.hh"')
4718602Snilay@cs.wisc.edu            code('#include "mem/ruby/common/Address.hh"')
4728602Snilay@cs.wisc.edu            code('struct MachineID;')
4738602Snilay@cs.wisc.edu
4748086SBrad.Beckmann@amd.com        code('''
4756657Snate@binkert.org
4767007Snate@binkert.org// Class definition
4776657Snate@binkert.org/** \\enum ${{self.c_ident}}
4786657Snate@binkert.org *  \\brief ${{self.desc}}
4796657Snate@binkert.org */
4806657Snate@binkert.orgenum ${{self.c_ident}} {
4816657Snate@binkert.org    ${{self.c_ident}}_FIRST,
4826657Snate@binkert.org''')
4836657Snate@binkert.org
4846657Snate@binkert.org        code.indent()
4856657Snate@binkert.org        # For each field
4866657Snate@binkert.org        for i,(ident,enum) in enumerate(self.enums.iteritems()):
4876657Snate@binkert.org            desc = enum.get("desc", "No description avaliable")
4886862Sdrh5@cs.wisc.edu            if i == 0:
4896862Sdrh5@cs.wisc.edu                init = ' = %s_FIRST' % self.c_ident
4906862Sdrh5@cs.wisc.edu            else:
4916862Sdrh5@cs.wisc.edu                init = ''
4926657Snate@binkert.org            code('${{self.c_ident}}_${{enum.ident}}$init, /**< $desc */')
4936657Snate@binkert.org        code.dedent()
4946657Snate@binkert.org        code('''
4956657Snate@binkert.org    ${{self.c_ident}}_NUM
4966657Snate@binkert.org};
4977007Snate@binkert.org
4987007Snate@binkert.org// Code to convert from a string to the enumeration
4997002Snate@binkert.org${{self.c_ident}} string_to_${{self.c_ident}}(const std::string& str);
5007007Snate@binkert.org
5017007Snate@binkert.org// Code to convert state to a string
5027002Snate@binkert.orgstd::string ${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj);
5037007Snate@binkert.org
5047007Snate@binkert.org// Code to increment an enumeration type
5056657Snate@binkert.org${{self.c_ident}} &operator++(${{self.c_ident}} &e);
5066657Snate@binkert.org''')
5076657Snate@binkert.org
5086657Snate@binkert.org        # MachineType hack used to set the base component id for each Machine
5096657Snate@binkert.org        if self.isMachineType:
5106657Snate@binkert.org            code('''
5116657Snate@binkert.orgint ${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj);
5126657Snate@binkert.orgMachineType ${{self.c_ident}}_from_base_level(int);
5136657Snate@binkert.orgint ${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj);
5146657Snate@binkert.orgint ${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj);
5156657Snate@binkert.org''')
5166657Snate@binkert.org
5176657Snate@binkert.org            for enum in self.enums.itervalues():
5188602Snilay@cs.wisc.edu                if enum.ident == "DMA":
5198602Snilay@cs.wisc.edu                    code('''
5208602Snilay@cs.wisc.eduMachineID map_Address_to_DMA(const Address &addr);
5218602Snilay@cs.wisc.edu''')
5228602Snilay@cs.wisc.edu                code('''
5238602Snilay@cs.wisc.edu
5248602Snilay@cs.wisc.eduMachineID get${{enum.ident}}MachineID(NodeID RubyNode);
5258602Snilay@cs.wisc.edu''')
5268602Snilay@cs.wisc.edu
5278602Snilay@cs.wisc.edu            code('''
5288602Snilay@cs.wisc.eduinline GenericMachineType
5298602Snilay@cs.wisc.eduConvertMachToGenericMach(MachineType machType)
5308602Snilay@cs.wisc.edu{
5318602Snilay@cs.wisc.edu''')
5328602Snilay@cs.wisc.edu            for enum in self.enums.itervalues():
5338602Snilay@cs.wisc.edu                code('''
5348602Snilay@cs.wisc.edu      if (machType == MachineType_${{enum.ident}})
5358602Snilay@cs.wisc.edu          return GenericMachineType_${{enum.ident}};
5368602Snilay@cs.wisc.edu''')
5378602Snilay@cs.wisc.edu            code('''
5388602Snilay@cs.wisc.edu      panic("cannot convert to a GenericMachineType");
5398602Snilay@cs.wisc.edu}
5408602Snilay@cs.wisc.edu''')
5416657Snate@binkert.org
5428086SBrad.Beckmann@amd.com        if self.isStateDecl:
5438086SBrad.Beckmann@amd.com            code('''
5448086SBrad.Beckmann@amd.com
5458086SBrad.Beckmann@amd.com// Code to convert the current state to an access permission
5468086SBrad.Beckmann@amd.comAccessPermission ${{self.c_ident}}_to_permission(const ${{self.c_ident}}& obj);
5478086SBrad.Beckmann@amd.com
5488086SBrad.Beckmann@amd.com''')
5498086SBrad.Beckmann@amd.com
5506657Snate@binkert.org        # Trailer
5516657Snate@binkert.org        code('''
5527002Snate@binkert.orgstd::ostream& operator<<(std::ostream& out, const ${{self.c_ident}}& obj);
5536657Snate@binkert.org
5547007Snate@binkert.org#endif // __${{self.c_ident}}_HH__
5556657Snate@binkert.org''')
5566657Snate@binkert.org
5576657Snate@binkert.org        code.write(path, "%s.hh" % self.c_ident)
5586657Snate@binkert.org
5596657Snate@binkert.org    def printEnumCC(self, path):
5606999Snate@binkert.org        code = self.symtab.codeFormatter()
5616657Snate@binkert.org        code('''
5626657Snate@binkert.org/** \\file ${{self.c_ident}}.hh
5636657Snate@binkert.org *
5646657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
5656657Snate@binkert.org */
5666657Snate@binkert.org
5677832Snate@binkert.org#include <cassert>
5687002Snate@binkert.org#include <iostream>
5697002Snate@binkert.org#include <string>
5707002Snate@binkert.org
5717805Snilay@cs.wisc.edu#include "base/misc.hh"
5726657Snate@binkert.org#include "mem/protocol/${{self.c_ident}}.hh"
5736657Snate@binkert.org
5747002Snate@binkert.orgusing namespace std;
5757002Snate@binkert.org
5766657Snate@binkert.org''')
5776657Snate@binkert.org
5788086SBrad.Beckmann@amd.com        if self.isStateDecl:
5798086SBrad.Beckmann@amd.com            code('''
5808086SBrad.Beckmann@amd.com// Code to convert the current state to an access permission
5818086SBrad.Beckmann@amd.comAccessPermission ${{self.c_ident}}_to_permission(const ${{self.c_ident}}& obj)
5828086SBrad.Beckmann@amd.com{
5838086SBrad.Beckmann@amd.com    switch(obj) {
5848086SBrad.Beckmann@amd.com''')
5858086SBrad.Beckmann@amd.com            # For each case
5868086SBrad.Beckmann@amd.com            code.indent()
5878086SBrad.Beckmann@amd.com            for statePerm in self.statePermPairs:
5888086SBrad.Beckmann@amd.com                code('  case ${{self.c_ident}}_${{statePerm[0]}}:')
5898086SBrad.Beckmann@amd.com                code('    return AccessPermission_${{statePerm[1]}};')
5908086SBrad.Beckmann@amd.com            code.dedent()
5918086SBrad.Beckmann@amd.com            code ('''
5928086SBrad.Beckmann@amd.com      default:
5938086SBrad.Beckmann@amd.com        panic("Unknown state access permission converstion for ${{self.c_ident}}");
5948086SBrad.Beckmann@amd.com    }
5958086SBrad.Beckmann@amd.com}
5968086SBrad.Beckmann@amd.com
5978086SBrad.Beckmann@amd.com''')
5988086SBrad.Beckmann@amd.com
5996657Snate@binkert.org        if self.isMachineType:
6006657Snate@binkert.org            for enum in self.enums.itervalues():
6016657Snate@binkert.org                code('#include "mem/protocol/${{enum.ident}}_Controller.hh"')
6028602Snilay@cs.wisc.edu            code('#include "mem/ruby/system/MachineID.hh"')
6036657Snate@binkert.org
6046657Snate@binkert.org        code('''
6057007Snate@binkert.org// Code for output operator
6067007Snate@binkert.orgostream&
6077007Snate@binkert.orgoperator<<(ostream& out, const ${{self.c_ident}}& obj)
6086657Snate@binkert.org{
6096657Snate@binkert.org    out << ${{self.c_ident}}_to_string(obj);
6106657Snate@binkert.org    out << flush;
6116657Snate@binkert.org    return out;
6126657Snate@binkert.org}
6136657Snate@binkert.org
6147007Snate@binkert.org// Code to convert state to a string
6157007Snate@binkert.orgstring
6167007Snate@binkert.org${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj)
6176657Snate@binkert.org{
6186657Snate@binkert.org    switch(obj) {
6196657Snate@binkert.org''')
6206657Snate@binkert.org
6216657Snate@binkert.org        # For each field
6226657Snate@binkert.org        code.indent()
6236657Snate@binkert.org        for enum in self.enums.itervalues():
6246657Snate@binkert.org            code('  case ${{self.c_ident}}_${{enum.ident}}:')
6256657Snate@binkert.org            code('    return "${{enum.ident}}";')
6266657Snate@binkert.org        code.dedent()
6276657Snate@binkert.org
6286657Snate@binkert.org        # Trailer
6296657Snate@binkert.org        code('''
6306657Snate@binkert.org      default:
6317805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
6326657Snate@binkert.org    }
6336657Snate@binkert.org}
6346657Snate@binkert.org
6357007Snate@binkert.org// Code to convert from a string to the enumeration
6367007Snate@binkert.org${{self.c_ident}}
6377007Snate@binkert.orgstring_to_${{self.c_ident}}(const string& str)
6386657Snate@binkert.org{
6396657Snate@binkert.org''')
6406657Snate@binkert.org
6416657Snate@binkert.org        # For each field
6427007Snate@binkert.org        start = ""
6436657Snate@binkert.org        code.indent()
6446657Snate@binkert.org        for enum in self.enums.itervalues():
6456657Snate@binkert.org            code('${start}if (str == "${{enum.ident}}") {')
6466657Snate@binkert.org            code('    return ${{self.c_ident}}_${{enum.ident}};')
6477007Snate@binkert.org            start = "} else "
6486657Snate@binkert.org        code.dedent()
6496657Snate@binkert.org
6506657Snate@binkert.org        code('''
6516657Snate@binkert.org    } else {
6527805Snilay@cs.wisc.edu        panic("Invalid string conversion for %s, type ${{self.c_ident}}", str);
6536657Snate@binkert.org    }
6546657Snate@binkert.org}
6556657Snate@binkert.org
6567007Snate@binkert.org// Code to increment an enumeration type
6577007Snate@binkert.org${{self.c_ident}}&
6587007Snate@binkert.orgoperator++(${{self.c_ident}}& e)
6597007Snate@binkert.org{
6606657Snate@binkert.org    assert(e < ${{self.c_ident}}_NUM);
6616657Snate@binkert.org    return e = ${{self.c_ident}}(e+1);
6626657Snate@binkert.org}
6636657Snate@binkert.org''')
6646657Snate@binkert.org
6656657Snate@binkert.org        # MachineType hack used to set the base level and number of
6666657Snate@binkert.org        # components for each Machine
6676657Snate@binkert.org        if self.isMachineType:
6686657Snate@binkert.org            code('''
6697007Snate@binkert.org/** \\brief returns the base vector index for each machine type to be
6707007Snate@binkert.org  * used by NetDest
6716657Snate@binkert.org  *
6726657Snate@binkert.org  * \\return the base vector index for each machine type to be used by NetDest
6736657Snate@binkert.org  * \\see NetDest.hh
6746657Snate@binkert.org  */
6757007Snate@binkert.orgint
6767007Snate@binkert.org${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj)
6776657Snate@binkert.org{
6786657Snate@binkert.org    switch(obj) {
6796657Snate@binkert.org''')
6806657Snate@binkert.org
6816657Snate@binkert.org            # For each field
6826657Snate@binkert.org            code.indent()
6836657Snate@binkert.org            for i,enum in enumerate(self.enums.itervalues()):
6846657Snate@binkert.org                code('  case ${{self.c_ident}}_${{enum.ident}}:')
6856657Snate@binkert.org                code('    return $i;')
6866657Snate@binkert.org            code.dedent()
6876657Snate@binkert.org
6886657Snate@binkert.org            # total num
6896657Snate@binkert.org            code('''
6906657Snate@binkert.org      case ${{self.c_ident}}_NUM:
6916657Snate@binkert.org        return ${{len(self.enums)}};
6926657Snate@binkert.org
6936657Snate@binkert.org      default:
6947805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
6956657Snate@binkert.org    }
6966657Snate@binkert.org}
6976657Snate@binkert.org
6986657Snate@binkert.org/** \\brief returns the machine type for each base vector index used by NetDest
6996657Snate@binkert.org *
7007007Snate@binkert.org * \\return the MachineType
7016657Snate@binkert.org */
7027007Snate@binkert.orgMachineType
7037007Snate@binkert.org${{self.c_ident}}_from_base_level(int type)
7046657Snate@binkert.org{
7056657Snate@binkert.org    switch(type) {
7066657Snate@binkert.org''')
7076657Snate@binkert.org
7086657Snate@binkert.org            # For each field
7096657Snate@binkert.org            code.indent()
7106657Snate@binkert.org            for i,enum in enumerate(self.enums.itervalues()):
7116657Snate@binkert.org                code('  case $i:')
7126657Snate@binkert.org                code('    return ${{self.c_ident}}_${{enum.ident}};')
7136657Snate@binkert.org            code.dedent()
7146657Snate@binkert.org
7156657Snate@binkert.org            # Trailer
7166657Snate@binkert.org            code('''
7176657Snate@binkert.org      default:
7187805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
7196657Snate@binkert.org    }
7206657Snate@binkert.org}
7216657Snate@binkert.org
7226657Snate@binkert.org/** \\brief The return value indicates the number of components created
7236657Snate@binkert.org * before a particular machine\'s components
7246657Snate@binkert.org *
7256657Snate@binkert.org * \\return the base number of components for each machine
7266657Snate@binkert.org */
7277007Snate@binkert.orgint
7287007Snate@binkert.org${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj)
7296657Snate@binkert.org{
7306657Snate@binkert.org    int base = 0;
7316657Snate@binkert.org    switch(obj) {
7326657Snate@binkert.org''')
7336657Snate@binkert.org
7346657Snate@binkert.org            # For each field
7356657Snate@binkert.org            code.indent()
7366657Snate@binkert.org            code('  case ${{self.c_ident}}_NUM:')
7376657Snate@binkert.org            for enum in reversed(self.enums.values()):
7386657Snate@binkert.org                code('    base += ${{enum.ident}}_Controller::getNumControllers();')
7396657Snate@binkert.org                code('  case ${{self.c_ident}}_${{enum.ident}}:')
7406657Snate@binkert.org            code('    break;')
7416657Snate@binkert.org            code.dedent()
7426657Snate@binkert.org
7436657Snate@binkert.org            code('''
7446657Snate@binkert.org      default:
7457805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
7466657Snate@binkert.org    }
7476657Snate@binkert.org
7486657Snate@binkert.org    return base;
7496657Snate@binkert.org}
7506657Snate@binkert.org
7516657Snate@binkert.org/** \\brief returns the total number of components for each machine
7526657Snate@binkert.org * \\return the total number of components for each machine
7536657Snate@binkert.org */
7547007Snate@binkert.orgint
7557007Snate@binkert.org${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj)
7566657Snate@binkert.org{
7576657Snate@binkert.org    switch(obj) {
7586657Snate@binkert.org''')
7596657Snate@binkert.org
7606657Snate@binkert.org            # For each field
7616657Snate@binkert.org            for enum in self.enums.itervalues():
7626657Snate@binkert.org                code('''
7636657Snate@binkert.org      case ${{self.c_ident}}_${{enum.ident}}:
7646657Snate@binkert.org        return ${{enum.ident}}_Controller::getNumControllers();
7656657Snate@binkert.org''')
7666657Snate@binkert.org
7676657Snate@binkert.org            # total num
7686657Snate@binkert.org            code('''
7696657Snate@binkert.org      case ${{self.c_ident}}_NUM:
7706657Snate@binkert.org      default:
7717805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
7726657Snate@binkert.org    }
7736657Snate@binkert.org}
7746657Snate@binkert.org''')
7756657Snate@binkert.org
7768602Snilay@cs.wisc.edu            for enum in self.enums.itervalues():
7778602Snilay@cs.wisc.edu                if enum.ident == "DMA":
7788602Snilay@cs.wisc.edu                    code('''
7798602Snilay@cs.wisc.eduMachineID
7808602Snilay@cs.wisc.edumap_Address_to_DMA(const Address &addr)
7818602Snilay@cs.wisc.edu{
7828602Snilay@cs.wisc.edu      MachineID dma = {MachineType_DMA, 0};
7838602Snilay@cs.wisc.edu      return dma;
7848602Snilay@cs.wisc.edu}
7858602Snilay@cs.wisc.edu''')
7868602Snilay@cs.wisc.edu
7878602Snilay@cs.wisc.edu                code('''
7888602Snilay@cs.wisc.edu
7898602Snilay@cs.wisc.eduMachineID
7908602Snilay@cs.wisc.eduget${{enum.ident}}MachineID(NodeID RubyNode)
7918602Snilay@cs.wisc.edu{
7928602Snilay@cs.wisc.edu      MachineID mach = {MachineType_${{enum.ident}}, RubyNode};
7938602Snilay@cs.wisc.edu      return mach;
7948602Snilay@cs.wisc.edu}
7958602Snilay@cs.wisc.edu''')
7968602Snilay@cs.wisc.edu
7976657Snate@binkert.org        # Write the file
7986657Snate@binkert.org        code.write(path, "%s.cc" % self.c_ident)
7996657Snate@binkert.org
8006657Snate@binkert.org__all__ = [ "Type" ]
801