Skip to content

Commit

Permalink
cleaning pywrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
xtofalex committed Mar 27, 2024
1 parent 0c289c0 commit 41a0ce1
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 29 deletions.
18 changes: 13 additions & 5 deletions src/apps/edit/NajaEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ using namespace naja::SNL;

namespace {

enum class FormatType { UNKNOWN, VERILOG, SNL };
enum class FormatType { NOT_PROVIDED, UNKNOWN, VERILOG, SNL };

FormatType argToFormatType(const std::string& inputFormat) {
if (inputFormat == "verilog") {
if (inputFormat.empty()) {
return FormatType::NOT_PROVIDED;
} else if (inputFormat == "verilog") {
return FormatType::VERILOG;
} else if (inputFormat == "snl") {
return FormatType::SNL;
Expand Down Expand Up @@ -95,7 +97,11 @@ int main(int argc, char* argv[]) {
if (auto outputFormatArg = program.present("-t")) {
outputFormat = *outputFormatArg;
} else {
outputFormat = inputFormat;
if (auto output = program.is_used("-o")) {
//in case output format is not provided and output path is provided
//output format is same as input format
outputFormat = inputFormat;
}
}
FormatType inputFormatType = argToFormatType(inputFormat);
FormatType outputFormatType = argToFormatType(outputFormat);
Expand Down Expand Up @@ -144,8 +150,10 @@ int main(int argc, char* argv[]) {
if (auto output = program.present("-o")) {
outputPath = std::filesystem::path(*output);
} else {
spdlog::critical("No output path was provided");
std::exit(EXIT_FAILURE);
if (outputFormatType != FormatType::NOT_PROVIDED) {
spdlog::critical("output option (-o) is mandatory when the output format provided");
std::exit(EXIT_FAILURE);
}
}

try {
Expand Down
3 changes: 2 additions & 1 deletion src/snl/python/snl_wrapping/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(SOURCES
PySNLInstance.cpp PySNLInstParameter.cpp PySNLInstTerm.cpp
PySNLLibraries.cpp PySNLDesigns.cpp
PySNLParameters.cpp PySNLInstParameters.cpp
PySNLNetComponents.cpp
PySNLTerms.cpp PySNLBitTerms.cpp PySNLScalarTerms.cpp PySNLBusTerms.cpp
PySNLNets.cpp PySNLBitNets.cpp
PySNLInstances.cpp PySNLInstTerms.cpp
Expand All @@ -29,4 +30,4 @@ Python3_add_library(snl MODULE PySNL.cpp)
target_link_libraries(snl PRIVATE naja_snl_python)

install(TARGETS naja_snl_python LIBRARY DESTINATION lib)
install(TARGETS snl LIBRARY DESTINATION lib/python)
install(TARGETS snl LIBRARY DESTINATION lib/python)
10 changes: 5 additions & 5 deletions src/snl/python/snl_wrapping/PyInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,12 @@ PyObject* richCompare(T left, T right, int op) {
return nullptr; \
}

#define GetContainerMethod(TYPE, ITERATED, CONTAINER) \
static PyObject* PySNL##TYPE##_get##CONTAINER(PySNL##TYPE *self) { \
METHOD_HEAD("SNL" #TYPE ".get" #CONTAINER "()") \
#define GetContainerMethod(TYPE, ITERATED, CONTAINER, GET_OBJECTS) \
static PyObject* PySNL##TYPE##_get##GET_OBJECTS(PySNL##TYPE *self) { \
METHOD_HEAD("SNL" #TYPE ".get" #GET_OBJECTS "()") \
PySNL##CONTAINER* pyObjects = nullptr; \
SNLTRY \
auto objects = new naja::NajaCollection<SNL##ITERATED*>(selfObject->get##CONTAINER()); \
auto objects = new naja::NajaCollection<SNL##ITERATED*>(selfObject->get##GET_OBJECTS()); \
pyObjects = PyObject_NEW(PySNL##CONTAINER, &PyTypeSNL##CONTAINER); \
if (not pyObjects) return nullptr; \
pyObjects->object_ = objects; \
Expand Down Expand Up @@ -451,4 +451,4 @@ PyObject* richCompare(T left, T right, int op) {
setError("malformed " #OWNER_TYPE "." #GETTER " method"); \
return nullptr;

#endif /* __PY_INTERFACE_H */
#endif /* __PY_INTERFACE_H */
5 changes: 5 additions & 0 deletions src/snl/python/snl_wrapping/PySNL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "PySNLBusTerms.h"
#include "PySNLNets.h"
#include "PySNLBitNets.h"
#include "PySNLNetComponents.h"
#include "PySNLInstances.h"
#include "PySNLInstTerms.h"

Expand Down Expand Up @@ -76,6 +77,7 @@ PyMODINIT_FUNC PyInit_snl(void) {
PySNLBusTerms_LinkPyType();
PySNLNets_LinkPyType();
PySNLBitNets_LinkPyType();
PySNLNetComponents_LinkPyType();
PySNLInstances_LinkPyType();
PySNLInstParameters_LinkPyType();
PySNLInstTerms_LinkPyType();
Expand Down Expand Up @@ -120,6 +122,8 @@ PyMODINIT_FUNC PyInit_snl(void) {
PYTYPE_READY(SNLNetsIterator);
PYTYPE_READY(SNLBitNets);
PYTYPE_READY(SNLBitNetsIterator);
PYTYPE_READY(SNLNetComponents);
PYTYPE_READY(SNLNetComponentsIterator);
PYTYPE_READY(SNLInstances);
PYTYPE_READY(SNLInstancesIterator);
PYTYPE_READY(SNLInstParameters);
Expand Down Expand Up @@ -157,6 +161,7 @@ PyMODINIT_FUNC PyInit_snl(void) {
Py_INCREF(&PyTypeSNLBusTerms);
Py_INCREF(&PyTypeSNLNets);
Py_INCREF(&PyTypeSNLBitNets);
Py_INCREF(&PyTypeSNLNetComponents);
Py_INCREF(&PyTypeSNLInstances);
Py_INCREF(&PyTypeSNLInstParameters);
Py_INCREF(&PyTypeSNLInstTerms);
Expand Down
18 changes: 16 additions & 2 deletions src/snl/python/snl_wrapping/PySNLBitNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
// SPDX-License-Identifier: Apache-2.0

#include "PySNLBitNet.h"

#include "PyInterface.h"
#include "PySNLNetComponents.h"
#include "PySNLInstTerms.h"
#include "PySNLBitTerms.h"

namespace PYSNL {

Expand All @@ -13,9 +17,19 @@ using namespace naja::SNL;
#undef ACCESS_CLASS
#define ACCESS_OBJECT parent_.parent_.object_
#define ACCESS_CLASS(_pyObject) &(_pyObject->parent_->parent_)
#define METHOD_HEAD(function) GENERIC_METHOD_HEAD(BitNet, net, function)
#define METHOD_HEAD(function) GENERIC_METHOD_HEAD(SNLBitNet, function)

GetContainerMethod(BitNet, NetComponent, NetComponents, Components)
GetContainerMethod(BitNet, InstTerm, InstTerms, InstTerms)
GetContainerMethod(BitNet, BitTerm, BitTerms, BitTerms)

PyMethodDef PySNLBitNet_Methods[] = {
{ "getComponents", (PyCFunction)PySNLBitNet_getComponents, METH_NOARGS,
"get a container of Net Components."},
{ "getInstTerms", (PyCFunction)PySNLBitNet_getInstTerms, METH_NOARGS,
"get a container of Net InstTerms."},
{ "getBitTerms", (PyCFunction)PySNLBitNet_getBitTerms, METH_NOARGS,
"get a container of Net BitTerms."},
{NULL, NULL, 0, NULL} /* sentinel */
};

Expand All @@ -25,4 +39,4 @@ DBoLinkCreateMethod(SNLBitNet)
PyTypeSNLObjectWithSNLIDLinkPyType(SNLBitNet)
PyTypeInheritedObjectDefinitions(SNLBitNet, SNLNet)

}
}
18 changes: 15 additions & 3 deletions src/snl/python/snl_wrapping/PySNLBitTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "PySNLTerm.h"
#include "PySNLBitTerm.h"

#include "PyInterface.h"
#include "PySNLBitTerm.h"
#include "PySNLScalarTerm.h"
#include "PySNLBusTermBit.h"

namespace PYSNL {

Expand All @@ -23,7 +24,18 @@ PyMethodDef PySNLBitTerm_Methods[] = {

DBoDeallocMethod(SNLBitTerm)

DBoLinkCreateMethod(SNLBitTerm)
PyObject* PySNLBitTerm_Link(SNLBitTerm* object) {
if (not object) {
Py_RETURN_NONE;

Check warning on line 29 in src/snl/python/snl_wrapping/PySNLBitTerm.cpp

View check run for this annotation

Codecov / codecov/patch

src/snl/python/snl_wrapping/PySNLBitTerm.cpp#L29

Added line #L29 was not covered by tests
}
if (auto busTermBit = dynamic_cast<SNLBusTermBit*>(object)) {
return PySNLBusTermBit_Link(busTermBit);
} else {
auto scalarTerm = static_cast<SNLScalarTerm*>(object);
return PySNLScalarTerm_Link(scalarTerm);
}
}

PyTypeSNLObjectWithSNLIDLinkPyType(SNLBitTerm)
PyTypeInheritedObjectDefinitions(SNLBitTerm, SNLTerm)

Expand Down
2 changes: 1 addition & 1 deletion src/snl/python/snl_wrapping/PySNLDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static PyObject* PySNLDB_create(PyObject*, PyObject* args) {
}

GetObjectByName(DB, Library)
GetContainerMethod(DB, Library, Libraries)
GetContainerMethod(DB, Library, Libraries, Libraries)

DBoDestroyAttribute(PySNLDB_destroy, PySNLDB)

Expand Down
16 changes: 8 additions & 8 deletions src/snl/python/snl_wrapping/PySNLDesign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,14 @@ GetObjectByName(Design, BusNet)
GetObjectByName(Design, Parameter)
GetNameMethod(SNLDesign)
GetBoolAttribute(Design, isAnonymous)
GetContainerMethod(Design, Term, Terms)
GetContainerMethod(Design, BitTerm, BitTerms)
GetContainerMethod(Design, ScalarTerm, ScalarTerms)
GetContainerMethod(Design, BusTerm, BusTerms)
GetContainerMethod(Design, Net, Nets)
GetContainerMethod(Design, BitNet, BitNets)
GetContainerMethod(Design, Instance, Instances)
GetContainerMethod(Design, Parameter, Parameters)
GetContainerMethod(Design, Term, Terms, Terms)
GetContainerMethod(Design, BitTerm, BitTerms, BitTerms)
GetContainerMethod(Design, ScalarTerm, ScalarTerms, ScalarTerms)
GetContainerMethod(Design, BusTerm, BusTerms, BusTerms)
GetContainerMethod(Design, Net, Nets, Nets)
GetContainerMethod(Design, BitNet, BitNets, BitNets)
GetContainerMethod(Design, Instance, Instances, Instances)
GetContainerMethod(Design, Parameter, Parameters, Parameters)

DBoDestroyAttribute(PySNLDesign_destroy, PySNLDesign)

Expand Down
4 changes: 2 additions & 2 deletions src/snl/python/snl_wrapping/PySNLInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ static PyObject* PySNLInstance_getInstTerm(PySNLInstance* self, PyObject* args)
return PySNLInstTerm_Link(obj);
}

GetContainerMethod(Instance, InstTerm, InstTerms)
GetContainerMethod(Instance, InstParameter, InstParameters)
GetContainerMethod(Instance, InstTerm, InstTerms, InstTerms)
GetContainerMethod(Instance, InstParameter, InstParameters, InstParameters)

PyMethodDef PySNLInstance_Methods[] = {
{ "create", (PyCFunction)PySNLInstance_create, METH_VARARGS|METH_STATIC,
Expand Down
2 changes: 1 addition & 1 deletion src/snl/python/snl_wrapping/PySNLLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ GetBoolAttribute(Library, isPrimitives)

GetNameMethod(SNLLibrary)

GetContainerMethod(Library, Design, Designs)
GetContainerMethod(Library, Design, Designs, Designs)

DBoDeallocMethod(SNLLibrary)

Expand Down
15 changes: 14 additions & 1 deletion src/snl/python/snl_wrapping/PySNLNetComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "PySNLNetComponent.h"

#include "PyInterface.h"
#include "PySNLInstTerm.h"
#include "PySNLBitTerm.h"
#include "PySNLBitNet.h"

namespace PYSNL {
Expand Down Expand Up @@ -49,7 +51,18 @@ PyMethodDef PySNLNetComponent_Methods[] = {

DBoDeallocMethod(SNLNetComponent)

DBoLinkCreateMethod(SNLNetComponent)
PyObject* PySNLNetComponent_Link(SNLNetComponent* object) {
if (not object) {
Py_RETURN_NONE;

Check warning on line 56 in src/snl/python/snl_wrapping/PySNLNetComponent.cpp

View check run for this annotation

Codecov / codecov/patch

src/snl/python/snl_wrapping/PySNLNetComponent.cpp#L56

Added line #L56 was not covered by tests
}
if (auto instTerm = dynamic_cast<SNLInstTerm*>(object)) {
return PySNLInstTerm_Link(instTerm);

Check warning on line 59 in src/snl/python/snl_wrapping/PySNLNetComponent.cpp

View check run for this annotation

Codecov / codecov/patch

src/snl/python/snl_wrapping/PySNLNetComponent.cpp#L59

Added line #L59 was not covered by tests
} else {
auto bitTerm = static_cast<SNLBitTerm*>(object);
return PySNLBitTerm_Link(bitTerm);
}
}

PyTypeSNLObjectWithSNLIDLinkPyType(SNLNetComponent)
PyTypeInheritedObjectDefinitions(SNLNetComponent, SNLDesignObject)

Expand Down
19 changes: 19 additions & 0 deletions src/snl/python/snl_wrapping/PySNLNetComponents.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2023 The Naja authors <https://github.com/najaeda/naja/blob/main/AUTHORS>
//
// SPDX-License-Identifier: Apache-2.0

#include "PySNLNetComponents.h"

#include "PyInterface.h"
#include "PySNLNetComponent.h"

namespace PYSNL {

using namespace naja::SNL;

PyTypeContainerObjectDefinitions(SNLNetComponents)
PyTypeContainerObjectDefinitions(SNLNetComponentsIterator)

PyContainerMethods(SNLNetComponent, SNLNetComponents)

}
35 changes: 35 additions & 0 deletions src/snl/python/snl_wrapping/PySNLNetComponents.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: 2024 The Naja authors <https://github.com/najaeda/naja/blob/main/AUTHORS>
//
// SPDX-License-Identifier: Apache-2.0

#ifndef __PY_SNL_NET_COMPONENTS_H_
#define __PY_SNL_NET_COMPONENTS_H_

#include <Python.h>
#include "NajaCollection.h"

namespace naja::SNL {
class SNLNetComponent;
}

namespace PYSNL {

typedef struct {
PyObject_HEAD
naja::NajaCollection<naja::SNL::SNLNetComponent*>* object_;
} PySNLNetComponents;

typedef struct {
PyObject_HEAD
naja::NajaCollection<naja::SNL::SNLNetComponent*>::Iterator* object_;
PySNLNetComponents* container_;
} PySNLNetComponentsIterator;

extern PyTypeObject PyTypeSNLNetComponents;
extern PyTypeObject PyTypeSNLNetComponentsIterator;

extern void PySNLNetComponents_LinkPyType();

} /* PYSNL namespace */

#endif /* __PY_SNL_NET_COMPONENTS_H_ */
20 changes: 20 additions & 0 deletions test/snl/python/snl_wrapping/test_snlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,19 @@ def test0(self):
self.assertIsNone(self.design.getBusNet("I0"))
self.assertEqual(1, sum(1 for b in i0Net.getBits()))
self.assertEqual(1, sum(1 for n in self.design.getNets()))
self.assertEqual(1, sum(1 for c in i0Net.getComponents()))
self.assertEqual(1, sum(1 for b in i0Net.getBitTerms()))
self.assertEqual(0, sum(1 for i in i0Net.getInstTerms()))
c = next(iter(i0Net.getComponents()))
self.assertEqual(i0, c)
self.assertIsInstance(c, snl.SNLNetComponent)
self.assertIsInstance(c, snl.SNLBitTerm)
self.assertIsInstance(c, snl.SNLScalarTerm)

i1Net = snl.SNLBusNet.create(self.design, 4, 0, "I1")
self.assertIsInstance(i1Net, snl.SNLBusNet)
self.assertIsInstance(i1Net, snl.SNLNet)
self.assertIsInstance(i1Net, snl.SNLDesignObject)
self.assertIsNotNone(i1Net)
self.assertEqual("I1", i1Net.getName())
self.assertEqual(4, i1.getMSB())
Expand All @@ -57,12 +68,21 @@ def test0(self):
self.assertEqual(5, sum(1 for b in i1Net.getBits()))
self.assertEqual(2, sum(1 for n in self.design.getNets()))
self.assertEqual(1+5, sum(1 for b in self.design.getBitNets()))
i1Bit4 = i1.getBit(4)

i1Bit4 = i1.getBit(4)
i1NetBit4 = i1Net.getBit(4)
self.assertIsNotNone(i1Bit4)
self.assertIsNotNone(i1NetBit4)
self.assertEqual(i1Bit4.getNet(), i1NetBit4)
self.assertEqual(1, sum(1 for c in i1NetBit4.getComponents()))
self.assertEqual(1, sum(1 for b in i1NetBit4.getBitTerms()))
self.assertEqual(0, sum(1 for i in i1NetBit4.getInstTerms()))
c = next(iter(i1NetBit4.getComponents()))
self.assertEqual(i1Bit4, c)
self.assertIsInstance(c, snl.SNLNetComponent)
self.assertIsInstance(c, snl.SNLBitTerm)
self.assertIsInstance(c, snl.SNLBusTermBit)

self.assertIsNone(i1.getBit(5))
self.assertIsNone(i1Net.getBit(5))
Expand Down

0 comments on commit 41a0ce1

Please sign in to comment.