Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshanlon committed Sep 10, 2023
1 parent 6e46ce1 commit 2c19b0e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 23 deletions.
5 changes: 5 additions & 0 deletions tools/netlist/TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
To dos
======

- Bug report:

In ast::ElementSelectExpr.syntax does not correspond to selector source
range and includes entire variable reference + selectors.

- Bug report:

module m;
Expand Down
59 changes: 46 additions & 13 deletions tools/netlist/include/Netlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ struct VariableSelectorBase {

/// A variable selector representing an element selector.
struct VariableElementSelect : public VariableSelectorBase {
const ast::ElementSelectExpression &expr;
ConstantValue index;

VariableElementSelect(ConstantValue index) :
VariableSelectorBase(VariableSelectorKind::ElementSelect), index(std::move(index)) {}
VariableElementSelect(ast::ElementSelectExpression const& expr, ConstantValue index) :
VariableSelectorBase(VariableSelectorKind::ElementSelect), expr(expr),
index(std::move(index)) {}

static bool isKind(VariableSelectorKind otherKind) {
return otherKind == VariableSelectorKind::ElementSelect;
Expand All @@ -86,19 +88,24 @@ struct VariableElementSelect : public VariableSelectorBase {
return *intValue;
}

std::string toString() const override { return fmt::format("[{}]", index.toString()); }
std::string toString() const override {
if (indexIsConstant()) {
return fmt::format("[{}]", index.toString());
} else {
return fmt::format("[{}]", expr.syntax->toString());
}
}
};

/// A variable selector representing a range selector.
struct VariableRangeSelect : public VariableSelectorBase {
const ast::RangeSelectExpression &expr;
ConstantValue leftIndex, rightIndex;
ast::RangeSelectionKind selectionKind;

VariableRangeSelect(ConstantValue leftIndex, ConstantValue rightIndex,
ast::RangeSelectionKind selectionKind) :
VariableRangeSelect(ast::RangeSelectExpression const& expr, ConstantValue leftIndex,
ConstantValue rightIndex) :
VariableSelectorBase(VariableSelectorKind::RangeSelect),
leftIndex(std::move(leftIndex)), rightIndex(std::move(rightIndex)),
selectionKind(selectionKind) {}
expr(expr), leftIndex(std::move(leftIndex)), rightIndex(std::move(rightIndex)) {}

static bool isKind(VariableSelectorKind otherKind) {
return otherKind == VariableSelectorKind::RangeSelect;
Expand All @@ -125,7 +132,28 @@ struct VariableRangeSelect : public VariableSelectorBase {
}

std::string toString() const override {
return fmt::format("[{}:{}]", leftIndex.toString(), rightIndex.toString());
std::string left;
if (leftIndexIsConstant()) {
left = leftIndex.toString();
} else {
left = expr.left().syntax->toString();
}
std::string right;
if (rightIndexIsConstant()) {
right = rightIndex.toString();
} else {
right = expr.right().syntax->toString();
}
switch (expr.getSelectionKind()) {
case ast::RangeSelectionKind::Simple:
return fmt::format("[{}:{}]", left, right);
case ast::RangeSelectionKind::IndexedUp:
return fmt::format("[{}+:{}]", left, right);
case ast::RangeSelectionKind::IndexedDown:
return fmt::format("[{}-:{}]", left, right);
default:
SLANG_UNREACHABLE;
}
}
};

Expand Down Expand Up @@ -243,12 +271,17 @@ class NetlistVariableReference : public NetlistNode {
NetlistNode(NodeKind::VariableReference, symbol),
expression(expr), leftOperand(leftOperand) {}

void addElementSelect(const ConstantValue& index) {
selectors.emplace_back(std::make_unique<VariableElementSelect>(index));
void addElementSelect(ast::ElementSelectExpression const &expr, const ConstantValue& index) {
selectors.emplace_back(std::make_unique<VariableElementSelect>(expr, index));
//std::cout << "Add elem select "<< expr.syntax->toString() << "\n";
}
void addRangeSelect(const ConstantValue& leftIndex, const ConstantValue& rightIndex, ast::RangeSelectionKind selectionKind) {
selectors.emplace_back(std::make_unique<VariableRangeSelect>(leftIndex, rightIndex, selectionKind));

void addRangeSelect(ast::RangeSelectExpression const& expr,
const ConstantValue& leftIndex, const ConstantValue& rightIndex) {
selectors.emplace_back(std::make_unique<VariableRangeSelect>(expr, leftIndex, rightIndex));
//std::cout << "Add range select "<< expr.left().syntax->toString() << " : " << expr.right().syntax->toString() << "\n";
}

void addMemberAccess(std::string_view name) {
selectors.emplace_back(std::make_unique<VariableMemberAccess>(name));
}
Expand Down
13 changes: 7 additions & 6 deletions tools/netlist/include/NetlistVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ class VariableReferenceVisitor : public ast::ASTVisitor<VariableReferenceVisitor
varList.push_back(&node);
for (auto* selector : selectors) {
if (selector->kind == ast::ExpressionKind::ElementSelect) {
auto index = selector->as<ast::ElementSelectExpression>().selector().eval(evalCtx);
node.addElementSelect(index);
const auto& expr = selector->as<ast::ElementSelectExpression>();
auto index = expr.selector().eval(evalCtx);
node.addElementSelect(expr, index);
}
else if (selector->kind == ast::ExpressionKind::RangeSelect) {
auto& rangeSelectExpr = selector->as<ast::RangeSelectExpression>();
auto leftIndex = rangeSelectExpr.left().eval(evalCtx);
auto rightIndex = rangeSelectExpr.right().eval(evalCtx);
node.addRangeSelect(leftIndex, rightIndex, rangeSelectExpr.getSelectionKind());
const auto& expr = selector->as<ast::RangeSelectExpression>();
auto leftIndex = expr.left().eval(evalCtx);
auto rightIndex = expr.right().eval(evalCtx);
node.addRangeSelect(expr, leftIndex, rightIndex);
}
else if (selector->kind == ast::ExpressionKind::MemberAccess) {
node.addMemberAccess(selector->as<ast::MemberAccessExpression>().member.name);
Expand Down
4 changes: 2 additions & 2 deletions tools/netlist/include/SplitVariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class AnalyseVariableReference {
return handleScalarElementSelect(type, range);
}
else if (selectorsIt->get()->isRangeSelect()) {
switch (selectorsIt->get()->as<VariableRangeSelect>().selectionKind) {
switch (selectorsIt->get()->as<VariableRangeSelect>().expr.getSelectionKind()) {
case ast::RangeSelectionKind::Simple:
return handleScalarRangeSelect(type, range);
case ast::RangeSelectionKind::IndexedUp:
Expand All @@ -249,7 +249,7 @@ class AnalyseVariableReference {
return handleArrayElementSelect(type, range);
}
else if (selectorsIt->get()->isRangeSelect()) {
switch (selectorsIt->get()->as<VariableRangeSelect>().selectionKind) {
switch (selectorsIt->get()->as<VariableRangeSelect>().expr.getSelectionKind()) {
case ast::RangeSelectionKind::Simple:
return handleArrayRangeSelect(type, range);
case ast::RangeSelectionKind::IndexedUp:
Expand Down
11 changes: 9 additions & 2 deletions tools/netlist/tests/VariableSelectorsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ module m (input int a);
foo[3:1][2:1][1] = 0;
foo[7:4][6:5][5] = 0;
foo[a] = 0;
foo[a+:2][a] = 0;
foo[a+:2][a+:2][a] = 0;
end
endmodule
)");
Compilation compilation;
compilation.addSyntaxTree(tree);
NO_COMPILATION_ERRORS;
auto netlist = createNetlist(compilation);
//For (auto &node : netlist) {
// if (node->kind == NodeKind::VariableReference)
// {
// std::cout << "node " << node->as<NetlistVariableReference>().toString()<<"\n";
// }
//}
CHECK(getBitRange(netlist, "foo") == ConstantRange(0, 31));
CHECK(getBitRange(netlist, "foo[0]") == ConstantRange(0, 0));
CHECK(getBitRange(netlist, "foo[1]") == ConstantRange(1, 1));
Expand All @@ -56,8 +64,7 @@ endmodule
CHECK(getBitRange(netlist, "foo[7:4][6:5]") == ConstantRange(5, 6));
CHECK(getBitRange(netlist, "foo[3:1][2:1][1]") == ConstantRange(1, 1));
CHECK(getBitRange(netlist, "foo[7:4][6:5][5]") == ConstantRange(5, 5));
// Can't lookup foo[a] in the netlist.
//CHECK(getBitRange(netlist, "foo[a]") == ConstantRange(0, 31));
CHECK(getBitRange(netlist, "foo[a]") == ConstantRange(0, 31));
}

TEST_CASE("Packed 1D array element and range") {
Expand Down

0 comments on commit 2c19b0e

Please sign in to comment.