SimObject.py revision 10023
18840Sandreas.hansson@arm.com# Copyright (c) 2012 ARM Limited 28840Sandreas.hansson@arm.com# All rights reserved. 38840Sandreas.hansson@arm.com# 48840Sandreas.hansson@arm.com# The license below extends only to copyright in the software and shall 58840Sandreas.hansson@arm.com# not be construed as granting a license to any other intellectual 68840Sandreas.hansson@arm.com# property including but not limited to intellectual property relating 78840Sandreas.hansson@arm.com# to a hardware implementation of the functionality of the software 88840Sandreas.hansson@arm.com# licensed hereunder. You may use the software subject to the license 98840Sandreas.hansson@arm.com# terms below provided that you ensure that this notice is replicated 108840Sandreas.hansson@arm.com# unmodified and in its entirety in all distributions of the software, 118840Sandreas.hansson@arm.com# modified or unmodified, in source code or in binary form. 128840Sandreas.hansson@arm.com# 132740SN/A# Copyright (c) 2004-2006 The Regents of The University of Michigan 149983Sstever@gmail.com# Copyright (c) 2010-20013 Advanced Micro Devices, Inc. 159983Sstever@gmail.com# Copyright (c) 2013 Mark D. Hill and David A. Wood 161046SN/A# All rights reserved. 171046SN/A# 181046SN/A# Redistribution and use in source and binary forms, with or without 191046SN/A# modification, are permitted provided that the following conditions are 201046SN/A# met: redistributions of source code must retain the above copyright 211046SN/A# notice, this list of conditions and the following disclaimer; 221046SN/A# redistributions in binary form must reproduce the above copyright 231046SN/A# notice, this list of conditions and the following disclaimer in the 241046SN/A# documentation and/or other materials provided with the distribution; 251046SN/A# neither the name of the copyright holders nor the names of its 261046SN/A# contributors may be used to endorse or promote products derived from 271046SN/A# this software without specific prior written permission. 281046SN/A# 291046SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 301046SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 311046SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 321046SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 331046SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 341046SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 351046SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 361046SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 371046SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 381046SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 391046SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 402665SN/A# 412665SN/A# Authors: Steve Reinhardt 422665SN/A# Nathan Binkert 438840Sandreas.hansson@arm.com# Andreas Hansson 441046SN/A 455766Snate@binkert.orgimport sys 468331Ssteve.reinhardt@amd.comfrom types import FunctionType, MethodType, ModuleType 471438SN/A 484762Snate@binkert.orgimport m5 496654Snate@binkert.orgfrom m5.util import * 503102Sstever@eecs.umich.edu 513102Sstever@eecs.umich.edu# Have to import params up top since Param is referenced on initial 523102Sstever@eecs.umich.edu# load (when SimObject class references Param to create a class 533102Sstever@eecs.umich.edu# variable, the 'name' param)... 546654Snate@binkert.orgfrom m5.params import * 553102Sstever@eecs.umich.edu# There are a few things we need that aren't in params.__all__ since 563102Sstever@eecs.umich.edu# normal users don't need them 577528Ssteve.reinhardt@amd.comfrom m5.params import ParamDesc, VectorParamDesc, \ 588839Sandreas.hansson@arm.com isNullPointer, SimObjectVector, Port 593102Sstever@eecs.umich.edu 606654Snate@binkert.orgfrom m5.proxy import * 616654Snate@binkert.orgfrom m5.proxy import isproxy 62679SN/A 63679SN/A##################################################################### 64679SN/A# 65679SN/A# M5 Python Configuration Utility 66679SN/A# 67679SN/A# The basic idea is to write simple Python programs that build Python 681692SN/A# objects corresponding to M5 SimObjects for the desired simulation 69679SN/A# configuration. For now, the Python emits a .ini file that can be 70679SN/A# parsed by M5. In the future, some tighter integration between M5 71679SN/A# and the Python interpreter may allow bypassing the .ini file. 72679SN/A# 73679SN/A# Each SimObject class in M5 is represented by a Python class with the 74679SN/A# same name. The Python inheritance tree mirrors the M5 C++ tree 75679SN/A# (e.g., SimpleCPU derives from BaseCPU in both cases, and all 76679SN/A# SimObjects inherit from a single SimObject base class). To specify 77679SN/A# an instance of an M5 SimObject in a configuration, the user simply 78679SN/A# instantiates the corresponding Python object. The parameters for 79679SN/A# that SimObject are given by assigning to attributes of the Python 80679SN/A# object, either using keyword assignment in the constructor or in 81679SN/A# separate assignment statements. For example: 82679SN/A# 831692SN/A# cache = BaseCache(size='64KB') 84679SN/A# cache.hit_latency = 3 85679SN/A# cache.assoc = 8 86679SN/A# 87679SN/A# The magic lies in the mapping of the Python attributes for SimObject 88679SN/A# classes to the actual SimObject parameter specifications. This 89679SN/A# allows parameter validity checking in the Python code. Continuing 90679SN/A# the example above, the statements "cache.blurfl=3" or 91679SN/A# "cache.assoc='hello'" would both result in runtime errors in Python, 92679SN/A# since the BaseCache object has no 'blurfl' parameter and the 'assoc' 93679SN/A# parameter requires an integer, respectively. This magic is done 94679SN/A# primarily by overriding the special __setattr__ method that controls 95679SN/A# assignment to object attributes. 96679SN/A# 97679SN/A# Once a set of Python objects have been instantiated in a hierarchy, 98679SN/A# calling 'instantiate(obj)' (where obj is the root of the hierarchy) 992740SN/A# will generate a .ini file. 100679SN/A# 101679SN/A##################################################################### 102679SN/A 1034762Snate@binkert.org# list of all SimObject classes 1044762Snate@binkert.orgallClasses = {} 1054762Snate@binkert.org 1062738SN/A# dict to look up SimObjects based on path 1072738SN/AinstanceDict = {} 1082738SN/A 1099338SAndreas.Sandberg@arm.com# Did any of the SimObjects lack a header file? 1109338SAndreas.Sandberg@arm.comnoCxxHeader = False 1119338SAndreas.Sandberg@arm.com 1127673Snate@binkert.orgdef public_value(key, value): 1137673Snate@binkert.org return key.startswith('_') or \ 1148331Ssteve.reinhardt@amd.com isinstance(value, (FunctionType, MethodType, ModuleType, 1158331Ssteve.reinhardt@amd.com classmethod, type)) 1167673Snate@binkert.org 1172740SN/A# The metaclass for SimObject. This class controls how new classes 1182740SN/A# that derive from SimObject are instantiated, and provides inherited 1192740SN/A# class behavior (just like a class controls how instances of that 1202740SN/A# class are instantiated, and provides inherited instance behavior). 1211692SN/Aclass MetaSimObject(type): 1221427SN/A # Attributes that can be set only at initialization time 1237493Ssteve.reinhardt@amd.com init_keywords = { 'abstract' : bool, 1247493Ssteve.reinhardt@amd.com 'cxx_class' : str, 1257493Ssteve.reinhardt@amd.com 'cxx_type' : str, 1269338SAndreas.Sandberg@arm.com 'cxx_header' : str, 1279342SAndreas.Sandberg@arm.com 'type' : str, 1289342SAndreas.Sandberg@arm.com 'cxx_bases' : list } 1291427SN/A # Attributes that can be set any time 1307493Ssteve.reinhardt@amd.com keywords = { 'check' : FunctionType } 131679SN/A 132679SN/A # __new__ is called before __init__, and is where the statements 133679SN/A # in the body of the class definition get loaded into the class's 1342740SN/A # __dict__. We intercept this to filter out parameter & port assignments 135679SN/A # and only allow "private" attributes to be passed to the base 136679SN/A # __new__ (starting with underscore). 1371310SN/A def __new__(mcls, name, bases, dict): 1386654Snate@binkert.org assert name not in allClasses, "SimObject %s already present" % name 1394762Snate@binkert.org 1402740SN/A # Copy "private" attributes, functions, and classes to the 1412740SN/A # official dict. Everything else goes in _init_dict to be 1422740SN/A # filtered in __init__. 1432740SN/A cls_dict = {} 1442740SN/A value_dict = {} 1452740SN/A for key,val in dict.items(): 1467673Snate@binkert.org if public_value(key, val): 1472740SN/A cls_dict[key] = val 1482740SN/A else: 1492740SN/A # must be a param/port setting 1502740SN/A value_dict[key] = val 1514762Snate@binkert.org if 'abstract' not in value_dict: 1524762Snate@binkert.org value_dict['abstract'] = False 1539342SAndreas.Sandberg@arm.com if 'cxx_bases' not in value_dict: 1549342SAndreas.Sandberg@arm.com value_dict['cxx_bases'] = [] 1552740SN/A cls_dict['_value_dict'] = value_dict 1564762Snate@binkert.org cls = super(MetaSimObject, mcls).__new__(mcls, name, bases, cls_dict) 1574762Snate@binkert.org if 'type' in value_dict: 1584762Snate@binkert.org allClasses[name] = cls 1594762Snate@binkert.org return cls 160679SN/A 1612711SN/A # subclass initialization 162679SN/A def __init__(cls, name, bases, dict): 1632711SN/A # calls type.__init__()... I think that's a no-op, but leave 1642711SN/A # it here just in case it's not. 1651692SN/A super(MetaSimObject, cls).__init__(name, bases, dict) 1661310SN/A 1671427SN/A # initialize required attributes 1682740SN/A 1692740SN/A # class-only attributes 1702740SN/A cls._params = multidict() # param descriptions 1712740SN/A cls._ports = multidict() # port descriptions 1722740SN/A 1732740SN/A # class or instance attributes 1742740SN/A cls._values = multidict() # param values 1757528Ssteve.reinhardt@amd.com cls._children = multidict() # SimObject children 1763105Sstever@eecs.umich.edu cls._port_refs = multidict() # port ref objects 1772740SN/A cls._instantiated = False # really instantiated, cloned, or subclassed 1781310SN/A 1799100SBrad.Beckmann@amd.com # We don't support multiple inheritance of sim objects. If you want 1809100SBrad.Beckmann@amd.com # to, you must fix multidict to deal with it properly. Non sim-objects 1819100SBrad.Beckmann@amd.com # are ok, though 1829100SBrad.Beckmann@amd.com bTotal = 0 1839100SBrad.Beckmann@amd.com for c in bases: 1849100SBrad.Beckmann@amd.com if isinstance(c, MetaSimObject): 1859100SBrad.Beckmann@amd.com bTotal += 1 1869100SBrad.Beckmann@amd.com if bTotal > 1: 1879100SBrad.Beckmann@amd.com raise TypeError, "SimObjects do not support multiple inheritance" 1881692SN/A 1891692SN/A base = bases[0] 1901692SN/A 1912740SN/A # Set up general inheritance via multidicts. A subclass will 1922740SN/A # inherit all its settings from the base class. The only time 1932740SN/A # the following is not true is when we define the SimObject 1942740SN/A # class itself (in which case the multidicts have no parent). 1951692SN/A if isinstance(base, MetaSimObject): 1965610Snate@binkert.org cls._base = base 1971692SN/A cls._params.parent = base._params 1982740SN/A cls._ports.parent = base._ports 1991692SN/A cls._values.parent = base._values 2007528Ssteve.reinhardt@amd.com cls._children.parent = base._children 2013105Sstever@eecs.umich.edu cls._port_refs.parent = base._port_refs 2022740SN/A # mark base as having been subclassed 2032712SN/A base._instantiated = True 2045610Snate@binkert.org else: 2055610Snate@binkert.org cls._base = None 2061692SN/A 2074762Snate@binkert.org # default keyword values 2084762Snate@binkert.org if 'type' in cls._value_dict: 2094762Snate@binkert.org if 'cxx_class' not in cls._value_dict: 2105610Snate@binkert.org cls._value_dict['cxx_class'] = cls._value_dict['type'] 2114762Snate@binkert.org 2125610Snate@binkert.org cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class'] 2134859Snate@binkert.org 2149338SAndreas.Sandberg@arm.com if 'cxx_header' not in cls._value_dict: 2159338SAndreas.Sandberg@arm.com global noCxxHeader 2169338SAndreas.Sandberg@arm.com noCxxHeader = True 2179528Ssascha.bischoff@arm.com warn("No header file specified for SimObject: %s", name) 2189338SAndreas.Sandberg@arm.com 2198597Ssteve.reinhardt@amd.com # Export methods are automatically inherited via C++, so we 2208597Ssteve.reinhardt@amd.com # don't want the method declarations to get inherited on the 2218597Ssteve.reinhardt@amd.com # python side (and thus end up getting repeated in the wrapped 2228597Ssteve.reinhardt@amd.com # versions of derived classes). The code below basicallly 2238597Ssteve.reinhardt@amd.com # suppresses inheritance by substituting in the base (null) 2248597Ssteve.reinhardt@amd.com # versions of these methods unless a different version is 2258597Ssteve.reinhardt@amd.com # explicitly supplied. 2268597Ssteve.reinhardt@amd.com for method_name in ('export_methods', 'export_method_cxx_predecls', 2278597Ssteve.reinhardt@amd.com 'export_method_swig_predecls'): 2288597Ssteve.reinhardt@amd.com if method_name not in cls.__dict__: 2298597Ssteve.reinhardt@amd.com base_method = getattr(MetaSimObject, method_name) 2308597Ssteve.reinhardt@amd.com m = MethodType(base_method, cls, MetaSimObject) 2318597Ssteve.reinhardt@amd.com setattr(cls, method_name, m) 2328597Ssteve.reinhardt@amd.com 2332740SN/A # Now process the _value_dict items. They could be defining 2342740SN/A # new (or overriding existing) parameters or ports, setting 2352740SN/A # class keywords (e.g., 'abstract'), or setting parameter 2362740SN/A # values or port bindings. The first 3 can only be set when 2372740SN/A # the class is defined, so we handle them here. The others 2382740SN/A # can be set later too, so just emulate that by calling 2392740SN/A # setattr(). 2402740SN/A for key,val in cls._value_dict.items(): 2411527SN/A # param descriptions 2422740SN/A if isinstance(val, ParamDesc): 2431585SN/A cls._new_param(key, val) 2441427SN/A 2452738SN/A # port objects 2462738SN/A elif isinstance(val, Port): 2473105Sstever@eecs.umich.edu cls._new_port(key, val) 2482738SN/A 2491427SN/A # init-time-only keywords 2501427SN/A elif cls.init_keywords.has_key(key): 2511427SN/A cls._set_keyword(key, val, cls.init_keywords[key]) 2521427SN/A 2531427SN/A # default: use normal path (ends up in __setattr__) 2541427SN/A else: 2551427SN/A setattr(cls, key, val) 2561427SN/A 2571427SN/A def _set_keyword(cls, keyword, val, kwtype): 2581427SN/A if not isinstance(val, kwtype): 2591427SN/A raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \ 2601427SN/A (keyword, type(val), kwtype) 2617493Ssteve.reinhardt@amd.com if isinstance(val, FunctionType): 2621427SN/A val = classmethod(val) 2631427SN/A type.__setattr__(cls, keyword, val) 2641427SN/A 2653100SN/A def _new_param(cls, name, pdesc): 2663100SN/A # each param desc should be uniquely assigned to one variable 2673100SN/A assert(not hasattr(pdesc, 'name')) 2683100SN/A pdesc.name = name 2693100SN/A cls._params[name] = pdesc 2703100SN/A if hasattr(pdesc, 'default'): 2713105Sstever@eecs.umich.edu cls._set_param(name, pdesc.default, pdesc) 2723105Sstever@eecs.umich.edu 2733105Sstever@eecs.umich.edu def _set_param(cls, name, value, param): 2743105Sstever@eecs.umich.edu assert(param.name == name) 2753105Sstever@eecs.umich.edu try: 2768321Ssteve.reinhardt@amd.com value = param.convert(value) 2773105Sstever@eecs.umich.edu except Exception, e: 2783105Sstever@eecs.umich.edu msg = "%s\nError setting param %s.%s to %s\n" % \ 2793105Sstever@eecs.umich.edu (e, cls.__name__, name, value) 2803105Sstever@eecs.umich.edu e.args = (msg, ) 2813105Sstever@eecs.umich.edu raise 2828321Ssteve.reinhardt@amd.com cls._values[name] = value 2838321Ssteve.reinhardt@amd.com # if param value is a SimObject, make it a child too, so that 2848321Ssteve.reinhardt@amd.com # it gets cloned properly when the class is instantiated 2858321Ssteve.reinhardt@amd.com if isSimObjectOrVector(value) and not value.has_parent(): 2868321Ssteve.reinhardt@amd.com cls._add_cls_child(name, value) 2878321Ssteve.reinhardt@amd.com 2888321Ssteve.reinhardt@amd.com def _add_cls_child(cls, name, child): 2898321Ssteve.reinhardt@amd.com # It's a little funky to have a class as a parent, but these 2908321Ssteve.reinhardt@amd.com # objects should never be instantiated (only cloned, which 2918321Ssteve.reinhardt@amd.com # clears the parent pointer), and this makes it clear that the 2928321Ssteve.reinhardt@amd.com # object is not an orphan and can provide better error 2938321Ssteve.reinhardt@amd.com # messages. 2948321Ssteve.reinhardt@amd.com child.set_parent(cls, name) 2958321Ssteve.reinhardt@amd.com cls._children[name] = child 2963105Sstever@eecs.umich.edu 2973105Sstever@eecs.umich.edu def _new_port(cls, name, port): 2983105Sstever@eecs.umich.edu # each port should be uniquely assigned to one variable 2993105Sstever@eecs.umich.edu assert(not hasattr(port, 'name')) 3003105Sstever@eecs.umich.edu port.name = name 3013105Sstever@eecs.umich.edu cls._ports[name] = port 3023105Sstever@eecs.umich.edu 3033105Sstever@eecs.umich.edu # same as _get_port_ref, effectively, but for classes 3043105Sstever@eecs.umich.edu def _cls_get_port_ref(cls, attr): 3053105Sstever@eecs.umich.edu # Return reference that can be assigned to another port 3063105Sstever@eecs.umich.edu # via __setattr__. There is only ever one reference 3073105Sstever@eecs.umich.edu # object per port, but we create them lazily here. 3083105Sstever@eecs.umich.edu ref = cls._port_refs.get(attr) 3093105Sstever@eecs.umich.edu if not ref: 3103105Sstever@eecs.umich.edu ref = cls._ports[attr].makeRef(cls) 3113105Sstever@eecs.umich.edu cls._port_refs[attr] = ref 3123105Sstever@eecs.umich.edu return ref 3131585SN/A 3141310SN/A # Set attribute (called on foo.attr = value when foo is an 3151310SN/A # instance of class cls). 3161310SN/A def __setattr__(cls, attr, value): 3171310SN/A # normal processing for private attributes 3187673Snate@binkert.org if public_value(attr, value): 3191310SN/A type.__setattr__(cls, attr, value) 3201310SN/A return 3211310SN/A 3221310SN/A if cls.keywords.has_key(attr): 3231427SN/A cls._set_keyword(attr, value, cls.keywords[attr]) 3241310SN/A return 3251310SN/A 3262738SN/A if cls._ports.has_key(attr): 3273105Sstever@eecs.umich.edu cls._cls_get_port_ref(attr).connect(value) 3282738SN/A return 3292738SN/A 3302740SN/A if isSimObjectOrSequence(value) and cls._instantiated: 3312740SN/A raise RuntimeError, \ 3322740SN/A "cannot set SimObject parameter '%s' after\n" \ 3332740SN/A " class %s has been instantiated or subclassed" \ 3342740SN/A % (attr, cls.__name__) 3352740SN/A 3362740SN/A # check for param 3373105Sstever@eecs.umich.edu param = cls._params.get(attr) 3381310SN/A if param: 3393105Sstever@eecs.umich.edu cls._set_param(attr, value, param) 3403105Sstever@eecs.umich.edu return 3413105Sstever@eecs.umich.edu 3423105Sstever@eecs.umich.edu if isSimObjectOrSequence(value): 3433105Sstever@eecs.umich.edu # If RHS is a SimObject, it's an implicit child assignment. 3448321Ssteve.reinhardt@amd.com cls._add_cls_child(attr, coerceSimObjectOrVector(value)) 3453105Sstever@eecs.umich.edu return 3463105Sstever@eecs.umich.edu 3473105Sstever@eecs.umich.edu # no valid assignment... raise exception 3483105Sstever@eecs.umich.edu raise AttributeError, \ 3493105Sstever@eecs.umich.edu "Class %s has no parameter \'%s\'" % (cls.__name__, attr) 3501310SN/A 3511585SN/A def __getattr__(cls, attr): 3527675Snate@binkert.org if attr == 'cxx_class_path': 3537675Snate@binkert.org return cls.cxx_class.split('::') 3547675Snate@binkert.org 3557675Snate@binkert.org if attr == 'cxx_class_name': 3567675Snate@binkert.org return cls.cxx_class_path[-1] 3577675Snate@binkert.org 3587675Snate@binkert.org if attr == 'cxx_namespaces': 3597675Snate@binkert.org return cls.cxx_class_path[:-1] 3607675Snate@binkert.org 3611692SN/A if cls._values.has_key(attr): 3621692SN/A return cls._values[attr] 3631585SN/A 3647528Ssteve.reinhardt@amd.com if cls._children.has_key(attr): 3657528Ssteve.reinhardt@amd.com return cls._children[attr] 3667528Ssteve.reinhardt@amd.com 3671585SN/A raise AttributeError, \ 3681585SN/A "object '%s' has no attribute '%s'" % (cls.__name__, attr) 3691585SN/A 3703100SN/A def __str__(cls): 3713100SN/A return cls.__name__ 3723100SN/A 3738596Ssteve.reinhardt@amd.com # See ParamValue.cxx_predecls for description. 3748596Ssteve.reinhardt@amd.com def cxx_predecls(cls, code): 3758596Ssteve.reinhardt@amd.com code('#include "params/$cls.hh"') 3768596Ssteve.reinhardt@amd.com 3778596Ssteve.reinhardt@amd.com # See ParamValue.swig_predecls for description. 3788596Ssteve.reinhardt@amd.com def swig_predecls(cls, code): 3798596Ssteve.reinhardt@amd.com code('%import "python/m5/internal/param_$cls.i"') 3808596Ssteve.reinhardt@amd.com 3818597Ssteve.reinhardt@amd.com # Hook for exporting additional C++ methods to Python via SWIG. 3828597Ssteve.reinhardt@amd.com # Default is none, override using @classmethod in class definition. 3838597Ssteve.reinhardt@amd.com def export_methods(cls, code): 3848597Ssteve.reinhardt@amd.com pass 3858597Ssteve.reinhardt@amd.com 3868597Ssteve.reinhardt@amd.com # Generate the code needed as a prerequisite for the C++ methods 3878597Ssteve.reinhardt@amd.com # exported via export_methods() to be compiled in the _wrap.cc 3888597Ssteve.reinhardt@amd.com # file. Typically generates one or more #include statements. If 3898597Ssteve.reinhardt@amd.com # any methods are exported, typically at least the C++ header 3908597Ssteve.reinhardt@amd.com # declaring the relevant SimObject class must be included. 3918597Ssteve.reinhardt@amd.com def export_method_cxx_predecls(cls, code): 3928597Ssteve.reinhardt@amd.com pass 3938597Ssteve.reinhardt@amd.com 3948597Ssteve.reinhardt@amd.com # Generate the code needed as a prerequisite for the C++ methods 3958597Ssteve.reinhardt@amd.com # exported via export_methods() to be processed by SWIG. 3968597Ssteve.reinhardt@amd.com # Typically generates one or more %include or %import statements. 3978597Ssteve.reinhardt@amd.com # If any methods are exported, typically at least the C++ header 3988597Ssteve.reinhardt@amd.com # declaring the relevant SimObject class must be included. 3998597Ssteve.reinhardt@amd.com def export_method_swig_predecls(cls, code): 4008597Ssteve.reinhardt@amd.com pass 4018597Ssteve.reinhardt@amd.com 4028596Ssteve.reinhardt@amd.com # Generate the declaration for this object for wrapping with SWIG. 4038596Ssteve.reinhardt@amd.com # Generates code that goes into a SWIG .i file. Called from 4048596Ssteve.reinhardt@amd.com # src/SConscript. 4058596Ssteve.reinhardt@amd.com def swig_decl(cls, code): 4068596Ssteve.reinhardt@amd.com class_path = cls.cxx_class.split('::') 4078596Ssteve.reinhardt@amd.com classname = class_path[-1] 4088596Ssteve.reinhardt@amd.com namespaces = class_path[:-1] 4098596Ssteve.reinhardt@amd.com 4108596Ssteve.reinhardt@amd.com # The 'local' attribute restricts us to the params declared in 4118596Ssteve.reinhardt@amd.com # the object itself, not including inherited params (which 4128596Ssteve.reinhardt@amd.com # will also be inherited from the base class's param struct 4138596Ssteve.reinhardt@amd.com # here). 4148596Ssteve.reinhardt@amd.com params = cls._params.local.values() 4158840Sandreas.hansson@arm.com ports = cls._ports.local 4168596Ssteve.reinhardt@amd.com 4178596Ssteve.reinhardt@amd.com code('%module(package="m5.internal") param_$cls') 4188596Ssteve.reinhardt@amd.com code() 4198596Ssteve.reinhardt@amd.com code('%{') 4209342SAndreas.Sandberg@arm.com code('#include "sim/sim_object.hh"') 4218596Ssteve.reinhardt@amd.com code('#include "params/$cls.hh"') 4228596Ssteve.reinhardt@amd.com for param in params: 4238596Ssteve.reinhardt@amd.com param.cxx_predecls(code) 4249338SAndreas.Sandberg@arm.com code('#include "${{cls.cxx_header}}"') 4258597Ssteve.reinhardt@amd.com cls.export_method_cxx_predecls(code) 4268860Sandreas.hansson@arm.com code('''\ 4278860Sandreas.hansson@arm.com/** 4288860Sandreas.hansson@arm.com * This is a workaround for bug in swig. Prior to gcc 4.6.1 the STL 4298860Sandreas.hansson@arm.com * headers like vector, string, etc. used to automatically pull in 4308860Sandreas.hansson@arm.com * the cstddef header but starting with gcc 4.6.1 they no longer do. 4318860Sandreas.hansson@arm.com * This leads to swig generated a file that does not compile so we 4328860Sandreas.hansson@arm.com * explicitly include cstddef. Additionally, including version 2.0.4, 4338860Sandreas.hansson@arm.com * swig uses ptrdiff_t without the std:: namespace prefix which is 4348860Sandreas.hansson@arm.com * required with gcc 4.6.1. We explicitly provide access to it. 4358860Sandreas.hansson@arm.com */ 4368860Sandreas.hansson@arm.com#include <cstddef> 4378860Sandreas.hansson@arm.comusing std::ptrdiff_t; 4388860Sandreas.hansson@arm.com''') 4398596Ssteve.reinhardt@amd.com code('%}') 4408596Ssteve.reinhardt@amd.com code() 4418596Ssteve.reinhardt@amd.com 4428596Ssteve.reinhardt@amd.com for param in params: 4438596Ssteve.reinhardt@amd.com param.swig_predecls(code) 4448597Ssteve.reinhardt@amd.com cls.export_method_swig_predecls(code) 4458596Ssteve.reinhardt@amd.com 4468596Ssteve.reinhardt@amd.com code() 4478596Ssteve.reinhardt@amd.com if cls._base: 4488596Ssteve.reinhardt@amd.com code('%import "python/m5/internal/param_${{cls._base}}.i"') 4498596Ssteve.reinhardt@amd.com code() 4508596Ssteve.reinhardt@amd.com 4518596Ssteve.reinhardt@amd.com for ns in namespaces: 4528596Ssteve.reinhardt@amd.com code('namespace $ns {') 4538596Ssteve.reinhardt@amd.com 4548596Ssteve.reinhardt@amd.com if namespaces: 4558596Ssteve.reinhardt@amd.com code('// avoid name conflicts') 4568596Ssteve.reinhardt@amd.com sep_string = '_COLONS_' 4578596Ssteve.reinhardt@amd.com flat_name = sep_string.join(class_path) 4588596Ssteve.reinhardt@amd.com code('%rename($flat_name) $classname;') 4598596Ssteve.reinhardt@amd.com 4608597Ssteve.reinhardt@amd.com code() 4618597Ssteve.reinhardt@amd.com code('// stop swig from creating/wrapping default ctor/dtor') 4628597Ssteve.reinhardt@amd.com code('%nodefault $classname;') 4638597Ssteve.reinhardt@amd.com code('class $classname') 4648597Ssteve.reinhardt@amd.com if cls._base: 4659342SAndreas.Sandberg@arm.com bases = [ cls._base.cxx_class ] + cls.cxx_bases 4669342SAndreas.Sandberg@arm.com else: 4679342SAndreas.Sandberg@arm.com bases = cls.cxx_bases 4689342SAndreas.Sandberg@arm.com base_first = True 4699342SAndreas.Sandberg@arm.com for base in bases: 4709342SAndreas.Sandberg@arm.com if base_first: 4719342SAndreas.Sandberg@arm.com code(' : public ${{base}}') 4729342SAndreas.Sandberg@arm.com base_first = False 4739342SAndreas.Sandberg@arm.com else: 4749342SAndreas.Sandberg@arm.com code(' , public ${{base}}') 4759342SAndreas.Sandberg@arm.com 4768597Ssteve.reinhardt@amd.com code('{') 4778597Ssteve.reinhardt@amd.com code(' public:') 4788597Ssteve.reinhardt@amd.com cls.export_methods(code) 4798597Ssteve.reinhardt@amd.com code('};') 4808596Ssteve.reinhardt@amd.com 4818596Ssteve.reinhardt@amd.com for ns in reversed(namespaces): 4828596Ssteve.reinhardt@amd.com code('} // namespace $ns') 4838596Ssteve.reinhardt@amd.com 4848596Ssteve.reinhardt@amd.com code() 4858596Ssteve.reinhardt@amd.com code('%include "params/$cls.hh"') 4868596Ssteve.reinhardt@amd.com 4878596Ssteve.reinhardt@amd.com 4888596Ssteve.reinhardt@amd.com # Generate the C++ declaration (.hh file) for this SimObject's 4898596Ssteve.reinhardt@amd.com # param struct. Called from src/SConscript. 4908596Ssteve.reinhardt@amd.com def cxx_param_decl(cls, code): 4918596Ssteve.reinhardt@amd.com # The 'local' attribute restricts us to the params declared in 4923100SN/A # the object itself, not including inherited params (which 4933100SN/A # will also be inherited from the base class's param struct 4943100SN/A # here). 4954762Snate@binkert.org params = cls._params.local.values() 4968840Sandreas.hansson@arm.com ports = cls._ports.local 4973100SN/A try: 4983100SN/A ptypes = [p.ptype for p in params] 4993100SN/A except: 5003100SN/A print cls, p, p.ptype_str 5013100SN/A print params 5023100SN/A raise 5033100SN/A 5047675Snate@binkert.org class_path = cls._value_dict['cxx_class'].split('::') 5057675Snate@binkert.org 5067675Snate@binkert.org code('''\ 5077675Snate@binkert.org#ifndef __PARAMS__${cls}__ 5087675Snate@binkert.org#define __PARAMS__${cls}__ 5097675Snate@binkert.org 5107675Snate@binkert.org''') 5117675Snate@binkert.org 5127675Snate@binkert.org # A forward class declaration is sufficient since we are just 5137675Snate@binkert.org # declaring a pointer. 5147675Snate@binkert.org for ns in class_path[:-1]: 5157675Snate@binkert.org code('namespace $ns {') 5167675Snate@binkert.org code('class $0;', class_path[-1]) 5177675Snate@binkert.org for ns in reversed(class_path[:-1]): 5187811Ssteve.reinhardt@amd.com code('} // namespace $ns') 5197675Snate@binkert.org code() 5207675Snate@binkert.org 5218597Ssteve.reinhardt@amd.com # The base SimObject has a couple of params that get 5228597Ssteve.reinhardt@amd.com # automatically set from Python without being declared through 5238597Ssteve.reinhardt@amd.com # the normal Param mechanism; we slip them in here (needed 5248597Ssteve.reinhardt@amd.com # predecls now, actual declarations below) 5258597Ssteve.reinhardt@amd.com if cls == SimObject: 5268597Ssteve.reinhardt@amd.com code(''' 5278597Ssteve.reinhardt@amd.com#ifndef PY_VERSION 5288597Ssteve.reinhardt@amd.comstruct PyObject; 5298597Ssteve.reinhardt@amd.com#endif 5308597Ssteve.reinhardt@amd.com 5318597Ssteve.reinhardt@amd.com#include <string> 5328597Ssteve.reinhardt@amd.com''') 5337673Snate@binkert.org for param in params: 5347673Snate@binkert.org param.cxx_predecls(code) 5358840Sandreas.hansson@arm.com for port in ports.itervalues(): 5368840Sandreas.hansson@arm.com port.cxx_predecls(code) 5377673Snate@binkert.org code() 5384762Snate@binkert.org 5395610Snate@binkert.org if cls._base: 5407673Snate@binkert.org code('#include "params/${{cls._base.type}}.hh"') 5417673Snate@binkert.org code() 5424762Snate@binkert.org 5434762Snate@binkert.org for ptype in ptypes: 5444762Snate@binkert.org if issubclass(ptype, Enum): 5457673Snate@binkert.org code('#include "enums/${{ptype.__name__}}.hh"') 5467673Snate@binkert.org code() 5474762Snate@binkert.org 5488596Ssteve.reinhardt@amd.com # now generate the actual param struct 5498597Ssteve.reinhardt@amd.com code("struct ${cls}Params") 5508597Ssteve.reinhardt@amd.com if cls._base: 5518597Ssteve.reinhardt@amd.com code(" : public ${{cls._base.type}}Params") 5528597Ssteve.reinhardt@amd.com code("{") 5538597Ssteve.reinhardt@amd.com if not hasattr(cls, 'abstract') or not cls.abstract: 5548597Ssteve.reinhardt@amd.com if 'type' in cls.__dict__: 5558597Ssteve.reinhardt@amd.com code(" ${{cls.cxx_type}} create();") 5568597Ssteve.reinhardt@amd.com 5578597Ssteve.reinhardt@amd.com code.indent() 5588596Ssteve.reinhardt@amd.com if cls == SimObject: 5598597Ssteve.reinhardt@amd.com code(''' 5609983Sstever@gmail.com SimObjectParams() {} 5618597Ssteve.reinhardt@amd.com virtual ~SimObjectParams() {} 5628596Ssteve.reinhardt@amd.com 5638597Ssteve.reinhardt@amd.com std::string name; 5648597Ssteve.reinhardt@amd.com PyObject *pyobj; 5658597Ssteve.reinhardt@amd.com ''') 5668597Ssteve.reinhardt@amd.com for param in params: 5678597Ssteve.reinhardt@amd.com param.cxx_decl(code) 5688840Sandreas.hansson@arm.com for port in ports.itervalues(): 5698840Sandreas.hansson@arm.com port.cxx_decl(code) 5708840Sandreas.hansson@arm.com 5718597Ssteve.reinhardt@amd.com code.dedent() 5728597Ssteve.reinhardt@amd.com code('};') 5735488Snate@binkert.org 5747673Snate@binkert.org code() 5757673Snate@binkert.org code('#endif // __PARAMS__${cls}__') 5765488Snate@binkert.org return code 5775488Snate@binkert.org 5785488Snate@binkert.org 5799983Sstever@gmail.com# This *temporary* definition is required to support calls from the 5809983Sstever@gmail.com# SimObject class definition to the MetaSimObject methods (in 5819983Sstever@gmail.com# particular _set_param, which gets called for parameters with default 5829983Sstever@gmail.com# values defined on the SimObject class itself). It will get 5839983Sstever@gmail.com# overridden by the permanent definition (which requires that 5849983Sstever@gmail.com# SimObject be defined) lower in this file. 5859983Sstever@gmail.comdef isSimObjectOrVector(value): 5869983Sstever@gmail.com return False 5873100SN/A 5882740SN/A# The SimObject class is the root of the special hierarchy. Most of 589679SN/A# the code in this class deals with the configuration hierarchy itself 590679SN/A# (parent/child node relationships). 5911692SN/Aclass SimObject(object): 5921692SN/A # Specify metaclass. Any class inheriting from SimObject will 593679SN/A # get this metaclass. 5941692SN/A __metaclass__ = MetaSimObject 5953100SN/A type = 'SimObject' 5964762Snate@binkert.org abstract = True 5979983Sstever@gmail.com 5989338SAndreas.Sandberg@arm.com cxx_header = "sim/sim_object.hh" 5999345SAndreas.Sandberg@ARM.com cxx_bases = [ "Drainable", "Serializable" ] 6009983Sstever@gmail.com eventq_index = Param.UInt32(Parent.eventq_index, "Event Queue Index") 6019342SAndreas.Sandberg@arm.com 6028597Ssteve.reinhardt@amd.com @classmethod 6038597Ssteve.reinhardt@amd.com def export_method_swig_predecls(cls, code): 6048597Ssteve.reinhardt@amd.com code(''' 6058597Ssteve.reinhardt@amd.com%include <std_string.i> 6069342SAndreas.Sandberg@arm.com 6079342SAndreas.Sandberg@arm.com%import "python/swig/drain.i" 6089345SAndreas.Sandberg@ARM.com%import "python/swig/serialize.i" 6098597Ssteve.reinhardt@amd.com''') 6108597Ssteve.reinhardt@amd.com 6118597Ssteve.reinhardt@amd.com @classmethod 6128597Ssteve.reinhardt@amd.com def export_methods(cls, code): 6138597Ssteve.reinhardt@amd.com code(''' 6148597Ssteve.reinhardt@amd.com void init(); 6158597Ssteve.reinhardt@amd.com void loadState(Checkpoint *cp); 6168597Ssteve.reinhardt@amd.com void initState(); 6178597Ssteve.reinhardt@amd.com void regStats(); 6188597Ssteve.reinhardt@amd.com void resetStats(); 61910023Smatt.horsnell@ARM.com void regProbePoints(); 62010023Smatt.horsnell@ARM.com void regProbeListeners(); 6218597Ssteve.reinhardt@amd.com void startup(); 6228597Ssteve.reinhardt@amd.com''') 6238597Ssteve.reinhardt@amd.com 6242740SN/A # Initialize new instance. For objects with SimObject-valued 6252740SN/A # children, we need to recursively clone the classes represented 6262740SN/A # by those param values as well in a consistent "deep copy"-style 6272740SN/A # fashion. That is, we want to make sure that each instance is 6282740SN/A # cloned only once, and that if there are multiple references to 6292740SN/A # the same original object, we end up with the corresponding 6302740SN/A # cloned references all pointing to the same cloned instance. 6312740SN/A def __init__(self, **kwargs): 6322740SN/A ancestor = kwargs.get('_ancestor') 6332740SN/A memo_dict = kwargs.get('_memo') 6342740SN/A if memo_dict is None: 6352740SN/A # prepare to memoize any recursively instantiated objects 6362740SN/A memo_dict = {} 6372740SN/A elif ancestor: 6382740SN/A # memoize me now to avoid problems with recursive calls 6392740SN/A memo_dict[ancestor] = self 6402711SN/A 6412740SN/A if not ancestor: 6422740SN/A ancestor = self.__class__ 6432740SN/A ancestor._instantiated = True 6442711SN/A 6452740SN/A # initialize required attributes 6462740SN/A self._parent = None 6477528Ssteve.reinhardt@amd.com self._name = None 6482740SN/A self._ccObject = None # pointer to C++ object 6494762Snate@binkert.org self._ccParams = None 6502740SN/A self._instantiated = False # really "cloned" 6512712SN/A 6528321Ssteve.reinhardt@amd.com # Clone children specified at class level. No need for a 6538321Ssteve.reinhardt@amd.com # multidict here since we will be cloning everything. 6548321Ssteve.reinhardt@amd.com # Do children before parameter values so that children that 6558321Ssteve.reinhardt@amd.com # are also param values get cloned properly. 6568321Ssteve.reinhardt@amd.com self._children = {} 6578321Ssteve.reinhardt@amd.com for key,val in ancestor._children.iteritems(): 6588321Ssteve.reinhardt@amd.com self.add_child(key, val(_memo=memo_dict)) 6598321Ssteve.reinhardt@amd.com 6602711SN/A # Inherit parameter values from class using multidict so 6617528Ssteve.reinhardt@amd.com # individual value settings can be overridden but we still 6627528Ssteve.reinhardt@amd.com # inherit late changes to non-overridden class values. 6632740SN/A self._values = multidict(ancestor._values) 6642740SN/A # clone SimObject-valued parameters 6652740SN/A for key,val in ancestor._values.iteritems(): 6667528Ssteve.reinhardt@amd.com val = tryAsSimObjectOrVector(val) 6677528Ssteve.reinhardt@amd.com if val is not None: 6687528Ssteve.reinhardt@amd.com self._values[key] = val(_memo=memo_dict) 6697528Ssteve.reinhardt@amd.com 6702740SN/A # clone port references. no need to use a multidict here 6712740SN/A # since we will be creating new references for all ports. 6723105Sstever@eecs.umich.edu self._port_refs = {} 6733105Sstever@eecs.umich.edu for key,val in ancestor._port_refs.iteritems(): 6743105Sstever@eecs.umich.edu self._port_refs[key] = val.clone(self, memo_dict) 6751692SN/A # apply attribute assignments from keyword args, if any 6761692SN/A for key,val in kwargs.iteritems(): 6771692SN/A setattr(self, key, val) 678679SN/A 6792740SN/A # "Clone" the current instance by creating another instance of 6802740SN/A # this instance's class, but that inherits its parameter values 6812740SN/A # and port mappings from the current instance. If we're in a 6822740SN/A # "deep copy" recursive clone, check the _memo dict to see if 6832740SN/A # we've already cloned this instance. 6841692SN/A def __call__(self, **kwargs): 6852740SN/A memo_dict = kwargs.get('_memo') 6862740SN/A if memo_dict is None: 6872740SN/A # no memo_dict: must be top-level clone operation. 6882740SN/A # this is only allowed at the root of a hierarchy 6892740SN/A if self._parent: 6902740SN/A raise RuntimeError, "attempt to clone object %s " \ 6912740SN/A "not at the root of a tree (parent = %s)" \ 6922740SN/A % (self, self._parent) 6932740SN/A # create a new dict and use that. 6942740SN/A memo_dict = {} 6952740SN/A kwargs['_memo'] = memo_dict 6962740SN/A elif memo_dict.has_key(self): 6972740SN/A # clone already done & memoized 6982740SN/A return memo_dict[self] 6992740SN/A return self.__class__(_ancestor = self, **kwargs) 7001343SN/A 7013105Sstever@eecs.umich.edu def _get_port_ref(self, attr): 7023105Sstever@eecs.umich.edu # Return reference that can be assigned to another port 7033105Sstever@eecs.umich.edu # via __setattr__. There is only ever one reference 7043105Sstever@eecs.umich.edu # object per port, but we create them lazily here. 7053105Sstever@eecs.umich.edu ref = self._port_refs.get(attr) 7069940SGeoffrey.Blake@arm.com if ref == None: 7073105Sstever@eecs.umich.edu ref = self._ports[attr].makeRef(self) 7083105Sstever@eecs.umich.edu self._port_refs[attr] = ref 7093105Sstever@eecs.umich.edu return ref 7103105Sstever@eecs.umich.edu 7111692SN/A def __getattr__(self, attr): 7122738SN/A if self._ports.has_key(attr): 7133105Sstever@eecs.umich.edu return self._get_port_ref(attr) 7142738SN/A 7151692SN/A if self._values.has_key(attr): 7161692SN/A return self._values[attr] 7171427SN/A 7187528Ssteve.reinhardt@amd.com if self._children.has_key(attr): 7197528Ssteve.reinhardt@amd.com return self._children[attr] 7207528Ssteve.reinhardt@amd.com 7217500Ssteve.reinhardt@amd.com # If the attribute exists on the C++ object, transparently 7227500Ssteve.reinhardt@amd.com # forward the reference there. This is typically used for 7237500Ssteve.reinhardt@amd.com # SWIG-wrapped methods such as init(), regStats(), 7249195SAndreas.Sandberg@arm.com # resetStats(), startup(), drain(), and 7257527Ssteve.reinhardt@amd.com # resume(). 7267500Ssteve.reinhardt@amd.com if self._ccObject and hasattr(self._ccObject, attr): 7277500Ssteve.reinhardt@amd.com return getattr(self._ccObject, attr) 7287500Ssteve.reinhardt@amd.com 72910002Ssteve.reinhardt@amd.com err_string = "object '%s' has no attribute '%s'" \ 7301692SN/A % (self.__class__.__name__, attr) 7311427SN/A 73210002Ssteve.reinhardt@amd.com if not self._ccObject: 73310002Ssteve.reinhardt@amd.com err_string += "\n (C++ object is not yet constructed," \ 73410002Ssteve.reinhardt@amd.com " so wrapped C++ methods are unavailable.)" 73510002Ssteve.reinhardt@amd.com 73610002Ssteve.reinhardt@amd.com raise AttributeError, err_string 73710002Ssteve.reinhardt@amd.com 7381692SN/A # Set attribute (called on foo.attr = value when foo is an 7391692SN/A # instance of class cls). 7401692SN/A def __setattr__(self, attr, value): 7411692SN/A # normal processing for private attributes 7421692SN/A if attr.startswith('_'): 7431692SN/A object.__setattr__(self, attr, value) 7441692SN/A return 7451427SN/A 7462738SN/A if self._ports.has_key(attr): 7472738SN/A # set up port connection 7483105Sstever@eecs.umich.edu self._get_port_ref(attr).connect(value) 7492738SN/A return 7502738SN/A 7513105Sstever@eecs.umich.edu param = self._params.get(attr) 7521692SN/A if param: 7531310SN/A try: 7541692SN/A value = param.convert(value) 7551587SN/A except Exception, e: 7561692SN/A msg = "%s\nError setting param %s.%s to %s\n" % \ 7571692SN/A (e, self.__class__.__name__, attr, value) 7581605SN/A e.args = (msg, ) 7591605SN/A raise 7607528Ssteve.reinhardt@amd.com self._values[attr] = value 7618321Ssteve.reinhardt@amd.com # implicitly parent unparented objects assigned as params 7628321Ssteve.reinhardt@amd.com if isSimObjectOrVector(value) and not value.has_parent(): 7638321Ssteve.reinhardt@amd.com self.add_child(attr, value) 7643105Sstever@eecs.umich.edu return 7651310SN/A 7667528Ssteve.reinhardt@amd.com # if RHS is a SimObject, it's an implicit child assignment 7673105Sstever@eecs.umich.edu if isSimObjectOrSequence(value): 7687528Ssteve.reinhardt@amd.com self.add_child(attr, value) 7693105Sstever@eecs.umich.edu return 7701693SN/A 7713105Sstever@eecs.umich.edu # no valid assignment... raise exception 7723105Sstever@eecs.umich.edu raise AttributeError, "Class %s has no parameter %s" \ 7733105Sstever@eecs.umich.edu % (self.__class__.__name__, attr) 7741310SN/A 7751310SN/A 7761692SN/A # this hack allows tacking a '[0]' onto parameters that may or may 7771692SN/A # not be vectors, and always getting the first element (e.g. cpus) 7781692SN/A def __getitem__(self, key): 7791692SN/A if key == 0: 7801692SN/A return self 7811692SN/A raise TypeError, "Non-zero index '%s' to SimObject" % key 7821310SN/A 7837528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 7847528Ssteve.reinhardt@amd.com def clear_parent(self, old_parent): 7857528Ssteve.reinhardt@amd.com assert self._parent is old_parent 7867528Ssteve.reinhardt@amd.com self._parent = None 7877528Ssteve.reinhardt@amd.com 7887528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 7897528Ssteve.reinhardt@amd.com def set_parent(self, parent, name): 7907528Ssteve.reinhardt@amd.com self._parent = parent 7917528Ssteve.reinhardt@amd.com self._name = name 7927528Ssteve.reinhardt@amd.com 7939953Sgeoffrey.blake@arm.com # Return parent object of this SimObject, not implemented by SimObjectVector 7949953Sgeoffrey.blake@arm.com # because the elements in a SimObjectVector may not share the same parent 7959953Sgeoffrey.blake@arm.com def get_parent(self): 7969953Sgeoffrey.blake@arm.com return self._parent 7979953Sgeoffrey.blake@arm.com 7987528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 7997528Ssteve.reinhardt@amd.com def get_name(self): 8007528Ssteve.reinhardt@amd.com return self._name 8017528Ssteve.reinhardt@amd.com 8028321Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 8038321Ssteve.reinhardt@amd.com def has_parent(self): 8048321Ssteve.reinhardt@amd.com return self._parent is not None 8057528Ssteve.reinhardt@amd.com 8067742Sgblack@eecs.umich.edu # clear out child with given name. This code is not likely to be exercised. 8077742Sgblack@eecs.umich.edu # See comment in add_child. 8081693SN/A def clear_child(self, name): 8091693SN/A child = self._children[name] 8107528Ssteve.reinhardt@amd.com child.clear_parent(self) 8111693SN/A del self._children[name] 8121693SN/A 8137528Ssteve.reinhardt@amd.com # Add a new child to this object. 8147528Ssteve.reinhardt@amd.com def add_child(self, name, child): 8157528Ssteve.reinhardt@amd.com child = coerceSimObjectOrVector(child) 8168321Ssteve.reinhardt@amd.com if child.has_parent(): 8179528Ssascha.bischoff@arm.com warn("add_child('%s'): child '%s' already has parent", name, 8189528Ssascha.bischoff@arm.com child.get_name()) 8197528Ssteve.reinhardt@amd.com if self._children.has_key(name): 8207742Sgblack@eecs.umich.edu # This code path had an undiscovered bug that would make it fail 8217742Sgblack@eecs.umich.edu # at runtime. It had been here for a long time and was only 8227742Sgblack@eecs.umich.edu # exposed by a buggy script. Changes here will probably not be 8237742Sgblack@eecs.umich.edu # exercised without specialized testing. 8247738Sgblack@eecs.umich.edu self.clear_child(name) 8257528Ssteve.reinhardt@amd.com child.set_parent(self, name) 8267528Ssteve.reinhardt@amd.com self._children[name] = child 8271310SN/A 8287528Ssteve.reinhardt@amd.com # Take SimObject-valued parameters that haven't been explicitly 8297528Ssteve.reinhardt@amd.com # assigned as children and make them children of the object that 8307528Ssteve.reinhardt@amd.com # they were assigned to as a parameter value. This guarantees 8317528Ssteve.reinhardt@amd.com # that when we instantiate all the parameter objects we're still 8327528Ssteve.reinhardt@amd.com # inside the configuration hierarchy. 8337528Ssteve.reinhardt@amd.com def adoptOrphanParams(self): 8347528Ssteve.reinhardt@amd.com for key,val in self._values.iteritems(): 8357528Ssteve.reinhardt@amd.com if not isSimObjectVector(val) and isSimObjectSequence(val): 8367528Ssteve.reinhardt@amd.com # need to convert raw SimObject sequences to 8378321Ssteve.reinhardt@amd.com # SimObjectVector class so we can call has_parent() 8387528Ssteve.reinhardt@amd.com val = SimObjectVector(val) 8397528Ssteve.reinhardt@amd.com self._values[key] = val 8408321Ssteve.reinhardt@amd.com if isSimObjectOrVector(val) and not val.has_parent(): 8419528Ssascha.bischoff@arm.com warn("%s adopting orphan SimObject param '%s'", self, key) 8427528Ssteve.reinhardt@amd.com self.add_child(key, val) 8433105Sstever@eecs.umich.edu 8441692SN/A def path(self): 8452740SN/A if not self._parent: 8468321Ssteve.reinhardt@amd.com return '<orphan %s>' % self.__class__ 8471692SN/A ppath = self._parent.path() 8481692SN/A if ppath == 'root': 8491692SN/A return self._name 8501692SN/A return ppath + "." + self._name 8511310SN/A 8521692SN/A def __str__(self): 8531692SN/A return self.path() 8541310SN/A 8551692SN/A def ini_str(self): 8561692SN/A return self.path() 8571310SN/A 8581692SN/A def find_any(self, ptype): 8591692SN/A if isinstance(self, ptype): 8601692SN/A return self, True 8611310SN/A 8621692SN/A found_obj = None 8631692SN/A for child in self._children.itervalues(): 8641692SN/A if isinstance(child, ptype): 8651692SN/A if found_obj != None and child != found_obj: 8661692SN/A raise AttributeError, \ 8671692SN/A 'parent.any matched more than one: %s %s' % \ 8681814SN/A (found_obj.path, child.path) 8691692SN/A found_obj = child 8701692SN/A # search param space 8711692SN/A for pname,pdesc in self._params.iteritems(): 8721692SN/A if issubclass(pdesc.ptype, ptype): 8731692SN/A match_obj = self._values[pname] 8741692SN/A if found_obj != None and found_obj != match_obj: 8751692SN/A raise AttributeError, \ 8765952Ssaidi@eecs.umich.edu 'parent.any matched more than one: %s and %s' % (found_obj.path, match_obj.path) 8771692SN/A found_obj = match_obj 8781692SN/A return found_obj, found_obj != None 8791692SN/A 8808459SAli.Saidi@ARM.com def find_all(self, ptype): 8818459SAli.Saidi@ARM.com all = {} 8828459SAli.Saidi@ARM.com # search children 8838459SAli.Saidi@ARM.com for child in self._children.itervalues(): 8849410Sandreas.hansson@arm.com # a child could be a list, so ensure we visit each item 8859410Sandreas.hansson@arm.com if isinstance(child, list): 8869410Sandreas.hansson@arm.com children = child 8879410Sandreas.hansson@arm.com else: 8889410Sandreas.hansson@arm.com children = [child] 8899410Sandreas.hansson@arm.com 8909410Sandreas.hansson@arm.com for child in children: 8919410Sandreas.hansson@arm.com if isinstance(child, ptype) and not isproxy(child) and \ 8929410Sandreas.hansson@arm.com not isNullPointer(child): 8939410Sandreas.hansson@arm.com all[child] = True 8949410Sandreas.hansson@arm.com if isSimObject(child): 8959410Sandreas.hansson@arm.com # also add results from the child itself 8969410Sandreas.hansson@arm.com child_all, done = child.find_all(ptype) 8979410Sandreas.hansson@arm.com all.update(dict(zip(child_all, [done] * len(child_all)))) 8988459SAli.Saidi@ARM.com # search param space 8998459SAli.Saidi@ARM.com for pname,pdesc in self._params.iteritems(): 9008459SAli.Saidi@ARM.com if issubclass(pdesc.ptype, ptype): 9018459SAli.Saidi@ARM.com match_obj = self._values[pname] 9028459SAli.Saidi@ARM.com if not isproxy(match_obj) and not isNullPointer(match_obj): 9038459SAli.Saidi@ARM.com all[match_obj] = True 9048459SAli.Saidi@ARM.com return all.keys(), True 9058459SAli.Saidi@ARM.com 9061815SN/A def unproxy(self, base): 9071815SN/A return self 9081815SN/A 9097527Ssteve.reinhardt@amd.com def unproxyParams(self): 9103105Sstever@eecs.umich.edu for param in self._params.iterkeys(): 9113105Sstever@eecs.umich.edu value = self._values.get(param) 9126654Snate@binkert.org if value != None and isproxy(value): 9133105Sstever@eecs.umich.edu try: 9143105Sstever@eecs.umich.edu value = value.unproxy(self) 9153105Sstever@eecs.umich.edu except: 9163105Sstever@eecs.umich.edu print "Error in unproxying param '%s' of %s" % \ 9173105Sstever@eecs.umich.edu (param, self.path()) 9183105Sstever@eecs.umich.edu raise 9193105Sstever@eecs.umich.edu setattr(self, param, value) 9203105Sstever@eecs.umich.edu 9213107Sstever@eecs.umich.edu # Unproxy ports in sorted order so that 'append' operations on 9223107Sstever@eecs.umich.edu # vector ports are done in a deterministic fashion. 9233107Sstever@eecs.umich.edu port_names = self._ports.keys() 9243107Sstever@eecs.umich.edu port_names.sort() 9253107Sstever@eecs.umich.edu for port_name in port_names: 9263105Sstever@eecs.umich.edu port = self._port_refs.get(port_name) 9273105Sstever@eecs.umich.edu if port != None: 9283105Sstever@eecs.umich.edu port.unproxy(self) 9293105Sstever@eecs.umich.edu 9305037Smilesck@eecs.umich.edu def print_ini(self, ini_file): 9315543Ssaidi@eecs.umich.edu print >>ini_file, '[' + self.path() + ']' # .ini section header 9321692SN/A 9332738SN/A instanceDict[self.path()] = self 9342738SN/A 9354081Sbinkertn@umich.edu if hasattr(self, 'type'): 9365037Smilesck@eecs.umich.edu print >>ini_file, 'type=%s' % self.type 9371692SN/A 9388664SAli.Saidi@ARM.com if len(self._children.keys()): 9397528Ssteve.reinhardt@amd.com print >>ini_file, 'children=%s' % \ 9408664SAli.Saidi@ARM.com ' '.join(self._children[n].get_name() \ 9418664SAli.Saidi@ARM.com for n in sorted(self._children.keys())) 9421692SN/A 9438664SAli.Saidi@ARM.com for param in sorted(self._params.keys()): 9443105Sstever@eecs.umich.edu value = self._values.get(param) 9451692SN/A if value != None: 9465037Smilesck@eecs.umich.edu print >>ini_file, '%s=%s' % (param, 9475037Smilesck@eecs.umich.edu self._values[param].ini_str()) 9481692SN/A 9498664SAli.Saidi@ARM.com for port_name in sorted(self._ports.keys()): 9503105Sstever@eecs.umich.edu port = self._port_refs.get(port_name, None) 9513105Sstever@eecs.umich.edu if port != None: 9525037Smilesck@eecs.umich.edu print >>ini_file, '%s=%s' % (port_name, port.ini_str()) 9533103Sstever@eecs.umich.edu 9545543Ssaidi@eecs.umich.edu print >>ini_file # blank line between objects 9551692SN/A 9568664SAli.Saidi@ARM.com # generate a tree of dictionaries expressing all the parameters in the 9578664SAli.Saidi@ARM.com # instantiated system for use by scripts that want to do power, thermal 9588664SAli.Saidi@ARM.com # visualization, and other similar tasks 9598664SAli.Saidi@ARM.com def get_config_as_dict(self): 9608664SAli.Saidi@ARM.com d = attrdict() 9618664SAli.Saidi@ARM.com if hasattr(self, 'type'): 9628664SAli.Saidi@ARM.com d.type = self.type 9638664SAli.Saidi@ARM.com if hasattr(self, 'cxx_class'): 9648664SAli.Saidi@ARM.com d.cxx_class = self.cxx_class 9659017Sandreas.hansson@arm.com # Add the name and path of this object to be able to link to 9669017Sandreas.hansson@arm.com # the stats 9679017Sandreas.hansson@arm.com d.name = self.get_name() 9689017Sandreas.hansson@arm.com d.path = self.path() 9698664SAli.Saidi@ARM.com 9708664SAli.Saidi@ARM.com for param in sorted(self._params.keys()): 9718664SAli.Saidi@ARM.com value = self._values.get(param) 9728848Ssteve.reinhardt@amd.com if value != None: 9738848Ssteve.reinhardt@amd.com try: 9748848Ssteve.reinhardt@amd.com # Use native type for those supported by JSON and 9758848Ssteve.reinhardt@amd.com # strings for everything else. skipkeys=True seems 9768848Ssteve.reinhardt@amd.com # to not work as well as one would hope 9778848Ssteve.reinhardt@amd.com if type(self._values[param].value) in \ 9788848Ssteve.reinhardt@amd.com [str, unicode, int, long, float, bool, None]: 9798848Ssteve.reinhardt@amd.com d[param] = self._values[param].value 9808848Ssteve.reinhardt@amd.com else: 9818848Ssteve.reinhardt@amd.com d[param] = str(self._values[param]) 9828669Ssaidi@eecs.umich.edu 9838848Ssteve.reinhardt@amd.com except AttributeError: 9848848Ssteve.reinhardt@amd.com pass 9858664SAli.Saidi@ARM.com 9868664SAli.Saidi@ARM.com for n in sorted(self._children.keys()): 9879017Sandreas.hansson@arm.com child = self._children[n] 9889017Sandreas.hansson@arm.com # Use the name of the attribute (and not get_name()) as 9899017Sandreas.hansson@arm.com # the key in the JSON dictionary to capture the hierarchy 9909017Sandreas.hansson@arm.com # in the Python code that assembled this system 9919017Sandreas.hansson@arm.com d[n] = child.get_config_as_dict() 9928664SAli.Saidi@ARM.com 9938664SAli.Saidi@ARM.com for port_name in sorted(self._ports.keys()): 9948664SAli.Saidi@ARM.com port = self._port_refs.get(port_name, None) 9958664SAli.Saidi@ARM.com if port != None: 9969017Sandreas.hansson@arm.com # Represent each port with a dictionary containing the 9979017Sandreas.hansson@arm.com # prominent attributes 9989017Sandreas.hansson@arm.com d[port_name] = port.get_config_as_dict() 9998664SAli.Saidi@ARM.com 10008664SAli.Saidi@ARM.com return d 10018664SAli.Saidi@ARM.com 10024762Snate@binkert.org def getCCParams(self): 10034762Snate@binkert.org if self._ccParams: 10044762Snate@binkert.org return self._ccParams 10054762Snate@binkert.org 10067677Snate@binkert.org cc_params_struct = getattr(m5.internal.params, '%sParams' % self.type) 10074762Snate@binkert.org cc_params = cc_params_struct() 10085488Snate@binkert.org cc_params.pyobj = self 10094762Snate@binkert.org cc_params.name = str(self) 10104762Snate@binkert.org 10114762Snate@binkert.org param_names = self._params.keys() 10124762Snate@binkert.org param_names.sort() 10134762Snate@binkert.org for param in param_names: 10144762Snate@binkert.org value = self._values.get(param) 10154762Snate@binkert.org if value is None: 10166654Snate@binkert.org fatal("%s.%s without default or user set value", 10176654Snate@binkert.org self.path(), param) 10184762Snate@binkert.org 10194762Snate@binkert.org value = value.getValue() 10204762Snate@binkert.org if isinstance(self._params[param], VectorParamDesc): 10214762Snate@binkert.org assert isinstance(value, list) 10224762Snate@binkert.org vec = getattr(cc_params, param) 10234762Snate@binkert.org assert not len(vec) 10244762Snate@binkert.org for v in value: 10254762Snate@binkert.org vec.append(v) 10264762Snate@binkert.org else: 10274762Snate@binkert.org setattr(cc_params, param, value) 10284762Snate@binkert.org 10294762Snate@binkert.org port_names = self._ports.keys() 10304762Snate@binkert.org port_names.sort() 10314762Snate@binkert.org for port_name in port_names: 10324762Snate@binkert.org port = self._port_refs.get(port_name, None) 10338912Sandreas.hansson@arm.com if port != None: 10348912Sandreas.hansson@arm.com port_count = len(port) 10358912Sandreas.hansson@arm.com else: 10368912Sandreas.hansson@arm.com port_count = 0 10378900Sandreas.hansson@arm.com setattr(cc_params, 'port_' + port_name + '_connection_count', 10388912Sandreas.hansson@arm.com port_count) 10394762Snate@binkert.org self._ccParams = cc_params 10404762Snate@binkert.org return self._ccParams 10412738SN/A 10422740SN/A # Get C++ object corresponding to this object, calling C++ if 10432740SN/A # necessary to construct it. Does *not* recursively create 10442740SN/A # children. 10452740SN/A def getCCObject(self): 10462740SN/A if not self._ccObject: 10477526Ssteve.reinhardt@amd.com # Make sure this object is in the configuration hierarchy 10487526Ssteve.reinhardt@amd.com if not self._parent and not isRoot(self): 10497526Ssteve.reinhardt@amd.com raise RuntimeError, "Attempt to instantiate orphan node" 10507526Ssteve.reinhardt@amd.com # Cycles in the configuration hierarchy are not supported. This 10515244Sgblack@eecs.umich.edu # will catch the resulting recursion and stop. 10525244Sgblack@eecs.umich.edu self._ccObject = -1 10535244Sgblack@eecs.umich.edu params = self.getCCParams() 10544762Snate@binkert.org self._ccObject = params.create() 10552740SN/A elif self._ccObject == -1: 10567526Ssteve.reinhardt@amd.com raise RuntimeError, "%s: Cycle found in configuration hierarchy." \ 10572740SN/A % self.path() 10582740SN/A return self._ccObject 10592740SN/A 10607527Ssteve.reinhardt@amd.com def descendants(self): 10617527Ssteve.reinhardt@amd.com yield self 10627527Ssteve.reinhardt@amd.com for child in self._children.itervalues(): 10637527Ssteve.reinhardt@amd.com for obj in child.descendants(): 10647527Ssteve.reinhardt@amd.com yield obj 10657527Ssteve.reinhardt@amd.com 10667527Ssteve.reinhardt@amd.com # Call C++ to create C++ object corresponding to this object 10674762Snate@binkert.org def createCCObject(self): 10684762Snate@binkert.org self.getCCParams() 10694762Snate@binkert.org self.getCCObject() # force creation 10704762Snate@binkert.org 10714762Snate@binkert.org def getValue(self): 10724762Snate@binkert.org return self.getCCObject() 10734762Snate@binkert.org 10742738SN/A # Create C++ port connections corresponding to the connections in 10757527Ssteve.reinhardt@amd.com # _port_refs 10762738SN/A def connectPorts(self): 10773105Sstever@eecs.umich.edu for portRef in self._port_refs.itervalues(): 10783105Sstever@eecs.umich.edu portRef.ccConnect() 10792797SN/A 10803101Sstever@eecs.umich.edu# Function to provide to C++ so it can look up instances based on paths 10813101Sstever@eecs.umich.edudef resolveSimObject(name): 10823101Sstever@eecs.umich.edu obj = instanceDict[name] 10833101Sstever@eecs.umich.edu return obj.getCCObject() 1084679SN/A 10856654Snate@binkert.orgdef isSimObject(value): 10866654Snate@binkert.org return isinstance(value, SimObject) 10876654Snate@binkert.org 10886654Snate@binkert.orgdef isSimObjectClass(value): 10896654Snate@binkert.org return issubclass(value, SimObject) 10906654Snate@binkert.org 10917528Ssteve.reinhardt@amd.comdef isSimObjectVector(value): 10927528Ssteve.reinhardt@amd.com return isinstance(value, SimObjectVector) 10937528Ssteve.reinhardt@amd.com 10946654Snate@binkert.orgdef isSimObjectSequence(value): 10956654Snate@binkert.org if not isinstance(value, (list, tuple)) or len(value) == 0: 10966654Snate@binkert.org return False 10976654Snate@binkert.org 10986654Snate@binkert.org for val in value: 10996654Snate@binkert.org if not isNullPointer(val) and not isSimObject(val): 11006654Snate@binkert.org return False 11016654Snate@binkert.org 11026654Snate@binkert.org return True 11036654Snate@binkert.org 11046654Snate@binkert.orgdef isSimObjectOrSequence(value): 11056654Snate@binkert.org return isSimObject(value) or isSimObjectSequence(value) 11066654Snate@binkert.org 11077526Ssteve.reinhardt@amd.comdef isRoot(obj): 11087526Ssteve.reinhardt@amd.com from m5.objects import Root 11097526Ssteve.reinhardt@amd.com return obj and obj is Root.getInstance() 11107526Ssteve.reinhardt@amd.com 11117528Ssteve.reinhardt@amd.comdef isSimObjectOrVector(value): 11127528Ssteve.reinhardt@amd.com return isSimObject(value) or isSimObjectVector(value) 11137528Ssteve.reinhardt@amd.com 11147528Ssteve.reinhardt@amd.comdef tryAsSimObjectOrVector(value): 11157528Ssteve.reinhardt@amd.com if isSimObjectOrVector(value): 11167528Ssteve.reinhardt@amd.com return value 11177528Ssteve.reinhardt@amd.com if isSimObjectSequence(value): 11187528Ssteve.reinhardt@amd.com return SimObjectVector(value) 11197528Ssteve.reinhardt@amd.com return None 11207528Ssteve.reinhardt@amd.com 11217528Ssteve.reinhardt@amd.comdef coerceSimObjectOrVector(value): 11227528Ssteve.reinhardt@amd.com value = tryAsSimObjectOrVector(value) 11237528Ssteve.reinhardt@amd.com if value is None: 11247528Ssteve.reinhardt@amd.com raise TypeError, "SimObject or SimObjectVector expected" 11257528Ssteve.reinhardt@amd.com return value 11267528Ssteve.reinhardt@amd.com 11276654Snate@binkert.orgbaseClasses = allClasses.copy() 11286654Snate@binkert.orgbaseInstances = instanceDict.copy() 11296654Snate@binkert.org 11306654Snate@binkert.orgdef clear(): 11319338SAndreas.Sandberg@arm.com global allClasses, instanceDict, noCxxHeader 11326654Snate@binkert.org 11336654Snate@binkert.org allClasses = baseClasses.copy() 11346654Snate@binkert.org instanceDict = baseInstances.copy() 11359338SAndreas.Sandberg@arm.com noCxxHeader = False 11366654Snate@binkert.org 11371528SN/A# __all__ defines the list of symbols that get exported when 11381528SN/A# 'from config import *' is invoked. Try to keep this reasonably 11391528SN/A# short to avoid polluting other namespaces. 11404762Snate@binkert.org__all__ = [ 'SimObject' ] 1141