aboutsummaryrefslogtreecommitdiffstats
path: root/passes/cmds/plugin.cc
diff options
context:
space:
mode:
authorBenedikt Tutzer <e1225461@student.tuwien.ac.at>2018-08-16 16:00:11 +0200
committerBenedikt Tutzer <e1225461@student.tuwien.ac.at>2018-08-16 16:00:11 +0200
commitd79a2808cf2446fa21d91a6141f6fbe2318c03ec (patch)
treee0c630f1dd8ccab3c5d04ddf8a6fa06376d1f5e7 /passes/cmds/plugin.cc
parentbf7b73acfc2b5e46206e5688b8a6e8d9b0d60d8f (diff)
downloadyosys-d79a2808cf2446fa21d91a6141f6fbe2318c03ec.tar.gz
yosys-d79a2808cf2446fa21d91a6141f6fbe2318c03ec.tar.bz2
yosys-d79a2808cf2446fa21d91a6141f6fbe2318c03ec.zip
Python Passes can now be added with the -m option or with the plugin command. There are still issues when run in shell mode, but they can be used just fine in a python script
Diffstat (limited to 'passes/cmds/plugin.cc')
-rw-r--r--passes/cmds/plugin.cc63
1 files changed, 63 insertions, 0 deletions
diff --git a/passes/cmds/plugin.cc b/passes/cmds/plugin.cc
index 828c671de..b5d22a84a 100644
--- a/passes/cmds/plugin.cc
+++ b/passes/cmds/plugin.cc
@@ -23,9 +23,17 @@
# include <dlfcn.h>
#endif
+#ifdef WITH_PYTHON
+# include <boost/algorithm/string/predicate.hpp>
+# include <Python.h>
+#endif
+
YOSYS_NAMESPACE_BEGIN
std::map<std::string, void*> loaded_plugins;
+#ifdef WITH_PYTHON
+std::map<std::string, void*> loaded_python_plugins;
+#endif
std::map<std::string, std::string> loaded_plugin_aliases;
#ifdef YOSYS_ENABLE_PLUGINS
@@ -37,6 +45,48 @@ void load_plugin(std::string filename, std::vector<std::string> aliases)
filename = "./" + filename;
if (!loaded_plugins.count(filename)) {
+
+ #ifdef WITH_PYTHON
+ if(boost::algorithm::ends_with(filename, ".py"))
+ {
+ int last_slash = filename.find('/');
+ filename = filename.substr(last_slash+1, filename.size());
+ filename = filename.substr(0,filename.size()-3);
+ PyObject *filename_p = PyUnicode_FromString(filename.c_str());//filename.c_str());
+ if(filename_p == NULL)
+ {
+ log_cmd_error("Issues converting `%s' to Python\n", filename.c_str());
+ return;
+ }
+ PyObject *module_p = PyImport_Import(filename_p);
+ if(module_p == NULL)
+ {
+ log_cmd_error("Can't load python module `%s'\n", filename.c_str());
+ return;
+ }/*
+ PyObject *dict_p = PyModule_GetDict(module_p);
+ if(dict_p == NULL)
+ {
+ log_cmd_error("Can't load dictionary from module `%s'\n", filename.c_str());
+ return;
+ }
+ PyObject *func_p = PyDict_GetItemString(dict_p, "test");
+ if(module_p == NULL)
+ {
+ log_cmd_error("Module `%s' does not contain test function\n", filename.c_str());
+ return;
+ }
+ PyObject *args_p = PyTuple_New(0);
+ PyObject *result_p = PyObject_CallObject(func_p, args_p);
+ if(result_p == NULL)
+ printf("Calling test failed\n");
+ printf("Loaded Python module\n");
+ */
+ loaded_python_plugins[orig_filename] = module_p;
+ Pass::init_register();
+ } else {
+ #endif
+
void *hdl = dlopen(filename.c_str(), RTLD_LAZY|RTLD_LOCAL);
if (hdl == NULL && orig_filename.find('/') == std::string::npos)
hdl = dlopen((proc_share_dirname() + "plugins/" + orig_filename + ".so").c_str(), RTLD_LAZY|RTLD_LOCAL);
@@ -44,6 +94,10 @@ void load_plugin(std::string filename, std::vector<std::string> aliases)
log_cmd_error("Can't load module `%s': %s\n", filename.c_str(), dlerror());
loaded_plugins[orig_filename] = hdl;
Pass::init_register();
+
+ #ifdef WITH_PYTHON
+ }
+ #endif
}
for (auto &alias : aliases)
@@ -107,7 +161,11 @@ struct PluginPass : public Pass {
if (list_mode)
{
log("\n");
+#ifdef WITH_PYTHON
+ if (loaded_plugins.empty() and loaded_python_plugins.empty())
+#else
if (loaded_plugins.empty())
+#endif
log("No plugins loaded.\n");
else
log("Loaded plugins:\n");
@@ -115,6 +173,11 @@ struct PluginPass : public Pass {
for (auto &it : loaded_plugins)
log(" %s\n", it.first.c_str());
+#ifdef WITH_PYTHON
+ for (auto &it : loaded_python_plugins)
+ log(" %s\n", it.first.c_str());
+#endif
+
if (!loaded_plugin_aliases.empty()) {
log("\n");
int max_alias_len = 1;