diff options
author | David Shah <davey1576@gmail.com> | 2018-07-02 16:20:59 +0200 |
---|---|---|
committer | David Shah <davey1576@gmail.com> | 2018-07-04 14:55:24 +0200 |
commit | 45ec502dedf1503fa0117c6eef4a765e4c736315 (patch) | |
tree | aa2a8154be97c3167831bb2ea1fd1109d71b76e3 | |
parent | 1e96d65ded029f68bb294cfb25c56f138dd51180 (diff) | |
download | nextpnr-45ec502dedf1503fa0117c6eef4a765e4c736315.tar.gz nextpnr-45ec502dedf1503fa0117c6eef4a765e4c736315.tar.bz2 nextpnr-45ec502dedf1503fa0117c6eef4a765e4c736315.zip |
python: Adding more bindings
Signed-off-by: David Shah <davey1576@gmail.com>
-rw-r--r-- | common/pybindings.cc | 4 | ||||
-rw-r--r-- | common/pycontainers.h | 20 | ||||
-rw-r--r-- | ice40/pybindings.cc | 31 |
3 files changed, 46 insertions, 9 deletions
diff --git a/common/pybindings.cc b/common/pybindings.cc index 1746c517..f84d21f5 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -117,10 +117,6 @@ BOOST_PYTHON_MODULE(MODULE_NAME) def("parse_json", parse_json_shim); def("load_design", load_design_shim, return_value_policy<manage_new_object>()); - class_<IdString>("IdString") - .def("__str__", &IdString::global_str, return_value_policy<copy_const_reference>()) - .def(self < self) - .def(self == self); arch_wrap_python(); } diff --git a/common/pycontainers.h b/common/pycontainers.h index e1a73d75..6870c3a4 100644 --- a/common/pycontainers.h +++ b/common/pycontainers.h @@ -24,6 +24,7 @@ #include <boost/python.hpp> #include <boost/python/suite/indexing/map_indexing_suite.hpp> #include <boost/python/suite/indexing/vector_indexing_suite.hpp> +#include <sstream> #include <stdexcept> #include <type_traits> #include <utility> @@ -81,6 +82,7 @@ template <typename T, typename P = return_value_policy<return_by_value>, struct range_wrapper { typedef decltype(std::declval<T>().begin()) iterator_t; + typedef decltype(*(std::declval<iterator_t>())) value_t; typedef typename PythonConversion::ContextualWrapper<T> wrapped_range; typedef typename PythonConversion::ContextualWrapper<std::pair<iterator_t, iterator_t>> wrapped_pair; static wrapped_pair iter(wrapped_range &range) @@ -88,9 +90,25 @@ struct range_wrapper return wrapped_pair(range.ctx, std::make_pair(range.base.begin(), range.base.end())); } + static std::string repr(wrapped_range &range) + { + PythonConversion::string_converter<value_t> conv; + bool first = true; + std::stringstream ss; + ss << "["; + for (const auto &item : range.base) { + if (!first) + ss << ", "; + ss << "'" << conv.to_str(range.ctx, item) << "'"; + first = false; + } + ss << "]"; + return ss.str(); + } + static void wrap(const char *range_name, const char *iter_name) { - class_<wrapped_range>(range_name, no_init).def("__iter__", iter); + class_<wrapped_range>(range_name, no_init).def("__iter__", iter).def("__repr__", repr); iterator_wrapper<iterator_t, P, value_conv>().wrap(iter_name); } diff --git a/ice40/pybindings.cc b/ice40/pybindings.cc index dce03ecd..c824f822 100644 --- a/ice40/pybindings.cc +++ b/ice40/pybindings.cc @@ -41,6 +41,20 @@ template <> struct string_converter<BelType> std::string to_str(Context *ctx, BelType typ) { return ctx->belTypeToId(typ).str(ctx); } }; +template <> struct string_converter<WireId> +{ + WireId from_str(Context *ctx, std::string name) { return ctx->getWireByName(ctx->id(name)); } + + std::string to_str(Context *ctx, WireId id) { return ctx->getWireName(id).str(ctx); } +}; + +template <> struct string_converter<PipId> +{ + PipId from_str(Context *ctx, std::string name) { return ctx->getPipByName(ctx->id(name)); } + + std::string to_str(Context *ctx, PipId id) { return ctx->getPipName(id).str(ctx); } +}; + } // namespace PythonConversion void arch_wrap_python() @@ -78,11 +92,16 @@ void arch_wrap_python() .def("checksum", &Context::checksum); fn_wrapper_1a<Context, typeof(&Context::getBelType), &Context::getBelType, conv_to_str<BelType>, - conv_from_str<BelId>>::def_wrap(ctx_cls, "getBelType"); + conv_from_str<BelId>>::def_wrap(ctx_cls, "getBelType"); fn_wrapper_1a<Context, typeof(&Context::checkBelAvail), &Context::checkBelAvail, pass_through<bool>, - conv_from_str<BelId>>::def_wrap(ctx_cls, "checkBelAvail"); - fn_wrapper_0a<Context, typeof(&Context::getBels), &Context::getBels, wrap_context<BelRange>>::def_wrap( - ctx_cls, "getBels"); + conv_from_str<BelId>>::def_wrap(ctx_cls, "checkBelAvail"); + fn_wrapper_0a<Context, typeof(&Context::getBels), &Context::getBels, wrap_context<BelRange>>::def_wrap(ctx_cls, + "getBels"); + fn_wrapper_0a<Context, typeof(&Context::getWires), &Context::getWires, wrap_context<WireRange>>::def_wrap( + ctx_cls, "getWires"); + fn_wrapper_1a<Context, typeof(&Context::getPipsDownhill), &Context::getPipsDownhill, wrap_context<PipRange>, + conv_from_str<WireId>>::def_wrap(ctx_cls, "getPipsDownhill"); + /* .def("getBelByName", &Arch::getBelByName) .def("getWireByName", &Arch::getWireByName) @@ -107,6 +126,10 @@ void arch_wrap_python() */ WRAP_RANGE(Bel, conv_to_str<BelId>); + WRAP_RANGE(Wire, conv_to_str<WireId>); + WRAP_RANGE(AllPip, conv_to_str<PipId>); + WRAP_RANGE(Pip, conv_to_str<PipId>); + // WRAP_RANGE(BelPin); // WRAP_RANGE(Wire); // WRAP_RANGE(AllPip); |