Skip to content

SystemVerilog: const #1063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions regression/verilog/const/const2.desc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
KNOWNBUG
CORE
const2.sv

^EXIT=10$
^file .* line 9: assignment to const$
^EXIT=2$
^SIGNAL=0$
--
^warning: ignoring
--
Should be rejected owing to assignment to const variable.
1 change: 1 addition & 0 deletions src/hw_cbmc_irep_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ IREP_ID_ONE(sva_implies)
IREP_ID_ONE(sva_not)
IREP_ID_ONE(sva_or)
IREP_ID_ONE(module_instance)
IREP_ID_TWO(C_const, #const)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't we just use C_constant from CBMC? Over there, it's used for exactly the same purpose.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but it's mis-named. There is nothing constant about const variables.

Ideally, I'd like to rename #constant into #c_const.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with the mis-naming. I obviously don't intend to block this PR, but I'd prefer to see a medium-term cleanup of this across the two code bases.

IREP_ID_TWO(C_offset, #offset)
IREP_ID_TWO(C_increasing, #increasing)
IREP_ID_ONE(ports)
Expand Down
8 changes: 6 additions & 2 deletions src/verilog/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1381,20 +1381,24 @@ data_declaration:
const_opt TOK_VAR lifetime_opt data_type_or_implicit list_of_variable_decl_assignments ';'
{ init($$, ID_decl);
stack_expr($$).set(ID_class, ID_var);
addswap($$, ID_type, $4);
add_as_subtype(stack_type($1), stack_type($4));
addswap($$, ID_type, $1);
swapop($$, $5); }
| const_opt lifetime_opt data_type list_of_variable_decl_assignments ';'
{ init($$, ID_decl);
stack_expr($$).set(ID_class, ID_reg);
addswap($$, ID_type, $3);
add_as_subtype(stack_type($1), stack_type($3));
addswap($$, ID_type, $1);
swapop($$, $4); }
| type_declaration
| package_import_declaration
;

const_opt:
/* Optional */
{ init($$, ID_nil); }
| TOK_CONST
{ init($$, ID_const); stack_type($$).add_subtype().make_nil(); }
;

package_import_declaration_brace:
Expand Down
7 changes: 7 additions & 0 deletions src/verilog/verilog_elaborate_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,13 @@ typet verilog_typecheck_exprt::elaborate_type(const typet &src)
tmp.subtype() = elaborate_type(tmp.subtype());
return std::move(tmp);
}
else if(src.id() == ID_const)
{
auto tmp = to_type_with_subtype(src).subtype();
tmp = elaborate_type(tmp);
tmp.set(ID_C_const, true);
return tmp;
}
else
{
throw errort().with_location(source_location)
Expand Down
7 changes: 7 additions & 0 deletions src/verilog/verilog_typecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,13 @@ void verilog_typecheckt::check_lhs(
{
const symbolt &symbol=ns.lookup(to_symbol_expr(lhs));

// check for 'const'
if(symbol.type.get_bool(ID_C_const))
{
throw errort().with_location(lhs.source_location())
<< "assignment to const";
}

switch(vassign)
{
case A_CONTINUOUS:
Expand Down
2 changes: 1 addition & 1 deletion src/verilog/verilog_typecheck_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class verilog_typecheck_exprt:public verilog_typecheck_baset

void propagate_type(exprt &expr, const typet &type);

typet elaborate_type(const typet &);
[[nodiscard]] typet elaborate_type(const typet &);
typet elaborate_package_scope_typedef(const verilog_package_scope_typet &);
typet convert_enum(const class verilog_enum_typet &);
array_typet convert_unpacked_array_type(const type_with_subtypet &);
Expand Down
Loading