diff options
author | David Shah <davey1576@gmail.com> | 2018-07-02 15:13:09 +0200 |
---|---|---|
committer | David Shah <davey1576@gmail.com> | 2018-07-04 14:55:24 +0200 |
commit | 65195513eb2676d974f1cef94be5e84f42a52f0f (patch) | |
tree | 9cec33a31f9b15d4bf1bdc670231e411bc9f1fef | |
parent | 2e8c0c872f99276602f9cd0daf06bfaf11832a08 (diff) | |
download | nextpnr-65195513eb2676d974f1cef94be5e84f42a52f0f.tar.gz nextpnr-65195513eb2676d974f1cef94be5e84f42a52f0f.tar.bz2 nextpnr-65195513eb2676d974f1cef94be5e84f42a52f0f.zip |
python: Restructuring wrapper system
Signed-off-by: David Shah <davey1576@gmail.com>
-rw-r--r-- | common/pywrappers.h | 57 | ||||
-rw-r--r-- | 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 <typename T> 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 <typename T> struct WrapIfNotContext { + typedef ContextualWrapper<T> maybe_wrapped_t; +}; + +template <> struct WrapIfNotContext<Context> { + typedef Context maybe_wrapped_t; +}; + + +template <typename T> inline Context *get_ctx(typename WrapIfNotContext<T>::maybe_wrapped_t &wrp_ctx) { + return wrp_ctx.ctx; +} +template <> inline Context *get_ctx<Context>(WrapIfNotContext<Context>::maybe_wrapped_t &unwrp_ctx) { + return &unwrp_ctx; +} + +template <typename T> inline T&get_base(typename WrapIfNotContext<T>::maybe_wrapped_t &wrp_ctx) { + return wrp_ctx.base; +} +template <> inline Context &get_base<Context>(WrapIfNotContext<Context>::maybe_wrapped_t &unwrp_ctx) { + return unwrp_ctx; +} + template <typename T> ContextualWrapper<T> wrap_ctx(Context *ctx, T x) { return ContextualWrapper<T>(ctx, x); } // Dummy class, to be implemented by users -template <typename T> class string_converter; +template <typename T> struct string_converter; // Action options -template <typename T> class do_nothing +template <typename T> 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 <typename T> class wrap_context +template <typename T> struct wrap_context { - ContextualWrapper<T> operator()(Context *ctx, T x) { return ContextualWrapper<T>(ctx, x); } + inline ContextualWrapper<T> operator()(Context *ctx, T x) { return ContextualWrapper<T>(ctx, x); } using arg_type = T; using ret_type = ContextualWrapper<T>; }; -template <typename T> class unwrap_context +template <typename T> struct unwrap_context { - T operator()(Context *ctx, ContextualWrapper<T> x) { return x.base; } + inline T operator()(Context *ctx, ContextualWrapper<T> x) { return x.base; } using ret_type = T; using arg_type = ContextualWrapper<T>; }; -template <typename T> class conv_from_string +template <typename T> struct conv_from_str { - T operator()(Context *ctx, std::string x) { return string_converter<T>().from_str(ctx, x); } + inline T operator()(Context *ctx, std::string x) { return string_converter<T>().from_str(ctx, x); } using ret_type = T; using arg_type = std::string; }; -template <typename T> class conv_to_string +template <typename T> struct conv_to_str { - std::string operator()(Context *ctx, T x) { return string_converter<T>().to_str(ctx, x); } + inline std::string operator()(Context *ctx, T x) { return string_converter<T>().to_str(ctx, x); } using ret_type = std::string; using arg_type = T; }; // Function wrapper // Example: one parameter, one return -template <typename Class, typename FuncT, FuncT fn, typename rv_conv, typename arg1_conv> struct function_wrapper +template <typename Class, typename FuncT, FuncT fn, typename rv_conv, typename arg1_conv> struct fn_wrapper { - using class_type = ContextualWrapper<Class>; + using class_type = typename WrapIfNotContext<Class>::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 <typename WrapCls> 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> +{ + 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> +{ + 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>("ArchArgs").def_readwrite("type", &ArchArgs::type); enum_<decltype(std::declval<ArchArgs>().type)>("iCE40Type") @@ -53,28 +72,30 @@ void arch_wrap_python() ; #undef X - class_<Arch, Arch *, bases<BaseCtx>, boost::noncopyable>("Arch", init<ArchArgs>()) - .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_<Arch, Arch *, bases<BaseCtx>, boost::noncopyable>("Arch", init<ArchArgs>()) + .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<Arch, typeof(&Arch::getBelType), &Arch::getBelType, conv_from_str<BelId>, + conv_to_str<BelType>>::def_wrap(arch_cls, "getBelType");*/ WRAP_RANGE(Bel); WRAP_RANGE(BelPin); |