Skip to content

Commit

Permalink
Attributes support (#126)
Browse files Browse the repository at this point in the history
* save work in progress

* just some documentation

* WIP

* Testing WIP

* Adding attributes in VRL dumper and testing

* WIP

* WIP

* collect on ports and more testing

* coverage

* update attributes

* bump naja_verilog version

* more testing

* numerical attributes

* bump naja-verilog version
  • Loading branch information
xtofalex authored Oct 30, 2024
1 parent 0a3c6b8 commit 1929df9
Show file tree
Hide file tree
Showing 20 changed files with 855 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/apps/naja_edit/examples/arm_core/gen_edge_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def edit():
edge = set()
first = True
for component in net.getComponents():
print(str(type(component)))
if isinstance(component, snl.SNLInstTerm):
instance = component.getInstance()
edge.add(instance)
Expand Down
21 changes: 21 additions & 0 deletions src/snl/formats/verilog/backend/SNLVRLDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "SNLBusNet.h"
#include "SNLBusNetBit.h"
#include "SNLInstTerm.h"
#include "SNLAttributes.h"
#include "SNLUtils.h"
#include "SNLDB0.h"

Expand Down Expand Up @@ -256,6 +257,23 @@ SNLName SNLVRLDumper::getNetName(const SNLNet* net, const DesignInsideAnonymousN
}
}

void SNLVRLDumper::dumpAttributes(const SNLObject* object, std::ostream& o) {
for (const auto& attribute: SNLAttributes::getAttributes(object)) {
o << "(* ";
o << attribute.getName().getString();
if (attribute.hasValue()) {
if (attribute.getValue().isString()) {
o << "=\"";
}
o << "=" << attribute.getValue().getString();
if (attribute.getValue().isString()) {
o << "\"";
}
}
o << " *)" << std::endl;
}
}

