Skip to content

Commit

Permalink
Move some helper functions into library
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardt committed Jul 12, 2019
1 parent 7f91e1f commit 6cab41d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 65 deletions.
28 changes: 28 additions & 0 deletions include/verilogAST.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,32 @@ class File : public Node {
~File(){};
};

// Helper functions for constructing unique pointers
std::unique_ptr<Identifier> make_id(std::string name) {
return std::make_unique<Identifier>(name);
}

std::unique_ptr<NumericLiteral> make_num(std::string val) {
return std::make_unique<NumericLiteral>(val);
}

std::unique_ptr<BinaryOp> make_binop(std::unique_ptr<Expression> left,
BinOp::BinOp op,
std::unique_ptr<Expression> right) {
return std::make_unique<BinaryOp>(std::move(left), op, std::move(right));
}

std::unique_ptr<Port> make_port(
std::variant<std::unique_ptr<Identifier>, std::unique_ptr<Vector>> value,
Direction direction, PortType data_type) {
return std::make_unique<Port>(std::move(value), direction, data_type);
}

std::unique_ptr<Vector> make_vector(std::unique_ptr<Identifier> id,
std::unique_ptr<Expression> msb,
std::unique_ptr<Expression> lsb) {
return std::make_unique<Vector>(std::move(id), std::move(msb),
std::move(lsb));
}

} // namespace verilogAST
28 changes: 15 additions & 13 deletions tests/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@ TEST(BasicTests, TestString) {
}

TEST(BasicTests, TestIndex) {
vAST::Index index(make_id("x"), make_num("0"));
vAST::Index index(vAST::make_id("x"), vAST::make_num("0"));
EXPECT_EQ(index.toString(), "x[0]");
}

TEST(BasicTests, TestSlice) {
vAST::Identifier id("x");
vAST::Slice slice(make_id("x"), make_num("31"), make_num("0"));
vAST::Slice slice(vAST::make_id("x"), vAST::make_num("31"),
vAST::make_num("0"));
EXPECT_EQ(slice.toString(), "x[31:0]");
}

TEST(BasicTests, TestVector) {
vAST::Vector slice(make_id("x"), make_num("31"), make_num("0"));
vAST::Vector slice(vAST::make_id("x"), vAST::make_num("31"),
vAST::make_num("0"));
EXPECT_EQ(slice.toString(), "[31:0] x");
}

Expand All @@ -77,7 +79,7 @@ TEST(BasicTests, TestBinaryOp) {
for (auto it : ops) {
vAST::BinOp::BinOp op = it.first;
std::string op_str = it.second;
vAST::BinaryOp bin_op(make_id("x"), op, make_id("y"));
vAST::BinaryOp bin_op(vAST::make_id("x"), op, vAST::make_id("y"));
EXPECT_EQ(bin_op.toString(), "x " + op_str + " y");
}
}
Expand All @@ -98,7 +100,7 @@ TEST(BasicTests, TestUnaryOp) {
for (auto it : ops) {
vAST::UnOp::UnOp op = it.first;
std::string op_str = it.second;
vAST::UnaryOp un_op(make_id("x"), op);
vAST::UnaryOp un_op(vAST::make_id("x"), op);
EXPECT_EQ(un_op.toString(), op_str + " x");
}
}
Expand All @@ -107,37 +109,37 @@ TEST(BasicTests, TestTernaryOp) {
vAST::UnaryOp un_op(std::make_unique<vAST::Identifier>("x"),
vAST::UnOp::INVERT);
vAST::TernaryOp tern_op(
std::make_unique<vAST::UnaryOp>(make_id("x"), vAST::UnOp::INVERT),
make_num("1"), make_num("0"));
std::make_unique<vAST::UnaryOp>(vAST::make_id("x"), vAST::UnOp::INVERT),
vAST::make_num("1"), vAST::make_num("0"));
EXPECT_EQ(tern_op.toString(), "~ x ? 1 : 0");
}

TEST(BasicTests, TestNegEdge) {
vAST::NegEdge neg_edge(make_id("clk"));
vAST::NegEdge neg_edge(vAST::make_id("clk"));

EXPECT_EQ(neg_edge.toString(), "negedge clk");
}

TEST(BasicTests, TestPosEdge) {
vAST::PosEdge pos_edge(make_id("clk"));
vAST::PosEdge pos_edge(vAST::make_id("clk"));

EXPECT_EQ(pos_edge.toString(), "posedge clk");
}

TEST(BasicTests, TestPort) {
vAST::Port i_port(make_id("i"), vAST::INPUT, vAST::WIRE);
vAST::Port i_port(vAST::make_id("i"), vAST::INPUT, vAST::WIRE);

EXPECT_EQ(i_port.toString(), "input i");

vAST::Port o_port(make_id("o"), vAST::OUTPUT, vAST::WIRE);
vAST::Port o_port(vAST::make_id("o"), vAST::OUTPUT, vAST::WIRE);

EXPECT_EQ(o_port.toString(), "output o");

vAST::Port io_port(make_id("io"), vAST::INOUT, vAST::WIRE);
vAST::Port io_port(vAST::make_id("io"), vAST::INOUT, vAST::WIRE);

EXPECT_EQ(io_port.toString(), "inout io");

vAST::Port o_reg_port(make_id("o"), vAST::OUTPUT, vAST::REG);
vAST::Port o_reg_port(vAST::make_id("o"), vAST::OUTPUT, vAST::REG);

EXPECT_EQ(o_reg_port.toString(), "output reg o");
}
Expand Down
53 changes: 13 additions & 40 deletions tests/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,12 @@

