From 9d86bef2b36db85af4a79fcaf6c5b51902c7f905 Mon Sep 17 00:00:00 2001 From: Lenny Truong Date: Thu, 6 Feb 2020 13:13:08 -0800 Subject: [PATCH 1/4] Use vector of pairs instead of map for instance ports --- include/verilogAST.hpp | 5 +++-- tests/assign_inliner.cpp | 14 +++++++------- tests/common.cpp | 17 ++++++++++------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/include/verilogAST.hpp b/include/verilogAST.hpp index 14e74d5..1e071d5 100644 --- a/include/verilogAST.hpp +++ b/include/verilogAST.hpp @@ -489,13 +489,14 @@ class ModuleInstantiation : public StructuralStatement { // map from instance port names to connection expression // NOTE: anonymous style of module connections is not supported - std::map> connections; + std::vector>> connections; // TODO Need to make sure that the instance parameters are a subset of the // module parameters ModuleInstantiation( std::string module_name, Parameters parameters, std::string instance_name, - std::map> connections) + std::vector>> + connections) : module_name(module_name), parameters(std::move(parameters)), instance_name(instance_name), diff --git a/tests/assign_inliner.cpp b/tests/assign_inliner.cpp index 2f83bca..309a889 100644 --- a/tests/assign_inliner.cpp +++ b/tests/assign_inliner.cpp @@ -490,20 +490,20 @@ TEST(InlineAssignTests, TestInstConn) { std::make_unique("a"))); vAST::Parameters parameters; - std::map> connections; - connections["c"] = vAST::make_id("a"); - connections["i"] = vAST::make_id("x"); - connections["o"] = vAST::make_id("y"); + std::vector>> + connections; + connections.push_back(std::make_pair("c", vAST::make_id("a"))); + connections.push_back(std::make_pair("i", vAST::make_id("x"))); + connections.push_back(std::make_pair("o", vAST::make_id("y"))); body.push_back(std::make_unique( - "inner_module", std::move(parameters), "inner_module_inst", - std::move(connections))); + "inner_module", std::move(parameters), "inner_module_inst", + std::move(connections))); body.push_back(std::make_unique( std::make_unique("o"), std::make_unique("y"))); - std::unique_ptr module = std::make_unique( "test_module", std::move(ports), std::move(body)); diff --git a/tests/common.cpp b/tests/common.cpp index 9e2f9ff..569b606 100644 --- a/tests/common.cpp +++ b/tests/common.cpp @@ -11,14 +11,17 @@ vAST::Parameters make_simple_params() { return parameters; } -std::map> +std::vector>> make_simple_connections() { - std::map> connections; - connections["a"] = vAST::make_id("a"); - connections["b"] = - std::make_unique(vAST::make_id("b"), vAST::make_num("0")); - connections["c"] = std::make_unique( - vAST::make_id("c"), vAST::make_num("31"), vAST::make_num("0")); + std::vector>> + connections; + connections.push_back(std::make_pair("a", vAST::make_id("a"))); + connections.push_back(std::make_pair( + "b", + std::make_unique(vAST::make_id("b"), vAST::make_num("0")))); + connections.push_back(std::make_pair( + "c", std::make_unique( + vAST::make_id("c"), vAST::make_num("31"), vAST::make_num("0")))); return connections; } From e03cec4b32a1835a5cc9d22111d5ad73b77d2161 Mon Sep 17 00:00:00 2001 From: Lenny Truong Date: Wed, 12 Feb 2020 14:26:01 -0800 Subject: [PATCH 2/4] Use connections class --- include/verilogAST.hpp | 34 ++++++++++++++++++++++++++++------ src/transformer.cpp | 2 +- src/verilogAST.cpp | 4 ++-- tests/assign_inliner.cpp | 9 ++++----- tests/common.cpp | 16 +++++++--------- 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/include/verilogAST.hpp b/include/verilogAST.hpp index 1e071d5..2b1cfbf 100644 --- a/include/verilogAST.hpp +++ b/include/verilogAST.hpp @@ -478,6 +478,31 @@ typedef std::vector< std::pair, std::unique_ptr>> Parameters; +typedef std::vector>> + ConnectionVector; + +class Connections { + public: + Connections() : connections() {} + ~Connections() = default; + + // Non-copyable class + Connections(const Connections&) = delete; + + // Takes ownership of @expr. + void insert(std::string name, std::unique_ptr expr) { + connections.push_back(std::make_pair(name, std::move(expr))); + } + + ConnectionVector::iterator begin() { return connections.begin(); } + ConnectionVector::iterator end() { return connections.end(); } + + bool empty() { return connections.empty(); } + + private: + ConnectionVector connections; +}; + class ModuleInstantiation : public StructuralStatement { public: std::string module_name; @@ -487,16 +512,13 @@ class ModuleInstantiation : public StructuralStatement { std::string instance_name; - // map from instance port names to connection expression // NOTE: anonymous style of module connections is not supported - std::vector>> connections; + std::unique_ptr connections; // TODO Need to make sure that the instance parameters are a subset of the // module parameters - ModuleInstantiation( - std::string module_name, Parameters parameters, std::string instance_name, - std::vector>> - connections) + ModuleInstantiation(std::string module_name, Parameters parameters, + std::string instance_name, std::unique_ptr connections) : module_name(module_name), parameters(std::move(parameters)), instance_name(instance_name), diff --git a/src/transformer.cpp b/src/transformer.cpp index 94f13b2..76d4f5c 100644 --- a/src/transformer.cpp +++ b/src/transformer.cpp @@ -186,7 +186,7 @@ std::unique_ptr Transformer::visit( std::unique_ptr Transformer::visit( std::unique_ptr node) { - for (auto&& conn : node->connections) { + for (auto&& conn : *node->connections) { conn.second = this->visit(std::move(conn.second)); } for (auto&& param : node->parameters) { diff --git a/src/verilogAST.cpp b/src/verilogAST.cpp index f026959..93d1311 100644 --- a/src/verilogAST.cpp +++ b/src/verilogAST.cpp @@ -385,9 +385,9 @@ std::string ModuleInstantiation::toString() { module_inst_str += ")"; } module_inst_str += " " + instance_name + "("; - if (!connections.empty()) { + if (!connections->empty()) { std::vector param_strs; - for (auto &it : connections) { + for (auto &it : *connections) { param_strs.push_back("." + it.first + "(" + it.second->toString() + ")"); } module_inst_str += join(param_strs, ", "); diff --git a/tests/assign_inliner.cpp b/tests/assign_inliner.cpp index 309a889..dcb3b27 100644 --- a/tests/assign_inliner.cpp +++ b/tests/assign_inliner.cpp @@ -490,11 +490,10 @@ TEST(InlineAssignTests, TestInstConn) { std::make_unique("a"))); vAST::Parameters parameters; - std::vector>> - connections; - connections.push_back(std::make_pair("c", vAST::make_id("a"))); - connections.push_back(std::make_pair("i", vAST::make_id("x"))); - connections.push_back(std::make_pair("o", vAST::make_id("y"))); + std::unique_ptr connections = std::make_unique(); + connections->insert("c", vAST::make_id("a")); + connections->insert("i", vAST::make_id("x")); + connections->insert("o", vAST::make_id("y")); body.push_back(std::make_unique( "inner_module", std::move(parameters), "inner_module_inst", diff --git a/tests/common.cpp b/tests/common.cpp index 569b606..d1cded5 100644 --- a/tests/common.cpp +++ b/tests/common.cpp @@ -11,17 +11,15 @@ vAST::Parameters make_simple_params() { return parameters; } -std::vector>> -make_simple_connections() { - std::vector>> - connections; - connections.push_back(std::make_pair("a", vAST::make_id("a"))); - connections.push_back(std::make_pair( +std::unique_ptr make_simple_connections() { + std::unique_ptr connections = std::make_unique(); + connections->insert("a", vAST::make_id("a")); + connections->insert( "b", - std::make_unique(vAST::make_id("b"), vAST::make_num("0")))); - connections.push_back(std::make_pair( + std::make_unique(vAST::make_id("b"), vAST::make_num("0"))); + connections->insert( "c", std::make_unique( - vAST::make_id("c"), vAST::make_num("31"), vAST::make_num("0")))); + vAST::make_id("c"), vAST::make_num("31"), vAST::make_num("0"))); return connections; } From c592a800d8d658c69ed654f052602584f0e3338c Mon Sep 17 00:00:00 2001 From: Lenny Truong Date: Wed, 12 Feb 2020 14:35:49 -0800 Subject: [PATCH 3/4] Add at methodg --- include/verilogAST.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/verilogAST.hpp b/include/verilogAST.hpp index 2b1cfbf..ff9e00f 100644 --- a/include/verilogAST.hpp +++ b/include/verilogAST.hpp @@ -494,6 +494,14 @@ class Connections { connections.push_back(std::make_pair(name, std::move(expr))); } + // Releases ownership of expression at @name if exists, othwerwise throws error. + std::unique_ptr at(std::string name) { + auto is_name = [name](auto& element) { return element.first == name; }; + auto it = std::find_if(connections.begin(), connections.end(), is_name); + if (it != connections.end()) return std::move(it->second); + throw std::runtime_error("Could not find '" + name + "'"); + } + ConnectionVector::iterator begin() { return connections.begin(); } ConnectionVector::iterator end() { return connections.end(); } From c2d4e017795377acbb74733c7ff42d047eb9fde3 Mon Sep 17 00:00:00 2001 From: Lenny Truong Date: Thu, 13 Feb 2020 14:25:58 -0800 Subject: [PATCH 4/4] Empty is const --- include/verilogAST.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/verilogAST.hpp b/include/verilogAST.hpp index ff9e00f..b675712 100644 --- a/include/verilogAST.hpp +++ b/include/verilogAST.hpp @@ -505,7 +505,7 @@ class Connections { ConnectionVector::iterator begin() { return connections.begin(); } ConnectionVector::iterator end() { return connections.end(); } - bool empty() { return connections.empty(); } + bool empty() const { return connections.empty(); } private: ConnectionVector connections;