SimObject.py revision 8840
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 147534Ssteve.reinhardt@amd.com# Copyright (c) 2010 Advanced Micro Devices, Inc. 151046SN/A# All rights reserved. 161046SN/A# 171046SN/A# Redistribution and use in source and binary forms, with or without 181046SN/A# modification, are permitted provided that the following conditions are 191046SN/A# met: redistributions of source code must retain the above copyright 201046SN/A# notice, this list of conditions and the following disclaimer; 211046SN/A# redistributions in binary form must reproduce the above copyright 221046SN/A# notice, this list of conditions and the following disclaimer in the 231046SN/A# documentation and/or other materials provided with the distribution; 241046SN/A# neither the name of the copyright holders nor the names of its 251046SN/A# contributors may be used to endorse or promote products derived from 261046SN/A# this software without specific prior written permission. 271046SN/A# 281046SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 291046SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 301046SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 311046SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 321046SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 331046SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 341046SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 351046SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 361046SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 371046SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 381046SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 392665SN/A# 402665SN/A# Authors: Steve Reinhardt 412665SN/A# Nathan Binkert 428840Sandreas.hansson@arm.com# Andreas Hansson 431046SN/A 445766Snate@binkert.orgimport sys 458331Ssteve.reinhardt@amd.comfrom types import FunctionType, MethodType, ModuleType 461438SN/A 476654Snate@binkert.orgtry: 486654Snate@binkert.org import pydot 496654Snate@binkert.orgexcept: 506654Snate@binkert.org pydot = False 516654Snate@binkert.org 524762Snate@binkert.orgimport m5 536654Snate@binkert.orgfrom m5.util import * 543102Sstever@eecs.umich.edu 553102Sstever@eecs.umich.edu# Have to import params up top since Param is referenced on initial 563102Sstever@eecs.umich.edu# load (when SimObject class references Param to create a class 573102Sstever@eecs.umich.edu# variable, the 'name' param)... 586654Snate@binkert.orgfrom m5.params import * 593102Sstever@eecs.umich.edu# There are a few things we need that aren't in params.__all__ since 603102Sstever@eecs.umich.edu# normal users don't need them 617528Ssteve.reinhardt@amd.comfrom m5.params import ParamDesc, VectorParamDesc, \ 628839Sandreas.hansson@arm.com isNullPointer, SimObjectVector, Port 633102Sstever@eecs.umich.edu 646654Snate@binkert.orgfrom m5.proxy import * 656654Snate@binkert.orgfrom m5.proxy import isproxy 66679SN/A 67679SN/A##################################################################### 68679SN/A# 69679SN/A# M5 Python Configuration Utility 70679SN/A# 71679SN/A# The basic idea is to write simple Python programs that build Python 721692SN/A# objects corresponding to M5 SimObjects for the desired simulation 73679SN/A# configuration. For now, the Python emits a .ini file that can be 74679SN/A# parsed by M5. In the future, some tighter integration between M5 75679SN/A# and the Python interpreter may allow bypassing the .ini file. 76679SN/A# 77679SN/A# Each SimObject class in M5 is represented by a Python class with the 78679SN/A# same name. The Python inheritance tree mirrors the M5 C++ tree 79679SN/A# (e.g., SimpleCPU derives from BaseCPU in both cases, and all 80679SN/A# SimObjects inherit from a single SimObject base class). To specify 81679SN/A# an instance of an M5 SimObject in a configuration, the user simply 82679SN/A# instantiates the corresponding Python object. The parameters for 83679SN/A# that SimObject are given by assigning to attributes of the Python 84679SN/A# object, either using keyword assignment in the constructor or in 85679SN/A# separate assignment statements. For example: 86679SN/A# 871692SN/A# cache = BaseCache(size='64KB') 88679SN/A# cache.hit_latency = 3 89679SN/A# cache.assoc = 8 90679SN/A# 91679SN/A# The magic lies in the mapping of the Python attributes for SimObject 92679SN/A# classes to the actual SimObject parameter specifications. This 93679SN/A# allows parameter validity checking in the Python code. Continuing 94679SN/A# the example above, the statements "cache.blurfl=3" or 95679SN/A# "cache.assoc='hello'" would both result in runtime errors in Python, 96679SN/A# since the BaseCache object has no 'blurfl' parameter and the 'assoc' 97679SN/A# parameter requires an integer, respectively. This magic is done 98679SN/A# primarily by overriding the special __setattr__ method that controls 99679SN/A# assignment to object attributes. 100679SN/A# 101679SN/A# Once a set of Python objects have been instantiated in a hierarchy, 102679SN/A# calling 'instantiate(obj)' (where obj is the root of the hierarchy) 1032740SN/A# will generate a .ini file. 104679SN/A# 105679SN/A##################################################################### 106679SN/A 1074762Snate@binkert.org# list of all SimObject classes 1084762Snate@binkert.orgallClasses = {} 1094762Snate@binkert.org 1102738SN/A# dict to look up SimObjects based on path 1112738SN/AinstanceDict = {} 1122738SN/A 1137673Snate@binkert.orgdef public_value(key, value): 1147673Snate@binkert.org return key.startswith('_') or \ 1158331Ssteve.reinhardt@amd.com isinstance(value, (FunctionType, MethodType, ModuleType, 1168331Ssteve.reinhardt@amd.com classmethod, type)) 1177673Snate@binkert.org 1182740SN/A# The metaclass for SimObject. This class controls how new classes 1192740SN/A# that derive from SimObject are instantiated, and provides inherited 1202740SN/A# class behavior (just like a class controls how instances of that 1212740SN/A# class are instantiated, and provides inherited instance behavior). 1221692SN/Aclass MetaSimObject(type): 1231427SN/A # Attributes that can be set only at initialization time 1247493Ssteve.reinhardt@amd.com init_keywords = { 'abstract' : bool, 1257493Ssteve.reinhardt@amd.com 'cxx_class' : str, 1267493Ssteve.reinhardt@amd.com 'cxx_type' : str, 1277493Ssteve.reinhardt@amd.com 'type' : str } 1281427SN/A # Attributes that can be set any time 1297493Ssteve.reinhardt@amd.com keywords = { 'check' : FunctionType } 130679SN/A 131679SN/A # __new__ is called before __init__, and is where the statements 132679SN/A # in the body of the class definition get loaded into the class's 1332740SN/A # __dict__. We intercept this to filter out parameter & port assignments 134679SN/A # and only allow "private" attributes to be passed to the base 135679SN/A # __new__ (starting with underscore). 1361310SN/A def __new__(mcls, name, bases, dict): 1376654Snate@binkert.org assert name not in allClasses, "SimObject %s already present" % name 1384762Snate@binkert.org 1392740SN/A # Copy "private" attributes, functions, and classes to the 1402740SN/A # official dict. Everything else goes in _init_dict to be 1412740SN/A # filtered in __init__. 1422740SN/A cls_dict = {} 1432740SN/A value_dict = {} 1442740SN/A for key,val in dict.items(): 1457673Snate@binkert.org if public_value(key, val): 1462740SN/A cls_dict[key] = val 1472740SN/A else: 1482740SN/A # must be a param/port setting 1492740SN/A value_dict[key] = val 1504762Snate@binkert.org if 'abstract' not in value_dict: 1514762Snate@binkert.org value_dict['abstract'] = False 1522740SN/A cls_dict['_value_dict'] = value_dict 1534762Snate@binkert.org cls = super(MetaSimObject, mcls).__new__(mcls, name, bases, cls_dict) 1544762Snate@binkert.org if 'type' in value_dict: 1554762Snate@binkert.org allClasses[name] = cls 1564762Snate@binkert.org return cls 157679SN/A 1582711SN/A # subclass initialization 159679SN/A def __init__(cls, name, bases, dict): 1602711SN/A # calls type.__init__()... I think that's a no-op, but leave 1612711SN/A # it here just in case it's not. 1621692SN/A super(MetaSimObject, cls).__init__(name, bases, dict) 1631310SN/A 1641427SN/A # initialize required attributes 1652740SN/A 1662740SN/A # class-only attributes 1672740SN/A cls._params = multidict() # param descriptions 1682740SN/A cls._ports = multidict() # port descriptions 1692740SN/A 1702740SN/A # class or instance attributes 1712740SN/A cls._values = multidict() # param values 1727528Ssteve.reinhardt@amd.com cls._children = multidict() # SimObject children 1733105Sstever@eecs.umich.edu cls._port_refs = multidict() # port ref objects 1742740SN/A cls._instantiated = False # really instantiated, cloned, or subclassed 1751310SN/A 1761692SN/A # We don't support multiple inheritance. If you want to, you 1771585SN/A # must fix multidict to deal with it properly. 1781692SN/A if len(bases) > 1: 1791692SN/A raise TypeError, "SimObjects do not support multiple inheritance" 1801692SN/A 1811692SN/A base = bases[0] 1821692SN/A 1832740SN/A # Set up general inheritance via multidicts. A subclass will 1842740SN/A # inherit all its settings from the base class. The only time 1852740SN/A # the following is not true is when we define the SimObject 1862740SN/A # class itself (in which case the multidicts have no parent). 1871692SN/A if isinstance(base, MetaSimObject): 1885610Snate@binkert.org cls._base = base 1891692SN/A cls._params.parent = base._params 1902740SN/A cls._ports.parent = base._ports 1911692SN/A cls._values.parent = base._values 1927528Ssteve.reinhardt@amd.com cls._children.parent = base._children 1933105Sstever@eecs.umich.edu cls._port_refs.parent = base._port_refs 1942740SN/A # mark base as having been subclassed 1952712SN/A base._instantiated = True 1965610Snate@binkert.org else: 1975610Snate@binkert.org cls._base = None 1981692SN/A 1994762Snate@binkert.org # default keyword values 2004762Snate@binkert.org if 'type' in cls._value_dict: 2014762Snate@binkert.org if 'cxx_class' not in cls._value_dict: 2025610Snate@binkert.org cls._value_dict['cxx_class'] = cls._value_dict['type'] 2034762Snate@binkert.org 2045610Snate@binkert.org cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class'] 2054859Snate@binkert.org 2068597Ssteve.reinhardt@amd.com # Export methods are automatically inherited via C++, so we 2078597Ssteve.reinhardt@amd.com # don't want the method declarations to get inherited on the 2088597Ssteve.reinhardt@amd.com # python side (and thus end up getting repeated in the wrapped 2098597Ssteve.reinhardt@amd.com # versions of derived classes). The code below basicallly 2108597Ssteve.reinhardt@amd.com # suppresses inheritance by substituting in the base (null) 2118597Ssteve.reinhardt@amd.com # versions of these methods unless a different version is 2128597Ssteve.reinhardt@amd.com # explicitly supplied. 2138597Ssteve.reinhardt@amd.com for method_name in ('export_methods', 'export_method_cxx_predecls', 2148597Ssteve.reinhardt@amd.com 'export_method_swig_predecls'): 2158597Ssteve.reinhardt@amd.com if method_name not in cls.__dict__: 2168597Ssteve.reinhardt@amd.com base_method = getattr(MetaSimObject, method_name) 2178597Ssteve.reinhardt@amd.com m = MethodType(base_method, cls, MetaSimObject) 2188597Ssteve.reinhardt@amd.com setattr(cls, method_name, m) 2198597Ssteve.reinhardt@amd.com 2202740SN/A # Now process the _value_dict items. They could be defining 2212740SN/A # new (or overriding existing) parameters or ports, setting 2222740SN/A # class keywords (e.g., 'abstract'), or setting parameter 2232740SN/A # values or port bindings. The first 3 can only be set when 2242740SN/A # the class is defined, so we handle them here. The others 2252740SN/A # can be set later too, so just emulate that by calling 2262740SN/A # setattr(). 2272740SN/A for key,val in cls._value_dict.items(): 2281527SN/A # param descriptions 2292740SN/A if isinstance(val, ParamDesc): 2301585SN/A cls._new_param(key, val) 2311427SN/A 2322738SN/A # port objects 2332738SN/A elif isinstance(val, Port): 2343105Sstever@eecs.umich.edu cls._new_port(key, val) 2352738SN/A 2361427SN/A # init-time-only keywords 2371427SN/A elif cls.init_keywords.has_key(key): 2381427SN/A cls._set_keyword(key, val, cls.init_keywords[key]) 2391427SN/A 2401427SN/A # default: use normal path (ends up in __setattr__) 2411427SN/A else: 2421427SN/A setattr(cls, key, val) 2431427SN/A 2441427SN/A def _set_keyword(cls, keyword, val, kwtype): 2451427SN/A if not isinstance(val, kwtype): 2461427SN/A raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \ 2471427SN/A (keyword, type(val), kwtype) 2487493Ssteve.reinhardt@amd.com if isinstance(val, FunctionType): 2491427SN/A val = classmethod(val) 2501427SN/A type.__setattr__(cls, keyword, val) 2511427SN/A 2523100SN/A def _new_param(cls, name, pdesc): 2533100SN/A # each param desc should be uniquely assigned to one variable 2543100SN/A assert(not hasattr(pdesc, 'name')) 2553100SN/A pdesc.name = name 2563100SN/A cls._params[name] = pdesc 2573100SN/A if hasattr(pdesc, 'default'): 2583105Sstever@eecs.umich.edu cls._set_param(name, pdesc.default, pdesc) 2593105Sstever@eecs.umich.edu 2603105Sstever@eecs.umich.edu def _set_param(cls, name, value, param): 2613105Sstever@eecs.umich.edu assert(param.name == name) 2623105Sstever@eecs.umich.edu try: 2638321Ssteve.reinhardt@amd.com value = param.convert(value) 2643105Sstever@eecs.umich.edu except Exception, e: 2653105Sstever@eecs.umich.edu msg = "%s\nError setting param %s.%s to %s\n" % \ 2663105Sstever@eecs.umich.edu (e, cls.__name__, name, value) 2673105Sstever@eecs.umich.edu e.args = (msg, ) 2683105Sstever@eecs.umich.edu raise 2698321Ssteve.reinhardt@amd.com cls._values[name] = value 2708321Ssteve.reinhardt@amd.com # if param value is a SimObject, make it a child too, so that 2718321Ssteve.reinhardt@amd.com # it gets cloned properly when the class is instantiated 2728321Ssteve.reinhardt@amd.com if isSimObjectOrVector(value) and not value.has_parent(): 2738321Ssteve.reinhardt@amd.com cls._add_cls_child(name, value) 2748321Ssteve.reinhardt@amd.com 2758321Ssteve.reinhardt@amd.com def _add_cls_child(cls, name, child): 2768321Ssteve.reinhardt@amd.com # It's a little funky to have a class as a parent, but these 2778321Ssteve.reinhardt@amd.com # objects should never be instantiated (only cloned, which 2788321Ssteve.reinhardt@amd.com # clears the parent pointer), and this makes it clear that the 2798321Ssteve.reinhardt@amd.com # object is not an orphan and can provide better error 2808321Ssteve.reinhardt@amd.com # messages. 2818321Ssteve.reinhardt@amd.com child.set_parent(cls, name) 2828321Ssteve.reinhardt@amd.com cls._children[name] = child 2833105Sstever@eecs.umich.edu 2843105Sstever@eecs.umich.edu def _new_port(cls, name, port): 2853105Sstever@eecs.umich.edu # each port should be uniquely assigned to one variable 2863105Sstever@eecs.umich.edu assert(not hasattr(port, 'name')) 2873105Sstever@eecs.umich.edu port.name = name 2883105Sstever@eecs.umich.edu cls._ports[name] = port 2893105Sstever@eecs.umich.edu 2903105Sstever@eecs.umich.edu # same as _get_port_ref, effectively, but for classes 2913105Sstever@eecs.umich.edu def _cls_get_port_ref(cls, attr): 2923105Sstever@eecs.umich.edu # Return reference that can be assigned to another port 2933105Sstever@eecs.umich.edu # via __setattr__. There is only ever one reference 2943105Sstever@eecs.umich.edu # object per port, but we create them lazily here. 2953105Sstever@eecs.umich.edu ref = cls._port_refs.get(attr) 2963105Sstever@eecs.umich.edu if not ref: 2973105Sstever@eecs.umich.edu ref = cls._ports[attr].makeRef(cls) 2983105Sstever@eecs.umich.edu cls._port_refs[attr] = ref 2993105Sstever@eecs.umich.edu return ref 3001585SN/A 3011310SN/A # Set attribute (called on foo.attr = value when foo is an 3021310SN/A # instance of class cls). 3031310SN/A def __setattr__(cls, attr, value): 3041310SN/A # normal processing for private attributes 3057673Snate@binkert.org if public_value(attr, value): 3061310SN/A type.__setattr__(cls, attr, value) 3071310SN/A return 3081310SN/A 3091310SN/A if cls.keywords.has_key(attr): 3101427SN/A cls._set_keyword(attr, value, cls.keywords[attr]) 3111310SN/A return 3121310SN/A 3132738SN/A if cls._ports.has_key(attr): 3143105Sstever@eecs.umich.edu cls._cls_get_port_ref(attr).connect(value) 3152738SN/A return 3162738SN/A 3172740SN/A if isSimObjectOrSequence(value) and cls._instantiated: 3182740SN/A raise RuntimeError, \ 3192740SN/A "cannot set SimObject parameter '%s' after\n" \ 3202740SN/A " class %s has been instantiated or subclassed" \ 3212740SN/A % (attr, cls.__name__) 3222740SN/A 3232740SN/A # check for param 3243105Sstever@eecs.umich.edu param = cls._params.get(attr) 3251310SN/A if param: 3263105Sstever@eecs.umich.edu cls._set_param(attr, value, param) 3273105Sstever@eecs.umich.edu return 3283105Sstever@eecs.umich.edu 3293105Sstever@eecs.umich.edu if isSimObjectOrSequence(value): 3303105Sstever@eecs.umich.edu # If RHS is a SimObject, it's an implicit child assignment. 3318321Ssteve.reinhardt@amd.com cls._add_cls_child(attr, coerceSimObjectOrVector(value)) 3323105Sstever@eecs.umich.edu return 3333105Sstever@eecs.umich.edu 3343105Sstever@eecs.umich.edu # no valid assignment... raise exception 3353105Sstever@eecs.umich.edu raise AttributeError, \ 3363105Sstever@eecs.umich.edu "Class %s has no parameter \'%s\'" % (cls.__name__, attr) 3371310SN/A 3381585SN/A def __getattr__(cls, attr): 3397675Snate@binkert.org if attr == 'cxx_class_path': 3407675Snate@binkert.org return cls.cxx_class.split('::') 3417675Snate@binkert.org 3427675Snate@binkert.org if attr == 'cxx_class_name': 3437675Snate@binkert.org return cls.cxx_class_path[-1] 3447675Snate@binkert.org 3457675Snate@binkert.org if attr == 'cxx_namespaces': 3467675Snate@binkert.org return cls.cxx_class_path[:-1] 3477675Snate@binkert.org 3481692SN/A if cls._values.has_key(attr): 3491692SN/A return cls._values[attr] 3501585SN/A 3517528Ssteve.reinhardt@amd.com if cls._children.has_key(attr): 3527528Ssteve.reinhardt@amd.com return cls._children[attr] 3537528Ssteve.reinhardt@amd.com 3541585SN/A raise AttributeError, \ 3551585SN/A "object '%s' has no attribute '%s'" % (cls.__name__, attr) 3561585SN/A 3573100SN/A def __str__(cls): 3583100SN/A return cls.__name__ 3593100SN/A 3608596Ssteve.reinhardt@amd.com # See ParamValue.cxx_predecls for description. 3618596Ssteve.reinhardt@amd.com def cxx_predecls(cls, code): 3628596Ssteve.reinhardt@amd.com code('#include "params/$cls.hh"') 3638596Ssteve.reinhardt@amd.com 3648596Ssteve.reinhardt@amd.com # See ParamValue.swig_predecls for description. 3658596Ssteve.reinhardt@amd.com def swig_predecls(cls, code): 3668596Ssteve.reinhardt@amd.com code('%import "python/m5/internal/param_$cls.i"') 3678596Ssteve.reinhardt@amd.com 3688597Ssteve.reinhardt@amd.com # Hook for exporting additional C++ methods to Python via SWIG. 3698597Ssteve.reinhardt@amd.com # Default is none, override using @classmethod in class definition. 3708597Ssteve.reinhardt@amd.com def export_methods(cls, code): 3718597Ssteve.reinhardt@amd.com pass 3728597Ssteve.reinhardt@amd.com 3738597Ssteve.reinhardt@amd.com # Generate the code needed as a prerequisite for the C++ methods 3748597Ssteve.reinhardt@amd.com # exported via export_methods() to be compiled in the _wrap.cc 3758597Ssteve.reinhardt@amd.com # file. Typically generates one or more #include statements. If 3768597Ssteve.reinhardt@amd.com # any methods are exported, typically at least the C++ header 3778597Ssteve.reinhardt@amd.com # declaring the relevant SimObject class must be included. 3788597Ssteve.reinhardt@amd.com def export_method_cxx_predecls(cls, code): 3798597Ssteve.reinhardt@amd.com pass 3808597Ssteve.reinhardt@amd.com 3818597Ssteve.reinhardt@amd.com # Generate the code needed as a prerequisite for the C++ methods 3828597Ssteve.reinhardt@amd.com # exported via export_methods() to be processed by SWIG. 3838597Ssteve.reinhardt@amd.com # Typically generates one or more %include or %import statements. 3848597Ssteve.reinhardt@amd.com # If any methods are exported, typically at least the C++ header 3858597Ssteve.reinhardt@amd.com # declaring the relevant SimObject class must be included. 3868597Ssteve.reinhardt@amd.com def export_method_swig_predecls(cls, code): 3878597Ssteve.reinhardt@amd.com pass 3888597Ssteve.reinhardt@amd.com 3898596Ssteve.reinhardt@amd.com # Generate the declaration for this object for wrapping with SWIG. 3908596Ssteve.reinhardt@amd.com # Generates code that goes into a SWIG .i file. Called from 3918596Ssteve.reinhardt@amd.com # src/SConscript. 3928596Ssteve.reinhardt@amd.com def swig_decl(cls, code): 3938596Ssteve.reinhardt@amd.com class_path = cls.cxx_class.split('::') 3948596Ssteve.reinhardt@amd.com classname = class_path[-1] 3958596Ssteve.reinhardt@amd.com namespaces = class_path[:-1] 3968596Ssteve.reinhardt@amd.com 3978596Ssteve.reinhardt@amd.com # The 'local' attribute restricts us to the params declared in 3988596Ssteve.reinhardt@amd.com # the object itself, not including inherited params (which 3998596Ssteve.reinhardt@amd.com # will also be inherited from the base class's param struct 4008596Ssteve.reinhardt@amd.com # here). 4018596Ssteve.reinhardt@amd.com params = cls._params.local.values() 4028840Sandreas.hansson@arm.com ports = cls._ports.local 4038596Ssteve.reinhardt@amd.com 4048596Ssteve.reinhardt@amd.com code('%module(package="m5.internal") param_$cls') 4058596Ssteve.reinhardt@amd.com code() 4068596Ssteve.reinhardt@amd.com code('%{') 4078596Ssteve.reinhardt@amd.com code('#include "params/$cls.hh"') 4088596Ssteve.reinhardt@amd.com for param in params: 4098596Ssteve.reinhardt@amd.com param.cxx_predecls(code) 4108597Ssteve.reinhardt@amd.com cls.export_method_cxx_predecls(code) 4118596Ssteve.reinhardt@amd.com code('%}') 4128596Ssteve.reinhardt@amd.com code() 4138596Ssteve.reinhardt@amd.com 4148596Ssteve.reinhardt@amd.com for param in params: 4158596Ssteve.reinhardt@amd.com param.swig_predecls(code) 4168597Ssteve.reinhardt@amd.com cls.export_method_swig_predecls(code) 4178596Ssteve.reinhardt@amd.com 4188596Ssteve.reinhardt@amd.com code() 4198596Ssteve.reinhardt@amd.com if cls._base: 4208596Ssteve.reinhardt@amd.com code('%import "python/m5/internal/param_${{cls._base}}.i"') 4218596Ssteve.reinhardt@amd.com code() 4228596Ssteve.reinhardt@amd.com 4238596Ssteve.reinhardt@amd.com for ns in namespaces: 4248596Ssteve.reinhardt@amd.com code('namespace $ns {') 4258596Ssteve.reinhardt@amd.com 4268596Ssteve.reinhardt@amd.com if namespaces: 4278596Ssteve.reinhardt@amd.com code('// avoid name conflicts') 4288596Ssteve.reinhardt@amd.com sep_string = '_COLONS_' 4298596Ssteve.reinhardt@amd.com flat_name = sep_string.join(class_path) 4308596Ssteve.reinhardt@amd.com code('%rename($flat_name) $classname;') 4318596Ssteve.reinhardt@amd.com 4328597Ssteve.reinhardt@amd.com code() 4338597Ssteve.reinhardt@amd.com code('// stop swig from creating/wrapping default ctor/dtor') 4348597Ssteve.reinhardt@amd.com code('%nodefault $classname;') 4358597Ssteve.reinhardt@amd.com code('class $classname') 4368597Ssteve.reinhardt@amd.com if cls._base: 4378597Ssteve.reinhardt@amd.com code(' : public ${{cls._base.cxx_class}}') 4388597Ssteve.reinhardt@amd.com code('{') 4398597Ssteve.reinhardt@amd.com code(' public:') 4408597Ssteve.reinhardt@amd.com cls.export_methods(code) 4418597Ssteve.reinhardt@amd.com code('};') 4428596Ssteve.reinhardt@amd.com 4438596Ssteve.reinhardt@amd.com for ns in reversed(namespaces): 4448596Ssteve.reinhardt@amd.com code('} // namespace $ns') 4458596Ssteve.reinhardt@amd.com 4468596Ssteve.reinhardt@amd.com code() 4478596Ssteve.reinhardt@amd.com code('%include "params/$cls.hh"') 4488596Ssteve.reinhardt@amd.com 4498596Ssteve.reinhardt@amd.com 4508596Ssteve.reinhardt@amd.com # Generate the C++ declaration (.hh file) for this SimObject's 4518596Ssteve.reinhardt@amd.com # param struct. Called from src/SConscript. 4528596Ssteve.reinhardt@amd.com def cxx_param_decl(cls, code): 4538596Ssteve.reinhardt@amd.com # The 'local' attribute restricts us to the params declared in 4543100SN/A # the object itself, not including inherited params (which 4553100SN/A # will also be inherited from the base class's param struct 4563100SN/A # here). 4574762Snate@binkert.org params = cls._params.local.values() 4588840Sandreas.hansson@arm.com ports = cls._ports.local 4593100SN/A try: 4603100SN/A ptypes = [p.ptype for p in params] 4613100SN/A except: 4623100SN/A print cls, p, p.ptype_str 4633100SN/A print params 4643100SN/A raise 4653100SN/A 4667675Snate@binkert.org class_path = cls._value_dict['cxx_class'].split('::') 4677675Snate@binkert.org 4687675Snate@binkert.org code('''\ 4697675Snate@binkert.org#ifndef __PARAMS__${cls}__ 4707675Snate@binkert.org#define __PARAMS__${cls}__ 4717675Snate@binkert.org 4727675Snate@binkert.org''') 4737675Snate@binkert.org 4747675Snate@binkert.org # A forward class declaration is sufficient since we are just 4757675Snate@binkert.org # declaring a pointer. 4767675Snate@binkert.org for ns in class_path[:-1]: 4777675Snate@binkert.org code('namespace $ns {') 4787675Snate@binkert.org code('class $0;', class_path[-1]) 4797675Snate@binkert.org for ns in reversed(class_path[:-1]): 4807811Ssteve.reinhardt@amd.com code('} // namespace $ns') 4817675Snate@binkert.org code() 4827675Snate@binkert.org 4838597Ssteve.reinhardt@amd.com # The base SimObject has a couple of params that get 4848597Ssteve.reinhardt@amd.com # automatically set from Python without being declared through 4858597Ssteve.reinhardt@amd.com # the normal Param mechanism; we slip them in here (needed 4868597Ssteve.reinhardt@amd.com # predecls now, actual declarations below) 4878597Ssteve.reinhardt@amd.com if cls == SimObject: 4888597Ssteve.reinhardt@amd.com code(''' 4898597Ssteve.reinhardt@amd.com#ifndef PY_VERSION 4908597Ssteve.reinhardt@amd.comstruct PyObject; 4918597Ssteve.reinhardt@amd.com#endif 4928597Ssteve.reinhardt@amd.com 4938597Ssteve.reinhardt@amd.com#include <string> 4948597Ssteve.reinhardt@amd.com 4958737Skoansin.tan@gmail.comclass EventQueue; 4968597Ssteve.reinhardt@amd.com''') 4977673Snate@binkert.org for param in params: 4987673Snate@binkert.org param.cxx_predecls(code) 4998840Sandreas.hansson@arm.com for port in ports.itervalues(): 5008840Sandreas.hansson@arm.com port.cxx_predecls(code) 5017673Snate@binkert.org code() 5024762Snate@binkert.org 5035610Snate@binkert.org if cls._base: 5047673Snate@binkert.org code('#include "params/${{cls._base.type}}.hh"') 5057673Snate@binkert.org code() 5064762Snate@binkert.org 5074762Snate@binkert.org for ptype in ptypes: 5084762Snate@binkert.org if issubclass(ptype, Enum): 5097673Snate@binkert.org code('#include "enums/${{ptype.__name__}}.hh"') 5107673Snate@binkert.org code() 5114762Snate@binkert.org 5128596Ssteve.reinhardt@amd.com # now generate the actual param struct 5138597Ssteve.reinhardt@amd.com code("struct ${cls}Params") 5148597Ssteve.reinhardt@amd.com if cls._base: 5158597Ssteve.reinhardt@amd.com code(" : public ${{cls._base.type}}Params") 5168597Ssteve.reinhardt@amd.com code("{") 5178597Ssteve.reinhardt@amd.com if not hasattr(cls, 'abstract') or not cls.abstract: 5188597Ssteve.reinhardt@amd.com if 'type' in cls.__dict__: 5198597Ssteve.reinhardt@amd.com code(" ${{cls.cxx_type}} create();") 5208597Ssteve.reinhardt@amd.com 5218597Ssteve.reinhardt@amd.com code.indent() 5228596Ssteve.reinhardt@amd.com if cls == SimObject: 5238597Ssteve.reinhardt@amd.com code(''' 5248597Ssteve.reinhardt@amd.com SimObjectParams() 5258597Ssteve.reinhardt@amd.com { 5268597Ssteve.reinhardt@amd.com extern EventQueue mainEventQueue; 5278597Ssteve.reinhardt@amd.com eventq = &mainEventQueue; 5288597Ssteve.reinhardt@amd.com } 5298597Ssteve.reinhardt@amd.com virtual ~SimObjectParams() {} 5308596Ssteve.reinhardt@amd.com 5318597Ssteve.reinhardt@amd.com std::string name; 5328597Ssteve.reinhardt@amd.com PyObject *pyobj; 5338597Ssteve.reinhardt@amd.com EventQueue *eventq; 5348597Ssteve.reinhardt@amd.com ''') 5358597Ssteve.reinhardt@amd.com for param in params: 5368597Ssteve.reinhardt@amd.com param.cxx_decl(code) 5378840Sandreas.hansson@arm.com for port in ports.itervalues(): 5388840Sandreas.hansson@arm.com port.cxx_decl(code) 5398840Sandreas.hansson@arm.com 5408597Ssteve.reinhardt@amd.com code.dedent() 5418597Ssteve.reinhardt@amd.com code('};') 5425488Snate@binkert.org 5437673Snate@binkert.org code() 5447673Snate@binkert.org code('#endif // __PARAMS__${cls}__') 5455488Snate@binkert.org return code 5465488Snate@binkert.org 5475488Snate@binkert.org 5483100SN/A 5492740SN/A# The SimObject class is the root of the special hierarchy. Most of 550679SN/A# the code in this class deals with the configuration hierarchy itself 551679SN/A# (parent/child node relationships). 5521692SN/Aclass SimObject(object): 5531692SN/A # Specify metaclass. Any class inheriting from SimObject will 554679SN/A # get this metaclass. 5551692SN/A __metaclass__ = MetaSimObject 5563100SN/A type = 'SimObject' 5574762Snate@binkert.org abstract = True 5583100SN/A 5598597Ssteve.reinhardt@amd.com @classmethod 5608597Ssteve.reinhardt@amd.com def export_method_cxx_predecls(cls, code): 5618597Ssteve.reinhardt@amd.com code(''' 5628597Ssteve.reinhardt@amd.com#include <Python.h> 5638597Ssteve.reinhardt@amd.com 5648597Ssteve.reinhardt@amd.com#include "sim/serialize.hh" 5658597Ssteve.reinhardt@amd.com#include "sim/sim_object.hh" 5668597Ssteve.reinhardt@amd.com''') 5678597Ssteve.reinhardt@amd.com 5688597Ssteve.reinhardt@amd.com @classmethod 5698597Ssteve.reinhardt@amd.com def export_method_swig_predecls(cls, code): 5708597Ssteve.reinhardt@amd.com code(''' 5718597Ssteve.reinhardt@amd.com%include <std_string.i> 5728597Ssteve.reinhardt@amd.com''') 5738597Ssteve.reinhardt@amd.com 5748597Ssteve.reinhardt@amd.com @classmethod 5758597Ssteve.reinhardt@amd.com def export_methods(cls, code): 5768597Ssteve.reinhardt@amd.com code(''' 5778597Ssteve.reinhardt@amd.com enum State { 5788597Ssteve.reinhardt@amd.com Running, 5798597Ssteve.reinhardt@amd.com Draining, 5808597Ssteve.reinhardt@amd.com Drained 5818597Ssteve.reinhardt@amd.com }; 5828597Ssteve.reinhardt@amd.com 5838597Ssteve.reinhardt@amd.com void init(); 5848597Ssteve.reinhardt@amd.com void loadState(Checkpoint *cp); 5858597Ssteve.reinhardt@amd.com void initState(); 5868597Ssteve.reinhardt@amd.com void regStats(); 5878597Ssteve.reinhardt@amd.com void regFormulas(); 5888597Ssteve.reinhardt@amd.com void resetStats(); 5898597Ssteve.reinhardt@amd.com void startup(); 5908597Ssteve.reinhardt@amd.com 5918597Ssteve.reinhardt@amd.com unsigned int drain(Event *drain_event); 5928597Ssteve.reinhardt@amd.com void resume(); 5938597Ssteve.reinhardt@amd.com void switchOut(); 5948597Ssteve.reinhardt@amd.com void takeOverFrom(BaseCPU *cpu); 5958597Ssteve.reinhardt@amd.com''') 5968597Ssteve.reinhardt@amd.com 5972740SN/A # Initialize new instance. For objects with SimObject-valued 5982740SN/A # children, we need to recursively clone the classes represented 5992740SN/A # by those param values as well in a consistent "deep copy"-style 6002740SN/A # fashion. That is, we want to make sure that each instance is 6012740SN/A # cloned only once, and that if there are multiple references to 6022740SN/A # the same original object, we end up with the corresponding 6032740SN/A # cloned references all pointing to the same cloned instance. 6042740SN/A def __init__(self, **kwargs): 6052740SN/A ancestor = kwargs.get('_ancestor') 6062740SN/A memo_dict = kwargs.get('_memo') 6072740SN/A if memo_dict is None: 6082740SN/A # prepare to memoize any recursively instantiated objects 6092740SN/A memo_dict = {} 6102740SN/A elif ancestor: 6112740SN/A # memoize me now to avoid problems with recursive calls 6122740SN/A memo_dict[ancestor] = self 6132711SN/A 6142740SN/A if not ancestor: 6152740SN/A ancestor = self.__class__ 6162740SN/A ancestor._instantiated = True 6172711SN/A 6182740SN/A # initialize required attributes 6192740SN/A self._parent = None 6207528Ssteve.reinhardt@amd.com self._name = None 6212740SN/A self._ccObject = None # pointer to C++ object 6224762Snate@binkert.org self._ccParams = None 6232740SN/A self._instantiated = False # really "cloned" 6242712SN/A 6258321Ssteve.reinhardt@amd.com # Clone children specified at class level. No need for a 6268321Ssteve.reinhardt@amd.com # multidict here since we will be cloning everything. 6278321Ssteve.reinhardt@amd.com # Do children before parameter values so that children that 6288321Ssteve.reinhardt@amd.com # are also param values get cloned properly. 6298321Ssteve.reinhardt@amd.com self._children = {} 6308321Ssteve.reinhardt@amd.com for key,val in ancestor._children.iteritems(): 6318321Ssteve.reinhardt@amd.com self.add_child(key, val(_memo=memo_dict)) 6328321Ssteve.reinhardt@amd.com 6332711SN/A # Inherit parameter values from class using multidict so 6347528Ssteve.reinhardt@amd.com # individual value settings can be overridden but we still 6357528Ssteve.reinhardt@amd.com # inherit late changes to non-overridden class values. 6362740SN/A self._values = multidict(ancestor._values) 6372740SN/A # clone SimObject-valued parameters 6382740SN/A for key,val in ancestor._values.iteritems(): 6397528Ssteve.reinhardt@amd.com val = tryAsSimObjectOrVector(val) 6407528Ssteve.reinhardt@amd.com if val is not None: 6417528Ssteve.reinhardt@amd.com self._values[key] = val(_memo=memo_dict) 6427528Ssteve.reinhardt@amd.com 6432740SN/A # clone port references. no need to use a multidict here 6442740SN/A # since we will be creating new references for all ports. 6453105Sstever@eecs.umich.edu self._port_refs = {} 6463105Sstever@eecs.umich.edu for key,val in ancestor._port_refs.iteritems(): 6473105Sstever@eecs.umich.edu self._port_refs[key] = val.clone(self, memo_dict) 6481692SN/A # apply attribute assignments from keyword args, if any 6491692SN/A for key,val in kwargs.iteritems(): 6501692SN/A setattr(self, key, val) 651679SN/A 6522740SN/A # "Clone" the current instance by creating another instance of 6532740SN/A # this instance's class, but that inherits its parameter values 6542740SN/A # and port mappings from the current instance. If we're in a 6552740SN/A # "deep copy" recursive clone, check the _memo dict to see if 6562740SN/A # we've already cloned this instance. 6571692SN/A def __call__(self, **kwargs): 6582740SN/A memo_dict = kwargs.get('_memo') 6592740SN/A if memo_dict is None: 6602740SN/A # no memo_dict: must be top-level clone operation. 6612740SN/A # this is only allowed at the root of a hierarchy 6622740SN/A if self._parent: 6632740SN/A raise RuntimeError, "attempt to clone object %s " \ 6642740SN/A "not at the root of a tree (parent = %s)" \ 6652740SN/A % (self, self._parent) 6662740SN/A # create a new dict and use that. 6672740SN/A memo_dict = {} 6682740SN/A kwargs['_memo'] = memo_dict 6692740SN/A elif memo_dict.has_key(self): 6702740SN/A # clone already done & memoized 6712740SN/A return memo_dict[self] 6722740SN/A return self.__class__(_ancestor = self, **kwargs) 6731343SN/A 6743105Sstever@eecs.umich.edu def _get_port_ref(self, attr): 6753105Sstever@eecs.umich.edu # Return reference that can be assigned to another port 6763105Sstever@eecs.umich.edu # via __setattr__. There is only ever one reference 6773105Sstever@eecs.umich.edu # object per port, but we create them lazily here. 6783105Sstever@eecs.umich.edu ref = self._port_refs.get(attr) 6793105Sstever@eecs.umich.edu if not ref: 6803105Sstever@eecs.umich.edu ref = self._ports[attr].makeRef(self) 6813105Sstever@eecs.umich.edu self._port_refs[attr] = ref 6823105Sstever@eecs.umich.edu return ref 6833105Sstever@eecs.umich.edu 6841692SN/A def __getattr__(self, attr): 6852738SN/A if self._ports.has_key(attr): 6863105Sstever@eecs.umich.edu return self._get_port_ref(attr) 6872738SN/A 6881692SN/A if self._values.has_key(attr): 6891692SN/A return self._values[attr] 6901427SN/A 6917528Ssteve.reinhardt@amd.com if self._children.has_key(attr): 6927528Ssteve.reinhardt@amd.com return self._children[attr] 6937528Ssteve.reinhardt@amd.com 6947500Ssteve.reinhardt@amd.com # If the attribute exists on the C++ object, transparently 6957500Ssteve.reinhardt@amd.com # forward the reference there. This is typically used for 6967500Ssteve.reinhardt@amd.com # SWIG-wrapped methods such as init(), regStats(), 6977527Ssteve.reinhardt@amd.com # regFormulas(), resetStats(), startup(), drain(), and 6987527Ssteve.reinhardt@amd.com # resume(). 6997500Ssteve.reinhardt@amd.com if self._ccObject and hasattr(self._ccObject, attr): 7007500Ssteve.reinhardt@amd.com return getattr(self._ccObject, attr) 7017500Ssteve.reinhardt@amd.com 7021692SN/A raise AttributeError, "object '%s' has no attribute '%s'" \ 7031692SN/A % (self.__class__.__name__, attr) 7041427SN/A 7051692SN/A # Set attribute (called on foo.attr = value when foo is an 7061692SN/A # instance of class cls). 7071692SN/A def __setattr__(self, attr, value): 7081692SN/A # normal processing for private attributes 7091692SN/A if attr.startswith('_'): 7101692SN/A object.__setattr__(self, attr, value) 7111692SN/A return 7121427SN/A 7132738SN/A if self._ports.has_key(attr): 7142738SN/A # set up port connection 7153105Sstever@eecs.umich.edu self._get_port_ref(attr).connect(value) 7162738SN/A return 7172738SN/A 7182740SN/A if isSimObjectOrSequence(value) and self._instantiated: 7192740SN/A raise RuntimeError, \ 7202740SN/A "cannot set SimObject parameter '%s' after\n" \ 7212740SN/A " instance been cloned %s" % (attr, `self`) 7222740SN/A 7233105Sstever@eecs.umich.edu param = self._params.get(attr) 7241692SN/A if param: 7251310SN/A try: 7261692SN/A value = param.convert(value) 7271587SN/A except Exception, e: 7281692SN/A msg = "%s\nError setting param %s.%s to %s\n" % \ 7291692SN/A (e, self.__class__.__name__, attr, value) 7301605SN/A e.args = (msg, ) 7311605SN/A raise 7327528Ssteve.reinhardt@amd.com self._values[attr] = value 7338321Ssteve.reinhardt@amd.com # implicitly parent unparented objects assigned as params 7348321Ssteve.reinhardt@amd.com if isSimObjectOrVector(value) and not value.has_parent(): 7358321Ssteve.reinhardt@amd.com self.add_child(attr, value) 7363105Sstever@eecs.umich.edu return 7371310SN/A 7387528Ssteve.reinhardt@amd.com # if RHS is a SimObject, it's an implicit child assignment 7393105Sstever@eecs.umich.edu if isSimObjectOrSequence(value): 7407528Ssteve.reinhardt@amd.com self.add_child(attr, value) 7413105Sstever@eecs.umich.edu return 7421693SN/A 7433105Sstever@eecs.umich.edu # no valid assignment... raise exception 7443105Sstever@eecs.umich.edu raise AttributeError, "Class %s has no parameter %s" \ 7453105Sstever@eecs.umich.edu % (self.__class__.__name__, attr) 7461310SN/A 7471310SN/A 7481692SN/A # this hack allows tacking a '[0]' onto parameters that may or may 7491692SN/A # not be vectors, and always getting the first element (e.g. cpus) 7501692SN/A def __getitem__(self, key): 7511692SN/A if key == 0: 7521692SN/A return self 7531692SN/A raise TypeError, "Non-zero index '%s' to SimObject" % key 7541310SN/A 7557528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 7567528Ssteve.reinhardt@amd.com def clear_parent(self, old_parent): 7577528Ssteve.reinhardt@amd.com assert self._parent is old_parent 7587528Ssteve.reinhardt@amd.com self._parent = None 7597528Ssteve.reinhardt@amd.com 7607528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 7617528Ssteve.reinhardt@amd.com def set_parent(self, parent, name): 7627528Ssteve.reinhardt@amd.com self._parent = parent 7637528Ssteve.reinhardt@amd.com self._name = name 7647528Ssteve.reinhardt@amd.com 7657528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 7667528Ssteve.reinhardt@amd.com def get_name(self): 7677528Ssteve.reinhardt@amd.com return self._name 7687528Ssteve.reinhardt@amd.com 7698321Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 7708321Ssteve.reinhardt@amd.com def has_parent(self): 7718321Ssteve.reinhardt@amd.com return self._parent is not None 7727528Ssteve.reinhardt@amd.com 7737742Sgblack@eecs.umich.edu # clear out child with given name. This code is not likely to be exercised. 7747742Sgblack@eecs.umich.edu # See comment in add_child. 7751693SN/A def clear_child(self, name): 7761693SN/A child = self._children[name] 7777528Ssteve.reinhardt@amd.com child.clear_parent(self) 7781693SN/A del self._children[name] 7791693SN/A 7807528Ssteve.reinhardt@amd.com # Add a new child to this object. 7817528Ssteve.reinhardt@amd.com def add_child(self, name, child): 7827528Ssteve.reinhardt@amd.com child = coerceSimObjectOrVector(child) 7838321Ssteve.reinhardt@amd.com if child.has_parent(): 7848321Ssteve.reinhardt@amd.com print "warning: add_child('%s'): child '%s' already has parent" % \ 7858321Ssteve.reinhardt@amd.com (name, child.get_name()) 7867528Ssteve.reinhardt@amd.com if self._children.has_key(name): 7877742Sgblack@eecs.umich.edu # This code path had an undiscovered bug that would make it fail 7887742Sgblack@eecs.umich.edu # at runtime. It had been here for a long time and was only 7897742Sgblack@eecs.umich.edu # exposed by a buggy script. Changes here will probably not be 7907742Sgblack@eecs.umich.edu # exercised without specialized testing. 7917738Sgblack@eecs.umich.edu self.clear_child(name) 7927528Ssteve.reinhardt@amd.com child.set_parent(self, name) 7937528Ssteve.reinhardt@amd.com self._children[name] = child 7941310SN/A 7957528Ssteve.reinhardt@amd.com # Take SimObject-valued parameters that haven't been explicitly 7967528Ssteve.reinhardt@amd.com # assigned as children and make them children of the object that 7977528Ssteve.reinhardt@amd.com # they were assigned to as a parameter value. This guarantees 7987528Ssteve.reinhardt@amd.com # that when we instantiate all the parameter objects we're still 7997528Ssteve.reinhardt@amd.com # inside the configuration hierarchy. 8007528Ssteve.reinhardt@amd.com def adoptOrphanParams(self): 8017528Ssteve.reinhardt@amd.com for key,val in self._values.iteritems(): 8027528Ssteve.reinhardt@amd.com if not isSimObjectVector(val) and isSimObjectSequence(val): 8037528Ssteve.reinhardt@amd.com # need to convert raw SimObject sequences to 8048321Ssteve.reinhardt@amd.com # SimObjectVector class so we can call has_parent() 8057528Ssteve.reinhardt@amd.com val = SimObjectVector(val) 8067528Ssteve.reinhardt@amd.com self._values[key] = val 8078321Ssteve.reinhardt@amd.com if isSimObjectOrVector(val) and not val.has_parent(): 8088321Ssteve.reinhardt@amd.com print "warning: %s adopting orphan SimObject param '%s'" \ 8098321Ssteve.reinhardt@amd.com % (self, key) 8107528Ssteve.reinhardt@amd.com self.add_child(key, val) 8113105Sstever@eecs.umich.edu 8121692SN/A def path(self): 8132740SN/A if not self._parent: 8148321Ssteve.reinhardt@amd.com return '<orphan %s>' % self.__class__ 8151692SN/A ppath = self._parent.path() 8161692SN/A if ppath == 'root': 8171692SN/A return self._name 8181692SN/A return ppath + "." + self._name 8191310SN/A 8201692SN/A def __str__(self): 8211692SN/A return self.path() 8221310SN/A 8231692SN/A def ini_str(self): 8241692SN/A return self.path() 8251310SN/A 8261692SN/A def find_any(self, ptype): 8271692SN/A if isinstance(self, ptype): 8281692SN/A return self, True 8291310SN/A 8301692SN/A found_obj = None 8311692SN/A for child in self._children.itervalues(): 8321692SN/A if isinstance(child, ptype): 8331692SN/A if found_obj != None and child != found_obj: 8341692SN/A raise AttributeError, \ 8351692SN/A 'parent.any matched more than one: %s %s' % \ 8361814SN/A (found_obj.path, child.path) 8371692SN/A found_obj = child 8381692SN/A # search param space 8391692SN/A for pname,pdesc in self._params.iteritems(): 8401692SN/A if issubclass(pdesc.ptype, ptype): 8411692SN/A match_obj = self._values[pname] 8421692SN/A if found_obj != None and found_obj != match_obj: 8431692SN/A raise AttributeError, \ 8445952Ssaidi@eecs.umich.edu 'parent.any matched more than one: %s and %s' % (found_obj.path, match_obj.path) 8451692SN/A found_obj = match_obj 8461692SN/A return found_obj, found_obj != None 8471692SN/A 8488459SAli.Saidi@ARM.com def find_all(self, ptype): 8498459SAli.Saidi@ARM.com all = {} 8508459SAli.Saidi@ARM.com # search children 8518459SAli.Saidi@ARM.com for child in self._children.itervalues(): 8528459SAli.Saidi@ARM.com if isinstance(child, ptype) and not isproxy(child) and \ 8538459SAli.Saidi@ARM.com not isNullPointer(child): 8548459SAli.Saidi@ARM.com all[child] = True 8558459SAli.Saidi@ARM.com # search param space 8568459SAli.Saidi@ARM.com for pname,pdesc in self._params.iteritems(): 8578459SAli.Saidi@ARM.com if issubclass(pdesc.ptype, ptype): 8588459SAli.Saidi@ARM.com match_obj = self._values[pname] 8598459SAli.Saidi@ARM.com if not isproxy(match_obj) and not isNullPointer(match_obj): 8608459SAli.Saidi@ARM.com all[match_obj] = True 8618459SAli.Saidi@ARM.com return all.keys(), True 8628459SAli.Saidi@ARM.com 8631815SN/A def unproxy(self, base): 8641815SN/A return self 8651815SN/A 8667527Ssteve.reinhardt@amd.com def unproxyParams(self): 8673105Sstever@eecs.umich.edu for param in self._params.iterkeys(): 8683105Sstever@eecs.umich.edu value = self._values.get(param) 8696654Snate@binkert.org if value != None and isproxy(value): 8703105Sstever@eecs.umich.edu try: 8713105Sstever@eecs.umich.edu value = value.unproxy(self) 8723105Sstever@eecs.umich.edu except: 8733105Sstever@eecs.umich.edu print "Error in unproxying param '%s' of %s" % \ 8743105Sstever@eecs.umich.edu (param, self.path()) 8753105Sstever@eecs.umich.edu raise 8763105Sstever@eecs.umich.edu setattr(self, param, value) 8773105Sstever@eecs.umich.edu 8783107Sstever@eecs.umich.edu # Unproxy ports in sorted order so that 'append' operations on 8793107Sstever@eecs.umich.edu # vector ports are done in a deterministic fashion. 8803107Sstever@eecs.umich.edu port_names = self._ports.keys() 8813107Sstever@eecs.umich.edu port_names.sort() 8823107Sstever@eecs.umich.edu for port_name in port_names: 8833105Sstever@eecs.umich.edu port = self._port_refs.get(port_name) 8843105Sstever@eecs.umich.edu if port != None: 8853105Sstever@eecs.umich.edu port.unproxy(self) 8863105Sstever@eecs.umich.edu 8875037Smilesck@eecs.umich.edu def print_ini(self, ini_file): 8885543Ssaidi@eecs.umich.edu print >>ini_file, '[' + self.path() + ']' # .ini section header 8891692SN/A 8902738SN/A instanceDict[self.path()] = self 8912738SN/A 8924081Sbinkertn@umich.edu if hasattr(self, 'type'): 8935037Smilesck@eecs.umich.edu print >>ini_file, 'type=%s' % self.type 8941692SN/A 8958664SAli.Saidi@ARM.com if len(self._children.keys()): 8967528Ssteve.reinhardt@amd.com print >>ini_file, 'children=%s' % \ 8978664SAli.Saidi@ARM.com ' '.join(self._children[n].get_name() \ 8988664SAli.Saidi@ARM.com for n in sorted(self._children.keys())) 8991692SN/A 9008664SAli.Saidi@ARM.com for param in sorted(self._params.keys()): 9013105Sstever@eecs.umich.edu value = self._values.get(param) 9021692SN/A if value != None: 9035037Smilesck@eecs.umich.edu print >>ini_file, '%s=%s' % (param, 9045037Smilesck@eecs.umich.edu self._values[param].ini_str()) 9051692SN/A 9068664SAli.Saidi@ARM.com for port_name in sorted(self._ports.keys()): 9073105Sstever@eecs.umich.edu port = self._port_refs.get(port_name, None) 9083105Sstever@eecs.umich.edu if port != None: 9095037Smilesck@eecs.umich.edu print >>ini_file, '%s=%s' % (port_name, port.ini_str()) 9103103Sstever@eecs.umich.edu 9115543Ssaidi@eecs.umich.edu print >>ini_file # blank line between objects 9121692SN/A 9138664SAli.Saidi@ARM.com # generate a tree of dictionaries expressing all the parameters in the 9148664SAli.Saidi@ARM.com # instantiated system for use by scripts that want to do power, thermal 9158664SAli.Saidi@ARM.com # visualization, and other similar tasks 9168664SAli.Saidi@ARM.com def get_config_as_dict(self): 9178664SAli.Saidi@ARM.com d = attrdict() 9188664SAli.Saidi@ARM.com if hasattr(self, 'type'): 9198664SAli.Saidi@ARM.com d.type = self.type 9208664SAli.Saidi@ARM.com if hasattr(self, 'cxx_class'): 9218664SAli.Saidi@ARM.com d.cxx_class = self.cxx_class 9228664SAli.Saidi@ARM.com 9238664SAli.Saidi@ARM.com for param in sorted(self._params.keys()): 9248664SAli.Saidi@ARM.com value = self._values.get(param) 9258664SAli.Saidi@ARM.com try: 9268669Ssaidi@eecs.umich.edu # Use native type for those supported by JSON and 9278669Ssaidi@eecs.umich.edu # strings for everything else. skipkeys=True seems 9288669Ssaidi@eecs.umich.edu # to not work as well as one would hope 9298669Ssaidi@eecs.umich.edu if type(self._values[param].value) in \ 9308669Ssaidi@eecs.umich.edu [str, unicode, int, long, float, bool, None]: 9318669Ssaidi@eecs.umich.edu d[param] = self._values[param].value 9328669Ssaidi@eecs.umich.edu else: 9338669Ssaidi@eecs.umich.edu d[param] = str(self._values[param]) 9348669Ssaidi@eecs.umich.edu 9358664SAli.Saidi@ARM.com except AttributeError: 9368664SAli.Saidi@ARM.com pass 9378664SAli.Saidi@ARM.com 9388664SAli.Saidi@ARM.com for n in sorted(self._children.keys()): 9398664SAli.Saidi@ARM.com d[self._children[n].get_name()] = self._children[n].get_config_as_dict() 9408664SAli.Saidi@ARM.com 9418664SAli.Saidi@ARM.com for port_name in sorted(self._ports.keys()): 9428664SAli.Saidi@ARM.com port = self._port_refs.get(port_name, None) 9438664SAli.Saidi@ARM.com if port != None: 9448664SAli.Saidi@ARM.com # Might want to actually make this reference the object 9458664SAli.Saidi@ARM.com # in the future, although execing the string problem would 9468664SAli.Saidi@ARM.com # get some of the way there 9478664SAli.Saidi@ARM.com d[port_name] = port.ini_str() 9488664SAli.Saidi@ARM.com 9498664SAli.Saidi@ARM.com return d 9508664SAli.Saidi@ARM.com 9514762Snate@binkert.org def getCCParams(self): 9524762Snate@binkert.org if self._ccParams: 9534762Snate@binkert.org return self._ccParams 9544762Snate@binkert.org 9557677Snate@binkert.org cc_params_struct = getattr(m5.internal.params, '%sParams' % self.type) 9564762Snate@binkert.org cc_params = cc_params_struct() 9575488Snate@binkert.org cc_params.pyobj = self 9584762Snate@binkert.org cc_params.name = str(self) 9594762Snate@binkert.org 9604762Snate@binkert.org param_names = self._params.keys() 9614762Snate@binkert.org param_names.sort() 9624762Snate@binkert.org for param in param_names: 9634762Snate@binkert.org value = self._values.get(param) 9644762Snate@binkert.org if value is None: 9656654Snate@binkert.org fatal("%s.%s without default or user set value", 9666654Snate@binkert.org self.path(), param) 9674762Snate@binkert.org 9684762Snate@binkert.org value = value.getValue() 9694762Snate@binkert.org if isinstance(self._params[param], VectorParamDesc): 9704762Snate@binkert.org assert isinstance(value, list) 9714762Snate@binkert.org vec = getattr(cc_params, param) 9724762Snate@binkert.org assert not len(vec) 9734762Snate@binkert.org for v in value: 9744762Snate@binkert.org vec.append(v) 9754762Snate@binkert.org else: 9764762Snate@binkert.org setattr(cc_params, param, value) 9774762Snate@binkert.org 9784762Snate@binkert.org port_names = self._ports.keys() 9794762Snate@binkert.org port_names.sort() 9804762Snate@binkert.org for port_name in port_names: 9814762Snate@binkert.org port = self._port_refs.get(port_name, None) 9824762Snate@binkert.org if port != None: 9838840Sandreas.hansson@arm.com setattr(cc_params, 'port_' + port_name + '_connection_count', 9848840Sandreas.hansson@arm.com len(port)) 9854762Snate@binkert.org self._ccParams = cc_params 9864762Snate@binkert.org return self._ccParams 9872738SN/A 9882740SN/A # Get C++ object corresponding to this object, calling C++ if 9892740SN/A # necessary to construct it. Does *not* recursively create 9902740SN/A # children. 9912740SN/A def getCCObject(self): 9922740SN/A if not self._ccObject: 9937526Ssteve.reinhardt@amd.com # Make sure this object is in the configuration hierarchy 9947526Ssteve.reinhardt@amd.com if not self._parent and not isRoot(self): 9957526Ssteve.reinhardt@amd.com raise RuntimeError, "Attempt to instantiate orphan node" 9967526Ssteve.reinhardt@amd.com # Cycles in the configuration hierarchy are not supported. This 9975244Sgblack@eecs.umich.edu # will catch the resulting recursion and stop. 9985244Sgblack@eecs.umich.edu self._ccObject = -1 9995244Sgblack@eecs.umich.edu params = self.getCCParams() 10004762Snate@binkert.org self._ccObject = params.create() 10012740SN/A elif self._ccObject == -1: 10027526Ssteve.reinhardt@amd.com raise RuntimeError, "%s: Cycle found in configuration hierarchy." \ 10032740SN/A % self.path() 10042740SN/A return self._ccObject 10052740SN/A 10067527Ssteve.reinhardt@amd.com def descendants(self): 10077527Ssteve.reinhardt@amd.com yield self 10087527Ssteve.reinhardt@amd.com for child in self._children.itervalues(): 10097527Ssteve.reinhardt@amd.com for obj in child.descendants(): 10107527Ssteve.reinhardt@amd.com yield obj 10117527Ssteve.reinhardt@amd.com 10127527Ssteve.reinhardt@amd.com # Call C++ to create C++ object corresponding to this object 10134762Snate@binkert.org def createCCObject(self): 10144762Snate@binkert.org self.getCCParams() 10154762Snate@binkert.org self.getCCObject() # force creation 10164762Snate@binkert.org 10174762Snate@binkert.org def getValue(self): 10184762Snate@binkert.org return self.getCCObject() 10194762Snate@binkert.org 10202738SN/A # Create C++ port connections corresponding to the connections in 10217527Ssteve.reinhardt@amd.com # _port_refs 10222738SN/A def connectPorts(self): 10233105Sstever@eecs.umich.edu for portRef in self._port_refs.itervalues(): 10243105Sstever@eecs.umich.edu portRef.ccConnect() 10252797SN/A 10264553Sbinkertn@umich.edu def getMemoryMode(self): 10274553Sbinkertn@umich.edu if not isinstance(self, m5.objects.System): 10284553Sbinkertn@umich.edu return None 10294553Sbinkertn@umich.edu 10304859Snate@binkert.org return self._ccObject.getMemoryMode() 10314553Sbinkertn@umich.edu 10322797SN/A def changeTiming(self, mode): 10333202Shsul@eecs.umich.edu if isinstance(self, m5.objects.System): 10343202Shsul@eecs.umich.edu # i don't know if there's a better way to do this - calling 10353202Shsul@eecs.umich.edu # setMemoryMode directly from self._ccObject results in calling 10363202Shsul@eecs.umich.edu # SimObject::setMemoryMode, not the System::setMemoryMode 10374859Snate@binkert.org self._ccObject.setMemoryMode(mode) 10382797SN/A 10392797SN/A def takeOverFrom(self, old_cpu): 10404859Snate@binkert.org self._ccObject.takeOverFrom(old_cpu._ccObject) 10412797SN/A 10421692SN/A # generate output file for 'dot' to display as a pretty graph. 10431692SN/A # this code is currently broken. 10441342SN/A def outputDot(self, dot): 10451342SN/A label = "{%s|" % self.path 10461342SN/A if isSimObject(self.realtype): 10471342SN/A label += '%s|' % self.type 10481342SN/A 10491342SN/A if self.children: 10501342SN/A # instantiate children in same order they were added for 10511342SN/A # backward compatibility (else we can end up with cpu1 10521342SN/A # before cpu0). 10531342SN/A for c in self.children: 10541342SN/A dot.add_edge(pydot.Edge(self.path,c.path, style="bold")) 10551342SN/A 10561342SN/A simobjs = [] 10571342SN/A for param in self.params: 10581342SN/A try: 10591342SN/A if param.value is None: 10601342SN/A raise AttributeError, 'Parameter with no value' 10611342SN/A 10621692SN/A value = param.value 10631342SN/A string = param.string(value) 10641587SN/A except Exception, e: 10651605SN/A msg = 'exception in %s:%s\n%s' % (self.name, param.name, e) 10661605SN/A e.args = (msg, ) 10671342SN/A raise 10681605SN/A 10691692SN/A if isSimObject(param.ptype) and string != "Null": 10701342SN/A simobjs.append(string) 10711342SN/A else: 10721342SN/A label += '%s = %s\\n' % (param.name, string) 10731342SN/A 10741342SN/A for so in simobjs: 10751342SN/A label += "|<%s> %s" % (so, so) 10761587SN/A dot.add_edge(pydot.Edge("%s:%s" % (self.path, so), so, 10771587SN/A tailport="w")) 10781342SN/A label += '}' 10791342SN/A dot.add_node(pydot.Node(self.path,shape="Mrecord",label=label)) 10801342SN/A 10811342SN/A # recursively dump out children 10821342SN/A for c in self.children: 10831342SN/A c.outputDot(dot) 10841342SN/A 10853101Sstever@eecs.umich.edu# Function to provide to C++ so it can look up instances based on paths 10863101Sstever@eecs.umich.edudef resolveSimObject(name): 10873101Sstever@eecs.umich.edu obj = instanceDict[name] 10883101Sstever@eecs.umich.edu return obj.getCCObject() 1089679SN/A 10906654Snate@binkert.orgdef isSimObject(value): 10916654Snate@binkert.org return isinstance(value, SimObject) 10926654Snate@binkert.org 10936654Snate@binkert.orgdef isSimObjectClass(value): 10946654Snate@binkert.org return issubclass(value, SimObject) 10956654Snate@binkert.org 10967528Ssteve.reinhardt@amd.comdef isSimObjectVector(value): 10977528Ssteve.reinhardt@amd.com return isinstance(value, SimObjectVector) 10987528Ssteve.reinhardt@amd.com 10996654Snate@binkert.orgdef isSimObjectSequence(value): 11006654Snate@binkert.org if not isinstance(value, (list, tuple)) or len(value) == 0: 11016654Snate@binkert.org return False 11026654Snate@binkert.org 11036654Snate@binkert.org for val in value: 11046654Snate@binkert.org if not isNullPointer(val) and not isSimObject(val): 11056654Snate@binkert.org return False 11066654Snate@binkert.org 11076654Snate@binkert.org return True 11086654Snate@binkert.org 11096654Snate@binkert.orgdef isSimObjectOrSequence(value): 11106654Snate@binkert.org return isSimObject(value) or isSimObjectSequence(value) 11116654Snate@binkert.org 11127526Ssteve.reinhardt@amd.comdef isRoot(obj): 11137526Ssteve.reinhardt@amd.com from m5.objects import Root 11147526Ssteve.reinhardt@amd.com return obj and obj is Root.getInstance() 11157526Ssteve.reinhardt@amd.com 11167528Ssteve.reinhardt@amd.comdef isSimObjectOrVector(value): 11177528Ssteve.reinhardt@amd.com return isSimObject(value) or isSimObjectVector(value) 11187528Ssteve.reinhardt@amd.com 11197528Ssteve.reinhardt@amd.comdef tryAsSimObjectOrVector(value): 11207528Ssteve.reinhardt@amd.com if isSimObjectOrVector(value): 11217528Ssteve.reinhardt@amd.com return value 11227528Ssteve.reinhardt@amd.com if isSimObjectSequence(value): 11237528Ssteve.reinhardt@amd.com return SimObjectVector(value) 11247528Ssteve.reinhardt@amd.com return None 11257528Ssteve.reinhardt@amd.com 11267528Ssteve.reinhardt@amd.comdef coerceSimObjectOrVector(value): 11277528Ssteve.reinhardt@amd.com value = tryAsSimObjectOrVector(value) 11287528Ssteve.reinhardt@amd.com if value is None: 11297528Ssteve.reinhardt@amd.com raise TypeError, "SimObject or SimObjectVector expected" 11307528Ssteve.reinhardt@amd.com return value 11317528Ssteve.reinhardt@amd.com 11326654Snate@binkert.orgbaseClasses = allClasses.copy() 11336654Snate@binkert.orgbaseInstances = instanceDict.copy() 11346654Snate@binkert.org 11356654Snate@binkert.orgdef clear(): 11366654Snate@binkert.org global allClasses, instanceDict 11376654Snate@binkert.org 11386654Snate@binkert.org allClasses = baseClasses.copy() 11396654Snate@binkert.org instanceDict = baseInstances.copy() 11406654Snate@binkert.org 11411528SN/A# __all__ defines the list of symbols that get exported when 11421528SN/A# 'from config import *' is invoked. Try to keep this reasonably 11431528SN/A# short to avoid polluting other namespaces. 11444762Snate@binkert.org__all__ = [ 'SimObject' ] 1145