void SNLVRLDumper::dumpInterface(const SNLDesign* design, std::ostream& o, DesignInsideAnonymousNaming& naming) {
size_t nbChars = std::char_traits<char>::length("module (");
nbChars += design->getName().getString().size();
Expand Down Expand Up @@ -297,6 +315,7 @@ bool SNLVRLDumper::dumpNet(const SNLNet* net, std::ostream& o, DesignInsideAnony
} else {
netName = net->getName();
}
dumpAttributes(net, o);
o << "wire ";
if (auto bus = dynamic_cast<const SNLBusNet*>(net)) {
o << "[" << bus->getMSB() << ":" << bus->getLSB() << "] ";
Expand Down Expand Up @@ -533,6 +552,7 @@ bool SNLVRLDumper::dumpInstance(
} else {
instanceName = instance->getName().getString();
}
dumpAttributes(instance, o);
auto model = instance->getModel();
if (not model->isAnonymous()) { //FIXME !!
o << dumpName(model->getName().getString()) << " ";
Expand Down Expand Up @@ -698,6 +718,7 @@ void SNLVRLDumper::dumpOneDesign(const SNLDesign* design, std::ostream& o) {
if (design->isAnonymous()) {
createDesignName(design);
}
dumpAttributes(design, o);
o << "module " << dumpName(design->getName().getString());

dumpInterface(design, o, naming);
Expand Down
2 changes: 2 additions & 0 deletions src/snl/formats/verilog/backend/SNLVRLDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class SNLVRLDumper {
void setTopFileName(const std::string& name);
void setLibraryFileName(const std::string& name);
void setDumpHierarchy(bool mode);

void dumpAttributes(const SNLObject*, std::ostream& o);
/**
* \param design SNLDesign to dump.
* \param path directory path in which the dump will be created.
Expand Down
86 changes: 79 additions & 7 deletions src/snl/formats/verilog/frontend/SNLVRLConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,83 @@
#include "SNLBusNetBit.h"
#include "SNLScalarNet.h"
#include "SNLInstParameter.h"
#include "SNLAttributes.h"

#include "SNLVRLConstructorUtils.h"
#include "SNLVRLConstructorException.h"

namespace {

void createPort(naja::SNL::SNLDesign* design, const naja::verilog::Port& port) {
void collectAttributes(
naja::SNL::SNLObject* object,
const naja::SNL::SNLVRLConstructor::Attributes& attributes) {
if (auto design = dynamic_cast<naja::SNL::SNLDesign*>(object)) {
for (const auto attribute: attributes) {
naja::SNL::SNLName attributeName(attribute.name_.getString());
std::string expression;
naja::SNL::SNLAttributes::SNLAttribute::Value::Type valueType;
switch (attribute.expression_.getType()) {
case naja::verilog::ConstantExpression::Type::STRING:
valueType = naja::SNL::SNLAttributes::SNLAttribute::Value::Type::STRING;
break;
case naja::verilog::ConstantExpression::Type::NUMBER:
valueType = naja::SNL::SNLAttributes::SNLAttribute::Value::Type::NUMBER;
break;
}
if (attribute.expression_.valid_) {
expression = attribute.expression_.getString();
}
naja::SNL::SNLAttributes::addAttribute(
design,
naja::SNL::SNLAttributes::SNLAttribute(
attributeName,
naja::SNL::SNLAttributes::SNLAttribute::Value(valueType, expression)));
}
} else if (auto designObject = dynamic_cast<naja::SNL::SNLDesignObject*>(object)) {
for (const auto attribute: attributes) {
naja::SNL::SNLName attributeName(attribute.name_.getString());
std::string expression;
naja::SNL::SNLAttributes::SNLAttribute::Value::Type valueType;
switch (attribute.expression_.getType()) {
case naja::verilog::ConstantExpression::Type::STRING:
valueType = naja::SNL::SNLAttributes::SNLAttribute::Value::Type::STRING;
break;
case naja::verilog::ConstantExpression::Type::NUMBER:
valueType = naja::SNL::SNLAttributes::SNLAttribute::Value::Type::NUMBER;
break;
}
if (attribute.expression_.valid_) {
expression = attribute.expression_.getString();
}
naja::SNL::SNLAttributes::addAttribute(
designObject,
naja::SNL::SNLAttributes::SNLAttribute(
attributeName,
naja::SNL::SNLAttributes::SNLAttribute::Value(valueType, expression)));
}
}
}

void createPort(
naja::SNL::SNLDesign* design,
const naja::verilog::Port& port,
const naja::SNL::SNLVRLConstructor::Attributes& attributes) {
//spdlog::trace("Module {} create port: {}", design->getDescription(), port.getString());
naja::SNL::SNLTerm* term = nullptr;
if (port.isBus()) {
naja::SNL::SNLBusTerm::create(
term = naja::SNL::SNLBusTerm::create(
design,
naja::SNL::SNLVRLConstructor::VRLDirectionToSNLDirection(port.direction_),
port.range_.msb_,
port.range_.lsb_,
naja::SNL::SNLName(port.identifier_.name_));
} else {
naja::SNL::SNLScalarTerm::create(
term = naja::SNL::SNLScalarTerm::create(
design,
naja::SNL::SNLVRLConstructor::VRLDirectionToSNLDirection(port.direction_),
naja::SNL::SNLName(port.identifier_.name_));
}
collectAttributes(term, attributes);
}

void createPortNet(naja::SNL::SNLDesign* design, const naja::verilog::Port& port) {
Expand Down Expand Up @@ -181,6 +237,7 @@ void SNLVRLConstructor::construct(const std::filesystem::path& path) {
void SNLVRLConstructor::startModule(const naja::verilog::Identifier& module) {
if (inFirstPass()) {
currentModule_ = SNLDesign::create(library_, SNLName(module.name_));
collectAttributes(currentModule_, nextObjectAttributes_);
if (verbose_) {
std::cerr << "Construct Module: " << module.getString() << std::endl; //LCOV_EXCL_LINE
}
Expand All @@ -201,6 +258,7 @@ void SNLVRLConstructor::startModule(const naja::verilog::Identifier& module) {
}
createCurrentModuleAssignNets();
}
nextObjectAttributes_.clear();
}

void SNLVRLConstructor::moduleInterfaceSimplePort(const naja::verilog::Identifier& port) {
Expand Down Expand Up @@ -242,14 +300,16 @@ void SNLVRLConstructor::moduleImplementationPort(const naja::verilog::Port& port
} else {
createPortNet(currentModule_, port);
}
nextObjectAttributes_.clear();
}

void SNLVRLConstructor::moduleInterfaceCompletePort(const naja::verilog::Port& port) {
if (inFirstPass()) {
createPort(currentModule_, port);
createPort(currentModule_, port, nextObjectAttributes_);
} else {
createPortNet(currentModule_, port);
}
nextObjectAttributes_.clear();
}

void SNLVRLConstructor::addNet(const naja::verilog::Net& net) {
Expand Down Expand Up @@ -279,6 +339,7 @@ void SNLVRLConstructor::addNet(const naja::verilog::Net& net) {
snlNet = SNLScalarNet::create(currentModule_, SNLName(net.identifier_.name_));
}
snlNet->setType(VRLTypeToSNLType(net.type_));
collectAttributes(snlNet, nextObjectAttributes_);
//LCOV_EXCL_START
} catch (const SNLException& exception) {
std::ostringstream reason;
Expand All @@ -288,6 +349,7 @@ void SNLVRLConstructor::addNet(const naja::verilog::Net& net) {
}
//LCOV_EXCL_STOP
}
nextObjectAttributes_.clear();
}

void SNLVRLConstructor::addAssign(
Expand Down Expand Up @@ -411,7 +473,9 @@ void SNLVRLConstructor::addInstance(const naja::verilog::Identifier& instance) {
//might be a good idea to create a cache <Name, SNLDesign*> here
//in particular for primitives
currentInstance_ = SNLInstance::create(currentModule_, model, SNLName(instance.name_));
collectAttributes(currentInstance_, nextObjectAttributes_);
}
nextObjectAttributes_.clear();
}

void SNLVRLConstructor::addParameterAssignment(
Expand Down Expand Up @@ -581,6 +645,14 @@ void SNLVRLConstructor::addOrderedInstanceConnection(
}
}

void SNLVRLConstructor::addAttribute(
const naja::verilog::Identifier& attributeName,
const naja::verilog::ConstantExpression& expression) {
if (getParseAttributes()) {
nextObjectAttributes_.push_back(naja::verilog::Attribute(attributeName, expression));
}
}

void SNLVRLConstructor::endModule() {
if (verbose_) {
//LCOV_EXCL_START
Expand Down Expand Up @@ -608,8 +680,8 @@ void SNLVRLConstructor::endModule() {
reason << portName << " declared in interface has no declaration in module implementation.";
throw SNLVRLConstructorException(reason.str());
}
for (auto& port: currentModuleInterfacePorts_) {
createPort(currentModule_, *port);
for (const auto& port: currentModuleInterfacePorts_) {
createPort(currentModule_, *port, nextObjectAttributes_);
}
} else {
//Blackbox detection
Expand Down Expand Up @@ -730,7 +802,7 @@ void SNLVRLConstructor::collectIdentifierNets(

void SNLVRLConstructor::addDefParameterAssignment(
const naja::verilog::Identifiers& hierarchicalParameter,
const naja::verilog::Expression& expression) {
const naja::verilog::ConstantExpression& expression) {
if (not inFirstPass()) {
if (hierarchicalParameter.size() != 2) {
std::ostringstream reason;
Expand Down
12 changes: 10 additions & 2 deletions src/snl/formats/verilog/frontend/SNLVRLConstructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class SNLVRLConstructor: public naja::verilog::VerilogConstructor {

void setVerbose(bool verbose) { verbose_ = verbose; }
bool getVerbose() const { return verbose_; }
void setParseAttributes(bool parseAttributes) { parseAttributes_ = parseAttributes; }
bool getParseAttributes() const { return parseAttributes_; }

bool inFirstPass() const { return firstPass_; }
void setFirstPass(bool mode) { firstPass_ = mode; }
Expand All @@ -60,7 +62,12 @@ class SNLVRLConstructor: public naja::verilog::VerilogConstructor {
const naja::verilog::Expression& expression) override;
void addDefParameterAssignment(
const naja::verilog::Identifiers& hierarchicalParameter,
const naja::verilog::Expression& expression) override;
const naja::verilog::ConstantExpression& expression) override;

using Attributes = std::vector<naja::verilog::Attribute>;
void addAttribute(
const naja::verilog::Identifier& attributeName,
const naja::verilog::ConstantExpression& expression) override;
void endModule() override;
private:
void createCurrentModuleAssignNets();
Expand All @@ -82,15 +89,16 @@ class SNLVRLConstructor: public naja::verilog::VerilogConstructor {
bool verbose_ {false};
bool firstPass_ {true};
bool blackboxDetection_ {true};
bool parseAttributes_ {true};
SNLLibrary* library_ {nullptr};
Attributes nextObjectAttributes_ {};
SNLDesign* currentModule_ {nullptr};
std::string currentModelName_ {};
SNLInstance* currentInstance_ {nullptr};
using ParameterValues = std::map<std::string, std::string>;
ParameterValues currentInstanceParameterValues_ {};
SNLScalarNet* currentModuleAssign0_ {nullptr};
SNLScalarNet* currentModuleAssign1_ {nullptr};
//Following is used when
using InterfacePorts = std::vector<std::unique_ptr<naja::verilog::Port>>;
using InterfacePortsMap = std::map<std::string, size_t>;
InterfacePorts currentModuleInterfacePorts_ {};
Expand Down
1 change: 1 addition & 0 deletions src/snl/snl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_subdirectory(visual)

set(SNL_KERNEL_SOURCES
kernel/SNLObject.cpp
kernel/SNLAttributes.cpp
kernel/SNLUniverse.cpp kernel/SNLDB.cpp kernel/SNLDB0.cpp
kernel/SNLLibrary.cpp
kernel/SNLDesignObject.cpp
Expand Down
Loading

0 comments on commit 1929df9

Please sign in to comment.