From 243a9665247cf16443ad5e19e04d3c2eb5b44669 Mon Sep 17 00:00:00 2001 From: Richard Herveille Date: Fri, 15 Mar 2024 18:55:05 +0100 Subject: [PATCH 1/5] Added cast to type support --- frontends/ast/ast.cc | 30 ++++++++++-- frontends/ast/simplify.cc | 76 +++++++++++++++++++++++++++++- frontends/verilog/verilog_parser.y | 6 +++ 3 files changed, 107 insertions(+), 5 deletions(-) diff --git a/frontends/ast/ast.cc b/frontends/ast/ast.cc index 996f6715d05..e5c0d4136d4 100644 --- a/frontends/ast/ast.cc +++ b/frontends/ast/ast.cc @@ -474,6 +474,10 @@ void AstNode::dumpVlog(FILE *f, std::string indent) const fprintf(f, ";\n"); break; + case AST_WIRETYPE: + fprintf(f, id2vl(str).c_str()); + break; + case AST_MEMORY: fprintf(f, "%s" "memory", indent.c_str()); if (is_signed) @@ -690,12 +694,30 @@ void AstNode::dumpVlog(FILE *f, std::string indent) const break; case AST_CAST_SIZE: - children[0]->dumpVlog(f, ""); - fprintf(f, "'("); - children[1]->dumpVlog(f, ""); - fprintf(f, ")"); + switch (children[0]->type) + { + case AST_WIRE: + if (children[0]->children.size() > 0) { + children[0]->children[0]->dumpVlog(f, ""); + fprintf(f, "'("); + children[1]->dumpVlog(f, ""); + fprintf(f, ")"); + } else { + fprintf(f, "%d'(", children[0]->range_left - children[0]->range_right +1); + children[1]->dumpVlog(f, ""); + fprintf(f, ")"); + } + break; + + default: //AST_IDENTIFIER + children[0]->dumpVlog(f, ""); + fprintf(f, "'("); + children[1]->dumpVlog(f, ""); + fprintf(f, ")"); + } break; + if (0) { case AST_BIT_AND: txt = "&"; } if (0) { case AST_BIT_OR: txt = "|"; } if (0) { case AST_BIT_XOR: txt = "^"; } diff --git a/frontends/ast/simplify.cc b/frontends/ast/simplify.cc index 43a4e03a29d..aa2bab1744e 100644 --- a/frontends/ast/simplify.cc +++ b/frontends/ast/simplify.cc @@ -1487,6 +1487,7 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin width_hint = max(width_hint, children[1]->range_left - children[1]->range_right + 1); } break; + case AST_ENUM_ITEM: while (!children[0]->basic_prep && children[0]->simplify(false, stage, -1, false)) did_something = true; @@ -1500,11 +1501,84 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin } break; + case AST_CAST_SIZE: { + int width = 1; + AstNode *node; + AstNode *child = children[0]; + + if (child->type == AST_WIRE) { + if (child->children.size() == 0) { + //Base type (e.g. int) + + //Construct AST_CONSTANT node + width = child->range_left - child->range_right +1; + node = mkconst_int(width, child->is_signed); + } else { + //User defined type + log_assert(child->children[0]->type == AST_WIRETYPE); + + auto type_name = child->children[0]->str; + if (!current_scope.count(type_name)) { + input_error("Unknown identifier `%s' used as type name\n", type_name.c_str()); + } + AstNode *resolved_type_node = current_scope.at(type_name); + if (resolved_type_node->type != AST_TYPEDEF) + input_error("`%s' does not name a type\n", type_name.c_str()); + log_assert(resolved_type_node->children.size() == 1); + AstNode *template_node = resolved_type_node->children[0]; + + // Ensure typedef itself is fully simplified + while (template_node->simplify(const_fold, stage, width_hint, sign_hint)) {}; + + switch (template_node->type) + { + case AST_WIRE: { + if (template_node->children.size() > 0 && template_node->children[0]->type == AST_RANGE) + width = range_width(this, template_node->children[0]); + + //delete child's children + child->delete_children(); + + //create AST_CONSTANT node + node = mkconst_int(width, true); + + break; + } + + case AST_STRUCT: { + //delete child's children + child->delete_children(); + + //create AST_CONSTANT node + width = size_packed_struct(template_node, 0); + node = mkconst_int(width, false); + + break; + } + + default: + log_error("Don't know how to translate static cast of type %s\n", type2str(template_node->type).c_str()); + } + } + + //Remove child node + delete child; + children.erase(children.begin()); + + //insert AST_CONSTANT + children.insert(children.begin(),node); + } + + detect_width_simple = true; + children_are_self_determined = true; + + break; + } + case AST_TO_BITS: case AST_TO_SIGNED: case AST_TO_UNSIGNED: case AST_SELFSZ: - case AST_CAST_SIZE: case AST_CONCAT: case AST_REPLICATE: case AST_REDUCE_AND: diff --git a/frontends/verilog/verilog_parser.y b/frontends/verilog/verilog_parser.y index 15a04eb2885..31d69ad0ca0 100644 --- a/frontends/verilog/verilog_parser.y +++ b/frontends/verilog/verilog_parser.y @@ -3506,6 +3506,12 @@ basic_expr: $$ = new AstNode(AST_CAST_SIZE, $1, $4); SET_AST_NODE_LOC($$, @1, @4); } | + typedef_base_type OP_CAST '(' expr ')' { + if (!sv_mode) + frontend_verilog_yyerror("Static cast is only supported in SystemVerilog mode."); + $$ = new AstNode(AST_CAST_SIZE, $1, $4); + SET_AST_NODE_LOC($$, @1, @4); + } | '(' expr '=' expr ')' { ensureAsgnExprAllowed(); AstNode *node = new AstNode(AST_ASSIGN_EQ, $2, $4); From 9d54d9b07bb93fa22325a1806e9b3d4a4793b36c Mon Sep 17 00:00:00 2001 From: Richard Herveille Date: Sat, 16 Mar 2024 21:23:44 +0100 Subject: [PATCH 2/5] Added cast-to-type tests --- tests/verilog/size_cast.sv | 391 +++++++++++++++++++++++++++++++++++++ 1 file changed, 391 insertions(+) diff --git a/tests/verilog/size_cast.sv b/tests/verilog/size_cast.sv index 1636f8d701e..4fb23a0a118 100644 --- a/tests/verilog/size_cast.sv +++ b/tests/verilog/size_cast.sv @@ -15,6 +15,12 @@ module top; logic signed [1:0] L2sb10 = 2; logic signed [1:0] L2sb11 = 3; + typedef logic u1bit_t; + typedef logic signed s1bit_t; + typedef logic [1:0] u2bit_t; + typedef logic signed [1:0] s2bit_t; + typedef logic [2:0] u3bit_t; + logic y = 1; always @* begin @@ -32,6 +38,20 @@ module top; assert (1'(L2sb10) == 1'b0); assert (1'(L2sb11) == 1'b1); + assert (u1bit_t'(L1b0 ) == 1'b0); + assert (u1bit_t'(L1b1 ) == 1'b1); + assert (s1bit_t'(L1sb0 ) == 1'b0); + assert (s1bit_t'(L1sb1 ) == 1'b1); + assert (u1bit_t'(L2b00 ) == 1'b0); + assert (u1bit_t'(L2b01 ) == 1'b1); + assert (u1bit_t'(L2b10 ) == 1'b0); + assert (u1bit_t'(L2b11 ) == 1'b1); + assert (s1bit_t'(L2sb00) == 1'b0); + assert (s1bit_t'(L2sb01) == 1'b1); + assert (s1bit_t'(L2sb10) == 1'b0); + assert (s1bit_t'(L2sb11) == 1'b1); + + assert (2'(L1b0 ) == 2'b00); assert (2'(L1b1 ) == 2'b01); assert (2'(L1sb0 ) == 2'b00); @@ -45,6 +65,20 @@ module top; assert (2'(L2sb10) == 2'b10); assert (2'(L2sb11) == 2'b11); + assert (u2bit_t'(L1b0 ) == 2'b00); + assert (u2bit_t'(L1b1 ) == 2'b01); + assert (s2bit_t'(L1sb0 ) == 2'b00); + assert (s2bit_t'(L1sb1 ) == 2'b11); + assert (u2bit_t'(L2b00 ) == 2'b00); + assert (u2bit_t'(L2b01 ) == 2'b01); + assert (u2bit_t'(L2b10 ) == 2'b10); + assert (u2bit_t'(L2b11 ) == 2'b11); + assert (s2bit_t'(L2sb00) == 2'b00); + assert (s2bit_t'(L2sb01) == 2'b01); + assert (s2bit_t'(L2sb10) == 2'b10); + assert (s2bit_t'(L2sb11) == 2'b11); + + assert (3'(L1b0 ) == 3'b000); assert (3'(L1b1 ) == 3'b001); assert (3'(L1sb0 ) == 3'b000); @@ -58,6 +92,20 @@ module top; assert (3'(L2sb10) == 3'b110); assert (3'(L2sb11) == 3'b111); + assert (u3bit_t'(L1b0 ) == 3'b000); + assert (u3bit_t'(L1b1 ) == 3'b001); + assert (u3bit_t'(L1sb0 ) == 3'b000); + assert (u3bit_t'(L1sb1 ) == 3'b111); + assert (u3bit_t'(L2b00 ) == 3'b000); + assert (u3bit_t'(L2b01 ) == 3'b001); + assert (u3bit_t'(L2b10 ) == 3'b010); + assert (u3bit_t'(L2b11 ) == 3'b011); + assert (u3bit_t'(L2sb00) == 3'b000); + assert (u3bit_t'(L2sb01) == 3'b001); + assert (u3bit_t'(L2sb10) == 3'b110); + assert (u3bit_t'(L2sb11) == 3'b111); + + assert (3'(L1b0 | '1) == 3'b111); assert (3'(L1b1 | '1) == 3'b111); assert (3'(L1sb0 | '1) == 3'b111); @@ -71,6 +119,46 @@ module top; assert (3'(L2sb10 | '1) == 3'b111); assert (3'(L2sb11 | '1) == 3'b111); + assert (u3bit_t'(L1b0 | '1) == 3'b111); + assert (u3bit_t'(L1b1 | '1) == 3'b111); + assert (u3bit_t'(L1sb0 | '1) == 3'b111); + assert (u3bit_t'(L1sb1 | '1) == 3'b111); + assert (u3bit_t'(L2b00 | '1) == 3'b111); + assert (u3bit_t'(L2b01 | '1) == 3'b111); + assert (u3bit_t'(L2b10 | '1) == 3'b111); + assert (u3bit_t'(L2b11 | '1) == 3'b111); + assert (u3bit_t'(L2sb00 | '1) == 3'b111); + assert (u3bit_t'(L2sb01 | '1) == 3'b111); + assert (u3bit_t'(L2sb10 | '1) == 3'b111); + assert (u3bit_t'(L2sb11 | '1) == 3'b111); + + assert (byte'(L1b0 | '1) == 8'hff); + assert (byte'(L1b1 | '1) == 8'hff); + assert (byte'(L1sb0 | '1) == 8'hff); + assert (byte'(L1sb1 | '1) == 8'hff); + assert (byte'(L2b00 | '1) == 8'hff); + assert (byte'(L2b01 | '1) == 8'hff); + assert (byte'(L2b10 | '1) == 8'hff); + assert (byte'(L2b11 | '1) == 8'hff); + assert (byte'(L2sb00 | '1) == 8'hff); + assert (byte'(L2sb01 | '1) == 8'hff); + assert (byte'(L2sb10 | '1) == 8'hff); + assert (byte'(L2sb11 | '1) == 8'hff); + + assert (int'(L1b0 | '1) == 32'hffff_ffff); + assert (int'(L1b1 | '1) == 32'hffff_ffff); + assert (int'(L1sb0 | '1) == 32'hffff_ffff); + assert (int'(L1sb1 | '1) == 32'hffff_ffff); + assert (int'(L2b00 | '1) == 32'hffff_ffff); + assert (int'(L2b01 | '1) == 32'hffff_ffff); + assert (int'(L2b10 | '1) == 32'hffff_ffff); + assert (int'(L2b11 | '1) == 32'hffff_ffff); + assert (int'(L2sb00 | '1) == 32'hffff_ffff); + assert (int'(L2sb01 | '1) == 32'hffff_ffff); + assert (int'(L2sb10 | '1) == 32'hffff_ffff); + assert (int'(L2sb11 | '1) == 32'hffff_ffff); + + assert (3'(L1b0 | '0) == 3'b000); assert (3'(L1b1 | '0) == 3'b001); assert (3'(L1sb0 | '0) == 3'b000); @@ -84,6 +172,46 @@ module top; assert (3'(L2sb10 | '0) == 3'b010); assert (3'(L2sb11 | '0) == 3'b011); + assert (u3bit_t'(L1b0 | '0) == 3'b000); + assert (u3bit_t'(L1b1 | '0) == 3'b001); + assert (u3bit_t'(L1sb0 | '0) == 3'b000); + assert (u3bit_t'(L1sb1 | '0) == 3'b001); + assert (u3bit_t'(L2b00 | '0) == 3'b000); + assert (u3bit_t'(L2b01 | '0) == 3'b001); + assert (u3bit_t'(L2b10 | '0) == 3'b010); + assert (u3bit_t'(L2b11 | '0) == 3'b011); + assert (u3bit_t'(L2sb00 | '0) == 3'b000); + assert (u3bit_t'(L2sb01 | '0) == 3'b001); + assert (u3bit_t'(L2sb10 | '0) == 3'b010); + assert (u3bit_t'(L2sb11 | '0) == 3'b011); + + assert (byte'(L1b0 | '0) == 8'h00); + assert (byte'(L1b1 | '0) == 8'h01); + assert (byte'(L1sb0 | '0) == 8'h00); + assert (byte'(L1sb1 | '0) == 8'h01); + assert (byte'(L2b00 | '0) == 8'h00); + assert (byte'(L2b01 | '0) == 8'h01); + assert (byte'(L2b10 | '0) == 8'h02); + assert (byte'(L2b11 | '0) == 8'h03); + assert (byte'(L2sb00 | '0) == 8'h00); + assert (byte'(L2sb01 | '0) == 8'h01); + assert (byte'(L2sb10 | '0) == 8'h02); + assert (byte'(L2sb11 | '0) == 8'h03); + + assert (int'(L1b0 | '0) == 32'h0000_0000); + assert (int'(L1b1 | '0) == 32'h0000_0001); + assert (int'(L1sb0 | '0) == 32'h0000_0000); + assert (int'(L1sb1 | '0) == 32'h0000_0001); + assert (int'(L2b00 | '0) == 32'h0000_0000); + assert (int'(L2b01 | '0) == 32'h0000_0001); + assert (int'(L2b10 | '0) == 32'h0000_0002); + assert (int'(L2b11 | '0) == 32'h0000_0003); + assert (int'(L2sb00 | '0) == 32'h0000_0000); + assert (int'(L2sb01 | '0) == 32'h0000_0001); + assert (int'(L2sb10 | '0) == 32'h0000_0002); + assert (int'(L2sb11 | '0) == 32'h0000_0003); + + assert (3'(y ? L1b0 : '1) == 3'b000); assert (3'(y ? L1b1 : '1) == 3'b001); assert (3'(y ? L1sb0 : '1) == 3'b000); @@ -97,6 +225,46 @@ module top; assert (3'(y ? L2sb10 : '1) == 3'b010); assert (3'(y ? L2sb11 : '1) == 3'b011); + assert (u3bit_t'(y ? L1b0 : '1) == 3'b000); + assert (u3bit_t'(y ? L1b1 : '1) == 3'b001); + assert (u3bit_t'(y ? L1sb0 : '1) == 3'b000); + assert (u3bit_t'(y ? L1sb1 : '1) == 3'b001); + assert (u3bit_t'(y ? L2b00 : '1) == 3'b000); + assert (u3bit_t'(y ? L2b01 : '1) == 3'b001); + assert (u3bit_t'(y ? L2b10 : '1) == 3'b010); + assert (u3bit_t'(y ? L2b11 : '1) == 3'b011); + assert (u3bit_t'(y ? L2sb00 : '1) == 3'b000); + assert (u3bit_t'(y ? L2sb01 : '1) == 3'b001); + assert (u3bit_t'(y ? L2sb10 : '1) == 3'b010); + assert (u3bit_t'(y ? L2sb11 : '1) == 3'b011); + + assert (byte'(y ? L1b0 : '1) == 8'h00); + assert (byte'(y ? L1b1 : '1) == 8'h01); + assert (byte'(y ? L1sb0 : '1) == 8'h00); + assert (byte'(y ? L1sb1 : '1) == 8'h01); + assert (byte'(y ? L2b00 : '1) == 8'h00); + assert (byte'(y ? L2b01 : '1) == 8'h01); + assert (byte'(y ? L2b10 : '1) == 8'h02); + assert (byte'(y ? L2b11 : '1) == 8'h03); + assert (byte'(y ? L2sb00 : '1) == 8'h00); + assert (byte'(y ? L2sb01 : '1) == 8'h01); + assert (byte'(y ? L2sb10 : '1) == 8'h02); + assert (byte'(y ? L2sb11 : '1) == 8'h03); + + assert (int'(y ? L1b0 : '1) == 32'h0000_0000); + assert (int'(y ? L1b1 : '1) == 32'h0000_0001); + assert (int'(y ? L1sb0 : '1) == 32'h0000_0000); + assert (int'(y ? L1sb1 : '1) == 32'h0000_0001); + assert (int'(y ? L2b00 : '1) == 32'h0000_0000); + assert (int'(y ? L2b01 : '1) == 32'h0000_0001); + assert (int'(y ? L2b10 : '1) == 32'h0000_0002); + assert (int'(y ? L2b11 : '1) == 32'h0000_0003); + assert (int'(y ? L2sb00 : '1) == 32'h0000_0000); + assert (int'(y ? L2sb01 : '1) == 32'h0000_0001); + assert (int'(y ? L2sb10 : '1) == 32'h0000_0002); + assert (int'(y ? L2sb11 : '1) == 32'h0000_0003); + + assert (3'(y ? L1b0 : '0) == 3'b000); assert (3'(y ? L1b1 : '0) == 3'b001); assert (3'(y ? L1sb0 : '0) == 3'b000); @@ -110,6 +278,46 @@ module top; assert (3'(y ? L2sb10 : '0) == 3'b010); assert (3'(y ? L2sb11 : '0) == 3'b011); + assert (u3bit_t'(y ? L1b0 : '0) == 3'b000); + assert (u3bit_t'(y ? L1b1 : '0) == 3'b001); + assert (u3bit_t'(y ? L1sb0 : '0) == 3'b000); + assert (u3bit_t'(y ? L1sb1 : '0) == 3'b001); + assert (u3bit_t'(y ? L2b00 : '0) == 3'b000); + assert (u3bit_t'(y ? L2b01 : '0) == 3'b001); + assert (u3bit_t'(y ? L2b10 : '0) == 3'b010); + assert (u3bit_t'(y ? L2b11 : '0) == 3'b011); + assert (u3bit_t'(y ? L2sb00 : '0) == 3'b000); + assert (u3bit_t'(y ? L2sb01 : '0) == 3'b001); + assert (u3bit_t'(y ? L2sb10 : '0) == 3'b010); + assert (u3bit_t'(y ? L2sb11 : '0) == 3'b011); + + assert (byte'(y ? L1b0 : '0) == 8'h00); + assert (byte'(y ? L1b1 : '0) == 8'h01); + assert (byte'(y ? L1sb0 : '0) == 8'h00); + assert (byte'(y ? L1sb1 : '0) == 8'h01); + assert (byte'(y ? L2b00 : '0) == 8'h00); + assert (byte'(y ? L2b01 : '0) == 8'h01); + assert (byte'(y ? L2b10 : '0) == 8'h02); + assert (byte'(y ? L2b11 : '0) == 8'h03); + assert (byte'(y ? L2sb00 : '0) == 8'h00); + assert (byte'(y ? L2sb01 : '0) == 8'h01); + assert (byte'(y ? L2sb10 : '0) == 8'h02); + assert (byte'(y ? L2sb11 : '0) == 8'h03); + + assert (int'(y ? L1b0 : '0) == 32'h0000_0000); + assert (int'(y ? L1b1 : '0) == 32'h0000_0001); + assert (int'(y ? L1sb0 : '0) == 32'h0000_0000); + assert (int'(y ? L1sb1 : '0) == 32'h0000_0001); + assert (int'(y ? L2b00 : '0) == 32'h0000_0000); + assert (int'(y ? L2b01 : '0) == 32'h0000_0001); + assert (int'(y ? L2b10 : '0) == 32'h0000_0002); + assert (int'(y ? L2b11 : '0) == 32'h0000_0003); + assert (int'(y ? L2sb00 : '0) == 32'h0000_0000); + assert (int'(y ? L2sb01 : '0) == 32'h0000_0001); + assert (int'(y ? L2sb10 : '0) == 32'h0000_0002); + assert (int'(y ? L2sb11 : '0) == 32'h0000_0003); + + assert (3'(y ? L1b0 : 1'sb0) == 3'b000); assert (3'(y ? L1b1 : 1'sb0) == 3'b001); assert (3'(y ? L1sb0 : 1'sb0) == 3'b000); @@ -123,6 +331,98 @@ module top; assert (3'(y ? L2sb10 : 1'sb0) == 3'b110); assert (3'(y ? L2sb11 : 1'sb0) == 3'b111); + assert (u3bit_t'(y ? L1b0 : 1'sb0) == 3'b000); + assert (u3bit_t'(y ? L1b1 : 1'sb0) == 3'b001); + assert (u3bit_t'(y ? L1sb0 : 1'sb0) == 3'b000); + assert (u3bit_t'(y ? L1sb1 : 1'sb0) == 3'b111); + assert (u3bit_t'(y ? L2b00 : 1'sb0) == 3'b000); + assert (u3bit_t'(y ? L2b01 : 1'sb0) == 3'b001); + assert (u3bit_t'(y ? L2b10 : 1'sb0) == 3'b010); + assert (u3bit_t'(y ? L2b11 : 1'sb0) == 3'b011); + assert (u3bit_t'(y ? L2sb00 : 1'sb0) == 3'b000); + assert (u3bit_t'(y ? L2sb01 : 1'sb0) == 3'b001); + assert (u3bit_t'(y ? L2sb10 : 1'sb0) == 3'b110); + assert (u3bit_t'(y ? L2sb11 : 1'sb0) == 3'b111); + + assert (byte'(y ? L1b0 : 1'sb0) == 8'h00); + assert (byte'(y ? L1b1 : 1'sb0) == 8'h01); + assert (byte'(y ? L1sb0 : 1'sb0) == 8'h00); + assert (byte'(y ? L1sb1 : 1'sb0) == 8'hff); + assert (byte'(y ? L2b00 : 1'sb0) == 8'h00); + assert (byte'(y ? L2b01 : 1'sb0) == 8'h01); + assert (byte'(y ? L2b10 : 1'sb0) == 8'h02); + assert (byte'(y ? L2b11 : 1'sb0) == 8'h03); + assert (byte'(y ? L2sb00 : 1'sb0) == 8'h00); + assert (byte'(y ? L2sb01 : 1'sb0) == 8'h01); + assert (byte'(y ? L2sb10 : 1'sb0) == 8'hfe); + assert (byte'(y ? L2sb11 : 1'sb0) == 8'hff); + + assert (int'(y ? L1b0 : 1'sb0) == 32'h0000_0000); + assert (int'(y ? L1b1 : 1'sb0) == 32'h0000_0001); + assert (int'(y ? L1sb0 : 1'sb0) == 32'h0000_0000); + assert (int'(y ? L1sb1 : 1'sb0) == 32'hffff_ffff); + assert (int'(y ? L2b00 : 1'sb0) == 32'h0000_0000); + assert (int'(y ? L2b01 : 1'sb0) == 32'h0000_0001); + assert (int'(y ? L2b10 : 1'sb0) == 32'h0000_0002); + assert (int'(y ? L2b11 : 1'sb0) == 32'h0000_0003); + assert (int'(y ? L2sb00 : 1'sb0) == 32'h0000_0000); + assert (int'(y ? L2sb01 : 1'sb0) == 32'h0000_0001); + assert (int'(y ? L2sb10 : 1'sb0) == 32'hffff_fffe); + assert (int'(y ? L2sb11 : 1'sb0) == 32'hffff_ffff); + + assert (3'(y ? L1b0 : s1bit_t'(0)) == 3'b000); + assert (3'(y ? L1b1 : s1bit_t'(0)) == 3'b001); + assert (3'(y ? L1sb0 : s1bit_t'(0)) == 3'b000); + assert (3'(y ? L1sb1 : s1bit_t'(0)) == 3'b111); + assert (3'(y ? L2b00 : s1bit_t'(0)) == 3'b000); + assert (3'(y ? L2b01 : s1bit_t'(0)) == 3'b001); + assert (3'(y ? L2b10 : s1bit_t'(0)) == 3'b010); + assert (3'(y ? L2b11 : s1bit_t'(0)) == 3'b011); + assert (3'(y ? L2sb00 : s1bit_t'(0)) == 3'b000); + assert (3'(y ? L2sb01 : s1bit_t'(0)) == 3'b001); + assert (3'(y ? L2sb10 : s1bit_t'(0)) == 3'b110); + assert (3'(y ? L2sb11 : s1bit_t'(0)) == 3'b111); + + assert (u3bit_t'(y ? L1b0 : s1bit_t'(0)) == 3'b000); + assert (u3bit_t'(y ? L1b1 : s1bit_t'(0)) == 3'b001); + assert (u3bit_t'(y ? L1sb0 : s1bit_t'(0)) == 3'b000); + assert (u3bit_t'(y ? L1sb1 : s1bit_t'(0)) == 3'b111); + assert (u3bit_t'(y ? L2b00 : s1bit_t'(0)) == 3'b000); + assert (u3bit_t'(y ? L2b01 : s1bit_t'(0)) == 3'b001); + assert (u3bit_t'(y ? L2b10 : s1bit_t'(0)) == 3'b010); + assert (u3bit_t'(y ? L2b11 : s1bit_t'(0)) == 3'b011); + assert (u3bit_t'(y ? L2sb00 : s1bit_t'(0)) == 3'b000); + assert (u3bit_t'(y ? L2sb01 : s1bit_t'(0)) == 3'b001); + assert (u3bit_t'(y ? L2sb10 : s1bit_t'(0)) == 3'b110); + assert (u3bit_t'(y ? L2sb11 : s1bit_t'(0)) == 3'b111); + + assert (byte'(y ? L1b0 : s1bit_t'(0)) == 8'h00); + assert (byte'(y ? L1b1 : s1bit_t'(0)) == 8'h01); + assert (byte'(y ? L1sb0 : s1bit_t'(0)) == 8'h00); + assert (byte'(y ? L1sb1 : s1bit_t'(0)) == 8'hff); + assert (byte'(y ? L2b00 : s1bit_t'(0)) == 8'h00); + assert (byte'(y ? L2b01 : s1bit_t'(0)) == 8'h01); + assert (byte'(y ? L2b10 : s1bit_t'(0)) == 8'h02); + assert (byte'(y ? L2b11 : s1bit_t'(0)) == 8'h03); + assert (byte'(y ? L2sb00 : s1bit_t'(0)) == 8'h00); + assert (byte'(y ? L2sb01 : s1bit_t'(0)) == 8'h01); + assert (byte'(y ? L2sb10 : s1bit_t'(0)) == 8'hfe); + assert (byte'(y ? L2sb11 : s1bit_t'(0)) == 8'hff); + + assert (int'(y ? L1b0 : s1bit_t'(0)) == 32'h0000_0000); + assert (int'(y ? L1b1 : s1bit_t'(0)) == 32'h0000_0001); + assert (int'(y ? L1sb0 : s1bit_t'(0)) == 32'h0000_0000); + assert (int'(y ? L1sb1 : s1bit_t'(0)) == 32'hffff_ffff); + assert (int'(y ? L2b00 : s1bit_t'(0)) == 32'h0000_0000); + assert (int'(y ? L2b01 : s1bit_t'(0)) == 32'h0000_0001); + assert (int'(y ? L2b10 : s1bit_t'(0)) == 32'h0000_0002); + assert (int'(y ? L2b11 : s1bit_t'(0)) == 32'h0000_0003); + assert (int'(y ? L2sb00 : s1bit_t'(0)) == 32'h0000_0000); + assert (int'(y ? L2sb01 : s1bit_t'(0)) == 32'h0000_0001); + assert (int'(y ? L2sb10 : s1bit_t'(0)) == 32'hffff_fffe); + assert (int'(y ? L2sb11 : s1bit_t'(0)) == 32'hffff_ffff); + + assert (3'(y ? L1b0 : 1'sb1) == 3'b000); assert (3'(y ? L1b1 : 1'sb1) == 3'b001); assert (3'(y ? L1sb0 : 1'sb1) == 3'b000); @@ -136,5 +436,96 @@ module top; assert (3'(y ? L2sb10 : 1'sb1) == 3'b110); assert (3'(y ? L2sb11 : 1'sb1) == 3'b111); + assert (u3bit_t'(y ? L1b0 : 1'sb1) == 3'b000); + assert (u3bit_t'(y ? L1b1 : 1'sb1) == 3'b001); + assert (u3bit_t'(y ? L1sb0 : 1'sb1) == 3'b000); + assert (u3bit_t'(y ? L1sb1 : 1'sb1) == 3'b111); + assert (u3bit_t'(y ? L2b00 : 1'sb1) == 3'b000); + assert (u3bit_t'(y ? L2b01 : 1'sb1) == 3'b001); + assert (u3bit_t'(y ? L2b10 : 1'sb1) == 3'b010); + assert (u3bit_t'(y ? L2b11 : 1'sb1) == 3'b011); + assert (u3bit_t'(y ? L2sb00 : 1'sb1) == 3'b000); + assert (u3bit_t'(y ? L2sb01 : 1'sb1) == 3'b001); + assert (u3bit_t'(y ? L2sb10 : 1'sb1) == 3'b110); + assert (u3bit_t'(y ? L2sb11 : 1'sb1) == 3'b111); + + assert (byte'(y ? L1b0 : 1'sb1) == 8'h00); + assert (byte'(y ? L1b1 : 1'sb1) == 8'h01); + assert (byte'(y ? L1sb0 : 1'sb1) == 8'h00); + assert (byte'(y ? L1sb1 : 1'sb1) == 8'hff); + assert (byte'(y ? L2b00 : 1'sb1) == 8'h00); + assert (byte'(y ? L2b01 : 1'sb1) == 8'h01); + assert (byte'(y ? L2b10 : 1'sb1) == 8'h02); + assert (byte'(y ? L2b11 : 1'sb1) == 8'h03); + assert (byte'(y ? L2sb00 : 1'sb1) == 8'h00); + assert (byte'(y ? L2sb01 : 1'sb1) == 8'h01); + assert (byte'(y ? L2sb10 : 1'sb1) == 8'hfe); + assert (byte'(y ? L2sb11 : 1'sb1) == 8'hff); + + assert (int'(y ? L1b0 : 1'sb1) == 32'h0000_0000); + assert (int'(y ? L1b1 : 1'sb1) == 32'h0000_0001); + assert (int'(y ? L1sb0 : 1'sb1) == 32'h0000_0000); + assert (int'(y ? L1sb1 : 1'sb1) == 32'hffff_ffff); + assert (int'(y ? L2b00 : 1'sb1) == 32'h0000_0000); + assert (int'(y ? L2b01 : 1'sb1) == 32'h0000_0001); + assert (int'(y ? L2b10 : 1'sb1) == 32'h0000_0002); + assert (int'(y ? L2b11 : 1'sb1) == 32'h0000_0003); + assert (int'(y ? L2sb00 : 1'sb1) == 32'h0000_0000); + assert (int'(y ? L2sb01 : 1'sb1) == 32'h0000_0001); + assert (int'(y ? L2sb10 : 1'sb1) == 32'hffff_fffe); + assert (int'(y ? L2sb11 : 1'sb1) == 32'hffff_ffff); + + assert (3'(y ? L1b0 : s1bit_t'(1)) == 3'b000); + assert (3'(y ? L1b1 : s1bit_t'(1)) == 3'b001); + assert (3'(y ? L1sb0 : s1bit_t'(1)) == 3'b000); + assert (3'(y ? L1sb1 : s1bit_t'(1)) == 3'b111); + assert (3'(y ? L2b00 : s1bit_t'(1)) == 3'b000); + assert (3'(y ? L2b01 : s1bit_t'(1)) == 3'b001); + assert (3'(y ? L2b10 : s1bit_t'(1)) == 3'b010); + assert (3'(y ? L2b11 : s1bit_t'(1)) == 3'b011); + assert (3'(y ? L2sb00 : s1bit_t'(1)) == 3'b000); + assert (3'(y ? L2sb01 : s1bit_t'(1)) == 3'b001); + assert (3'(y ? L2sb10 : s1bit_t'(1)) == 3'b110); + assert (3'(y ? L2sb11 : s1bit_t'(1)) == 3'b111); + + assert (u3bit_t'(y ? L1b0 : s1bit_t'(1)) == 3'b000); + assert (u3bit_t'(y ? L1b1 : s1bit_t'(1)) == 3'b001); + assert (u3bit_t'(y ? L1sb0 : s1bit_t'(1)) == 3'b000); + assert (u3bit_t'(y ? L1sb1 : s1bit_t'(1)) == 3'b111); + assert (u3bit_t'(y ? L2b00 : s1bit_t'(1)) == 3'b000); + assert (u3bit_t'(y ? L2b01 : s1bit_t'(1)) == 3'b001); + assert (u3bit_t'(y ? L2b10 : s1bit_t'(1)) == 3'b010); + assert (u3bit_t'(y ? L2b11 : s1bit_t'(1)) == 3'b011); + assert (u3bit_t'(y ? L2sb00 : s1bit_t'(1)) == 3'b000); + assert (u3bit_t'(y ? L2sb01 : s1bit_t'(1)) == 3'b001); + assert (u3bit_t'(y ? L2sb10 : s1bit_t'(1)) == 3'b110); + assert (u3bit_t'(y ? L2sb11 : s1bit_t'(1)) == 3'b111); + + assert (byte'(y ? L1b0 : s1bit_t'(1)) == 8'h00); + assert (byte'(y ? L1b1 : s1bit_t'(1)) == 8'h01); + assert (byte'(y ? L1sb0 : s1bit_t'(1)) == 8'h00); + assert (byte'(y ? L1sb1 : s1bit_t'(1)) == 8'hff); + assert (byte'(y ? L2b00 : s1bit_t'(1)) == 8'h00); + assert (byte'(y ? L2b01 : s1bit_t'(1)) == 8'h01); + assert (byte'(y ? L2b10 : s1bit_t'(1)) == 8'h02); + assert (byte'(y ? L2b11 : s1bit_t'(1)) == 8'h03); + assert (byte'(y ? L2sb00 : s1bit_t'(1)) == 8'h00); + assert (byte'(y ? L2sb01 : s1bit_t'(1)) == 8'h01); + assert (byte'(y ? L2sb10 : s1bit_t'(1)) == 8'hfe); + assert (byte'(y ? L2sb11 : s1bit_t'(1)) == 8'hff); + + assert (int'(y ? L1b0 : s1bit_t'(1)) == 32'h0000_0000); + assert (int'(y ? L1b1 : s1bit_t'(1)) == 32'h0000_0001); + assert (int'(y ? L1sb0 : s1bit_t'(1)) == 32'h0000_0000); + assert (int'(y ? L1sb1 : s1bit_t'(1)) == 32'hffff_ffff); + assert (int'(y ? L2b00 : s1bit_t'(1)) == 32'h0000_0000); + assert (int'(y ? L2b01 : s1bit_t'(1)) == 32'h0000_0001); + assert (int'(y ? L2b10 : s1bit_t'(1)) == 32'h0000_0002); + assert (int'(y ? L2b11 : s1bit_t'(1)) == 32'h0000_0003); + assert (int'(y ? L2sb00 : s1bit_t'(1)) == 32'h0000_0000); + assert (int'(y ? L2sb01 : s1bit_t'(1)) == 32'h0000_0001); + assert (int'(y ? L2sb10 : s1bit_t'(1)) == 32'hffff_fffe); + assert (int'(y ? L2sb11 : s1bit_t'(1)) == 32'hffff_ffff); + end endmodule From 951a705a271598d2d45531988239b49dbe48b37b Mon Sep 17 00:00:00 2001 From: Richard Herveille Date: Sun, 17 Mar 2024 19:22:26 +0100 Subject: [PATCH 3/5] Added user defined struct type tests --- tests/verilog/size_cast.sv | 113 ++++++++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 1 deletion(-) diff --git a/tests/verilog/size_cast.sv b/tests/verilog/size_cast.sv index 4fb23a0a118..0d7ea3b7bfe 100644 --- a/tests/verilog/size_cast.sv +++ b/tests/verilog/size_cast.sv @@ -21,6 +21,13 @@ module top; typedef logic signed [1:0] s2bit_t; typedef logic [2:0] u3bit_t; + typedef struct packed { + u1bit_t sign; + u3bit_t msbs; + byte lsbs; + } s12bit_packed_struct_t; + + logic y = 1; always @* begin @@ -158,6 +165,19 @@ module top; assert (int'(L2sb10 | '1) == 32'hffff_ffff); assert (int'(L2sb11 | '1) == 32'hffff_ffff); + assert (s12bit_packed_struct_t'(L1b0 | '1) == 12'hfff); + assert (s12bit_packed_struct_t'(L1b1 | '1) == 12'hfff); + assert (s12bit_packed_struct_t'(L1sb0 | '1) == 12'hfff); + assert (s12bit_packed_struct_t'(L1sb1 | '1) == 12'hfff); + assert (s12bit_packed_struct_t'(L2b00 | '1) == 12'hfff); + assert (s12bit_packed_struct_t'(L2b01 | '1) == 12'hfff); + assert (s12bit_packed_struct_t'(L2b10 | '1) == 12'hfff); + assert (s12bit_packed_struct_t'(L2b11 | '1) == 12'hfff); + assert (s12bit_packed_struct_t'(L2sb00 | '1) == 12'hfff); + assert (s12bit_packed_struct_t'(L2sb01 | '1) == 12'hfff); + assert (s12bit_packed_struct_t'(L2sb10 | '1) == 12'hfff); + assert (s12bit_packed_struct_t'(L2sb11 | '1) == 12'hfff); + assert (3'(L1b0 | '0) == 3'b000); assert (3'(L1b1 | '0) == 3'b001); @@ -211,6 +231,19 @@ module top; assert (int'(L2sb10 | '0) == 32'h0000_0002); assert (int'(L2sb11 | '0) == 32'h0000_0003); + assert (s12bit_packed_struct_t'(L1b0 | '0) == 12'h000); + assert (s12bit_packed_struct_t'(L1b1 | '0) == 12'h001); + assert (s12bit_packed_struct_t'(L1sb0 | '0) == 12'h000); + assert (s12bit_packed_struct_t'(L1sb1 | '0) == 12'h001); + assert (s12bit_packed_struct_t'(L2b00 | '0) == 12'h000); + assert (s12bit_packed_struct_t'(L2b01 | '0) == 12'h001); + assert (s12bit_packed_struct_t'(L2b10 | '0) == 12'h002); + assert (s12bit_packed_struct_t'(L2b11 | '0) == 12'h003); + assert (s12bit_packed_struct_t'(L2sb00 | '0) == 12'h000); + assert (s12bit_packed_struct_t'(L2sb01 | '0) == 12'h001); + assert (s12bit_packed_struct_t'(L2sb10 | '0) == 12'h002); + assert (s12bit_packed_struct_t'(L2sb11 | '0) == 12'h003); + assert (3'(y ? L1b0 : '1) == 3'b000); assert (3'(y ? L1b1 : '1) == 3'b001); @@ -264,6 +297,19 @@ module top; assert (int'(y ? L2sb10 : '1) == 32'h0000_0002); assert (int'(y ? L2sb11 : '1) == 32'h0000_0003); + assert (s12bit_packed_struct_t'(y ? L1b0 : '1) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1b1 : '1) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L1sb0 : '1) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1sb1 : '1) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2b00 : '1) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2b01 : '1) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2b10 : '1) == 12'h002); + assert (s12bit_packed_struct_t'(y ? L2b11 : '1) == 12'h003); + assert (s12bit_packed_struct_t'(y ? L2sb00 : '1) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2sb01 : '1) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2sb10 : '1) == 12'h002); + assert (s12bit_packed_struct_t'(y ? L2sb11 : '1) == 12'h003); + assert (3'(y ? L1b0 : '0) == 3'b000); assert (3'(y ? L1b1 : '0) == 3'b001); @@ -316,6 +362,19 @@ module top; assert (int'(y ? L2sb01 : '0) == 32'h0000_0001); assert (int'(y ? L2sb10 : '0) == 32'h0000_0002); assert (int'(y ? L2sb11 : '0) == 32'h0000_0003); + + assert (s12bit_packed_struct_t'(y ? L1b0 : '0) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1b1 : '0) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L1sb0 : '0) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1sb1 : '0) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2b00 : '0) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2b01 : '0) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2b10 : '0) == 12'h002); + assert (s12bit_packed_struct_t'(y ? L2b11 : '0) == 12'h003); + assert (s12bit_packed_struct_t'(y ? L2sb00 : '0) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2sb01 : '0) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2sb10 : '0) == 12'h002); + assert (s12bit_packed_struct_t'(y ? L2sb11 : '0) == 12'h003); assert (3'(y ? L1b0 : 1'sb0) == 3'b000); @@ -370,6 +429,19 @@ module top; assert (int'(y ? L2sb10 : 1'sb0) == 32'hffff_fffe); assert (int'(y ? L2sb11 : 1'sb0) == 32'hffff_ffff); + assert (s12bit_packed_struct_t'(y ? L1b0 : 1'sb0) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1b1 : 1'sb0) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L1sb0 : 1'sb0) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1sb1 : 1'sb0) == 12'hfff); + assert (s12bit_packed_struct_t'(y ? L2b00 : 1'sb0) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2b01 : 1'sb0) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2b10 : 1'sb0) == 12'h002); + assert (s12bit_packed_struct_t'(y ? L2b11 : 1'sb0) == 12'h003); + assert (s12bit_packed_struct_t'(y ? L2sb00 : 1'sb0) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2sb01 : 1'sb0) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2sb10 : 1'sb0) == 12'hffe); + assert (s12bit_packed_struct_t'(y ? L2sb11 : 1'sb0) == 12'hfff); + assert (3'(y ? L1b0 : s1bit_t'(0)) == 3'b000); assert (3'(y ? L1b1 : s1bit_t'(0)) == 3'b001); assert (3'(y ? L1sb0 : s1bit_t'(0)) == 3'b000); @@ -420,7 +492,20 @@ module top; assert (int'(y ? L2sb00 : s1bit_t'(0)) == 32'h0000_0000); assert (int'(y ? L2sb01 : s1bit_t'(0)) == 32'h0000_0001); assert (int'(y ? L2sb10 : s1bit_t'(0)) == 32'hffff_fffe); - assert (int'(y ? L2sb11 : s1bit_t'(0)) == 32'hffff_ffff); + assert (int'(y ? L2sb11 : s1bit_t'(0)) == 32'hffff_ffff); + + assert (s12bit_packed_struct_t'(y ? L1b0 : s1bit_t'(0)) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1b1 : s1bit_t'(0)) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L1sb0 : s1bit_t'(0)) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1sb1 : s1bit_t'(0)) == 12'hfff); + assert (s12bit_packed_struct_t'(y ? L2b00 : s1bit_t'(0)) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2b01 : s1bit_t'(0)) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2b10 : s1bit_t'(0)) == 12'h002); + assert (s12bit_packed_struct_t'(y ? L2b11 : s1bit_t'(0)) == 12'h003); + assert (s12bit_packed_struct_t'(y ? L2sb00 : s1bit_t'(0)) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2sb01 : s1bit_t'(0)) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2sb10 : s1bit_t'(0)) == 12'hffe); + assert (s12bit_packed_struct_t'(y ? L2sb11 : s1bit_t'(0)) == 12'hfff); assert (3'(y ? L1b0 : 1'sb1) == 3'b000); @@ -475,6 +560,19 @@ module top; assert (int'(y ? L2sb10 : 1'sb1) == 32'hffff_fffe); assert (int'(y ? L2sb11 : 1'sb1) == 32'hffff_ffff); + assert (s12bit_packed_struct_t'(y ? L1b0 : 1'sb1) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1b1 : 1'sb1) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L1sb0 : 1'sb1) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1sb1 : 1'sb1) == 12'hfff); + assert (s12bit_packed_struct_t'(y ? L2b00 : 1'sb1) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2b01 : 1'sb1) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2b10 : 1'sb1) == 12'h002); + assert (s12bit_packed_struct_t'(y ? L2b11 : 1'sb1) == 12'h003); + assert (s12bit_packed_struct_t'(y ? L2sb00 : 1'sb1) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2sb01 : 1'sb1) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2sb10 : 1'sb1) == 12'hffe); + assert (s12bit_packed_struct_t'(y ? L2sb11 : 1'sb1) == 12'hfff); + assert (3'(y ? L1b0 : s1bit_t'(1)) == 3'b000); assert (3'(y ? L1b1 : s1bit_t'(1)) == 3'b001); assert (3'(y ? L1sb0 : s1bit_t'(1)) == 3'b000); @@ -527,5 +625,18 @@ module top; assert (int'(y ? L2sb10 : s1bit_t'(1)) == 32'hffff_fffe); assert (int'(y ? L2sb11 : s1bit_t'(1)) == 32'hffff_ffff); + assert (s12bit_packed_struct_t'(y ? L1b0 : s1bit_t'(1)) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1b1 : s1bit_t'(1)) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L1sb0 : s1bit_t'(1)) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L1sb1 : s1bit_t'(1)) == 12'hfff); + assert (s12bit_packed_struct_t'(y ? L2b00 : s1bit_t'(1)) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2b01 : s1bit_t'(1)) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2b10 : s1bit_t'(1)) == 12'h002); + assert (s12bit_packed_struct_t'(y ? L2b11 : s1bit_t'(1)) == 12'h003); + assert (s12bit_packed_struct_t'(y ? L2sb00 : s1bit_t'(1)) == 12'h000); + assert (s12bit_packed_struct_t'(y ? L2sb01 : s1bit_t'(1)) == 12'h001); + assert (s12bit_packed_struct_t'(y ? L2sb10 : s1bit_t'(1)) == 12'hffe); + assert (s12bit_packed_struct_t'(y ? L2sb11 : s1bit_t'(1)) == 12'hfff); + end endmodule From 4961f94679b06146e99b77548622d1a906b03432 Mon Sep 17 00:00:00 2001 From: Richard Herveille Date: Fri, 31 May 2024 17:58:06 +0200 Subject: [PATCH 4/5] Incorporated feedback from Yosys/HQ --- frontends/ast/ast.cc | 20 +++++++------------- frontends/ast/simplify.cc | 14 -------------- tests/verilog/size_cast.sv | 9 --------- 3 files changed, 7 insertions(+), 36 deletions(-) diff --git a/frontends/ast/ast.cc b/frontends/ast/ast.cc index e5c0d4136d4..3530005989d 100644 --- a/frontends/ast/ast.cc +++ b/frontends/ast/ast.cc @@ -697,26 +697,20 @@ void AstNode::dumpVlog(FILE *f, std::string indent) const switch (children[0]->type) { case AST_WIRE: - if (children[0]->children.size() > 0) { + if (children[0]->children.size() > 0) children[0]->children[0]->dumpVlog(f, ""); - fprintf(f, "'("); - children[1]->dumpVlog(f, ""); - fprintf(f, ")"); - } else { - fprintf(f, "%d'(", children[0]->range_left - children[0]->range_right +1); - children[1]->dumpVlog(f, ""); - fprintf(f, ")"); - } + else + fprintf(f, "%d'", children[0]->range_left - children[0]->range_right +1); break; default: //AST_IDENTIFIER children[0]->dumpVlog(f, ""); - fprintf(f, "'("); - children[1]->dumpVlog(f, ""); - fprintf(f, ")"); } - break; + fprintf(f, "'("); + children[1]->dumpVlog(f, ""); + fprintf(f, ")"); + break; if (0) { case AST_BIT_AND: txt = "&"; } if (0) { case AST_BIT_OR: txt = "|"; } diff --git a/frontends/ast/simplify.cc b/frontends/ast/simplify.cc index aa2bab1744e..8d04c4ec1f0 100644 --- a/frontends/ast/simplify.cc +++ b/frontends/ast/simplify.cc @@ -1509,8 +1509,6 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin if (child->type == AST_WIRE) { if (child->children.size() == 0) { //Base type (e.g. int) - - //Construct AST_CONSTANT node width = child->range_left - child->range_right +1; node = mkconst_int(width, child->is_signed); } else { @@ -1536,23 +1534,15 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin if (template_node->children.size() > 0 && template_node->children[0]->type == AST_RANGE) width = range_width(this, template_node->children[0]); - //delete child's children child->delete_children(); - - //create AST_CONSTANT node node = mkconst_int(width, true); - break; } case AST_STRUCT: { - //delete child's children child->delete_children(); - - //create AST_CONSTANT node width = size_packed_struct(template_node, 0); node = mkconst_int(width, false); - break; } @@ -1561,17 +1551,13 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin } } - //Remove child node delete child; children.erase(children.begin()); - - //insert AST_CONSTANT children.insert(children.begin(),node); } detect_width_simple = true; children_are_self_determined = true; - break; } diff --git a/tests/verilog/size_cast.sv b/tests/verilog/size_cast.sv index 0d7ea3b7bfe..d22cd3310bf 100644 --- a/tests/verilog/size_cast.sv +++ b/tests/verilog/size_cast.sv @@ -27,7 +27,6 @@ module top; byte lsbs; } s12bit_packed_struct_t; - logic y = 1; always @* begin @@ -58,7 +57,6 @@ module top; assert (s1bit_t'(L2sb10) == 1'b0); assert (s1bit_t'(L2sb11) == 1'b1); - assert (2'(L1b0 ) == 2'b00); assert (2'(L1b1 ) == 2'b01); assert (2'(L1sb0 ) == 2'b00); @@ -85,7 +83,6 @@ module top; assert (s2bit_t'(L2sb10) == 2'b10); assert (s2bit_t'(L2sb11) == 2'b11); - assert (3'(L1b0 ) == 3'b000); assert (3'(L1b1 ) == 3'b001); assert (3'(L1sb0 ) == 3'b000); @@ -112,7 +109,6 @@ module top; assert (u3bit_t'(L2sb10) == 3'b110); assert (u3bit_t'(L2sb11) == 3'b111); - assert (3'(L1b0 | '1) == 3'b111); assert (3'(L1b1 | '1) == 3'b111); assert (3'(L1sb0 | '1) == 3'b111); @@ -178,7 +174,6 @@ module top; assert (s12bit_packed_struct_t'(L2sb10 | '1) == 12'hfff); assert (s12bit_packed_struct_t'(L2sb11 | '1) == 12'hfff); - assert (3'(L1b0 | '0) == 3'b000); assert (3'(L1b1 | '0) == 3'b001); assert (3'(L1sb0 | '0) == 3'b000); @@ -244,7 +239,6 @@ module top; assert (s12bit_packed_struct_t'(L2sb10 | '0) == 12'h002); assert (s12bit_packed_struct_t'(L2sb11 | '0) == 12'h003); - assert (3'(y ? L1b0 : '1) == 3'b000); assert (3'(y ? L1b1 : '1) == 3'b001); assert (3'(y ? L1sb0 : '1) == 3'b000); @@ -310,7 +304,6 @@ module top; assert (s12bit_packed_struct_t'(y ? L2sb10 : '1) == 12'h002); assert (s12bit_packed_struct_t'(y ? L2sb11 : '1) == 12'h003); - assert (3'(y ? L1b0 : '0) == 3'b000); assert (3'(y ? L1b1 : '0) == 3'b001); assert (3'(y ? L1sb0 : '0) == 3'b000); @@ -375,7 +368,6 @@ module top; assert (s12bit_packed_struct_t'(y ? L2sb01 : '0) == 12'h001); assert (s12bit_packed_struct_t'(y ? L2sb10 : '0) == 12'h002); assert (s12bit_packed_struct_t'(y ? L2sb11 : '0) == 12'h003); - assert (3'(y ? L1b0 : 1'sb0) == 3'b000); assert (3'(y ? L1b1 : 1'sb0) == 3'b001); @@ -507,7 +499,6 @@ module top; assert (s12bit_packed_struct_t'(y ? L2sb10 : s1bit_t'(0)) == 12'hffe); assert (s12bit_packed_struct_t'(y ? L2sb11 : s1bit_t'(0)) == 12'hfff); - assert (3'(y ? L1b0 : 1'sb1) == 3'b000); assert (3'(y ? L1b1 : 1'sb1) == 3'b001); assert (3'(y ? L1sb0 : 1'sb1) == 3'b000); From 23ea477854179b684900fc21171af69d73476e3b Mon Sep 17 00:00:00 2001 From: Zachary Snow Date: Sun, 1 Sep 2024 09:34:49 -0400 Subject: [PATCH 5/5] minor tweaks for consistency --- frontends/ast/ast.cc | 8 +++----- frontends/ast/simplify.cc | 16 +++++++--------- tests/verilog/size_cast.sv | 24 ++++++++++++------------ 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/frontends/ast/ast.cc b/frontends/ast/ast.cc index 3530005989d..67a45f19bae 100644 --- a/frontends/ast/ast.cc +++ b/frontends/ast/ast.cc @@ -475,7 +475,7 @@ void AstNode::dumpVlog(FILE *f, std::string indent) const break; case AST_WIRETYPE: - fprintf(f, id2vl(str).c_str()); + fprintf(f, "%s", id2vl(str).c_str()); break; case AST_MEMORY: @@ -700,13 +700,11 @@ void AstNode::dumpVlog(FILE *f, std::string indent) const if (children[0]->children.size() > 0) children[0]->children[0]->dumpVlog(f, ""); else - fprintf(f, "%d'", children[0]->range_left - children[0]->range_right +1); + fprintf(f, "%d'", children[0]->range_left - children[0]->range_right + 1); break; - - default: //AST_IDENTIFIER + default: children[0]->dumpVlog(f, ""); } - fprintf(f, "'("); children[1]->dumpVlog(f, ""); fprintf(f, ")"); diff --git a/frontends/ast/simplify.cc b/frontends/ast/simplify.cc index 8d04c4ec1f0..a8e376c1f0e 100644 --- a/frontends/ast/simplify.cc +++ b/frontends/ast/simplify.cc @@ -1487,7 +1487,6 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin width_hint = max(width_hint, children[1]->range_left - children[1]->range_right + 1); } break; - case AST_ENUM_ITEM: while (!children[0]->basic_prep && children[0]->simplify(false, stage, -1, false)) did_something = true; @@ -1508,17 +1507,16 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin if (child->type == AST_WIRE) { if (child->children.size() == 0) { - //Base type (e.g. int) + // Base type (e.g., int) width = child->range_left - child->range_right +1; node = mkconst_int(width, child->is_signed); } else { - //User defined type + // User defined type log_assert(child->children[0]->type == AST_WIRETYPE); - auto type_name = child->children[0]->str; - if (!current_scope.count(type_name)) { + const std::string &type_name = child->children[0]->str; + if (!current_scope.count(type_name)) input_error("Unknown identifier `%s' used as type name\n", type_name.c_str()); - } AstNode *resolved_type_node = current_scope.at(type_name); if (resolved_type_node->type != AST_TYPEDEF) input_error("`%s' does not name a type\n", type_name.c_str()); @@ -1533,13 +1531,13 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin case AST_WIRE: { if (template_node->children.size() > 0 && template_node->children[0]->type == AST_RANGE) width = range_width(this, template_node->children[0]); - child->delete_children(); node = mkconst_int(width, true); break; } - case AST_STRUCT: { + case AST_STRUCT: + case AST_UNION: { child->delete_children(); width = size_packed_struct(template_node, 0); node = mkconst_int(width, false); @@ -1553,7 +1551,7 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin delete child; children.erase(children.begin()); - children.insert(children.begin(),node); + children.insert(children.begin(), node); } detect_width_simple = true; diff --git a/tests/verilog/size_cast.sv b/tests/verilog/size_cast.sv index d22cd3310bf..a1d6bfae554 100644 --- a/tests/verilog/size_cast.sv +++ b/tests/verilog/size_cast.sv @@ -19,12 +19,12 @@ module top; typedef logic signed s1bit_t; typedef logic [1:0] u2bit_t; typedef logic signed [1:0] s2bit_t; - typedef logic [2:0] u3bit_t; + typedef logic [2:0] u3bit_t; typedef struct packed { - u1bit_t sign; - u3bit_t msbs; - byte lsbs; + u1bit_t sign; + u3bit_t msbs; + byte lsbs; } s12bit_packed_struct_t; logic y = 1; @@ -200,7 +200,7 @@ module top; assert (u3bit_t'(L2sb10 | '0) == 3'b010); assert (u3bit_t'(L2sb11 | '0) == 3'b011); - assert (byte'(L1b0 | '0) == 8'h00); + assert (byte'(L1b0 | '0) == 8'h00); assert (byte'(L1b1 | '0) == 8'h01); assert (byte'(L1sb0 | '0) == 8'h00); assert (byte'(L1sb1 | '0) == 8'h01); @@ -265,7 +265,7 @@ module top; assert (u3bit_t'(y ? L2sb10 : '1) == 3'b010); assert (u3bit_t'(y ? L2sb11 : '1) == 3'b011); - assert (byte'(y ? L1b0 : '1) == 8'h00); + assert (byte'(y ? L1b0 : '1) == 8'h00); assert (byte'(y ? L1b1 : '1) == 8'h01); assert (byte'(y ? L1sb0 : '1) == 8'h00); assert (byte'(y ? L1sb1 : '1) == 8'h01); @@ -330,7 +330,7 @@ module top; assert (u3bit_t'(y ? L2sb10 : '0) == 3'b010); assert (u3bit_t'(y ? L2sb11 : '0) == 3'b011); - assert (byte'(y ? L1b0 : '0) == 8'h00); + assert (byte'(y ? L1b0 : '0) == 8'h00); assert (byte'(y ? L1b1 : '0) == 8'h01); assert (byte'(y ? L1sb0 : '0) == 8'h00); assert (byte'(y ? L1sb1 : '0) == 8'h01); @@ -342,7 +342,7 @@ module top; assert (byte'(y ? L2sb01 : '0) == 8'h01); assert (byte'(y ? L2sb10 : '0) == 8'h02); assert (byte'(y ? L2sb11 : '0) == 8'h03); - + assert (int'(y ? L1b0 : '0) == 32'h0000_0000); assert (int'(y ? L1b1 : '0) == 32'h0000_0001); assert (int'(y ? L1sb0 : '0) == 32'h0000_0000); @@ -395,7 +395,7 @@ module top; assert (u3bit_t'(y ? L2sb10 : 1'sb0) == 3'b110); assert (u3bit_t'(y ? L2sb11 : 1'sb0) == 3'b111); - assert (byte'(y ? L1b0 : 1'sb0) == 8'h00); + assert (byte'(y ? L1b0 : 1'sb0) == 8'h00); assert (byte'(y ? L1b1 : 1'sb0) == 8'h01); assert (byte'(y ? L1sb0 : 1'sb0) == 8'h00); assert (byte'(y ? L1sb1 : 1'sb0) == 8'hff); @@ -460,7 +460,7 @@ module top; assert (u3bit_t'(y ? L2sb10 : s1bit_t'(0)) == 3'b110); assert (u3bit_t'(y ? L2sb11 : s1bit_t'(0)) == 3'b111); - assert (byte'(y ? L1b0 : s1bit_t'(0)) == 8'h00); + assert (byte'(y ? L1b0 : s1bit_t'(0)) == 8'h00); assert (byte'(y ? L1b1 : s1bit_t'(0)) == 8'h01); assert (byte'(y ? L1sb0 : s1bit_t'(0)) == 8'h00); assert (byte'(y ? L1sb1 : s1bit_t'(0)) == 8'hff); @@ -525,7 +525,7 @@ module top; assert (u3bit_t'(y ? L2sb10 : 1'sb1) == 3'b110); assert (u3bit_t'(y ? L2sb11 : 1'sb1) == 3'b111); - assert (byte'(y ? L1b0 : 1'sb1) == 8'h00); + assert (byte'(y ? L1b0 : 1'sb1) == 8'h00); assert (byte'(y ? L1b1 : 1'sb1) == 8'h01); assert (byte'(y ? L1sb0 : 1'sb1) == 8'h00); assert (byte'(y ? L1sb1 : 1'sb1) == 8'hff); @@ -590,7 +590,7 @@ module top; assert (u3bit_t'(y ? L2sb10 : s1bit_t'(1)) == 3'b110); assert (u3bit_t'(y ? L2sb11 : s1bit_t'(1)) == 3'b111); - assert (byte'(y ? L1b0 : s1bit_t'(1)) == 8'h00); + assert (byte'(y ? L1b0 : s1bit_t'(1)) == 8'h00); assert (byte'(y ? L1b1 : s1bit_t'(1)) == 8'h01); assert (byte'(y ? L1sb0 : s1bit_t'(1)) == 8'h00); assert (byte'(y ? L1sb1 : s1bit_t'(1)) == 8'hff);