Skip to content

Commit

Permalink
numerical attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
xtofalex committed Oct 30, 2024
1 parent 3e9993e commit 0255532
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
23 changes: 18 additions & 5 deletions src/snl/formats/verilog/frontend/SNLVRLConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,32 @@ void collectAttributes(
}
naja::SNL::SNLAttributes::addAttribute(
design,
naja::SNL::SNLAttributes::SNLAttribute(attributeName, expression));
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, expression));
naja::SNL::SNLAttributes::SNLAttribute(
attributeName,
naja::SNL::SNLAttributes::SNLAttribute::Value(valueType, expression)));
}
} else {
throw naja::SNL::SNLException("Unsupported object type for adding attributes");
}
}

Expand Down Expand Up @@ -637,7 +648,9 @@ void SNLVRLConstructor::addOrderedInstanceConnection(
void SNLVRLConstructor::addAttribute(
const naja::verilog::Identifier& attributeName,
const naja::verilog::ConstantExpression& expression) {
nextObjectAttributes_.push_back(naja::verilog::Attribute(attributeName, expression));
if (getParseAttributes()) {
nextObjectAttributes_.push_back(naja::verilog::Attribute(attributeName, expression));
}
}

void SNLVRLConstructor::endModule() {
Expand Down
3 changes: 3 additions & 0 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 Down Expand Up @@ -87,6 +89,7 @@ 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};
Expand Down
2 changes: 2 additions & 0 deletions test/snl/formats/verilog/benchmarks/test_attributes.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Attribute for the entire module
(* MODULE_ATTRIBUTE = "Top level simple_netlist module", MODULE_VERSION = "1.0" *)
(* VERSION = 3 *)
module simple_netlist (
(* INPUT_ATTRIBUTE_A = "Input signal A" *)
input a, // Input a
Expand All @@ -21,6 +22,7 @@ module simple_netlist (

// Attribute for and2 gate instance
(* INSTANCE_ATTRIBUTE_AND = "and2_inst", description = "2-input AND gate instance" *)
(* VERSION = 3 *)
and2 and2_inst (
.a(a),
.b(b),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SNLVRLConstructorTestAttributes: public ::testing::Test {
SNLLibrary* library_;
};

TEST_F(SNLVRLConstructorTestAttributes, test) {
TEST_F(SNLVRLConstructorTestAttributes, test0) {
auto db = SNLDB::create(SNLUniverse::get());
SNLVRLConstructor constructor(library_);
std::filesystem::path benchmarksPath(SNL_VRL_BENCHMARKS_PATH);
Expand All @@ -45,6 +45,29 @@ TEST_F(SNLVRLConstructorTestAttributes, test) {
ASSERT_EQ(3, library_->getDesigns().size());
auto simple_netlist = library_->getDesign(SNLName("simple_netlist"));
ASSERT_NE(simple_netlist, nullptr);

ASSERT_EQ(3, SNLAttributes::getAttributes(simple_netlist).size());
using Attributes = std::vector<SNLAttributes::SNLAttribute>;
Attributes simple_netlistAttributes(
SNLAttributes::getAttributes(simple_netlist).begin(),
SNLAttributes::getAttributes(simple_netlist).end());
EXPECT_EQ(3, simple_netlistAttributes.size());
EXPECT_THAT(simple_netlistAttributes,
ElementsAre(
SNLAttributes::SNLAttribute(
SNLName("MODULE_ATTRIBUTE"),
SNLAttributes::SNLAttribute::Value("Top level simple_netlist module")),
SNLAttributes::SNLAttribute(
SNLName("MODULE_VERSION"),
SNLAttributes::SNLAttribute::Value("1.0")),
SNLAttributes::SNLAttribute(
SNLName("VERSION"),
SNLAttributes::SNLAttribute::Value(
SNLAttributes::SNLAttribute::Value::Type::NUMBER,
"3"))
)
);

//2 standard instances, 2 assigns
ASSERT_EQ(4, simple_netlist->getInstances().size());
using Instances = std::vector<SNLInstance*>;
Expand All @@ -60,20 +83,27 @@ TEST_F(SNLVRLConstructorTestAttributes, test) {

auto ins0 = simple_netlist->getInstance(SNLName("and2_inst"));
ASSERT_NE(ins0, nullptr);
EXPECT_EQ(2, SNLAttributes::getAttributes(ins0).size());
using Attributes = std::vector<SNLAttributes::SNLAttribute>;
EXPECT_EQ(3, SNLAttributes::getAttributes(ins0).size());
Attributes ins0Attributes(
SNLAttributes::getAttributes(ins0).begin(),
SNLAttributes::getAttributes(ins0).end());
EXPECT_EQ(2, ins0Attributes.size());
EXPECT_EQ(3, ins0Attributes.size());
for (const auto& attribute: ins0Attributes) {
std::cout << attribute.getString() << std::endl;
}
EXPECT_THAT(ins0Attributes,
ElementsAre(
SNLAttributes::SNLAttribute(
SNLName("INSTANCE_ATTRIBUTE_AND"),
SNLAttributes::SNLAttribute::Value("and2_inst")),
SNLAttributes::SNLAttribute(
SNLName("description"),
SNLAttributes::SNLAttribute::Value("2-input AND gate instance"))
SNLAttributes::SNLAttribute::Value("2-input AND gate instance")),
SNLAttributes::SNLAttribute(
SNLName("VERSION"),
SNLAttributes::SNLAttribute::Value(
SNLAttributes::SNLAttribute::Value::Type::NUMBER,
"3"))
)
);

Expand Down Expand Up @@ -199,5 +229,23 @@ TEST_F(SNLVRLConstructorTestAttributes, test) {
SNLAttributes::SNLAttribute::Value("Wire connecting OR gate output to top output")),
orWireAttributes[0]
);
}

TEST_F(SNLVRLConstructorTestAttributes, testDisableAttributes) {
auto db = SNLDB::create(SNLUniverse::get());
SNLVRLConstructor constructor(library_);
std::filesystem::path benchmarksPath(SNL_VRL_BENCHMARKS_PATH);
constructor.setParseAttributes(false); //disable attributes
constructor.construct(benchmarksPath/"test_attributes.v");

ASSERT_EQ(3, library_->getDesigns().size());
auto simple_netlist = library_->getDesign(SNLName("simple_netlist"));
ASSERT_NE(simple_netlist, nullptr);
ASSERT_TRUE(SNLAttributes::getAttributes(simple_netlist).empty());

auto ins0 = simple_netlist->getInstance(SNLName("and2_inst"));
ASSERT_NE(ins0, nullptr);
EXPECT_TRUE(SNLAttributes::getAttributes(ins0).empty());
auto ins1 = simple_netlist->getInstance(SNLName("or2_inst"));
EXPECT_TRUE(SNLAttributes::getAttributes(ins1).empty());
}

0 comments on commit 0255532

Please sign in to comment.