From 46db5001234ff42cd5b94407c2122bbf0b2994df Mon Sep 17 00:00:00 2001 From: David Shah Date: Wed, 4 Jul 2018 14:44:45 +0200 Subject: python: More design-related bindings, dump_design.py working again Signed-off-by: David Shah --- common/pybindings.cc | 70 +++++++++++++++++++++++++++++++++++---------------- common/pycontainers.h | 14 +++++++++-- common/pywrappers.h | 65 +++++++++++++++++++++++++++++++++++++++++++++-- python/dump_design.py | 2 +- 4 files changed, 125 insertions(+), 26 deletions(-) diff --git a/common/pybindings.cc b/common/pybindings.cc index a85c5fe2..9bca307c 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -82,43 +82,71 @@ BOOST_PYTHON_MODULE(MODULE_NAME) .def_readwrite("y2", &GraphicElement::y2) .def_readwrite("text", &GraphicElement::text); - class_("PortRef").def_readwrite("cell", &PortRef::cell).def_readwrite("port", &PortRef::port); - - class_("NetInfo") - .def_readwrite("name", &NetInfo::name) - .def_readwrite("driver", &NetInfo::driver) - .def_readwrite("users", &NetInfo::users) - .def_readwrite("attrs", &NetInfo::attrs) - .def_readwrite("wires", &NetInfo::wires); - - // WRAP_MAP(decltype(NetInfo::attrs), "IdStrMap"); - - class_>("PortRefVector").def(vector_indexing_suite>()); - enum_("PortType") .value("PORT_IN", PORT_IN) .value("PORT_OUT", PORT_OUT) .value("PORT_INOUT", PORT_INOUT) .export_values(); - class_("PortInfo") - .def_readwrite("name", &PortInfo::name) - .def_readwrite("net", &PortInfo::net) - .def_readwrite("type", &PortInfo::type); + typedef std::unordered_map AttrMap; + typedef std::unordered_map PortMap; + typedef std::unordered_map PinMap; class_("BaseCtx", no_init); - typedef std::unordered_map AttrMap; - auto ci_cls = class_>("CellInfo", no_init); - readonly_wrapper>::def_wrap(ci_cls, - "type"); + readwrite_wrapper, + conv_from_str>::def_wrap(ci_cls, "name"); + readwrite_wrapper, + conv_from_str>::def_wrap(ci_cls, "type"); readonly_wrapper>::def_wrap( ci_cls, "attrs"); + readonly_wrapper>::def_wrap( + ci_cls, "params"); + readonly_wrapper>::def_wrap( + ci_cls, "ports"); + readwrite_wrapper, + conv_from_str>::def_wrap(ci_cls, "bel"); + readwrite_wrapper, + pass_through>::def_wrap(ci_cls, "belStrength"); + readonly_wrapper>::def_wrap(ci_cls, + "pins"); + + auto pi_cls = class_>("PortInfo", no_init); + readwrite_wrapper, + conv_from_str>::def_wrap(pi_cls, "name"); + readonly_wrapper>::def_wrap(pi_cls, + "net"); + readwrite_wrapper, + pass_through>::def_wrap(pi_cls, "type"); + + typedef std::vector PortVector; + typedef std::unordered_map WireMap; + + auto ni_cls = class_>("NetInfo", no_init); + readwrite_wrapper, + conv_from_str>::def_wrap(ni_cls, "name"); + readwrite_wrapper, + unwrap_context>::def_wrap(ni_cls, "driver"); + readonly_wrapper>::def_wrap( + ni_cls, "users"); + readonly_wrapper>::def_wrap(ni_cls, + "wires"); + + auto pr_cls = class_>("PortRef", no_init); + readonly_wrapper>::def_wrap(pr_cls, + "cell"); + readwrite_wrapper, + conv_from_str>::def_wrap(pr_cls, "port"); + readwrite_wrapper, + pass_through>::def_wrap(pr_cls, "budget"); + def("parse_json", parse_json_shim); def("load_design", load_design_shim, return_value_policy()); WRAP_MAP(AttrMap, pass_through, "AttrMap"); + WRAP_MAP(PortMap, wrap_context, "PortMap"); + WRAP_MAP(PinMap, conv_to_str, "PinMap"); arch_wrap_python(); } diff --git a/common/pycontainers.h b/common/pycontainers.h index c8d2257b..260a9dd6 100644 --- a/common/pycontainers.h +++ b/common/pycontainers.h @@ -276,6 +276,11 @@ template struct map_wrapper x.base[PythonConversion::string_converter().from_str(x.ctx, i)] = v; } + static size_t len(wrapped_map &x) + { + return x.base.size(); + } + static void del(T const &x, std::string const &i) { K k = PythonConversion::string_converter().from_str(x.ctx, i); @@ -293,7 +298,7 @@ template struct map_wrapper typename rw::iter_wrap().wrap(iter_name); class_(map_name, no_init) .def("__iter__", rw::iter) - .def("__len__", &T::size) + .def("__len__", len) .def("__getitem__", get) .def("__setitem__", set, with_custodian_and_ward<1, 2>()); } @@ -394,6 +399,11 @@ template struct map_wrapper_uptr x.base[PythonConversion::string_converter().from_str(x.ctx, i)] = typename T::mapped_type(v); } + static size_t len(wrapped_map &x) + { + return x.base.size(); + } + static void del(T const &x, std::string const &i) { K k = PythonConversion::string_converter().from_str(x.ctx, i); @@ -411,7 +421,7 @@ template struct map_wrapper_uptr typename rw::iter_wrap().wrap(iter_name); class_(map_name, no_init) .def("__iter__", rw::iter) - .def("__len__", &T::size) + .def("__len__", len) .def("__getitem__", get) .def("__setitem__", set, with_custodian_and_ward<1, 2>()); } diff --git a/common/pywrappers.h b/common/pywrappers.h index 560d645d..0643eb0a 100644 --- a/common/pywrappers.h +++ b/common/pywrappers.h @@ -61,6 +61,7 @@ template inline Context *get_ctx(typename WrapIfNotContext::mayb { return wrp_ctx.ctx; } + template <> inline Context *get_ctx(WrapIfNotContext::maybe_wrapped_t &unwrp_ctx) { return &unwrp_ctx; @@ -70,6 +71,7 @@ template inline T &get_base(typename WrapIfNotContext::maybe_wra { return wrp_ctx.base; } + template <> inline Context &get_base(WrapIfNotContext::maybe_wrapped_t &unwrp_ctx) { return unwrp_ctx; @@ -80,6 +82,10 @@ template ContextualWrapper wrap_ctx(Context *ctx, T x) { return // Dummy class, to be implemented by users template struct string_converter; +class bad_wrap +{ +}; + // Action options template struct pass_through { @@ -92,6 +98,7 @@ template struct pass_through template struct wrap_context { inline ContextualWrapper operator()(Context *ctx, T x) { return ContextualWrapper(ctx, x); } + using arg_type = T; using ret_type = ContextualWrapper; }; @@ -99,6 +106,7 @@ template struct wrap_context template struct unwrap_context { inline T operator()(Context *ctx, ContextualWrapper x) { return x.base; } + using ret_type = T; using arg_type = ContextualWrapper; }; @@ -106,6 +114,7 @@ template struct unwrap_context template struct conv_from_str { inline T operator()(Context *ctx, std::string x) { return string_converter().from_str(ctx, x); } + using ret_type = T; using arg_type = std::string; }; @@ -113,10 +122,24 @@ template struct conv_from_str template struct conv_to_str { inline std::string operator()(Context *ctx, T x) { return string_converter().to_str(ctx, x); } + using ret_type = std::string; using arg_type = T; }; +template struct deref_and_wrap +{ + inline ContextualWrapper operator()(Context *ctx, T *x) + { + if (x == nullptr) + throw bad_wrap(); + return ContextualWrapper(ctx, *x); + } + + using arg_type = T *; + using ret_type = ContextualWrapper; +}; + // Function wrapper // Zero parameters, one return template struct fn_wrapper_0a @@ -133,6 +156,7 @@ template struct fn_ template static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } }; + // One parameter, one return template struct fn_wrapper_1a { @@ -204,6 +228,7 @@ template struct fn_wrapper_0a_v template static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); } }; + // One parameter, void template struct fn_wrapper_1a_v { @@ -262,11 +287,15 @@ template struct reado using class_type = typename WrapIfNotContext::maybe_wrapped_t; using conv_val_type = typename v_conv::ret_type; - static conv_val_type wrapped_getter(class_type &cls) + static object wrapped_getter(class_type &cls) { Context *ctx = get_ctx(cls); Class &base = get_base(cls); - return v_conv()(ctx, (base.*mem)); + try { + return object(v_conv()(ctx, (base.*mem))); + } catch (bad_wrap &) { + return object(); + } } template static void def_wrap(WrapCls cls_, const char *name) @@ -275,6 +304,38 @@ template struct reado } }; +// Wrapped getter/setter +template struct readwrite_wrapper +{ + using class_type = typename WrapIfNotContext::maybe_wrapped_t; + using conv_val_type = typename get_conv::ret_type; + + static object wrapped_getter(class_type &cls) + { + Context *ctx = get_ctx(cls); + Class &base = get_base(cls); + try { + return object(get_conv()(ctx, (base.*mem))); + } catch (bad_wrap &) { + return object(); + } + } + + using conv_arg_type = typename set_conv::arg_type; + + static void wrapped_setter(class_type &cls, conv_arg_type val) + { + Context *ctx = get_ctx(cls); + Class &base = get_base(cls); + (base.*mem) = set_conv()(ctx, val); + } + + template static void def_wrap(WrapCls cls_, const char *name) + { + cls_.add_property(name, wrapped_getter, wrapped_setter); + } +}; + } // namespace PythonConversion NEXTPNR_NAMESPACE_END diff --git a/python/dump_design.py b/python/dump_design.py index 37309f6f..c75df9a1 100644 --- a/python/dump_design.py +++ b/python/dump_design.py @@ -21,5 +21,5 @@ for cell, cinfo in sorted(ctx.cells, key=lambda x: x.first): print("\t\t{}: {}".format(param, val)) if cinfo.bel.index != -1: - print("\tBel: {}".format(ctx.getBelName(cinfo.bel))) + print("\tBel: {}".format(cinfo.bel)) print() -- cgit v1.2.3