我是gem5模拟器的新手。我正在阅读文档(http://www.m5sim.org/Configuration_/_Simulation_Scripts),试图了解所有内容是如何实现的。当他们写Python类时,他们会说:
gem5 provides a collection of Python object classes that correspond to its C++ simulation object classes. These Python classes are defined in a Python module called "m5.objects". The Python class definitions for these objects can be found in .py files in src, typically in the same directory as their C++ definitions.
To make the Python classes visible, the configuration file must first import the class definitions from the m5 module
在m5/objects目录中,只有一个文件“\uuu init\uu.py”。代码如下:
from __future__ import print_function
from __future__ import absolute_import
from m5.internal import params
from m5.SimObject import *
try:
modules = __loader__.modules
except NameError:
modules = { }
for module in modules.keys():
if module.startswith('m5.objects.'):
exec("from %s import *" % module)
通常我不使用Python编程,所以这可能就是问题所在,但我还没有完全理解这里发生的事情。在另一篇博文Python's __loader__, what is it?中,他们谈到了加载程序的含义,但我觉得我遗漏了一些东西。任何帮助都将不胜感激。提前谢谢
{}
考虑下面的代码:
这将导致输出:
只要在
PYTHONPATH
中没有名为foo
的python模块Python导入钩子(PEP 302)允许我们自定义
import
的行为。在上面的示例中,模块foo
被称为是由FooImporter
找到并处理的。注意导入程序将创建模块foo
作为sys
的别名。完整的导入器(与我们看到的简化导入器不同)将负责将导入模块的__loader__
属性设置为导入器本身Gem5进口挂钩
回到您的问题,gem5通过其模块化设计使用相同的机制来加载
SimObject
。您可以在src/python/importer.py
找到类名为CodeImporter
的导入器类当模块
m5.object
被导入时CodeImporter
将负责处理导入任务,其中将为导入的模块设置__loader__
属性(在本例中为m5.objects
)。如果您尝试在m5/objects/__init__.py
中打印__loader__
,您将得到如下结果:__loader__.modules
是一个包含gem5维护的SimObjects
的字典,其中每个项都将由来自src/sim/init.cc
的addModule()
调用添加只要一个^ {< CD10>}的C++对应称为{{CD24>}的构造函数,它将被添加到一个列表中,因此GEM5初始化将记住将它添加到^ {CD12>}的实例中。例如,应该能够在注册
Root
对象的构建文件夹中找到Root.py.cc
文件。m5/object/__init__.py
末尾的循环只是通过此机制导入已知的SimObject
的列表我认为这应该足以让某人了解潜在的魔法,并(希望)解决他们的好奇心
相关问题 更多 >
编程相关推荐