From bd4d73d9ae02b89b31ffa3b648fbac2dad1eb39f Mon Sep 17 00:00:00 2001 From: Martin Whitaker Date: Sat, 15 Dec 2018 11:11:35 +0000 Subject: [PATCH] Fix GitHub issue #219 and #220 - incorrect results from SV size cast. (cherry picked from commit 230f0bc13c1adc695a68c0345340ac32ae3c495d) --- elab_expr.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/elab_expr.cc b/elab_expr.cc index 699c4f5d5..3901b5723 100644 --- a/elab_expr.cc +++ b/elab_expr.cc @@ -2535,7 +2535,13 @@ NetExpr* PECastSize::elaborate_expr(Design*des, NetScope*scope, ivl_assert(*this, size_); ivl_assert(*this, base_); - NetExpr*sub = base_->elaborate_expr(des, scope, base_->expr_width(), flags); + // When changing size, a cast behaves exactly like an assignment, + // so the result size affects the final expression width. + unsigned cast_width = base_->expr_width(); + if (cast_width < expr_width_) + cast_width = expr_width_; + + NetExpr*sub = base_->elaborate_expr(des, scope, cast_width, flags); // Perform the cast. The extension method (zero/sign), if needed, // depends on the type of the base expression.