Merge zizzer.eecs.umich.edu:/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 * The indices i1 & i2 will be -1 for regular ports, >= 0 for vector ports.
66 */
67 int
68 connectPorts(SimObject *o1, const std::string &name1, int i1,
69 SimObject *o2, const std::string &name2, int i2)
70 {
71 Port *p1 = lookupPort(o1, name1, i1);
72 Port *p2 = lookupPort(o2, name2, i2);
73
74 if (p1 == NULL || p2 == NULL) {
75 warn("connectPorts: port lookup error");
76 return 0;
77 }
78
79 p1->setPeer(p2);
80 p2->setPeer(p1);
81
82 return 1;
83 }
84
85 inline IniFile &
86 inifile()
87 {
88 static IniFile inifile;
89 return inifile;
90 }
91
92 SimObject *
93 createSimObject(const string &name)
94 {
95 return SimObjectClass::createObject(inifile(), name);
96 }
97
98 /**
99 * Pointer to the Python function that maps names to SimObjects.
100 */
101 PyObject *resolveFunc = NULL;
102
103 /**
104 * Convert a pointer to the Python object that SWIG wraps around a C++
105 * SimObject pointer back to the actual C++ pointer. See main.i.
106 */
107 extern "C" SimObject *convertSwigSimObjectPtr(PyObject *);
108
109 SimObject *
110 resolveSimObject(const string &name)
111 {
112 PyObject *pyPtr = PyEval_CallFunction(resolveFunc, "(s)", name.c_str());
113 if (pyPtr == NULL) {
114 PyErr_Print();
115 panic("resolveSimObject: failure on call to Python for %s", name);
116 }
117
118 SimObject *simObj = convertSwigSimObjectPtr(pyPtr);
119 if (simObj == NULL)
120 panic("resolveSimObject: failure on pointer conversion for %s", name);
121
122 return simObj;
123 }
124
125 /**
126 * Load config.ini into C++ database. Exported to Python via SWIG;
127 * invoked from m5.instantiate().
128 */
129 void
130 loadIniFile(PyObject *_resolveFunc)
131 {
132 resolveFunc = _resolveFunc;
133 configStream = simout.find("config.out");
134
135 // The configuration database is now complete; start processing it.
136 inifile().load(simout.resolve("config.ini"));
137 }
138