Type.py revision 10228
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 Method(object):
476657Snate@binkert.org    def __init__(self, return_type, param_types):
486657Snate@binkert.org        self.return_type = return_type
496657Snate@binkert.org        self.param_types = param_types
506657Snate@binkert.org
516657Snate@binkert.orgclass Type(Symbol):
526657Snate@binkert.org    def __init__(self, table, ident, location, pairs, machine=None):
536657Snate@binkert.org        super(Type, self).__init__(table, ident, location, pairs)
546657Snate@binkert.org        self.c_ident = ident
556882SBrad.Beckmann@amd.com        self.abstract_ident = ""
566657Snate@binkert.org        if machine:
576657Snate@binkert.org            if self.isExternal or self.isPrimitive:
586657Snate@binkert.org                if "external_name" in self:
596657Snate@binkert.org                    self.c_ident = self["external_name"]
606657Snate@binkert.org            else:
616657Snate@binkert.org                # Append with machine name
626657Snate@binkert.org                self.c_ident = "%s_%s" % (machine, ident)
636657Snate@binkert.org
646657Snate@binkert.org        self.pairs.setdefault("desc", "No description avaliable")
656657Snate@binkert.org
666657Snate@binkert.org        # check for interface that this Type implements
676657Snate@binkert.org        if "interface" in self:
686657Snate@binkert.org            interface = self["interface"]
696657Snate@binkert.org            if interface in ("Message", "NetworkMessage"):
706657Snate@binkert.org                self["message"] = "yes"
716657Snate@binkert.org            if interface == "NetworkMessage":
726657Snate@binkert.org                self["networkmessage"] = "yes"
736657Snate@binkert.org
746657Snate@binkert.org        # FIXME - all of the following id comparisons are fragile hacks
7510228Snilay@cs.wisc.edu        if self.ident in ("CacheMemory"):
766657Snate@binkert.org            self["cache"] = "yes"
776657Snate@binkert.org
7810228Snilay@cs.wisc.edu        if self.ident in ("TBETable"):
796657Snate@binkert.org            self["tbe"] = "yes"
806657Snate@binkert.org
816657Snate@binkert.org        if self.ident == "TimerTable":
826657Snate@binkert.org            self["timer"] = "yes"
836657Snate@binkert.org
846657Snate@binkert.org        if self.ident == "DirectoryMemory":
856657Snate@binkert.org            self["dir"] = "yes"
866657Snate@binkert.org
876657Snate@binkert.org        if self.ident == "PersistentTable":
886657Snate@binkert.org            self["persistent"] = "yes"
896657Snate@binkert.org
906657Snate@binkert.org        if self.ident == "Prefetcher":
916657Snate@binkert.org            self["prefetcher"] = "yes"
926657Snate@binkert.org
936657Snate@binkert.org        self.isMachineType = (ident == "MachineType")
946657Snate@binkert.org
958086SBrad.Beckmann@amd.com        self.isStateDecl = ("state_decl" in self)
968086SBrad.Beckmann@amd.com        self.statePermPairs = []
978086SBrad.Beckmann@amd.com
986657Snate@binkert.org        self.data_members = orderdict()
996657Snate@binkert.org
1006657Snate@binkert.org        # Methods
1016657Snate@binkert.org        self.methods = {}
1029298Snilay@cs.wisc.edu        self.functions = {}
1036657Snate@binkert.org
1046657Snate@binkert.org        # Enums
1056657Snate@binkert.org        self.enums = orderdict()
1066657Snate@binkert.org
1076657Snate@binkert.org    @property
1086657Snate@binkert.org    def isPrimitive(self):
1096657Snate@binkert.org        return "primitive" in self
1106657Snate@binkert.org    @property
1116657Snate@binkert.org    def isNetworkMessage(self):
1126657Snate@binkert.org        return "networkmessage" in self
1136657Snate@binkert.org    @property
1146657Snate@binkert.org    def isMessage(self):
1156657Snate@binkert.org        return "message" in self
1166657Snate@binkert.org    @property
1176657Snate@binkert.org    def isBuffer(self):
1186657Snate@binkert.org        return "buffer" in self
1196657Snate@binkert.org    @property
1206657Snate@binkert.org    def isInPort(self):
1216657Snate@binkert.org        return "inport" in self
1226657Snate@binkert.org    @property
1236657Snate@binkert.org    def isOutPort(self):
1246657Snate@binkert.org        return "outport" in self
1256657Snate@binkert.org    @property
1266657Snate@binkert.org    def isEnumeration(self):
1276657Snate@binkert.org        return "enumeration" in self
1286657Snate@binkert.org    @property
1296657Snate@binkert.org    def isExternal(self):
1306657Snate@binkert.org        return "external" in self
1316657Snate@binkert.org    @property
1326657Snate@binkert.org    def isGlobal(self):
1336657Snate@binkert.org        return "global" in self
1346657Snate@binkert.org    @property
1356657Snate@binkert.org    def isInterface(self):
1366657Snate@binkert.org        return "interface" in self
1376657Snate@binkert.org
1386657Snate@binkert.org    # Return false on error
1399298Snilay@cs.wisc.edu    def addDataMember(self, ident, type, pairs, init_code):
1406657Snate@binkert.org        if ident in self.data_members:
1416657Snate@binkert.org            return False
1426657Snate@binkert.org
1436657Snate@binkert.org        member = DataMember(ident, type, pairs, init_code)
1446657Snate@binkert.org        self.data_members[ident] = member
1456657Snate@binkert.org
1469302Snilay@cs.wisc.edu        var = Var(self.symtab, ident, self.location, type,
1479302Snilay@cs.wisc.edu                "m_%s" % ident, {}, None)
1489302Snilay@cs.wisc.edu        self.symtab.registerSym(ident, var)
1496657Snate@binkert.org        return True
1506657Snate@binkert.org
1516657Snate@binkert.org    def dataMemberType(self, ident):
1526657Snate@binkert.org        return self.data_members[ident].type
1536657Snate@binkert.org
1546657Snate@binkert.org    def methodId(self, name, param_type_vec):
1556657Snate@binkert.org        return '_'.join([name] + [ pt.c_ident for pt in param_type_vec ])
1566657Snate@binkert.org
1576882SBrad.Beckmann@amd.com    def methodIdAbstract(self, name, param_type_vec):
1586882SBrad.Beckmann@amd.com        return '_'.join([name] + [ pt.abstract_ident for pt in param_type_vec ])
1596882SBrad.Beckmann@amd.com
1608086SBrad.Beckmann@amd.com    def statePermPairAdd(self, state_name, perm_name):
1618086SBrad.Beckmann@amd.com        self.statePermPairs.append([state_name, perm_name])
1628086SBrad.Beckmann@amd.com
1639298Snilay@cs.wisc.edu    def addMethod(self, name, return_type, param_type_vec):
1646657Snate@binkert.org        ident = self.methodId(name, param_type_vec)
1656657Snate@binkert.org        if ident in self.methods:
1666657Snate@binkert.org            return False
1676657Snate@binkert.org
1686657Snate@binkert.org        self.methods[ident] = Method(return_type, param_type_vec)
1696657Snate@binkert.org        return True
1706657Snate@binkert.org
1719298Snilay@cs.wisc.edu    # Ideally either this function or the one above should exist. But
1729298Snilay@cs.wisc.edu    # methods and functions have different structures right now.
1739298Snilay@cs.wisc.edu    # Hence, these are different, at least for the time being.
1749298Snilay@cs.wisc.edu    def addFunc(self, func):
1759298Snilay@cs.wisc.edu        ident = self.methodId(func.ident, func.param_types)
1769298Snilay@cs.wisc.edu        if ident in self.functions:
1779298Snilay@cs.wisc.edu            return False
1789298Snilay@cs.wisc.edu
1799298Snilay@cs.wisc.edu        self.functions[ident] = func
1809298Snilay@cs.wisc.edu        return True
1819298Snilay@cs.wisc.edu
1829298Snilay@cs.wisc.edu    def addEnum(self, ident, pairs):
1836657Snate@binkert.org        if ident in self.enums:
1846657Snate@binkert.org            return False
1856657Snate@binkert.org
1866657Snate@binkert.org        self.enums[ident] = Enumeration(ident, pairs)
1876657Snate@binkert.org
1886657Snate@binkert.org        # Add default
1896657Snate@binkert.org        if "default" not in self:
1906657Snate@binkert.org            self["default"] = "%s_NUM" % self.c_ident
1916657Snate@binkert.org
1926657Snate@binkert.org        return True
1936657Snate@binkert.org
1949219Spower.jg@gmail.com    def writeCodeFiles(self, path, includes):
1956657Snate@binkert.org        if self.isExternal:
1966657Snate@binkert.org            # Do nothing
1976657Snate@binkert.org            pass
1986657Snate@binkert.org        elif self.isEnumeration:
1996657Snate@binkert.org            self.printEnumHH(path)
2006657Snate@binkert.org            self.printEnumCC(path)
2016657Snate@binkert.org        else:
2026657Snate@binkert.org            # User defined structs and messages
2036657Snate@binkert.org            self.printTypeHH(path)
2046657Snate@binkert.org            self.printTypeCC(path)
2056657Snate@binkert.org
2066657Snate@binkert.org    def printTypeHH(self, path):
2076999Snate@binkert.org        code = self.symtab.codeFormatter()
2086657Snate@binkert.org        code('''
2096657Snate@binkert.org/** \\file ${{self.c_ident}}.hh
2106657Snate@binkert.org *
2116657Snate@binkert.org *
2126657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
2136657Snate@binkert.org */
2146657Snate@binkert.org
2157007Snate@binkert.org#ifndef __${{self.c_ident}}_HH__
2167007Snate@binkert.org#define __${{self.c_ident}}_HH__
2176657Snate@binkert.org
2187002Snate@binkert.org#include <iostream>
2197002Snate@binkert.org
2209466Snilay@cs.wisc.edu#include "mem/ruby/slicc_interface/RubySlicc_Util.hh"
2216657Snate@binkert.org''')
2226657Snate@binkert.org
2236657Snate@binkert.org        for dm in self.data_members.values():
2246657Snate@binkert.org            if not dm.type.isPrimitive:
2256657Snate@binkert.org                code('#include "mem/protocol/$0.hh"', dm.type.c_ident)
2266657Snate@binkert.org
2276657Snate@binkert.org        parent = ""
2286657Snate@binkert.org        if "interface" in self:
2296657Snate@binkert.org            code('#include "mem/protocol/$0.hh"', self["interface"])
2306657Snate@binkert.org            parent = " :  public %s" % self["interface"]
2316657Snate@binkert.org
2326657Snate@binkert.org        code('''
2337007Snate@binkert.org$klass ${{self.c_ident}}$parent
2347007Snate@binkert.org{
2356657Snate@binkert.org  public:
2369466Snilay@cs.wisc.edu    ${{self.c_ident}}
2376657Snate@binkert.org''', klass="class")
2386657Snate@binkert.org
2399466Snilay@cs.wisc.edu        if self.isMessage:
2409508Snilay@cs.wisc.edu            code('(Tick curTime) : %s(curTime) {' % self["interface"])
2419466Snilay@cs.wisc.edu        else:
2429466Snilay@cs.wisc.edu            code('()\n\t\t{')
2439466Snilay@cs.wisc.edu
2446657Snate@binkert.org        code.indent()
2456657Snate@binkert.org        if not self.isGlobal:
2466657Snate@binkert.org            code.indent()
2476657Snate@binkert.org            for dm in self.data_members.values():
2486657Snate@binkert.org                ident = dm.ident
2496657Snate@binkert.org                if "default" in dm:
2506657Snate@binkert.org                    # look for default value
2516657Snate@binkert.org                    code('m_$ident = ${{dm["default"]}}; // default for this field')
2526657Snate@binkert.org                elif "default" in dm.type:
2536657Snate@binkert.org                    # Look for the type default
2546657Snate@binkert.org                    tid = dm.type.c_ident
2556657Snate@binkert.org                    code('m_$ident = ${{dm.type["default"]}}; // default value of $tid')
2566657Snate@binkert.org                else:
2576657Snate@binkert.org                    code('// m_$ident has no default')
2586657Snate@binkert.org            code.dedent()
2596657Snate@binkert.org        code('}')
2606657Snate@binkert.org
2617453Snate@binkert.org        # ******** Copy constructor ********
2627453Snate@binkert.org        if not self.isGlobal:
2637453Snate@binkert.org            code('${{self.c_ident}}(const ${{self.c_ident}}&other)')
2647453Snate@binkert.org
2657453Snate@binkert.org            # Call superclass constructor
2667453Snate@binkert.org            if "interface" in self:
2677453Snate@binkert.org                code('    : ${{self["interface"]}}(other)')
2687453Snate@binkert.org
2697453Snate@binkert.org            code('{')
2707453Snate@binkert.org            code.indent()
2717453Snate@binkert.org
2727453Snate@binkert.org            for dm in self.data_members.values():
2737453Snate@binkert.org                code('m_${{dm.ident}} = other.m_${{dm.ident}};')
2747453Snate@binkert.org
2757453Snate@binkert.org            code.dedent()
2767453Snate@binkert.org            code('}')
2777453Snate@binkert.org
2786657Snate@binkert.org        # ******** Full init constructor ********
2796657Snate@binkert.org        if not self.isGlobal:
2806657Snate@binkert.org            params = [ 'const %s& local_%s' % (dm.type.c_ident, dm.ident) \
2816657Snate@binkert.org                       for dm in self.data_members.itervalues() ]
2829466Snilay@cs.wisc.edu            params = ', '.join(params)
2836657Snate@binkert.org
2849466Snilay@cs.wisc.edu            if self.isMessage:
2859508Snilay@cs.wisc.edu                params = "const Tick curTime, " + params
2869466Snilay@cs.wisc.edu
2876657Snate@binkert.org            code('${{self.c_ident}}($params)')
2886657Snate@binkert.org
2896657Snate@binkert.org            # Call superclass constructor
2906657Snate@binkert.org            if "interface" in self:
2919466Snilay@cs.wisc.edu                if self.isMessage:
2929466Snilay@cs.wisc.edu                    code('    : ${{self["interface"]}}(curTime)')
2939466Snilay@cs.wisc.edu                else:
2949466Snilay@cs.wisc.edu                    code('    : ${{self["interface"]}}()')
2956657Snate@binkert.org
2966657Snate@binkert.org            code('{')
2976657Snate@binkert.org            code.indent()
2986657Snate@binkert.org            for dm in self.data_members.values():
2996657Snate@binkert.org                code('m_${{dm.ident}} = local_${{dm.ident}};')
3006657Snate@binkert.org                if "nextLineCallHack" in dm:
3016657Snate@binkert.org                    code('m_${{dm.ident}}${{dm["nextLineCallHack"]}};')
3026657Snate@binkert.org
3036657Snate@binkert.org            code.dedent()
3046657Snate@binkert.org            code('}')
3056657Snate@binkert.org
3069466Snilay@cs.wisc.edu        # create a clone member
3077453Snate@binkert.org        code('''
3087453Snate@binkert.org${{self.c_ident}}*
3097007Snate@binkert.orgclone() const
3107007Snate@binkert.org{
3117453Snate@binkert.org     return new ${{self.c_ident}}(*this);
3127007Snate@binkert.org}
3136657Snate@binkert.org''')
3146657Snate@binkert.org
3156657Snate@binkert.org        if not self.isGlobal:
3166657Snate@binkert.org            # const Get methods for each field
3176657Snate@binkert.org            code('// Const accessors methods for each field')
3186657Snate@binkert.org            for dm in self.data_members.values():
3196657Snate@binkert.org                code('''
3206657Snate@binkert.org/** \\brief Const accessor method for ${{dm.ident}} field.
3216657Snate@binkert.org *  \\return ${{dm.ident}} field
3226657Snate@binkert.org */
3237007Snate@binkert.orgconst ${{dm.type.c_ident}}&
3247007Snate@binkert.orgget${{dm.ident}}() const
3257007Snate@binkert.org{
3267007Snate@binkert.org    return m_${{dm.ident}};
3277007Snate@binkert.org}
3286657Snate@binkert.org''')
3296657Snate@binkert.org
3306657Snate@binkert.org            # Non-const Get methods for each field
3316657Snate@binkert.org            code('// Non const Accessors methods for each field')
3326657Snate@binkert.org            for dm in self.data_members.values():
3336657Snate@binkert.org                code('''
3346657Snate@binkert.org/** \\brief Non-const accessor method for ${{dm.ident}} field.
3356657Snate@binkert.org *  \\return ${{dm.ident}} field
3366657Snate@binkert.org */
3377007Snate@binkert.org${{dm.type.c_ident}}&
3387007Snate@binkert.orgget${{dm.ident}}()
3397007Snate@binkert.org{
3407007Snate@binkert.org    return m_${{dm.ident}};
3417007Snate@binkert.org}
3426657Snate@binkert.org''')
3436657Snate@binkert.org
3446657Snate@binkert.org            #Set methods for each field
3456657Snate@binkert.org            code('// Mutator methods for each field')
3466657Snate@binkert.org            for dm in self.data_members.values():
3476657Snate@binkert.org                code('''
3486657Snate@binkert.org/** \\brief Mutator method for ${{dm.ident}} field */
3497007Snate@binkert.orgvoid
3507007Snate@binkert.orgset${{dm.ident}}(const ${{dm.type.c_ident}}& local_${{dm.ident}})
3517007Snate@binkert.org{
3527007Snate@binkert.org    m_${{dm.ident}} = local_${{dm.ident}};
3537007Snate@binkert.org}
3546657Snate@binkert.org''')
3556657Snate@binkert.org
3567002Snate@binkert.org        code('void print(std::ostream& out) const;')
3576657Snate@binkert.org        code.dedent()
3586657Snate@binkert.org        code('  //private:')
3596657Snate@binkert.org        code.indent()
3606657Snate@binkert.org
3616657Snate@binkert.org        # Data members for each field
3626657Snate@binkert.org        for dm in self.data_members.values():
3636657Snate@binkert.org            if "abstract" not in dm:
3646657Snate@binkert.org                const = ""
3656657Snate@binkert.org                init = ""
3666657Snate@binkert.org
3676657Snate@binkert.org                # global structure
3686657Snate@binkert.org                if self.isGlobal:
3696657Snate@binkert.org                    const = "static const "
3706657Snate@binkert.org
3716657Snate@binkert.org                # init value
3726657Snate@binkert.org                if dm.init_code:
3736657Snate@binkert.org                    # only global structure can have init value here
3746657Snate@binkert.org                    assert self.isGlobal
3756657Snate@binkert.org                    init = " = %s" % (dm.init_code)
3766657Snate@binkert.org
3776657Snate@binkert.org                if "desc" in dm:
3787007Snate@binkert.org                    code('/** ${{dm["desc"]}} */')
3796657Snate@binkert.org
3807007Snate@binkert.org                code('$const${{dm.type.c_ident}} m_${{dm.ident}}$init;')
3816657Snate@binkert.org
3829298Snilay@cs.wisc.edu        # Prototypes for functions defined for the Type
3839298Snilay@cs.wisc.edu        for item in self.functions:
3849298Snilay@cs.wisc.edu            proto = self.functions[item].prototype
3859298Snilay@cs.wisc.edu            if proto:
3869298Snilay@cs.wisc.edu                code('$proto')
3879298Snilay@cs.wisc.edu
3886657Snate@binkert.org        code.dedent()
3896657Snate@binkert.org        code('};')
3906657Snate@binkert.org
3916657Snate@binkert.org        code('''
3927055Snate@binkert.orginline std::ostream&
3937007Snate@binkert.orgoperator<<(std::ostream& out, const ${{self.c_ident}}& obj)
3946657Snate@binkert.org{
3956657Snate@binkert.org    obj.print(out);
3967002Snate@binkert.org    out << std::flush;
3976657Snate@binkert.org    return out;
3986657Snate@binkert.org}
3996657Snate@binkert.org
4007007Snate@binkert.org#endif // __${{self.c_ident}}_HH__
4016657Snate@binkert.org''')
4026657Snate@binkert.org
4036657Snate@binkert.org        code.write(path, "%s.hh" % self.c_ident)
4046657Snate@binkert.org
4056657Snate@binkert.org    def printTypeCC(self, path):
4066999Snate@binkert.org        code = self.symtab.codeFormatter()
4076657Snate@binkert.org
4086657Snate@binkert.org        code('''
4096657Snate@binkert.org/** \\file ${{self.c_ident}}.cc
4106657Snate@binkert.org *
4116657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
4126657Snate@binkert.org */
4136657Snate@binkert.org
4147002Snate@binkert.org#include <iostream>
4157002Snate@binkert.org
4166657Snate@binkert.org#include "mem/protocol/${{self.c_ident}}.hh"
4179499Snilay@cs.wisc.edu#include "mem/ruby/common/Global.hh"
4189499Snilay@cs.wisc.edu#include "mem/ruby/system/System.hh"
4197002Snate@binkert.org
4207002Snate@binkert.orgusing namespace std;
4216657Snate@binkert.org''')
4226657Snate@binkert.org
4236657Snate@binkert.org        code('''
4246657Snate@binkert.org/** \\brief Print the state of this object */
4257007Snate@binkert.orgvoid
4267007Snate@binkert.org${{self.c_ident}}::print(ostream& out) const
4276657Snate@binkert.org{
4286657Snate@binkert.org    out << "[${{self.c_ident}}: ";
4296657Snate@binkert.org''')
4306657Snate@binkert.org
4316657Snate@binkert.org        # For each field
4326657Snate@binkert.org        code.indent()
4336657Snate@binkert.org        for dm in self.data_members.values():
4346657Snate@binkert.org            code('out << "${{dm.ident}} = " << m_${{dm.ident}} << " ";''')
4356657Snate@binkert.org
4366657Snate@binkert.org        if self.isMessage:
4379206Snilay@cs.wisc.edu            code('out << "Time = " << g_system_ptr->clockPeriod() * getTime() << " ";')
4386657Snate@binkert.org        code.dedent()
4396657Snate@binkert.org
4406657Snate@binkert.org        # Trailer
4416657Snate@binkert.org        code('''
4426657Snate@binkert.org    out << "]";
4436657Snate@binkert.org}''')
4446657Snate@binkert.org
4459298Snilay@cs.wisc.edu        # print the code for the functions in the type
4469298Snilay@cs.wisc.edu        for item in self.functions:
4479298Snilay@cs.wisc.edu            code(self.functions[item].generateCode())
4489298Snilay@cs.wisc.edu
4496657Snate@binkert.org        code.write(path, "%s.cc" % self.c_ident)
4506657Snate@binkert.org
4516657Snate@binkert.org    def printEnumHH(self, path):
4526999Snate@binkert.org        code = self.symtab.codeFormatter()
4536657Snate@binkert.org        code('''
4546657Snate@binkert.org/** \\file ${{self.c_ident}}.hh
4556657Snate@binkert.org *
4566657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
4576657Snate@binkert.org */
4587007Snate@binkert.org
4597007Snate@binkert.org#ifndef __${{self.c_ident}}_HH__
4607007Snate@binkert.org#define __${{self.c_ident}}_HH__
4616657Snate@binkert.org
4627002Snate@binkert.org#include <iostream>
4637002Snate@binkert.org#include <string>
4647002Snate@binkert.org
4658086SBrad.Beckmann@amd.com''')
4668086SBrad.Beckmann@amd.com        if self.isStateDecl:
4678086SBrad.Beckmann@amd.com            code('#include "mem/protocol/AccessPermission.hh"')
4688086SBrad.Beckmann@amd.com
4698602Snilay@cs.wisc.edu        if self.isMachineType:
4708602Snilay@cs.wisc.edu            code('#include "base/misc.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
5278086SBrad.Beckmann@amd.com        if self.isStateDecl:
5288086SBrad.Beckmann@amd.com            code('''
5298086SBrad.Beckmann@amd.com
5308086SBrad.Beckmann@amd.com// Code to convert the current state to an access permission
5318086SBrad.Beckmann@amd.comAccessPermission ${{self.c_ident}}_to_permission(const ${{self.c_ident}}& obj);
5328086SBrad.Beckmann@amd.com
5338086SBrad.Beckmann@amd.com''')
5348086SBrad.Beckmann@amd.com
5356657Snate@binkert.org        # Trailer
5366657Snate@binkert.org        code('''
5377002Snate@binkert.orgstd::ostream& operator<<(std::ostream& out, const ${{self.c_ident}}& obj);
5386657Snate@binkert.org
5397007Snate@binkert.org#endif // __${{self.c_ident}}_HH__
5406657Snate@binkert.org''')
5416657Snate@binkert.org
5426657Snate@binkert.org        code.write(path, "%s.hh" % self.c_ident)
5436657Snate@binkert.org
5446657Snate@binkert.org    def printEnumCC(self, path):
5456999Snate@binkert.org        code = self.symtab.codeFormatter()
5466657Snate@binkert.org        code('''
5476657Snate@binkert.org/** \\file ${{self.c_ident}}.hh
5486657Snate@binkert.org *
5496657Snate@binkert.org * Auto generated C++ code started by $__file__:$__line__
5506657Snate@binkert.org */
5516657Snate@binkert.org
5527832Snate@binkert.org#include <cassert>
5537002Snate@binkert.org#include <iostream>
5547002Snate@binkert.org#include <string>
5557002Snate@binkert.org
5567805Snilay@cs.wisc.edu#include "base/misc.hh"
5576657Snate@binkert.org#include "mem/protocol/${{self.c_ident}}.hh"
5586657Snate@binkert.org
5597002Snate@binkert.orgusing namespace std;
5607002Snate@binkert.org
5616657Snate@binkert.org''')
5626657Snate@binkert.org
5638086SBrad.Beckmann@amd.com        if self.isStateDecl:
5648086SBrad.Beckmann@amd.com            code('''
5658086SBrad.Beckmann@amd.com// Code to convert the current state to an access permission
5668086SBrad.Beckmann@amd.comAccessPermission ${{self.c_ident}}_to_permission(const ${{self.c_ident}}& obj)
5678086SBrad.Beckmann@amd.com{
5688086SBrad.Beckmann@amd.com    switch(obj) {
5698086SBrad.Beckmann@amd.com''')
5708086SBrad.Beckmann@amd.com            # For each case
5718086SBrad.Beckmann@amd.com            code.indent()
5728086SBrad.Beckmann@amd.com            for statePerm in self.statePermPairs:
5738086SBrad.Beckmann@amd.com                code('  case ${{self.c_ident}}_${{statePerm[0]}}:')
5748086SBrad.Beckmann@amd.com                code('    return AccessPermission_${{statePerm[1]}};')
5758086SBrad.Beckmann@amd.com            code.dedent()
5768086SBrad.Beckmann@amd.com            code ('''
5778086SBrad.Beckmann@amd.com      default:
5788086SBrad.Beckmann@amd.com        panic("Unknown state access permission converstion for ${{self.c_ident}}");
5798086SBrad.Beckmann@amd.com    }
5808086SBrad.Beckmann@amd.com}
5818086SBrad.Beckmann@amd.com
5828086SBrad.Beckmann@amd.com''')
5838086SBrad.Beckmann@amd.com
5846657Snate@binkert.org        if self.isMachineType:
5856657Snate@binkert.org            for enum in self.enums.itervalues():
5869773Snilay@cs.wisc.edu                if enum.get("Primary"):
5879773Snilay@cs.wisc.edu                    code('#include "mem/protocol/${{enum.ident}}_Controller.hh"')
5888602Snilay@cs.wisc.edu            code('#include "mem/ruby/system/MachineID.hh"')
5896657Snate@binkert.org
5906657Snate@binkert.org        code('''
5917007Snate@binkert.org// Code for output operator
5927007Snate@binkert.orgostream&
5937007Snate@binkert.orgoperator<<(ostream& out, const ${{self.c_ident}}& obj)
5946657Snate@binkert.org{
5956657Snate@binkert.org    out << ${{self.c_ident}}_to_string(obj);
5966657Snate@binkert.org    out << flush;
5976657Snate@binkert.org    return out;
5986657Snate@binkert.org}
5996657Snate@binkert.org
6007007Snate@binkert.org// Code to convert state to a string
6017007Snate@binkert.orgstring
6027007Snate@binkert.org${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj)
6036657Snate@binkert.org{
6046657Snate@binkert.org    switch(obj) {
6056657Snate@binkert.org''')
6066657Snate@binkert.org
6076657Snate@binkert.org        # For each field
6086657Snate@binkert.org        code.indent()
6096657Snate@binkert.org        for enum in self.enums.itervalues():
6106657Snate@binkert.org            code('  case ${{self.c_ident}}_${{enum.ident}}:')
6116657Snate@binkert.org            code('    return "${{enum.ident}}";')
6126657Snate@binkert.org        code.dedent()
6136657Snate@binkert.org
6146657Snate@binkert.org        # Trailer
6156657Snate@binkert.org        code('''
6166657Snate@binkert.org      default:
6177805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
6186657Snate@binkert.org    }
6196657Snate@binkert.org}
6206657Snate@binkert.org
6217007Snate@binkert.org// Code to convert from a string to the enumeration
6227007Snate@binkert.org${{self.c_ident}}
6237007Snate@binkert.orgstring_to_${{self.c_ident}}(const string& str)
6246657Snate@binkert.org{
6256657Snate@binkert.org''')
6266657Snate@binkert.org
6276657Snate@binkert.org        # For each field
6287007Snate@binkert.org        start = ""
6296657Snate@binkert.org        code.indent()
6306657Snate@binkert.org        for enum in self.enums.itervalues():
6316657Snate@binkert.org            code('${start}if (str == "${{enum.ident}}") {')
6326657Snate@binkert.org            code('    return ${{self.c_ident}}_${{enum.ident}};')
6337007Snate@binkert.org            start = "} else "
6346657Snate@binkert.org        code.dedent()
6356657Snate@binkert.org
6366657Snate@binkert.org        code('''
6376657Snate@binkert.org    } else {
6387805Snilay@cs.wisc.edu        panic("Invalid string conversion for %s, type ${{self.c_ident}}", str);
6396657Snate@binkert.org    }
6406657Snate@binkert.org}
6416657Snate@binkert.org
6427007Snate@binkert.org// Code to increment an enumeration type
6437007Snate@binkert.org${{self.c_ident}}&
6447007Snate@binkert.orgoperator++(${{self.c_ident}}& e)
6457007Snate@binkert.org{
6466657Snate@binkert.org    assert(e < ${{self.c_ident}}_NUM);
6476657Snate@binkert.org    return e = ${{self.c_ident}}(e+1);
6486657Snate@binkert.org}
6496657Snate@binkert.org''')
6506657Snate@binkert.org
6516657Snate@binkert.org        # MachineType hack used to set the base level and number of
6526657Snate@binkert.org        # components for each Machine
6536657Snate@binkert.org        if self.isMachineType:
6546657Snate@binkert.org            code('''
6557007Snate@binkert.org/** \\brief returns the base vector index for each machine type to be
6567007Snate@binkert.org  * used by NetDest
6576657Snate@binkert.org  *
6586657Snate@binkert.org  * \\return the base vector index for each machine type to be used by NetDest
6596657Snate@binkert.org  * \\see NetDest.hh
6606657Snate@binkert.org  */
6617007Snate@binkert.orgint
6627007Snate@binkert.org${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj)
6636657Snate@binkert.org{
6646657Snate@binkert.org    switch(obj) {
6656657Snate@binkert.org''')
6666657Snate@binkert.org
6676657Snate@binkert.org            # For each field
6686657Snate@binkert.org            code.indent()
6696657Snate@binkert.org            for i,enum in enumerate(self.enums.itervalues()):
6706657Snate@binkert.org                code('  case ${{self.c_ident}}_${{enum.ident}}:')
6716657Snate@binkert.org                code('    return $i;')
6726657Snate@binkert.org            code.dedent()
6736657Snate@binkert.org
6746657Snate@binkert.org            # total num
6756657Snate@binkert.org            code('''
6766657Snate@binkert.org      case ${{self.c_ident}}_NUM:
6776657Snate@binkert.org        return ${{len(self.enums)}};
6786657Snate@binkert.org
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 returns the machine type for each base vector index used by NetDest
6856657Snate@binkert.org *
6867007Snate@binkert.org * \\return the MachineType
6876657Snate@binkert.org */
6887007Snate@binkert.orgMachineType
6897007Snate@binkert.org${{self.c_ident}}_from_base_level(int type)
6906657Snate@binkert.org{
6916657Snate@binkert.org    switch(type) {
6926657Snate@binkert.org''')
6936657Snate@binkert.org
6946657Snate@binkert.org            # For each field
6956657Snate@binkert.org            code.indent()
6966657Snate@binkert.org            for i,enum in enumerate(self.enums.itervalues()):
6976657Snate@binkert.org                code('  case $i:')
6986657Snate@binkert.org                code('    return ${{self.c_ident}}_${{enum.ident}};')
6996657Snate@binkert.org            code.dedent()
7006657Snate@binkert.org
7016657Snate@binkert.org            # Trailer
7026657Snate@binkert.org            code('''
7036657Snate@binkert.org      default:
7047805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
7056657Snate@binkert.org    }
7066657Snate@binkert.org}
7076657Snate@binkert.org
7086657Snate@binkert.org/** \\brief The return value indicates the number of components created
7096657Snate@binkert.org * before a particular machine\'s components
7106657Snate@binkert.org *
7116657Snate@binkert.org * \\return the base number of components for each machine
7126657Snate@binkert.org */
7137007Snate@binkert.orgint
7147007Snate@binkert.org${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj)
7156657Snate@binkert.org{
7166657Snate@binkert.org    int base = 0;
7176657Snate@binkert.org    switch(obj) {
7186657Snate@binkert.org''')
7196657Snate@binkert.org
7206657Snate@binkert.org            # For each field
7216657Snate@binkert.org            code.indent()
7226657Snate@binkert.org            code('  case ${{self.c_ident}}_NUM:')
7236657Snate@binkert.org            for enum in reversed(self.enums.values()):
7249773Snilay@cs.wisc.edu                # Check if there is a defined machine with this type
7259773Snilay@cs.wisc.edu                if enum.get("Primary"):
7269773Snilay@cs.wisc.edu                    code('    base += ${{enum.ident}}_Controller::getNumControllers();')
7279773Snilay@cs.wisc.edu                else:
7289773Snilay@cs.wisc.edu                    code('    base += 0;')
7296657Snate@binkert.org                code('  case ${{self.c_ident}}_${{enum.ident}}:')
7306657Snate@binkert.org            code('    break;')
7316657Snate@binkert.org            code.dedent()
7326657Snate@binkert.org
7336657Snate@binkert.org            code('''
7346657Snate@binkert.org      default:
7357805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
7366657Snate@binkert.org    }
7376657Snate@binkert.org
7386657Snate@binkert.org    return base;
7396657Snate@binkert.org}
7406657Snate@binkert.org
7416657Snate@binkert.org/** \\brief returns the total number of components for each machine
7426657Snate@binkert.org * \\return the total number of components for each machine
7436657Snate@binkert.org */
7447007Snate@binkert.orgint
7457007Snate@binkert.org${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj)
7466657Snate@binkert.org{
7476657Snate@binkert.org    switch(obj) {
7486657Snate@binkert.org''')
7496657Snate@binkert.org
7506657Snate@binkert.org            # For each field
7516657Snate@binkert.org            for enum in self.enums.itervalues():
7529773Snilay@cs.wisc.edu                code('case ${{self.c_ident}}_${{enum.ident}}:')
7539773Snilay@cs.wisc.edu                if enum.get("Primary"):
7549773Snilay@cs.wisc.edu                    code('return ${{enum.ident}}_Controller::getNumControllers();')
7559773Snilay@cs.wisc.edu                else:
7569773Snilay@cs.wisc.edu                    code('return 0;')
7576657Snate@binkert.org
7586657Snate@binkert.org            # total num
7596657Snate@binkert.org            code('''
7606657Snate@binkert.org      case ${{self.c_ident}}_NUM:
7616657Snate@binkert.org      default:
7627805Snilay@cs.wisc.edu        panic("Invalid range for type ${{self.c_ident}}");
7636657Snate@binkert.org    }
7646657Snate@binkert.org}
7656657Snate@binkert.org''')
7666657Snate@binkert.org
7678602Snilay@cs.wisc.edu            for enum in self.enums.itervalues():
7688602Snilay@cs.wisc.edu                if enum.ident == "DMA":
7698602Snilay@cs.wisc.edu                    code('''
7708602Snilay@cs.wisc.eduMachineID
7718602Snilay@cs.wisc.edumap_Address_to_DMA(const Address &addr)
7728602Snilay@cs.wisc.edu{
7738602Snilay@cs.wisc.edu      MachineID dma = {MachineType_DMA, 0};
7748602Snilay@cs.wisc.edu      return dma;
7758602Snilay@cs.wisc.edu}
7768602Snilay@cs.wisc.edu''')
7778602Snilay@cs.wisc.edu
7788602Snilay@cs.wisc.edu                code('''
7798602Snilay@cs.wisc.edu
7808602Snilay@cs.wisc.eduMachineID
7818602Snilay@cs.wisc.eduget${{enum.ident}}MachineID(NodeID RubyNode)
7828602Snilay@cs.wisc.edu{
7838602Snilay@cs.wisc.edu      MachineID mach = {MachineType_${{enum.ident}}, RubyNode};
7848602Snilay@cs.wisc.edu      return mach;
7858602Snilay@cs.wisc.edu}
7868602Snilay@cs.wisc.edu''')
7878602Snilay@cs.wisc.edu
7886657Snate@binkert.org        # Write the file
7896657Snate@binkert.org        code.write(path, "%s.cc" % self.c_ident)
7906657Snate@binkert.org
7916657Snate@binkert.org__all__ = [ "Type" ]
792