dev: Move network devices to src/dev/net/
[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 "config/the_isa.hh"
38 #if THE_ISA != NULL_ISA
39 #include "dev/net/etherdevice.hh"
40 #include "dev/net/etherobject.hh"
41 #endif
42 #include "mem/ruby/slicc_interface/AbstractController.hh"
43 #include "mem/mem_object.hh"
44 #include "python/swig/pyobject.hh"
45 #include "sim/full_system.hh"
46 #include "sim/sim_object.hh"
47
48 using namespace std;
49
50 #if THE_ISA != NULL_ISA
51 EtherInt *
52 lookupEthPort(SimObject *so, const std::string &name, int i)
53 {
54 EtherObject *eo = dynamic_cast<EtherObject *>(so);
55 EtherDevice *ed = dynamic_cast<EtherDevice *>(so);
56 if (eo == NULL && ed == NULL) {
57 warn("error casting SimObject %s", so->name());
58 return NULL;
59 }
60
61 EtherInt *p = NULL;
62 if (eo)
63 p = eo->getEthPort(name, i);
64 else
65 p = ed->getEthPort(name, i);
66 return p;
67 }
68 #endif
69
70 /**
71 * Connect the described MemObject ports. Called from Python via SWIG.
72 * The indices i1 & i2 will be -1 for regular ports, >= 0 for vector ports.
73 * SimObject1 is the master, and SimObject2 is the slave
74 */
75 int
76 connectPorts(SimObject *o1, const std::string &name1, int i1,
77 SimObject *o2, const std::string &name2, int i2)
78 {
79 #if THE_ISA != NULL_ISA
80 if (FullSystem) {
81 EtherObject *eo1, *eo2;
82 EtherDevice *ed1, *ed2;
83 eo1 = dynamic_cast<EtherObject*>(o1);
84 ed1 = dynamic_cast<EtherDevice*>(o1);
85 eo2 = dynamic_cast<EtherObject*>(o2);
86 ed2 = dynamic_cast<EtherDevice*>(o2);
87
88 if ((eo1 || ed1) && (eo2 || ed2)) {
89 EtherInt *p1 = lookupEthPort(o1, name1, i1);
90 EtherInt *p2 = lookupEthPort(o2, name2, i2);
91
92 if (p1 != NULL && p2 != NULL) {
93
94 p1->setPeer(p2);
95 p2->setPeer(p1);
96
97 return 1;
98 }
99 }
100 }
101 #endif
102
103 // These could be MessageBuffers from the ruby memory system. If so, they
104 // need not be connected to anything currently.
105 MessageBuffer *mb1, *mb2;
106 mb1 = dynamic_cast<MessageBuffer*>(o1);
107 mb2 = dynamic_cast<MessageBuffer*>(o2);
108
109 if (mb1 || mb2) {
110 // No need to connect anything here currently. MessageBuffer
111 // connections in Python only serve to print the connections in
112 // the config output.
113 // TODO: Add real ports to MessageBuffers and use MemObject connect
114 // code below to bind MessageBuffer senders and receivers
115 return 1;
116 }
117
118 MemObject *mo1, *mo2;
119 mo1 = dynamic_cast<MemObject*>(o1);
120 mo2 = dynamic_cast<MemObject*>(o2);
121
122 if(mo1 == NULL || mo2 == NULL) {
123 panic ("Error casting SimObjects %s and %s to MemObject", o1->name(),
124 o2->name());
125 }
126
127 // generic master/slave port connection
128 BaseMasterPort& masterPort = mo1->getMasterPort(name1, i1);
129 BaseSlavePort& slavePort = mo2->getSlavePort(name2, i2);
130
131 masterPort.bind(slavePort);
132
133 return 1;
134 }
135
136 inline IniFile &
137 inifile()
138 {
139 static IniFile inifile;
140 return inifile;
141 }
142
143 /**
144 * Convert a pointer to the Python object that SWIG wraps around a C++
145 * SimObject pointer back to the actual C++ pointer. See main.i.
146 */
147 extern "C" SimObject *convertSwigSimObjectPtr(PyObject *);
148
149 // Python.h is notoriously not const-correct (for 2.4, anyway)... make
150 // a little define here to reduce the noise and make it easier to
151 // #ifdef away if Python.h gets fixed. Note there are a couple of
152 // these in sim/main.cc as well that are handled without this define.
153 #define PCC(s) const_cast<char *>(s)
154
155 /** Single instance of PythonSimObjectResolver as its action is effectively
156 * static but SimObjectResolver can use a non-persistent object */
157 PythonSimObjectResolver pythonSimObjectResolver;
158
159 SimObject *
160 PythonSimObjectResolver::resolveSimObject(const string &name)
161 {
162 PyObject *module = PyImport_ImportModule(PCC("m5.SimObject"));
163 if (module == NULL)
164 panic("Could not import m5.SimObject");
165
166 PyObject *resolver =
167 PyObject_GetAttrString(module, PCC("resolveSimObject"));
168 if (resolver == NULL) {
169 PyErr_Print();
170 panic("resolveSimObject: failed to find resolveSimObject");
171 }
172
173 PyObject *ptr = PyObject_CallFunction(resolver, PCC("(s)"), name.c_str());
174 if (ptr == NULL) {
175 PyErr_Print();
176 panic("resolveSimObject: failure on call to Python for %s", name);
177 }
178
179 SimObject *obj = convertSwigSimObjectPtr(ptr);
180 if (obj == NULL)
181 panic("resolveSimObject: failure on pointer conversion for %s", name);
182
183 Py_DECREF(module);
184 Py_DECREF(resolver);
185 Py_DECREF(ptr);
186
187 return obj;
188 }
189
190 CheckpointIn *
191 getCheckpoint(const std::string &cpt_dir)
192 {
193 return new CheckpointIn(cpt_dir, pythonSimObjectResolver);
194 }