2018-06-01 21:02:38 +08:00
|
|
|
/*
|
|
|
|
* nextpnr -- Next Generation Place and Route
|
|
|
|
*
|
2018-06-22 22:19:17 +08:00
|
|
|
* Copyright (C) 2018 Clifford Wolf <clifford@symbioticeda.com>
|
2018-06-17 17:49:57 +08:00
|
|
|
* Copyright (C) 2018 David Shah <david@symbioticeda.com>
|
2018-06-01 21:02:38 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-06-23 20:32:18 +08:00
|
|
|
#ifndef NO_PYTHON
|
|
|
|
|
2018-06-12 02:12:57 +08:00
|
|
|
#include "pybindings.h"
|
2018-07-04 20:13:55 +08:00
|
|
|
#include "arch_pybindings.h"
|
2018-06-08 21:53:24 +08:00
|
|
|
#include "jsonparse.h"
|
2018-06-12 02:12:57 +08:00
|
|
|
#include "nextpnr.h"
|
2018-06-07 18:48:53 +08:00
|
|
|
|
2018-06-08 21:53:24 +08:00
|
|
|
#include <fstream>
|
2018-06-26 20:13:52 +08:00
|
|
|
#include <memory>
|
2018-06-14 16:08:54 +08:00
|
|
|
#include <signal.h>
|
2018-06-12 20:24:59 +08:00
|
|
|
NEXTPNR_NAMESPACE_BEGIN
|
|
|
|
|
2018-06-07 19:10:53 +08:00
|
|
|
// Required to determine concatenated module name (which differs for different
|
|
|
|
// archs)
|
|
|
|
#define PASTER(x, y) x##_##y
|
|
|
|
#define EVALUATOR(x, y) PASTER(x, y)
|
2018-06-01 19:57:06 +08:00
|
|
|
#define MODULE_NAME EVALUATOR(nextpnrpy, ARCHNAME)
|
2018-06-06 03:03:06 +08:00
|
|
|
#define PYINIT_MODULE_NAME EVALUATOR(&PyInit_nextpnrpy, ARCHNAME)
|
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
#define TOSTRING(x) STRINGIFY(x)
|
2018-06-01 19:57:06 +08:00
|
|
|
|
2018-06-07 19:10:53 +08:00
|
|
|
// Architecture-specific bindings should be created in the below function, which
|
|
|
|
// must be implemented in all architectures
|
2018-06-01 21:18:18 +08:00
|
|
|
void arch_wrap_python();
|
|
|
|
|
2018-06-23 22:12:52 +08:00
|
|
|
bool operator==(const PortRef &a, const PortRef &b) { return (a.cell == b.cell) && (a.port == b.port); }
|
2018-06-07 17:41:54 +08:00
|
|
|
|
2018-06-08 21:53:24 +08:00
|
|
|
// Load a JSON file into a design
|
2018-06-18 20:06:37 +08:00
|
|
|
void parse_json_shim(std::string filename, Context &d)
|
2018-06-08 21:53:24 +08:00
|
|
|
{
|
|
|
|
std::ifstream inf(filename);
|
|
|
|
if (!inf)
|
|
|
|
throw std::runtime_error("failed to open file " + filename);
|
2018-06-21 22:16:58 +08:00
|
|
|
parse_json_file(inf, filename, &d);
|
2018-06-08 21:53:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new Chip and load design from json file
|
2018-06-26 20:13:52 +08:00
|
|
|
Context *load_design_shim(std::string filename, ArchArgs args)
|
2018-06-08 21:53:24 +08:00
|
|
|
{
|
2018-06-26 20:13:52 +08:00
|
|
|
Context *d = new Context(args);
|
|
|
|
parse_json_shim(filename, *d);
|
2018-06-08 21:53:24 +08:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2018-07-04 19:35:15 +08:00
|
|
|
void translate_assertfail(const assertion_failure &e)
|
|
|
|
{
|
2018-07-04 18:23:35 +08:00
|
|
|
// Use the Python 'C' API to set up an exception object
|
|
|
|
PyErr_SetString(PyExc_AssertionError, e.what());
|
|
|
|
}
|
|
|
|
|
2018-11-22 20:35:07 +08:00
|
|
|
namespace PythonConversion {
|
|
|
|
template <> struct string_converter<PortRef &>
|
|
|
|
{
|
|
|
|
inline PortRef from_str(Context *ctx, std::string name) { NPNR_ASSERT_FALSE("PortRef from_str not implemented"); }
|
|
|
|
|
|
|
|
inline std::string to_str(Context *ctx, const PortRef &pr)
|
|
|
|
{
|
|
|
|
return pr.cell->name.str(ctx) + "." + pr.port.str(ctx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace PythonConversion
|
|
|
|
|
2018-06-07 19:10:53 +08:00
|
|
|
BOOST_PYTHON_MODULE(MODULE_NAME)
|
|
|
|
{
|
2018-07-04 18:23:35 +08:00
|
|
|
register_exception_translator<assertion_failure>(&translate_assertfail);
|
|
|
|
|
2018-07-03 17:04:55 +08:00
|
|
|
using namespace PythonConversion;
|
|
|
|
|
2018-06-07 19:10:53 +08:00
|
|
|
class_<GraphicElement>("GraphicElement")
|
|
|
|
.def_readwrite("type", &GraphicElement::type)
|
|
|
|
.def_readwrite("x1", &GraphicElement::x1)
|
|
|
|
.def_readwrite("y1", &GraphicElement::y1)
|
|
|
|
.def_readwrite("x2", &GraphicElement::x2)
|
|
|
|
.def_readwrite("y2", &GraphicElement::y2)
|
|
|
|
.def_readwrite("text", &GraphicElement::text);
|
|
|
|
|
|
|
|
enum_<PortType>("PortType")
|
|
|
|
.value("PORT_IN", PORT_IN)
|
|
|
|
.value("PORT_OUT", PORT_OUT)
|
|
|
|
.value("PORT_INOUT", PORT_INOUT)
|
|
|
|
.export_values();
|
|
|
|
|
2018-07-04 20:44:45 +08:00
|
|
|
typedef std::unordered_map<IdString, std::string> AttrMap;
|
|
|
|
typedef std::unordered_map<IdString, PortInfo> PortMap;
|
|
|
|
typedef std::unordered_map<IdString, IdString> PinMap;
|
2018-12-14 20:16:29 +08:00
|
|
|
typedef std::unordered_map<IdString, std::unique_ptr<Region>> RegionMap;
|
2018-06-07 19:10:53 +08:00
|
|
|
|
2018-07-03 16:55:32 +08:00
|
|
|
class_<BaseCtx, BaseCtx *, boost::noncopyable>("BaseCtx", no_init);
|
2018-06-07 19:10:53 +08:00
|
|
|
|
2018-07-04 19:35:15 +08:00
|
|
|
auto ci_cls = class_<ContextualWrapper<CellInfo &>>("CellInfo", no_init);
|
2018-07-05 16:13:29 +08:00
|
|
|
readwrite_wrapper<CellInfo &, decltype(&CellInfo::name), &CellInfo::name, conv_to_str<IdString>,
|
2018-07-04 20:44:45 +08:00
|
|
|
conv_from_str<IdString>>::def_wrap(ci_cls, "name");
|
2018-07-05 16:13:29 +08:00
|
|
|
readwrite_wrapper<CellInfo &, decltype(&CellInfo::type), &CellInfo::type, conv_to_str<IdString>,
|
2018-07-04 20:44:45 +08:00
|
|
|
conv_from_str<IdString>>::def_wrap(ci_cls, "type");
|
2018-07-05 16:13:29 +08:00
|
|
|
readonly_wrapper<CellInfo &, decltype(&CellInfo::attrs), &CellInfo::attrs, wrap_context<AttrMap &>>::def_wrap(
|
2018-07-04 20:13:55 +08:00
|
|
|
ci_cls, "attrs");
|
2018-07-05 16:13:29 +08:00
|
|
|
readonly_wrapper<CellInfo &, decltype(&CellInfo::params), &CellInfo::params, wrap_context<AttrMap &>>::def_wrap(
|
2018-07-04 20:44:45 +08:00
|
|
|
ci_cls, "params");
|
2018-07-05 16:13:29 +08:00
|
|
|
readonly_wrapper<CellInfo &, decltype(&CellInfo::ports), &CellInfo::ports, wrap_context<PortMap &>>::def_wrap(
|
2018-07-04 20:44:45 +08:00
|
|
|
ci_cls, "ports");
|
2018-07-05 16:13:29 +08:00
|
|
|
readwrite_wrapper<CellInfo &, decltype(&CellInfo::bel), &CellInfo::bel, conv_to_str<BelId>,
|
2018-07-04 20:44:45 +08:00
|
|
|
conv_from_str<BelId>>::def_wrap(ci_cls, "bel");
|
2018-07-05 16:13:29 +08:00
|
|
|
readwrite_wrapper<CellInfo &, decltype(&CellInfo::belStrength), &CellInfo::belStrength, pass_through<PlaceStrength>,
|
2018-07-04 20:44:45 +08:00
|
|
|
pass_through<PlaceStrength>>::def_wrap(ci_cls, "belStrength");
|
2018-07-05 16:13:29 +08:00
|
|
|
readonly_wrapper<CellInfo &, decltype(&CellInfo::pins), &CellInfo::pins, wrap_context<PinMap &>>::def_wrap(ci_cls,
|
2018-07-06 02:59:11 +08:00
|
|
|
"pins");
|
2018-07-04 20:44:45 +08:00
|
|
|
|
|
|
|
auto pi_cls = class_<ContextualWrapper<PortInfo &>>("PortInfo", no_init);
|
2018-07-05 16:13:29 +08:00
|
|
|
readwrite_wrapper<PortInfo &, decltype(&PortInfo::name), &PortInfo::name, conv_to_str<IdString>,
|
2018-07-04 20:44:45 +08:00
|
|
|
conv_from_str<IdString>>::def_wrap(pi_cls, "name");
|
2018-07-05 16:13:29 +08:00
|
|
|
readonly_wrapper<PortInfo &, decltype(&PortInfo::net), &PortInfo::net, deref_and_wrap<NetInfo>>::def_wrap(pi_cls,
|
2018-07-06 02:59:11 +08:00
|
|
|
"net");
|
2018-07-05 16:13:29 +08:00
|
|
|
readwrite_wrapper<PortInfo &, decltype(&PortInfo::type), &PortInfo::type, pass_through<PortType>,
|
2018-07-04 20:44:45 +08:00
|
|
|
pass_through<PortType>>::def_wrap(pi_cls, "type");
|
|
|
|
|
2018-11-22 20:35:07 +08:00
|
|
|
typedef std::vector<PortRef> PortRefVector;
|
2018-07-04 20:44:45 +08:00
|
|
|
typedef std::unordered_map<WireId, PipMap> WireMap;
|
2018-12-14 20:16:29 +08:00
|
|
|
typedef std::unordered_set<BelId> BelSet;
|
|
|
|
typedef std::unordered_set<WireId> WireSet;
|
2018-07-04 20:44:45 +08:00
|
|
|
|
|
|
|
auto ni_cls = class_<ContextualWrapper<NetInfo &>>("NetInfo", no_init);
|
2018-07-05 16:13:29 +08:00
|
|
|
readwrite_wrapper<NetInfo &, decltype(&NetInfo::name), &NetInfo::name, conv_to_str<IdString>,
|
2018-07-04 20:44:45 +08:00
|
|
|
conv_from_str<IdString>>::def_wrap(ni_cls, "name");
|
2018-07-05 16:13:29 +08:00
|
|
|
readwrite_wrapper<NetInfo &, decltype(&NetInfo::driver), &NetInfo::driver, wrap_context<PortRef &>,
|
2018-07-04 20:44:45 +08:00
|
|
|
unwrap_context<PortRef &>>::def_wrap(ni_cls, "driver");
|
2018-11-22 20:35:07 +08:00
|
|
|
readonly_wrapper<NetInfo &, decltype(&NetInfo::users), &NetInfo::users, wrap_context<PortRefVector &>>::def_wrap(
|
2018-07-04 20:44:45 +08:00
|
|
|
ni_cls, "users");
|
2018-07-05 16:13:29 +08:00
|
|
|
readonly_wrapper<NetInfo &, decltype(&NetInfo::wires), &NetInfo::wires, wrap_context<WireMap &>>::def_wrap(ni_cls,
|
2018-07-06 02:59:11 +08:00
|
|
|
"wires");
|
2018-07-04 20:44:45 +08:00
|
|
|
|
|
|
|
auto pr_cls = class_<ContextualWrapper<PortRef &>>("PortRef", no_init);
|
2018-07-05 16:13:29 +08:00
|
|
|
readonly_wrapper<PortRef &, decltype(&PortRef::cell), &PortRef::cell, deref_and_wrap<CellInfo>>::def_wrap(pr_cls,
|
2018-07-06 02:59:11 +08:00
|
|
|
"cell");
|
2018-07-05 16:13:29 +08:00
|
|
|
readwrite_wrapper<PortRef &, decltype(&PortRef::port), &PortRef::port, conv_to_str<IdString>,
|
2018-07-04 20:44:45 +08:00
|
|
|
conv_from_str<IdString>>::def_wrap(pr_cls, "port");
|
2018-07-05 16:13:29 +08:00
|
|
|
readwrite_wrapper<PortRef &, decltype(&PortRef::budget), &PortRef::budget, pass_through<delay_t>,
|
2018-07-04 20:44:45 +08:00
|
|
|
pass_through<delay_t>>::def_wrap(pr_cls, "budget");
|
|
|
|
|
2018-11-22 21:42:20 +08:00
|
|
|
auto pm_cls = class_<ContextualWrapper<PipMap &>>("PipMap", no_init);
|
|
|
|
readwrite_wrapper<PipMap &, decltype(&PipMap::pip), &PipMap::pip, conv_to_str<PipId>,
|
|
|
|
conv_from_str<PipId>>::def_wrap(pm_cls, "pip");
|
|
|
|
readwrite_wrapper<PipMap &, decltype(&PipMap::strength), &PipMap::strength, pass_through<PlaceStrength>,
|
|
|
|
pass_through<PlaceStrength>>::def_wrap(pm_cls, "strength");
|
|
|
|
|
2018-06-08 21:53:24 +08:00
|
|
|
def("parse_json", parse_json_shim);
|
2018-06-26 20:13:52 +08:00
|
|
|
def("load_design", load_design_shim, return_value_policy<manage_new_object>());
|
2018-06-08 21:53:24 +08:00
|
|
|
|
2018-12-14 20:16:29 +08:00
|
|
|
auto region_cls = class_<ContextualWrapper<Region &>>("Region", no_init);
|
|
|
|
readwrite_wrapper<Region &, decltype(&Region::name), &Region::name, conv_to_str<IdString>,
|
|
|
|
conv_from_str<IdString>>::def_wrap(region_cls, "name");
|
|
|
|
readwrite_wrapper<Region &, decltype(&Region::constr_bels), &Region::constr_bels, pass_through<bool>,
|
|
|
|
pass_through<bool>>::def_wrap(region_cls, "constr_bels");
|
|
|
|
readwrite_wrapper<Region &, decltype(&Region::constr_wires), &Region::constr_wires, pass_through<bool>,
|
|
|
|
pass_through<bool>>::def_wrap(region_cls, "constr_bels");
|
|
|
|
readwrite_wrapper<Region &, decltype(&Region::constr_pips), &Region::constr_pips, pass_through<bool>,
|
|
|
|
pass_through<bool>>::def_wrap(region_cls, "constr_pips");
|
|
|
|
readonly_wrapper<Region &, decltype(&Region::bels), &Region::bels, wrap_context<BelSet &>>::def_wrap(region_cls,
|
|
|
|
"bels");
|
|
|
|
readonly_wrapper<Region &, decltype(&Region::wires), &Region::wires, wrap_context<WireSet &>>::def_wrap(region_cls,
|
|
|
|
"wires");
|
|
|
|
|
2018-07-04 20:13:55 +08:00
|
|
|
WRAP_MAP(AttrMap, pass_through<std::string>, "AttrMap");
|
2018-07-04 20:44:45 +08:00
|
|
|
WRAP_MAP(PortMap, wrap_context<PortInfo &>, "PortMap");
|
|
|
|
WRAP_MAP(PinMap, conv_to_str<IdString>, "PinMap");
|
2018-11-22 21:42:20 +08:00
|
|
|
WRAP_MAP(WireMap, wrap_context<PipMap &>, "WireMap");
|
2018-12-14 20:16:29 +08:00
|
|
|
WRAP_MAP_UPTR(RegionMap, "RegionMap");
|
2018-07-04 20:13:55 +08:00
|
|
|
|
2018-11-22 20:35:07 +08:00
|
|
|
WRAP_VECTOR(PortRefVector, wrap_context<PortRef &>);
|
|
|
|
|
2018-06-07 19:10:53 +08:00
|
|
|
arch_wrap_python();
|
2018-06-01 19:57:06 +08:00
|
|
|
}
|
2018-06-06 03:03:06 +08:00
|
|
|
|
2018-07-11 14:05:42 +08:00
|
|
|
#ifdef MAIN_EXECUTABLE
|
2018-06-07 14:56:54 +08:00
|
|
|
static wchar_t *program;
|
2018-07-11 14:05:42 +08:00
|
|
|
#endif
|
2018-06-06 03:03:06 +08:00
|
|
|
|
2018-06-27 18:18:52 +08:00
|
|
|
void init_python(const char *executable, bool first)
|
2018-06-07 19:10:53 +08:00
|
|
|
{
|
2018-06-13 01:56:03 +08:00
|
|
|
#ifdef MAIN_EXECUTABLE
|
2018-06-07 19:10:53 +08:00
|
|
|
program = Py_DecodeLocale(executable, NULL);
|
|
|
|
if (program == NULL) {
|
|
|
|
fprintf(stderr, "Fatal error: cannot decode executable filename\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
try {
|
2018-06-28 22:22:46 +08:00
|
|
|
if (first)
|
2018-06-27 17:45:19 +08:00
|
|
|
PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME);
|
2018-06-07 19:10:53 +08:00
|
|
|
Py_SetProgramName(program);
|
|
|
|
Py_Initialize();
|
2018-06-27 17:45:19 +08:00
|
|
|
if (first)
|
|
|
|
PyImport_ImportModule(TOSTRING(MODULE_NAME));
|
2018-06-07 19:10:53 +08:00
|
|
|
} catch (boost::python::error_already_set const &) {
|
|
|
|
// Parse and output the exception
|
|
|
|
std::string perror_str = parse_python_exception();
|
|
|
|
std::cout << "Error in Python: " << perror_str << std::endl;
|
|
|
|
}
|
2018-06-14 16:08:54 +08:00
|
|
|
signal(SIGINT, SIG_DFL);
|
2018-06-08 17:17:04 +08:00
|
|
|
#endif
|
2018-06-07 14:56:54 +08:00
|
|
|
}
|
|
|
|
|
2018-06-07 19:10:53 +08:00
|
|
|
void deinit_python()
|
|
|
|
{
|
2018-06-13 01:56:03 +08:00
|
|
|
#ifdef MAIN_EXECUTABLE
|
2018-06-07 19:10:53 +08:00
|
|
|
Py_Finalize();
|
|
|
|
PyMem_RawFree(program);
|
2018-06-08 17:17:04 +08:00
|
|
|
#endif
|
2018-06-07 14:56:54 +08:00
|
|
|
}
|
2018-06-06 03:03:06 +08:00
|
|
|
|
2018-06-07 19:10:53 +08:00
|
|
|
void execute_python_file(const char *python_file)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
FILE *fp = fopen(python_file, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
fprintf(stderr, "Fatal error: file not found %s\n", python_file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
PyRun_SimpleFile(fp, python_file);
|
|
|
|
fclose(fp);
|
|
|
|
} catch (boost::python::error_already_set const &) {
|
|
|
|
// Parse and output the exception
|
|
|
|
std::string perror_str = parse_python_exception();
|
|
|
|
std::cout << "Error in Python: " << perror_str << std::endl;
|
|
|
|
}
|
2018-06-06 03:03:06 +08:00
|
|
|
}
|
2018-06-12 20:24:59 +08:00
|
|
|
|
|
|
|
NEXTPNR_NAMESPACE_END
|
2018-06-23 20:32:18 +08:00
|
|
|
|
2018-06-24 01:51:22 +08:00
|
|
|
#endif // NO_PYTHON
|