Skip to content

Commit 809871c

Browse files
committed
Add support for packed arrays
1 parent 5a1e7d9 commit 809871c

File tree

7 files changed

+157
-2
lines changed

7 files changed

+157
-2
lines changed

CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ include_directories(
2121
${PROJECT_SOURCE_DIR}/include/
2222
)
2323

24-
set(LIB_SOURCES src/verilogAST.cpp src/transformer.cpp src/assign_inliner.cpp
25-
src/concat_coalescer.cpp src/zext_coalescer.cpp)
24+
set(LIB_SOURCES
25+
src/verilogAST.cpp
26+
src/transformer.cpp
27+
src/assign_inliner.cpp
28+
src/concat_coalescer.cpp
29+
src/zext_coalescer.cpp
30+
src/make_packed.cpp
31+
)
2632

2733
set(LIBRARY_NAME verilogAST)
2834
add_library(${LIBRARY_NAME} SHARED ${LIB_SOURCES})
@@ -74,6 +80,10 @@ if (VERILOGAST_BUILD_TESTS)
7480
add_executable(zext_coalescer tests/zext_coalescer.cpp)
7581
target_link_libraries(zext_coalescer gtest_main ${LIBRARY_NAME})
7682
add_test(NAME zext_coalescer_tests COMMAND zext_coalescer)
83+
84+
add_executable(make_packed tests/make_packed.cpp)
85+
target_link_libraries(make_packed gtest_main ${LIBRARY_NAME})
86+
add_test(NAME make_packed_tests COMMAND make_packed)
7787
endif()
7888

7989
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)

include/verilogAST.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,21 @@ class NDVector : public Vector {
507507
~NDVector(){};
508508
};
509509

