Skip to content

Commit 5c1fcb1

Browse files
committed
add support for range select increment
1 parent 4eaec19 commit 5c1fcb1

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

src/codegen/expr.cc

+33-13
Original file line numberDiff line numberDiff line change
@@ -338,21 +338,41 @@ uint64_t get_constant_value(const slang::ConstantValue &constant,
338338
}
339339

340340
void ExprCodeGenVisitor::handle(const slang::RangeSelectExpression &expr) {
341-
if (!expr.left().constant)
342-
throw NotSupportedException("Only constant range select supported",
343-
expr.left().syntax->sourceRange().start());
344-
345-
if (!expr.right().constant)
346-
throw NotSupportedException("Only constant range select supported",
347-
expr.right().syntax->sourceRange().start());
348-
349341
s << "(";
350342
expr.value().visit(*this);
351-
s << ").slice<";
352-
s << get_constant_value(*expr.left().constant, expr.left().sourceRange.start());
353-
s << ", ";
354-
s << get_constant_value(*expr.right().constant, expr.right().sourceRange.start());
355-
s << ">()";
343+
s << ")";
344+
switch (expr.selectionKind) {
345+
case slang::RangeSelectionKind::Simple: {
346+
if (!expr.left().constant)
347+
throw NotSupportedException("Only constant range select supported",
348+
expr.left().syntax->sourceRange().start());
349+
350+
if (!expr.right().constant)
351+
throw NotSupportedException("Only constant range select supported",
352+
expr.right().syntax->sourceRange().start());
353+
354+
s << ".slice<";
355+
s << get_constant_value(*expr.left().constant, expr.left().sourceRange.start());
356+
s << ", ";
357+
s << get_constant_value(*expr.right().constant, expr.right().sourceRange.start());
358+
s << ">()";
359+
break;
360+
}
361+
case slang::RangeSelectionKind::IndexedDown:
362+
case slang::RangeSelectionKind::IndexedUp: {
363+
if (!expr.right().constant)
364+
throw NotSupportedException("Only constant range select supported",
365+
expr.right().syntax->sourceRange().start());
366+
auto target_size =
367+
get_constant_value(*expr.right().constant, expr.right().sourceRange.start());
368+
const auto *increase =
369+
expr.selectionKind == slang::RangeSelectionKind::IndexedUp ? "true" : "false";
370+
s << ".slice<";
371+
s << target_size << ", " << increase << ">(";
372+
expr.left().visit(*this);
373+
s << ")";
374+
}
375+
}
356376
}
357377

358378
[[maybe_unused]] void ExprCodeGenVisitor::handle(const slang::ConcatenationExpression &sym) {

tests/test_codegen.cc

+5
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,10 @@ initial begin
764764
b = a? 2 : 3;
765765
c = b[1:0];
766766
$display("b = %0d c = %0d", b, c);
767+
a = 'hFFFF;
768+
b = 3;
769+
c = a[b+:8];
770+
$display("c = %0d", c);
767771
end
768772
769773
endmodule
@@ -780,6 +784,7 @@ endmodule
780784
builder.build(&compilation);
781785
std::string output = testing::internal::GetCapturedStdout();
782786
EXPECT_NE(output.find("b = 2 c = 2"), std::string::npos);
787+
EXPECT_NE(output.find("c = 255"), std::string::npos);
783788
}
784789

785790
TEST(code, case_) { // NOLINT

0 commit comments

Comments
 (0)