Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/verilogAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,22 +329,22 @@ std::string Module::emitModuleHeader() {

// emit parameter string
if (!parameters.empty()) {
module_header_str += " #(";
module_header_str += " #(\n ";
std::vector<std::string> param_strs;
for (auto &it : parameters) {
param_strs.push_back("parameter " + it.first->toString() + " = " +
it.second->toString());
}
module_header_str += join(param_strs, ", ");
module_header_str += ")";
module_header_str += join(param_strs, ",\n ");
module_header_str += "\n)";
}

// emit port string
module_header_str += " (";
module_header_str += " (\n ";
std::vector<std::string> ports_strs;
for (auto &it : ports) ports_strs.push_back(it->toString());
module_header_str += join(ports_strs, ", ");
module_header_str += ");\n";
module_header_str += join(ports_strs, ",\n ");
module_header_str += "\n);\n";
return module_header_str;
}

Expand Down Expand Up @@ -375,24 +375,24 @@ std::string ModuleInstantiation::toString() {
std::string module_inst_str = "";
module_inst_str += module_name;
if (!parameters.empty()) {
module_inst_str += " #(";
module_inst_str += " #(\n ";
std::vector<std::string> param_strs;
for (auto &it : parameters) {
param_strs.push_back("." + it.first->toString() + "(" +
it.second->toString() + ")");
}
module_inst_str += join(param_strs, ", ");
module_inst_str += ")";
module_inst_str += join(param_strs, ",\n ");
module_inst_str += "\n)";
}
module_inst_str += " " + instance_name + "(";
module_inst_str += " " + instance_name + " (\n ";
if (!connections->empty()) {
std::vector<std::string> param_strs;
for (auto &it : *connections) {
param_strs.push_back("." + it.first + "(" + it.second->toString() + ")");
}
module_inst_str += join(param_strs, ", ");
module_inst_str += join(param_strs, ",\n ");
}
module_inst_str += ");";
module_inst_str += "\n);";
return module_inst_str;
}

Expand Down
106 changes: 90 additions & 16 deletions tests/assign_inliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ TEST(InlineAssignTests, TestBasic) {
"test_module", std::move(ports), std::move(body));

