diff options
-rw-r--r-- | common/pybindings.cc | 23 | ||||
-rw-r--r-- | common/pycontainers.h | 68 | ||||
-rw-r--r-- | ice40/arch_pybindings.h | 70 | ||||
-rw-r--r-- | ice40/pybindings.cc | 75 |
4 files changed, 137 insertions, 99 deletions
diff --git a/common/pybindings.cc b/common/pybindings.cc index 5ad8a983..a85c5fe2 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -21,6 +21,7 @@ #ifndef NO_PYTHON #include "pybindings.h" +#include "arch_pybindings.h" #include "jsonparse.h" #include "nextpnr.h" @@ -105,32 +106,20 @@ BOOST_PYTHON_MODULE(MODULE_NAME) .def_readwrite("net", &PortInfo::net) .def_readwrite("type", &PortInfo::type); - /*class_<CellInfo, CellInfo *>("CellInfo") - .def_readwrite("name", &CellInfo::name) - .def_readwrite("type", &CellInfo::type) - .def_readwrite("ports", &CellInfo::ports) - .def_readwrite("attrs", &CellInfo::attrs) - .def_readwrite("params", &CellInfo::params) - .def_readwrite("bel", &CellInfo::bel) - .def_readwrite("pins", &CellInfo::pins);*/ - - // WRAP_MAP(decltype(CellInfo::ports), "IdPortMap"); - // WRAP_MAP(decltype(CellInfo::pins), "IdIdMap"); - class_<BaseCtx, BaseCtx *, boost::noncopyable>("BaseCtx", no_init); - //.add_property("nets", make_getter(&Context::nets, return_internal_reference<>())) - //.add_property("cells", make_getter(&Context::cells, return_internal_reference<>())); - // WRAP_MAP_UPTR(decltype(Context::nets), "IdNetMap"); - // WRAP_MAP_UPTR(decltype(Context::cells), "IdCellMap"); + typedef std::unordered_map<IdString, std::string> AttrMap; 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"); - + readonly_wrapper<CellInfo &, typeof(&CellInfo::attrs), &CellInfo::attrs, wrap_context<AttrMap &>>::def_wrap( + ci_cls, "attrs"); def("parse_json", parse_json_shim); def("load_design", load_design_shim, return_value_policy<manage_new_object>()); + WRAP_MAP(AttrMap, pass_through<std::string>, "AttrMap"); + arch_wrap_python(); } diff --git a/common/pycontainers.h b/common/pycontainers.h index cc3c2c84..c8d2257b 100644 --- a/common/pycontainers.h +++ b/common/pycontainers.h @@ -188,20 +188,23 @@ template <typename T1, typename T2> struct pair_wrapper /* Special case of above for map key/values */ -template <typename T1, typename T2> struct map_pair_wrapper +template <typename T1, typename T2, typename value_conv> struct map_pair_wrapper { typedef std::pair<T1, T2> T; + typedef PythonConversion::ContextualWrapper<T &> wrapped_pair; + typedef typename T::second_type V; struct pair_iterator_wrapper { - static object next(std::pair<T &, int> &iter) + static object next(std::pair<wrapped_pair &, int> &iter) { if (iter.second == 0) { iter.second++; - return object(iter.first.first); + return object(PythonConversion::string_converter<typeof(iter.first.base.first)>().to_str( + iter.first.ctx, iter.first.base.first)); } else if (iter.second == 1) { iter.second++; - return object(iter.first.second); + return object(value_conv()(iter.first.ctx, iter.first.base.second)); } else { PyErr_SetString(PyExc_StopIteration, "End of range reached"); boost::python::throw_error_already_set(); @@ -213,30 +216,37 @@ template <typename T1, typename T2> struct map_pair_wrapper static void wrap(const char *python_name) { - class_<std::pair<T &, int>>(python_name, no_init).def("__next__", next); + class_<std::pair<wrapped_pair &, int>>(python_name, no_init).def("__next__", next); } }; - static object get(T &x, int i) + static object get(wrapped_pair &x, int i) { if ((i >= 2) || (i < 0)) KeyError(); - return (i == 1) ? object(x.second) : object(x.first); + return (i == 1) ? object(value_conv()(x.ctx, x.base.second)) : object(x.base.first); } - static int len(T &x) { return 2; } + static int len(wrapped_pair &x) { return 2; } - static std::pair<T &, int> iter(T &x) { return std::make_pair(boost::ref(x), 0); }; + static std::pair<wrapped_pair &, int> iter(wrapped_pair &x) { return std::make_pair(boost::ref(x), 0); }; + + static std::string first_getter(wrapped_pair &t) + { + return PythonConversion::string_converter<typeof(t.base.first)>().to_str(t.ctx, t.base.first); + } + + static typename value_conv::ret_type second_getter(wrapped_pair &t) { return value_conv()(t.ctx, t.base.second); } static void wrap(const char *pair_name, const char *iter_name) { pair_iterator_wrapper::wrap(iter_name); - class_<T>(pair_name, no_init) + class_<wrapped_pair>(pair_name, no_init) .def("__iter__", iter) .def("__len__", len) .def("__getitem__", get) - .def_readonly("first", &T::first) - .def_readwrite("second", &T::second); + .add_property("first", first_getter) + .add_property("second", second_getter); } }; @@ -244,26 +254,33 @@ template <typename T1, typename T2> struct map_pair_wrapper Wrapper for a map, either an unordered_map, regular map or dict */ -template <typename T> struct map_wrapper +template <typename T, typename value_conv> struct map_wrapper { typedef typename std::remove_cv<typename std::remove_reference<typename T::key_type>::type>::type K; typedef typename T::mapped_type V; + typedef typename value_conv::ret_type wrapped_V; typedef typename T::value_type KV; + typedef typename PythonConversion::ContextualWrapper<T &> wrapped_map; - static V &get(T &x, K const &i) + static wrapped_V get(wrapped_map &x, std::string const &i) { - if (x.find(i) != x.end()) - return x.at(i); + K k = PythonConversion::string_converter<K>().from_str(x.ctx, i); + if (x.base.find(k) != x.base.end()) + return value_conv()(x.ctx, x.base.at(k)); KeyError(); std::terminate(); } - static void set(T &x, K const &i, V &v) { x[i] = v; } + static void set(wrapped_map &x, std::string const &i, V const &v) + { + x.base[PythonConversion::string_converter<K>().from_str(x.ctx, i)] = v; + } - static void del(T const &x, K const &i) + static void del(T const &x, std::string const &i) { - if (x.find(i) != x.end()) - x.erase(i); + K k = PythonConversion::string_converter<K>().from_str(x.ctx, i); + if (x.base.find(k) != x.base.end()) + x.base.erase(k); else KeyError(); std::terminate(); @@ -271,13 +288,13 @@ template <typename T> struct map_wrapper static void wrap(const char *map_name, const char *kv_name, const char *kv_iter_name, const char *iter_name) { - map_pair_wrapper<typename KV::first_type, typename KV::second_type>::wrap(kv_name, kv_iter_name); - typedef range_wrapper<T, return_value_policy<copy_non_const_reference>> rw; + map_pair_wrapper<typename KV::first_type, typename KV::second_type, value_conv>::wrap(kv_name, kv_iter_name); + typedef range_wrapper<T &, return_value_policy<return_by_value>, PythonConversion::wrap_context<KV &>> rw; typename rw::iter_wrap().wrap(iter_name); - class_<T>(map_name, no_init) + class_<wrapped_map>(map_name, no_init) .def("__iter__", rw::iter) .def("__len__", &T::size) - .def("__getitem__", get, return_internal_reference<>()) + .def("__getitem__", get) .def("__setitem__", set, with_custodian_and_ward<1, 2>()); } }; @@ -400,7 +417,8 @@ template <typename T> struct map_wrapper_uptr } }; -#define WRAP_MAP(t, name) map_wrapper<t>().wrap(#name, #name "KeyValue", #name "KeyValueIter", #name "Iterator") +#define WRAP_MAP(t, conv, name) \ + map_wrapper<t, conv>().wrap(#name, #name "KeyValue", #name "KeyValueIter", #name "Iterator") #define WRAP_MAP_UPTR(t, name) \ map_wrapper_uptr<t>().wrap(#name, #name "KeyValue", #name "KeyValueIter", #name "Iterator") diff --git a/ice40/arch_pybindings.h b/ice40/arch_pybindings.h new file mode 100644 index 00000000..42fe8a6d --- /dev/null +++ b/ice40/arch_pybindings.h @@ -0,0 +1,70 @@ +/* + * nextpnr -- Next Generation Place and Route + * + * Copyright (C) 2018 Clifford Wolf <clifford@symbioticeda.com> + * Copyright (C) 2018 David Shah <david@symbioticeda.com> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ +#ifndef ARCH_PYBINDINGS_H +#define ARCH_PYBINDINGS_H +#ifndef NO_PYTHON + +#include "pybindings.h" +#include "nextpnr.h" + +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); } +}; + +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); } +}; + +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 + +NEXTPNR_NAMESPACE_END +#endif +#endif diff --git a/ice40/pybindings.cc b/ice40/pybindings.cc index 532ba4e7..bcf6039d 100644 --- a/ice40/pybindings.cc +++ b/ice40/pybindings.cc @@ -21,49 +21,11 @@ #ifndef NO_PYTHON #include "pybindings.h" +#include "arch_pybindings.h" #include "nextpnr.h" 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); } -}; - -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); } -}; - -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() { using namespace PythonConversion; @@ -125,17 +87,17 @@ void arch_wrap_python() wrap_context<BelPinRange>, conv_from_str<WireId>>::def_wrap(ctx_cls, "getBelPinsDownhill"); fn_wrapper_1a<Context, typeof(&Context::getWireChecksum), &Context::getWireChecksum, pass_through<uint32_t>, - conv_from_str<WireId>>::def_wrap(ctx_cls, "getWireChecksum"); + conv_from_str<WireId>>::def_wrap(ctx_cls, "getWireChecksum"); fn_wrapper_3a_v<Context, typeof(&Context::bindWire), &Context::bindWire, conv_from_str<WireId>, - conv_from_str<IdString>, pass_through<PlaceStrength>>::def_wrap(ctx_cls, "bindWire"); + conv_from_str<IdString>, pass_through<PlaceStrength>>::def_wrap(ctx_cls, "bindWire"); fn_wrapper_1a_v<Context, typeof(&Context::unbindWire), &Context::unbindWire, conv_from_str<WireId>>::def_wrap( ctx_cls, "unbindWire"); fn_wrapper_1a<Context, typeof(&Context::checkWireAvail), &Context::checkWireAvail, pass_through<bool>, - conv_from_str<WireId>>::def_wrap(ctx_cls, "checkWireAvail"); + conv_from_str<WireId>>::def_wrap(ctx_cls, "checkWireAvail"); fn_wrapper_1a<Context, typeof(&Context::getBoundWireNet), &Context::getBoundWireNet, conv_to_str<IdString>, - conv_from_str<WireId>>::def_wrap(ctx_cls, "getBoundWireNet"); - fn_wrapper_1a<Context, typeof(&Context::getConflictingWireNet), &Context::getConflictingWireNet, conv_to_str<IdString>, - conv_from_str<WireId>>::def_wrap(ctx_cls, "getConflictingWireNet"); + conv_from_str<WireId>>::def_wrap(ctx_cls, "getBoundWireNet"); + fn_wrapper_1a<Context, typeof(&Context::getConflictingWireNet), &Context::getConflictingWireNet, + conv_to_str<IdString>, conv_from_str<WireId>>::def_wrap(ctx_cls, "getConflictingWireNet"); fn_wrapper_0a<Context, typeof(&Context::getWires), &Context::getWires, wrap_context<WireRange>>::def_wrap( ctx_cls, "getWires"); @@ -143,36 +105,36 @@ void arch_wrap_python() fn_wrapper_0a<Context, typeof(&Context::getPips), &Context::getPips, wrap_context<AllPipRange>>::def_wrap( ctx_cls, "getPips"); fn_wrapper_1a<Context, typeof(&Context::getPipChecksum), &Context::getPipChecksum, pass_through<uint32_t>, - conv_from_str<PipId>>::def_wrap(ctx_cls, "getPipChecksum"); + conv_from_str<PipId>>::def_wrap(ctx_cls, "getPipChecksum"); fn_wrapper_3a_v<Context, typeof(&Context::bindPip), &Context::bindPip, conv_from_str<PipId>, - conv_from_str<IdString>, pass_through<PlaceStrength>>::def_wrap(ctx_cls, "bindPip"); + conv_from_str<IdString>, pass_through<PlaceStrength>>::def_wrap(ctx_cls, "bindPip"); fn_wrapper_1a_v<Context, typeof(&Context::unbindPip), &Context::unbindPip, conv_from_str<PipId>>::def_wrap( ctx_cls, "unbindPip"); fn_wrapper_1a<Context, typeof(&Context::checkPipAvail), &Context::checkPipAvail, pass_through<bool>, - conv_from_str<PipId>>::def_wrap(ctx_cls, "checkPipAvail"); + conv_from_str<PipId>>::def_wrap(ctx_cls, "checkPipAvail"); fn_wrapper_1a<Context, typeof(&Context::getBoundPipNet), &Context::getBoundPipNet, conv_to_str<IdString>, - conv_from_str<PipId>>::def_wrap(ctx_cls, "getBoundPipNet"); - fn_wrapper_1a<Context, typeof(&Context::getConflictingPipNet), &Context::getConflictingPipNet, conv_to_str<IdString>, - conv_from_str<PipId>>::def_wrap(ctx_cls, "getConflictingPipNet"); + conv_from_str<PipId>>::def_wrap(ctx_cls, "getBoundPipNet"); + fn_wrapper_1a<Context, typeof(&Context::getConflictingPipNet), &Context::getConflictingPipNet, + conv_to_str<IdString>, conv_from_str<PipId>>::def_wrap(ctx_cls, "getConflictingPipNet"); fn_wrapper_1a<Context, typeof(&Context::getPipsDownhill), &Context::getPipsDownhill, wrap_context<PipRange>, conv_from_str<WireId>>::def_wrap(ctx_cls, "getPipsDownhill"); fn_wrapper_1a<Context, typeof(&Context::getPipsUphill), &Context::getPipsUphill, wrap_context<PipRange>, conv_from_str<WireId>>::def_wrap(ctx_cls, "getPipsUphill"); fn_wrapper_1a<Context, typeof(&Context::getWireAliases), &Context::getWireAliases, wrap_context<PipRange>, - conv_from_str<WireId>>::def_wrap(ctx_cls, "getWireAliases"); + conv_from_str<WireId>>::def_wrap(ctx_cls, "getWireAliases"); fn_wrapper_1a<Context, typeof(&Context::getPipSrcWire), &Context::getPipSrcWire, conv_to_str<WireId>, conv_from_str<PipId>>::def_wrap(ctx_cls, "getPipSrcWire"); fn_wrapper_1a<Context, typeof(&Context::getPipDstWire), &Context::getPipDstWire, conv_to_str<WireId>, conv_from_str<PipId>>::def_wrap(ctx_cls, "getPipDstWire"); fn_wrapper_1a<Context, typeof(&Context::getPipDelay), &Context::getPipDelay, pass_through<DelayInfo>, - conv_from_str<PipId>>::def_wrap(ctx_cls, "getPipDelay"); + conv_from_str<PipId>>::def_wrap(ctx_cls, "getPipDelay"); fn_wrapper_1a<Context, typeof(&Context::getPackagePinBel), &Context::getPackagePinBel, conv_to_str<BelId>, - pass_through<std::string>>::def_wrap(ctx_cls, "getPackagePinBel"); + pass_through<std::string>>::def_wrap(ctx_cls, "getPackagePinBel"); fn_wrapper_1a<Context, typeof(&Context::getBelPackagePin), &Context::getBelPackagePin, pass_through<std::string>, - conv_from_str<BelId>>::def_wrap(ctx_cls, "getBelPackagePin"); + conv_from_str<BelId>>::def_wrap(ctx_cls, "getBelPackagePin"); fn_wrapper_0a<Context, typeof(&Context::getChipName), &Context::getChipName, pass_through<std::string>>::def_wrap( ctx_cls, "getChipName"); @@ -185,7 +147,7 @@ void arch_wrap_python() readonly_wrapper<Context, typeof(&Context::cells), &Context::cells, wrap_context<CellMap &>>::def_wrap(ctx_cls, "cells"); readonly_wrapper<Context, typeof(&Context::nets), &Context::nets, wrap_context<NetMap &>>::def_wrap(ctx_cls, - "nets"); + "nets"); WRAP_RANGE(Bel, conv_to_str<BelId>); WRAP_RANGE(Wire, conv_to_str<WireId>); WRAP_RANGE(AllPip, conv_to_str<PipId>); @@ -193,7 +155,6 @@ void arch_wrap_python() WRAP_MAP_UPTR(CellMap, "IdCellMap"); WRAP_MAP_UPTR(NetMap, "IdNetMap"); - } NEXTPNR_NAMESPACE_END |