From 45ec502dedf1503fa0117c6eef4a765e4c736315 Mon Sep 17 00:00:00 2001 From: David Shah Date: Mon, 2 Jul 2018 16:20:59 +0200 Subject: [PATCH] python: Adding more bindings Signed-off-by: David Shah --- common/pybindings.cc | 4 ---- common/pycontainers.h | 20 +++++++++++++++++++- 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()); - class_("IdString") - .def("__str__", &IdString::global_str, return_value_policy()) - .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 #include #include +#include #include #include #include @@ -81,6 +82,7 @@ template , struct range_wrapper { typedef decltype(std::declval().begin()) iterator_t; + typedef decltype(*(std::declval())) value_t; typedef typename PythonConversion::ContextualWrapper wrapped_range; typedef typename PythonConversion::ContextualWrapper> 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 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_(range_name, no_init).def("__iter__", iter); + class_(range_name, no_init).def("__iter__", iter).def("__repr__", repr); iterator_wrapper().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 std::string to_str(Context *ctx, BelType typ) { return ctx->belTypeToId(typ).str(ctx); } }; +template <> struct string_converter +{ + 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 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, - conv_from_str>::def_wrap(ctx_cls, "getBelType"); + conv_from_str>::def_wrap(ctx_cls, "getBelType"); fn_wrapper_1a, - conv_from_str>::def_wrap(ctx_cls, "checkBelAvail"); - fn_wrapper_0a>::def_wrap( - ctx_cls, "getBels"); + conv_from_str>::def_wrap(ctx_cls, "checkBelAvail"); + fn_wrapper_0a>::def_wrap(ctx_cls, + "getBels"); + fn_wrapper_0a>::def_wrap( + ctx_cls, "getWires"); + fn_wrapper_1a, + conv_from_str>::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); + WRAP_RANGE(Wire, conv_to_str); + WRAP_RANGE(AllPip, conv_to_str); + WRAP_RANGE(Pip, conv_to_str); + // WRAP_RANGE(BelPin); // WRAP_RANGE(Wire); // WRAP_RANGE(AllPip);