Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into linebreaks
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardt committed Feb 21, 2020
2 parents 2e9ef03 + c8c05c9 commit 0b6327c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
41 changes: 36 additions & 5 deletions include/verilogAST.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,39 @@ typedef std::vector<
std::pair<std::unique_ptr<Identifier>, std::unique_ptr<Expression>>>
Parameters;

typedef std::vector<std::pair<std::string, std::unique_ptr<Expression>>>
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<Expression> expr) {
connections.push_back(std::make_pair(name, std::move(expr)));
}

// Releases ownership of expression at @name if exists, othwerwise throws error.
std::unique_ptr<Expression> 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(); }

bool empty() const { return connections.empty(); }

private:
ConnectionVector connections;
};

class ModuleInstantiation : public StructuralStatement {
public:
std::string module_name;
Expand All @@ -487,15 +520,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::map<std::string, std::unique_ptr<Expression>> connections;
std::unique_ptr<Connections> 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<std::string, std::unique_ptr<Expression>> connections)
ModuleInstantiation(std::string module_name, Parameters parameters,
std::string instance_name, std::unique_ptr<Connections> connections)
: module_name(module_name),
parameters(std::move(parameters)),
instance_name(instance_name),
Expand Down
2 changes: 1 addition & 1 deletion src/transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ std::unique_ptr<InlineVerilog> Transformer::visit(

std::unique_ptr<ModuleInstantiation> Transformer::visit(
std::unique_ptr<ModuleInstantiation> node) {
for (auto&& conn : node->connections) {
for (auto&& conn : *node->connections) {
conn.second = this->visit(std::move(conn.second));
}
for (auto&& param : node->parameters) {
Expand Down
4 changes: 2 additions & 2 deletions src/verilogAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ std::string ModuleInstantiation::toString() {
module_inst_str += "\n)";
}
module_inst_str += " " + instance_name + " (\n ";
if (!connections.empty()) {
if (!connections->empty()) {
std::vector<std::string> 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, ",\n ");
Expand Down
13 changes: 6 additions & 7 deletions tests/assign_inliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,20 +546,19 @@ TEST(InlineAssignTests, TestInstConn) {
std::make_unique<vAST::Identifier>("a")));

vAST::Parameters parameters;
std::map<std::string, std::unique_ptr<vAST::Expression>> connections;
connections["c"] = vAST::make_id("a");
connections["i"] = vAST::make_id("x");
connections["o"] = vAST::make_id("y");
std::unique_ptr<vAST::Connections> connections = std::make_unique<vAST::Connections>();
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<vAST::ModuleInstantiation>(
"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<vAST::ContinuousAssign>(
std::make_unique<vAST::Identifier>("o"),
std::make_unique<vAST::Identifier>("y")));


std::unique_ptr<vAST::AbstractModule> module = std::make_unique<vAST::Module>(
"test_module", std::move(ports), std::move(body));

Expand Down
17 changes: 9 additions & 8 deletions tests/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ vAST::Parameters make_simple_params() {
return parameters;
}

std::map<std::string, std::unique_ptr<vAST::Expression>>
make_simple_connections() {
std::map<std::string, std::unique_ptr<vAST::Expression>> connections;
connections["a"] = vAST::make_id("a");
connections["b"] =
std::make_unique<vAST::Index>(vAST::make_id("b"), vAST::make_num("0"));
connections["c"] = std::make_unique<vAST::Slice>(
vAST::make_id("c"), vAST::make_num("31"), vAST::make_num("0"));
std::unique_ptr<vAST::Connections> make_simple_connections() {
std::unique_ptr<vAST::Connections> connections = std::make_unique<vAST::Connections>();
connections->insert("a", vAST::make_id("a"));
connections->insert(
"b",
std::make_unique<vAST::Index>(vAST::make_id("b"), vAST::make_num("0")));
connections->insert(
"c", std::make_unique<vAST::Slice>(
vAST::make_id("c"), vAST::make_num("31"), vAST::make_num("0")));

return connections;
}
Expand Down

0 comments on commit 0b6327c

Please sign in to comment.