Skip to content

Commit efeca65

Browse files
committed
Verilog: introduce verilog_identifier_exprt
The RHS of the '.' operator is always a simple identifier, which is never decorated or qualified, and hence, the use of symbol_exprt is inappropriate. This introduces verilog_identifier_exprt as the RHS of '.', which represents a base name only.
1 parent 518bff1 commit efeca65

File tree

6 files changed

+64
-14
lines changed

6 files changed

+64
-14
lines changed

src/hw_cbmc_irep_ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ IREP_ID_ONE(verilog_associative_array)
127127
IREP_ID_ONE(verilog_declarations)
128128
IREP_ID_ONE(verilog_default_clocking)
129129
IREP_ID_ONE(verilog_default_disable)
130+
IREP_ID_ONE(verilog_identifier)
130131
IREP_ID_ONE(verilog_interconnect)
131132
IREP_ID_ONE(verilog_lifetime)
132133
IREP_ID_ONE(verilog_logical_equality)

src/verilog/parser.y

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4770,6 +4770,7 @@ hierarchical_identifier:
47704770
| hierarchical_identifier '.' identifier
47714771
{ init($$, ID_hierarchical_identifier);
47724772
stack_expr($$).reserve_operands(2);
4773+
stack_expr($3).id(ID_verilog_identifier);
47734774
mto($$, $1);
47744775
mto($$, $3);
47754776
}

src/verilog/verilog_expr.h

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,36 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include <util/std_expr.h>
1313

14+
/// A simple Verilog identifier, unqualified
15+
class verilog_identifier_exprt : public nullary_exprt
16+
{
17+
public:
18+
const irep_idt &base_name() const
19+
{
20+
return get(ID_base_name);
21+
}
22+
23+
void identifier(irep_idt _base_name)
24+
{
25+
set(ID_base_name, _base_name);
26+
}
27+
};
28+
29+
inline const verilog_identifier_exprt &
30+
to_verilog_identifier_expr(const exprt &expr)
31+
{
32+
PRECONDITION(expr.id() == ID_verilog_identifier);
33+
verilog_identifier_exprt::check(expr);
34+
return static_cast<const verilog_identifier_exprt &>(expr);
35+
}
36+
37+
inline verilog_identifier_exprt &to_verilog_identifier_expr(exprt &expr)
38+
{
39+
PRECONDITION(expr.id() == ID_verilog_identifier);
40+
verilog_identifier_exprt::check(expr);
41+
return static_cast<verilog_identifier_exprt &>(expr);
42+
}
43+
1444
/// The syntax for these A.B, where A is a module identifier and B
1545
/// is an identifier within that module. B is given als symbol_exprt.
1646
class hierarchical_identifier_exprt : public binary_exprt
@@ -21,12 +51,12 @@ class hierarchical_identifier_exprt : public binary_exprt
2151
return op0();
2252
}
2353

24-
const symbol_exprt &item() const
54+
const verilog_identifier_exprt &item() const
2555
{
26-
return static_cast<const symbol_exprt &>(binary_exprt::op1());
56+
return static_cast<const verilog_identifier_exprt &>(binary_exprt::op1());
2757
}
2858

29-
const symbol_exprt &rhs() const
59+
const verilog_identifier_exprt &rhs() const
3060
{
3161
return item();
3262
}
@@ -2049,6 +2079,7 @@ class verilog_assert_assume_cover_module_itemt : public verilog_module_itemt
20492079
return op1();
20502080
}
20512081

2082+
// The full identifier created by the type checker
20522083
const irep_idt &identifier() const
20532084
{
20542085
return get(ID_identifier);
@@ -2058,6 +2089,11 @@ class verilog_assert_assume_cover_module_itemt : public verilog_module_itemt
20582089
{
20592090
set(ID_identifier, identifier);
20602091
}
2092+
2093+
const irep_idt &base_name() const
2094+
{
2095+
return get(ID_base_name);
2096+
}
20612097
};
20622098

20632099
inline const verilog_assert_assume_cover_module_itemt &
@@ -2119,6 +2155,16 @@ class verilog_assert_assume_cover_statementt : public verilog_statementt
21192155
{
21202156
set(ID_identifier, _identifier);
21212157
}
2158+
2159+
const irep_idt &base_name() const
2160+
{
2161+
return get(ID_base_name);
2162+
}
2163+
2164+
void base_name(irep_idt _base_name)
2165+
{
2166+
set(ID_base_name, _base_name);
2167+
}
21222168
};
21232169

