Skip to content

Commit

Permalink
Merge pull request #144 from nanocoh/main
Browse files Browse the repository at this point in the history
load primitives and load design separation in py interface
  • Loading branch information
nanocoh authored Nov 13, 2024
2 parents 5f60504 + a93af53 commit 46146fc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 31 deletions.
58 changes: 37 additions & 21 deletions src/snl/python/snl_wrapping/PySNLDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,46 +81,60 @@ PyObject* PySNLDB_dumpSNL(PySNLDB* self, PyObject* args) {
Py_RETURN_TRUE;
}

static PyObject* PySNLDB_loadVerilog(PyObject*, PyObject* args) {

PyObject* PySNLDB_loadLibertyPrimitives(PySNLDB* self, PyObject* args) {
PyObject* arg0 = nullptr;
PyObject* arg1 = nullptr;
if (not PyArg_ParseTuple(args, "OO:SNLDB.loadVerilog", &arg0, &arg1)) {
setError("malformed SNLDB loadVerilog");
return nullptr;
if (not PyArg_ParseTuple(args, "O:SNLDB.loadLibertyPrimitives", &arg0)) {
setError("malformed SNLDB loadLibertyPrimitives");
Py_RETURN_FALSE;
}
if (not PyList_Check(arg0) || not PyList_Check(arg1)) {
setError("malformed SNLDesign.loadVerilog method");
return nullptr;
if (not PyList_Check(arg0)) {
setError("malformed SNLDesign.loadLibertyPrimitives method");
Py_RETURN_FALSE;
}
SNLDB* db = nullptr;
SNLLibrary* primitivesLibrary = nullptr;
db = SNLDB::create(SNLUniverse::get());
primitivesLibrary =
db = self->object_;
primitivesLibrary = db->getLibrary(SNLName("PRIMS"));
if (primitivesLibrary == nullptr) {
primitivesLibrary =
SNLLibrary::create(db, SNLLibrary::Type::Primitives, SNLName("PRIMS"));
}
for (int i = 0; i < PyList_Size(arg0); ++i) {
PyObject* object = PyList_GetItem(arg0, i);
if (not PyUnicode_Check(object)) {
setError("SNLDB loadVerilog argument should be a file path");
return nullptr;
setError("SNLDB loadLibertyPrimitives argument should be a file path");
Py_RETURN_FALSE;
}
std::string pathStr = PyUnicode_AsUTF8(object);
const std::filesystem::path path(pathStr);

auto extension = path.extension();
if (extension.empty()) {
setError("SNLDB loadVerilog design path has no extension");
return nullptr;
/*} else if (extension == ".py") {
SNLPyLoader::loadPrimitives(primitivesLibrary, path);*/
setError("SNLDB loadLibertyPrimitives design path has no extension");
Py_RETURN_FALSE;
} else if (extension == ".lib") {
SNLLibertyConstructor constructor(primitivesLibrary);
constructor.construct(path);
} else {
setError("SNLDB loadVerilog");
return nullptr;
setError("SNLDB loadLibertyPrimitives");
Py_RETURN_FALSE;
}
}
Py_RETURN_TRUE;
}

PyObject* PySNLDB_loadVerilog(PySNLDB* self, PyObject* args) {
PyObject* arg1 = nullptr;
if (not PyArg_ParseTuple(args, "O:SNLDB.loadVerilog", &arg1)) {
setError("malformed SNLDB loadVerilog");
Py_RETURN_FALSE;
}
if (not PyList_Check(arg1)) {
setError("malformed SNLDesign.loadVerilog method");
Py_RETURN_FALSE;
}
SNLDB* db = self->object_;
auto designLibrary = SNLLibrary::create(db, SNLName("DESIGN"));
SNLVRLConstructor constructor(designLibrary);
using Paths = std::vector<std::filesystem::path>;
Expand All @@ -129,7 +143,7 @@ static PyObject* PySNLDB_loadVerilog(PyObject*, PyObject* args) {
PyObject* object = PyList_GetItem(arg1, i);
if (not PyUnicode_Check(object)) {
setError("SNLDB loadSNL argument should be a file path");
return nullptr;
Py_RETURN_FALSE;
}
std::string pathStr = PyUnicode_AsUTF8(object);
const std::filesystem::path path(pathStr);
Expand All @@ -142,7 +156,7 @@ static PyObject* PySNLDB_loadVerilog(PyObject*, PyObject* args) {
} else {
setError("No top design was found after parsing verilog");
}
return PySNLDB_Link(db);
Py_RETURN_TRUE;
}

PyObject* PySNLDB_dumpVerilog(PySNLDB* self, PyObject* args) {
Expand Down Expand Up @@ -174,8 +188,10 @@ DBoDestroyAttribute(PySNLDB_destroy, PySNLDB)
METH_VARARGS | METH_STATIC, "create a SNLDB from SNL format."},
{"dumpSNL", (PyCFunction)PySNLDB_dumpSNL, METH_VARARGS,
"dump this SNLDB to SNL format."},
{"loadLibertyPrimitives", (PyCFunction)PySNLDB_loadLibertyPrimitives,
METH_VARARGS, "import primitives from Liberty format."},
{"loadVerilog", (PyCFunction)PySNLDB_loadVerilog,
METH_VARARGS | METH_STATIC, "create a SNLDB from SNL format."},
METH_VARARGS, "create a design from Verilog format."},
{"dumpVerilog", (PyCFunction)PySNLDB_dumpVerilog, METH_VARARGS,
"dump this SNLDB to SNL format."},
{"getLibrary", (PyCFunction)PySNLDB_getLibrary, METH_VARARGS,
Expand Down
23 changes: 13 additions & 10 deletions test/snl/python/snl_wrapping/test_snldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ def test(self):

def testVerilog(self):
u = snl.SNLUniverse.get()
db = snl.SNLDB.create(u)
self.assertIsNotNone(u)
designs = ["../../../../../test/snl/formats/verilog/benchmarks/test0.v"]
primitives = ["../../../../../test/snl/formats/liberty/benchmarks/asap7_excerpt/test0.lib"]
db = snl.SNLDB.loadVerilog(primitives, designs)
db.loadLibertyPrimitives(primitives)
db.loadVerilog(designs)
db.dumpVerilog("./test_verilog")
with self.assertRaises(SystemError) as context: db.dumpVerilog()
with self.assertRaises(SystemError) as context: db.dumpVerilog(-1)
Expand All @@ -56,22 +58,23 @@ def testCreationError(self):
with self.assertRaises(RuntimeError) as context: snl.SNLDB.create("ERROR")
with self.assertRaises(RuntimeError) as context: snl.SNLDB.loadSNL(u)
with self.assertRaises(RuntimeError) as context: snl.SNLDB.loadSNL("./test_verilogError.v")
u.destroy()
primitives = [1]
designs = [2]
primitivesNoExtention = ["../../../../../test/snl/formats/liberty/benchmarks/asap7_excerpt/test0"]
primitivesCorrect = ["../../../../../test/snl/formats/liberty/benchmarks/asap7_excerpt/test0.lib"]
primitivesWrongExtention = ["../../../../../test/snl/formats/liberty/benchmarks/asap7_excerpt/test0.lib"]
primitivesWrongExtention = ["../../../../../test/snl/formats/liberty/benchmarks/asap7_excerpt/test0.sd"]
with self.assertRaises(SystemError) as context: db.loadVerilog("Error", "Error")
with self.assertRaises(SystemError) as context: db.loadLibertyPrimitives("Error", "Error")
with self.assertRaises(SystemError) as context: db.loadVerilog("Error")
with self.assertRaises(SystemError) as context: db.loadLibertyPrimitives("Error")
with self.assertRaises(SystemError) as context: db.loadLibertyPrimitives(primitives)
with self.assertRaises(SystemError) as context: db.loadVerilog(designs)
with self.assertRaises(SystemError) as context: db.loadLibertyPrimitives(primitivesNoExtention)
with self.assertRaises(SystemError) as context: db.loadLibertyPrimitives(primitivesWrongExtention)
u.destroy()
with self.assertRaises(RuntimeError) as context: snl.SNLDB.create(u)
with self.assertRaises(RuntimeError) as context: snl.SNLDB.loadSNL()
with self.assertRaises(RuntimeError) as context: snl.SNLDB.loadSNL("./error")
with self.assertRaises(RuntimeError) as context: snl.SNLDB.loadVerilog("Error", "Error", "Error")
with self.assertRaises(RuntimeError) as context: snl.SNLDB.loadVerilog(primitivesCorrect, "Error")
with self.assertRaises(RuntimeError) as context: snl.SNLDB.loadVerilog("Error", "Error")
with self.assertRaises(RuntimeError) as context: snl.SNLDB.loadVerilog(primitives, designs)
with self.assertRaises(RuntimeError) as context: snl.SNLDB.loadVerilog(primitivesCorrect, designs)
with self.assertRaises(RuntimeError) as context: snl.SNLDB.loadVerilog(primitivesNoExtention, designs)
with self.assertRaises(RuntimeError) as context: snl.SNLDB.loadVerilog(primitivesWrongExtention, designs)

if __name__ == '__main__':
faulthandler.enable()
Expand Down

0 comments on commit 46146fc

Please sign in to comment.