From 025553247f21812f9f0fc9814632122d094d0223 Mon Sep 17 00:00:00 2001 From: xtof Date: Wed, 30 Oct 2024 11:34:11 +0100 Subject: [PATCH] numerical attributes --- .../verilog/frontend/SNLVRLConstructor.cpp | 23 ++++++-- .../verilog/frontend/SNLVRLConstructor.h | 3 + .../verilog/benchmarks/test_attributes.v | 2 + .../SNLVRLConstructorTestAttributes.cpp | 58 +++++++++++++++++-- 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/snl/formats/verilog/frontend/SNLVRLConstructor.cpp b/src/snl/formats/verilog/frontend/SNLVRLConstructor.cpp index 639be083..71b17d07 100644 --- a/src/snl/formats/verilog/frontend/SNLVRLConstructor.cpp +++ b/src/snl/formats/verilog/frontend/SNLVRLConstructor.cpp @@ -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(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"); } } @@ -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() { diff --git a/src/snl/formats/verilog/frontend/SNLVRLConstructor.h b/src/snl/formats/verilog/frontend/SNLVRLConstructor.h index 48ff93c5..123ae96f 100644 --- a/src/snl/formats/verilog/frontend/SNLVRLConstructor.h +++ b/src/snl/formats/verilog/frontend/SNLVRLConstructor.h @@ -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; } @@ -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}; diff --git a/test/snl/formats/verilog/benchmarks/test_attributes.v b/test/snl/formats/verilog/benchmarks/test_attributes.v index 6ca0510b..ed42fcd9 100644 --- a/test/snl/formats/verilog/benchmarks/test_attributes.v +++ b/test/snl/formats/verilog/benchmarks/test_attributes.v @@ -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 @@ -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), diff --git a/test/snl/formats/verilog/frontend/SNLVRLConstructorTestAttributes.cpp b/test/snl/formats/verilog/frontend/SNLVRLConstructorTestAttributes.cpp index 28824f52..18170895 100644 --- a/test/snl/formats/verilog/frontend/SNLVRLConstructorTestAttributes.cpp +++ b/test/snl/formats/verilog/frontend/SNLVRLConstructorTestAttributes.cpp @@ -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); @@ -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; + 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; @@ -60,12 +83,14 @@ 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; + 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( @@ -73,7 +98,12 @@ TEST_F(SNLVRLConstructorTestAttributes, test) { 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")) ) ); @@ -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()); } \ No newline at end of file