Skip to content

Commit 3c227f1

Browse files
committed
SystemVerilog: type parameter ports
This adds support for SystemVerilog type parameter ports (1800-2017 6.20.3).
1 parent b02498f commit 3c227f1

11 files changed

+93
-22
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Verilog: fix for typed parameter ports
44
* SystemVerilog: fix for type parameters
5+
* SystemVerilog: type parameter ports
56
* SMV: word constants
67
* SMV: IVAR declarations
78
* SMV: bit selection operator

regression/verilog/modules/parameter_ports4.desc

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
type_parameter_port1.sv
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
--
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
type_parameter_port2.sv
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
--
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module sub #(parameter type T = int)();
2+
3+
var T my_var;
4+
5+
endmodule
6+
7+
module main;
8+
9+
sub #(.T(byte)) submodule();
10+
11+
p1: assert final ($bits(submodule.my_var) == 8);
12+
13+
endmodule // main
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
type_parameter_port3.sv
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
--
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module sub #(type T = int)();
2+
3+
var T my_var;
4+
5+
endmodule
6+
7+
module main;
8+
9+
sub #(byte) submodule();
10+
11+
p1: assert final ($bits(submodule.my_var) == 8);
12+
13+
endmodule // main

src/verilog/parser.y

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,8 @@ list_of_variable_identifiers:
19851985
parameter_port_declaration:
19861986
TOK_PARAMETER data_type_or_implicit param_assignment
19871987
{ init($$, ID_decl); stack_expr($$).type() = std::move(stack_type($2)); mto($$, $3); }
1988+
| TOK_PARAMETER TOK_TYPE type_assignment
1989+
{ init($$, ID_decl); stack_expr($$).type() = typet{ID_type}; mto($$, $3); }
19881990
| TOK_LOCALPARAM data_type_or_implicit param_assignment
19891991
{ init($$, ID_decl); stack_expr($$).type() = std::move(stack_type($2)); mto($$, $3); }
19901992
| data_type param_assignment
@@ -2028,7 +2030,7 @@ list_of_type_assignments:
20282030
;
20292031

20302032
type_assignment: param_identifier '=' data_type
2031-
{ init($$, ID_parameter);
2033+
{ init($$, ID_declarator);
20322034
auto base_name = stack_expr($1).id();
20332035
stack_expr($$).set(ID_identifier, base_name);
20342036
stack_expr($$).set(ID_base_name, base_name);
@@ -3162,11 +3164,23 @@ named_parameter_assignment_brace:
31623164
{ $$=$1; mto($$, $3); }
31633165
;
31643166

3165-
ordered_parameter_assignment:
3166-
expression;
3167+
ordered_parameter_assignment: param_expression
3168+
;
3169+
3170+
param_expression:
3171+
expression
3172+
| data_type
3173+
{ init($$, ID_type); stack_expr($$).type() = stack_type($1); }
3174+
;
3175+
3176+
param_expression_opt:
3177+
/* empty */
3178+
{ init($$, ID_nil); }
3179+
| param_expression
3180+
;
31673181

31683182
named_parameter_assignment:
3169-
'.' parameter_identifier '(' expression_opt ')'
3183+
'.' parameter_identifier '(' param_expression_opt ')'
31703184
{ init($$, ID_named_parameter_assignment);
31713185
stack_expr($$).add(ID_parameter).swap(stack_expr($2));
31723186
stack_expr($$).add(ID_value).swap(stack_expr($4));

src/verilog/verilog_parameterize_module.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, kroening@kroening.com
1010
#include <util/replace_symbol.h>
1111
#include <util/simplify_expr.h>
1212

13+
#include "typename.h"
1314
#include "verilog_typecheck.h"
1415

1516
#include "verilog_expr.h"
@@ -245,15 +246,17 @@ irep_idt verilog_typecheckt::parameterize_module(
245246

246247
if(pv.is_not_nil())
247248
{
248-
mp_integer i;
249-
if(to_integer_non_constant(pv, i))
249+
if(pv.id() == ID_type)
250250
{
251-
throw errort().with_location(pv.source_location())
252-
<< "parameter value expected to be constant, but got `"
253-
<< to_string(pv) << "'";
251+
suffix += verilog_typename(to_type_expr(pv).type());
254252
}
255-
else
253+
else if(pv.id() == ID_constant)
254+
{
255+
mp_integer i = numeric_cast_v<mp_integer>(to_constant_expr(pv));
256256
suffix += integer2string(i);
257+
}
258+
else
259+
DATA_INVARIANT(false, "parameter value expected to be type or constant");
257260
}
258261
}
259262

0 commit comments

Comments
 (0)