init.cc (7502:3ef7ff12c788) | init.cc (7674:8e3734851770) |
---|---|
1/* 2 * Copyright (c) 2000-2005 The Regents of The University of Michigan 3 * Copyright (c) 2008 The Hewlett-Packard Development Company 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: redistributions of source code must retain the above copyright --- 19 unchanged lines hidden (view full) --- 28 * 29 * Authors: Nathan Binkert 30 */ 31 32#include <Python.h> 33#include <marshal.h> 34#include <signal.h> 35 | 1/* 2 * Copyright (c) 2000-2005 The Regents of The University of Michigan 3 * Copyright (c) 2008 The Hewlett-Packard Development Company 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: redistributions of source code must retain the above copyright --- 19 unchanged lines hidden (view full) --- 28 * 29 * Authors: Nathan Binkert 30 */ 31 32#include <Python.h> 33#include <marshal.h> 34#include <signal.h> 35 |
36#include <list> |
|
36#include <iostream> 37#include <string> 38#include <zlib.h> 39 40#include "base/cprintf.hh" 41#include "base/misc.hh" 42#include "base/types.hh" 43#include "sim/async.hh" --- 55 unchanged lines hidden (view full) --- 99 100 // Exit cleanly on Interrupt (Ctrl-C) 101 signal(SIGINT, exitNowHandler); 102 103 // Print out cycle number on abort 104 signal(SIGABRT, abortHandler); 105} 106 | 37#include <iostream> 38#include <string> 39#include <zlib.h> 40 41#include "base/cprintf.hh" 42#include "base/misc.hh" 43#include "base/types.hh" 44#include "sim/async.hh" --- 55 unchanged lines hidden (view full) --- 100 101 // Exit cleanly on Interrupt (Ctrl-C) 102 signal(SIGINT, exitNowHandler); 103 104 // Print out cycle number on abort 105 signal(SIGABRT, abortHandler); 106} 107 |
108// The python library is totally messed up with respect to constness, 109// so make a simple macro to make life a little easier 110#define PyCC(x) (const_cast<char *>(x)) 111 112EmbeddedPython *EmbeddedPython::importer = NULL; 113PyObject *EmbeddedPython::importerModule = NULL; 114EmbeddedPython::EmbeddedPython(const char *filename, const char *abspath, 115 const char *modpath, const char *code, int zlen, int len) 116 : filename(filename), abspath(abspath), modpath(modpath), code(code), 117 zlen(zlen), len(len) 118{ 119 // if we've added the importer keep track of it because we need it 120 // to bootstrap. 121 if (string(modpath) == string("importer")) 122 importer = this; 123 else 124 getList().push_back(this); 125} 126 127list<EmbeddedPython *> & 128EmbeddedPython::getList() 129{ 130 static list<EmbeddedPython *> the_list; 131 return the_list; 132} 133 |
|
107/* 108 * Uncompress and unmarshal the code object stored in the | 134/* 135 * Uncompress and unmarshal the code object stored in the |
109 * EmbeddedPyModule | 136 * EmbeddedPython |
110 */ 111PyObject * | 137 */ 138PyObject * |
112getCode(const EmbeddedPyModule *pymod) | 139EmbeddedPython::getCode() const |
113{ | 140{ |
114 assert(pymod->zlen == pymod->code_end - pymod->code); 115 Bytef *marshalled = new Bytef[pymod->mlen]; 116 uLongf unzlen = pymod->mlen; 117 int ret = uncompress(marshalled, &unzlen, (const Bytef *)pymod->code, 118 pymod->zlen); | 141 Bytef marshalled[len]; 142 uLongf unzlen = len; 143 int ret = uncompress(marshalled, &unzlen, (const Bytef *)code, zlen); |
119 if (ret != Z_OK) 120 panic("Could not uncompress code: %s\n", zError(ret)); | 144 if (ret != Z_OK) 145 panic("Could not uncompress code: %s\n", zError(ret)); |
121 assert(unzlen == (uLongf)pymod->mlen); | 146 assert(unzlen == (uLongf)len); |
122 | 147 |
123 return PyMarshal_ReadObjectFromString((char *)marshalled, pymod->mlen); | 148 return PyMarshal_ReadObjectFromString((char *)marshalled, len); |
124} 125 | 149} 150 |
126// The python library is totally messed up with respect to constness, 127// so make a simple macro to make life a little easier 128#define PyCC(x) (const_cast<char *>(x)) | 151bool 152EmbeddedPython::addModule() const 153{ 154 PyObject *code = getCode(); 155 PyObject *result = PyObject_CallMethod(importerModule, PyCC("add_module"), 156 PyCC("sssO"), filename, abspath, modpath, code); 157 if (!result) { 158 PyErr_Print(); 159 return false; 160 } |
129 | 161 |
162 Py_DECREF(result); 163 return true; 164} 165 |
|
130/* 131 * Load and initialize all of the python parts of M5, including Swig 132 * and the embedded module importer. 133 */ 134int | 166/* 167 * Load and initialize all of the python parts of M5, including Swig 168 * and the embedded module importer. 169 */ 170int |
135initM5Python() | 171EmbeddedPython::initAll() |
136{ | 172{ |
137 extern void initSwig(); 138 139 // initialize SWIG modules. initSwig() is autogenerated and calls 140 // all of the individual swig initialization functions. 141 initSwig(); 142 | |
143 // Load the importer module | 173 // Load the importer module |
144 PyObject *code = getCode(&embeddedPyImporter); 145 PyObject *module = PyImport_ExecCodeModule(PyCC("importer"), code); 146 if (!module) { | 174 PyObject *code = importer->getCode(); 175 importerModule = PyImport_ExecCodeModule(PyCC("importer"), code); 176 if (!importerModule) { |
147 PyErr_Print(); 148 return 1; 149 } 150 151 // Load the rest of the embedded python files into the embedded 152 // python importer | 177 PyErr_Print(); 178 return 1; 179 } 180 181 // Load the rest of the embedded python files into the embedded 182 // python importer |
153 const EmbeddedPyModule *pymod = &embeddedPyModules[0]; 154 while (pymod->filename) { 155 PyObject *code = getCode(pymod); 156 PyObject *result = PyObject_CallMethod(module, PyCC("add_module"), 157 PyCC("sssO"), pymod->filename, pymod->abspath, pymod->modpath, 158 code); 159 if (!result) { 160 PyErr_Print(); | 183 list<EmbeddedPython *>::iterator i = getList().begin(); 184 list<EmbeddedPython *>::iterator end = getList().end(); 185 for (; i != end; ++i) 186 if (!(*i)->addModule()) |
161 return 1; | 187 return 1; |
162 } 163 Py_DECREF(result); 164 ++pymod; 165 } | |
166 167 return 0; 168} 169 | 188 189 return 0; 190} 191 |
192EmbeddedSwig::EmbeddedSwig(void (*init_func)()) 193 : initFunc(init_func) 194{ 195 getList().push_back(this); 196} 197 198list<EmbeddedSwig *> & 199EmbeddedSwig::getList() 200{ 201 static list<EmbeddedSwig *> the_list; 202 return the_list; 203} 204 205void 206EmbeddedSwig::initAll() 207{ 208 // initialize SWIG modules. initSwig() is autogenerated and calls 209 // all of the individual swig initialization functions. 210 list<EmbeddedSwig *>::iterator i = getList().begin(); 211 list<EmbeddedSwig *>::iterator end = getList().end(); 212 for (; i != end; ++i) 213 (*i)->initFunc(); 214} 215 216int 217initM5Python() 218{ 219 EmbeddedSwig::initAll(); 220 return EmbeddedPython::initAll(); 221} 222 |
|
170/* 171 * Start up the M5 simulator. This mostly vectors into the python 172 * main function. 173 */ 174int 175m5Main(int argc, char **argv) 176{ 177 PySys_SetArgv(argc, argv); --- 33 unchanged lines hidden --- | 223/* 224 * Start up the M5 simulator. This mostly vectors into the python 225 * main function. 226 */ 227int 228m5Main(int argc, char **argv) 229{ 230 PySys_SetArgv(argc, argv); --- 33 unchanged lines hidden --- |