Skip to content

Commit

Permalink
Add support for nd-vector
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardt committed Jul 30, 2020
1 parent 311b34e commit de572f2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/verilogAST.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,22 @@ class Vector : public Node {
~Vector(){};
};

class NDVector : public Vector {
public:
std::unique_ptr<Identifier> id;
std::unique_ptr<Expression> msb;
std::unique_ptr<Expression> lsb;
std::vector<std::unique_ptr<Expression>> dims;

NDVector(std::unique_ptr<Identifier> id, std::unique_ptr<Expression> msb,
std::unique_ptr<Expression> lsb,
std::vector<std::unique_ptr<Expression>> dims)
: Vector(std::move(id), std::move(msb), std::move(lsb)),
dims(std::move(dims)){};
std::string toString() override;
~NDVector(){};
};

class Port : public AbstractPort {
public:
// Required
Expand Down
9 changes: 9 additions & 0 deletions src/verilogAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ std::string Vector::toString() {
return "[" + msb->toString() + ':' + lsb->toString() + "] " + id->toString();
}

std::string NDVector::toString() {
std::string base_str = Vector::toString();
base_str += " ";
for (auto &dim : dims) {
base_str += "[" + dim->toString() + "]";
}
return base_str;
}

std::string BinaryOp::toString() {
std::string op_str;
switch (op) {
Expand Down
9 changes: 9 additions & 0 deletions tests/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ TEST(BasicTests, TestVector) {
EXPECT_EQ(slice.toString(), "[31:0] x");
}

TEST(BasicTests, TestNDVector) {
std::vector<std::unique_ptr<vAST::Expression>> dims;
dims.push_back(vAST::make_num("8"));
dims.push_back(vAST::make_num("32"));
vAST::NDVector slice(vAST::make_id("x"), vAST::make_num("31"),
vAST::make_num("0"), std::move(dims));
EXPECT_EQ(slice.toString(), "[31:0] x [8][32]");
}

TEST(BasicTests, TestBinaryOp) {
std::vector<std::pair<vAST::BinOp::BinOp, std::string>> ops;
ops.push_back(std::make_pair(vAST::BinOp::LSHIFT, "<<"));
Expand Down

0 comments on commit de572f2

Please sign in to comment.