21242170
inline const verilog_assert_assume_cover_statementt &

src/verilog/verilog_synthesis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,12 @@ void verilog_synthesist::expand_hierarchical_identifier(
510510
const irep_idt &lhs_identifier = expr.lhs().get(ID_identifier);
511511

512512
// rhs
513-
const irep_idt &rhs_identifier = expr.rhs().get_identifier();
513+
const irep_idt &rhs_base_name = expr.rhs().base_name();
514514

515515
// just patch together
516516

517517
irep_idt full_identifier =
518-
id2string(lhs_identifier) + '.' + id2string(rhs_identifier);
518+
id2string(lhs_identifier) + '.' + id2string(rhs_base_name);
519519

520520
// Note: the instance copy may not yet be in symbol table,
521521
// as the inst module item may be later.

src/verilog/verilog_typecheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ void verilog_typecheckt::convert_parameter_override(
962962

963963
auto module_instance =
964964
to_symbol_expr(hierarchical_identifier.module()).get_identifier();
965-
auto parameter_name = hierarchical_identifier.item().get_identifier();
965+
auto parameter_name = hierarchical_identifier.item().base_name();
966966

967967
// The rhs must be a constant at this point.
968968
auto rhs_value =

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,19 +1421,21 @@ exprt verilog_typecheck_exprt::convert_hierarchical_identifier(
14211421
{
14221422
convert_expr(expr.lhs());
14231423

1424-
DATA_INVARIANT(expr.rhs().id() == ID_symbol, "expected symbol on rhs of `.'");
1424+
DATA_INVARIANT(
1425+
expr.rhs().id() == ID_verilog_identifier,
1426+
"expected verilog_identifier as rhs of `.'");
14251427

1426-
const irep_idt &rhs_identifier = expr.rhs().get_identifier();
1428+
const irep_idt &rhs_base_name = expr.rhs().base_name();
14271429

14281430
if(expr.lhs().type().id() == ID_struct || expr.lhs().type().id() == ID_union)
14291431
{
14301432
// look up the component
14311433
auto &compound_type = to_struct_union_type(expr.lhs().type());
1432-
auto &component = compound_type.get_component(rhs_identifier);
1434+
auto &component = compound_type.get_component(rhs_base_name);
14331435
if(component.is_nil())
14341436
throw errort().with_location(expr.source_location())
14351437
<< compound_type.id() << " does not have a member named "
1436-
<< rhs_identifier;
1438+
<< rhs_base_name;
14371439

14381440
// create the member expression
14391441
return member_exprt{expr.lhs(), component.get_name(), component.type()}
@@ -1467,7 +1469,7 @@ exprt verilog_typecheck_exprt::convert_hierarchical_identifier(
14671469

14681470
// the identifier in the module
14691471
const irep_idt full_identifier =
1470-
id2string(module) + "." + id2string(rhs_identifier);
1472+
id2string(module) + "." + id2string(rhs_base_name);
14711473

14721474
const symbolt *symbol;
14731475
if(!ns.lookup(full_identifier, symbol))
@@ -1485,7 +1487,7 @@ exprt verilog_typecheck_exprt::convert_hierarchical_identifier(
14851487
else
14861488
{
14871489
throw errort().with_location(expr.source_location())
1488-
<< "identifier `" << rhs_identifier << "' not found in module `"
1490+
<< "identifier `" << rhs_base_name << "' not found in module `"
14891491
<< module_instance_symbol->pretty_name << "'";
14901492
}
14911493

@@ -1497,7 +1499,7 @@ exprt verilog_typecheck_exprt::convert_hierarchical_identifier(
14971499
else if(expr.lhs().type().id() == ID_named_block)
14981500
{
14991501
const irep_idt full_identifier =
1500-
id2string(lhs_identifier) + "." + id2string(rhs_identifier);
1502+
id2string(lhs_identifier) + "." + id2string(rhs_base_name);
15011503

15021504
const symbolt *symbol;
15031505
if(!ns.lookup(full_identifier, symbol))
@@ -1519,7 +1521,7 @@ exprt verilog_typecheck_exprt::convert_hierarchical_identifier(
15191521
else
15201522
{
15211523
throw errort().with_location(expr.source_location())
1522-
<< "identifier `" << rhs_identifier << "' not found in named block";
1524+
<< "identifier `" << rhs_base_name << "' not found in named block";
15231525
}
15241526
}
15251527
else

0 commit comments

Comments
 (0)