Skip to content

Commit 66769ff

Browse files
committed
Verilog: $typename
This implements SystemVerilog's $typename system function.
1 parent c12e5ff commit 66769ff

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
typename1.sv
3+
--module main --bound 0
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module main;
2+
3+
bit some_bit;
4+
bit [31:0] vector1;
5+
bit [0:31] vector2;
6+
bit signed [31:0] vector3;
7+
8+
assert final ($typename(some_bit)=="bit");
9+
assert final ($typename(vector1)=="bit[31:0]");
10+
assert final ($typename(vector2)=="bit[0:31]");
11+
assert final ($typename(vector3)=="bit signed[31:0]");
12+
13+
// $typename yields an elaboration-time constant
14+
parameter P = $typename(some_bit);
15+
16+
endmodule

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,45 @@ constant_exprt verilog_typecheck_exprt::high(const exprt &expr)
664664

665665
/*******************************************************************\
666666
667+
Function: verilog_typecheck_exprt::typename_string
668+
669+
Inputs:
670+
671+
Outputs:
672+
673+
Purpose:
674+
675+
\*******************************************************************/
676+
677+
exprt verilog_typecheck_exprt::typename_string(const exprt &expr)
678+
{
679+
auto &type = expr.type();
680+
681+
auto left = this->left(expr);
682+
auto right = this->right(expr);
683+
684+
std::string s;
685+
686+
if(type.id() == ID_unsignedbv || type.id() == ID_verilog_unsignedbv)
687+
{
688+
s = "bit[" + to_string(left) + ":" + to_string(right) + "]";
689+
}
690+
else if(type.id() == ID_bool)
691+
{
692+
s = "bit";
693+
}
694+
else if(type.id() == ID_signedbv || type.id() == ID_verilog_signedbv)
695+
{
696+
s = "bit signed[" + to_string(left) + ":" + to_string(right) + "]";
697+
}
698+
else
699+
s = "?";
700+
701+
return convert_constant(constant_exprt{s, string_typet{}});
702+
}
703+
704+
/*******************************************************************\
705+
667706
Function: verilog_typecheck_exprt::convert_system_function
668707
669708
Inputs:
@@ -866,6 +905,16 @@ exprt verilog_typecheck_exprt::convert_system_function(
866905

867906
return std::move(expr);
868907
}
908+
else if(identifier == "$typename")
909+
{
910+
if(arguments.size() != 1)
911+
{
912+
throw errort().with_location(expr.source_location())
913+
<< "$typename takes one argument";
914+
}
915+
916+
return typename_string(arguments[0]);
917+
}
869918
else
870919
{
871920
throw errort().with_location(expr.function().source_location())

src/verilog/verilog_typecheck_expr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ class verilog_typecheck_exprt:public verilog_typecheck_baset
196196
constant_exprt low(const exprt &);
197197
constant_exprt high(const exprt &);
198198
constant_exprt increment(const exprt &);
199+
exprt typename_string(const exprt &);
199200
};
200201

201202
bool verilog_typecheck(

0 commit comments

Comments
 (0)