From d79a2808cf2446fa21d91a6141f6fbe2318c03ec Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Thu, 16 Aug 2018 16:00:11 +0200 Subject: 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 --- passes/cmds/plugin.cc | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'passes') 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 #endif +#ifdef WITH_PYTHON +# include +# include +#endif + YOSYS_NAMESPACE_BEGIN std::map loaded_plugins; +#ifdef WITH_PYTHON +std::map loaded_python_plugins; +#endif std::map loaded_plugin_aliases; #ifdef YOSYS_ENABLE_PLUGINS @@ -37,6 +45,48 @@ void load_plugin(std::string filename, std::vector 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 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; -- cgit v1.2.3 From 6d18837d62436b297b34c20d5a005ef0b6a75da2 Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Mon, 20 Aug 2018 15:11:06 +0200 Subject: Python passes are now looked for in share/plugins and can be added by specifying a relative or absolute path --- passes/cmds/plugin.cc | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) (limited to 'passes') diff --git a/passes/cmds/plugin.cc b/passes/cmds/plugin.cc index b5d22a84a..940092301 100644 --- a/passes/cmds/plugin.cc +++ b/passes/cmds/plugin.cc @@ -49,10 +49,12 @@ void load_plugin(std::string filename, std::vector aliases) #ifdef WITH_PYTHON if(boost::algorithm::ends_with(filename, ".py")) { - int last_slash = filename.find('/'); + int last_slash = filename.find_last_of('/'); + std::string path = filename.substr(0, last_slash); 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()); + PyRun_SimpleString(("sys.path.insert(0,\""+path+"\")").c_str()); + PyObject *filename_p = PyUnicode_FromString(filename.c_str()); if(filename_p == NULL) { log_cmd_error("Issues converting `%s' to Python\n", filename.c_str()); @@ -63,25 +65,7 @@ void load_plugin(std::string filename, std::vector aliases) { 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 { -- cgit v1.2.3 From d87c7df27f8268ab849d3f9d84c4b000f83b44e2 Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Mon, 20 Aug 2018 15:28:09 +0200 Subject: Two passes are not allowed to have the same filename --- passes/cmds/plugin.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'passes') diff --git a/passes/cmds/plugin.cc b/passes/cmds/plugin.cc index 940092301..1a39140d4 100644 --- a/passes/cmds/plugin.cc +++ b/passes/cmds/plugin.cc @@ -44,7 +44,7 @@ void load_plugin(std::string filename, std::vector aliases) if (filename.find('/') == std::string::npos) filename = "./" + filename; - if (!loaded_plugins.count(filename)) { + if (!loaded_plugins.count(filename) && !loaded_python_plugins.count(filename)) { #ifdef WITH_PYTHON if(boost::algorithm::ends_with(filename, ".py")) -- cgit v1.2.3 From 95d65971f3f114adb8b62a9d29bc0829467e3d81 Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Mon, 20 Aug 2018 16:04:43 +0200 Subject: added some checks if python is enabled to make sure everything compiles if python is disabled in the makefile --- passes/cmds/plugin.cc | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'passes') diff --git a/passes/cmds/plugin.cc b/passes/cmds/plugin.cc index 1a39140d4..a889397e2 100644 --- a/passes/cmds/plugin.cc +++ b/passes/cmds/plugin.cc @@ -44,7 +44,11 @@ void load_plugin(std::string filename, std::vector aliases) if (filename.find('/') == std::string::npos) filename = "./" + filename; + #ifdef WITH_PYTHON if (!loaded_plugins.count(filename) && !loaded_python_plugins.count(filename)) { + #else + if (!loaded_plugins.count(filename)) { + #endif #ifdef WITH_PYTHON if(boost::algorithm::ends_with(filename, ".py")) -- cgit v1.2.3 From bbfb43006d2a7d67d06ee151a1b8ad05ec5b1750 Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Wed, 3 Apr 2019 12:21:56 +0200 Subject: Improved Error reporting when Python passes are loaded --- passes/cmds/plugin.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'passes') diff --git a/passes/cmds/plugin.cc b/passes/cmds/plugin.cc index 2b06690f9..bb1ec8716 100644 --- a/passes/cmds/plugin.cc +++ b/passes/cmds/plugin.cc @@ -61,12 +61,14 @@ void load_plugin(std::string filename, std::vector aliases) PyObject *filename_p = PyUnicode_FromString(filename.c_str()); if(filename_p == NULL) { + PyErr_Print(); log_cmd_error("Issues converting `%s' to Python\n", filename.c_str()); return; } PyObject *module_p = PyImport_Import(filename_p); if(module_p == NULL) { + PyErr_Print(); log_cmd_error("Can't load python module `%s'\n", filename.c_str()); return; } -- cgit v1.2.3 From fd7fb1377d4d30d692c78eb55173198339fea17d Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Wed, 3 Apr 2019 13:21:40 +0200 Subject: Added cross-platform support for plugin-paths --- passes/cmds/plugin.cc | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'passes') diff --git a/passes/cmds/plugin.cc b/passes/cmds/plugin.cc index bb1ec8716..60dab38dd 100644 --- a/passes/cmds/plugin.cc +++ b/passes/cmds/plugin.cc @@ -26,6 +26,7 @@ #ifdef WITH_PYTHON # include # include +# include #endif YOSYS_NAMESPACE_BEGIN @@ -51,25 +52,28 @@ void load_plugin(std::string filename, std::vector aliases) #endif #ifdef WITH_PYTHON - if(boost::algorithm::ends_with(filename, ".py")) + + std::experimental::filesystem::path full_path(filename); + + if(strcmp(full_path.extension().c_str(), ".py") == 0) { - int last_slash = filename.find_last_of('/'); - std::string path = filename.substr(0, last_slash); - filename = filename.substr(last_slash+1, filename.size()); + std::string path(full_path.parent_path().c_str()); + filename = full_path.filename().c_str(); filename = filename.substr(0,filename.size()-3); PyRun_SimpleString(("sys.path.insert(0,\""+path+"\")").c_str()); + PyErr_Print(); PyObject *filename_p = PyUnicode_FromString(filename.c_str()); if(filename_p == NULL) { PyErr_Print(); - log_cmd_error("Issues converting `%s' to Python\n", filename.c_str()); + log_cmd_error("Issues converting `%s' to Python\n", full_path.filename().c_str()); return; } PyObject *module_p = PyImport_Import(filename_p); if(module_p == NULL) { PyErr_Print(); - log_cmd_error("Can't load python module `%s'\n", filename.c_str()); + log_cmd_error("Can't load python module `%s'\n", full_path.filename().c_str()); return; } loaded_python_plugins[orig_filename] = module_p; -- cgit v1.2.3 From e64b3f107411c150bbc773fc72b915bc60813c52 Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Thu, 4 Apr 2019 09:24:50 +0200 Subject: Changed filesystem dependency to boost instead of experimental std library --- passes/cmds/plugin.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'passes') diff --git a/passes/cmds/plugin.cc b/passes/cmds/plugin.cc index 60dab38dd..5da8f5b0b 100644 --- a/passes/cmds/plugin.cc +++ b/passes/cmds/plugin.cc @@ -26,7 +26,7 @@ #ifdef WITH_PYTHON # include # include -# include +# include #endif YOSYS_NAMESPACE_BEGIN @@ -53,7 +53,7 @@ void load_plugin(std::string filename, std::vector aliases) #ifdef WITH_PYTHON - std::experimental::filesystem::path full_path(filename); + boost::filesystem::path full_path(filename); if(strcmp(full_path.extension().c_str(), ".py") == 0) { @@ -63,6 +63,7 @@ void load_plugin(std::string filename, std::vector aliases) PyRun_SimpleString(("sys.path.insert(0,\""+path+"\")").c_str()); PyErr_Print(); PyObject *filename_p = PyUnicode_FromString(filename.c_str()); + if(filename_p == NULL) { PyErr_Print(); -- cgit v1.2.3 From cae657cebd1f4aa119a1264f80d89294a23be845 Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Thu, 4 Apr 2019 10:35:01 +0200 Subject: Used PyImport_ImportModule instead of PyImport_Import to avoid the explicit conversion to a python string --- passes/cmds/plugin.cc | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'passes') diff --git a/passes/cmds/plugin.cc b/passes/cmds/plugin.cc index 5da8f5b0b..4c16b56c4 100644 --- a/passes/cmds/plugin.cc +++ b/passes/cmds/plugin.cc @@ -60,17 +60,9 @@ void load_plugin(std::string filename, std::vector aliases) std::string path(full_path.parent_path().c_str()); filename = full_path.filename().c_str(); filename = filename.substr(0,filename.size()-3); - PyRun_SimpleString(("sys.path.insert(0,\""+path+"\")").c_str()); + PyRun_SimpleString(("sys.path.insert(0,\""+path+"\")").c_str()); PyErr_Print(); - PyObject *filename_p = PyUnicode_FromString(filename.c_str()); - - if(filename_p == NULL) - { - PyErr_Print(); - log_cmd_error("Issues converting `%s' to Python\n", full_path.filename().c_str()); - return; - } - PyObject *module_p = PyImport_Import(filename_p); + PyObject *module_p = PyImport_ImportModule(filename.c_str()); if(module_p == NULL) { PyErr_Print(); -- cgit v1.2.3 From 7b35d5759289f7a3139c6eaa525ef737b8d5d82b Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Mon, 22 Apr 2019 02:07:36 +0200 Subject: Disable blackbox detection in techmap files Signed-off-by: Clifford Wolf --- passes/techmap/techmap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'passes') diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index ee319b6e6..1a4318460 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -1036,7 +1036,7 @@ struct TechmapPass : public Pass { simplemap_get_mappers(worker.simplemap_mappers); std::vector map_files; - std::string verilog_frontend = "verilog -nooverwrite"; + std::string verilog_frontend = "verilog -nooverwrite -noblackbox"; int max_iter = -1; size_t argidx; -- cgit v1.2.3 From aeeefc32d8603b138f24824d01b29d28cb0f85cf Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 22 Apr 2019 14:18:15 +0000 Subject: attrmap: extend -remove to allow removing attributes with any value. Currently, `-remove foo` would only remove an attribute `foo = ""`, which doesn't work on an attribute like `src` that may have any value. Extend `-remove` to handle both cases. `-remove foo=""` has the old behavior, and `-remove foo` will remove the attribute with whatever value it may have, which is still compatible with the old behavior. --- passes/techmap/attrmap.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'passes') diff --git a/passes/techmap/attrmap.cc b/passes/techmap/attrmap.cc index 0b5576b06..aa48e1125 100644 --- a/passes/techmap/attrmap.cc +++ b/passes/techmap/attrmap.cc @@ -111,9 +111,10 @@ struct AttrmapMap : AttrmapAction { }; struct AttrmapRemove : AttrmapAction { + bool has_value; string name, value; bool apply(IdString &id, Const &val) YS_OVERRIDE { - return !(match_name(name, id) && match_value(value, val)); + return !(match_name(name, id) && (!has_value || match_value(value, val))); } }; @@ -235,6 +236,7 @@ struct AttrmapPass : public Pass { } auto action = new AttrmapRemove; action->name = arg1; + action->has_value = (p != string::npos); action->value = val1; actions.push_back(std::unique_ptr(action)); continue; -- cgit v1.2.3