Type.py revision 10307
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
329302Snilay@cs.wisc.edufrom slicc.symbols.Var import Var
336657Snate@binkert.org
346657Snate@binkert.orgclass DataMember(PairContainer):
356657Snate@binkert.org    def __init__(self, ident, type, pairs, init_code):
366657Snate@binkert.org        super(DataMember, self).__init__(pairs)
376657Snate@binkert.org        self.ident = ident
386657Snate@binkert.org        self.type = type
396657Snate@binkert.org        self.init_code = init_code
406657Snate@binkert.org
416657Snate@binkert.orgclass Enumeration(PairContainer):
426657Snate@binkert.org    def __init__(self, ident, pairs):
436657Snate@binkert.org        super(Enumeration, self).__init__(pairs)
446657Snate@binkert.org        self.ident = ident
456657Snate@binkert.org
466657Snate@binkert.orgclass Type(Symbol):
476657Snate@binkert.org    def __init__(self, table, ident, location, pairs, machine=None):
486657Snate@binkert.org        super(Type, self).__init__(table, ident, location, pairs)
496657Snate@binkert.org        self.c_ident = ident
506882SBrad.Beckmann@amd.com        self.abstract_ident = ""
516657Snate@binkert.org        if machine:
526657Snate@binkert.org            if self.isExternal or self.isPrimitive:
536657Snate@binkert.org                if "external_name" in self:
546657Snate@binkert.org                    self.c_ident = self["external_name"]
556657Snate@binkert.org            else:
566657Snate@binkert.org                # Append with machine name
576657Snate@binkert.org                self.c_ident = "%s_%s" % (machine, ident)
586657Snate@binkert.org
596657Snate@binkert.org        self.pairs.setdefault("desc", "No description avaliable")
606657Snate@binkert.org
616657Snate@binkert.org        # check for interface that this Type implements
626657Snate@binkert.org        if "interface" in self:
636657Snate@binkert.org            interface = self["interface"]
646657Snate@binkert.org            if interface in ("Message", "NetworkMessage"):
656657Snate@binkert.org                self["message"] = "yes"
666657Snate@binkert.org            if interface == "NetworkMessage":
676657Snate@binkert.org                self["networkmessage"] = "yes"
686657Snate@binkert.org
696657Snate@binkert.org        # FIXME - all of the following id comparisons are fragile hacks
7010228Snilay@cs.wisc.edu        if self.ident in ("CacheMemory"):
716657Snate@binkert.org            self["cache"] = "yes"
726657Snate@binkert.org
7310228Snilay@cs.wisc.edu        if self.ident in ("TBETable"):
746657Snate@binkert.org            self["tbe"] = "yes"
756657Snate@binkert.org
766657Snate@binkert.org        if self.ident == "TimerTable":
776657Snate@binkert.org            self["timer"] = "yes"
786657Snate@binkert.org
796657Snate@binkert.org        if self.ident == "DirectoryMemory":
806657Snate@binkert.org            self["dir"] = "yes"
816657Snate@binkert.org
826657Snate@binkert.org        if self.ident == "PersistentTable":
836657Snate@binkert.org            self["persistent"] = "yes"
846657Snate@binkert.org
856657Snate@binkert.org        if self.ident == "Prefetcher":
866657Snate@binkert.org            self["prefetcher"] = "yes"
876657Snate@binkert.org
886657Snate@binkert.org        self.isMachineType = (ident == "MachineType")
896657Snate@binkert.org
908086SBrad.Beckmann@amd.com        self.isStateDecl = ("state_decl" in self)
918086SBrad.Beckmann@amd.com        self.statePermPairs = []
928086SBrad.Beckmann@amd.com
936657Snate@binkert.org        self.data_members = orderdict()
946657Snate@binkert.org        self.methods = {}
956657Snate@binkert.org        self.enums = orderdict()
966657Snate@binkert.org
976657Snate@binkert.org    @property
986657Snate@binkert.org    def isPrimitive(self):
996657Snate@binkert.org        return "primitive" in self
1006657Snate@binkert.org    @property
1016657Snate@binkert.org    def isNetworkMessage(self):
1026657Snate@binkert.org        return "networkmessage" in self
1036657Snate@binkert.org    @property
1046657Snate@binkert.org    def isMessage(self):
1056657Snate@binkert.org        return "message" in self
1066657Snate@binkert.org    @property
1076657Snate@binkert.org    def isBuffer(self):
1086657Snate@binkert.org        return "buffer" in self
1096657Snate@binkert.org    @property
1106657Snate@binkert.org    def isInPort(self):
1116657Snate@binkert.org        return "inport" in self
1126657Snate@binkert.org    @property
1136657Snate@binkert.org    def isOutPort(self):
1146657Snate@binkert.org        return "outport" in self
1156657Snate@binkert.org    @property
1166657Snate@binkert.org    def isEnumeration(self):
1176657Snate@binkert.org        return "enumeration" in self
1186657Snate@binkert.org    @property
1196657Snate@binkert.org    def isExternal(self):
1206657Snate@binkert.org        return "external" in self
1216657Snate@binkert.org    @property
1226657Snate@binkert.org    def isGlobal(self):
1236657Snate@binkert.org        return "global" in self
1246657Snate@binkert.org    @property
1256657Snate@binkert.org    def isInterface(self):
1266657Snate@binkert.org        return "interface" in self
1276657Snate@binkert.org
1286657Snate@binkert.org    # Return false on error
1299298Snilay@cs.wisc.edu    def addDataMember(self, ident, type, pairs, init_code):
1306657Snate@binkert.org        if ident in self.data_members:
1316657Snate@binkert.org            return False
1326657Snate@binkert.org
1336657Snate@binkert.org        member = DataMember(ident, type, pairs, init_code)
1346657Snate@binkert.org        self.data_members[ident] = member
1356657Snate@binkert.org
1369302Snilay@cs.wisc.edu        var = Var(self.symtab, ident, self.location, type,
1379302Snilay@cs.wisc.edu                "m_%s" % ident, {}, None)
1389302Snilay@cs.wisc.edu        self.symtab.registerSym(ident, var)
1396657Snate@binkert.org        return True
1406657Snate@binkert.org
1416657Snate@binkert.org    def dataMemberType(self, ident):
1426657Snate@binkert.org        return self.data_members[ident].type
1436657Snate@binkert.org
1446657Snate@binkert.org    def methodId(self, name, param_type_vec):
1456657Snate@binkert.org        return '_'.join([name] + [ pt.c_ident for pt in param_type_vec ])
1466657Snate@binkert.org
1476882SBrad.Beckmann@amd.com    def methodIdAbstract(self, name, param_type_vec):
1486882SBrad.Beckmann@amd.com        return '_'.join([name] + [ pt.abstract_ident for pt in param_type_vec ])
1496882SBrad.Beckmann@amd.com
1508086SBrad.Beckmann@amd.com    def statePermPairAdd(self, state_name, perm_name):
1518086SBrad.Beckmann@amd.com        self.statePermPairs.append([state_name, perm_name])
1528086SBrad.Beckmann@amd.com
15310307Snilay@cs.wisc.edu    def addFunc(self, func):
15410307Snilay@cs.wisc.edu        ident = self.methodId(func.ident, func.param_types)
1556657Snate@binkert.org        if ident in self.methods:
1566657Snate@binkert.org            return False
1576657Snate@binkert.org
15810307Snilay@cs.wisc.edu        self.methods[ident] = func
1599298Snilay@cs.wisc.edu        return True
1609298Snilay@cs.wisc.edu
1619298Snilay@cs.wisc.edu    def addEnum(self, ident, pairs):
1626657Snate@binkert.org        if ident in self.enums:
1636657Snate@binkert.org            return False
1646657Snate@binkert.org
1656657Snate@binkert.org        self.enums[ident] = Enumeration(ident, pairs)
1666657Snate@binkert.org
1676657Snate@binkert.org        # Add default
1686657Snate@binkert.org        if "default" not in self:
1696657Snate@binkert.org            self["default"] = "%s_NUM" % self.c_ident
1706657Snate@binkert.org
1716657Snate@binkert.org        return True
1726657Snate@binkert.org
1739219Spower.jg@gmail.com    def writeCodeFiles(self, path, includes):
1746657Snate@binkert.org        if self.isExternal:
1756657Snate@binkert.org            # Do nothing
1766657Snate@binkert.org            pass
1776657Snate@binkert.org        elif self.isEnumeration:
1786657Snate@binkert.org            self.printEnumHH(path)
1796657Snate@binkert.org            self.printEnumCC(path)
1806657Snate@binkert.org        else:
1816657Snate@binkert.org            # User defined structs and messages
1826657Snate@binkert.org            self.printTypeHH(path)
1836657Snate@binkert.org            self.printTypeCC(path)
1846657Snate@binkert.org
1856657Snate@binkert.org    def printTypeHH(self, path):
1866999Snate@binkert.org        code = self.symtab.codeFormatter()
1876657Snate@binkert.org        code('''
1886657Snate@binkert.org/** \\file ${{self.c_ident}}.hh
1896657Snate@binkert.org *
1906657Snate@binkert.org *
1916657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
1926657Snate@binkert.org */
1936657Snate@binkert.org
1947007Snate@binkert.org#ifndef __${{self.c_ident}}_HH__
1957007Snate@binkert.org#define __${{self.c_ident}}_HH__
1966657Snate@binkert.org
1977002Snate@binkert.org#include <iostream>
1987002Snate@binkert.org
1999466Snilay@cs.wisc.edu#include "mem/ruby/slicc_interface/RubySlicc_Util.hh"
2006657Snate@binkert.org''')
2016657Snate@binkert.org
2026657Snate@binkert.org        for dm in self.data_members.values():
2036657Snate@binkert.org            if not dm.type.isPrimitive:
2046657Snate@binkert.org                code('#include "mem/protocol/$0.hh"', dm.type.c_ident)
2056657Snate@binkert.org
2066657Snate@binkert.org        parent = ""
2076657Snate@binkert.org        if "interface" in self:
2086657Snate@binkert.org            code('#include "mem/protocol/$0.hh"', self["interface"])
2096657Snate@binkert.org            parent = " :  public %s" % self["interface"]
2106657Snate@binkert.org
2116657Snate@binkert.org        code('''
2127007Snate@binkert.org$klass ${{self.c_ident}}$parent
2137007Snate@binkert.org{
2146657Snate@binkert.org  public:
2159466Snilay@cs.wisc.edu    ${{self.c_ident}}
2166657Snate@binkert.org''', klass="class")
2176657Snate@binkert.org
2189466Snilay@cs.wisc.edu        if self.isMessage:
2199508Snilay@cs.wisc.edu            code('(Tick curTime) : %s(curTime) {' % self["interface"])
2209466Snilay@cs.wisc.edu        else:
2219466Snilay@cs.wisc.edu            code('()\n\t\t{')
2229466Snilay@cs.wisc.edu
2236657Snate@binkert.org        code.indent()
2246657Snate@binkert.org        if not self.isGlobal:
2256657Snate@binkert.org            code.indent()
2266657Snate@binkert.org            for dm in self.data_members.values():
2276657Snate@binkert.org                ident = dm.ident
2286657Snate@binkert.org                if "default" in dm:
2296657Snate@binkert.org                    # look for default value
2306657Snate@binkert.org                    code('m_$ident = ${{dm["default"]}}; // default for this field')
2316657Snate@binkert.org                elif "default" in dm.type:
2326657Snate@binkert.org                    # Look for the type default
2336657Snate@binkert.org                    tid = dm.type.c_ident
2346657Snate@binkert.org                    code('m_$ident = ${{dm.type["default"]}}; // default value of $tid')
2356657Snate@binkert.org                else:
2366657Snate@binkert.org                    code('// m_$ident has no default')
2376657Snate@binkert.org            code.dedent()
2386657Snate@binkert.org        code('}')
2396657Snate@binkert.org
2407453Snate@binkert.org        # ******** Copy constructor ********
2417453Snate@binkert.org        if not self.isGlobal:
2427453Snate@binkert.org            code('${{self.c_ident}}(const ${{self.c_ident}}&other)')
2437453Snate@binkert.org
2447453Snate@binkert.org            # Call superclass constructor
2457453Snate@binkert.org            if "interface" in self:
2467453Snate@binkert.org                code('    : ${{self["interface"]}}(other)')
2477453Snate@binkert.org
2487453Snate@binkert.org            code('{')
2497453Snate@binkert.org            code.indent()
2507453Snate@binkert.org
2517453Snate@binkert.org            for dm in self.data_members.values():
2527453Snate@binkert.org                code('m_${{dm.ident}} = other.m_${{dm.ident}};')
2537453Snate@binkert.org
2547453Snate@binkert.org            code.dedent()
2557453Snate@binkert.org            code('}')
2567453Snate@binkert.org
2576657Snate@binkert.org        # ******** Full init constructor ********
2586657Snate@binkert.org        if not self.isGlobal:
2596657Snate@binkert.org            params = [ 'const %s& local_%s' % (dm.type.c_ident, dm.ident) \
2606657Snate@binkert.org                       for dm in self.data_members.itervalues() ]
2619466Snilay@cs.wisc.edu            params = ', '.join(params)
2626657Snate@binkert.org
2639466Snilay@cs.wisc.edu            if self.isMessage:
2649508Snilay@cs.wisc.edu                params = "const Tick curTime, " + params
2659466Snilay@cs.wisc.edu
2666657Snate@binkert.org            code('${{self.c_ident}}($params)')
2676657Snate@binkert.org
2686657Snate@binkert.org            # Call superclass constructor
2696657Snate@binkert.org            if "interface" in self:
2709466Snilay@cs.wisc.edu                if self.isMessage:
2719466Snilay@cs.wisc.edu                    code('    : ${{self["interface"]}}(curTime)')
2729466Snilay@cs.wisc.edu                else:
2739466Snilay@cs.wisc.edu                    code('    : ${{self["interface"]}}()')
2746657Snate@binkert.org
2756657Snate@binkert.org            code('{')
2766657Snate@binkert.org            code.indent()
2776657Snate@binkert.org            for dm in self.data_members.values():
2786657Snate@binkert.org                code('m_${{dm.ident}} = local_${{dm.ident}};')
2796657Snate@binkert.org                if "nextLineCallHack" in dm:
2806657Snate@binkert.org                    code('m_${{dm.ident}}${{dm["nextLineCallHack"]}};')
2816657Snate@binkert.org
2826657Snate@binkert.org            code.dedent()
2836657Snate@binkert.org            code('}')
2846657Snate@binkert.org
2859466Snilay@cs.wisc.edu        # create a clone member
2867453Snate@binkert.org        code('''
2877453Snate@binkert.org${{self.c_ident}}*
2887007Snate@binkert.orgclone() const
2897007Snate@binkert.org{
2907453Snate@binkert.org     return new ${{self.c_ident}}(*this);
2917007Snate@binkert.org}
2926657Snate@binkert.org''')
2936657Snate@binkert.org
2946657Snate@binkert.org        if not self.isGlobal:
2956657Snate@binkert.org            # const Get methods for each field
2966657Snate@binkert.org            code('// Const accessors methods for each field')
2976657Snate@binkert.org            for dm in self.data_members.values():
2986657Snate@binkert.org                code('''
2996657Snate@binkert.org/** \\brief Const accessor method for ${{dm.ident}} field.
3006657Snate@binkert.org *  \\return ${{dm.ident}} field
3016657Snate@binkert.org */
3027007Snate@binkert.orgconst ${{dm.type.c_ident}}&
3037007Snate@binkert.orgget${{dm.ident}}() const
3047007Snate@binkert.org{
3057007Snate@binkert.org    return m_${{dm.ident}};
3067007Snate@binkert.org}
3076657Snate@binkert.org''')
3086657Snate@binkert.org
3096657Snate@binkert.org            # Non-const Get methods for each field
3106657Snate@binkert.org            code('// Non const Accessors methods for each field')
3116657Snate@binkert.org            for dm in self.data_members.values():
3126657Snate@binkert.org                code('''
3136657Snate@binkert.org/** \\brief Non-const accessor method for ${{dm.ident}} field.
3146657Snate@binkert.org *  \\return ${{dm.ident}} field
3156657Snate@binkert.org */
3167007Snate@binkert.org${{dm.type.c_ident}}&
3177007Snate@binkert.orgget${{dm.ident}}()
3187007Snate@binkert.org{
3197007Snate@binkert.org    return m_${{dm.ident}};
3207007Snate@binkert.org}
3216657Snate@binkert.org''')
3226657Snate@binkert.org
3236657Snate@binkert.org            #Set methods for each field
3246657Snate@binkert.org            code('// Mutator methods for each field')
3256657Snate@binkert.org            for dm in self.data_members.values():
3266657Snate@binkert.org                code('''
3276657Snate@binkert.org/** \\brief Mutator method for ${{dm.ident}} field */
3287007Snate@binkert.orgvoid
3297007Snate@binkert.orgset${{dm.ident}}(const ${{dm.type.c_ident}}& local_${{dm.ident}})
3307007Snate@binkert.org{
3317007Snate@binkert.org    m_${{dm.ident}} = local_${{dm.ident}};
3327007Snate@binkert.org}
3336657Snate@binkert.org''')
3346657Snate@binkert.org
3357002Snate@binkert.org        code('void print(std::ostream& out) const;')
3366657Snate@binkert.org        code.dedent()
3376657Snate@binkert.org        code('  //private:')
3386657Snate@binkert.org        code.indent()
3396657Snate@binkert.org
3406657Snate@binkert.org        # Data members for each field
3416657Snate@binkert.org        for dm in self.data_members.values():
3426657Snate@binkert.org            if "abstract" not in dm:
3436657Snate@binkert.org                const = ""
3446657Snate@binkert.org                init = ""
3456657Snate@binkert.org
3466657Snate@binkert.org                # global structure
3476657Snate@binkert.org                if self.isGlobal:
3486657Snate@binkert.org                    const = "static const "
3496657Snate@binkert.org
3506657Snate@binkert.org                # init value
3516657Snate@binkert.org                if dm.init_code:
3526657Snate@binkert.org                    # only global structure can have init value here
3536657Snate@binkert.org                    assert self.isGlobal
3546657Snate@binkert.org                    init = " = %s" % (dm.init_code)
3556657Snate@binkert.org
3566657Snate@binkert.org                if "desc" in dm:
3577007Snate@binkert.org                    code('/** ${{dm["desc"]}} */')
3586657Snate@binkert.org
3597007Snate@binkert.org                code('$const${{dm.type.c_ident}} m_${{dm.ident}}$init;')
3606657Snate@binkert.org
36110307Snilay@cs.wisc.edu        # Prototypes for methods defined for the Type
36210307Snilay@cs.wisc.edu        for item in self.methods:
36310307Snilay@cs.wisc.edu            proto = self.methods[item].prototype
3649298Snilay@cs.wisc.edu            if proto:
3659298Snilay@cs.wisc.edu                code('$proto')
3669298Snilay@cs.wisc.edu
3676657Snate@binkert.org        code.dedent()
3686657Snate@binkert.org        code('};')
3696657Snate@binkert.org
3706657Snate@binkert.org        code('''
3717055Snate@binkert.orginline std::ostream&
3727007Snate@binkert.orgoperator<<(std::ostream& out, const ${{self.c_ident}}& obj)
3736657Snate@binkert.org{
3746657Snate@binkert.org    obj.print(out);
3757002Snate@binkert.org    out << std::flush;
3766657Snate@binkert.org    return out;
3776657Snate@binkert.org}
3786657Snate@binkert.org
3797007Snate@binkert.org#endif // __${{self.c_ident}}_HH__
3806657Snate@binkert.org''')
3816657Snate@binkert.org
3826657Snate@binkert.org        code.write(path, "%s.hh" % self.c_ident)
3836657Snate@binkert.org
3846657Snate@binkert.org    def printTypeCC(self, path):
3856999Snate@binkert.org        code = self.symtab.codeFormatter()
3866657Snate@binkert.org
3876657Snate@binkert.org        code('''
3886657Snate@binkert.org/** \\file ${{self.c_ident}}.cc
3896657Snate@binkert.org *
3906657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
3916657Snate@binkert.org */
3926657Snate@binkert.org
3937002Snate@binkert.org#include <iostream>
3947002Snate@binkert.org
3956657Snate@binkert.org#include "mem/protocol/${{self.c_ident}}.hh"
3969499Snilay@cs.wisc.edu#include "mem/ruby/common/Global.hh"
3979499Snilay@cs.wisc.edu#include "mem/ruby/system/System.hh"
3987002Snate@binkert.org
3997002Snate@binkert.orgusing namespace std;
4006657Snate@binkert.org''')
4016657Snate@binkert.org
4026657Snate@binkert.org        code('''
4036657Snate@binkert.org/** \\brief Print the state of this object */
4047007Snate@binkert.orgvoid
4057007Snate@binkert.org${{self.c_ident}}::print(ostream& out) const
4066657Snate@binkert.org{
4076657Snate@binkert.org    out << "[${{self.c_ident}}: ";
4086657Snate@binkert.org''')
4096657Snate@binkert.org
4106657Snate@binkert.org        # For each field
4116657Snate@binkert.org        code.indent()
4126657Snate@binkert.org        for dm in self.data_members.values():
4136657Snate@binkert.org            code('out << "${{dm.ident}} = " << m_${{dm.ident}} << " ";''')
4146657Snate@binkert.org
4156657Snate@binkert.org        if self.isMessage:
4169206Snilay@cs.wisc.edu            code('out << "Time = " << g_system_ptr->clockPeriod() * getTime() << " ";')
4176657Snate@binkert.org        code.dedent()
4186657Snate@binkert.org
4196657Snate@binkert.org        # Trailer
4206657Snate@binkert.org        code('''
4216657Snate@binkert.org    out << "]";
4226657Snate@binkert.org}''')
4236657Snate@binkert.org
42410307Snilay@cs.wisc.edu        # print the code for the methods in the type
42510307Snilay@cs.wisc.edu        for item in self.methods:
42610307Snilay@cs.wisc.edu            code(self.methods[item].generateCode())
4279298Snilay@cs.wisc.edu
4286657Snate@binkert.org        code.write(path, "%s.cc" % self.c_ident)
4296657Snate@binkert.org
4306657Snate@binkert.org    def printEnumHH(self, path):
4316999Snate@binkert.org        code = self.symtab.codeFormatter()
4326657Snate@binkert.org        code('''
4336657Snate@binkert.org/** \\file ${{self.c_ident}}.hh
4346657Snate@binkert.org *
4356657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
4366657Snate@binkert.org */
4377007Snate@binkert.org
4387007Snate@binkert.org#ifndef __${{self.c_ident}}_HH__
4397007Snate@binkert.org#define __${{self.c_ident}}_HH__
4406657Snate@binkert.org
4417002Snate@binkert.org#include <iostream>
4427002Snate@binkert.org#include <string>
4437002Snate@binkert.org
4448086SBrad.Beckmann@amd.com''')
4458086SBrad.Beckmann@amd.com        if self.isStateDecl:
4468086SBrad.Beckmann@amd.com            code('#include "mem/protocol/AccessPermission.hh"')
4478086SBrad.Beckmann@amd.com
4488602Snilay@cs.wisc.edu        if self.isMachineType:
4498602Snilay@cs.wisc.edu            code('#include "base/misc.hh"')
4508602Snilay@cs.wisc.edu            code('#include "mem/ruby/common/Address.hh"')
4518602Snilay@cs.wisc.edu            code('struct MachineID;')
4528602Snilay@cs.wisc.edu
4538086SBrad.Beckmann@amd.com        code('''
4546657Snate@binkert.org
4557007Snate@binkert.org// Class definition
4566657Snate@binkert.org/** \\enum ${{self.c_ident}}
4576657Snate@binkert.org *  \\brief ${{self.desc}}
4586657Snate@binkert.org */
4596657Snate@binkert.orgenum ${{self.c_ident}} {
4606657Snate@binkert.org    ${{self.c_ident}}_FIRST,
4616657Snate@binkert.org''')
4626657Snate@binkert.org
4636657Snate@binkert.org        code.indent()
4646657Snate@binkert.org        # For each field
4656657Snate@binkert.org        for i,(ident,enum) in enumerate(self.enums.iteritems()):
4666657Snate@binkert.org            desc = enum.get("desc", "No description avaliable")
4676862Sdrh5@cs.wisc.edu            if i == 0:
4686862Sdrh5@cs.wisc.edu                init = ' = %s_FIRST' % self.c_ident
4696862Sdrh5@cs.wisc.edu            else:
4706862Sdrh5@cs.wisc.edu                init = ''
4716657Snate@binkert.org            code('${{self.c_ident}}_${{enum.ident}}$init, /**< $desc */')
4726657Snate@binkert.org        code.dedent()
4736657Snate@binkert.org        code('''
4746657Snate@binkert.org    ${{self.c_ident}}_NUM
4756657Snate@binkert.org};
4767007Snate@binkert.org
4777007Snate@binkert.org// Code to convert from a string to the enumeration
4787002Snate@binkert.org${{self.c_ident}} string_to_${{self.c_ident}}(const std::string& str);
4797007Snate@binkert.org
4807007Snate@binkert.org// Code to convert state to a string
4817002Snate@binkert.orgstd::string ${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj);
4827007Snate@binkert.org
4837007Snate@binkert.org// Code to increment an enumeration type
4846657Snate@binkert.org${{self.c_ident}} &operator++(${{self.c_ident}} &e);
4856657Snate@binkert.org''')
4866657Snate@binkert.org
4876657Snate@binkert.org        # MachineType hack used to set the base component id for each Machine
4886657Snate@binkert.org        if self.isMachineType:
4896657Snate@binkert.org            code('''
4906657Snate@binkert.orgint ${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj);
4916657Snate@binkert.orgMachineType ${{self.c_ident}}_from_base_level(int);
4926657Snate@binkert.orgint ${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj);
4936657Snate@binkert.orgint ${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj);
4946657Snate@binkert.org''')
4956657Snate@binkert.org
4966657Snate@binkert.org            for enum in self.enums.itervalues():
4978602Snilay@cs.wisc.edu                if enum.ident == "DMA":
4988602Snilay@cs.wisc.edu                    code('''
4998602Snilay@cs.wisc.eduMachineID map_Address_to_DMA(const Address &addr);
5008602Snilay@cs.wisc.edu''')
5018602Snilay@cs.wisc.edu                code('''
5028602Snilay@cs.wisc.edu
5038602Snilay@cs.wisc.eduMachineID get${{enum.ident}}MachineID(NodeID RubyNode);
5048602Snilay@cs.wisc.edu''')
5058602Snilay@cs.wisc.edu
5068086SBrad.Beckmann@amd.com        if self.isStateDecl:
5078086SBrad.Beckmann@amd.com            code('''
5088086SBrad.Beckmann@amd.com
5098086SBrad.Beckmann@amd.com// Code to convert the current state to an access permission
5108086SBrad.Beckmann@amd.comAccessPermission ${{self.c_ident}}_to_permission(const ${{self.c_ident}}& obj);
5118086SBrad.Beckmann@amd.com
5128086SBrad.Beckmann@amd.com''')
5138086SBrad.Beckmann@amd.com
5146657Snate@binkert.org        # Trailer
5156657Snate@binkert.org        code('''
5167002Snate@binkert.orgstd::ostream& operator<<(std::ostream& out, const ${{self.c_ident}}& obj);
5176657Snate@binkert.org
5187007Snate@binkert.org#endif // __${{self.c_ident}}_HH__
5196657Snate@binkert.org''')
5206657Snate@binkert.org
5216657Snate@binkert.org        code.write(path, "%s.hh" % self.c_ident)
5226657Snate@binkert.org
5236657Snate@binkert.org    def printEnumCC(self, path):
5246999Snate@binkert.org        code = self.symtab.codeFormatter()
5256657Snate@binkert.org        code('''
5266657Snate@binkert.org/** \\file ${{self.c_ident}}.hh
5276657Snate@binkert.org *
5286657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
5296657Snate@binkert.org */
5306657Snate@binkert.org
5317832Snate@binkert.org#include <cassert>
5327002Snate@binkert.org#include <iostream>
5337002Snate@binkert.org#include <string>
5347002Snate@binkert.org
5357805Snilay@cs.wisc.edu#include "base/misc.hh"
5366657Snate@binkert.org#include "mem/protocol/${{self.c_ident}}.hh"
5376657Snate@binkert.org
5387002Snate@binkert.orgusing namespace std;
5397002Snate@binkert.org
5406657Snate@binkert.org''')
5416657Snate@binkert.org
5428086SBrad.Beckmann@amd.com        if self.isStateDecl:
5438086SBrad.Beckmann@amd.com            code('''
5448086SBrad.Beckmann@amd.com// Code to convert the current state to an access permission
5458086SBrad.Beckmann@amd.comAccessPermission ${{self.c_ident}}_to_permission(const ${{self.c_ident}}& obj)
5468086SBrad.Beckmann@amd.com{
5478086SBrad.Beckmann@amd.com    switch(obj) {
5488086SBrad.Beckmann@amd.com''')
5498086SBrad.Beckmann@amd.com            # For each case
5508086SBrad.Beckmann@amd.com            code.indent()
5518086SBrad.Beckmann@amd.com            for statePerm in self.statePermPairs:
5528086SBrad.Beckmann@amd.com                code('  case ${{self.c_ident}}_${{statePerm[0]}}:')
5538086SBrad.Beckmann@amd.com                code('    return AccessPermission_${{statePerm[1]}};')
5548086SBrad.Beckmann@amd.com            code.dedent()
5558086SBrad.Beckmann@amd.com            code ('''
5568086SBrad.Beckmann@amd.com      default:
5578086SBrad.Beckmann@amd.com        panic("Unknown state access permission converstion for ${{self.c_ident}}");
5588086SBrad.Beckmann@amd.com    }
5598086SBrad.Beckmann@amd.com}
5608086SBrad.Beckmann@amd.com
5618086SBrad.Beckmann@amd.com''')
5628086SBrad.Beckmann@amd.com
5636657Snate@binkert.org        if self.isMachineType:
5646657Snate@binkert.org            for enum in self.enums.itervalues():
5659773Snilay@cs.wisc.edu                if enum.get("Primary"):
5669773Snilay@cs.wisc.edu                    code('#include "mem/protocol/${{enum.ident}}_Controller.hh"')
56710301Snilay@cs.wisc.edu            code('#include "mem/ruby/common/MachineID.hh"')
5686657Snate@binkert.org
5696657Snate@binkert.org        code('''
5707007Snate@binkert.org// Code for output operator
5717007Snate@binkert.orgostream&
5727007Snate@binkert.orgoperator<<(ostream& out, const ${{self.c_ident}}& obj)
5736657Snate@binkert.org{
5746657Snate@binkert.org    out << ${{self.c_ident}}_to_string(obj);
5756657Snate@binkert.org    out << flush;
5766657Snate@binkert.org    return out;
5776657Snate@binkert.org}
5786657Snate@binkert.org
5797007Snate@binkert.org// Code to convert state to a string
5807007Snate@binkert.orgstring
5817007Snate@binkert.org${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj)
5826657Snate@binkert.org{
5836657Snate@binkert.org    switch(obj) {
5846657Snate@binkert.org''')
5856657Snate@binkert.org
5866657Snate@binkert.org        # For each field
5876657Snate@binkert.org        code.indent()
5886657Snate@binkert.org        for enum in self.enums.itervalues():
5896657Snate@binkert.org            code('  case ${{self.c_ident}}_${{enum.ident}}:')
5906657Snate@binkert.org            code('    return "${{enum.ident}}";')
5916657Snate@binkert.org        code.dedent()
5926657Snate@binkert.org
5936657Snate@binkert.org        # Trailer
5946657Snate@binkert.org        code('''
5956657Snate@binkert.org      default:
5967805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
5976657Snate@binkert.org    }
5986657Snate@binkert.org}
5996657Snate@binkert.org
6007007Snate@binkert.org// Code to convert from a string to the enumeration
6017007Snate@binkert.org${{self.c_ident}}
6027007Snate@binkert.orgstring_to_${{self.c_ident}}(const string& str)
6036657Snate@binkert.org{
6046657Snate@binkert.org''')
6056657Snate@binkert.org
6066657Snate@binkert.org        # For each field
6077007Snate@binkert.org        start = ""
6086657Snate@binkert.org        code.indent()
6096657Snate@binkert.org        for enum in self.enums.itervalues():
6106657Snate@binkert.org            code('${start}if (str == "${{enum.ident}}") {')
6116657Snate@binkert.org            code('    return ${{self.c_ident}}_${{enum.ident}};')
6127007Snate@binkert.org            start = "} else "
6136657Snate@binkert.org        code.dedent()
6146657Snate@binkert.org
6156657Snate@binkert.org        code('''
6166657Snate@binkert.org    } else {
6177805Snilay@cs.wisc.edu        panic("Invalid string conversion for %s, type ${{self.c_ident}}", str);
6186657Snate@binkert.org    }
6196657Snate@binkert.org}
6206657Snate@binkert.org
6217007Snate@binkert.org// Code to increment an enumeration type
6227007Snate@binkert.org${{self.c_ident}}&
6237007Snate@binkert.orgoperator++(${{self.c_ident}}& e)
6247007Snate@binkert.org{
6256657Snate@binkert.org    assert(e < ${{self.c_ident}}_NUM);
6266657Snate@binkert.org    return e = ${{self.c_ident}}(e+1);
6276657Snate@binkert.org}
6286657Snate@binkert.org''')
6296657Snate@binkert.org
6306657Snate@binkert.org        # MachineType hack used to set the base level and number of
6316657Snate@binkert.org        # components for each Machine
6326657Snate@binkert.org        if self.isMachineType:
6336657Snate@binkert.org            code('''
6347007Snate@binkert.org/** \\brief returns the base vector index for each machine type to be
6357007Snate@binkert.org  * used by NetDest
6366657Snate@binkert.org  *
6376657Snate@binkert.org  * \\return the base vector index for each machine type to be used by NetDest
6386657Snate@binkert.org  * \\see NetDest.hh
6396657Snate@binkert.org  */
6407007Snate@binkert.orgint
6417007Snate@binkert.org${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj)
6426657Snate@binkert.org{
6436657Snate@binkert.org    switch(obj) {
6446657Snate@binkert.org''')
6456657Snate@binkert.org
6466657Snate@binkert.org            # For each field
6476657Snate@binkert.org            code.indent()
6486657Snate@binkert.org            for i,enum in enumerate(self.enums.itervalues()):
6496657Snate@binkert.org                code('  case ${{self.c_ident}}_${{enum.ident}}:')
6506657Snate@binkert.org                code('    return $i;')
6516657Snate@binkert.org            code.dedent()
6526657Snate@binkert.org
6536657Snate@binkert.org            # total num
6546657Snate@binkert.org            code('''
6556657Snate@binkert.org      case ${{self.c_ident}}_NUM:
6566657Snate@binkert.org        return ${{len(self.enums)}};
6576657Snate@binkert.org
6586657Snate@binkert.org      default:
6597805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
6606657Snate@binkert.org    }
6616657Snate@binkert.org}
6626657Snate@binkert.org
6636657Snate@binkert.org/** \\brief returns the machine type for each base vector index used by NetDest
6646657Snate@binkert.org *
6657007Snate@binkert.org * \\return the MachineType
6666657Snate@binkert.org */
6677007Snate@binkert.orgMachineType
6687007Snate@binkert.org${{self.c_ident}}_from_base_level(int type)
6696657Snate@binkert.org{
6706657Snate@binkert.org    switch(type) {
6716657Snate@binkert.org''')
6726657Snate@binkert.org
6736657Snate@binkert.org            # For each field
6746657Snate@binkert.org            code.indent()
6756657Snate@binkert.org            for i,enum in enumerate(self.enums.itervalues()):
6766657Snate@binkert.org                code('  case $i:')
6776657Snate@binkert.org                code('    return ${{self.c_ident}}_${{enum.ident}};')
6786657Snate@binkert.org            code.dedent()
6796657Snate@binkert.org
6806657Snate@binkert.org            # Trailer
6816657Snate@binkert.org            code('''
6826657Snate@binkert.org      default:
6837805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
6846657Snate@binkert.org    }
6856657Snate@binkert.org}
6866657Snate@binkert.org
6876657Snate@binkert.org/** \\brief The return value indicates the number of components created
6886657Snate@binkert.org * before a particular machine\'s components
6896657Snate@binkert.org *
6906657Snate@binkert.org * \\return the base number of components for each machine
6916657Snate@binkert.org */
6927007Snate@binkert.orgint
6937007Snate@binkert.org${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj)
6946657Snate@binkert.org{
6956657Snate@binkert.org    int base = 0;
6966657Snate@binkert.org    switch(obj) {
6976657Snate@binkert.org''')
6986657Snate@binkert.org
6996657Snate@binkert.org            # For each field
7006657Snate@binkert.org            code.indent()
7016657Snate@binkert.org            code('  case ${{self.c_ident}}_NUM:')
7026657Snate@binkert.org            for enum in reversed(self.enums.values()):
7039773Snilay@cs.wisc.edu                # Check if there is a defined machine with this type
7049773Snilay@cs.wisc.edu                if enum.get("Primary"):
7059773Snilay@cs.wisc.edu                    code('    base += ${{enum.ident}}_Controller::getNumControllers();')
7069773Snilay@cs.wisc.edu                else:
7079773Snilay@cs.wisc.edu                    code('    base += 0;')
7086657Snate@binkert.org                code('  case ${{self.c_ident}}_${{enum.ident}}:')
7096657Snate@binkert.org            code('    break;')
7106657Snate@binkert.org            code.dedent()
7116657Snate@binkert.org
7126657Snate@binkert.org            code('''
7136657Snate@binkert.org      default:
7147805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
7156657Snate@binkert.org    }
7166657Snate@binkert.org
7176657Snate@binkert.org    return base;
7186657Snate@binkert.org}
7196657Snate@binkert.org
7206657Snate@binkert.org/** \\brief returns the total number of components for each machine
7216657Snate@binkert.org * \\return the total number of components for each machine
7226657Snate@binkert.org */
7237007Snate@binkert.orgint
7247007Snate@binkert.org${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj)
7256657Snate@binkert.org{
7266657Snate@binkert.org    switch(obj) {
7276657Snate@binkert.org''')
7286657Snate@binkert.org
7296657Snate@binkert.org            # For each field
7306657Snate@binkert.org            for enum in self.enums.itervalues():
7319773Snilay@cs.wisc.edu                code('case ${{self.c_ident}}_${{enum.ident}}:')
7329773Snilay@cs.wisc.edu                if enum.get("Primary"):
7339773Snilay@cs.wisc.edu                    code('return ${{enum.ident}}_Controller::getNumControllers();')
7349773Snilay@cs.wisc.edu                else:
7359773Snilay@cs.wisc.edu                    code('return 0;')
7366657Snate@binkert.org
7376657Snate@binkert.org            # total num
7386657Snate@binkert.org            code('''
7396657Snate@binkert.org      case ${{self.c_ident}}_NUM:
7406657Snate@binkert.org      default:
7417805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
7426657Snate@binkert.org    }
7436657Snate@binkert.org}
7446657Snate@binkert.org''')
7456657Snate@binkert.org
7468602Snilay@cs.wisc.edu            for enum in self.enums.itervalues():
7478602Snilay@cs.wisc.edu                if enum.ident == "DMA":
7488602Snilay@cs.wisc.edu                    code('''
7498602Snilay@cs.wisc.eduMachineID
7508602Snilay@cs.wisc.edumap_Address_to_DMA(const Address &addr)
7518602Snilay@cs.wisc.edu{
7528602Snilay@cs.wisc.edu      MachineID dma = {MachineType_DMA, 0};
7538602Snilay@cs.wisc.edu      return dma;
7548602Snilay@cs.wisc.edu}
7558602Snilay@cs.wisc.edu''')
7568602Snilay@cs.wisc.edu
7578602Snilay@cs.wisc.edu                code('''
7588602Snilay@cs.wisc.edu
7598602Snilay@cs.wisc.eduMachineID
7608602Snilay@cs.wisc.eduget${{enum.ident}}MachineID(NodeID RubyNode)
7618602Snilay@cs.wisc.edu{
7628602Snilay@cs.wisc.edu      MachineID mach = {MachineType_${{enum.ident}}, RubyNode};
7638602Snilay@cs.wisc.edu      return mach;
7648602Snilay@cs.wisc.edu}
7658602Snilay@cs.wisc.edu''')
7668602Snilay@cs.wisc.edu
7676657Snate@binkert.org        # Write the file
7686657Snate@binkert.org        code.write(path, "%s.cc" % self.c_ident)
7696657Snate@binkert.org
7706657Snate@binkert.org__all__ = [ "Type" ]
771