From 25dae60bb6e65c125ab0c80c651ac384e0f595d2 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Sat, 15 Jan 2022 23:09:19 +0100 Subject: [PATCH] Add support for explicit cast to packed struct and packed array Currently explicit cast is supported to atom2 and vector types. packed struct, packed array and enum are not supported. An explicit cast to packed type works the same for all packed types though. Add support for handling also packed structs, packed arrays and enums by make the code more generic and querying the packed base type from the ivl_type_t. To correctly handle enums a bit more work is necessary, which will be done in a follow up patch. Signed-off-by: Lars-Peter Clausen --- PExpr.h | 1 + elab_expr.cc | 19 ++++++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/PExpr.h b/PExpr.h index 6420e057d..93ddae44d 100644 --- a/PExpr.h +++ b/PExpr.h @@ -1007,6 +1007,7 @@ class PECastType : public PExpr { private: data_type_t* target_; + ivl_type_t target_type_; PExpr* base_; }; diff --git a/elab_expr.cc b/elab_expr.cc index ca71a89b6..ffcbae2e2 100644 --- a/elab_expr.cc +++ b/elab_expr.cc @@ -3351,25 +3351,25 @@ NetExpr* PECastSize::elaborate_expr(Design*des, NetScope*scope, unsigned PECastType::test_width(Design*des, NetScope*scope, width_mode_t&) { - ivl_type_t t = target_->elaborate_type(des, scope); + target_type_ = target_->elaborate_type(des, scope); width_mode_t tmp_mode = PExpr::SIZED; base_->test_width(des, scope, tmp_mode); - if (const netdarray_t*use_darray = dynamic_cast(t)) { + if (const netdarray_t*use_darray = dynamic_cast(target_type_)) { expr_type_ = use_darray->element_base_type(); expr_width_ = use_darray->element_width(); - } else if (const netstring_t*use_string = dynamic_cast(t)) { + } else if (const netstring_t*use_string = dynamic_cast(target_type_)) { expr_type_ = use_string->base_type(); expr_width_ = 8; } else { - expr_type_ = t->base_type(); - expr_width_ = t->packed_width(); + expr_type_ = target_type_->base_type(); + expr_width_ = target_type_->packed_width(); } min_width_ = expr_width_; - signed_flag_ = t->get_signed(); + signed_flag_ = target_type_->get_signed(); return expr_width_; } @@ -3427,11 +3427,8 @@ NetExpr* PECastType::elaborate_expr(Design*des, NetScope*scope, } NetExpr*tmp = 0; - if (dynamic_cast(target_)) { - tmp = cast_to_int2(sub, expr_width_); - } - if (const vector_type_t*vec = dynamic_cast(target_)) { - switch (vec->base_type) { + if (target_type_ && target_type_->packed()) { + switch (target_type_->base_type()) { case IVL_VT_BOOL: tmp = cast_to_int2(sub, expr_width_); break;