Merge zizzer:/bk/newmem
[gem5.git] / src / python / swig / pyobject.cc
1 /*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31 #include <Python.h>
32
33 #include <string>
34
35 #include "base/inifile.hh"
36 #include "base/output.hh"
37 #include "mem/mem_object.hh"
38 #include "mem/port.hh"
39 #include "sim/builder.hh"
40 #include "sim/sim_object.hh"
41
42 using namespace std;
43
44 /**
45 * Look up a MemObject port. Helper function for connectPorts().
46 */
47 Port *
48 lookupPort(SimObject *so, const std::string &name, int i)
49 {
50 MemObject *mo = dynamic_cast<MemObject *>(so);
51 if (mo == NULL) {
52 warn("error casting SimObject %s to MemObject", so->name());
53 return NULL;
54 }
55
56 Port *p = mo->getPort(name, i);
57 if (p == NULL)
58 warn("error looking up port %s on object %s", name, so->name());
59 return p;
60 }
61
62
63 /**
64 * Connect the described MemObject ports. Called from Python via SWIG.
65 */
66 int
67 connectPorts(SimObject *o1, const std::string &name1, int i1,
68 SimObject *o2, const std::string &name2, int i2)
69 {
70 Port *p1 = lookupPort(o1, name1, i1);
71 Port *p2 = lookupPort(o2, name2, i2);
72
73 if (p1 == NULL || p2 == NULL) {
74 warn("connectPorts: port lookup error");
75 return 0;
76 }
77
78 p1->setPeer(p2);
79 p2->setPeer(p1);
80
81 return 1;
82 }
83
84 inline IniFile &
85 inifile()
86 {
87 static IniFile inifile;
88 return inifile;
89 }
90
91 SimObject *
92 createSimObject(const string &name)
93 {
94 return SimObjectClass::createObject(inifile(), name);
95 }
96
97 /**
98 * Pointer to the Python function that maps names to SimObjects.
99 */
100 PyObject *resolveFunc = NULL;
101
102 /**
103 * Convert a pointer to the Python object that SWIG wraps around a C++
104 * SimObject pointer back to the actual C++ pointer. See main.i.
105 */
106 extern "C" SimObject *convertSwigSimObjectPtr(PyObject *);
107
108 SimObject *
109 resolveSimObject(const string &name)
110 {
111 PyObject *pyPtr = PyEval_CallFunction(resolveFunc, "(s)", name.c_str());
112 if (pyPtr == NULL) {
113 PyErr_Print();
114 panic("resolveSimObject: failure on call to Python for %s", name);
115 }
116
117 SimObject *simObj = convertSwigSimObjectPtr(pyPtr);
118 if (simObj == NULL)
119 panic("resolveSimObject: failure on pointer conversion for %s", name);
120
121 return simObj;
122 }
123
124 /**
125 * Load config.ini into C++ database. Exported to Python via SWIG;
126 * invoked from m5.instantiate().
127 */
128 void
129 loadIniFile(PyObject *_resolveFunc)
130 {
131 resolveFunc = _resolveFunc;
132 configStream = simout.find("config.out");
133
134 // The configuration database is now complete; start processing it.
135 inifile().load(simout.resolve("config.ini"));
136 }
137