Skip to content

[ImportVerilog] Support case inside statements#10090

Open
sunhailong2001 wants to merge 1 commit intollvm:mainfrom
sunhailong2001:hailong/case-inside
Open

[ImportVerilog] Support case inside statements#10090
sunhailong2001 wants to merge 1 commit intollvm:mainfrom
sunhailong2001:hailong/case-inside

Conversation

@sunhailong2001
Copy link
Copy Markdown
Contributor

@sunhailong2001 sunhailong2001 commented Mar 31, 2026

Support translating case (...) inside statements. As part of this change, refactor existing slang::ast::InsideExpression handling by moving ValueRangeExpression translation into a dedicated visit.

@sunhailong2001
Copy link
Copy Markdown
Contributor Author

Hey, @fabianschuiki ! Thanks for reviewing this. 😃

Comment on lines +1628 to +1653
Value visit(const slang::ast::ValueRangeExpression &expr) {
assert(!context.lvalueStack.empty() && "inside pushes lvalue");
auto lhs = context.lvalueStack.back();
// Handle ranges.
auto lowBound = context.convertToSimpleBitVector(
context.convertRvalueExpression(expr.left()));
auto highBound = context.convertToSimpleBitVector(
context.convertRvalueExpression(expr.right()));
if (!lowBound || !highBound)
return {};
Value leftValue, rightValue;
// Determine if the expression on the left-hand side is inclusively
// within the range.
if (expr.left().type->isSigned() || expr.left().type->isSigned()) {
leftValue = moore::SgeOp::create(builder, loc, lhs, lowBound);
} else {
leftValue = moore::UgeOp::create(builder, loc, lhs, lowBound);
}
if (expr.right().type->isSigned() || expr.left().type->isSigned()) {
rightValue = moore::SleOp::create(builder, loc, lhs, highBound);
} else {
rightValue = moore::UleOp::create(builder, loc, lhs, highBound);
}
return moore::AndOp::create(builder, loc, leftValue, rightValue);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if using lvalueStack for this is too brittle. We have used the lvalue stack to specifically handle Slang's AST nodes that refer the LHS in assignments like a += b. Checking whether one expression is inside the range described by a ValueRangeExpression feels like it doesn't involve lvalues at all.

My suggestion: let's add a convertInsideValueRangeCheck(Value value, const slang::ast::ValueRangeExpression &range), and move the content of this visit(...) function in there. Then you don't need to use lvalueStack; instead, you can directly use the value passed to the function. The visit(InsideExpression) and visit(CaseStatement) functions can then call the new convertInsideValueRangeCheck directly to implement their inside checks 😃

What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, but it lacks elegance. Moreover, if convertInsideValueRangeCheck processes the entire range list or case item, for the non-ValueRange cases, an extra function invocation is required. 😞

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm do you think the code at the moment doesn't support certain inputs? Were you thinking of x inside {[a:b], 42, [c:d]}? If case items and inside expressions can have these lists of expressions, maybe we could move more things into convertInsideValueRangeCheck and rename it to something like convertInsideCheck, and pass the entire list of expressions to that function? That would allow us to share almost all code between case items and inside... What do you think?

Support translating  'case (...) inside' statements. And add the `convertInsideValueRangeCheck` function to handle the `slang::ast::ValueRangeExpression` dedicatedly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants