SimObject.py revision 10532
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 11710458Sandreas.hansson@arm.comdef createCxxConfigDirectoryEntryFile(code, name, simobj, is_header): 11810458Sandreas.hansson@arm.com entry_class = 'CxxConfigDirectoryEntry_%s' % name 11910458Sandreas.hansson@arm.com param_class = '%sCxxConfigParams' % name 12010458Sandreas.hansson@arm.com 12110458Sandreas.hansson@arm.com code('#include "params/%s.hh"' % name) 12210458Sandreas.hansson@arm.com 12310458Sandreas.hansson@arm.com if not is_header: 12410458Sandreas.hansson@arm.com for param in simobj._params.values(): 12510458Sandreas.hansson@arm.com if isSimObjectClass(param.ptype): 12610458Sandreas.hansson@arm.com code('#include "%s"' % param.ptype._value_dict['cxx_header']) 12710458Sandreas.hansson@arm.com code('#include "params/%s.hh"' % param.ptype.__name__) 12810458Sandreas.hansson@arm.com else: 12910458Sandreas.hansson@arm.com param.ptype.cxx_ini_predecls(code) 13010458Sandreas.hansson@arm.com 13110458Sandreas.hansson@arm.com if is_header: 13210458Sandreas.hansson@arm.com member_prefix = '' 13310458Sandreas.hansson@arm.com end_of_decl = ';' 13410458Sandreas.hansson@arm.com code('#include "sim/cxx_config.hh"') 13510458Sandreas.hansson@arm.com code() 13610458Sandreas.hansson@arm.com code('class ${param_class} : public CxxConfigParams,' 13710458Sandreas.hansson@arm.com ' public ${name}Params') 13810458Sandreas.hansson@arm.com code('{') 13910458Sandreas.hansson@arm.com code(' private:') 14010458Sandreas.hansson@arm.com code.indent() 14110458Sandreas.hansson@arm.com code('class DirectoryEntry : public CxxConfigDirectoryEntry') 14210458Sandreas.hansson@arm.com code('{') 14310458Sandreas.hansson@arm.com code(' public:') 14410458Sandreas.hansson@arm.com code.indent() 14510458Sandreas.hansson@arm.com code('DirectoryEntry();'); 14610458Sandreas.hansson@arm.com code() 14710458Sandreas.hansson@arm.com code('CxxConfigParams *makeParamsObject() const') 14810458Sandreas.hansson@arm.com code('{ return new ${param_class}; }') 14910458Sandreas.hansson@arm.com code.dedent() 15010458Sandreas.hansson@arm.com code('};') 15110458Sandreas.hansson@arm.com code() 15210458Sandreas.hansson@arm.com code.dedent() 15310458Sandreas.hansson@arm.com code(' public:') 15410458Sandreas.hansson@arm.com code.indent() 15510458Sandreas.hansson@arm.com else: 15610458Sandreas.hansson@arm.com member_prefix = '%s::' % param_class 15710458Sandreas.hansson@arm.com end_of_decl = '' 15810458Sandreas.hansson@arm.com code('#include "%s"' % simobj._value_dict['cxx_header']) 15910458Sandreas.hansson@arm.com code('#include "base/str.hh"') 16010458Sandreas.hansson@arm.com code('#include "cxx_config/${name}.hh"') 16110458Sandreas.hansson@arm.com 16210458Sandreas.hansson@arm.com if simobj._ports.values() != []: 16310458Sandreas.hansson@arm.com code('#include "mem/mem_object.hh"') 16410458Sandreas.hansson@arm.com code('#include "mem/port.hh"') 16510458Sandreas.hansson@arm.com 16610458Sandreas.hansson@arm.com code() 16710458Sandreas.hansson@arm.com code('${member_prefix}DirectoryEntry::DirectoryEntry()'); 16810458Sandreas.hansson@arm.com code('{') 16910458Sandreas.hansson@arm.com 17010458Sandreas.hansson@arm.com def cxx_bool(b): 17110458Sandreas.hansson@arm.com return 'true' if b else 'false' 17210458Sandreas.hansson@arm.com 17310458Sandreas.hansson@arm.com code.indent() 17410458Sandreas.hansson@arm.com for param in simobj._params.values(): 17510458Sandreas.hansson@arm.com is_vector = isinstance(param, m5.params.VectorParamDesc) 17610458Sandreas.hansson@arm.com is_simobj = issubclass(param.ptype, m5.SimObject.SimObject) 17710458Sandreas.hansson@arm.com 17810458Sandreas.hansson@arm.com code('parameters["%s"] = new ParamDesc("%s", %s, %s);' % 17910458Sandreas.hansson@arm.com (param.name, param.name, cxx_bool(is_vector), 18010458Sandreas.hansson@arm.com cxx_bool(is_simobj))); 18110458Sandreas.hansson@arm.com 18210458Sandreas.hansson@arm.com for port in simobj._ports.values(): 18310458Sandreas.hansson@arm.com is_vector = isinstance(port, m5.params.VectorPort) 18410458Sandreas.hansson@arm.com is_master = port.role == 'MASTER' 18510458Sandreas.hansson@arm.com 18610458Sandreas.hansson@arm.com code('ports["%s"] = new PortDesc("%s", %s, %s);' % 18710458Sandreas.hansson@arm.com (port.name, port.name, cxx_bool(is_vector), 18810458Sandreas.hansson@arm.com cxx_bool(is_master))) 18910458Sandreas.hansson@arm.com 19010458Sandreas.hansson@arm.com code.dedent() 19110458Sandreas.hansson@arm.com code('}') 19210458Sandreas.hansson@arm.com code() 19310458Sandreas.hansson@arm.com 19410458Sandreas.hansson@arm.com code('bool ${member_prefix}setSimObject(const std::string &name,') 19510458Sandreas.hansson@arm.com code(' SimObject *simObject)${end_of_decl}') 19610458Sandreas.hansson@arm.com 19710458Sandreas.hansson@arm.com if not is_header: 19810458Sandreas.hansson@arm.com code('{') 19910458Sandreas.hansson@arm.com code.indent() 20010458Sandreas.hansson@arm.com code('bool ret = true;') 20110458Sandreas.hansson@arm.com code() 20210458Sandreas.hansson@arm.com code('if (false) {') 20310458Sandreas.hansson@arm.com for param in simobj._params.values(): 20410458Sandreas.hansson@arm.com is_vector = isinstance(param, m5.params.VectorParamDesc) 20510458Sandreas.hansson@arm.com is_simobj = issubclass(param.ptype, m5.SimObject.SimObject) 20610458Sandreas.hansson@arm.com 20710458Sandreas.hansson@arm.com if is_simobj and not is_vector: 20810458Sandreas.hansson@arm.com code('} else if (name == "${{param.name}}") {') 20910458Sandreas.hansson@arm.com code.indent() 21010458Sandreas.hansson@arm.com code('this->${{param.name}} = ' 21110458Sandreas.hansson@arm.com 'dynamic_cast<${{param.ptype.cxx_type}}>(simObject);') 21210458Sandreas.hansson@arm.com code('if (simObject && !this->${{param.name}})') 21310458Sandreas.hansson@arm.com code(' ret = false;') 21410458Sandreas.hansson@arm.com code.dedent() 21510458Sandreas.hansson@arm.com code('} else {') 21610458Sandreas.hansson@arm.com code(' ret = false;') 21710458Sandreas.hansson@arm.com code('}') 21810458Sandreas.hansson@arm.com code() 21910458Sandreas.hansson@arm.com code('return ret;') 22010458Sandreas.hansson@arm.com code.dedent() 22110458Sandreas.hansson@arm.com code('}') 22210458Sandreas.hansson@arm.com 22310458Sandreas.hansson@arm.com code() 22410458Sandreas.hansson@arm.com code('bool ${member_prefix}setSimObjectVector(' 22510458Sandreas.hansson@arm.com 'const std::string &name,') 22610458Sandreas.hansson@arm.com code(' const std::vector<SimObject *> &simObjects)${end_of_decl}') 22710458Sandreas.hansson@arm.com 22810458Sandreas.hansson@arm.com if not is_header: 22910458Sandreas.hansson@arm.com code('{') 23010458Sandreas.hansson@arm.com code.indent() 23110458Sandreas.hansson@arm.com code('bool ret = true;') 23210458Sandreas.hansson@arm.com code() 23310458Sandreas.hansson@arm.com code('if (false) {') 23410458Sandreas.hansson@arm.com for param in simobj._params.values(): 23510458Sandreas.hansson@arm.com is_vector = isinstance(param, m5.params.VectorParamDesc) 23610458Sandreas.hansson@arm.com is_simobj = issubclass(param.ptype, m5.SimObject.SimObject) 23710458Sandreas.hansson@arm.com 23810458Sandreas.hansson@arm.com if is_simobj and is_vector: 23910458Sandreas.hansson@arm.com code('} else if (name == "${{param.name}}") {') 24010458Sandreas.hansson@arm.com code.indent() 24110458Sandreas.hansson@arm.com code('this->${{param.name}}.clear();') 24210458Sandreas.hansson@arm.com code('for (auto i = simObjects.begin(); ' 24310458Sandreas.hansson@arm.com 'ret && i != simObjects.end(); i ++)') 24410458Sandreas.hansson@arm.com code('{') 24510458Sandreas.hansson@arm.com code.indent() 24610458Sandreas.hansson@arm.com code('${{param.ptype.cxx_type}} object = ' 24710458Sandreas.hansson@arm.com 'dynamic_cast<${{param.ptype.cxx_type}}>(*i);') 24810458Sandreas.hansson@arm.com code('if (*i && !object)') 24910458Sandreas.hansson@arm.com code(' ret = false;') 25010458Sandreas.hansson@arm.com code('else') 25110458Sandreas.hansson@arm.com code(' this->${{param.name}}.push_back(object);') 25210458Sandreas.hansson@arm.com code.dedent() 25310458Sandreas.hansson@arm.com code('}') 25410458Sandreas.hansson@arm.com code.dedent() 25510458Sandreas.hansson@arm.com code('} else {') 25610458Sandreas.hansson@arm.com code(' ret = false;') 25710458Sandreas.hansson@arm.com code('}') 25810458Sandreas.hansson@arm.com code() 25910458Sandreas.hansson@arm.com code('return ret;') 26010458Sandreas.hansson@arm.com code.dedent() 26110458Sandreas.hansson@arm.com code('}') 26210458Sandreas.hansson@arm.com 26310458Sandreas.hansson@arm.com code() 26410458Sandreas.hansson@arm.com code('void ${member_prefix}setName(const std::string &name_)' 26510458Sandreas.hansson@arm.com '${end_of_decl}') 26610458Sandreas.hansson@arm.com 26710458Sandreas.hansson@arm.com if not is_header: 26810458Sandreas.hansson@arm.com code('{') 26910458Sandreas.hansson@arm.com code.indent() 27010458Sandreas.hansson@arm.com code('this->name = name_;') 27110458Sandreas.hansson@arm.com code('this->pyobj = NULL;') 27210458Sandreas.hansson@arm.com code.dedent() 27310458Sandreas.hansson@arm.com code('}') 27410458Sandreas.hansson@arm.com 27510458Sandreas.hansson@arm.com if is_header: 27610458Sandreas.hansson@arm.com code('const std::string &${member_prefix}getName()') 27710458Sandreas.hansson@arm.com code('{ return this->name; }') 27810458Sandreas.hansson@arm.com 27910458Sandreas.hansson@arm.com code() 28010458Sandreas.hansson@arm.com code('bool ${member_prefix}setParam(const std::string &name,') 28110458Sandreas.hansson@arm.com code(' const std::string &value, const Flags flags)${end_of_decl}') 28210458Sandreas.hansson@arm.com 28310458Sandreas.hansson@arm.com if not is_header: 28410458Sandreas.hansson@arm.com code('{') 28510458Sandreas.hansson@arm.com code.indent() 28610458Sandreas.hansson@arm.com code('bool ret = true;') 28710458Sandreas.hansson@arm.com code() 28810458Sandreas.hansson@arm.com code('if (false) {') 28910458Sandreas.hansson@arm.com for param in simobj._params.values(): 29010458Sandreas.hansson@arm.com is_vector = isinstance(param, m5.params.VectorParamDesc) 29110458Sandreas.hansson@arm.com is_simobj = issubclass(param.ptype, m5.SimObject.SimObject) 29210458Sandreas.hansson@arm.com 29310458Sandreas.hansson@arm.com if not is_simobj and not is_vector: 29410458Sandreas.hansson@arm.com code('} else if (name == "${{param.name}}") {') 29510458Sandreas.hansson@arm.com code.indent() 29610458Sandreas.hansson@arm.com param.ptype.cxx_ini_parse(code, 29710458Sandreas.hansson@arm.com 'value', 'this->%s' % param.name, 'ret =') 29810458Sandreas.hansson@arm.com code.dedent() 29910458Sandreas.hansson@arm.com code('} else {') 30010458Sandreas.hansson@arm.com code(' ret = false;') 30110458Sandreas.hansson@arm.com code('}') 30210458Sandreas.hansson@arm.com code() 30310458Sandreas.hansson@arm.com code('return ret;') 30410458Sandreas.hansson@arm.com code.dedent() 30510458Sandreas.hansson@arm.com code('}') 30610458Sandreas.hansson@arm.com 30710458Sandreas.hansson@arm.com code() 30810458Sandreas.hansson@arm.com code('bool ${member_prefix}setParamVector(' 30910458Sandreas.hansson@arm.com 'const std::string &name,') 31010458Sandreas.hansson@arm.com code(' const std::vector<std::string> &values,') 31110458Sandreas.hansson@arm.com code(' const Flags flags)${end_of_decl}') 31210458Sandreas.hansson@arm.com 31310458Sandreas.hansson@arm.com if not is_header: 31410458Sandreas.hansson@arm.com code('{') 31510458Sandreas.hansson@arm.com code.indent() 31610458Sandreas.hansson@arm.com code('bool ret = true;') 31710458Sandreas.hansson@arm.com code() 31810458Sandreas.hansson@arm.com code('if (false) {') 31910458Sandreas.hansson@arm.com for param in simobj._params.values(): 32010458Sandreas.hansson@arm.com is_vector = isinstance(param, m5.params.VectorParamDesc) 32110458Sandreas.hansson@arm.com is_simobj = issubclass(param.ptype, m5.SimObject.SimObject) 32210458Sandreas.hansson@arm.com 32310458Sandreas.hansson@arm.com if not is_simobj and is_vector: 32410458Sandreas.hansson@arm.com code('} else if (name == "${{param.name}}") {') 32510458Sandreas.hansson@arm.com code.indent() 32610458Sandreas.hansson@arm.com code('${{param.name}}.clear();') 32710458Sandreas.hansson@arm.com code('for (auto i = values.begin(); ' 32810458Sandreas.hansson@arm.com 'ret && i != values.end(); i ++)') 32910458Sandreas.hansson@arm.com code('{') 33010458Sandreas.hansson@arm.com code.indent() 33110458Sandreas.hansson@arm.com code('${{param.ptype.cxx_type}} elem;') 33210458Sandreas.hansson@arm.com param.ptype.cxx_ini_parse(code, 33310458Sandreas.hansson@arm.com '*i', 'elem', 'ret =') 33410458Sandreas.hansson@arm.com code('if (ret)') 33510458Sandreas.hansson@arm.com code(' this->${{param.name}}.push_back(elem);') 33610458Sandreas.hansson@arm.com code.dedent() 33710458Sandreas.hansson@arm.com code('}') 33810458Sandreas.hansson@arm.com code.dedent() 33910458Sandreas.hansson@arm.com code('} else {') 34010458Sandreas.hansson@arm.com code(' ret = false;') 34110458Sandreas.hansson@arm.com code('}') 34210458Sandreas.hansson@arm.com code() 34310458Sandreas.hansson@arm.com code('return ret;') 34410458Sandreas.hansson@arm.com code.dedent() 34510458Sandreas.hansson@arm.com code('}') 34610458Sandreas.hansson@arm.com 34710458Sandreas.hansson@arm.com code() 34810458Sandreas.hansson@arm.com code('bool ${member_prefix}setPortConnectionCount(' 34910458Sandreas.hansson@arm.com 'const std::string &name,') 35010458Sandreas.hansson@arm.com code(' unsigned int count)${end_of_decl}') 35110458Sandreas.hansson@arm.com 35210458Sandreas.hansson@arm.com if not is_header: 35310458Sandreas.hansson@arm.com code('{') 35410458Sandreas.hansson@arm.com code.indent() 35510458Sandreas.hansson@arm.com code('bool ret = true;') 35610458Sandreas.hansson@arm.com code() 35710458Sandreas.hansson@arm.com code('if (false)') 35810458Sandreas.hansson@arm.com code(' ;') 35910458Sandreas.hansson@arm.com for port in simobj._ports.values(): 36010458Sandreas.hansson@arm.com code('else if (name == "${{port.name}}")') 36110458Sandreas.hansson@arm.com code(' this->port_${{port.name}}_connection_count = count;') 36210458Sandreas.hansson@arm.com code('else') 36310458Sandreas.hansson@arm.com code(' ret = false;') 36410458Sandreas.hansson@arm.com code() 36510458Sandreas.hansson@arm.com code('return ret;') 36610458Sandreas.hansson@arm.com code.dedent() 36710458Sandreas.hansson@arm.com code('}') 36810458Sandreas.hansson@arm.com 36910458Sandreas.hansson@arm.com code() 37010458Sandreas.hansson@arm.com code('SimObject *${member_prefix}simObjectCreate()${end_of_decl}') 37110458Sandreas.hansson@arm.com 37210458Sandreas.hansson@arm.com if not is_header: 37310458Sandreas.hansson@arm.com code('{') 37410458Sandreas.hansson@arm.com if hasattr(simobj, 'abstract') and simobj.abstract: 37510458Sandreas.hansson@arm.com code(' return NULL;') 37610458Sandreas.hansson@arm.com else: 37710458Sandreas.hansson@arm.com code(' return this->create();') 37810458Sandreas.hansson@arm.com code('}') 37910458Sandreas.hansson@arm.com 38010458Sandreas.hansson@arm.com if is_header: 38110458Sandreas.hansson@arm.com code() 38210458Sandreas.hansson@arm.com code('static CxxConfigDirectoryEntry' 38310458Sandreas.hansson@arm.com ' *${member_prefix}makeDirectoryEntry()') 38410458Sandreas.hansson@arm.com code('{ return new DirectoryEntry; }') 38510458Sandreas.hansson@arm.com 38610458Sandreas.hansson@arm.com if is_header: 38710458Sandreas.hansson@arm.com code.dedent() 38810458Sandreas.hansson@arm.com code('};') 38910458Sandreas.hansson@arm.com 3902740SN/A# The metaclass for SimObject. This class controls how new classes 3912740SN/A# that derive from SimObject are instantiated, and provides inherited 3922740SN/A# class behavior (just like a class controls how instances of that 3932740SN/A# class are instantiated, and provides inherited instance behavior). 3941692SN/Aclass MetaSimObject(type): 3951427SN/A # Attributes that can be set only at initialization time 3967493Ssteve.reinhardt@amd.com init_keywords = { 'abstract' : bool, 3977493Ssteve.reinhardt@amd.com 'cxx_class' : str, 3987493Ssteve.reinhardt@amd.com 'cxx_type' : str, 3999338SAndreas.Sandberg@arm.com 'cxx_header' : str, 4009342SAndreas.Sandberg@arm.com 'type' : str, 4019342SAndreas.Sandberg@arm.com 'cxx_bases' : list } 4021427SN/A # Attributes that can be set any time 4037493Ssteve.reinhardt@amd.com keywords = { 'check' : FunctionType } 404679SN/A 405679SN/A # __new__ is called before __init__, and is where the statements 406679SN/A # in the body of the class definition get loaded into the class's 4072740SN/A # __dict__. We intercept this to filter out parameter & port assignments 408679SN/A # and only allow "private" attributes to be passed to the base 409679SN/A # __new__ (starting with underscore). 4101310SN/A def __new__(mcls, name, bases, dict): 4116654Snate@binkert.org assert name not in allClasses, "SimObject %s already present" % name 4124762Snate@binkert.org 4132740SN/A # Copy "private" attributes, functions, and classes to the 4142740SN/A # official dict. Everything else goes in _init_dict to be 4152740SN/A # filtered in __init__. 4162740SN/A cls_dict = {} 4172740SN/A value_dict = {} 4182740SN/A for key,val in dict.items(): 4197673Snate@binkert.org if public_value(key, val): 4202740SN/A cls_dict[key] = val 4212740SN/A else: 4222740SN/A # must be a param/port setting 4232740SN/A value_dict[key] = val 4244762Snate@binkert.org if 'abstract' not in value_dict: 4254762Snate@binkert.org value_dict['abstract'] = False 4269342SAndreas.Sandberg@arm.com if 'cxx_bases' not in value_dict: 4279342SAndreas.Sandberg@arm.com value_dict['cxx_bases'] = [] 4282740SN/A cls_dict['_value_dict'] = value_dict 4294762Snate@binkert.org cls = super(MetaSimObject, mcls).__new__(mcls, name, bases, cls_dict) 4304762Snate@binkert.org if 'type' in value_dict: 4314762Snate@binkert.org allClasses[name] = cls 4324762Snate@binkert.org return cls 433679SN/A 4342711SN/A # subclass initialization 435679SN/A def __init__(cls, name, bases, dict): 4362711SN/A # calls type.__init__()... I think that's a no-op, but leave 4372711SN/A # it here just in case it's not. 4381692SN/A super(MetaSimObject, cls).__init__(name, bases, dict) 4391310SN/A 4401427SN/A # initialize required attributes 4412740SN/A 4422740SN/A # class-only attributes 4432740SN/A cls._params = multidict() # param descriptions 4442740SN/A cls._ports = multidict() # port descriptions 4452740SN/A 4462740SN/A # class or instance attributes 4472740SN/A cls._values = multidict() # param values 44810267SGeoffrey.Blake@arm.com cls._hr_values = multidict() # human readable param values 4497528Ssteve.reinhardt@amd.com cls._children = multidict() # SimObject children 4503105Sstever@eecs.umich.edu cls._port_refs = multidict() # port ref objects 4512740SN/A cls._instantiated = False # really instantiated, cloned, or subclassed 4521310SN/A 4539100SBrad.Beckmann@amd.com # We don't support multiple inheritance of sim objects. If you want 4549100SBrad.Beckmann@amd.com # to, you must fix multidict to deal with it properly. Non sim-objects 4559100SBrad.Beckmann@amd.com # are ok, though 4569100SBrad.Beckmann@amd.com bTotal = 0 4579100SBrad.Beckmann@amd.com for c in bases: 4589100SBrad.Beckmann@amd.com if isinstance(c, MetaSimObject): 4599100SBrad.Beckmann@amd.com bTotal += 1 4609100SBrad.Beckmann@amd.com if bTotal > 1: 4619100SBrad.Beckmann@amd.com raise TypeError, "SimObjects do not support multiple inheritance" 4621692SN/A 4631692SN/A base = bases[0] 4641692SN/A 4652740SN/A # Set up general inheritance via multidicts. A subclass will 4662740SN/A # inherit all its settings from the base class. The only time 4672740SN/A # the following is not true is when we define the SimObject 4682740SN/A # class itself (in which case the multidicts have no parent). 4691692SN/A if isinstance(base, MetaSimObject): 4705610Snate@binkert.org cls._base = base 4711692SN/A cls._params.parent = base._params 4722740SN/A cls._ports.parent = base._ports 4731692SN/A cls._values.parent = base._values 47410267SGeoffrey.Blake@arm.com cls._hr_values.parent = base._hr_values 4757528Ssteve.reinhardt@amd.com cls._children.parent = base._children 4763105Sstever@eecs.umich.edu cls._port_refs.parent = base._port_refs 4772740SN/A # mark base as having been subclassed 4782712SN/A base._instantiated = True 4795610Snate@binkert.org else: 4805610Snate@binkert.org cls._base = None 4811692SN/A 4824762Snate@binkert.org # default keyword values 4834762Snate@binkert.org if 'type' in cls._value_dict: 4844762Snate@binkert.org if 'cxx_class' not in cls._value_dict: 4855610Snate@binkert.org cls._value_dict['cxx_class'] = cls._value_dict['type'] 4864762Snate@binkert.org 4875610Snate@binkert.org cls._value_dict['cxx_type'] = '%s *' % cls._value_dict['cxx_class'] 4884859Snate@binkert.org 4899338SAndreas.Sandberg@arm.com if 'cxx_header' not in cls._value_dict: 4909338SAndreas.Sandberg@arm.com global noCxxHeader 4919338SAndreas.Sandberg@arm.com noCxxHeader = True 4929528Ssascha.bischoff@arm.com warn("No header file specified for SimObject: %s", name) 4939338SAndreas.Sandberg@arm.com 4948597Ssteve.reinhardt@amd.com # Export methods are automatically inherited via C++, so we 4958597Ssteve.reinhardt@amd.com # don't want the method declarations to get inherited on the 4968597Ssteve.reinhardt@amd.com # python side (and thus end up getting repeated in the wrapped 4978597Ssteve.reinhardt@amd.com # versions of derived classes). The code below basicallly 4988597Ssteve.reinhardt@amd.com # suppresses inheritance by substituting in the base (null) 4998597Ssteve.reinhardt@amd.com # versions of these methods unless a different version is 5008597Ssteve.reinhardt@amd.com # explicitly supplied. 5018597Ssteve.reinhardt@amd.com for method_name in ('export_methods', 'export_method_cxx_predecls', 5028597Ssteve.reinhardt@amd.com 'export_method_swig_predecls'): 5038597Ssteve.reinhardt@amd.com if method_name not in cls.__dict__: 5048597Ssteve.reinhardt@amd.com base_method = getattr(MetaSimObject, method_name) 5058597Ssteve.reinhardt@amd.com m = MethodType(base_method, cls, MetaSimObject) 5068597Ssteve.reinhardt@amd.com setattr(cls, method_name, m) 5078597Ssteve.reinhardt@amd.com 5082740SN/A # Now process the _value_dict items. They could be defining 5092740SN/A # new (or overriding existing) parameters or ports, setting 5102740SN/A # class keywords (e.g., 'abstract'), or setting parameter 5112740SN/A # values or port bindings. The first 3 can only be set when 5122740SN/A # the class is defined, so we handle them here. The others 5132740SN/A # can be set later too, so just emulate that by calling 5142740SN/A # setattr(). 5152740SN/A for key,val in cls._value_dict.items(): 5161527SN/A # param descriptions 5172740SN/A if isinstance(val, ParamDesc): 5181585SN/A cls._new_param(key, val) 5191427SN/A 5202738SN/A # port objects 5212738SN/A elif isinstance(val, Port): 5223105Sstever@eecs.umich.edu cls._new_port(key, val) 5232738SN/A 5241427SN/A # init-time-only keywords 5251427SN/A elif cls.init_keywords.has_key(key): 5261427SN/A cls._set_keyword(key, val, cls.init_keywords[key]) 5271427SN/A 5281427SN/A # default: use normal path (ends up in __setattr__) 5291427SN/A else: 5301427SN/A setattr(cls, key, val) 5311427SN/A 5321427SN/A def _set_keyword(cls, keyword, val, kwtype): 5331427SN/A if not isinstance(val, kwtype): 5341427SN/A raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \ 5351427SN/A (keyword, type(val), kwtype) 5367493Ssteve.reinhardt@amd.com if isinstance(val, FunctionType): 5371427SN/A val = classmethod(val) 5381427SN/A type.__setattr__(cls, keyword, val) 5391427SN/A 5403100SN/A def _new_param(cls, name, pdesc): 5413100SN/A # each param desc should be uniquely assigned to one variable 5423100SN/A assert(not hasattr(pdesc, 'name')) 5433100SN/A pdesc.name = name 5443100SN/A cls._params[name] = pdesc 5453100SN/A if hasattr(pdesc, 'default'): 5463105Sstever@eecs.umich.edu cls._set_param(name, pdesc.default, pdesc) 5473105Sstever@eecs.umich.edu 5483105Sstever@eecs.umich.edu def _set_param(cls, name, value, param): 5493105Sstever@eecs.umich.edu assert(param.name == name) 5503105Sstever@eecs.umich.edu try: 55110267SGeoffrey.Blake@arm.com hr_value = value 5528321Ssteve.reinhardt@amd.com value = param.convert(value) 5533105Sstever@eecs.umich.edu except Exception, e: 5543105Sstever@eecs.umich.edu msg = "%s\nError setting param %s.%s to %s\n" % \ 5553105Sstever@eecs.umich.edu (e, cls.__name__, name, value) 5563105Sstever@eecs.umich.edu e.args = (msg, ) 5573105Sstever@eecs.umich.edu raise 5588321Ssteve.reinhardt@amd.com cls._values[name] = value 5598321Ssteve.reinhardt@amd.com # if param value is a SimObject, make it a child too, so that 5608321Ssteve.reinhardt@amd.com # it gets cloned properly when the class is instantiated 5618321Ssteve.reinhardt@amd.com if isSimObjectOrVector(value) and not value.has_parent(): 5628321Ssteve.reinhardt@amd.com cls._add_cls_child(name, value) 56310267SGeoffrey.Blake@arm.com # update human-readable values of the param if it has a literal 56410267SGeoffrey.Blake@arm.com # value and is not an object or proxy. 56510267SGeoffrey.Blake@arm.com if not (isSimObjectOrVector(value) or\ 56610267SGeoffrey.Blake@arm.com isinstance(value, m5.proxy.BaseProxy)): 56710267SGeoffrey.Blake@arm.com cls._hr_values[name] = hr_value 5688321Ssteve.reinhardt@amd.com 5698321Ssteve.reinhardt@amd.com def _add_cls_child(cls, name, child): 5708321Ssteve.reinhardt@amd.com # It's a little funky to have a class as a parent, but these 5718321Ssteve.reinhardt@amd.com # objects should never be instantiated (only cloned, which 5728321Ssteve.reinhardt@amd.com # clears the parent pointer), and this makes it clear that the 5738321Ssteve.reinhardt@amd.com # object is not an orphan and can provide better error 5748321Ssteve.reinhardt@amd.com # messages. 5758321Ssteve.reinhardt@amd.com child.set_parent(cls, name) 5768321Ssteve.reinhardt@amd.com cls._children[name] = child 5773105Sstever@eecs.umich.edu 5783105Sstever@eecs.umich.edu def _new_port(cls, name, port): 5793105Sstever@eecs.umich.edu # each port should be uniquely assigned to one variable 5803105Sstever@eecs.umich.edu assert(not hasattr(port, 'name')) 5813105Sstever@eecs.umich.edu port.name = name 5823105Sstever@eecs.umich.edu cls._ports[name] = port 5833105Sstever@eecs.umich.edu 5843105Sstever@eecs.umich.edu # same as _get_port_ref, effectively, but for classes 5853105Sstever@eecs.umich.edu def _cls_get_port_ref(cls, attr): 5863105Sstever@eecs.umich.edu # Return reference that can be assigned to another port 5873105Sstever@eecs.umich.edu # via __setattr__. There is only ever one reference 5883105Sstever@eecs.umich.edu # object per port, but we create them lazily here. 5893105Sstever@eecs.umich.edu ref = cls._port_refs.get(attr) 5903105Sstever@eecs.umich.edu if not ref: 5913105Sstever@eecs.umich.edu ref = cls._ports[attr].makeRef(cls) 5923105Sstever@eecs.umich.edu cls._port_refs[attr] = ref 5933105Sstever@eecs.umich.edu return ref 5941585SN/A 5951310SN/A # Set attribute (called on foo.attr = value when foo is an 5961310SN/A # instance of class cls). 5971310SN/A def __setattr__(cls, attr, value): 5981310SN/A # normal processing for private attributes 5997673Snate@binkert.org if public_value(attr, value): 6001310SN/A type.__setattr__(cls, attr, value) 6011310SN/A return 6021310SN/A 6031310SN/A if cls.keywords.has_key(attr): 6041427SN/A cls._set_keyword(attr, value, cls.keywords[attr]) 6051310SN/A return 6061310SN/A 6072738SN/A if cls._ports.has_key(attr): 6083105Sstever@eecs.umich.edu cls._cls_get_port_ref(attr).connect(value) 6092738SN/A return 6102738SN/A 6112740SN/A if isSimObjectOrSequence(value) and cls._instantiated: 6122740SN/A raise RuntimeError, \ 6132740SN/A "cannot set SimObject parameter '%s' after\n" \ 6142740SN/A " class %s has been instantiated or subclassed" \ 6152740SN/A % (attr, cls.__name__) 6162740SN/A 6172740SN/A # check for param 6183105Sstever@eecs.umich.edu param = cls._params.get(attr) 6191310SN/A if param: 6203105Sstever@eecs.umich.edu cls._set_param(attr, value, param) 6213105Sstever@eecs.umich.edu return 6223105Sstever@eecs.umich.edu 6233105Sstever@eecs.umich.edu if isSimObjectOrSequence(value): 6243105Sstever@eecs.umich.edu # If RHS is a SimObject, it's an implicit child assignment. 6258321Ssteve.reinhardt@amd.com cls._add_cls_child(attr, coerceSimObjectOrVector(value)) 6263105Sstever@eecs.umich.edu return 6273105Sstever@eecs.umich.edu 6283105Sstever@eecs.umich.edu # no valid assignment... raise exception 6293105Sstever@eecs.umich.edu raise AttributeError, \ 6303105Sstever@eecs.umich.edu "Class %s has no parameter \'%s\'" % (cls.__name__, attr) 6311310SN/A 6321585SN/A def __getattr__(cls, attr): 6337675Snate@binkert.org if attr == 'cxx_class_path': 6347675Snate@binkert.org return cls.cxx_class.split('::') 6357675Snate@binkert.org 6367675Snate@binkert.org if attr == 'cxx_class_name': 6377675Snate@binkert.org return cls.cxx_class_path[-1] 6387675Snate@binkert.org 6397675Snate@binkert.org if attr == 'cxx_namespaces': 6407675Snate@binkert.org return cls.cxx_class_path[:-1] 6417675Snate@binkert.org 6421692SN/A if cls._values.has_key(attr): 6431692SN/A return cls._values[attr] 6441585SN/A 6457528Ssteve.reinhardt@amd.com if cls._children.has_key(attr): 6467528Ssteve.reinhardt@amd.com return cls._children[attr] 6477528Ssteve.reinhardt@amd.com 6481585SN/A raise AttributeError, \ 6491585SN/A "object '%s' has no attribute '%s'" % (cls.__name__, attr) 6501585SN/A 6513100SN/A def __str__(cls): 6523100SN/A return cls.__name__ 6533100SN/A 6548596Ssteve.reinhardt@amd.com # See ParamValue.cxx_predecls for description. 6558596Ssteve.reinhardt@amd.com def cxx_predecls(cls, code): 6568596Ssteve.reinhardt@amd.com code('#include "params/$cls.hh"') 6578596Ssteve.reinhardt@amd.com 6588596Ssteve.reinhardt@amd.com # See ParamValue.swig_predecls for description. 6598596Ssteve.reinhardt@amd.com def swig_predecls(cls, code): 6608596Ssteve.reinhardt@amd.com code('%import "python/m5/internal/param_$cls.i"') 6618596Ssteve.reinhardt@amd.com 6628597Ssteve.reinhardt@amd.com # Hook for exporting additional C++ methods to Python via SWIG. 6638597Ssteve.reinhardt@amd.com # Default is none, override using @classmethod in class definition. 6648597Ssteve.reinhardt@amd.com def export_methods(cls, code): 6658597Ssteve.reinhardt@amd.com pass 6668597Ssteve.reinhardt@amd.com 6678597Ssteve.reinhardt@amd.com # Generate the code needed as a prerequisite for the C++ methods 6688597Ssteve.reinhardt@amd.com # exported via export_methods() to be compiled in the _wrap.cc 6698597Ssteve.reinhardt@amd.com # file. Typically generates one or more #include statements. If 6708597Ssteve.reinhardt@amd.com # any methods are exported, typically at least the C++ header 6718597Ssteve.reinhardt@amd.com # declaring the relevant SimObject class must be included. 6728597Ssteve.reinhardt@amd.com def export_method_cxx_predecls(cls, code): 6738597Ssteve.reinhardt@amd.com pass 6748597Ssteve.reinhardt@amd.com 6758597Ssteve.reinhardt@amd.com # Generate the code needed as a prerequisite for the C++ methods 6768597Ssteve.reinhardt@amd.com # exported via export_methods() to be processed by SWIG. 6778597Ssteve.reinhardt@amd.com # Typically generates one or more %include or %import statements. 6788597Ssteve.reinhardt@amd.com # If any methods are exported, typically at least the C++ header 6798597Ssteve.reinhardt@amd.com # declaring the relevant SimObject class must be included. 6808597Ssteve.reinhardt@amd.com def export_method_swig_predecls(cls, code): 6818597Ssteve.reinhardt@amd.com pass 6828597Ssteve.reinhardt@amd.com 6838596Ssteve.reinhardt@amd.com # Generate the declaration for this object for wrapping with SWIG. 6848596Ssteve.reinhardt@amd.com # Generates code that goes into a SWIG .i file. Called from 6858596Ssteve.reinhardt@amd.com # src/SConscript. 6868596Ssteve.reinhardt@amd.com def swig_decl(cls, code): 6878596Ssteve.reinhardt@amd.com class_path = cls.cxx_class.split('::') 6888596Ssteve.reinhardt@amd.com classname = class_path[-1] 6898596Ssteve.reinhardt@amd.com namespaces = class_path[:-1] 6908596Ssteve.reinhardt@amd.com 6918596Ssteve.reinhardt@amd.com # The 'local' attribute restricts us to the params declared in 6928596Ssteve.reinhardt@amd.com # the object itself, not including inherited params (which 6938596Ssteve.reinhardt@amd.com # will also be inherited from the base class's param struct 6948596Ssteve.reinhardt@amd.com # here). 6958596Ssteve.reinhardt@amd.com params = cls._params.local.values() 6968840Sandreas.hansson@arm.com ports = cls._ports.local 6978596Ssteve.reinhardt@amd.com 6988596Ssteve.reinhardt@amd.com code('%module(package="m5.internal") param_$cls') 6998596Ssteve.reinhardt@amd.com code() 7008596Ssteve.reinhardt@amd.com code('%{') 7019342SAndreas.Sandberg@arm.com code('#include "sim/sim_object.hh"') 7028596Ssteve.reinhardt@amd.com code('#include "params/$cls.hh"') 7038596Ssteve.reinhardt@amd.com for param in params: 7048596Ssteve.reinhardt@amd.com param.cxx_predecls(code) 7059338SAndreas.Sandberg@arm.com code('#include "${{cls.cxx_header}}"') 7068597Ssteve.reinhardt@amd.com cls.export_method_cxx_predecls(code) 7078860Sandreas.hansson@arm.com code('''\ 7088860Sandreas.hansson@arm.com/** 7098860Sandreas.hansson@arm.com * This is a workaround for bug in swig. Prior to gcc 4.6.1 the STL 7108860Sandreas.hansson@arm.com * headers like vector, string, etc. used to automatically pull in 7118860Sandreas.hansson@arm.com * the cstddef header but starting with gcc 4.6.1 they no longer do. 7128860Sandreas.hansson@arm.com * This leads to swig generated a file that does not compile so we 7138860Sandreas.hansson@arm.com * explicitly include cstddef. Additionally, including version 2.0.4, 7148860Sandreas.hansson@arm.com * swig uses ptrdiff_t without the std:: namespace prefix which is 7158860Sandreas.hansson@arm.com * required with gcc 4.6.1. We explicitly provide access to it. 7168860Sandreas.hansson@arm.com */ 7178860Sandreas.hansson@arm.com#include <cstddef> 7188860Sandreas.hansson@arm.comusing std::ptrdiff_t; 7198860Sandreas.hansson@arm.com''') 7208596Ssteve.reinhardt@amd.com code('%}') 7218596Ssteve.reinhardt@amd.com code() 7228596Ssteve.reinhardt@amd.com 7238596Ssteve.reinhardt@amd.com for param in params: 7248596Ssteve.reinhardt@amd.com param.swig_predecls(code) 7258597Ssteve.reinhardt@amd.com cls.export_method_swig_predecls(code) 7268596Ssteve.reinhardt@amd.com 7278596Ssteve.reinhardt@amd.com code() 7288596Ssteve.reinhardt@amd.com if cls._base: 7298596Ssteve.reinhardt@amd.com code('%import "python/m5/internal/param_${{cls._base}}.i"') 7308596Ssteve.reinhardt@amd.com code() 7318596Ssteve.reinhardt@amd.com 7328596Ssteve.reinhardt@amd.com for ns in namespaces: 7338596Ssteve.reinhardt@amd.com code('namespace $ns {') 7348596Ssteve.reinhardt@amd.com 7358596Ssteve.reinhardt@amd.com if namespaces: 7368596Ssteve.reinhardt@amd.com code('// avoid name conflicts') 7378596Ssteve.reinhardt@amd.com sep_string = '_COLONS_' 7388596Ssteve.reinhardt@amd.com flat_name = sep_string.join(class_path) 7398596Ssteve.reinhardt@amd.com code('%rename($flat_name) $classname;') 7408596Ssteve.reinhardt@amd.com 7418597Ssteve.reinhardt@amd.com code() 7428597Ssteve.reinhardt@amd.com code('// stop swig from creating/wrapping default ctor/dtor') 7438597Ssteve.reinhardt@amd.com code('%nodefault $classname;') 7448597Ssteve.reinhardt@amd.com code('class $classname') 7458597Ssteve.reinhardt@amd.com if cls._base: 7469342SAndreas.Sandberg@arm.com bases = [ cls._base.cxx_class ] + cls.cxx_bases 7479342SAndreas.Sandberg@arm.com else: 7489342SAndreas.Sandberg@arm.com bases = cls.cxx_bases 7499342SAndreas.Sandberg@arm.com base_first = True 7509342SAndreas.Sandberg@arm.com for base in bases: 7519342SAndreas.Sandberg@arm.com if base_first: 7529342SAndreas.Sandberg@arm.com code(' : public ${{base}}') 7539342SAndreas.Sandberg@arm.com base_first = False 7549342SAndreas.Sandberg@arm.com else: 7559342SAndreas.Sandberg@arm.com code(' , public ${{base}}') 7569342SAndreas.Sandberg@arm.com 7578597Ssteve.reinhardt@amd.com code('{') 7588597Ssteve.reinhardt@amd.com code(' public:') 7598597Ssteve.reinhardt@amd.com cls.export_methods(code) 7608597Ssteve.reinhardt@amd.com code('};') 7618596Ssteve.reinhardt@amd.com 7628596Ssteve.reinhardt@amd.com for ns in reversed(namespaces): 7638596Ssteve.reinhardt@amd.com code('} // namespace $ns') 7648596Ssteve.reinhardt@amd.com 7658596Ssteve.reinhardt@amd.com code() 7668596Ssteve.reinhardt@amd.com code('%include "params/$cls.hh"') 7678596Ssteve.reinhardt@amd.com 7688596Ssteve.reinhardt@amd.com 7698596Ssteve.reinhardt@amd.com # Generate the C++ declaration (.hh file) for this SimObject's 7708596Ssteve.reinhardt@amd.com # param struct. Called from src/SConscript. 7718596Ssteve.reinhardt@amd.com def cxx_param_decl(cls, code): 7728596Ssteve.reinhardt@amd.com # The 'local' attribute restricts us to the params declared in 7733100SN/A # the object itself, not including inherited params (which 7743100SN/A # will also be inherited from the base class's param struct 7753100SN/A # here). 7764762Snate@binkert.org params = cls._params.local.values() 7778840Sandreas.hansson@arm.com ports = cls._ports.local 7783100SN/A try: 7793100SN/A ptypes = [p.ptype for p in params] 7803100SN/A except: 7813100SN/A print cls, p, p.ptype_str 7823100SN/A print params 7833100SN/A raise 7843100SN/A 7857675Snate@binkert.org class_path = cls._value_dict['cxx_class'].split('::') 7867675Snate@binkert.org 7877675Snate@binkert.org code('''\ 7887675Snate@binkert.org#ifndef __PARAMS__${cls}__ 7897675Snate@binkert.org#define __PARAMS__${cls}__ 7907675Snate@binkert.org 7917675Snate@binkert.org''') 7927675Snate@binkert.org 7937675Snate@binkert.org # A forward class declaration is sufficient since we are just 7947675Snate@binkert.org # declaring a pointer. 7957675Snate@binkert.org for ns in class_path[:-1]: 7967675Snate@binkert.org code('namespace $ns {') 7977675Snate@binkert.org code('class $0;', class_path[-1]) 7987675Snate@binkert.org for ns in reversed(class_path[:-1]): 7997811Ssteve.reinhardt@amd.com code('} // namespace $ns') 8007675Snate@binkert.org code() 8017675Snate@binkert.org 8028597Ssteve.reinhardt@amd.com # The base SimObject has a couple of params that get 8038597Ssteve.reinhardt@amd.com # automatically set from Python without being declared through 8048597Ssteve.reinhardt@amd.com # the normal Param mechanism; we slip them in here (needed 8058597Ssteve.reinhardt@amd.com # predecls now, actual declarations below) 8068597Ssteve.reinhardt@amd.com if cls == SimObject: 8078597Ssteve.reinhardt@amd.com code(''' 8088597Ssteve.reinhardt@amd.com#ifndef PY_VERSION 8098597Ssteve.reinhardt@amd.comstruct PyObject; 8108597Ssteve.reinhardt@amd.com#endif 8118597Ssteve.reinhardt@amd.com 8128597Ssteve.reinhardt@amd.com#include <string> 8138597Ssteve.reinhardt@amd.com''') 8147673Snate@binkert.org for param in params: 8157673Snate@binkert.org param.cxx_predecls(code) 8168840Sandreas.hansson@arm.com for port in ports.itervalues(): 8178840Sandreas.hansson@arm.com port.cxx_predecls(code) 8187673Snate@binkert.org code() 8194762Snate@binkert.org 8205610Snate@binkert.org if cls._base: 8217673Snate@binkert.org code('#include "params/${{cls._base.type}}.hh"') 8227673Snate@binkert.org code() 8234762Snate@binkert.org 8244762Snate@binkert.org for ptype in ptypes: 8254762Snate@binkert.org if issubclass(ptype, Enum): 8267673Snate@binkert.org code('#include "enums/${{ptype.__name__}}.hh"') 8277673Snate@binkert.org code() 8284762Snate@binkert.org 8298596Ssteve.reinhardt@amd.com # now generate the actual param struct 8308597Ssteve.reinhardt@amd.com code("struct ${cls}Params") 8318597Ssteve.reinhardt@amd.com if cls._base: 8328597Ssteve.reinhardt@amd.com code(" : public ${{cls._base.type}}Params") 8338597Ssteve.reinhardt@amd.com code("{") 8348597Ssteve.reinhardt@amd.com if not hasattr(cls, 'abstract') or not cls.abstract: 8358597Ssteve.reinhardt@amd.com if 'type' in cls.__dict__: 8368597Ssteve.reinhardt@amd.com code(" ${{cls.cxx_type}} create();") 8378597Ssteve.reinhardt@amd.com 8388597Ssteve.reinhardt@amd.com code.indent() 8398596Ssteve.reinhardt@amd.com if cls == SimObject: 8408597Ssteve.reinhardt@amd.com code(''' 8419983Sstever@gmail.com SimObjectParams() {} 8428597Ssteve.reinhardt@amd.com virtual ~SimObjectParams() {} 8438596Ssteve.reinhardt@amd.com 8448597Ssteve.reinhardt@amd.com std::string name; 8458597Ssteve.reinhardt@amd.com PyObject *pyobj; 8468597Ssteve.reinhardt@amd.com ''') 8478597Ssteve.reinhardt@amd.com for param in params: 8488597Ssteve.reinhardt@amd.com param.cxx_decl(code) 8498840Sandreas.hansson@arm.com for port in ports.itervalues(): 8508840Sandreas.hansson@arm.com port.cxx_decl(code) 8518840Sandreas.hansson@arm.com 8528597Ssteve.reinhardt@amd.com code.dedent() 8538597Ssteve.reinhardt@amd.com code('};') 8545488Snate@binkert.org 8557673Snate@binkert.org code() 8567673Snate@binkert.org code('#endif // __PARAMS__${cls}__') 8575488Snate@binkert.org return code 8585488Snate@binkert.org 85910458Sandreas.hansson@arm.com # Generate the C++ declaration/definition files for this SimObject's 86010458Sandreas.hansson@arm.com # param struct to allow C++ initialisation 86110458Sandreas.hansson@arm.com def cxx_config_param_file(cls, code, is_header): 86210458Sandreas.hansson@arm.com createCxxConfigDirectoryEntryFile(code, cls.__name__, cls, is_header) 86310458Sandreas.hansson@arm.com return code 8645488Snate@binkert.org 8659983Sstever@gmail.com# This *temporary* definition is required to support calls from the 8669983Sstever@gmail.com# SimObject class definition to the MetaSimObject methods (in 8679983Sstever@gmail.com# particular _set_param, which gets called for parameters with default 8689983Sstever@gmail.com# values defined on the SimObject class itself). It will get 8699983Sstever@gmail.com# overridden by the permanent definition (which requires that 8709983Sstever@gmail.com# SimObject be defined) lower in this file. 8719983Sstever@gmail.comdef isSimObjectOrVector(value): 8729983Sstever@gmail.com return False 8733100SN/A 87410267SGeoffrey.Blake@arm.com# This class holds information about each simobject parameter 87510267SGeoffrey.Blake@arm.com# that should be displayed on the command line for use in the 87610267SGeoffrey.Blake@arm.com# configuration system. 87710267SGeoffrey.Blake@arm.comclass ParamInfo(object): 87810267SGeoffrey.Blake@arm.com def __init__(self, type, desc, type_str, example, default_val, access_str): 87910267SGeoffrey.Blake@arm.com self.type = type 88010267SGeoffrey.Blake@arm.com self.desc = desc 88110267SGeoffrey.Blake@arm.com self.type_str = type_str 88210267SGeoffrey.Blake@arm.com self.example_str = example 88310267SGeoffrey.Blake@arm.com self.default_val = default_val 88410267SGeoffrey.Blake@arm.com # The string representation used to access this param through python. 88510267SGeoffrey.Blake@arm.com # The method to access this parameter presented on the command line may 88610267SGeoffrey.Blake@arm.com # be different, so this needs to be stored for later use. 88710267SGeoffrey.Blake@arm.com self.access_str = access_str 88810267SGeoffrey.Blake@arm.com self.created = True 88910267SGeoffrey.Blake@arm.com 89010267SGeoffrey.Blake@arm.com # Make it so we can only set attributes at initialization time 89110267SGeoffrey.Blake@arm.com # and effectively make this a const object. 89210267SGeoffrey.Blake@arm.com def __setattr__(self, name, value): 89310267SGeoffrey.Blake@arm.com if not "created" in self.__dict__: 89410267SGeoffrey.Blake@arm.com self.__dict__[name] = value 89510267SGeoffrey.Blake@arm.com 8962740SN/A# The SimObject class is the root of the special hierarchy. Most of 897679SN/A# the code in this class deals with the configuration hierarchy itself 898679SN/A# (parent/child node relationships). 8991692SN/Aclass SimObject(object): 9001692SN/A # Specify metaclass. Any class inheriting from SimObject will 901679SN/A # get this metaclass. 9021692SN/A __metaclass__ = MetaSimObject 9033100SN/A type = 'SimObject' 9044762Snate@binkert.org abstract = True 9059983Sstever@gmail.com 9069338SAndreas.Sandberg@arm.com cxx_header = "sim/sim_object.hh" 9079345SAndreas.Sandberg@ARM.com cxx_bases = [ "Drainable", "Serializable" ] 9089983Sstever@gmail.com eventq_index = Param.UInt32(Parent.eventq_index, "Event Queue Index") 9099342SAndreas.Sandberg@arm.com 9108597Ssteve.reinhardt@amd.com @classmethod 9118597Ssteve.reinhardt@amd.com def export_method_swig_predecls(cls, code): 9128597Ssteve.reinhardt@amd.com code(''' 9138597Ssteve.reinhardt@amd.com%include <std_string.i> 9149342SAndreas.Sandberg@arm.com 9159342SAndreas.Sandberg@arm.com%import "python/swig/drain.i" 9169345SAndreas.Sandberg@ARM.com%import "python/swig/serialize.i" 9178597Ssteve.reinhardt@amd.com''') 9188597Ssteve.reinhardt@amd.com 9198597Ssteve.reinhardt@amd.com @classmethod 9208597Ssteve.reinhardt@amd.com def export_methods(cls, code): 9218597Ssteve.reinhardt@amd.com code(''' 9228597Ssteve.reinhardt@amd.com void init(); 9238597Ssteve.reinhardt@amd.com void loadState(Checkpoint *cp); 9248597Ssteve.reinhardt@amd.com void initState(); 9258597Ssteve.reinhardt@amd.com void regStats(); 9268597Ssteve.reinhardt@amd.com void resetStats(); 92710023Smatt.horsnell@ARM.com void regProbePoints(); 92810023Smatt.horsnell@ARM.com void regProbeListeners(); 9298597Ssteve.reinhardt@amd.com void startup(); 9308597Ssteve.reinhardt@amd.com''') 9318597Ssteve.reinhardt@amd.com 93210267SGeoffrey.Blake@arm.com # Returns a dict of all the option strings that can be 93310267SGeoffrey.Blake@arm.com # generated as command line options for this simobject instance 93410267SGeoffrey.Blake@arm.com # by tracing all reachable params in the top level instance and 93510267SGeoffrey.Blake@arm.com # any children it contains. 93610267SGeoffrey.Blake@arm.com def enumerateParams(self, flags_dict = {}, 93710267SGeoffrey.Blake@arm.com cmd_line_str = "", access_str = ""): 93810267SGeoffrey.Blake@arm.com if hasattr(self, "_paramEnumed"): 93910267SGeoffrey.Blake@arm.com print "Cycle detected enumerating params" 94010267SGeoffrey.Blake@arm.com else: 94110267SGeoffrey.Blake@arm.com self._paramEnumed = True 94210267SGeoffrey.Blake@arm.com # Scan the children first to pick up all the objects in this SimObj 94310267SGeoffrey.Blake@arm.com for keys in self._children: 94410267SGeoffrey.Blake@arm.com child = self._children[keys] 94510267SGeoffrey.Blake@arm.com next_cmdline_str = cmd_line_str + keys 94610267SGeoffrey.Blake@arm.com next_access_str = access_str + keys 94710267SGeoffrey.Blake@arm.com if not isSimObjectVector(child): 94810267SGeoffrey.Blake@arm.com next_cmdline_str = next_cmdline_str + "." 94910267SGeoffrey.Blake@arm.com next_access_str = next_access_str + "." 95010267SGeoffrey.Blake@arm.com flags_dict = child.enumerateParams(flags_dict, 95110267SGeoffrey.Blake@arm.com next_cmdline_str, 95210267SGeoffrey.Blake@arm.com next_access_str) 95310267SGeoffrey.Blake@arm.com 95410267SGeoffrey.Blake@arm.com # Go through the simple params in the simobject in this level 95510267SGeoffrey.Blake@arm.com # of the simobject hierarchy and save information about the 95610267SGeoffrey.Blake@arm.com # parameter to be used for generating and processing command line 95710267SGeoffrey.Blake@arm.com # options to the simulator to set these parameters. 95810267SGeoffrey.Blake@arm.com for keys,values in self._params.items(): 95910267SGeoffrey.Blake@arm.com if values.isCmdLineSettable(): 96010267SGeoffrey.Blake@arm.com type_str = '' 96110267SGeoffrey.Blake@arm.com ex_str = values.example_str() 96210267SGeoffrey.Blake@arm.com ptype = None 96310267SGeoffrey.Blake@arm.com if isinstance(values, VectorParamDesc): 96410267SGeoffrey.Blake@arm.com type_str = 'Vector_%s' % values.ptype_str 96510267SGeoffrey.Blake@arm.com ptype = values 96610267SGeoffrey.Blake@arm.com else: 96710267SGeoffrey.Blake@arm.com type_str = '%s' % values.ptype_str 96810267SGeoffrey.Blake@arm.com ptype = values.ptype 96910267SGeoffrey.Blake@arm.com 97010267SGeoffrey.Blake@arm.com if keys in self._hr_values\ 97110267SGeoffrey.Blake@arm.com and keys in self._values\ 97210267SGeoffrey.Blake@arm.com and not isinstance(self._values[keys], m5.proxy.BaseProxy): 97310267SGeoffrey.Blake@arm.com cmd_str = cmd_line_str + keys 97410267SGeoffrey.Blake@arm.com acc_str = access_str + keys 97510267SGeoffrey.Blake@arm.com flags_dict[cmd_str] = ParamInfo(ptype, 97610267SGeoffrey.Blake@arm.com self._params[keys].desc, type_str, ex_str, 97710267SGeoffrey.Blake@arm.com values.pretty_print(self._hr_values[keys]), 97810267SGeoffrey.Blake@arm.com acc_str) 97910267SGeoffrey.Blake@arm.com elif not keys in self._hr_values\ 98010267SGeoffrey.Blake@arm.com and not keys in self._values: 98110267SGeoffrey.Blake@arm.com # Empty param 98210267SGeoffrey.Blake@arm.com cmd_str = cmd_line_str + keys 98310267SGeoffrey.Blake@arm.com acc_str = access_str + keys 98410267SGeoffrey.Blake@arm.com flags_dict[cmd_str] = ParamInfo(ptype, 98510267SGeoffrey.Blake@arm.com self._params[keys].desc, 98610267SGeoffrey.Blake@arm.com type_str, ex_str, '', acc_str) 98710267SGeoffrey.Blake@arm.com 98810267SGeoffrey.Blake@arm.com return flags_dict 98910267SGeoffrey.Blake@arm.com 9902740SN/A # Initialize new instance. For objects with SimObject-valued 9912740SN/A # children, we need to recursively clone the classes represented 9922740SN/A # by those param values as well in a consistent "deep copy"-style 9932740SN/A # fashion. That is, we want to make sure that each instance is 9942740SN/A # cloned only once, and that if there are multiple references to 9952740SN/A # the same original object, we end up with the corresponding 9962740SN/A # cloned references all pointing to the same cloned instance. 9972740SN/A def __init__(self, **kwargs): 9982740SN/A ancestor = kwargs.get('_ancestor') 9992740SN/A memo_dict = kwargs.get('_memo') 10002740SN/A if memo_dict is None: 10012740SN/A # prepare to memoize any recursively instantiated objects 10022740SN/A memo_dict = {} 10032740SN/A elif ancestor: 10042740SN/A # memoize me now to avoid problems with recursive calls 10052740SN/A memo_dict[ancestor] = self 10062711SN/A 10072740SN/A if not ancestor: 10082740SN/A ancestor = self.__class__ 10092740SN/A ancestor._instantiated = True 10102711SN/A 10112740SN/A # initialize required attributes 10122740SN/A self._parent = None 10137528Ssteve.reinhardt@amd.com self._name = None 10142740SN/A self._ccObject = None # pointer to C++ object 10154762Snate@binkert.org self._ccParams = None 10162740SN/A self._instantiated = False # really "cloned" 10172712SN/A 10188321Ssteve.reinhardt@amd.com # Clone children specified at class level. No need for a 10198321Ssteve.reinhardt@amd.com # multidict here since we will be cloning everything. 10208321Ssteve.reinhardt@amd.com # Do children before parameter values so that children that 10218321Ssteve.reinhardt@amd.com # are also param values get cloned properly. 10228321Ssteve.reinhardt@amd.com self._children = {} 10238321Ssteve.reinhardt@amd.com for key,val in ancestor._children.iteritems(): 10248321Ssteve.reinhardt@amd.com self.add_child(key, val(_memo=memo_dict)) 10258321Ssteve.reinhardt@amd.com 10262711SN/A # Inherit parameter values from class using multidict so 10277528Ssteve.reinhardt@amd.com # individual value settings can be overridden but we still 10287528Ssteve.reinhardt@amd.com # inherit late changes to non-overridden class values. 10292740SN/A self._values = multidict(ancestor._values) 103010267SGeoffrey.Blake@arm.com self._hr_values = multidict(ancestor._hr_values) 10312740SN/A # clone SimObject-valued parameters 10322740SN/A for key,val in ancestor._values.iteritems(): 10337528Ssteve.reinhardt@amd.com val = tryAsSimObjectOrVector(val) 10347528Ssteve.reinhardt@amd.com if val is not None: 10357528Ssteve.reinhardt@amd.com self._values[key] = val(_memo=memo_dict) 10367528Ssteve.reinhardt@amd.com 10372740SN/A # clone port references. no need to use a multidict here 10382740SN/A # since we will be creating new references for all ports. 10393105Sstever@eecs.umich.edu self._port_refs = {} 10403105Sstever@eecs.umich.edu for key,val in ancestor._port_refs.iteritems(): 10413105Sstever@eecs.umich.edu self._port_refs[key] = val.clone(self, memo_dict) 10421692SN/A # apply attribute assignments from keyword args, if any 10431692SN/A for key,val in kwargs.iteritems(): 10441692SN/A setattr(self, key, val) 1045679SN/A 10462740SN/A # "Clone" the current instance by creating another instance of 10472740SN/A # this instance's class, but that inherits its parameter values 10482740SN/A # and port mappings from the current instance. If we're in a 10492740SN/A # "deep copy" recursive clone, check the _memo dict to see if 10502740SN/A # we've already cloned this instance. 10511692SN/A def __call__(self, **kwargs): 10522740SN/A memo_dict = kwargs.get('_memo') 10532740SN/A if memo_dict is None: 10542740SN/A # no memo_dict: must be top-level clone operation. 10552740SN/A # this is only allowed at the root of a hierarchy 10562740SN/A if self._parent: 10572740SN/A raise RuntimeError, "attempt to clone object %s " \ 10582740SN/A "not at the root of a tree (parent = %s)" \ 10592740SN/A % (self, self._parent) 10602740SN/A # create a new dict and use that. 10612740SN/A memo_dict = {} 10622740SN/A kwargs['_memo'] = memo_dict 10632740SN/A elif memo_dict.has_key(self): 10642740SN/A # clone already done & memoized 10652740SN/A return memo_dict[self] 10662740SN/A return self.__class__(_ancestor = self, **kwargs) 10671343SN/A 10683105Sstever@eecs.umich.edu def _get_port_ref(self, attr): 10693105Sstever@eecs.umich.edu # Return reference that can be assigned to another port 10703105Sstever@eecs.umich.edu # via __setattr__. There is only ever one reference 10713105Sstever@eecs.umich.edu # object per port, but we create them lazily here. 10723105Sstever@eecs.umich.edu ref = self._port_refs.get(attr) 10739940SGeoffrey.Blake@arm.com if ref == None: 10743105Sstever@eecs.umich.edu ref = self._ports[attr].makeRef(self) 10753105Sstever@eecs.umich.edu self._port_refs[attr] = ref 10763105Sstever@eecs.umich.edu return ref 10773105Sstever@eecs.umich.edu 10781692SN/A def __getattr__(self, attr): 10792738SN/A if self._ports.has_key(attr): 10803105Sstever@eecs.umich.edu return self._get_port_ref(attr) 10812738SN/A 10821692SN/A if self._values.has_key(attr): 10831692SN/A return self._values[attr] 10841427SN/A 10857528Ssteve.reinhardt@amd.com if self._children.has_key(attr): 10867528Ssteve.reinhardt@amd.com return self._children[attr] 10877528Ssteve.reinhardt@amd.com 10887500Ssteve.reinhardt@amd.com # If the attribute exists on the C++ object, transparently 10897500Ssteve.reinhardt@amd.com # forward the reference there. This is typically used for 10907500Ssteve.reinhardt@amd.com # SWIG-wrapped methods such as init(), regStats(), 10919195SAndreas.Sandberg@arm.com # resetStats(), startup(), drain(), and 10927527Ssteve.reinhardt@amd.com # resume(). 10937500Ssteve.reinhardt@amd.com if self._ccObject and hasattr(self._ccObject, attr): 10947500Ssteve.reinhardt@amd.com return getattr(self._ccObject, attr) 10957500Ssteve.reinhardt@amd.com 109610002Ssteve.reinhardt@amd.com err_string = "object '%s' has no attribute '%s'" \ 10971692SN/A % (self.__class__.__name__, attr) 10981427SN/A 109910002Ssteve.reinhardt@amd.com if not self._ccObject: 110010002Ssteve.reinhardt@amd.com err_string += "\n (C++ object is not yet constructed," \ 110110002Ssteve.reinhardt@amd.com " so wrapped C++ methods are unavailable.)" 110210002Ssteve.reinhardt@amd.com 110310002Ssteve.reinhardt@amd.com raise AttributeError, err_string 110410002Ssteve.reinhardt@amd.com 11051692SN/A # Set attribute (called on foo.attr = value when foo is an 11061692SN/A # instance of class cls). 11071692SN/A def __setattr__(self, attr, value): 11081692SN/A # normal processing for private attributes 11091692SN/A if attr.startswith('_'): 11101692SN/A object.__setattr__(self, attr, value) 11111692SN/A return 11121427SN/A 11132738SN/A if self._ports.has_key(attr): 11142738SN/A # set up port connection 11153105Sstever@eecs.umich.edu self._get_port_ref(attr).connect(value) 11162738SN/A return 11172738SN/A 11183105Sstever@eecs.umich.edu param = self._params.get(attr) 11191692SN/A if param: 11201310SN/A try: 112110267SGeoffrey.Blake@arm.com hr_value = value 11221692SN/A value = param.convert(value) 11231587SN/A except Exception, e: 11241692SN/A msg = "%s\nError setting param %s.%s to %s\n" % \ 11251692SN/A (e, self.__class__.__name__, attr, value) 11261605SN/A e.args = (msg, ) 11271605SN/A raise 11287528Ssteve.reinhardt@amd.com self._values[attr] = value 11298321Ssteve.reinhardt@amd.com # implicitly parent unparented objects assigned as params 11308321Ssteve.reinhardt@amd.com if isSimObjectOrVector(value) and not value.has_parent(): 11318321Ssteve.reinhardt@amd.com self.add_child(attr, value) 113210267SGeoffrey.Blake@arm.com # set the human-readable value dict if this is a param 113310267SGeoffrey.Blake@arm.com # with a literal value and is not being set as an object 113410267SGeoffrey.Blake@arm.com # or proxy. 113510267SGeoffrey.Blake@arm.com if not (isSimObjectOrVector(value) or\ 113610267SGeoffrey.Blake@arm.com isinstance(value, m5.proxy.BaseProxy)): 113710267SGeoffrey.Blake@arm.com self._hr_values[attr] = hr_value 113810267SGeoffrey.Blake@arm.com 11393105Sstever@eecs.umich.edu return 11401310SN/A 11417528Ssteve.reinhardt@amd.com # if RHS is a SimObject, it's an implicit child assignment 11423105Sstever@eecs.umich.edu if isSimObjectOrSequence(value): 11437528Ssteve.reinhardt@amd.com self.add_child(attr, value) 11443105Sstever@eecs.umich.edu return 11451693SN/A 11463105Sstever@eecs.umich.edu # no valid assignment... raise exception 11473105Sstever@eecs.umich.edu raise AttributeError, "Class %s has no parameter %s" \ 11483105Sstever@eecs.umich.edu % (self.__class__.__name__, attr) 11491310SN/A 11501310SN/A 11511692SN/A # this hack allows tacking a '[0]' onto parameters that may or may 11521692SN/A # not be vectors, and always getting the first element (e.g. cpus) 11531692SN/A def __getitem__(self, key): 11541692SN/A if key == 0: 11551692SN/A return self 115610267SGeoffrey.Blake@arm.com raise IndexError, "Non-zero index '%s' to SimObject" % key 115710267SGeoffrey.Blake@arm.com 115810267SGeoffrey.Blake@arm.com # this hack allows us to iterate over a SimObject that may 115910267SGeoffrey.Blake@arm.com # not be a vector, so we can call a loop over it and get just one 116010267SGeoffrey.Blake@arm.com # element. 116110267SGeoffrey.Blake@arm.com def __len__(self): 116210267SGeoffrey.Blake@arm.com return 1 11631310SN/A 11647528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 11657528Ssteve.reinhardt@amd.com def clear_parent(self, old_parent): 11667528Ssteve.reinhardt@amd.com assert self._parent is old_parent 11677528Ssteve.reinhardt@amd.com self._parent = None 11687528Ssteve.reinhardt@amd.com 11697528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 11707528Ssteve.reinhardt@amd.com def set_parent(self, parent, name): 11717528Ssteve.reinhardt@amd.com self._parent = parent 11727528Ssteve.reinhardt@amd.com self._name = name 11737528Ssteve.reinhardt@amd.com 11749953Sgeoffrey.blake@arm.com # Return parent object of this SimObject, not implemented by SimObjectVector 11759953Sgeoffrey.blake@arm.com # because the elements in a SimObjectVector may not share the same parent 11769953Sgeoffrey.blake@arm.com def get_parent(self): 11779953Sgeoffrey.blake@arm.com return self._parent 11789953Sgeoffrey.blake@arm.com 11797528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 11807528Ssteve.reinhardt@amd.com def get_name(self): 11817528Ssteve.reinhardt@amd.com return self._name 11827528Ssteve.reinhardt@amd.com 11838321Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 11848321Ssteve.reinhardt@amd.com def has_parent(self): 11858321Ssteve.reinhardt@amd.com return self._parent is not None 11867528Ssteve.reinhardt@amd.com 11877742Sgblack@eecs.umich.edu # clear out child with given name. This code is not likely to be exercised. 11887742Sgblack@eecs.umich.edu # See comment in add_child. 11891693SN/A def clear_child(self, name): 11901693SN/A child = self._children[name] 11917528Ssteve.reinhardt@amd.com child.clear_parent(self) 11921693SN/A del self._children[name] 11931693SN/A 11947528Ssteve.reinhardt@amd.com # Add a new child to this object. 11957528Ssteve.reinhardt@amd.com def add_child(self, name, child): 11967528Ssteve.reinhardt@amd.com child = coerceSimObjectOrVector(child) 11978321Ssteve.reinhardt@amd.com if child.has_parent(): 11989528Ssascha.bischoff@arm.com warn("add_child('%s'): child '%s' already has parent", name, 11999528Ssascha.bischoff@arm.com child.get_name()) 12007528Ssteve.reinhardt@amd.com if self._children.has_key(name): 12017742Sgblack@eecs.umich.edu # This code path had an undiscovered bug that would make it fail 12027742Sgblack@eecs.umich.edu # at runtime. It had been here for a long time and was only 12037742Sgblack@eecs.umich.edu # exposed by a buggy script. Changes here will probably not be 12047742Sgblack@eecs.umich.edu # exercised without specialized testing. 12057738Sgblack@eecs.umich.edu self.clear_child(name) 12067528Ssteve.reinhardt@amd.com child.set_parent(self, name) 12077528Ssteve.reinhardt@amd.com self._children[name] = child 12081310SN/A 12097528Ssteve.reinhardt@amd.com # Take SimObject-valued parameters that haven't been explicitly 12107528Ssteve.reinhardt@amd.com # assigned as children and make them children of the object that 12117528Ssteve.reinhardt@amd.com # they were assigned to as a parameter value. This guarantees 12127528Ssteve.reinhardt@amd.com # that when we instantiate all the parameter objects we're still 12137528Ssteve.reinhardt@amd.com # inside the configuration hierarchy. 12147528Ssteve.reinhardt@amd.com def adoptOrphanParams(self): 12157528Ssteve.reinhardt@amd.com for key,val in self._values.iteritems(): 12167528Ssteve.reinhardt@amd.com if not isSimObjectVector(val) and isSimObjectSequence(val): 12177528Ssteve.reinhardt@amd.com # need to convert raw SimObject sequences to 12188321Ssteve.reinhardt@amd.com # SimObjectVector class so we can call has_parent() 12197528Ssteve.reinhardt@amd.com val = SimObjectVector(val) 12207528Ssteve.reinhardt@amd.com self._values[key] = val 12218321Ssteve.reinhardt@amd.com if isSimObjectOrVector(val) and not val.has_parent(): 12229528Ssascha.bischoff@arm.com warn("%s adopting orphan SimObject param '%s'", self, key) 12237528Ssteve.reinhardt@amd.com self.add_child(key, val) 12243105Sstever@eecs.umich.edu 12251692SN/A def path(self): 12262740SN/A if not self._parent: 12278321Ssteve.reinhardt@amd.com return '<orphan %s>' % self.__class__ 12281692SN/A ppath = self._parent.path() 12291692SN/A if ppath == 'root': 12301692SN/A return self._name 12311692SN/A return ppath + "." + self._name 12321310SN/A 12331692SN/A def __str__(self): 12341692SN/A return self.path() 12351310SN/A 123610380SAndrew.Bardsley@arm.com def config_value(self): 123710380SAndrew.Bardsley@arm.com return self.path() 123810380SAndrew.Bardsley@arm.com 12391692SN/A def ini_str(self): 12401692SN/A return self.path() 12411310SN/A 12421692SN/A def find_any(self, ptype): 12431692SN/A if isinstance(self, ptype): 12441692SN/A return self, True 12451310SN/A 12461692SN/A found_obj = None 12471692SN/A for child in self._children.itervalues(): 124810195SGeoffrey.Blake@arm.com visited = False 124910195SGeoffrey.Blake@arm.com if hasattr(child, '_visited'): 125010195SGeoffrey.Blake@arm.com visited = getattr(child, '_visited') 125110195SGeoffrey.Blake@arm.com 125210195SGeoffrey.Blake@arm.com if isinstance(child, ptype) and not visited: 12531692SN/A if found_obj != None and child != found_obj: 12541692SN/A raise AttributeError, \ 12551692SN/A 'parent.any matched more than one: %s %s' % \ 12561814SN/A (found_obj.path, child.path) 12571692SN/A found_obj = child 12581692SN/A # search param space 12591692SN/A for pname,pdesc in self._params.iteritems(): 12601692SN/A if issubclass(pdesc.ptype, ptype): 12611692SN/A match_obj = self._values[pname] 12621692SN/A if found_obj != None and found_obj != match_obj: 12631692SN/A raise AttributeError, \ 12645952Ssaidi@eecs.umich.edu 'parent.any matched more than one: %s and %s' % (found_obj.path, match_obj.path) 12651692SN/A found_obj = match_obj 12661692SN/A return found_obj, found_obj != None 12671692SN/A 12688459SAli.Saidi@ARM.com def find_all(self, ptype): 12698459SAli.Saidi@ARM.com all = {} 12708459SAli.Saidi@ARM.com # search children 12718459SAli.Saidi@ARM.com for child in self._children.itervalues(): 12729410Sandreas.hansson@arm.com # a child could be a list, so ensure we visit each item 12739410Sandreas.hansson@arm.com if isinstance(child, list): 12749410Sandreas.hansson@arm.com children = child 12759410Sandreas.hansson@arm.com else: 12769410Sandreas.hansson@arm.com children = [child] 12779410Sandreas.hansson@arm.com 12789410Sandreas.hansson@arm.com for child in children: 12799410Sandreas.hansson@arm.com if isinstance(child, ptype) and not isproxy(child) and \ 12809410Sandreas.hansson@arm.com not isNullPointer(child): 12819410Sandreas.hansson@arm.com all[child] = True 12829410Sandreas.hansson@arm.com if isSimObject(child): 12839410Sandreas.hansson@arm.com # also add results from the child itself 12849410Sandreas.hansson@arm.com child_all, done = child.find_all(ptype) 12859410Sandreas.hansson@arm.com all.update(dict(zip(child_all, [done] * len(child_all)))) 12868459SAli.Saidi@ARM.com # search param space 12878459SAli.Saidi@ARM.com for pname,pdesc in self._params.iteritems(): 12888459SAli.Saidi@ARM.com if issubclass(pdesc.ptype, ptype): 12898459SAli.Saidi@ARM.com match_obj = self._values[pname] 12908459SAli.Saidi@ARM.com if not isproxy(match_obj) and not isNullPointer(match_obj): 12918459SAli.Saidi@ARM.com all[match_obj] = True 129210532Sandreas.hansson@arm.com # Also make sure to sort the keys based on the objects' path to 129310532Sandreas.hansson@arm.com # ensure that the order is the same on all hosts 129410532Sandreas.hansson@arm.com return sorted(all.keys(), key = lambda o: o.path()), True 12958459SAli.Saidi@ARM.com 12961815SN/A def unproxy(self, base): 12971815SN/A return self 12981815SN/A 12997527Ssteve.reinhardt@amd.com def unproxyParams(self): 13003105Sstever@eecs.umich.edu for param in self._params.iterkeys(): 13013105Sstever@eecs.umich.edu value = self._values.get(param) 13026654Snate@binkert.org if value != None and isproxy(value): 13033105Sstever@eecs.umich.edu try: 13043105Sstever@eecs.umich.edu value = value.unproxy(self) 13053105Sstever@eecs.umich.edu except: 13063105Sstever@eecs.umich.edu print "Error in unproxying param '%s' of %s" % \ 13073105Sstever@eecs.umich.edu (param, self.path()) 13083105Sstever@eecs.umich.edu raise 13093105Sstever@eecs.umich.edu setattr(self, param, value) 13103105Sstever@eecs.umich.edu 13113107Sstever@eecs.umich.edu # Unproxy ports in sorted order so that 'append' operations on 13123107Sstever@eecs.umich.edu # vector ports are done in a deterministic fashion. 13133107Sstever@eecs.umich.edu port_names = self._ports.keys() 13143107Sstever@eecs.umich.edu port_names.sort() 13153107Sstever@eecs.umich.edu for port_name in port_names: 13163105Sstever@eecs.umich.edu port = self._port_refs.get(port_name) 13173105Sstever@eecs.umich.edu if port != None: 13183105Sstever@eecs.umich.edu port.unproxy(self) 13193105Sstever@eecs.umich.edu 13205037Smilesck@eecs.umich.edu def print_ini(self, ini_file): 13215543Ssaidi@eecs.umich.edu print >>ini_file, '[' + self.path() + ']' # .ini section header 13221692SN/A 13232738SN/A instanceDict[self.path()] = self 13242738SN/A 13254081Sbinkertn@umich.edu if hasattr(self, 'type'): 13265037Smilesck@eecs.umich.edu print >>ini_file, 'type=%s' % self.type 13271692SN/A 13288664SAli.Saidi@ARM.com if len(self._children.keys()): 13297528Ssteve.reinhardt@amd.com print >>ini_file, 'children=%s' % \ 13308664SAli.Saidi@ARM.com ' '.join(self._children[n].get_name() \ 13318664SAli.Saidi@ARM.com for n in sorted(self._children.keys())) 13321692SN/A 13338664SAli.Saidi@ARM.com for param in sorted(self._params.keys()): 13343105Sstever@eecs.umich.edu value = self._values.get(param) 13351692SN/A if value != None: 13365037Smilesck@eecs.umich.edu print >>ini_file, '%s=%s' % (param, 13375037Smilesck@eecs.umich.edu self._values[param].ini_str()) 13381692SN/A 13398664SAli.Saidi@ARM.com for port_name in sorted(self._ports.keys()): 13403105Sstever@eecs.umich.edu port = self._port_refs.get(port_name, None) 13413105Sstever@eecs.umich.edu if port != None: 13425037Smilesck@eecs.umich.edu print >>ini_file, '%s=%s' % (port_name, port.ini_str()) 13433103Sstever@eecs.umich.edu 13445543Ssaidi@eecs.umich.edu print >>ini_file # blank line between objects 13451692SN/A 13468664SAli.Saidi@ARM.com # generate a tree of dictionaries expressing all the parameters in the 13478664SAli.Saidi@ARM.com # instantiated system for use by scripts that want to do power, thermal 13488664SAli.Saidi@ARM.com # visualization, and other similar tasks 13498664SAli.Saidi@ARM.com def get_config_as_dict(self): 13508664SAli.Saidi@ARM.com d = attrdict() 13518664SAli.Saidi@ARM.com if hasattr(self, 'type'): 13528664SAli.Saidi@ARM.com d.type = self.type 13538664SAli.Saidi@ARM.com if hasattr(self, 'cxx_class'): 13548664SAli.Saidi@ARM.com d.cxx_class = self.cxx_class 13559017Sandreas.hansson@arm.com # Add the name and path of this object to be able to link to 13569017Sandreas.hansson@arm.com # the stats 13579017Sandreas.hansson@arm.com d.name = self.get_name() 13589017Sandreas.hansson@arm.com d.path = self.path() 13598664SAli.Saidi@ARM.com 13608664SAli.Saidi@ARM.com for param in sorted(self._params.keys()): 13618664SAli.Saidi@ARM.com value = self._values.get(param) 13628848Ssteve.reinhardt@amd.com if value != None: 136310380SAndrew.Bardsley@arm.com d[param] = value.config_value() 13648664SAli.Saidi@ARM.com 13658664SAli.Saidi@ARM.com for n in sorted(self._children.keys()): 13669017Sandreas.hansson@arm.com child = self._children[n] 13679017Sandreas.hansson@arm.com # Use the name of the attribute (and not get_name()) as 13689017Sandreas.hansson@arm.com # the key in the JSON dictionary to capture the hierarchy 13699017Sandreas.hansson@arm.com # in the Python code that assembled this system 13709017Sandreas.hansson@arm.com d[n] = child.get_config_as_dict() 13718664SAli.Saidi@ARM.com 13728664SAli.Saidi@ARM.com for port_name in sorted(self._ports.keys()): 13738664SAli.Saidi@ARM.com port = self._port_refs.get(port_name, None) 13748664SAli.Saidi@ARM.com if port != None: 13759017Sandreas.hansson@arm.com # Represent each port with a dictionary containing the 13769017Sandreas.hansson@arm.com # prominent attributes 13779017Sandreas.hansson@arm.com d[port_name] = port.get_config_as_dict() 13788664SAli.Saidi@ARM.com 13798664SAli.Saidi@ARM.com return d 13808664SAli.Saidi@ARM.com 13814762Snate@binkert.org def getCCParams(self): 13824762Snate@binkert.org if self._ccParams: 13834762Snate@binkert.org return self._ccParams 13844762Snate@binkert.org 13857677Snate@binkert.org cc_params_struct = getattr(m5.internal.params, '%sParams' % self.type) 13864762Snate@binkert.org cc_params = cc_params_struct() 13875488Snate@binkert.org cc_params.pyobj = self 13884762Snate@binkert.org cc_params.name = str(self) 13894762Snate@binkert.org 13904762Snate@binkert.org param_names = self._params.keys() 13914762Snate@binkert.org param_names.sort() 13924762Snate@binkert.org for param in param_names: 13934762Snate@binkert.org value = self._values.get(param) 13944762Snate@binkert.org if value is None: 13956654Snate@binkert.org fatal("%s.%s without default or user set value", 13966654Snate@binkert.org self.path(), param) 13974762Snate@binkert.org 13984762Snate@binkert.org value = value.getValue() 13994762Snate@binkert.org if isinstance(self._params[param], VectorParamDesc): 14004762Snate@binkert.org assert isinstance(value, list) 14014762Snate@binkert.org vec = getattr(cc_params, param) 14024762Snate@binkert.org assert not len(vec) 14034762Snate@binkert.org for v in value: 14044762Snate@binkert.org vec.append(v) 14054762Snate@binkert.org else: 14064762Snate@binkert.org setattr(cc_params, param, value) 14074762Snate@binkert.org 14084762Snate@binkert.org port_names = self._ports.keys() 14094762Snate@binkert.org port_names.sort() 14104762Snate@binkert.org for port_name in port_names: 14114762Snate@binkert.org port = self._port_refs.get(port_name, None) 14128912Sandreas.hansson@arm.com if port != None: 14138912Sandreas.hansson@arm.com port_count = len(port) 14148912Sandreas.hansson@arm.com else: 14158912Sandreas.hansson@arm.com port_count = 0 14168900Sandreas.hansson@arm.com setattr(cc_params, 'port_' + port_name + '_connection_count', 14178912Sandreas.hansson@arm.com port_count) 14184762Snate@binkert.org self._ccParams = cc_params 14194762Snate@binkert.org return self._ccParams 14202738SN/A 14212740SN/A # Get C++ object corresponding to this object, calling C++ if 14222740SN/A # necessary to construct it. Does *not* recursively create 14232740SN/A # children. 14242740SN/A def getCCObject(self): 14252740SN/A if not self._ccObject: 14267526Ssteve.reinhardt@amd.com # Make sure this object is in the configuration hierarchy 14277526Ssteve.reinhardt@amd.com if not self._parent and not isRoot(self): 14287526Ssteve.reinhardt@amd.com raise RuntimeError, "Attempt to instantiate orphan node" 14297526Ssteve.reinhardt@amd.com # Cycles in the configuration hierarchy are not supported. This 14305244Sgblack@eecs.umich.edu # will catch the resulting recursion and stop. 14315244Sgblack@eecs.umich.edu self._ccObject = -1 143210267SGeoffrey.Blake@arm.com if not self.abstract: 143310267SGeoffrey.Blake@arm.com params = self.getCCParams() 143410267SGeoffrey.Blake@arm.com self._ccObject = params.create() 14352740SN/A elif self._ccObject == -1: 14367526Ssteve.reinhardt@amd.com raise RuntimeError, "%s: Cycle found in configuration hierarchy." \ 14372740SN/A % self.path() 14382740SN/A return self._ccObject 14392740SN/A 14407527Ssteve.reinhardt@amd.com def descendants(self): 14417527Ssteve.reinhardt@amd.com yield self 144210532Sandreas.hansson@arm.com # The order of the dict is implementation dependent, so sort 144310532Sandreas.hansson@arm.com # it based on the key (name) to ensure the order is the same 144410532Sandreas.hansson@arm.com # on all hosts 144510532Sandreas.hansson@arm.com for (name, child) in sorted(self._children.iteritems()): 14467527Ssteve.reinhardt@amd.com for obj in child.descendants(): 14477527Ssteve.reinhardt@amd.com yield obj 14487527Ssteve.reinhardt@amd.com 14497527Ssteve.reinhardt@amd.com # Call C++ to create C++ object corresponding to this object 14504762Snate@binkert.org def createCCObject(self): 14514762Snate@binkert.org self.getCCParams() 14524762Snate@binkert.org self.getCCObject() # force creation 14534762Snate@binkert.org 14544762Snate@binkert.org def getValue(self): 14554762Snate@binkert.org return self.getCCObject() 14564762Snate@binkert.org 14572738SN/A # Create C++ port connections corresponding to the connections in 14587527Ssteve.reinhardt@amd.com # _port_refs 14592738SN/A def connectPorts(self): 146010532Sandreas.hansson@arm.com # Sort the ports based on their attribute name to ensure the 146110532Sandreas.hansson@arm.com # order is the same on all hosts 146210532Sandreas.hansson@arm.com for (attr, portRef) in sorted(self._port_refs.iteritems()): 14633105Sstever@eecs.umich.edu portRef.ccConnect() 14642797SN/A 14653101Sstever@eecs.umich.edu# Function to provide to C++ so it can look up instances based on paths 14663101Sstever@eecs.umich.edudef resolveSimObject(name): 14673101Sstever@eecs.umich.edu obj = instanceDict[name] 14683101Sstever@eecs.umich.edu return obj.getCCObject() 1469679SN/A 14706654Snate@binkert.orgdef isSimObject(value): 14716654Snate@binkert.org return isinstance(value, SimObject) 14726654Snate@binkert.org 14736654Snate@binkert.orgdef isSimObjectClass(value): 14746654Snate@binkert.org return issubclass(value, SimObject) 14756654Snate@binkert.org 14767528Ssteve.reinhardt@amd.comdef isSimObjectVector(value): 14777528Ssteve.reinhardt@amd.com return isinstance(value, SimObjectVector) 14787528Ssteve.reinhardt@amd.com 14796654Snate@binkert.orgdef isSimObjectSequence(value): 14806654Snate@binkert.org if not isinstance(value, (list, tuple)) or len(value) == 0: 14816654Snate@binkert.org return False 14826654Snate@binkert.org 14836654Snate@binkert.org for val in value: 14846654Snate@binkert.org if not isNullPointer(val) and not isSimObject(val): 14856654Snate@binkert.org return False 14866654Snate@binkert.org 14876654Snate@binkert.org return True 14886654Snate@binkert.org 14896654Snate@binkert.orgdef isSimObjectOrSequence(value): 14906654Snate@binkert.org return isSimObject(value) or isSimObjectSequence(value) 14916654Snate@binkert.org 14927526Ssteve.reinhardt@amd.comdef isRoot(obj): 14937526Ssteve.reinhardt@amd.com from m5.objects import Root 14947526Ssteve.reinhardt@amd.com return obj and obj is Root.getInstance() 14957526Ssteve.reinhardt@amd.com 14967528Ssteve.reinhardt@amd.comdef isSimObjectOrVector(value): 14977528Ssteve.reinhardt@amd.com return isSimObject(value) or isSimObjectVector(value) 14987528Ssteve.reinhardt@amd.com 14997528Ssteve.reinhardt@amd.comdef tryAsSimObjectOrVector(value): 15007528Ssteve.reinhardt@amd.com if isSimObjectOrVector(value): 15017528Ssteve.reinhardt@amd.com return value 15027528Ssteve.reinhardt@amd.com if isSimObjectSequence(value): 15037528Ssteve.reinhardt@amd.com return SimObjectVector(value) 15047528Ssteve.reinhardt@amd.com return None 15057528Ssteve.reinhardt@amd.com 15067528Ssteve.reinhardt@amd.comdef coerceSimObjectOrVector(value): 15077528Ssteve.reinhardt@amd.com value = tryAsSimObjectOrVector(value) 15087528Ssteve.reinhardt@amd.com if value is None: 15097528Ssteve.reinhardt@amd.com raise TypeError, "SimObject or SimObjectVector expected" 15107528Ssteve.reinhardt@amd.com return value 15117528Ssteve.reinhardt@amd.com 15126654Snate@binkert.orgbaseClasses = allClasses.copy() 15136654Snate@binkert.orgbaseInstances = instanceDict.copy() 15146654Snate@binkert.org 15156654Snate@binkert.orgdef clear(): 15169338SAndreas.Sandberg@arm.com global allClasses, instanceDict, noCxxHeader 15176654Snate@binkert.org 15186654Snate@binkert.org allClasses = baseClasses.copy() 15196654Snate@binkert.org instanceDict = baseInstances.copy() 15209338SAndreas.Sandberg@arm.com noCxxHeader = False 15216654Snate@binkert.org 15221528SN/A# __all__ defines the list of symbols that get exported when 15231528SN/A# 'from config import *' is invoked. Try to keep this reasonably 15241528SN/A# short to avoid polluting other namespaces. 15254762Snate@binkert.org__all__ = [ 'SimObject' ] 1526