From 65195513eb2676d974f1cef94be5e84f42a52f0f Mon Sep 17 00:00:00 2001 From: David Shah Date: Mon, 2 Jul 2018 15:13:09 +0200 Subject: python: Restructuring wrapper system Signed-off-by: David Shah --- common/pywrappers.h | 57 +++++++++++++++++++++++++++++++++------------- ice40/pybindings.cc | 65 +++++++++++++++++++++++++++++++++++------------------ 2 files changed, 84 insertions(+), 38 deletions(-) diff --git a/common/pywrappers.h b/common/pywrappers.h index 2c91f26f..fe786a3b 100644 --- a/common/pywrappers.h +++ b/common/pywrappers.h @@ -41,66 +41,91 @@ template struct ContextualWrapper Context *ctx; T base; - ContextualWrapper(Context *c, T &&x) : ctx(c), base(x){}; + inline ContextualWrapper(Context *c, T &&x) : ctx(c), base(x){}; - operator T() { return base; }; + inline operator T() { return base; }; typedef T base_type; }; +template struct WrapIfNotContext { + typedef ContextualWrapper maybe_wrapped_t; +}; + +template <> struct WrapIfNotContext { + typedef Context maybe_wrapped_t; +}; + + +template inline Context *get_ctx(typename WrapIfNotContext::maybe_wrapped_t &wrp_ctx) { + return wrp_ctx.ctx; +} +template <> inline Context *get_ctx(WrapIfNotContext::maybe_wrapped_t &unwrp_ctx) { + return &unwrp_ctx; +} + +template inline T&get_base(typename WrapIfNotContext::maybe_wrapped_t &wrp_ctx) { + return wrp_ctx.base; +} +template <> inline Context &get_base(WrapIfNotContext::maybe_wrapped_t &unwrp_ctx) { + return unwrp_ctx; +} + template ContextualWrapper wrap_ctx(Context *ctx, T x) { return ContextualWrapper(ctx, x); } // Dummy class, to be implemented by users -template class string_converter; +template struct string_converter; // Action options -template class do_nothing +template struct do_nothing { - T operator()(Context *ctx, T x) { return x; } + inline T operator()(Context *ctx, T x) { return x; } using ret_type = T; using arg_type = T; }; -template class wrap_context +template struct wrap_context { - ContextualWrapper operator()(Context *ctx, T x) { return ContextualWrapper(ctx, x); } + inline ContextualWrapper operator()(Context *ctx, T x) { return ContextualWrapper(ctx, x); } using arg_type = T; using ret_type = ContextualWrapper; }; -template class unwrap_context +template struct unwrap_context { - T operator()(Context *ctx, ContextualWrapper x) { return x.base; } + inline T operator()(Context *ctx, ContextualWrapper x) { return x.base; } using ret_type = T; using arg_type = ContextualWrapper; }; -template class conv_from_string +template struct conv_from_str { - T operator()(Context *ctx, std::string x) { return string_converter().from_str(ctx, x); } + inline T operator()(Context *ctx, std::string x) { return string_converter().from_str(ctx, x); } using ret_type = T; using arg_type = std::string; }; -template class conv_to_string +template struct conv_to_str { - std::string operator()(Context *ctx, T x) { return string_converter().to_str(ctx, x); } + inline std::string operator()(Context *ctx, T x) { return string_converter().to_str(ctx, x); } using ret_type = std::string; using arg_type = T; }; // Function wrapper // Example: one parameter, one return -template struct function_wrapper +template struct fn_wrapper { - using class_type = ContextualWrapper; + using class_type = typename WrapIfNotContext::maybe_wrapped_t; using conv_result_type = typename rv_conv::ret_type; using conv_arg1_type = typename arg1_conv::arg_type; static conv_result_type wrapped_fn(class_type &cls, conv_arg1_type arg1) { - return rv_conv()(cls.ctx, cls.base.*fn(arg1_conv()(cls.ctx, arg1))); + return rv_conv()(get_base(cls).ctx, get_base(cls).*fn(arg1_conv()(get_ctx(cls), arg1))); } + + template static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } }; } // namespace PythonConversion diff --git a/ice40/pybindings.cc b/ice40/pybindings.cc index 3c3e2394..ea1c5547 100644 --- a/ice40/pybindings.cc +++ b/ice40/pybindings.cc @@ -25,8 +25,27 @@ NEXTPNR_NAMESPACE_BEGIN +namespace PythonConversion { + +template <> struct string_converter +{ + BelId from_str(Context *ctx, std::string name) { return ctx->getBelByName(ctx->id(name)); } + + std::string to_str(Context *ctx, BelId id) { return ctx->getBelName(id).str(ctx); } +}; + +template <> struct string_converter +{ + BelType from_str(Context *ctx, std::string name) { return ctx->belTypeFromId(ctx->id(name)); } + + std::string to_str(Context *ctx, BelType typ) { return ctx->belTypeToId(typ).str(ctx); } +}; + +} // namespace PythonConversion + void arch_wrap_python() { + using namespace PythonConversion; class_("ArchArgs").def_readwrite("type", &ArchArgs::type); enum_().type)>("iCE40Type") @@ -53,28 +72,30 @@ void arch_wrap_python() ; #undef X - class_, boost::noncopyable>("Arch", init()) - .def("getBelByName", &Arch::getBelByName) - .def("getWireByName", &Arch::getWireByName) - .def("getBelName", &Arch::getBelName) - .def("getWireName", &Arch::getWireName) - .def("getBels", &Arch::getBels) - .def("getBelType", &Arch::getBelType) - .def("getWireBelPin", &Arch::getWireBelPin) - .def("getBelPinUphill", &Arch::getBelPinUphill) - .def("getBelPinsDownhill", &Arch::getBelPinsDownhill) - .def("getWires", &Arch::getWires) - .def("getPipByName", &Arch::getPipByName) - .def("getPipName", &Arch::getPipName) - .def("getPips", &Arch::getPips) - .def("getPipSrcWire", &Arch::getPipSrcWire) - .def("getPipDstWire", &Arch::getPipDstWire) - .def("getPipDelay", &Arch::getPipDelay) - .def("getPipsDownhill", &Arch::getPipsDownhill) - .def("getPipsUphill", &Arch::getPipsUphill) - .def("getWireAliases", &Arch::getWireAliases) - .def("estimatePosition", &Arch::estimatePosition) - .def("estimateDelay", &Arch::estimateDelay); + auto arch_cls = class_, boost::noncopyable>("Arch", init()) + .def("getBelByName", &Arch::getBelByName) + .def("getWireByName", &Arch::getWireByName) + .def("getBelName", &Arch::getBelName) + .def("getWireName", &Arch::getWireName) + .def("getBels", &Arch::getBels) + .def("getWireBelPin", &Arch::getWireBelPin) + .def("getBelPinUphill", &Arch::getBelPinUphill) + .def("getBelPinsDownhill", &Arch::getBelPinsDownhill) + .def("getWires", &Arch::getWires) + .def("getPipByName", &Arch::getPipByName) + .def("getPipName", &Arch::getPipName) + .def("getPips", &Arch::getPips) + .def("getPipSrcWire", &Arch::getPipSrcWire) + .def("getPipDstWire", &Arch::getPipDstWire) + .def("getPipDelay", &Arch::getPipDelay) + .def("getPipsDownhill", &Arch::getPipsDownhill) + .def("getPipsUphill", &Arch::getPipsUphill) + .def("getWireAliases", &Arch::getWireAliases) + .def("estimatePosition", &Arch::estimatePosition) + .def("estimateDelay", &Arch::estimateDelay); + + /*fn_wrapper, + conv_to_str>::def_wrap(arch_cls, "getBelType");*/ WRAP_RANGE(Bel); WRAP_RANGE(BelPin); -- cgit v1.2.3