Reformat Python bindings and ice40 main

Signed-off-by: David Shah <davey1576@gmail.com>
This commit is contained in:
David Shah 2018-06-07 13:10:53 +02:00
parent 6236a10427
commit c3e0252703
6 changed files with 368 additions and 361 deletions

View File

@ -18,31 +18,33 @@
* *
*/ */
#include "design.h"
#include "chip.h" #include "chip.h"
#include "design.h"
#include "emb.h" #include "emb.h"
// include after design.h/chip.h // include after design.h/chip.h
#include "pybindings.h" #include "pybindings.h"
// Required to determine concatenated module name (which differs for different archs) // Required to determine concatenated module name (which differs for different
#define PASTER(x, y) x ## _ ## y // archs)
#define EVALUATOR(x, y) PASTER(x,y) #define PASTER(x, y) x##_##y
#define EVALUATOR(x, y) PASTER(x, y)
#define MODULE_NAME EVALUATOR(nextpnrpy, ARCHNAME) #define MODULE_NAME EVALUATOR(nextpnrpy, ARCHNAME)
#define PYINIT_MODULE_NAME EVALUATOR(&PyInit_nextpnrpy, ARCHNAME) #define PYINIT_MODULE_NAME EVALUATOR(&PyInit_nextpnrpy, ARCHNAME)
#define STRINGIFY(x) #x #define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x) #define TOSTRING(x) STRINGIFY(x)
// Architecture-specific bindings should be created in the below function, which must be implemented in all // Architecture-specific bindings should be created in the below function, which
// architectures // must be implemented in all architectures
void arch_wrap_python(); void arch_wrap_python();
bool operator==(const PortRef &a, const PortRef &b) { bool operator==(const PortRef &a, const PortRef &b)
{
return (a.cell == b.cell) && (a.port == b.port); return (a.cell == b.cell) && (a.port == b.port);
} }
BOOST_PYTHON_MODULE (MODULE_NAME) { BOOST_PYTHON_MODULE(MODULE_NAME)
{
class_<GraphicElement>("GraphicElement") class_<GraphicElement>("GraphicElement")
.def_readwrite("style", &GraphicElement::style) .def_readwrite("style", &GraphicElement::style)
.def_readwrite("type", &GraphicElement::type) .def_readwrite("type", &GraphicElement::type)
@ -56,7 +58,7 @@ BOOST_PYTHON_MODULE (MODULE_NAME) {
.def_readwrite("cell", &PortRef::cell) .def_readwrite("cell", &PortRef::cell)
.def_readwrite("port", &PortRef::port); .def_readwrite("port", &PortRef::port);
class_<NetInfo, NetInfo*>("NetInfo") class_<NetInfo, NetInfo *>("NetInfo")
.def_readwrite("name", &NetInfo::name) .def_readwrite("name", &NetInfo::name)
.def_readwrite("driver", &NetInfo::driver) .def_readwrite("driver", &NetInfo::driver)
.def_readwrite("users", &NetInfo::users) .def_readwrite("users", &NetInfo::users)
@ -79,7 +81,7 @@ BOOST_PYTHON_MODULE (MODULE_NAME) {
.def_readwrite("net", &PortInfo::net) .def_readwrite("net", &PortInfo::net)
.def_readwrite("type", &PortInfo::type); .def_readwrite("type", &PortInfo::type);
class_<CellInfo, CellInfo*>("CellInfo") class_<CellInfo, CellInfo *>("CellInfo")
.def_readwrite("name", &CellInfo::name) .def_readwrite("name", &CellInfo::name)
.def_readwrite("type", &CellInfo::type) .def_readwrite("type", &CellInfo::type)
.def_readwrite("ports", &CellInfo::ports) .def_readwrite("ports", &CellInfo::ports)
@ -89,9 +91,9 @@ BOOST_PYTHON_MODULE (MODULE_NAME) {
.def_readwrite("pins", &CellInfo::pins); .def_readwrite("pins", &CellInfo::pins);
WRAP_MAP(decltype(CellInfo::ports), "IdPortMap"); WRAP_MAP(decltype(CellInfo::ports), "IdPortMap");
//WRAP_MAP(decltype(CellInfo::pins), "IdIdMap"); // WRAP_MAP(decltype(CellInfo::pins), "IdIdMap");
class_<Design, Design*>("Design", no_init) class_<Design, Design *>("Design", no_init)
.def_readwrite("chip", &Design::chip) .def_readwrite("chip", &Design::chip)
.def_readwrite("nets", &Design::nets) .def_readwrite("nets", &Design::nets)
.def_readwrite("cells", &Design::cells); .def_readwrite("cells", &Design::cells);
@ -102,13 +104,15 @@ BOOST_PYTHON_MODULE (MODULE_NAME) {
arch_wrap_python(); arch_wrap_python();
} }
void arch_appendinittab() { void arch_appendinittab()
{
PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME); PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME);
} }
static wchar_t *program; static wchar_t *program;
void init_python(const char *executable) { void init_python(const char *executable)
{
program = Py_DecodeLocale(executable, NULL); program = Py_DecodeLocale(executable, NULL);
if (program == NULL) { if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode executable filename\n"); fprintf(stderr, "Fatal error: cannot decode executable filename\n");
@ -127,12 +131,14 @@ void init_python(const char *executable) {
} }
} }
void deinit_python() { void deinit_python()
{
Py_Finalize(); Py_Finalize();
PyMem_RawFree(program); PyMem_RawFree(program);
} }
void execute_python_file(const char *python_file) { void execute_python_file(const char *python_file)
{
try { try {
FILE *fp = fopen(python_file, "r"); FILE *fp = fopen(python_file, "r");
if (fp == NULL) { if (fp == NULL) {
@ -141,11 +147,9 @@ void execute_python_file(const char *python_file) {
} }
PyRun_SimpleFile(fp, python_file); PyRun_SimpleFile(fp, python_file);
fclose(fp); fclose(fp);
} } catch (boost::python::error_already_set const &) {
catch (boost::python::error_already_set const &) {
// Parse and output the exception // Parse and output the exception
std::string perror_str = parse_python_exception(); std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl; std::cout << "Error in Python: " << perror_str << std::endl;
} }
} }

View File

@ -23,60 +23,60 @@
#include "pycontainers.h" #include "pycontainers.h"
#include <utility>
#include <stdexcept>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <Python.h> #include <Python.h>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <stdexcept>
#include <utility>
using namespace boost::python; using namespace boost::python;
/* /*
A wrapper to enable custom type/ID to/from string conversions A wrapper to enable custom type/ID to/from string conversions
*/ */
template<typename T> template <typename T> struct string_wrapper
struct string_wrapper { {
template<typename F> template <typename F> struct from_pystring_converter
struct from_pystring_converter { {
from_pystring_converter() { from_pystring_converter()
converter::registry::push_back( {
&convertible, converter::registry::push_back(&convertible, &construct,
&construct,
boost::python::type_id<T>()); boost::python::type_id<T>());
}; };
static void *convertible(PyObject *object) { static void *convertible(PyObject *object)
{
return PyUnicode_Check(object) ? object : 0; return PyUnicode_Check(object) ? object : 0;
} }
static void construct( static void construct(PyObject *object,
PyObject *object, converter::rvalue_from_python_stage1_data *data)
converter::rvalue_from_python_stage1_data *data) { {
const wchar_t *value = PyUnicode_AsUnicode(object); const wchar_t *value = PyUnicode_AsUnicode(object);
const std::wstring value_ws(value); const std::wstring value_ws(value);
if (value == 0) throw_error_already_set(); if (value == 0)
void *storage = ( throw_error_already_set();
(boost::python::converter::rvalue_from_python_storage<T> *) void *storage =
data)->storage.bytes; ((boost::python::converter::rvalue_from_python_storage<T> *)
new(storage) T(fn(std::string(value_ws.begin(), value_ws.end()))); data)
->storage.bytes;
new (storage) T(fn(std::string(value_ws.begin(), value_ws.end())));
data->convertible = storage; data->convertible = storage;
} }
static F fn; static F fn;
}; };
template<typename F> template <typename F> struct to_str_wrapper
struct to_str_wrapper { {
static F fn; static F fn;
std::string str(T &x) { std::string str(T &x) { return fn(x); }
return fn(x);
}
}; };
template<typename F1, typename F2> template <typename F1, typename F2>
static void wrap(const char *type_name, F1 to_str_fn, F2 from_str_fn) { static void wrap(const char *type_name, F1 to_str_fn, F2 from_str_fn)
{
from_pystring_converter<F2>::fn = from_str_fn; from_pystring_converter<F2>::fn = from_str_fn;
from_pystring_converter<F2>(); from_pystring_converter<F2>();
to_str_wrapper<F1>::fn = to_str_fn; to_str_wrapper<F1>::fn = to_str_fn;
@ -86,15 +86,15 @@ struct string_wrapper {
std::string parse_python_exception(); std::string parse_python_exception();
template<typename Tn> template <typename Tn> void python_export_global(const char *name, Tn &x)
void python_export_global(const char *name, Tn &x) { {
PyObject * m, *d; PyObject *m, *d;
m = PyImport_AddModule("__main__"); m = PyImport_AddModule("__main__");
if (m == NULL) if (m == NULL)
return; return;
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
try { try {
PyObject * p = incref(object(boost::ref(x)).ptr()); PyObject *p = incref(object(boost::ref(x)).ptr());
PyDict_SetItemString(d, name, p); PyDict_SetItemString(d, name, p);
} catch (boost::python::error_already_set const &) { } catch (boost::python::error_already_set const &) {
// Parse and output the exception // Parse and output the exception

View File

@ -17,9 +17,9 @@
* *
*/ */
#include <QApplication>
#include "design.h" #include "design.h"
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {

View File

@ -18,12 +18,10 @@
* *
*/ */
#include "design.h"
#include "chip.h" #include "chip.h"
#include "design.h"
// include after design.h/chip.h // include after design.h/chip.h
#include "pybindings.h" #include "pybindings.h"
void arch_wrap_python() { void arch_wrap_python() { class_<ChipArgs>("ChipArgs"); }
class_<ChipArgs>("ChipArgs");
}

View File

@ -16,15 +16,15 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
* *
*/ */
#include "design.h"
#include "mainwindow.h"
#include <QApplication> #include <QApplication>
#include <iostream>
#include <fstream>
#include "version.h"
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include "pybindings.h" #include <fstream>
#include <iostream>
#include "design.h"
#include "jsonparse.h" #include "jsonparse.h"
#include "mainwindow.h"
#include "pybindings.h"
#include "version.h"
void svg_dump_el(const GraphicElement &el) void svg_dump_el(const GraphicElement &el)
{ {
@ -32,13 +32,17 @@ void svg_dump_el(const GraphicElement &el)
std::string style = "stroke=\"black\" stroke-width=\"0.1\" fill=\"none\""; std::string style = "stroke=\"black\" stroke-width=\"0.1\" fill=\"none\"";
if (el.type == GraphicElement::G_BOX) { if (el.type == GraphicElement::G_BOX) {
std::cout << "<rect x=\"" << (offset + scale*el.x1) << "\" y=\"" << (offset + scale*el.y1) << std::cout << "<rect x=\"" << (offset + scale * el.x1) << "\" y=\""
"\" height=\"" << (scale*(el.y2-el.y1)) << "\" width=\"" << (scale*(el.x2-el.x1)) << "\" " << style << "/>\n"; << (offset + scale * el.y1) << "\" height=\""
<< (scale * (el.y2 - el.y1)) << "\" width=\""
<< (scale * (el.x2 - el.x1)) << "\" " << style << "/>\n";
} }
if (el.type == GraphicElement::G_LINE) { if (el.type == GraphicElement::G_LINE) {
std::cout << "<line x1=\"" << (offset + scale*el.x1) << "\" y1=\"" << (offset + scale*el.y1) << std::cout << "<line x1=\"" << (offset + scale * el.x1) << "\" y1=\""
"\" x2=\"" << (offset + scale*el.x2) << "\" y2=\"" << (offset + scale*el.y2) << "\" " << style << "/>\n"; << (offset + scale * el.y1) << "\" x2=\""
<< (offset + scale * el.x2) << "\" y2=\""
<< (offset + scale * el.y2) << "\" " << style << "/>\n";
} }
} }
@ -49,55 +53,56 @@ int main(int argc, char *argv[])
std::string str; std::string str;
po::options_description options("Allowed options"); po::options_description options("Allowed options");
options.add_options()("help,h","show help"); options.add_options()("help,h", "show help");
options.add_options()("test","just a check"); options.add_options()("test", "just a check");
options.add_options()("gui","start gui"); options.add_options()("gui", "start gui");
options.add_options()("svg","dump SVG file"); options.add_options()("svg", "dump SVG file");
options.add_options()("run", po::value<std::vector<std::string>>(), "python file to execute"); options.add_options()("run", po::value<std::vector<std::string>>(),
options.add_options()("json", po::value<std::string>(), "JSON design file to ingest"); "python file to execute");
options.add_options()("version,v","show version"); options.add_options()("json", po::value<std::string>(),
options.add_options()("lp384","set device type to iCE40LP384"); "JSON design file to ingest");
options.add_options()("lp1k","set device type to iCE40LP1K"); options.add_options()("version,v", "show version");
options.add_options()("lp8k","set device type to iCE40LP8K"); options.add_options()("lp384", "set device type to iCE40LP384");
options.add_options()("hx1k","set device type to iCE40HX1K"); options.add_options()("lp1k", "set device type to iCE40LP1K");
options.add_options()("hx8k","set device type to iCE40HX8K"); options.add_options()("lp8k", "set device type to iCE40LP8K");
options.add_options()("up5k","set device type to iCE40UP5K"); options.add_options()("hx1k", "set device type to iCE40HX1K");
options.add_options()("hx8k", "set device type to iCE40HX8K");
options.add_options()("up5k", "set device type to iCE40UP5K");
po::positional_options_description pos; po::positional_options_description pos;
pos.add("run", -1); pos.add("run", -1);
po::variables_map vm; po::variables_map vm;
try { try {
po::parsed_options parsed = po::command_line_parser(argc, argv). po::parsed_options parsed = po::command_line_parser(argc, argv)
options(options). .options(options)
positional(pos). .positional(pos)
run(); .run();
po::store(parsed, vm); po::store(parsed, vm);
po::notify(vm); po::notify(vm);
} }
catch(std::exception& e) catch (std::exception &e) {
{
std::cout << e.what() << "\n"; std::cout << e.what() << "\n";
return 1; return 1;
} }
if (vm.count("help") || argc == 1) if (vm.count("help") || argc == 1) {
{
help: help:
std::cout << basename(argv[0]) << " -- Next Generation Place and Route (git sha1 " GIT_COMMIT_HASH_STR ")\n"; std::cout << basename(argv[0])
<< " -- Next Generation Place and Route (git "
"sha1 " GIT_COMMIT_HASH_STR ")\n";
std::cout << "\n"; std::cout << "\n";
std::cout << options << "\n"; std::cout << options << "\n";
return argc != 1; return argc != 1;
} }
if (vm.count("version")) if (vm.count("version")) {
{
std::cout << basename(argv[0]) std::cout << basename(argv[0])
<< " -- Next Generation Place and Route (git sha1 " << " -- Next Generation Place and Route (git "
GIT_COMMIT_HASH_STR ")\n"; "sha1 " GIT_COMMIT_HASH_STR ")\n";
return 1; return 1;
} }
@ -144,7 +149,8 @@ int main(int argc, char *argv[])
#ifdef ICE40_HX1K_ONLY #ifdef ICE40_HX1K_ONLY
if (chipArgs.type != ChipArgs::HX1K) { if (chipArgs.type != ChipArgs::HX1K) {
std::cout << "This version of nextpnr-ice40 is built with HX1K-support only.\n"; std::cout << "This version of nextpnr-ice40 is built with HX1K-support "
"only.\n";
return 1; return 1;
} }
#endif #endif
@ -153,8 +159,7 @@ int main(int argc, char *argv[])
init_python(argv[0]); init_python(argv[0]);
python_export_global("design", design); python_export_global("design", design);
if (vm.count("test")) if (vm.count("test")) {
{
int bel_count = 0, wire_count = 0, pip_count = 0; int bel_count = 0, wire_count = 0, pip_count = 0;
std::cout << "Checking bel names.\n"; std::cout << "Checking bel names.\n";
@ -185,7 +190,8 @@ int main(int argc, char *argv[])
for (auto dst : design.chip.getWires()) { for (auto dst : design.chip.getWires()) {
for (auto uphill_pip : design.chip.getPipsUphill(dst)) { for (auto uphill_pip : design.chip.getPipsUphill(dst)) {
bool found_downhill = false; bool found_downhill = false;
for (auto downhill_pip : design.chip.getPipsDownhill(design.chip.getPipSrcWire(uphill_pip))) { for (auto downhill_pip : design.chip.getPipsDownhill(
design.chip.getPipSrcWire(uphill_pip))) {
if (uphill_pip == downhill_pip) { if (uphill_pip == downhill_pip) {
assert(!found_downhill); assert(!found_downhill);
found_downhill = true; found_downhill = true;
@ -199,7 +205,8 @@ int main(int argc, char *argv[])
for (auto dst : design.chip.getWires()) { for (auto dst : design.chip.getWires()) {
for (auto downhill_pip : design.chip.getPipsDownhill(dst)) { for (auto downhill_pip : design.chip.getPipsDownhill(dst)) {
bool found_uphill = false; bool found_uphill = false;
for (auto uphill_pip : design.chip.getPipsUphill(design.chip.getPipDstWire(downhill_pip))) { for (auto uphill_pip : design.chip.getPipsUphill(
design.chip.getPipDstWire(downhill_pip))) {
if (uphill_pip == downhill_pip) { if (uphill_pip == downhill_pip) {
assert(!found_uphill); assert(!found_uphill);
found_uphill = true; found_uphill = true;
@ -212,9 +219,9 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
if (vm.count("svg")) if (vm.count("svg")) {
{ std::cout << "<svg xmlns=\"http://www.w3.org/2000/svg\" "
std::cout << "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n"; "xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n";
for (auto bel : design.chip.getBels()) { for (auto bel : design.chip.getBels()) {
std::cout << "<!-- " << design.chip.getBelName(bel) << " -->\n"; std::cout << "<!-- " << design.chip.getBelName(bel) << " -->\n";
for (auto &el : design.chip.getBelGraphics(bel)) for (auto &el : design.chip.getBelGraphics(bel))
@ -226,23 +233,21 @@ int main(int argc, char *argv[])
std::cout << "</svg>\n"; std::cout << "</svg>\n";
} }
if (vm.count("json")) if (vm.count("json")) {
{
std::string filename = vm["json"].as<std::string>(); std::string filename = vm["json"].as<std::string>();
std::istream *f = new std::ifstream(filename); std::istream *f = new std::ifstream(filename);
parse_json_file(f, filename, &design); parse_json_file(f, filename, &design);
} }
if (vm.count("run")) if (vm.count("run")) {
{ std::vector<std::string> files =
std::vector<std::string> files = vm["run"].as<std::vector<std::string>>(); vm["run"].as<std::vector<std::string>>();
for(auto filename : files) for (auto filename : files)
execute_python_file(filename.c_str()); execute_python_file(filename.c_str());
} }
if (vm.count("gui")) if (vm.count("gui")) {
{
QApplication a(argc, argv); QApplication a(argc, argv);
MainWindow w; MainWindow w;
w.show(); w.show();

View File

@ -18,15 +18,15 @@
* *
*/ */
#include "design.h"
#include "chip.h" #include "chip.h"
#include "design.h"
// include after design.h/chip.h // include after design.h/chip.h
#include "pybindings.h" #include "pybindings.h"
void arch_wrap_python() { void arch_wrap_python()
class_<ChipArgs>("ChipArgs") {
.def_readwrite("type", &ChipArgs::type); class_<ChipArgs>("ChipArgs").def_readwrite("type", &ChipArgs::type);
enum_<decltype(std::declval<ChipArgs>().type)>("iCE40Type") enum_<decltype(std::declval<ChipArgs>().type)>("iCE40Type")
.value("NONE", ChipArgs::NONE) .value("NONE", ChipArgs::NONE)