Skip to content

Commit 8e51eaa

Browse files
committed
Verilog: separate ID for typecast expressions
This is to avoid any confusion between explicit casts in the source language with automatically front-end generated, implicit typecasts.
1 parent d3071f4 commit 8e51eaa

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

src/hw_cbmc_irep_ids.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ IREP_ID_TWO(C_little_endian, #little_endian)
3535
IREP_ID_ONE(ports)
3636
IREP_ID_ONE(inst)
3737
IREP_ID_ONE(Verilog)
38+
IREP_ID_ONE(verilog_explicit_cast)
39+
IREP_ID_ONE(verilog_implicit_typecast)
3840
IREP_ID_ONE(verilog_non_indexed_part_select)
3941
IREP_ID_ONE(verilog_indexed_part_select_plus)
4042
IREP_ID_ONE(verilog_indexed_part_select_minus)

src/verilog/parser.y

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3140,7 +3140,9 @@ time_literal: TOK_TIME_LITERAL
31403140

31413141
cast:
31423142
casting_type '\'' '(' expression ')'
3143-
{ init($$, ID_typecast); stack_expr($$).type() = stack_type($1); mto($$, $4); }
3143+
{ init($$, ID_verilog_explicit_cast);
3144+
stack_expr($$).type() = stack_type($1);
3145+
mto($$, $4); }
31443146
;
31453147
31463148
// System Verilog standard 1800-2017

src/verilog/verilog_elaborate.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void verilog_typecheckt::collect_symbols(const typet &type)
118118
if(enum_name.value().is_not_nil())
119119
initializer = enum_name.value();
120120

121-
exprt value = typecast_exprt(initializer, tbd_type);
121+
exprt value = verilog_implicit_typecast_exprt(initializer, tbd_type);
122122
value.add_source_location() = enum_name.source_location();
123123

124124
const auto base_name = enum_name.base_name();
@@ -135,8 +135,9 @@ void verilog_typecheckt::collect_symbols(const typet &type)
135135
add_symbol(std::move(enum_name_symbol));
136136

137137
initializer = plus_exprt(
138-
typecast_exprt(initializer, tbd_type),
139-
typecast_exprt(from_integer(1, integer_typet()), tbd_type));
138+
verilog_implicit_typecast_exprt(initializer, tbd_type),
139+
verilog_implicit_typecast_exprt(
140+
from_integer(1, integer_typet()), tbd_type));
140141
}
141142

142143
// Add a symbol for the enum to the symbol table.

src/verilog/verilog_expr.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,4 +1843,30 @@ inline verilog_module_sourcet &to_verilog_module_source(irept &irep)
18431843
return static_cast<verilog_module_sourcet &>(irep);
18441844
}
18451845

1846+
class verilog_implicit_typecast_exprt : public unary_exprt
1847+
{
1848+
public:
1849+
verilog_implicit_typecast_exprt(exprt __op, typet __type)
1850+
: unary_exprt(
1851+
ID_verilog_implicit_typecast,
1852+
std::move(__op),
1853+
std::move(__type))
1854+
{
1855+
}
1856+
};
1857+
1858+
inline const verilog_implicit_typecast_exprt &
1859+
to_verilog_implicit_typecast_expr(const exprt &expr)
1860+
{
1861+
verilog_implicit_typecast_exprt::check(expr);
1862+
return static_cast<const verilog_implicit_typecast_exprt &>(expr);
1863+
}
1864+
1865+
inline verilog_implicit_typecast_exprt &
1866+
to_verilog_implicit_typecast_expr(exprt &expr)
1867+
{
1868+
verilog_implicit_typecast_exprt::check(expr);
1869+
return static_cast<verilog_implicit_typecast_exprt &>(expr);
1870+
}
1871+
18461872
#endif

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1877,12 +1877,20 @@ exprt verilog_typecheck_exprt::convert_unary_expr(unary_exprt expr)
18771877
make_boolean(expr.op());
18781878
expr.type()=bool_typet();
18791879
}
1880-
else if(expr.id() == ID_typecast)
1880+
else if(expr.id() == ID_verilog_explicit_cast)
18811881
{
18821882
// System Verilog has got type'(expr). This is an explicit
18831883
// type cast.
18841884
expr.type() = convert_type(expr.type());
18851885
convert_expr(expr.op());
1886+
expr.id(ID_typecast);
1887+
}
1888+
else if(expr.id() == ID_verilog_implicit_typecast)
1889+
{
1890+
// We generate implict casts during elaboration.
1891+
expr.type() = convert_type(expr.type());
1892+
convert_expr(expr.op());
1893+
expr.id(ID_typecast);
18861894
}
18871895
else
18881896
{

0 commit comments

Comments
 (0)