diff options
author | David Shah <davey1576@gmail.com> | 2018-07-04 13:35:15 +0200 |
---|---|---|
committer | David Shah <davey1576@gmail.com> | 2018-07-04 14:55:24 +0200 |
commit | f6432aa88e8a9d6347a60f9898e74a4ebc1b2b3a (patch) | |
tree | 5c97f25e9b9bfe165ef650e21c42b103a996e193 | |
parent | f8ad2b31a9a1a43ac415e8b203851c6f091dde6f (diff) | |
download | nextpnr-f6432aa88e8a9d6347a60f9898e74a4ebc1b2b3a.tar.gz nextpnr-f6432aa88e8a9d6347a60f9898e74a4ebc1b2b3a.tar.bz2 nextpnr-f6432aa88e8a9d6347a60f9898e74a4ebc1b2b3a.zip |
python: Adding more wrapped bindings for ice40
Signed-off-by: David Shah <davey1576@gmail.com>
-rw-r--r-- | common/pybindings.cc | 9 | ||||
-rw-r--r-- | common/pycontainers.h | 2 | ||||
-rw-r--r-- | common/pywrappers.h | 106 | ||||
-rw-r--r-- | ice40/pybindings.cc | 48 |
4 files changed, 137 insertions, 28 deletions
diff --git a/common/pybindings.cc b/common/pybindings.cc index ea57dbbb..5ad8a983 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -61,7 +61,8 @@ Context *load_design_shim(std::string filename, ArchArgs args) return d; } -void translate_assertfail(const assertion_failure &e) { +void translate_assertfail(const assertion_failure &e) +{ // Use the Python 'C' API to set up an exception object PyErr_SetString(PyExc_AssertionError, e.what()); } @@ -123,9 +124,9 @@ BOOST_PYTHON_MODULE(MODULE_NAME) // WRAP_MAP_UPTR(decltype(Context::nets), "IdNetMap"); // WRAP_MAP_UPTR(decltype(Context::cells), "IdCellMap"); - auto ci_cls = class_<ContextualWrapper<CellInfo&>>("CellInfo", no_init); - readonly_wrapper<CellInfo&, typeof(&CellInfo::type), &CellInfo::type, conv_to_str<IdString>>::def_wrap(ci_cls, - "type"); + auto ci_cls = class_<ContextualWrapper<CellInfo &>>("CellInfo", no_init); + readonly_wrapper<CellInfo &, typeof(&CellInfo::type), &CellInfo::type, conv_to_str<IdString>>::def_wrap(ci_cls, + "type"); def("parse_json", parse_json_shim); def("load_design", load_design_shim, return_value_policy<manage_new_object>()); diff --git a/common/pycontainers.h b/common/pycontainers.h index 0257e9e4..cc3c2c84 100644 --- a/common/pycontainers.h +++ b/common/pycontainers.h @@ -390,7 +390,7 @@ template <typename T> struct map_wrapper_uptr static void wrap(const char *map_name, const char *kv_name, const char *kv_iter_name, const char *iter_name) { map_pair_wrapper_uptr<typename KV::first_type, typename KV::second_type>::wrap(kv_name, kv_iter_name); - typedef range_wrapper<T&, return_value_policy<return_by_value>, PythonConversion::wrap_context<KV &>> rw; + typedef range_wrapper<T &, return_value_policy<return_by_value>, PythonConversion::wrap_context<KV &>> rw; typename rw::iter_wrap().wrap(iter_name); class_<wrapped_map>(map_name, no_init) .def("__iter__", rw::iter) diff --git a/common/pywrappers.h b/common/pywrappers.h index 125308e3..560d645d 100644 --- a/common/pywrappers.h +++ b/common/pywrappers.h @@ -150,6 +150,112 @@ template <typename Class, typename FuncT, FuncT fn, typename rv_conv, typename a template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } }; +// Two parameters, one return +template <typename Class, typename FuncT, FuncT fn, typename rv_conv, typename arg1_conv, typename arg2_conv> +struct fn_wrapper_2a +{ + 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; + using conv_arg2_type = typename arg2_conv::arg_type; + + static conv_result_type wrapped_fn(class_type &cls, conv_arg1_type arg1, conv_arg2_type arg2) + { + Context *ctx = get_ctx<Class>(cls); + Class &base = get_base<Class>(cls); + return rv_conv()(ctx, (base.*fn)(arg1_conv()(ctx, arg1), arg2_conv()(ctx, arg2))); + } + + template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } +}; + +// Three parameters, one return +template <typename Class, typename FuncT, FuncT fn, typename rv_conv, typename arg1_conv, typename arg2_conv, + typename arg3_conv> +struct fn_wrapper_3a +{ + 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; + using conv_arg2_type = typename arg2_conv::arg_type; + using conv_arg3_type = typename arg3_conv::arg_type; + + static conv_result_type wrapped_fn(class_type &cls, conv_arg1_type arg1, conv_arg2_type arg2, conv_arg3_type arg3) + { + Context *ctx = get_ctx<Class>(cls); + Class &base = get_base<Class>(cls); + return rv_conv()(ctx, (base.*fn)(arg1_conv()(ctx, arg1), arg2_conv()(ctx, arg2), arg3_conv()(ctx, arg3))); + } + + template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } +}; + +// Zero parameters void +template <typename Class, typename FuncT, FuncT fn> struct fn_wrapper_0a_v +{ + using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t; + + static void wrapped_fn(class_type &cls) + { + Context *ctx = get_ctx<Class>(cls); + Class &base = get_base<Class>(cls); + return (base.*fn)(); + } + + template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } +}; +// One parameter, void +template <typename Class, typename FuncT, FuncT fn, typename arg1_conv> struct fn_wrapper_1a_v +{ + using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t; + using conv_arg1_type = typename arg1_conv::arg_type; + + static void wrapped_fn(class_type &cls, conv_arg1_type arg1) + { + Context *ctx = get_ctx<Class>(cls); + Class &base = get_base<Class>(cls); + return (base.*fn)(arg1_conv()(ctx, arg1)); + } + + template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } +}; + +// Two parameters, one return +template <typename Class, typename FuncT, FuncT fn, typename arg1_conv, typename arg2_conv> struct fn_wrapper_2a_v +{ + using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t; + using conv_arg1_type = typename arg1_conv::arg_type; + using conv_arg2_type = typename arg2_conv::arg_type; + + static void wrapped_fn(class_type &cls, conv_arg1_type arg1, conv_arg2_type arg2) + { + Context *ctx = get_ctx<Class>(cls); + Class &base = get_base<Class>(cls); + return (base.*fn)(arg1_conv()(ctx, arg1), arg2_conv()(ctx, arg2)); + } + + template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } +}; + +// Three parameters, one return +template <typename Class, typename FuncT, FuncT fn, typename arg1_conv, typename arg2_conv, typename arg3_conv> +struct fn_wrapper_3a_v +{ + using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t; + using conv_arg1_type = typename arg1_conv::arg_type; + using conv_arg2_type = typename arg2_conv::arg_type; + using conv_arg3_type = typename arg3_conv::arg_type; + + static void wrapped_fn(class_type &cls, conv_arg1_type arg1, conv_arg2_type arg2, conv_arg3_type arg3) + { + Context *ctx = get_ctx<Class>(cls); + Class &base = get_base<Class>(cls); + return (base.*fn)(arg1_conv()(ctx, arg1), arg2_conv()(ctx, arg2), arg3_conv()(ctx, arg3)); + } + + template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } +}; + // Wrapped getter template <typename Class, typename MemT, MemT mem, typename v_conv> struct readonly_wrapper { diff --git a/ice40/pybindings.cc b/ice40/pybindings.cc index 45307477..24e6ae97 100644 --- a/ice40/pybindings.cc +++ b/ice40/pybindings.cc @@ -55,6 +55,13 @@ template <> struct string_converter<PipId> std::string to_str(Context *ctx, PipId id) { return ctx->getPipName(id).str(ctx); } }; +template <> struct string_converter<PortPin> +{ + PortPin from_str(Context *ctx, std::string name) { return ctx->portPinFromId(ctx->id(name)); } + + std::string to_str(Context *ctx, PortPin id) { return ctx->portPinToId(id).str(ctx); } +}; + } // namespace PythonConversion void arch_wrap_python() @@ -95,9 +102,21 @@ void arch_wrap_python() 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_1a<Context, typeof(&Context::getBelChecksum), &Context::getBelChecksum, pass_through<uint32_t>, + conv_from_str<BelId>>::def_wrap(ctx_cls, "getBelChecksum"); + fn_wrapper_3a_v<Context, typeof(&Context::bindBel), &Context::bindBel, conv_from_str<BelId>, + conv_from_str<IdString>, pass_through<PlaceStrength>>::def_wrap(ctx_cls, "bindBel"); + fn_wrapper_1a_v<Context, typeof(&Context::unbindBel), &Context::unbindBel, conv_from_str<BelId>>::def_wrap( + ctx_cls, "unbindBel"); + fn_wrapper_1a<Context, typeof(&Context::getBoundBelCell), &Context::getBoundBelCell, conv_to_str<IdString>, + conv_from_str<BelId>>::def_wrap(ctx_cls, "getBoundBelCell"); + fn_wrapper_1a<Context, typeof(&Context::getConflictingBelCell), &Context::getConflictingBelCell, + conv_to_str<IdString>, conv_from_str<BelId>>::def_wrap(ctx_cls, "getConflictingBelCell"); fn_wrapper_0a<Context, typeof(&Context::getBels), &Context::getBels, wrap_context<BelRange>>::def_wrap(ctx_cls, "getBels"); + fn_wrapper_1a<Context, typeof(&Context::getBelsAtSameTile), &Context::getBelsAtSameTile, wrap_context<BelRange>, + conv_from_str<BelId>>::def_wrap(ctx_cls, "getBelsAtSameTile"); + fn_wrapper_0a<Context, typeof(&Context::getWires), &Context::getWires, wrap_context<WireRange>>::def_wrap( ctx_cls, "getWires"); fn_wrapper_0a<Context, typeof(&Context::getPips), &Context::getPips, wrap_context<AllPipRange>>::def_wrap( @@ -113,32 +132,15 @@ void arch_wrap_python() fn_wrapper_1a<Context, typeof(&Context::getPipDstWire), &Context::getPipDstWire, conv_to_str<WireId>, conv_from_str<PipId>>::def_wrap(ctx_cls, "getPipDstWire"); + fn_wrapper_0a<Context, typeof(&Context::getChipName), &Context::getChipName, pass_through<std::string>>::def_wrap( + ctx_cls, "getChipName"); + fn_wrapper_0a<Context, typeof(&Context::archId), &Context::archId, conv_to_str<IdString>>::def_wrap(ctx_cls, + "archId"); + typedef std::unordered_map<IdString, std::unique_ptr<CellInfo>> CellMap; readonly_wrapper<Context, typeof(&Context::cells), &Context::cells, wrap_context<CellMap &>>::def_wrap(ctx_cls, "cells"); - /* - .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); - */ WRAP_RANGE(Bel, conv_to_str<BelId>); WRAP_RANGE(Wire, conv_to_str<WireId>); |