From ae954e5df7c8529ef1119e38df6e0b244a5517db Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Sat, 15 Jan 2022 14:09:11 +0100 Subject: [PATCH 1/4] Support direct packed arrays of structs and enums It is possible to directly declare a packed array of a struct or enum, without having to typedef the struct or enum first. E.g. ``` struct packed { int x; } [1:0] pa; ``` Add support to the parser for handling this. Signed-off-by: Lars-Peter Clausen --- parse.y | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/parse.y b/parse.y index 6c285e71f..355f09797 100644 --- a/parse.y +++ b/parse.y @@ -637,6 +637,7 @@ static void current_function_set_statement(const YYLTYPE&loc, std::vector data_type data_type_or_implicit data_type_or_implicit_or_void %type simple_type_or_string let_formal_type +%type packed_array_data_type %type class_identifier %type struct_union_member %type struct_union_member_list @@ -1170,6 +1171,23 @@ data_declaration /* IEEE1800-2005: A.2.1.3 */ | attribute_list_opt package_import_declaration ; +/* Data types that can have packed dimensions directly attached to it */ +packed_array_data_type /* IEEE1800-2005: A.2.2.1 */ + : enum_data_type + { $$ = $1; } + | struct_data_type + { if (!$1->packed_flag) { + yyerror(@1, "sorry: Unpacked structs not supported."); + } + $$ = $1; + } + | TYPE_IDENTIFIER + { pform_set_type_referenced(@1, $1.text); + delete[]$1.text; + $$ = $1.type; + } + ; + data_type /* IEEE1800-2005: A.2.2.1 */ : integer_vector_type unsigned_signed_opt dimensions_opt { ivl_variable_type_t use_vtype = $1; @@ -1188,14 +1206,6 @@ data_type /* IEEE1800-2005: A.2.2.1 */ FILE_NAME(tmp, @1); $$ = tmp; } - | struct_data_type - { if (!$1->packed_flag) { - yyerror(@1, "sorry: Unpacked structs not supported."); - } - $$ = $1; - } - | enum_data_type - { $$ = $1; } | atom2_type signed_unsigned_opt { atom2_type_t*tmp = new atom2_type_t($1, $2); FILE_NAME(tmp, @1); @@ -1214,14 +1224,14 @@ data_type /* IEEE1800-2005: A.2.2.1 */ tmp->reg_flag = !gn_system_verilog(); $$ = tmp; } - | TYPE_IDENTIFIER dimensions_opt - { pform_set_type_referenced(@1, $1.text); - if ($2) { - parray_type_t*tmp = new parray_type_t($1.type, $2); + | packed_array_data_type dimensions_opt + { if ($2) { + parray_type_t*tmp = new parray_type_t($1, $2); FILE_NAME(tmp, @1); $$ = tmp; - } else $$ = $1.type; - delete[]$1.text; + } else { + $$ = $1; + } } | PACKAGE_IDENTIFIER K_SCOPE_RES { lex_in_package_scope($1); } From 3ca1c129ce73c288b6ee7811963f2e0be9febf31 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Sat, 15 Jan 2022 14:16:28 +0100 Subject: [PATCH 2/4] Allow packed arrays of scoped types identifiers Allow scoped identifiers to be used as the base type for packed array types. Scoped type identifiers can be used the same way as unscoped type identifiers. E.g. ``` package p; typedef logic [1:0] vector; endpackage module test; p::vector [1:0] pa; endmodule ``` is a valid construct. Signed-off-by: Lars-Peter Clausen --- parse.y | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/parse.y b/parse.y index 355f09797..7491fcbeb 100644 --- a/parse.y +++ b/parse.y @@ -1186,6 +1186,13 @@ packed_array_data_type /* IEEE1800-2005: A.2.2.1 */ delete[]$1.text; $$ = $1.type; } + | PACKAGE_IDENTIFIER K_SCOPE_RES + { lex_in_package_scope($1); } + TYPE_IDENTIFIER + { lex_in_package_scope(0); + $$ = $4.type; + delete[]$4.text; + } ; data_type /* IEEE1800-2005: A.2.2.1 */ @@ -1233,13 +1240,6 @@ data_type /* IEEE1800-2005: A.2.2.1 */ $$ = $1; } } - | PACKAGE_IDENTIFIER K_SCOPE_RES - { lex_in_package_scope($1); } - TYPE_IDENTIFIER - { lex_in_package_scope(0); - $$ = $4.type; - delete[]$4.text; - } | K_string { string_type_t*tmp = new string_type_t; FILE_NAME(tmp, @1); From c670170ac03f14d122a3471342abcab6cf0d5560 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Sat, 15 Jan 2022 19:02:10 +0100 Subject: [PATCH 3/4] Add test for direct packed arrays of struct and enums It is possible to directly declare a packed array of a struct or enum without having to create a typedef first. Add a check to the array_packed test that this is supported and works as expected. Signed-off-by: Lars-Peter Clausen --- ivtest/ivltests/array_packed.v | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/ivtest/ivltests/array_packed.v b/ivtest/ivltests/array_packed.v index f84213381..10d2ebadd 100644 --- a/ivtest/ivltests/array_packed.v +++ b/ivtest/ivltests/array_packed.v @@ -23,6 +23,10 @@ EP [2:0] epp1; EPP epp2; EPP [3:0] eppp; +enum logic [7:0] { + B +} [1:0] ep3; + typedef struct packed { longint x; } S1; @@ -54,6 +58,10 @@ SP [9:0] spp1; SPP spp2; SPP [1:0] sppp; +struct packed { + S2 s; +} [3:0] sp3; + bit failed = 1'b0; initial begin @@ -65,17 +73,19 @@ initial begin failed |= $bits(e) !== 8; failed |= $bits(ep1) !== $bits(e) * 2; failed |= $bits(ep2) !== $bits(ep1); + failed |= $bits(ep3) !== $bits(ep1); failed |= $bits(epp1) !== $bits(ep1) * 3; failed |= $bits(epp2) !== $bits(epp1); failed |= $bits(eppp) !== $bits(epp1) * 4; // Packed arrays of structs failed |= $bits(s) !== S_SIZE; - failed |= $bits(sp1) != $bits(s) * 4; - failed |= $bits(sp2) != $bits(sp1); - failed |= $bits(spp1) != $bits(sp1) * 10; - failed |= $bits(spp1) != $bits(spp2); - failed |= $bits(sppp) != $bits(spp1) * 2; + failed |= $bits(sp1) !== $bits(s) * 4; + failed |= $bits(sp2) !== $bits(sp1); + failed |= $bits(sp3) !== $bits(sp1); + failed |= $bits(spp1) !== $bits(sp1) * 10; + failed |= $bits(spp1) !== $bits(spp2); + failed |= $bits(sppp) !== $bits(spp1) * 2; if (failed) $display("FAILED"); From 38c4c20cbcdd09012b4fd26163cc61e00cdfc8b1 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Sat, 15 Jan 2022 21:13:52 +0100 Subject: [PATCH 4/4] Add test for packed arrays of types from other scopes Extend the array_packed test to check declaring a packed array of a type from another scope that is addressed via a scoped identifier. Signed-off-by: Lars-Peter Clausen --- ivtest/ivltests/array_packed.v | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ivtest/ivltests/array_packed.v b/ivtest/ivltests/array_packed.v index 10d2ebadd..b0e231906 100644 --- a/ivtest/ivltests/array_packed.v +++ b/ivtest/ivltests/array_packed.v @@ -1,6 +1,10 @@ // Check that packed arrays of all sorts get elaborated without an error and // that the resulting type has the right packed width. +package p; + typedef logic [2:0] vector; +endpackage + module test; typedef bit bit2; @@ -8,6 +12,7 @@ typedef logic [1:0] vector; bit2 [1:0] b; vector [2:0] l; +p::vector [3:0] scoped_pa; typedef enum logic [7:0] { A @@ -68,6 +73,7 @@ initial begin // Packed arrays of basic types failed |= $bits(b) !== 2; failed |= $bits(l) !== 2 * 3; + failed |= $bits(scoped_pa) !== 3 * 4; // Packed arrays of enums failed |= $bits(e) !== 8;