Tried to add fixes *and* update clang-format jsonparse.cc

This commit is contained in:
ZipCPU 2018-06-07 15:46:21 -04:00
commit 87775d3c6f
8 changed files with 140 additions and 159 deletions

View File

@ -331,8 +331,7 @@ void json_import_cell_attributes(Design *design, string &modname,
pId = param_node->data_dict_keys[param_id]; pId = param_node->data_dict_keys[param_id];
if (param->type == 'N') { if (param->type == 'N') {
cell->params[pId] = std::to_string(param_node->data_number); cell->params[pId] = std::to_string(param->data_number);
;
} else if (param->type == 'S') } else if (param->type == 'S')
cell->params[pId] = param->data_string; cell->params[pId] = param->data_string;
else else

View File

@ -6,27 +6,24 @@
// //
// Blog article: http://mateusz.loskot.net/?p=2819 // Blog article: http://mateusz.loskot.net/?p=2819
#include "emb.h"
#include <Python.h>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <Python.h>
#include "emb.h"
namespace emb namespace emb {
{
struct Stdout struct Stdout
{ {
PyObject_HEAD PyObject_HEAD stdout_write_type write;
stdout_write_type write;
}; };
PyObject* Stdout_write(PyObject* self, PyObject* args) PyObject *Stdout_write(PyObject *self, PyObject *args)
{ {
std::size_t written(0); std::size_t written(0);
Stdout* selfimpl = reinterpret_cast<Stdout*>(self); Stdout *selfimpl = reinterpret_cast<Stdout *>(self);
if (selfimpl->write) if (selfimpl->write) {
{ char *data;
char* data;
if (!PyArg_ParseTuple(args, "s", &data)) if (!PyArg_ParseTuple(args, "s", &data))
return 0; return 0;
@ -37,70 +34,65 @@ PyObject* Stdout_write(PyObject* self, PyObject* args)
return PyLong_FromSize_t(written); return PyLong_FromSize_t(written);
} }
PyObject* Stdout_flush(PyObject* self, PyObject* args) PyObject *Stdout_flush(PyObject *self, PyObject *args)
{ {
// no-op // no-op
return Py_BuildValue(""); return Py_BuildValue("");
} }
PyMethodDef Stdout_methods[] = PyMethodDef Stdout_methods[] = {
{ {"write", Stdout_write, METH_VARARGS, "sys.stdout.write"},
{"write", Stdout_write, METH_VARARGS, "sys.stdout.write"}, {"flush", Stdout_flush, METH_VARARGS, "sys.stdout.write"},
{"flush", Stdout_flush, METH_VARARGS, "sys.stdout.write"}, {0, 0, 0, 0} // sentinel
{0, 0, 0, 0} // sentinel
}; };
PyTypeObject StdoutType = PyTypeObject StdoutType = {
{ PyVarObject_HEAD_INIT(0, 0) "emb.StdoutType", /* tp_name */
PyVarObject_HEAD_INIT(0, 0) sizeof(Stdout), /* tp_basicsize */
"emb.StdoutType", /* tp_name */ 0, /* tp_itemsize */
sizeof(Stdout), /* tp_basicsize */ 0, /* tp_dealloc */
0, /* tp_itemsize */ 0, /* tp_print */
0, /* tp_dealloc */ 0, /* tp_getattr */
0, /* tp_print */ 0, /* tp_setattr */
0, /* tp_getattr */ 0, /* tp_reserved */
0, /* tp_setattr */ 0, /* tp_repr */
0, /* tp_reserved */ 0, /* tp_as_number */
0, /* tp_repr */ 0, /* tp_as_sequence */
0, /* tp_as_number */ 0, /* tp_as_mapping */
0, /* tp_as_sequence */ 0, /* tp_hash */
0, /* tp_as_mapping */ 0, /* tp_call */
0, /* tp_hash */ 0, /* tp_str */
0, /* tp_call */ 0, /* tp_getattro */
0, /* tp_str */ 0, /* tp_setattro */
0, /* tp_getattro */ 0, /* tp_as_buffer */
0, /* tp_setattro */ Py_TPFLAGS_DEFAULT, /* tp_flags */
0, /* tp_as_buffer */ "emb.Stdout objects", /* tp_doc */
Py_TPFLAGS_DEFAULT, /* tp_flags */ 0, /* tp_traverse */
"emb.Stdout objects", /* tp_doc */ 0, /* tp_clear */
0, /* tp_traverse */ 0, /* tp_richcompare */
0, /* tp_clear */ 0, /* tp_weaklistoffset */
0, /* tp_richcompare */ 0, /* tp_iter */
0, /* tp_weaklistoffset */ 0, /* tp_iternext */
0, /* tp_iter */ Stdout_methods, /* tp_methods */
0, /* tp_iternext */ 0, /* tp_members */
Stdout_methods, /* tp_methods */ 0, /* tp_getset */
0, /* tp_members */ 0, /* tp_base */
0, /* tp_getset */ 0, /* tp_dict */
0, /* tp_base */ 0, /* tp_descr_get */
0, /* tp_dict */ 0, /* tp_descr_set */
0, /* tp_descr_get */ 0, /* tp_dictoffset */
0, /* tp_descr_set */ 0, /* tp_init */
0, /* tp_dictoffset */ 0, /* tp_alloc */
0, /* tp_init */ 0, /* tp_new */
0, /* tp_alloc */
0, /* tp_new */
}; };
PyModuleDef embmodule = PyModuleDef embmodule = {
{ PyModuleDef_HEAD_INIT, "emb", 0, -1, 0,
PyModuleDef_HEAD_INIT,
"emb", 0, -1, 0,
}; };
// Internal state // Internal state
PyObject* g_stdout; PyObject *g_stdout;
PyObject* g_stdout_saved; PyObject *g_stdout_saved;
PyMODINIT_FUNC PyInit_emb(void) PyMODINIT_FUNC PyInit_emb(void)
{ {
@ -111,24 +103,23 @@ PyMODINIT_FUNC PyInit_emb(void)
if (PyType_Ready(&StdoutType) < 0) if (PyType_Ready(&StdoutType) < 0)
return 0; return 0;
PyObject* m = PyModule_Create(&embmodule); PyObject *m = PyModule_Create(&embmodule);
if (m) if (m) {
{
Py_INCREF(&StdoutType); Py_INCREF(&StdoutType);
PyModule_AddObject(m, "Stdout", reinterpret_cast<PyObject*>(&StdoutType)); PyModule_AddObject(m, "Stdout",
reinterpret_cast<PyObject *>(&StdoutType));
} }
return m; return m;
} }
void set_stdout(stdout_write_type write) void set_stdout(stdout_write_type write)
{ {
if (!g_stdout) if (!g_stdout) {
{
g_stdout_saved = PySys_GetObject("stdout"); // borrowed g_stdout_saved = PySys_GetObject("stdout"); // borrowed
g_stdout = StdoutType.tp_new(&StdoutType, 0, 0); g_stdout = StdoutType.tp_new(&StdoutType, 0, 0);
} }
Stdout* impl = reinterpret_cast<Stdout*>(g_stdout); Stdout *impl = reinterpret_cast<Stdout *>(g_stdout);
impl->write = write; impl->write = write;
PySys_SetObject("stdout", g_stdout); PySys_SetObject("stdout", g_stdout);
} }
@ -142,9 +133,6 @@ void reset_stdout()
g_stdout = 0; g_stdout = 0;
} }
void append_inittab() void append_inittab() { PyImport_AppendInittab("emb", emb::PyInit_emb); }
{
PyImport_AppendInittab("emb", emb::PyInit_emb);
}
} // namespace emb } // namespace emb

View File

@ -10,12 +10,11 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
namespace emb namespace emb {
{ typedef std::function<void(std::string)> stdout_write_type;
typedef std::function<void(std::string)> stdout_write_type;
void set_stdout(stdout_write_type write); void set_stdout(stdout_write_type write);
void reset_stdout(); void reset_stdout();
void append_inittab(); void append_inittab();
} // namespace emb } // namespace emb

View File

@ -1,57 +1,42 @@
#include <QMouseEvent>
#include <QCoreApplication>
#include <math.h>
#include "fpgaviewwidget.h" #include "fpgaviewwidget.h"
#include <QCoreApplication>
#include <QMouseEvent>
#include <math.h>
FPGAViewWidget::FPGAViewWidget(QWidget *parent) FPGAViewWidget::FPGAViewWidget(QWidget *parent)
: QOpenGLWidget(parent), : QOpenGLWidget(parent), m_xMove(0), m_yMove(0), m_zDistance(1.0)
m_xMove(0),m_yMove(0),m_zDistance(1.0)
{
}
FPGAViewWidget::~FPGAViewWidget()
{ {
} }
QSize FPGAViewWidget::minimumSizeHint() const FPGAViewWidget::~FPGAViewWidget() {}
{
return QSize(640, 480);
}
QSize FPGAViewWidget::sizeHint() const QSize FPGAViewWidget::minimumSizeHint() const { return QSize(640, 480); }
{
return QSize(640, 480); QSize FPGAViewWidget::sizeHint() const { return QSize(640, 480); }
}
void FPGAViewWidget::setXTranslation(float t_x) void FPGAViewWidget::setXTranslation(float t_x)
{ {
if(t_x != m_xMove) if (t_x != m_xMove) {
{
m_xMove = t_x; m_xMove = t_x;
update(); update();
} }
} }
void FPGAViewWidget::setYTranslation(float t_y) void FPGAViewWidget::setYTranslation(float t_y)
{ {
if(t_y != m_yMove) if (t_y != m_yMove) {
{
m_yMove = t_y; m_yMove = t_y;
update(); update();
} }
} }
void FPGAViewWidget::setZoom(float t_z) void FPGAViewWidget::setZoom(float t_z)
{ {
if(t_z != m_zDistance) if (t_z != m_zDistance) {
{
m_zDistance -= t_z; m_zDistance -= t_z;
if(m_zDistance < 0.1f) if (m_zDistance < 0.1f)
m_zDistance = 0.1f; m_zDistance = 0.1f;
if(m_zDistance > 10.0f) if (m_zDistance > 10.0f)
m_zDistance = 10.0f; m_zDistance = 10.0f;
update(); update();
@ -70,21 +55,19 @@ void FPGAViewWidget::paintGL()
glLoadIdentity(); glLoadIdentity();
glTranslatef(m_xMove, m_yMove, -10.0); glTranslatef(m_xMove, m_yMove, -10.0);
glScalef(m_zDistance,m_zDistance, 0.0f); glScalef(m_zDistance, m_zDistance, 0.0f);
// Example grid // Example grid
glColor3f(0.8, 0.8, 0.8); glColor3f(0.8, 0.8, 0.8);
glBegin(GL_LINES); glBegin(GL_LINES);
for(float i = -100; i <= 100; i += 0.1) for (float i = -100; i <= 100; i += 0.1) {
{
glVertex3f((float)i, -100.0f, 0.0f); glVertex3f((float)i, -100.0f, 0.0f);
glVertex3f((float)i, 100.0f, 0.0f); glVertex3f((float)i, 100.0f, 0.0f);
glVertex3f(-100.0f, (float)i, 0.0f); glVertex3f(-100.0f, (float)i, 0.0f);
glVertex3f(100.0f, (float)i, 0.0f); glVertex3f(100.0f, (float)i, 0.0f);
} }
glColor3f(0.5, 0.5, 0.5); glColor3f(0.5, 0.5, 0.5);
for(int i = -100; i <= 100; i += 1) for (int i = -100; i <= 100; i += 1) {
{
glVertex3f((float)i, -100.0f, 0.0f); glVertex3f((float)i, -100.0f, 0.0f);
glVertex3f((float)i, 100.0f, 0.0f); glVertex3f((float)i, 100.0f, 0.0f);
glVertex3f(-100.0f, (float)i, 0.0f); glVertex3f(-100.0f, (float)i, 0.0f);
@ -101,7 +84,6 @@ void FPGAViewWidget::paintGL()
glColor3f(0.0, 0.0, 1.0); glColor3f(0.0, 0.0, 1.0);
glVertex3f(0, 0.5, 0); glVertex3f(0, 0.5, 0);
glEnd(); glEnd();
} }
void FPGAViewWidget::resizeGL(int width, int height) void FPGAViewWidget::resizeGL(int width, int height)
@ -114,7 +96,7 @@ void FPGAViewWidget::resizeGL(int width, int height)
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
glOrtho(-1.0* aspect, +1.0* aspect, -1.0, +1.0, 1.0, 15.0); glOrtho(-1.0 * aspect, +1.0 * aspect, -1.0, +1.0, 1.0, 15.0);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
} }
@ -130,13 +112,13 @@ void FPGAViewWidget::mouseMoveEvent(QMouseEvent *event)
float dx_scale = dx * (1 / (float)640); float dx_scale = dx * (1 / (float)640);
float dy_scale = -dy * (1 / (float)480); float dy_scale = -dy * (1 / (float)480);
if (event->buttons() & Qt::LeftButton) if (event->buttons() & Qt::LeftButton) {
{
float xpos = m_xMove + dx_scale; float xpos = m_xMove + dx_scale;
float ypos = m_yMove + dy_scale; float ypos = m_yMove + dy_scale;
if (m_xMove/m_zDistance <= 100.0 && m_xMove/m_zDistance>= -100.0) setXTranslation(xpos); if (m_xMove / m_zDistance <= 100.0 && m_xMove / m_zDistance >= -100.0)
if (m_yMove/m_zDistance <= 100.0 && m_yMove/m_zDistance>= -100.0) setYTranslation(ypos); setXTranslation(xpos);
if (m_yMove / m_zDistance <= 100.0 && m_yMove / m_zDistance >= -100.0)
setYTranslation(ypos);
} }
m_lastPos = event->pos(); m_lastPos = event->pos();
} }
@ -145,8 +127,7 @@ void FPGAViewWidget::wheelEvent(QWheelEvent *event)
{ {
QPoint degree = event->angleDelta() / 8; QPoint degree = event->angleDelta() / 8;
if(!degree.isNull()) if (!degree.isNull()) {
{
QPoint step = degree / 15; QPoint step = degree / 15;
setZoom(step.y() * -0.1f); setZoom(step.y() * -0.1f);
} }

View File

@ -1,15 +1,15 @@
#ifndef MAPGLWIDGET_H #ifndef MAPGLWIDGET_H
#define MAPGLWIDGET_H #define MAPGLWIDGET_H
#include <QOpenGLWidget>
#include <QOpenGLFunctions> #include <QOpenGLFunctions>
#include <QOpenGLWidget>
#include <QPainter> #include <QPainter>
class FPGAViewWidget : public QOpenGLWidget, protected QOpenGLFunctions class FPGAViewWidget : public QOpenGLWidget, protected QOpenGLFunctions
{ {
Q_OBJECT Q_OBJECT
public: public:
FPGAViewWidget(QWidget *parent = 0); FPGAViewWidget(QWidget *parent = 0);
~FPGAViewWidget(); ~FPGAViewWidget();
@ -24,7 +24,7 @@ public:
void yRotationChanged(int angle); void yRotationChanged(int angle);
void zRotationChanged(int angle); void zRotationChanged(int angle);
protected: protected:
void initializeGL() Q_DECL_OVERRIDE; void initializeGL() Q_DECL_OVERRIDE;
void paintGL() Q_DECL_OVERRIDE; void paintGL() Q_DECL_OVERRIDE;
void resizeGL(int width, int height) Q_DECL_OVERRIDE; void resizeGL(int width, int height) Q_DECL_OVERRIDE;
@ -32,7 +32,7 @@ protected:
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE; void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE;
private: private:
int m_windowWidth; int m_windowWidth;
int m_windowHeight; int m_windowHeight;
float m_xMove; float m_xMove;

View File

@ -1,36 +1,29 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "pybindings.h"
#include "emb.h" #include "emb.h"
#include "pybindings.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent)
QMainWindow(parent), : QMainWindow(parent), ui(new Ui::MainWindow)
ui(new Ui::MainWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
PyImport_ImportModule("emb"); PyImport_ImportModule("emb");
write = [this] (std::string s) { write = [this](std::string s) {
//ui->plainTextEdit->moveCursor(QTextCursor::End); // ui->plainTextEdit->moveCursor(QTextCursor::End);
//ui->plainTextEdit->insertPlainText(s.c_str()); // ui->plainTextEdit->insertPlainText(s.c_str());
//ui->plainTextEdit->moveCursor(QTextCursor::End); // ui->plainTextEdit->moveCursor(QTextCursor::End);
ui->plainTextEdit->appendPlainText(s.c_str()); ui->plainTextEdit->appendPlainText(s.c_str());
}; };
emb::set_stdout(write); emb::set_stdout(write);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow() { delete ui; }
{
delete ui;
}
void handle_system_exit() void handle_system_exit() { exit(-1); }
{
exit(-1);
}
int MainWindow::executePython(std::string command) int MainWindow::executePython(std::string command)
{ {
@ -39,9 +32,10 @@ int MainWindow::executePython(std::string command)
if (m == NULL) if (m == NULL)
return -1; return -1;
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
v = PyRun_StringFlags(command.c_str(), (command.empty() ? Py_file_input : Py_single_input), d, d, NULL); v = PyRun_StringFlags(command.c_str(),
if (v == NULL) (command.empty() ? Py_file_input : Py_single_input),
{ d, d, NULL);
if (v == NULL) {
PyObject *exception, *v, *tb; PyObject *exception, *v, *tb;
if (PyErr_ExceptionMatches(PyExc_SystemExit)) { if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
@ -60,8 +54,8 @@ int MainWindow::executePython(std::string command)
return 0; return 0;
PyErr_Clear(); PyErr_Clear();
PyObject* objectsRepresentation = PyObject_Str(v); PyObject *objectsRepresentation = PyObject_Str(v);
const char* errorStr = PyUnicode_AsUTF8(objectsRepresentation); const char *errorStr = PyUnicode_AsUTF8(objectsRepresentation);
ui->plainTextEdit->appendPlainText(errorStr); ui->plainTextEdit->appendPlainText(errorStr);
Py_DECREF(objectsRepresentation); Py_DECREF(objectsRepresentation);
Py_XDECREF(exception); Py_XDECREF(exception);
@ -75,7 +69,7 @@ int MainWindow::executePython(std::string command)
void MainWindow::on_lineEdit_returnPressed() void MainWindow::on_lineEdit_returnPressed()
{ {
std::string input = ui->lineEdit->text().toStdString(); std::string input = ui->lineEdit->text().toStdString();
ui->plainTextEdit->appendPlainText(std::string(">>> " + input).c_str()); ui->plainTextEdit->appendPlainText(std::string(">>> " + input).c_str());
ui->plainTextEdit->update(); ui->plainTextEdit->update();
ui->lineEdit->clear(); ui->lineEdit->clear();

View File

@ -12,17 +12,17 @@ class MainWindow : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit MainWindow(QWidget *parent = 0); explicit MainWindow(QWidget *parent = 0);
~MainWindow(); ~MainWindow();
private: private:
int executePython(std::string command); int executePython(std::string command);
private Q_SLOTS: private Q_SLOTS:
void on_lineEdit_returnPressed(); void on_lineEdit_returnPressed();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
emb::stdout_write_type write; emb::stdout_write_type write;
}; };

View File

@ -1,6 +1,26 @@
# Run ./nextpnr-ice40 --json ice40/blinky.json --run python/dump_design.py # Run ./nextpnr-ice40 --json ice40/blinky.json --run python/dump_design.py
for cell in sorted(design.cells, key=lambda x: x.first): for cell in sorted(design.cells, key=lambda x: x.first):
print("Cell {} : {}".format(cell.first, cell.second.type)) print("Cell {} : {}".format(cell.first, cell.second.type))
print("\tPorts:")
for port in sorted(cell.second.ports, key=lambda x: x.first): for port in sorted(cell.second.ports, key=lambda x: x.first):
dir = (" <-- ", " --> ", " <-> ")[int(port.second.type)] dir = (" <-- ", " --> ", " <-> ")[int(port.second.type)]
print(" {} {} {}".format(port.first, dir, port.second.net.name)) if port.second.net is not None:
print("\t\t{} {} {}".format(port.first, dir, port.second.net.name))
if len(cell.second.attrs) > 0:
print("\tAttrs:")
for attr in cell.second.attrs:
print("\t\t{}: {}".format(attr.first, attr.second))
if len(cell.second.params) > 0:
print("\tParams:")
for param in cell.second.params:
val = param.second
if val.isdigit():
val = bin(int(val))[2:]
val = "{}'b{}".format(len(val), val)
print("\t\t{}: {}".format(param.first, val))
if not cell.second.bel.nil():
print("\tBel: {}".format(chip.getBelName(cell.second.bel)))
print()