namespace vAST = verilogAST;

std::unique_ptr<vAST::Identifier> make_id(std::string name) {
return std::make_unique<vAST::Identifier>(name);
}

std::unique_ptr<vAST::NumericLiteral> make_num(std::string val) {
return std::make_unique<vAST::NumericLiteral>(val);
}

std::unique_ptr<vAST::BinaryOp> make_binop(
std::unique_ptr<vAST::Expression> left, vAST::BinOp::BinOp op,
std::unique_ptr<vAST::Expression> right) {
return std::make_unique<vAST::BinaryOp>(std::move(left), op,
std::move(right));
}

std::unique_ptr<vAST::Port> make_port(
std::variant<std::unique_ptr<vAST::Identifier>,
std::unique_ptr<vAST::Vector>>
value,
vAST::Direction direction, vAST::PortType data_type) {
return std::make_unique<vAST::Port>(std::move(value), direction, data_type);
}

std::unique_ptr<vAST::Vector> make_vector(
std::unique_ptr<vAST::Identifier> id, std::unique_ptr<vAST::Expression> msb,
std::unique_ptr<vAST::Expression> lsb) {
return std::make_unique<vAST::Vector>(std::move(id), std::move(msb),
std::move(lsb));
}

vAST::Parameters make_simple_params() {
vAST::Parameters parameters;
parameters.push_back(std::make_pair(make_id("param0"), make_num("0")));
parameters.push_back(std::make_pair(make_id("param1"), make_num("1")));
parameters.push_back(
std::make_pair(vAST::make_id("param0"), vAST::make_num("0")));
parameters.push_back(
std::make_pair(vAST::make_id("param1"), vAST::make_num("1")));
return parameters;
}

Expand All @@ -47,20 +19,21 @@ make_simple_connections() {
std::unique_ptr<vAST::Index>,
std::unique_ptr<vAST::Slice>>>
connections;
connections["a"] = make_id("a");
connections["b"] = std::make_unique<vAST::Index>(make_id("b"), make_num("0"));
connections["c"] = std::make_unique<vAST::Slice>(make_id("c"), make_num("31"),
make_num("0"));
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"));

return connections;
}

std::vector<std::unique_ptr<vAST::AbstractPort>> make_simple_ports() {
std::vector<std::unique_ptr<vAST::AbstractPort>> ports;
ports.push_back(
std::make_unique<vAST::Port>(make_id("i"), vAST::INPUT, vAST::WIRE));
ports.push_back(
std::make_unique<vAST::Port>(make_id("o"), vAST::OUTPUT, vAST::WIRE));
ports.push_back(std::make_unique<vAST::Port>(vAST::make_id("i"), vAST::INPUT,
vAST::WIRE));
ports.push_back(std::make_unique<vAST::Port>(vAST::make_id("o"), vAST::OUTPUT,
vAST::WIRE));
return ports;
}

Expand Down
29 changes: 17 additions & 12 deletions tests/parameterized_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,36 @@ TEST(ParameterizedModuleTests, TestEq) {
std::string name = "coreir_eq";

std::vector<std::unique_ptr<vAST::AbstractPort>> ports;
ports.push_back(make_port(
make_vector(make_id("in0"),
make_binop(make_id("width"), vAST::BinOp::SUB, make_num("1")),
make_num("0")),
ports.push_back(vAST::make_port(
vAST::make_vector(vAST::make_id("in0"),
vAST::make_binop(vAST::make_id("width"),
vAST::BinOp::SUB, vAST::make_num("1")),
vAST::make_num("0")),
vAST::INPUT, vAST::WIRE));
ports.push_back(make_port(
make_vector(make_id("in1"),
make_binop(make_id("width"), vAST::BinOp::SUB, make_num("1")),
make_num("0")),
ports.push_back(vAST::make_port(
vAST::make_vector(vAST::make_id("in1"),
vAST::make_binop(vAST::make_id("width"),
vAST::BinOp::SUB, vAST::make_num("1")),
vAST::make_num("0")),
vAST::INPUT, vAST::WIRE));
ports.push_back(make_port(make_id("out"), vAST::OUTPUT, vAST::WIRE));
ports.push_back(
vAST::make_port(vAST::make_id("out"), vAST::OUTPUT, vAST::WIRE));

std::vector<std::variant<std::unique_ptr<vAST::StructuralStatement>,
std::unique_ptr<vAST::Declaration>>>
body;

std::unique_ptr<vAST::ContinuousAssign> eq_assign =
std::make_unique<vAST::ContinuousAssign>(
make_id("out"),
make_binop(make_id("in0"), vAST::BinOp::EQ, make_id("in1")));
vAST::make_id("out"),
vAST::make_binop(vAST::make_id("in0"), vAST::BinOp::EQ,
vAST::make_id("in1")));

body.push_back(std::move(eq_assign));

vAST::Parameters parameters;
parameters.push_back(std::make_pair(make_id("width"), make_num("1")));
parameters.push_back(
std::make_pair(vAST::make_id("width"), vAST::make_num("1")));
std::unique_ptr<vAST::Module> coreir_eq = std::make_unique<vAST::Module>(
name, std::move(ports), std::move(body), std::move(parameters));

Expand Down

0 comments on commit 6cab41d

Please sign in to comment.