510+
class PackedNDVector : public NDVector {
511+
public:
512+
PackedNDVector(
513+
std::unique_ptr<Identifier> id, std::unique_ptr<Expression> msb,
514+
std::unique_ptr<Expression> lsb,
515+
std::vector<
516+
std::pair<std::unique_ptr<Expression>, std::unique_ptr<Expression>>>
517+
outer_dims)
518+
: NDVector(std::move(id), std::move(msb), std::move(lsb),
519+
std::move(outer_dims)) {}
520+
521+
std::string toString() override;
522+
~PackedNDVector() = default;
523+
};
524+
510525
class Port : public AbstractPort {
511526
public:
512527
// Required

include/verilogAST/make_packed.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef VERILOGAST_MAKE_PACKED_H
2+
#define VERILOGAST_MAKE_PACKED_H
3+
4+
#include "verilogAST.hpp"
5+
#include "verilogAST/transformer.hpp"
6+
7+
namespace verilogAST {
8+
9+
class MakePacked : public Transformer {
10+
public:
11+
MakePacked() = default;
12+
13+
using Transformer::visit;
14+
15+
std::unique_ptr<Vector> visit(std::unique_ptr<Vector> vector) override;
16+
};
17+
18+
} // namespace verilogAST
19+
20+
#endif // VERILOGAST_MAKE_PACKED_H

src/make_packed.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "verilogAST/make_packed.hpp"
2+
#include <cassert>
3+
4+
namespace verilogAST {
5+
6+
std::unique_ptr<Vector> MakePacked::visit(std::unique_ptr<Vector> vector) {
7+
auto ptr = dynamic_cast<const NDVector*>(vector.get());
8+
if (not ptr) return vector;
9+
std::vector<
10+
std::pair<std::unique_ptr<Expression>, std::unique_ptr<Expression>>>
11+
outer_dims;
12+
for (const auto& dim : ptr->outer_dims) {
13+
outer_dims.push_back(
14+
std::make_pair(dim.first->clone(), dim.second->clone()));
15+
}
16+
return std::make_unique<PackedNDVector>(ptr->id->clone(), ptr->msb->clone(),
17+
ptr->lsb->clone(),
18+
std::move(outer_dims));
19+
}
20+
21+
} // namespace verilogAST

src/verilogAST.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ std::string NDVector::toString() {
161161
return s;
162162
}
163163

164+
std::string PackedNDVector::toString() {
165+
std::string s = "";
166+
for (auto &dim : outer_dims) {
167+
s += "[" + dim.first->toString() + ":" + dim.second->toString() + "]";
168+
}
169+
return (s + Vector::toString());
170+
}
171+
164172
std::string BinaryOp::toString() {
165173
std::string op_str;
166174
switch (op) {

tests/basic.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ TEST(BasicTests, TestNDVector) {
133133
EXPECT_EQ(slice.toString(), "[31:0] x [7:0][15:0]");
134134
}
135135

136+
TEST(BasicTests, TestPackedNDVector) {
137+
std::vector<std::pair<std::unique_ptr<vAST::Expression>,
138+
std::unique_ptr<vAST::Expression>>>
139+
outer_dims;
140+
outer_dims.push_back({vAST::make_num("7"), vAST::make_num("0")});
141+
outer_dims.push_back({vAST::make_num("15"), vAST::make_num("0")});
142+
vAST::PackedNDVector slice(vAST::make_id("x"), vAST::make_num("31"),
143+
vAST::make_num("0"), std::move(outer_dims));
144+
EXPECT_EQ(slice.toString(), "[7:0][15:0][31:0] x");
145+
}
146+
136147
TEST(BasicTests, TestBinaryOp) {
137148
std::vector<std::pair<vAST::BinOp::BinOp, std::string>> ops;
138149
ops.push_back(std::make_pair(vAST::BinOp::LSHIFT, "<<"));

tests/make_packed.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "verilogAST/make_packed.hpp"
2+
#include "common.cpp"
3+
#include "gtest/gtest.h"
4+
5+
namespace vAST = verilogAST;
6+
7+
namespace {
8+
9+
using BodyElement = std::variant<std::unique_ptr<vAST::StructuralStatement>,
10+
std::unique_ptr<vAST::Declaration>>;
11+
using Dim = std::pair<std::unique_ptr<vAST::Expression>,
12+
std::unique_ptr<vAST::Expression>>;
13+
14+
15+
Dim makeDim(std::unique_ptr<vAST::Expression> hi,
16+
std::unique_ptr<vAST::Expression> lo) {
17+
return std::make_pair(std::move(hi), std::move(lo));
18+
}
19+
20+
TEST(MakePackedTests, TestBasic) {
21+
std::vector<std::unique_ptr<vAST::AbstractPort>> ports;
22+
23+
auto make_dims = []() {
24+
std::vector<Dim> dims;
25+
dims.push_back(makeDim(vAST::make_num("7"), vAST::make_num("0")));
26+
dims.push_back(makeDim(vAST::make_num("15"), vAST::make_num("0")));
27+
return dims;
28+
};
29+
ports.push_back(
30+
std::make_unique<vAST::Port>(
31+
std::make_unique<vAST::NDVector>(
32+
vAST::make_id("I"),
33+
vAST::make_num("31"),
34+
vAST::make_num("0"),
35+
make_dims()),
36+
vAST::INPUT,
37+
vAST::WIRE));
38+
39+
std::vector<BodyElement> body;
40+
41+
std::unique_ptr<vAST::AbstractModule> module = std::make_unique<vAST::Module>(
42+
"test_module",
43+
std::move(ports),
44+
std::move(body));
45+
46+
auto pre =
47+
"module test_module (\n"
48+
" input [31:0] I [7:0][15:0]\n"
49+
");\n"
50+
"endmodule\n";
51+
EXPECT_EQ(module->toString(), pre);
52+
53+
// Run MakePacked transformer.
54+
vAST::MakePacked transformer {};
55+
module = transformer.visit(std::move(module));
56+
57+
auto post =
58+
"module test_module (\n"
59+
" input [7:0][15:0][31:0] I\n"
60+
");\n"
61+
"endmodule\n";
62+
EXPECT_EQ(module->toString(), post);
63+
}
64+
65+
} // namespace
66+
67+
int main(int argc, char **argv) {
68+
::testing::InitGoogleTest(&argc, argv);
69+
return RUN_ALL_TESTS();
70+
}

0 commit comments

Comments
 (0)