Skip to content

Commit 9ab06ff

Browse files
authored
Merge pull request #1258 from diffblue/port_with_value1-fix
Verilog: module port declarations with default value
2 parents 107677c + d6e359a commit 9ab06ff

11 files changed

+89
-26
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CORE
22
missing_connection1.v
33

4-
^file missing_connection1\.v line 7: wrong number of arguments: expected 2 but got 1$
4+
^file missing_connection1\.v line 7: wrong number of port connections: expected 2 but got 1$
55
^EXIT=2$
66
^SIGNAL=0$
77
--

regression/verilog/modules/port_with_value1.desc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
KNOWNBUG
1+
CORE
22
port_with_value1.sv
33

4-
^EXIT=0$
4+
^\[main\.m1\.eq] always M1\.in1 == M1\.in2: REFUTED$
5+
^\[main\.m2\.eq] always M1\.in1 == M1\.in2: PROVED .*$
6+
^EXIT=10$
57
^SIGNAL=0$
68
--
79
^warning: ignoring
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
module M1(input [31:0] in1 = 1234, in2 = 4567);
22

3-
assert final (in1 == in2);
3+
eq: assert final (in1 == in2);
44

55
endmodule
66

77
module main;
8-
// inputs not connected
8+
// inputs not connected, should fail
99
M1 m1();
1010

11-
// in2 connected
11+
// in2 connected, should pass
1212
M1 m2(.in2(1234));
1313

1414
endmodule
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
port_with_value2.sv
3+
4+
^file .* line 2: output ports must not have a default value$
5+
^EXIT=2$
6+
^SIGNAL=0$
7+
--
8+
^warning: ignoring
9+
--
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// output ports must not have a default value
2+
module M(output [31:0] o = 4567);
3+
4+
endmodule
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
port_with_value3.sv
3+
4+
^file .* line 2: expected constant expression, but got `M\.a'$
5+
^EXIT=2$
6+
^SIGNAL=0$
7+
--
8+
^warning: ignoring
9+
--
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// default values for inputs must be constants
2+
module M(input a, input b = a);
3+
4+
endmodule

src/verilog/parser.y

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,14 +899,15 @@ ansi_port_declaration_brace:
899899

900900
// append to last one -- required to make
901901
// the grammar LR1
902-
| ansi_port_declaration_brace ',' port_identifier
902+
| ansi_port_declaration_brace ',' port_identifier ansi_port_initializer_opt
903903
{ $$=$1;
904904
exprt decl(ID_decl);
905905
decl.add_to_operands(std::move(stack_expr($3)));
906906
// grab the type and class from previous!
907907
const irept &prev=stack_expr($$).get_sub().back();
908908
decl.set(ID_type, prev.find(ID_type));
909909
decl.set(ID_class, prev.find(ID_class));
910+
decl.set(ID_value, stack_expr($4));
910911
stack_expr($$).move_to_sub(decl);
911912
}
912913
;
@@ -935,6 +936,7 @@ ansi_port_declaration:
935936
// and the unpacked_array_type goes onto the declarator.
936937
stack_expr($$).type() = std::move(stack_expr($1).type());
937938
addswap($2, ID_type, $3);
939+
stack_expr($2).set(ID_value, stack_expr($4));
938940
mto($$, $2); /* declarator */ }
939941
| variable_port_header port_identifier unpacked_dimension_brace ansi_port_initializer_opt
940942
{ init($$, ID_decl);
@@ -946,6 +948,7 @@ ansi_port_declaration:
946948
// and the unpacked_array_type goes onto the declarator.
947949
stack_expr($$).type() = std::move(stack_expr($1).type());
948950
addswap($2, ID_type, $3);
951+
stack_expr($2).set(ID_value, stack_expr($4));
949952
mto($$, $2); /* declarator */ }
950953
;
951954

src/verilog/verilog_elaborate.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ void verilog_typecheckt::collect_port_symbols(const verilog_declt &decl)
6262
new_symbol.base_name = base_name;
6363
new_symbol.pretty_name = strip_verilog_prefix(new_symbol.name);
6464

65+
// When using ANSI style, input ports may have an
66+
// elaboration-time constant default value
67+
auto &default_value = declarator.value();
68+
if(default_value.is_not_nil())
69+
{
70+
if(new_symbol.is_output)
71+
throw errort{}.with_location(default_value.source_location())
72+
<< "output ports must not have a default value";
73+
74+
auto value = default_value;
75+
convert_expr(value);
76+
auto elaborated_value = elaborate_constant_expression_check(value);
77+
new_symbol.value = elaborated_value;
78+
}
79+
6580
add_symbol(std::move(new_symbol));
6681
}
6782
}

src/verilog/verilog_synthesis.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,35 @@ void verilog_synthesist::instantiate_ports(
14441444
is_output, port, value, replace_map, inst.source_location(), trans);
14451445
}
14461446
}
1447+
1448+
std::set<irep_idt> connected_ports;
1449+
1450+
for(const auto &connection : inst.connections())
1451+
{
1452+
auto &named_connection = to_verilog_named_port_connection(connection);
1453+
connected_ports.insert(
1454+
to_symbol_expr(named_connection.port()).get_identifier());
1455+
}
1456+
1457+
// unconnected inputs may be given a default value
1458+
for(auto &port : ports)
1459+
if(port.get_bool(ID_input))
1460+
{
1461+
auto &port_symbol_expr = to_symbol_expr((const exprt &)(port));
1462+
auto identifier = port_symbol_expr.get_identifier();
1463+
if(connected_ports.find(identifier) == connected_ports.end())
1464+
{
1465+
auto &port_symbol = ns.lookup(port_symbol_expr);
1466+
if(port_symbol.value.is_not_nil())
1467+
instantiate_port(
1468+
false,
1469+
port_symbol_expr,
1470+
port_symbol.value,
1471+
replace_map,
1472+
inst.source_location(),
1473+
trans);
1474+
}
1475+
}
14471476
}
14481477
else // just a list without names
14491478
{

0 commit comments

Comments
 (0)