std::string raw_str =
"module test_module (input i, output o, output [1:0] o_vec);\n"
"module test_module (\n"
" input i,\n"
" output o,\n"
" output [1:0] o_vec\n"
");\n"
"wire x;\n"
"wire [1:0] x_vec;\n"
"assign x = i;\n"
Expand All @@ -67,7 +71,11 @@ TEST(InlineAssignTests, TestBasic) {
EXPECT_EQ(module->toString(), raw_str);

std::string expected_str =
"module test_module (input i, output o, output [1:0] o_vec);\n"
"module test_module (\n"
" input i,\n"
" output o,\n"
" output [1:0] o_vec\n"
");\n"
"assign o = i;\n"
"assign o_vec = {i,i};\n"
"endmodule\n";
Expand Down Expand Up @@ -109,7 +117,10 @@ TEST(InlineAssignTests, TestBasicChain) {
"test_module", std::move(ports), std::move(body));

std::string raw_str =
"module test_module (input i, output o);\n"
"module test_module (\n"
" input i,\n"
" output o\n"
");\n"
"wire x;\n"
"wire y;\n"
"assign x = i;\n"
Expand All @@ -120,7 +131,10 @@ TEST(InlineAssignTests, TestBasicChain) {
EXPECT_EQ(module->toString(), raw_str);

std::string expected_str =
"module test_module (input i, output o);\n"
"module test_module (\n"
" input i,\n"
" output o\n"
");\n"
"assign o = i;\n"
"endmodule\n";

Expand Down Expand Up @@ -156,7 +170,10 @@ TEST(InlineAssignTests, TestBasicExpr) {
"test_module", std::move(ports), std::move(body));

std::string raw_str =
"module test_module (input i, output o);\n"
"module test_module (\n"
" input i,\n"
" output o\n"
");\n"
"wire x;\n"
"assign x = i ^ 1;\n"
"assign o = x;\n"
Expand All @@ -165,7 +182,10 @@ TEST(InlineAssignTests, TestBasicExpr) {
EXPECT_EQ(module->toString(), raw_str);

std::string expected_str =
"module test_module (input i, output o);\n"
"module test_module (\n"
" input i,\n"
" output o\n"
");\n"
"assign o = i ^ 1;\n"
"endmodule\n";

Expand Down Expand Up @@ -208,7 +228,10 @@ TEST(InlineAssignTests, TestExprChain) {
"test_module", std::move(ports), std::move(body));

std::string raw_str =
"module test_module (input i, output o);\n"
"module test_module (\n"
" input i,\n"
" output o\n"
");\n"
"wire x;\n"
"wire y;\n"
"assign x = i ^ 1;\n"
Expand All @@ -219,7 +242,10 @@ TEST(InlineAssignTests, TestExprChain) {
EXPECT_EQ(module->toString(), raw_str);

std::string expected_str =
"module test_module (input i, output o);\n"
"module test_module (\n"
" input i,\n"
" output o\n"
");\n"
"assign o = i ^ 1;\n"
"endmodule\n";

Expand Down Expand Up @@ -328,7 +354,18 @@ TEST(InlineAssignTests, TestInlineFanOutIdOrNum) {
"test_module", std::move(ports), std::move(body));

std::string raw_str =
"module test_module (input i, input [1:0] i_vec, output o0, output o1, output o2, output o3, output [1:0] o_vec0, output [1:0] o_vec1, output [1:0] o_vec2, output [1:0] o_vec3);\n"
"module test_module (\n"
" input i,\n"
" input [1:0] i_vec,\n"
" output o0,\n"
" output o1,\n"
" output o2,\n"
" output o3,\n"
" output [1:0] o_vec0,\n"
" output [1:0] o_vec1,\n"
" output [1:0] o_vec2,\n"
" output [1:0] o_vec3\n"
");\n"
"wire x;\n"
"wire y;\n"
"wire [1:0] x_vec;\n"
Expand All @@ -350,7 +387,18 @@ TEST(InlineAssignTests, TestInlineFanOutIdOrNum) {
EXPECT_EQ(module->toString(), raw_str);

std::string expected_str =
"module test_module (input i, input [1:0] i_vec, output o0, output o1, output o2, output o3, output [1:0] o_vec0, output [1:0] o_vec1, output [1:0] o_vec2, output [1:0] o_vec3);\n"
"module test_module (\n"
" input i,\n"
" input [1:0] i_vec,\n"
" output o0,\n"
" output o1,\n"
" output o2,\n"
" output o3,\n"
" output [1:0] o_vec0,\n"
" output [1:0] o_vec1,\n"
" output [1:0] o_vec2,\n"
" output [1:0] o_vec3\n"
");\n"
"assign o0 = i;\n"
"assign o1 = i;\n"
"assign o2 = 1;\n"
Expand Down Expand Up @@ -398,7 +446,11 @@ TEST(InlineAssignTests, TestNoInlineFanOutExpr) {
"test_module", std::move(ports), std::move(body));

std::string raw_str =
"module test_module (input i, output o0, output o1);\n"
"module test_module (\n"
" input i,\n"
" output o0,\n"
" output o1\n"
");\n"
"wire x;\n"
"assign x = i ^ 1;\n"
"assign o0 = x;\n"
Expand Down Expand Up @@ -445,7 +497,11 @@ TEST(InlineAssignTests, TestMultipleAssign) {
"test_module", std::move(ports), std::move(body));

std::string raw_str =
"module test_module (inout io0, inout io1, inout io2);\n"
"module test_module (\n"
" inout io0,\n"
" inout io1,\n"
" inout io2\n"
");\n"
"wire x;\n"
"assign x = io0;\n"
"assign x = io1;\n"
Expand Down Expand Up @@ -507,21 +563,39 @@ TEST(InlineAssignTests, TestInstConn) {
"test_module", std::move(ports), std::move(body));

std::string raw_str =
"module test_module (input i, input a, output o, output b);\n"
"module test_module (\n"
" input i,\n"
" input a,\n"
" output o,\n"
" output b\n"
");\n"
"wire x;\n"
"wire y;\n"
"assign x = i;\n"
"assign b = a;\n"
"inner_module inner_module_inst(.c(a), .i(x), .o(y));\n"
"inner_module inner_module_inst (\n"
" .c(a),\n"
" .i(x),\n"
" .o(y)\n"
");\n"
"assign o = y;\n"
"endmodule\n";

EXPECT_EQ(module->toString(), raw_str);

std::string expected_str =
"module test_module (input i, input a, output o, output b);\n"
"module test_module (\n"
" input i,\n"
" input a,\n"
" output o,\n"
" output b\n"
");\n"
"assign b = a;\n"
"inner_module inner_module_inst(.c(a), .i(i), .o(o));\n"
"inner_module inner_module_inst (\n"
" .c(a),\n"
" .i(i),\n"
" .o(o)\n"
");\n"
"endmodule\n";
vAST::AssignInliner transformer;
EXPECT_EQ(transformer.visit(std::move(module))->toString(), expected_str);
Expand Down
97 changes: 78 additions & 19 deletions tests/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,14 @@ TEST(BasicTests, TestModuleInst) {
make_simple_connections());

EXPECT_EQ(module_inst.toString(),
"test_module #(.param0(0), .param1(1)) "
"test_module_inst(.a(a), .b(b[0]), .c(c[31:0]));");
"test_module #(\n"
" .param0(0),\n"
" .param1(1)\n"
") test_module_inst (\n"
" .a(a),\n"
" .b(b[0]),\n"
" .c(c[31:0])\n"
");");
}

TEST(BasicTests, TestModule) {
Expand All @@ -282,9 +288,19 @@ TEST(BasicTests, TestModule) {
std::move(parameters));

std::string expected_str =
"module test_module (input i, output o);\nother_module #(.param0(0), "
".param1(1)) other_module_inst(.a(a), .b(b[0]), "
".c(c[31:0]));\nendmodule\n";
"module test_module (\n"
" input i,\n"
" output o\n"
");\n"
"other_module #(\n"
" .param0(0),\n"
" .param1(1)\n"
") other_module_inst (\n"
" .a(a),\n"
" .b(b[0]),\n"
" .c(c[31:0])\n"
");\n"
"endmodule\n";
EXPECT_EQ(module.toString(), expected_str);
}

Expand All @@ -295,10 +311,22 @@ TEST(BasicTests, TestParamModule) {
make_simple_params());

std::string expected_str =
"module test_module #(parameter param0 = 0, parameter param1 = "
"1) (input i, output o);\nother_module #(.param0(0), "
".param1(1)) other_module_inst(.a(a), .b(b[0]), "
".c(c[31:0]));\nendmodule\n";
"module test_module #(\n"
" parameter param0 = 0,\n"
" parameter param1 = 1\n"
") (\n"
" input i,\n"
" output o\n"
");\n"
"other_module #(\n"
" .param0(0),\n"
" .param1(1)\n"
") other_module_inst (\n"
" .a(a),\n"
" .b(b[0]),\n"
" .c(c[31:0])\n"
");\n"
"endmodule\n";
EXPECT_EQ(module_with_params.toString(), expected_str);
}

Expand All @@ -311,9 +339,18 @@ TEST(BasicTests, TestStringBodyModule) {
vAST::StringBodyModule string_body_module(name, make_simple_ports(),
string_body, make_simple_params());
std::string expected_str =
"module test_module #(parameter param0 = 0, parameter param1 = "
"1) (input i, output o);\nreg d;\nassign d = a + b;\nassign c = "
"d;\nendmodule\n";
"module test_module #(\n"
" parameter param0 = 0,\n"
" parameter param1 = 1\n"
") (\n"
" input i,\n"
" output o\n"
");\n"
"reg d;\n"
"assign d = a + b;\n"
"assign c = "
"d;\n"
"endmodule\n";
EXPECT_EQ(string_body_module.toString(), expected_str);

vAST::StringModule string_module(expected_str);
Expand Down Expand Up @@ -425,13 +462,35 @@ TEST(BasicTests, File) {
vAST::File file(modules);

std::string expected_str =
"module test_module0 (input i, output o);\nother_module "
"#(.param0(0), .param1(1)) other_module_inst(.a(a), "
".b(b[0]), .c(c[31:0]));\nendmodule\n\n"
"module test_module1 #(parameter param0 = 0, parameter param1 = "
"1) (input i, output o);\nother_module #(.param0(0), "
".param1(1)) other_module_inst(.a(a), .b(b[0]), "
".c(c[31:0]));\nendmodule\n";
"module test_module0 (\n"
" input i,\n"
" output o\n"
");\n"
"other_module #(\n"
" .param0(0),\n"
" .param1(1)\n"
") other_module_inst (\n"
" .a(a),\n"
" .b(b[0]),\n"
" .c(c[31:0])\n"
");\n"
"endmodule\n\n"
"module test_module1 #(\n"
" parameter param0 = 0,\n"
" parameter param1 = 1\n"
") (\n"
" input i,\n"
" output o\n"
");\n"
"other_module #(\n"
" .param0(0),\n"
" .param1(1)\n"
") other_module_inst (\n"
" .a(a),\n"
" .b(b[0]),\n"
" .c(c[31:0])\n"
");\n"
"endmodule\n";
EXPECT_EQ(file.toString(), expected_str);
}
TEST(BasicTests, Comment) {
Expand Down
Loading