SimObject.py revision 11787
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. 50111787Sandreas.sandberg@arm.com for method_name in ('export_methods', 'export_method_swig_predecls'): 5028597Ssteve.reinhardt@amd.com if method_name not in cls.__dict__: 5038597Ssteve.reinhardt@amd.com base_method = getattr(MetaSimObject, method_name) 5048597Ssteve.reinhardt@amd.com m = MethodType(base_method, cls, MetaSimObject) 5058597Ssteve.reinhardt@amd.com setattr(cls, method_name, m) 5068597Ssteve.reinhardt@amd.com 5072740SN/A # Now process the _value_dict items. They could be defining 5082740SN/A # new (or overriding existing) parameters or ports, setting 5092740SN/A # class keywords (e.g., 'abstract'), or setting parameter 5102740SN/A # values or port bindings. The first 3 can only be set when 5112740SN/A # the class is defined, so we handle them here. The others 5122740SN/A # can be set later too, so just emulate that by calling 5132740SN/A # setattr(). 5142740SN/A for key,val in cls._value_dict.items(): 5151527SN/A # param descriptions 5162740SN/A if isinstance(val, ParamDesc): 5171585SN/A cls._new_param(key, val) 5181427SN/A 5192738SN/A # port objects 5202738SN/A elif isinstance(val, Port): 5213105Sstever@eecs.umich.edu cls._new_port(key, val) 5222738SN/A 5231427SN/A # init-time-only keywords 5241427SN/A elif cls.init_keywords.has_key(key): 5251427SN/A cls._set_keyword(key, val, cls.init_keywords[key]) 5261427SN/A 5271427SN/A # default: use normal path (ends up in __setattr__) 5281427SN/A else: 5291427SN/A setattr(cls, key, val) 5301427SN/A 5311427SN/A def _set_keyword(cls, keyword, val, kwtype): 5321427SN/A if not isinstance(val, kwtype): 5331427SN/A raise TypeError, 'keyword %s has bad type %s (expecting %s)' % \ 5341427SN/A (keyword, type(val), kwtype) 5357493Ssteve.reinhardt@amd.com if isinstance(val, FunctionType): 5361427SN/A val = classmethod(val) 5371427SN/A type.__setattr__(cls, keyword, val) 5381427SN/A 5393100SN/A def _new_param(cls, name, pdesc): 5403100SN/A # each param desc should be uniquely assigned to one variable 5413100SN/A assert(not hasattr(pdesc, 'name')) 5423100SN/A pdesc.name = name 5433100SN/A cls._params[name] = pdesc 5443100SN/A if hasattr(pdesc, 'default'): 5453105Sstever@eecs.umich.edu cls._set_param(name, pdesc.default, pdesc) 5463105Sstever@eecs.umich.edu 5473105Sstever@eecs.umich.edu def _set_param(cls, name, value, param): 5483105Sstever@eecs.umich.edu assert(param.name == name) 5493105Sstever@eecs.umich.edu try: 55010267SGeoffrey.Blake@arm.com hr_value = value 5518321Ssteve.reinhardt@amd.com value = param.convert(value) 5523105Sstever@eecs.umich.edu except Exception, e: 5533105Sstever@eecs.umich.edu msg = "%s\nError setting param %s.%s to %s\n" % \ 5543105Sstever@eecs.umich.edu (e, cls.__name__, name, value) 5553105Sstever@eecs.umich.edu e.args = (msg, ) 5563105Sstever@eecs.umich.edu raise 5578321Ssteve.reinhardt@amd.com cls._values[name] = value 5588321Ssteve.reinhardt@amd.com # if param value is a SimObject, make it a child too, so that 5598321Ssteve.reinhardt@amd.com # it gets cloned properly when the class is instantiated 5608321Ssteve.reinhardt@amd.com if isSimObjectOrVector(value) and not value.has_parent(): 5618321Ssteve.reinhardt@amd.com cls._add_cls_child(name, value) 56210267SGeoffrey.Blake@arm.com # update human-readable values of the param if it has a literal 56310267SGeoffrey.Blake@arm.com # value and is not an object or proxy. 56410267SGeoffrey.Blake@arm.com if not (isSimObjectOrVector(value) or\ 56510267SGeoffrey.Blake@arm.com isinstance(value, m5.proxy.BaseProxy)): 56610267SGeoffrey.Blake@arm.com cls._hr_values[name] = hr_value 5678321Ssteve.reinhardt@amd.com 5688321Ssteve.reinhardt@amd.com def _add_cls_child(cls, name, child): 5698321Ssteve.reinhardt@amd.com # It's a little funky to have a class as a parent, but these 5708321Ssteve.reinhardt@amd.com # objects should never be instantiated (only cloned, which 5718321Ssteve.reinhardt@amd.com # clears the parent pointer), and this makes it clear that the 5728321Ssteve.reinhardt@amd.com # object is not an orphan and can provide better error 5738321Ssteve.reinhardt@amd.com # messages. 5748321Ssteve.reinhardt@amd.com child.set_parent(cls, name) 5758321Ssteve.reinhardt@amd.com cls._children[name] = child 5763105Sstever@eecs.umich.edu 5773105Sstever@eecs.umich.edu def _new_port(cls, name, port): 5783105Sstever@eecs.umich.edu # each port should be uniquely assigned to one variable 5793105Sstever@eecs.umich.edu assert(not hasattr(port, 'name')) 5803105Sstever@eecs.umich.edu port.name = name 5813105Sstever@eecs.umich.edu cls._ports[name] = port 5823105Sstever@eecs.umich.edu 5833105Sstever@eecs.umich.edu # same as _get_port_ref, effectively, but for classes 5843105Sstever@eecs.umich.edu def _cls_get_port_ref(cls, attr): 5853105Sstever@eecs.umich.edu # Return reference that can be assigned to another port 5863105Sstever@eecs.umich.edu # via __setattr__. There is only ever one reference 5873105Sstever@eecs.umich.edu # object per port, but we create them lazily here. 5883105Sstever@eecs.umich.edu ref = cls._port_refs.get(attr) 5893105Sstever@eecs.umich.edu if not ref: 5903105Sstever@eecs.umich.edu ref = cls._ports[attr].makeRef(cls) 5913105Sstever@eecs.umich.edu cls._port_refs[attr] = ref 5923105Sstever@eecs.umich.edu return ref 5931585SN/A 5941310SN/A # Set attribute (called on foo.attr = value when foo is an 5951310SN/A # instance of class cls). 5961310SN/A def __setattr__(cls, attr, value): 5971310SN/A # normal processing for private attributes 5987673Snate@binkert.org if public_value(attr, value): 5991310SN/A type.__setattr__(cls, attr, value) 6001310SN/A return 6011310SN/A 6021310SN/A if cls.keywords.has_key(attr): 6031427SN/A cls._set_keyword(attr, value, cls.keywords[attr]) 6041310SN/A return 6051310SN/A 6062738SN/A if cls._ports.has_key(attr): 6073105Sstever@eecs.umich.edu cls._cls_get_port_ref(attr).connect(value) 6082738SN/A return 6092738SN/A 6102740SN/A if isSimObjectOrSequence(value) and cls._instantiated: 6112740SN/A raise RuntimeError, \ 6122740SN/A "cannot set SimObject parameter '%s' after\n" \ 6132740SN/A " class %s has been instantiated or subclassed" \ 6142740SN/A % (attr, cls.__name__) 6152740SN/A 6162740SN/A # check for param 6173105Sstever@eecs.umich.edu param = cls._params.get(attr) 6181310SN/A if param: 6193105Sstever@eecs.umich.edu cls._set_param(attr, value, param) 6203105Sstever@eecs.umich.edu return 6213105Sstever@eecs.umich.edu 6223105Sstever@eecs.umich.edu if isSimObjectOrSequence(value): 6233105Sstever@eecs.umich.edu # If RHS is a SimObject, it's an implicit child assignment. 6248321Ssteve.reinhardt@amd.com cls._add_cls_child(attr, coerceSimObjectOrVector(value)) 6253105Sstever@eecs.umich.edu return 6263105Sstever@eecs.umich.edu 6273105Sstever@eecs.umich.edu # no valid assignment... raise exception 6283105Sstever@eecs.umich.edu raise AttributeError, \ 6293105Sstever@eecs.umich.edu "Class %s has no parameter \'%s\'" % (cls.__name__, attr) 6301310SN/A 6311585SN/A def __getattr__(cls, attr): 6327675Snate@binkert.org if attr == 'cxx_class_path': 6337675Snate@binkert.org return cls.cxx_class.split('::') 6347675Snate@binkert.org 6357675Snate@binkert.org if attr == 'cxx_class_name': 6367675Snate@binkert.org return cls.cxx_class_path[-1] 6377675Snate@binkert.org 6387675Snate@binkert.org if attr == 'cxx_namespaces': 6397675Snate@binkert.org return cls.cxx_class_path[:-1] 6407675Snate@binkert.org 6411692SN/A if cls._values.has_key(attr): 6421692SN/A return cls._values[attr] 6431585SN/A 6447528Ssteve.reinhardt@amd.com if cls._children.has_key(attr): 6457528Ssteve.reinhardt@amd.com return cls._children[attr] 6467528Ssteve.reinhardt@amd.com 6471585SN/A raise AttributeError, \ 6481585SN/A "object '%s' has no attribute '%s'" % (cls.__name__, attr) 6491585SN/A 6503100SN/A def __str__(cls): 6513100SN/A return cls.__name__ 6523100SN/A 6538596Ssteve.reinhardt@amd.com # See ParamValue.cxx_predecls for description. 6548596Ssteve.reinhardt@amd.com def cxx_predecls(cls, code): 6558596Ssteve.reinhardt@amd.com code('#include "params/$cls.hh"') 6568596Ssteve.reinhardt@amd.com 6578596Ssteve.reinhardt@amd.com # See ParamValue.swig_predecls for description. 6588596Ssteve.reinhardt@amd.com def swig_predecls(cls, code): 6598596Ssteve.reinhardt@amd.com code('%import "python/m5/internal/param_$cls.i"') 6608596Ssteve.reinhardt@amd.com 6618597Ssteve.reinhardt@amd.com # Hook for exporting additional C++ methods to Python via SWIG. 6628597Ssteve.reinhardt@amd.com # Default is none, override using @classmethod in class definition. 6638597Ssteve.reinhardt@amd.com def export_methods(cls, code): 6648597Ssteve.reinhardt@amd.com pass 6658597Ssteve.reinhardt@amd.com 6668597Ssteve.reinhardt@amd.com # Generate the code needed as a prerequisite for the C++ methods 6678597Ssteve.reinhardt@amd.com # exported via export_methods() to be processed by SWIG. 6688597Ssteve.reinhardt@amd.com # Typically generates one or more %include or %import statements. 6698597Ssteve.reinhardt@amd.com # If any methods are exported, typically at least the C++ header 6708597Ssteve.reinhardt@amd.com # declaring the relevant SimObject class must be included. 6718597Ssteve.reinhardt@amd.com def export_method_swig_predecls(cls, code): 6728597Ssteve.reinhardt@amd.com pass 6738597Ssteve.reinhardt@amd.com 6748596Ssteve.reinhardt@amd.com # Generate the declaration for this object for wrapping with SWIG. 6758596Ssteve.reinhardt@amd.com # Generates code that goes into a SWIG .i file. Called from 6768596Ssteve.reinhardt@amd.com # src/SConscript. 6778596Ssteve.reinhardt@amd.com def swig_decl(cls, code): 6788596Ssteve.reinhardt@amd.com class_path = cls.cxx_class.split('::') 6798596Ssteve.reinhardt@amd.com classname = class_path[-1] 6808596Ssteve.reinhardt@amd.com namespaces = class_path[:-1] 6818596Ssteve.reinhardt@amd.com 6828596Ssteve.reinhardt@amd.com # The 'local' attribute restricts us to the params declared in 6838596Ssteve.reinhardt@amd.com # the object itself, not including inherited params (which 6848596Ssteve.reinhardt@amd.com # will also be inherited from the base class's param struct 68510584Sandreas.hansson@arm.com # here). Sort the params based on their key 68610584Sandreas.hansson@arm.com params = map(lambda (k, v): v, sorted(cls._params.local.items())) 6878840Sandreas.hansson@arm.com ports = cls._ports.local 6888596Ssteve.reinhardt@amd.com 6898596Ssteve.reinhardt@amd.com code('%module(package="m5.internal") param_$cls') 6908596Ssteve.reinhardt@amd.com code() 6918596Ssteve.reinhardt@amd.com code('%{') 6929342SAndreas.Sandberg@arm.com code('#include "sim/sim_object.hh"') 6938596Ssteve.reinhardt@amd.com code('#include "params/$cls.hh"') 6948596Ssteve.reinhardt@amd.com for param in params: 6958596Ssteve.reinhardt@amd.com param.cxx_predecls(code) 6969338SAndreas.Sandberg@arm.com code('#include "${{cls.cxx_header}}"') 6978860Sandreas.hansson@arm.com code('''\ 6988860Sandreas.hansson@arm.com/** 6998860Sandreas.hansson@arm.com * This is a workaround for bug in swig. Prior to gcc 4.6.1 the STL 7008860Sandreas.hansson@arm.com * headers like vector, string, etc. used to automatically pull in 7018860Sandreas.hansson@arm.com * the cstddef header but starting with gcc 4.6.1 they no longer do. 7028860Sandreas.hansson@arm.com * This leads to swig generated a file that does not compile so we 7038860Sandreas.hansson@arm.com * explicitly include cstddef. Additionally, including version 2.0.4, 7048860Sandreas.hansson@arm.com * swig uses ptrdiff_t without the std:: namespace prefix which is 7058860Sandreas.hansson@arm.com * required with gcc 4.6.1. We explicitly provide access to it. 7068860Sandreas.hansson@arm.com */ 7078860Sandreas.hansson@arm.com#include <cstddef> 7088860Sandreas.hansson@arm.comusing std::ptrdiff_t; 7098860Sandreas.hansson@arm.com''') 7108596Ssteve.reinhardt@amd.com code('%}') 7118596Ssteve.reinhardt@amd.com code() 7128596Ssteve.reinhardt@amd.com 7138596Ssteve.reinhardt@amd.com for param in params: 7148596Ssteve.reinhardt@amd.com param.swig_predecls(code) 7158597Ssteve.reinhardt@amd.com cls.export_method_swig_predecls(code) 7168596Ssteve.reinhardt@amd.com 7178596Ssteve.reinhardt@amd.com code() 7188596Ssteve.reinhardt@amd.com if cls._base: 7198596Ssteve.reinhardt@amd.com code('%import "python/m5/internal/param_${{cls._base}}.i"') 7208596Ssteve.reinhardt@amd.com code() 7218596Ssteve.reinhardt@amd.com 7228596Ssteve.reinhardt@amd.com for ns in namespaces: 7238596Ssteve.reinhardt@amd.com code('namespace $ns {') 7248596Ssteve.reinhardt@amd.com 7258596Ssteve.reinhardt@amd.com if namespaces: 7268596Ssteve.reinhardt@amd.com code('// avoid name conflicts') 7278596Ssteve.reinhardt@amd.com sep_string = '_COLONS_' 7288596Ssteve.reinhardt@amd.com flat_name = sep_string.join(class_path) 7298596Ssteve.reinhardt@amd.com code('%rename($flat_name) $classname;') 7308596Ssteve.reinhardt@amd.com 7318597Ssteve.reinhardt@amd.com code() 7328597Ssteve.reinhardt@amd.com code('// stop swig from creating/wrapping default ctor/dtor') 7338597Ssteve.reinhardt@amd.com code('%nodefault $classname;') 7348597Ssteve.reinhardt@amd.com code('class $classname') 7358597Ssteve.reinhardt@amd.com if cls._base: 7369342SAndreas.Sandberg@arm.com bases = [ cls._base.cxx_class ] + cls.cxx_bases 7379342SAndreas.Sandberg@arm.com else: 7389342SAndreas.Sandberg@arm.com bases = cls.cxx_bases 7399342SAndreas.Sandberg@arm.com base_first = True 7409342SAndreas.Sandberg@arm.com for base in bases: 7419342SAndreas.Sandberg@arm.com if base_first: 7429342SAndreas.Sandberg@arm.com code(' : public ${{base}}') 7439342SAndreas.Sandberg@arm.com base_first = False 7449342SAndreas.Sandberg@arm.com else: 7459342SAndreas.Sandberg@arm.com code(' , public ${{base}}') 7469342SAndreas.Sandberg@arm.com 7478597Ssteve.reinhardt@amd.com code('{') 7488597Ssteve.reinhardt@amd.com code(' public:') 7498597Ssteve.reinhardt@amd.com cls.export_methods(code) 7508597Ssteve.reinhardt@amd.com code('};') 7518596Ssteve.reinhardt@amd.com 7528596Ssteve.reinhardt@amd.com for ns in reversed(namespaces): 7538596Ssteve.reinhardt@amd.com code('} // namespace $ns') 7548596Ssteve.reinhardt@amd.com 7558596Ssteve.reinhardt@amd.com code() 7568596Ssteve.reinhardt@amd.com code('%include "params/$cls.hh"') 7578596Ssteve.reinhardt@amd.com 7588596Ssteve.reinhardt@amd.com 7598596Ssteve.reinhardt@amd.com # Generate the C++ declaration (.hh file) for this SimObject's 7608596Ssteve.reinhardt@amd.com # param struct. Called from src/SConscript. 7618596Ssteve.reinhardt@amd.com def cxx_param_decl(cls, code): 7628596Ssteve.reinhardt@amd.com # The 'local' attribute restricts us to the params declared in 7633100SN/A # the object itself, not including inherited params (which 7643100SN/A # will also be inherited from the base class's param struct 76510584Sandreas.hansson@arm.com # here). Sort the params based on their key 76610584Sandreas.hansson@arm.com params = map(lambda (k, v): v, sorted(cls._params.local.items())) 7678840Sandreas.hansson@arm.com ports = cls._ports.local 7683100SN/A try: 7693100SN/A ptypes = [p.ptype for p in params] 7703100SN/A except: 7713100SN/A print cls, p, p.ptype_str 7723100SN/A print params 7733100SN/A raise 7743100SN/A 7757675Snate@binkert.org class_path = cls._value_dict['cxx_class'].split('::') 7767675Snate@binkert.org 7777675Snate@binkert.org code('''\ 7787675Snate@binkert.org#ifndef __PARAMS__${cls}__ 7797675Snate@binkert.org#define __PARAMS__${cls}__ 7807675Snate@binkert.org 7817675Snate@binkert.org''') 7827675Snate@binkert.org 7837675Snate@binkert.org # A forward class declaration is sufficient since we are just 7847675Snate@binkert.org # declaring a pointer. 7857675Snate@binkert.org for ns in class_path[:-1]: 7867675Snate@binkert.org code('namespace $ns {') 7877675Snate@binkert.org code('class $0;', class_path[-1]) 7887675Snate@binkert.org for ns in reversed(class_path[:-1]): 7897811Ssteve.reinhardt@amd.com code('} // namespace $ns') 7907675Snate@binkert.org code() 7917675Snate@binkert.org 7928597Ssteve.reinhardt@amd.com # The base SimObject has a couple of params that get 7938597Ssteve.reinhardt@amd.com # automatically set from Python without being declared through 7948597Ssteve.reinhardt@amd.com # the normal Param mechanism; we slip them in here (needed 7958597Ssteve.reinhardt@amd.com # predecls now, actual declarations below) 7968597Ssteve.reinhardt@amd.com if cls == SimObject: 7978597Ssteve.reinhardt@amd.com code(''' 7988597Ssteve.reinhardt@amd.com#ifndef PY_VERSION 7998597Ssteve.reinhardt@amd.comstruct PyObject; 8008597Ssteve.reinhardt@amd.com#endif 8018597Ssteve.reinhardt@amd.com 8028597Ssteve.reinhardt@amd.com#include <string> 8038597Ssteve.reinhardt@amd.com''') 8047673Snate@binkert.org for param in params: 8057673Snate@binkert.org param.cxx_predecls(code) 8068840Sandreas.hansson@arm.com for port in ports.itervalues(): 8078840Sandreas.hansson@arm.com port.cxx_predecls(code) 8087673Snate@binkert.org code() 8094762Snate@binkert.org 8105610Snate@binkert.org if cls._base: 8117673Snate@binkert.org code('#include "params/${{cls._base.type}}.hh"') 8127673Snate@binkert.org code() 8134762Snate@binkert.org 8144762Snate@binkert.org for ptype in ptypes: 8154762Snate@binkert.org if issubclass(ptype, Enum): 8167673Snate@binkert.org code('#include "enums/${{ptype.__name__}}.hh"') 8177673Snate@binkert.org code() 8184762Snate@binkert.org 8198596Ssteve.reinhardt@amd.com # now generate the actual param struct 8208597Ssteve.reinhardt@amd.com code("struct ${cls}Params") 8218597Ssteve.reinhardt@amd.com if cls._base: 8228597Ssteve.reinhardt@amd.com code(" : public ${{cls._base.type}}Params") 8238597Ssteve.reinhardt@amd.com code("{") 8248597Ssteve.reinhardt@amd.com if not hasattr(cls, 'abstract') or not cls.abstract: 8258597Ssteve.reinhardt@amd.com if 'type' in cls.__dict__: 8268597Ssteve.reinhardt@amd.com code(" ${{cls.cxx_type}} create();") 8278597Ssteve.reinhardt@amd.com 8288597Ssteve.reinhardt@amd.com code.indent() 8298596Ssteve.reinhardt@amd.com if cls == SimObject: 8308597Ssteve.reinhardt@amd.com code(''' 8319983Sstever@gmail.com SimObjectParams() {} 8328597Ssteve.reinhardt@amd.com virtual ~SimObjectParams() {} 8338596Ssteve.reinhardt@amd.com 8348597Ssteve.reinhardt@amd.com std::string name; 8358597Ssteve.reinhardt@amd.com PyObject *pyobj; 8368597Ssteve.reinhardt@amd.com ''') 8378597Ssteve.reinhardt@amd.com for param in params: 8388597Ssteve.reinhardt@amd.com param.cxx_decl(code) 8398840Sandreas.hansson@arm.com for port in ports.itervalues(): 8408840Sandreas.hansson@arm.com port.cxx_decl(code) 8418840Sandreas.hansson@arm.com 8428597Ssteve.reinhardt@amd.com code.dedent() 8438597Ssteve.reinhardt@amd.com code('};') 8445488Snate@binkert.org 8457673Snate@binkert.org code() 8467673Snate@binkert.org code('#endif // __PARAMS__${cls}__') 8475488Snate@binkert.org return code 8485488Snate@binkert.org 84910458Sandreas.hansson@arm.com # Generate the C++ declaration/definition files for this SimObject's 85010458Sandreas.hansson@arm.com # param struct to allow C++ initialisation 85110458Sandreas.hansson@arm.com def cxx_config_param_file(cls, code, is_header): 85210458Sandreas.hansson@arm.com createCxxConfigDirectoryEntryFile(code, cls.__name__, cls, is_header) 85310458Sandreas.hansson@arm.com return code 8545488Snate@binkert.org 8559983Sstever@gmail.com# This *temporary* definition is required to support calls from the 8569983Sstever@gmail.com# SimObject class definition to the MetaSimObject methods (in 8579983Sstever@gmail.com# particular _set_param, which gets called for parameters with default 8589983Sstever@gmail.com# values defined on the SimObject class itself). It will get 8599983Sstever@gmail.com# overridden by the permanent definition (which requires that 8609983Sstever@gmail.com# SimObject be defined) lower in this file. 8619983Sstever@gmail.comdef isSimObjectOrVector(value): 8629983Sstever@gmail.com return False 8633100SN/A 86410267SGeoffrey.Blake@arm.com# This class holds information about each simobject parameter 86510267SGeoffrey.Blake@arm.com# that should be displayed on the command line for use in the 86610267SGeoffrey.Blake@arm.com# configuration system. 86710267SGeoffrey.Blake@arm.comclass ParamInfo(object): 86810267SGeoffrey.Blake@arm.com def __init__(self, type, desc, type_str, example, default_val, access_str): 86910267SGeoffrey.Blake@arm.com self.type = type 87010267SGeoffrey.Blake@arm.com self.desc = desc 87110267SGeoffrey.Blake@arm.com self.type_str = type_str 87210267SGeoffrey.Blake@arm.com self.example_str = example 87310267SGeoffrey.Blake@arm.com self.default_val = default_val 87410267SGeoffrey.Blake@arm.com # The string representation used to access this param through python. 87510267SGeoffrey.Blake@arm.com # The method to access this parameter presented on the command line may 87610267SGeoffrey.Blake@arm.com # be different, so this needs to be stored for later use. 87710267SGeoffrey.Blake@arm.com self.access_str = access_str 87810267SGeoffrey.Blake@arm.com self.created = True 87910267SGeoffrey.Blake@arm.com 88010267SGeoffrey.Blake@arm.com # Make it so we can only set attributes at initialization time 88110267SGeoffrey.Blake@arm.com # and effectively make this a const object. 88210267SGeoffrey.Blake@arm.com def __setattr__(self, name, value): 88310267SGeoffrey.Blake@arm.com if not "created" in self.__dict__: 88410267SGeoffrey.Blake@arm.com self.__dict__[name] = value 88510267SGeoffrey.Blake@arm.com 8862740SN/A# The SimObject class is the root of the special hierarchy. Most of 887679SN/A# the code in this class deals with the configuration hierarchy itself 888679SN/A# (parent/child node relationships). 8891692SN/Aclass SimObject(object): 8901692SN/A # Specify metaclass. Any class inheriting from SimObject will 891679SN/A # get this metaclass. 8921692SN/A __metaclass__ = MetaSimObject 8933100SN/A type = 'SimObject' 8944762Snate@binkert.org abstract = True 8959983Sstever@gmail.com 8969338SAndreas.Sandberg@arm.com cxx_header = "sim/sim_object.hh" 8979345SAndreas.Sandberg@ARM.com cxx_bases = [ "Drainable", "Serializable" ] 8989983Sstever@gmail.com eventq_index = Param.UInt32(Parent.eventq_index, "Event Queue Index") 8999342SAndreas.Sandberg@arm.com 9008597Ssteve.reinhardt@amd.com @classmethod 9018597Ssteve.reinhardt@amd.com def export_method_swig_predecls(cls, code): 9028597Ssteve.reinhardt@amd.com code(''' 9038597Ssteve.reinhardt@amd.com%include <std_string.i> 9049342SAndreas.Sandberg@arm.com 9059342SAndreas.Sandberg@arm.com%import "python/swig/drain.i" 9069345SAndreas.Sandberg@ARM.com%import "python/swig/serialize.i" 9078597Ssteve.reinhardt@amd.com''') 9088597Ssteve.reinhardt@amd.com 9098597Ssteve.reinhardt@amd.com @classmethod 9108597Ssteve.reinhardt@amd.com def export_methods(cls, code): 9118597Ssteve.reinhardt@amd.com code(''' 9128597Ssteve.reinhardt@amd.com void init(); 91310905Sandreas.sandberg@arm.com void loadState(CheckpointIn &cp); 9148597Ssteve.reinhardt@amd.com void initState(); 91510911Sandreas.sandberg@arm.com void memInvalidate(); 91610911Sandreas.sandberg@arm.com void memWriteback(); 9178597Ssteve.reinhardt@amd.com void regStats(); 9188597Ssteve.reinhardt@amd.com void resetStats(); 91910023Smatt.horsnell@ARM.com void regProbePoints(); 92010023Smatt.horsnell@ARM.com void regProbeListeners(); 9218597Ssteve.reinhardt@amd.com void startup(); 9228597Ssteve.reinhardt@amd.com''') 9238597Ssteve.reinhardt@amd.com 92410267SGeoffrey.Blake@arm.com # Returns a dict of all the option strings that can be 92510267SGeoffrey.Blake@arm.com # generated as command line options for this simobject instance 92610267SGeoffrey.Blake@arm.com # by tracing all reachable params in the top level instance and 92710267SGeoffrey.Blake@arm.com # any children it contains. 92810267SGeoffrey.Blake@arm.com def enumerateParams(self, flags_dict = {}, 92910267SGeoffrey.Blake@arm.com cmd_line_str = "", access_str = ""): 93010267SGeoffrey.Blake@arm.com if hasattr(self, "_paramEnumed"): 93110267SGeoffrey.Blake@arm.com print "Cycle detected enumerating params" 93210267SGeoffrey.Blake@arm.com else: 93310267SGeoffrey.Blake@arm.com self._paramEnumed = True 93410267SGeoffrey.Blake@arm.com # Scan the children first to pick up all the objects in this SimObj 93510267SGeoffrey.Blake@arm.com for keys in self._children: 93610267SGeoffrey.Blake@arm.com child = self._children[keys] 93710267SGeoffrey.Blake@arm.com next_cmdline_str = cmd_line_str + keys 93810267SGeoffrey.Blake@arm.com next_access_str = access_str + keys 93910267SGeoffrey.Blake@arm.com if not isSimObjectVector(child): 94010267SGeoffrey.Blake@arm.com next_cmdline_str = next_cmdline_str + "." 94110267SGeoffrey.Blake@arm.com next_access_str = next_access_str + "." 94210267SGeoffrey.Blake@arm.com flags_dict = child.enumerateParams(flags_dict, 94310267SGeoffrey.Blake@arm.com next_cmdline_str, 94410267SGeoffrey.Blake@arm.com next_access_str) 94510267SGeoffrey.Blake@arm.com 94610267SGeoffrey.Blake@arm.com # Go through the simple params in the simobject in this level 94710267SGeoffrey.Blake@arm.com # of the simobject hierarchy and save information about the 94810267SGeoffrey.Blake@arm.com # parameter to be used for generating and processing command line 94910267SGeoffrey.Blake@arm.com # options to the simulator to set these parameters. 95010267SGeoffrey.Blake@arm.com for keys,values in self._params.items(): 95110267SGeoffrey.Blake@arm.com if values.isCmdLineSettable(): 95210267SGeoffrey.Blake@arm.com type_str = '' 95310267SGeoffrey.Blake@arm.com ex_str = values.example_str() 95410267SGeoffrey.Blake@arm.com ptype = None 95510267SGeoffrey.Blake@arm.com if isinstance(values, VectorParamDesc): 95610267SGeoffrey.Blake@arm.com type_str = 'Vector_%s' % values.ptype_str 95710267SGeoffrey.Blake@arm.com ptype = values 95810267SGeoffrey.Blake@arm.com else: 95910267SGeoffrey.Blake@arm.com type_str = '%s' % values.ptype_str 96010267SGeoffrey.Blake@arm.com ptype = values.ptype 96110267SGeoffrey.Blake@arm.com 96210267SGeoffrey.Blake@arm.com if keys in self._hr_values\ 96310267SGeoffrey.Blake@arm.com and keys in self._values\ 96410267SGeoffrey.Blake@arm.com and not isinstance(self._values[keys], m5.proxy.BaseProxy): 96510267SGeoffrey.Blake@arm.com cmd_str = cmd_line_str + keys 96610267SGeoffrey.Blake@arm.com acc_str = access_str + keys 96710267SGeoffrey.Blake@arm.com flags_dict[cmd_str] = ParamInfo(ptype, 96810267SGeoffrey.Blake@arm.com self._params[keys].desc, type_str, ex_str, 96910267SGeoffrey.Blake@arm.com values.pretty_print(self._hr_values[keys]), 97010267SGeoffrey.Blake@arm.com acc_str) 97110267SGeoffrey.Blake@arm.com elif not keys in self._hr_values\ 97210267SGeoffrey.Blake@arm.com and not keys in self._values: 97310267SGeoffrey.Blake@arm.com # Empty param 97410267SGeoffrey.Blake@arm.com cmd_str = cmd_line_str + keys 97510267SGeoffrey.Blake@arm.com acc_str = access_str + keys 97610267SGeoffrey.Blake@arm.com flags_dict[cmd_str] = ParamInfo(ptype, 97710267SGeoffrey.Blake@arm.com self._params[keys].desc, 97810267SGeoffrey.Blake@arm.com type_str, ex_str, '', acc_str) 97910267SGeoffrey.Blake@arm.com 98010267SGeoffrey.Blake@arm.com return flags_dict 98110267SGeoffrey.Blake@arm.com 9822740SN/A # Initialize new instance. For objects with SimObject-valued 9832740SN/A # children, we need to recursively clone the classes represented 9842740SN/A # by those param values as well in a consistent "deep copy"-style 9852740SN/A # fashion. That is, we want to make sure that each instance is 9862740SN/A # cloned only once, and that if there are multiple references to 9872740SN/A # the same original object, we end up with the corresponding 9882740SN/A # cloned references all pointing to the same cloned instance. 9892740SN/A def __init__(self, **kwargs): 9902740SN/A ancestor = kwargs.get('_ancestor') 9912740SN/A memo_dict = kwargs.get('_memo') 9922740SN/A if memo_dict is None: 9932740SN/A # prepare to memoize any recursively instantiated objects 9942740SN/A memo_dict = {} 9952740SN/A elif ancestor: 9962740SN/A # memoize me now to avoid problems with recursive calls 9972740SN/A memo_dict[ancestor] = self 9982711SN/A 9992740SN/A if not ancestor: 10002740SN/A ancestor = self.__class__ 10012740SN/A ancestor._instantiated = True 10022711SN/A 10032740SN/A # initialize required attributes 10042740SN/A self._parent = None 10057528Ssteve.reinhardt@amd.com self._name = None 10062740SN/A self._ccObject = None # pointer to C++ object 10074762Snate@binkert.org self._ccParams = None 10082740SN/A self._instantiated = False # really "cloned" 10092712SN/A 10108321Ssteve.reinhardt@amd.com # Clone children specified at class level. No need for a 10118321Ssteve.reinhardt@amd.com # multidict here since we will be cloning everything. 10128321Ssteve.reinhardt@amd.com # Do children before parameter values so that children that 10138321Ssteve.reinhardt@amd.com # are also param values get cloned properly. 10148321Ssteve.reinhardt@amd.com self._children = {} 10158321Ssteve.reinhardt@amd.com for key,val in ancestor._children.iteritems(): 10168321Ssteve.reinhardt@amd.com self.add_child(key, val(_memo=memo_dict)) 10178321Ssteve.reinhardt@amd.com 10182711SN/A # Inherit parameter values from class using multidict so 10197528Ssteve.reinhardt@amd.com # individual value settings can be overridden but we still 10207528Ssteve.reinhardt@amd.com # inherit late changes to non-overridden class values. 10212740SN/A self._values = multidict(ancestor._values) 102210267SGeoffrey.Blake@arm.com self._hr_values = multidict(ancestor._hr_values) 10232740SN/A # clone SimObject-valued parameters 10242740SN/A for key,val in ancestor._values.iteritems(): 10257528Ssteve.reinhardt@amd.com val = tryAsSimObjectOrVector(val) 10267528Ssteve.reinhardt@amd.com if val is not None: 10277528Ssteve.reinhardt@amd.com self._values[key] = val(_memo=memo_dict) 10287528Ssteve.reinhardt@amd.com 10292740SN/A # clone port references. no need to use a multidict here 10302740SN/A # since we will be creating new references for all ports. 10313105Sstever@eecs.umich.edu self._port_refs = {} 10323105Sstever@eecs.umich.edu for key,val in ancestor._port_refs.iteritems(): 10333105Sstever@eecs.umich.edu self._port_refs[key] = val.clone(self, memo_dict) 10341692SN/A # apply attribute assignments from keyword args, if any 10351692SN/A for key,val in kwargs.iteritems(): 10361692SN/A setattr(self, key, val) 1037679SN/A 10382740SN/A # "Clone" the current instance by creating another instance of 10392740SN/A # this instance's class, but that inherits its parameter values 10402740SN/A # and port mappings from the current instance. If we're in a 10412740SN/A # "deep copy" recursive clone, check the _memo dict to see if 10422740SN/A # we've already cloned this instance. 10431692SN/A def __call__(self, **kwargs): 10442740SN/A memo_dict = kwargs.get('_memo') 10452740SN/A if memo_dict is None: 10462740SN/A # no memo_dict: must be top-level clone operation. 10472740SN/A # this is only allowed at the root of a hierarchy 10482740SN/A if self._parent: 10492740SN/A raise RuntimeError, "attempt to clone object %s " \ 10502740SN/A "not at the root of a tree (parent = %s)" \ 10512740SN/A % (self, self._parent) 10522740SN/A # create a new dict and use that. 10532740SN/A memo_dict = {} 10542740SN/A kwargs['_memo'] = memo_dict 10552740SN/A elif memo_dict.has_key(self): 10562740SN/A # clone already done & memoized 10572740SN/A return memo_dict[self] 10582740SN/A return self.__class__(_ancestor = self, **kwargs) 10591343SN/A 10603105Sstever@eecs.umich.edu def _get_port_ref(self, attr): 10613105Sstever@eecs.umich.edu # Return reference that can be assigned to another port 10623105Sstever@eecs.umich.edu # via __setattr__. There is only ever one reference 10633105Sstever@eecs.umich.edu # object per port, but we create them lazily here. 10643105Sstever@eecs.umich.edu ref = self._port_refs.get(attr) 10659940SGeoffrey.Blake@arm.com if ref == None: 10663105Sstever@eecs.umich.edu ref = self._ports[attr].makeRef(self) 10673105Sstever@eecs.umich.edu self._port_refs[attr] = ref 10683105Sstever@eecs.umich.edu return ref 10693105Sstever@eecs.umich.edu 10701692SN/A def __getattr__(self, attr): 10712738SN/A if self._ports.has_key(attr): 10723105Sstever@eecs.umich.edu return self._get_port_ref(attr) 10732738SN/A 10741692SN/A if self._values.has_key(attr): 10751692SN/A return self._values[attr] 10761427SN/A 10777528Ssteve.reinhardt@amd.com if self._children.has_key(attr): 10787528Ssteve.reinhardt@amd.com return self._children[attr] 10797528Ssteve.reinhardt@amd.com 10807500Ssteve.reinhardt@amd.com # If the attribute exists on the C++ object, transparently 10817500Ssteve.reinhardt@amd.com # forward the reference there. This is typically used for 10827500Ssteve.reinhardt@amd.com # SWIG-wrapped methods such as init(), regStats(), 10839195SAndreas.Sandberg@arm.com # resetStats(), startup(), drain(), and 10847527Ssteve.reinhardt@amd.com # resume(). 10857500Ssteve.reinhardt@amd.com if self._ccObject and hasattr(self._ccObject, attr): 10867500Ssteve.reinhardt@amd.com return getattr(self._ccObject, attr) 10877500Ssteve.reinhardt@amd.com 108810002Ssteve.reinhardt@amd.com err_string = "object '%s' has no attribute '%s'" \ 10891692SN/A % (self.__class__.__name__, attr) 10901427SN/A 109110002Ssteve.reinhardt@amd.com if not self._ccObject: 109210002Ssteve.reinhardt@amd.com err_string += "\n (C++ object is not yet constructed," \ 109310002Ssteve.reinhardt@amd.com " so wrapped C++ methods are unavailable.)" 109410002Ssteve.reinhardt@amd.com 109510002Ssteve.reinhardt@amd.com raise AttributeError, err_string 109610002Ssteve.reinhardt@amd.com 10971692SN/A # Set attribute (called on foo.attr = value when foo is an 10981692SN/A # instance of class cls). 10991692SN/A def __setattr__(self, attr, value): 11001692SN/A # normal processing for private attributes 11011692SN/A if attr.startswith('_'): 11021692SN/A object.__setattr__(self, attr, value) 11031692SN/A return 11041427SN/A 11052738SN/A if self._ports.has_key(attr): 11062738SN/A # set up port connection 11073105Sstever@eecs.umich.edu self._get_port_ref(attr).connect(value) 11082738SN/A return 11092738SN/A 11103105Sstever@eecs.umich.edu param = self._params.get(attr) 11111692SN/A if param: 11121310SN/A try: 111310267SGeoffrey.Blake@arm.com hr_value = value 11141692SN/A value = param.convert(value) 11151587SN/A except Exception, e: 11161692SN/A msg = "%s\nError setting param %s.%s to %s\n" % \ 11171692SN/A (e, self.__class__.__name__, attr, value) 11181605SN/A e.args = (msg, ) 11191605SN/A raise 11207528Ssteve.reinhardt@amd.com self._values[attr] = value 11218321Ssteve.reinhardt@amd.com # implicitly parent unparented objects assigned as params 11228321Ssteve.reinhardt@amd.com if isSimObjectOrVector(value) and not value.has_parent(): 11238321Ssteve.reinhardt@amd.com self.add_child(attr, value) 112410267SGeoffrey.Blake@arm.com # set the human-readable value dict if this is a param 112510267SGeoffrey.Blake@arm.com # with a literal value and is not being set as an object 112610267SGeoffrey.Blake@arm.com # or proxy. 112710267SGeoffrey.Blake@arm.com if not (isSimObjectOrVector(value) or\ 112810267SGeoffrey.Blake@arm.com isinstance(value, m5.proxy.BaseProxy)): 112910267SGeoffrey.Blake@arm.com self._hr_values[attr] = hr_value 113010267SGeoffrey.Blake@arm.com 11313105Sstever@eecs.umich.edu return 11321310SN/A 11337528Ssteve.reinhardt@amd.com # if RHS is a SimObject, it's an implicit child assignment 11343105Sstever@eecs.umich.edu if isSimObjectOrSequence(value): 11357528Ssteve.reinhardt@amd.com self.add_child(attr, value) 11363105Sstever@eecs.umich.edu return 11371693SN/A 11383105Sstever@eecs.umich.edu # no valid assignment... raise exception 11393105Sstever@eecs.umich.edu raise AttributeError, "Class %s has no parameter %s" \ 11403105Sstever@eecs.umich.edu % (self.__class__.__name__, attr) 11411310SN/A 11421310SN/A 11431692SN/A # this hack allows tacking a '[0]' onto parameters that may or may 11441692SN/A # not be vectors, and always getting the first element (e.g. cpus) 11451692SN/A def __getitem__(self, key): 11461692SN/A if key == 0: 11471692SN/A return self 114810267SGeoffrey.Blake@arm.com raise IndexError, "Non-zero index '%s' to SimObject" % key 114910267SGeoffrey.Blake@arm.com 115010267SGeoffrey.Blake@arm.com # this hack allows us to iterate over a SimObject that may 115110267SGeoffrey.Blake@arm.com # not be a vector, so we can call a loop over it and get just one 115210267SGeoffrey.Blake@arm.com # element. 115310267SGeoffrey.Blake@arm.com def __len__(self): 115410267SGeoffrey.Blake@arm.com return 1 11551310SN/A 11567528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 11577528Ssteve.reinhardt@amd.com def clear_parent(self, old_parent): 11587528Ssteve.reinhardt@amd.com assert self._parent is old_parent 11597528Ssteve.reinhardt@amd.com self._parent = None 11607528Ssteve.reinhardt@amd.com 11617528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 11627528Ssteve.reinhardt@amd.com def set_parent(self, parent, name): 11637528Ssteve.reinhardt@amd.com self._parent = parent 11647528Ssteve.reinhardt@amd.com self._name = name 11657528Ssteve.reinhardt@amd.com 11669953Sgeoffrey.blake@arm.com # Return parent object of this SimObject, not implemented by SimObjectVector 11679953Sgeoffrey.blake@arm.com # because the elements in a SimObjectVector may not share the same parent 11689953Sgeoffrey.blake@arm.com def get_parent(self): 11699953Sgeoffrey.blake@arm.com return self._parent 11709953Sgeoffrey.blake@arm.com 11717528Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 11727528Ssteve.reinhardt@amd.com def get_name(self): 11737528Ssteve.reinhardt@amd.com return self._name 11747528Ssteve.reinhardt@amd.com 11758321Ssteve.reinhardt@amd.com # Also implemented by SimObjectVector 11768321Ssteve.reinhardt@amd.com def has_parent(self): 11778321Ssteve.reinhardt@amd.com return self._parent is not None 11787528Ssteve.reinhardt@amd.com 11797742Sgblack@eecs.umich.edu # clear out child with given name. This code is not likely to be exercised. 11807742Sgblack@eecs.umich.edu # See comment in add_child. 11811693SN/A def clear_child(self, name): 11821693SN/A child = self._children[name] 11837528Ssteve.reinhardt@amd.com child.clear_parent(self) 11841693SN/A del self._children[name] 11851693SN/A 11867528Ssteve.reinhardt@amd.com # Add a new child to this object. 11877528Ssteve.reinhardt@amd.com def add_child(self, name, child): 11887528Ssteve.reinhardt@amd.com child = coerceSimObjectOrVector(child) 11898321Ssteve.reinhardt@amd.com if child.has_parent(): 11909528Ssascha.bischoff@arm.com warn("add_child('%s'): child '%s' already has parent", name, 11919528Ssascha.bischoff@arm.com child.get_name()) 11927528Ssteve.reinhardt@amd.com if self._children.has_key(name): 11937742Sgblack@eecs.umich.edu # This code path had an undiscovered bug that would make it fail 11947742Sgblack@eecs.umich.edu # at runtime. It had been here for a long time and was only 11957742Sgblack@eecs.umich.edu # exposed by a buggy script. Changes here will probably not be 11967742Sgblack@eecs.umich.edu # exercised without specialized testing. 11977738Sgblack@eecs.umich.edu self.clear_child(name) 11987528Ssteve.reinhardt@amd.com child.set_parent(self, name) 11997528Ssteve.reinhardt@amd.com self._children[name] = child 12001310SN/A 12017528Ssteve.reinhardt@amd.com # Take SimObject-valued parameters that haven't been explicitly 12027528Ssteve.reinhardt@amd.com # assigned as children and make them children of the object that 12037528Ssteve.reinhardt@amd.com # they were assigned to as a parameter value. This guarantees 12047528Ssteve.reinhardt@amd.com # that when we instantiate all the parameter objects we're still 12057528Ssteve.reinhardt@amd.com # inside the configuration hierarchy. 12067528Ssteve.reinhardt@amd.com def adoptOrphanParams(self): 12077528Ssteve.reinhardt@amd.com for key,val in self._values.iteritems(): 12087528Ssteve.reinhardt@amd.com if not isSimObjectVector(val) and isSimObjectSequence(val): 12097528Ssteve.reinhardt@amd.com # need to convert raw SimObject sequences to 12108321Ssteve.reinhardt@amd.com # SimObjectVector class so we can call has_parent() 12117528Ssteve.reinhardt@amd.com val = SimObjectVector(val) 12127528Ssteve.reinhardt@amd.com self._values[key] = val 12138321Ssteve.reinhardt@amd.com if isSimObjectOrVector(val) and not val.has_parent(): 12149528Ssascha.bischoff@arm.com warn("%s adopting orphan SimObject param '%s'", self, key) 12157528Ssteve.reinhardt@amd.com self.add_child(key, val) 12163105Sstever@eecs.umich.edu 12171692SN/A def path(self): 12182740SN/A if not self._parent: 12198321Ssteve.reinhardt@amd.com return '<orphan %s>' % self.__class__ 122011231Sandreas.sandberg@arm.com elif isinstance(self._parent, MetaSimObject): 122111231Sandreas.sandberg@arm.com return str(self.__class__) 122211231Sandreas.sandberg@arm.com 12231692SN/A ppath = self._parent.path() 12241692SN/A if ppath == 'root': 12251692SN/A return self._name 12261692SN/A return ppath + "." + self._name 12271310SN/A 12281692SN/A def __str__(self): 12291692SN/A return self.path() 12301310SN/A 123110380SAndrew.Bardsley@arm.com def config_value(self): 123210380SAndrew.Bardsley@arm.com return self.path() 123310380SAndrew.Bardsley@arm.com 12341692SN/A def ini_str(self): 12351692SN/A return self.path() 12361310SN/A 12371692SN/A def find_any(self, ptype): 12381692SN/A if isinstance(self, ptype): 12391692SN/A return self, True 12401310SN/A 12411692SN/A found_obj = None 12421692SN/A for child in self._children.itervalues(): 124310195SGeoffrey.Blake@arm.com visited = False 124410195SGeoffrey.Blake@arm.com if hasattr(child, '_visited'): 124510195SGeoffrey.Blake@arm.com visited = getattr(child, '_visited') 124610195SGeoffrey.Blake@arm.com 124710195SGeoffrey.Blake@arm.com if isinstance(child, ptype) and not visited: 12481692SN/A if found_obj != None and child != found_obj: 12491692SN/A raise AttributeError, \ 12501692SN/A 'parent.any matched more than one: %s %s' % \ 12511814SN/A (found_obj.path, child.path) 12521692SN/A found_obj = child 12531692SN/A # search param space 12541692SN/A for pname,pdesc in self._params.iteritems(): 12551692SN/A if issubclass(pdesc.ptype, ptype): 12561692SN/A match_obj = self._values[pname] 12571692SN/A if found_obj != None and found_obj != match_obj: 12581692SN/A raise AttributeError, \ 12595952Ssaidi@eecs.umich.edu 'parent.any matched more than one: %s and %s' % (found_obj.path, match_obj.path) 12601692SN/A found_obj = match_obj 12611692SN/A return found_obj, found_obj != None 12621692SN/A 12638459SAli.Saidi@ARM.com def find_all(self, ptype): 12648459SAli.Saidi@ARM.com all = {} 12658459SAli.Saidi@ARM.com # search children 12668459SAli.Saidi@ARM.com for child in self._children.itervalues(): 12679410Sandreas.hansson@arm.com # a child could be a list, so ensure we visit each item 12689410Sandreas.hansson@arm.com if isinstance(child, list): 12699410Sandreas.hansson@arm.com children = child 12709410Sandreas.hansson@arm.com else: 12719410Sandreas.hansson@arm.com children = [child] 12729410Sandreas.hansson@arm.com 12739410Sandreas.hansson@arm.com for child in children: 12749410Sandreas.hansson@arm.com if isinstance(child, ptype) and not isproxy(child) and \ 12759410Sandreas.hansson@arm.com not isNullPointer(child): 12769410Sandreas.hansson@arm.com all[child] = True 12779410Sandreas.hansson@arm.com if isSimObject(child): 12789410Sandreas.hansson@arm.com # also add results from the child itself 12799410Sandreas.hansson@arm.com child_all, done = child.find_all(ptype) 12809410Sandreas.hansson@arm.com all.update(dict(zip(child_all, [done] * len(child_all)))) 12818459SAli.Saidi@ARM.com # search param space 12828459SAli.Saidi@ARM.com for pname,pdesc in self._params.iteritems(): 12838459SAli.Saidi@ARM.com if issubclass(pdesc.ptype, ptype): 12848459SAli.Saidi@ARM.com match_obj = self._values[pname] 12858459SAli.Saidi@ARM.com if not isproxy(match_obj) and not isNullPointer(match_obj): 12868459SAli.Saidi@ARM.com all[match_obj] = True 128710532Sandreas.hansson@arm.com # Also make sure to sort the keys based on the objects' path to 128810532Sandreas.hansson@arm.com # ensure that the order is the same on all hosts 128910532Sandreas.hansson@arm.com return sorted(all.keys(), key = lambda o: o.path()), True 12908459SAli.Saidi@ARM.com 12911815SN/A def unproxy(self, base): 12921815SN/A return self 12931815SN/A 12947527Ssteve.reinhardt@amd.com def unproxyParams(self): 12953105Sstever@eecs.umich.edu for param in self._params.iterkeys(): 12963105Sstever@eecs.umich.edu value = self._values.get(param) 12976654Snate@binkert.org if value != None and isproxy(value): 12983105Sstever@eecs.umich.edu try: 12993105Sstever@eecs.umich.edu value = value.unproxy(self) 13003105Sstever@eecs.umich.edu except: 13013105Sstever@eecs.umich.edu print "Error in unproxying param '%s' of %s" % \ 13023105Sstever@eecs.umich.edu (param, self.path()) 13033105Sstever@eecs.umich.edu raise 13043105Sstever@eecs.umich.edu setattr(self, param, value) 13053105Sstever@eecs.umich.edu 13063107Sstever@eecs.umich.edu # Unproxy ports in sorted order so that 'append' operations on 13073107Sstever@eecs.umich.edu # vector ports are done in a deterministic fashion. 13083107Sstever@eecs.umich.edu port_names = self._ports.keys() 13093107Sstever@eecs.umich.edu port_names.sort() 13103107Sstever@eecs.umich.edu for port_name in port_names: 13113105Sstever@eecs.umich.edu port = self._port_refs.get(port_name) 13123105Sstever@eecs.umich.edu if port != None: 13133105Sstever@eecs.umich.edu port.unproxy(self) 13143105Sstever@eecs.umich.edu 13155037Smilesck@eecs.umich.edu def print_ini(self, ini_file): 13165543Ssaidi@eecs.umich.edu print >>ini_file, '[' + self.path() + ']' # .ini section header 13171692SN/A 13182738SN/A instanceDict[self.path()] = self 13192738SN/A 13204081Sbinkertn@umich.edu if hasattr(self, 'type'): 13215037Smilesck@eecs.umich.edu print >>ini_file, 'type=%s' % self.type 13221692SN/A 13238664SAli.Saidi@ARM.com if len(self._children.keys()): 13247528Ssteve.reinhardt@amd.com print >>ini_file, 'children=%s' % \ 13258664SAli.Saidi@ARM.com ' '.join(self._children[n].get_name() \ 13268664SAli.Saidi@ARM.com for n in sorted(self._children.keys())) 13271692SN/A 13288664SAli.Saidi@ARM.com for param in sorted(self._params.keys()): 13293105Sstever@eecs.umich.edu value = self._values.get(param) 13301692SN/A if value != None: 13315037Smilesck@eecs.umich.edu print >>ini_file, '%s=%s' % (param, 13325037Smilesck@eecs.umich.edu self._values[param].ini_str()) 13331692SN/A 13348664SAli.Saidi@ARM.com for port_name in sorted(self._ports.keys()): 13353105Sstever@eecs.umich.edu port = self._port_refs.get(port_name, None) 13363105Sstever@eecs.umich.edu if port != None: 13375037Smilesck@eecs.umich.edu print >>ini_file, '%s=%s' % (port_name, port.ini_str()) 13383103Sstever@eecs.umich.edu 13395543Ssaidi@eecs.umich.edu print >>ini_file # blank line between objects 13401692SN/A 13418664SAli.Saidi@ARM.com # generate a tree of dictionaries expressing all the parameters in the 13428664SAli.Saidi@ARM.com # instantiated system for use by scripts that want to do power, thermal 13438664SAli.Saidi@ARM.com # visualization, and other similar tasks 13448664SAli.Saidi@ARM.com def get_config_as_dict(self): 13458664SAli.Saidi@ARM.com d = attrdict() 13468664SAli.Saidi@ARM.com if hasattr(self, 'type'): 13478664SAli.Saidi@ARM.com d.type = self.type 13488664SAli.Saidi@ARM.com if hasattr(self, 'cxx_class'): 13498664SAli.Saidi@ARM.com d.cxx_class = self.cxx_class 13509017Sandreas.hansson@arm.com # Add the name and path of this object to be able to link to 13519017Sandreas.hansson@arm.com # the stats 13529017Sandreas.hansson@arm.com d.name = self.get_name() 13539017Sandreas.hansson@arm.com d.path = self.path() 13548664SAli.Saidi@ARM.com 13558664SAli.Saidi@ARM.com for param in sorted(self._params.keys()): 13568664SAli.Saidi@ARM.com value = self._values.get(param) 13578848Ssteve.reinhardt@amd.com if value != None: 135810380SAndrew.Bardsley@arm.com d[param] = value.config_value() 13598664SAli.Saidi@ARM.com 13608664SAli.Saidi@ARM.com for n in sorted(self._children.keys()): 13619017Sandreas.hansson@arm.com child = self._children[n] 13629017Sandreas.hansson@arm.com # Use the name of the attribute (and not get_name()) as 13639017Sandreas.hansson@arm.com # the key in the JSON dictionary to capture the hierarchy 13649017Sandreas.hansson@arm.com # in the Python code that assembled this system 13659017Sandreas.hansson@arm.com d[n] = child.get_config_as_dict() 13668664SAli.Saidi@ARM.com 13678664SAli.Saidi@ARM.com for port_name in sorted(self._ports.keys()): 13688664SAli.Saidi@ARM.com port = self._port_refs.get(port_name, None) 13698664SAli.Saidi@ARM.com if port != None: 13709017Sandreas.hansson@arm.com # Represent each port with a dictionary containing the 13719017Sandreas.hansson@arm.com # prominent attributes 13729017Sandreas.hansson@arm.com d[port_name] = port.get_config_as_dict() 13738664SAli.Saidi@ARM.com 13748664SAli.Saidi@ARM.com return d 13758664SAli.Saidi@ARM.com 13764762Snate@binkert.org def getCCParams(self): 13774762Snate@binkert.org if self._ccParams: 13784762Snate@binkert.org return self._ccParams 13794762Snate@binkert.org 13807677Snate@binkert.org cc_params_struct = getattr(m5.internal.params, '%sParams' % self.type) 13814762Snate@binkert.org cc_params = cc_params_struct() 13825488Snate@binkert.org cc_params.pyobj = self 13834762Snate@binkert.org cc_params.name = str(self) 13844762Snate@binkert.org 13854762Snate@binkert.org param_names = self._params.keys() 13864762Snate@binkert.org param_names.sort() 13874762Snate@binkert.org for param in param_names: 13884762Snate@binkert.org value = self._values.get(param) 13894762Snate@binkert.org if value is None: 13906654Snate@binkert.org fatal("%s.%s without default or user set value", 13916654Snate@binkert.org self.path(), param) 13924762Snate@binkert.org 13934762Snate@binkert.org value = value.getValue() 13944762Snate@binkert.org if isinstance(self._params[param], VectorParamDesc): 13954762Snate@binkert.org assert isinstance(value, list) 13964762Snate@binkert.org vec = getattr(cc_params, param) 13974762Snate@binkert.org assert not len(vec) 13984762Snate@binkert.org for v in value: 13994762Snate@binkert.org vec.append(v) 14004762Snate@binkert.org else: 14014762Snate@binkert.org setattr(cc_params, param, value) 14024762Snate@binkert.org 14034762Snate@binkert.org port_names = self._ports.keys() 14044762Snate@binkert.org port_names.sort() 14054762Snate@binkert.org for port_name in port_names: 14064762Snate@binkert.org port = self._port_refs.get(port_name, None) 14078912Sandreas.hansson@arm.com if port != None: 14088912Sandreas.hansson@arm.com port_count = len(port) 14098912Sandreas.hansson@arm.com else: 14108912Sandreas.hansson@arm.com port_count = 0 14118900Sandreas.hansson@arm.com setattr(cc_params, 'port_' + port_name + '_connection_count', 14128912Sandreas.hansson@arm.com port_count) 14134762Snate@binkert.org self._ccParams = cc_params 14144762Snate@binkert.org return self._ccParams 14152738SN/A 14162740SN/A # Get C++ object corresponding to this object, calling C++ if 14172740SN/A # necessary to construct it. Does *not* recursively create 14182740SN/A # children. 14192740SN/A def getCCObject(self): 14202740SN/A if not self._ccObject: 14217526Ssteve.reinhardt@amd.com # Make sure this object is in the configuration hierarchy 14227526Ssteve.reinhardt@amd.com if not self._parent and not isRoot(self): 14237526Ssteve.reinhardt@amd.com raise RuntimeError, "Attempt to instantiate orphan node" 14247526Ssteve.reinhardt@amd.com # Cycles in the configuration hierarchy are not supported. This 14255244Sgblack@eecs.umich.edu # will catch the resulting recursion and stop. 14265244Sgblack@eecs.umich.edu self._ccObject = -1 142710267SGeoffrey.Blake@arm.com if not self.abstract: 142810267SGeoffrey.Blake@arm.com params = self.getCCParams() 142910267SGeoffrey.Blake@arm.com self._ccObject = params.create() 14302740SN/A elif self._ccObject == -1: 14317526Ssteve.reinhardt@amd.com raise RuntimeError, "%s: Cycle found in configuration hierarchy." \ 14322740SN/A % self.path() 14332740SN/A return self._ccObject 14342740SN/A 14357527Ssteve.reinhardt@amd.com def descendants(self): 14367527Ssteve.reinhardt@amd.com yield self 143710532Sandreas.hansson@arm.com # The order of the dict is implementation dependent, so sort 143810532Sandreas.hansson@arm.com # it based on the key (name) to ensure the order is the same 143910532Sandreas.hansson@arm.com # on all hosts 144010532Sandreas.hansson@arm.com for (name, child) in sorted(self._children.iteritems()): 14417527Ssteve.reinhardt@amd.com for obj in child.descendants(): 14427527Ssteve.reinhardt@amd.com yield obj 14437527Ssteve.reinhardt@amd.com 14447527Ssteve.reinhardt@amd.com # Call C++ to create C++ object corresponding to this object 14454762Snate@binkert.org def createCCObject(self): 14464762Snate@binkert.org self.getCCParams() 14474762Snate@binkert.org self.getCCObject() # force creation 14484762Snate@binkert.org 14494762Snate@binkert.org def getValue(self): 14504762Snate@binkert.org return self.getCCObject() 14514762Snate@binkert.org 14522738SN/A # Create C++ port connections corresponding to the connections in 14537527Ssteve.reinhardt@amd.com # _port_refs 14542738SN/A def connectPorts(self): 145510532Sandreas.hansson@arm.com # Sort the ports based on their attribute name to ensure the 145610532Sandreas.hansson@arm.com # order is the same on all hosts 145710532Sandreas.hansson@arm.com for (attr, portRef) in sorted(self._port_refs.iteritems()): 14583105Sstever@eecs.umich.edu portRef.ccConnect() 14592797SN/A 14603101Sstever@eecs.umich.edu# Function to provide to C++ so it can look up instances based on paths 14613101Sstever@eecs.umich.edudef resolveSimObject(name): 14623101Sstever@eecs.umich.edu obj = instanceDict[name] 14633101Sstever@eecs.umich.edu return obj.getCCObject() 1464679SN/A 14656654Snate@binkert.orgdef isSimObject(value): 14666654Snate@binkert.org return isinstance(value, SimObject) 14676654Snate@binkert.org 14686654Snate@binkert.orgdef isSimObjectClass(value): 14696654Snate@binkert.org return issubclass(value, SimObject) 14706654Snate@binkert.org 14717528Ssteve.reinhardt@amd.comdef isSimObjectVector(value): 14727528Ssteve.reinhardt@amd.com return isinstance(value, SimObjectVector) 14737528Ssteve.reinhardt@amd.com 14746654Snate@binkert.orgdef isSimObjectSequence(value): 14756654Snate@binkert.org if not isinstance(value, (list, tuple)) or len(value) == 0: 14766654Snate@binkert.org return False 14776654Snate@binkert.org 14786654Snate@binkert.org for val in value: 14796654Snate@binkert.org if not isNullPointer(val) and not isSimObject(val): 14806654Snate@binkert.org return False 14816654Snate@binkert.org 14826654Snate@binkert.org return True 14836654Snate@binkert.org 14846654Snate@binkert.orgdef isSimObjectOrSequence(value): 14856654Snate@binkert.org return isSimObject(value) or isSimObjectSequence(value) 14866654Snate@binkert.org 14877526Ssteve.reinhardt@amd.comdef isRoot(obj): 14887526Ssteve.reinhardt@amd.com from m5.objects import Root 14897526Ssteve.reinhardt@amd.com return obj and obj is Root.getInstance() 14907526Ssteve.reinhardt@amd.com 14917528Ssteve.reinhardt@amd.comdef isSimObjectOrVector(value): 14927528Ssteve.reinhardt@amd.com return isSimObject(value) or isSimObjectVector(value) 14937528Ssteve.reinhardt@amd.com 14947528Ssteve.reinhardt@amd.comdef tryAsSimObjectOrVector(value): 14957528Ssteve.reinhardt@amd.com if isSimObjectOrVector(value): 14967528Ssteve.reinhardt@amd.com return value 14977528Ssteve.reinhardt@amd.com if isSimObjectSequence(value): 14987528Ssteve.reinhardt@amd.com return SimObjectVector(value) 14997528Ssteve.reinhardt@amd.com return None 15007528Ssteve.reinhardt@amd.com 15017528Ssteve.reinhardt@amd.comdef coerceSimObjectOrVector(value): 15027528Ssteve.reinhardt@amd.com value = tryAsSimObjectOrVector(value) 15037528Ssteve.reinhardt@amd.com if value is None: 15047528Ssteve.reinhardt@amd.com raise TypeError, "SimObject or SimObjectVector expected" 15057528Ssteve.reinhardt@amd.com return value 15067528Ssteve.reinhardt@amd.com 15076654Snate@binkert.orgbaseClasses = allClasses.copy() 15086654Snate@binkert.orgbaseInstances = instanceDict.copy() 15096654Snate@binkert.org 15106654Snate@binkert.orgdef clear(): 15119338SAndreas.Sandberg@arm.com global allClasses, instanceDict, noCxxHeader 15126654Snate@binkert.org 15136654Snate@binkert.org allClasses = baseClasses.copy() 15146654Snate@binkert.org instanceDict = baseInstances.copy() 15159338SAndreas.Sandberg@arm.com noCxxHeader = False 15166654Snate@binkert.org 15171528SN/A# __all__ defines the list of symbols that get exported when 15181528SN/A# 'from config import *' is invoked. Try to keep this reasonably 15191528SN/A# short to avoid polluting other namespaces. 15204762Snate@binkert.org__all__ = [ 'SimObject' ] 1521