Skip to content

Commit 4666a85

Browse files
committed
SystemVerilog: delay expansion of string literals
This delays the expansion of string literals to preserve these in property descriptions.
1 parent 68c8662 commit 4666a85

File tree

4 files changed

+76
-12
lines changed

4 files changed

+76
-12
lines changed

regression/verilog/system-functions/typename1.desc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
CORE
22
typename1.sv
33
--module main --bound 0
4-
^\[.*\] always \$typename\(main\.some_bit\) == 24'h626974: PROVED up to bound 0$
5-
^\[.*\] always \$typename\(main\.vector1\) == 72'h6269745B33313A305D: PROVED up to bound 0$
6-
^\[.*\] always \$typename\(main\.vector2\) == 72'h6269745B303A33315D: PROVED up to bound 0$
7-
^\[.*\] always \$typename\(main\.vector3\) == 128'h626974207369676E65645B33313A305D: PROVED up to bound 0$
8-
^\[.*\] always \$typename\(real'\(1\)\) == 32'h7265616C: PROVED up to bound 0$
9-
^\[.*\] always \$typename\(shortreal'\(1\)\) == 72'h73686F72747265616C: PROVED up to bound 0$
10-
^\[.*\] always \$typename\(realtime'\(1\)\) == 64'h7265616C74696D65: PROVED up to bound 0$
4+
^\[.*\] always \$typename\(main\.some_bit\) == "bit": PROVED up to bound 0$
5+
^\[.*\] always \$typename\(main\.vector1\) == "bit\[31:0\]": PROVED up to bound 0$
6+
^\[.*\] always \$typename\(main\.vector2\) == "bit\[0:31\]": PROVED up to bound 0$
7+
^\[.*\] always \$typename\(main\.vector3\) == "bit signed\[31:0\]": PROVED up to bound 0$
8+
^\[.*\] always \$typename\(real'\(1\)\) == "real": PROVED up to bound 0$
9+
^\[.*\] always \$typename\(shortreal'\(1\)\) == "shortreal": PROVED up to bound 0$
10+
^\[.*\] always \$typename\(realtime'\(1\)\) == "realtime": PROVED up to bound 0$
1111
^EXIT=0$
1212
^SIGNAL=0$
1313
--

src/verilog/expr2verilog.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Author: Daniel Kroening, [email protected]
2424

2525
#include <algorithm>
2626
#include <cstdlib>
27+
#include <iomanip>
2728
#include <sstream>
2829

2930
/*******************************************************************\
@@ -350,7 +351,10 @@ expr2verilogt::convert_function_call(const function_call_exprt &src)
350351
else
351352
dest+=", ";
352353

353-
dest += convert_rec(op).s;
354+
if(op.id() == ID_type)
355+
dest += convert(op.type());
356+
else
357+
dest += convert_rec(op).s;
354358
}
355359

356360
dest+=")";
@@ -1208,6 +1212,54 @@ expr2verilogt::resultt expr2verilogt::convert_constant(
12081212
ieee_float.from_expr(tmp);
12091213
return {precedence, ieee_float.to_ansi_c_string()};
12101214
}
1215+
else if(type.id() == ID_string)
1216+
{
1217+
dest = '"';
1218+
1219+
for(auto &ch : id2string(src.get_value()))
1220+
{
1221+
// Follows Table Table 5-1 in 1800-2017.
1222+
switch(ch)
1223+
{
1224+
case '\n':
1225+
dest += "\\n";
1226+
break;
1227+
case '\t':
1228+
dest += "\\t";
1229+
break;
1230+
case '\\':
1231+
dest += "\\\\";
1232+
break;
1233+
case '"':
1234+
dest += "\\\"";
1235+
break;
1236+
case '\v':
1237+
dest += "\\v";
1238+
break;
1239+
case '\f':
1240+
dest += "\\f";
1241+
break;
1242+
case '\a':
1243+
dest += "\\a";
1244+
break;
1245+
default:
1246+
if(
1247+
(unsigned(ch) >= ' ' && unsigned(ch) <= 126) ||
1248+
(unsigned(ch) >= 128 && unsigned(ch) <= 254))
1249+
{
1250+
dest += ch;
1251+
}
1252+
else
1253+
{
1254+
std::ostringstream oss;
1255+
oss << "\\x" << std::setw(2) << std::setfill('0') << std::hex << ch;
1256+
dest += oss.str();
1257+
}
1258+
}
1259+
}
1260+
1261+
dest += '"';
1262+
}
12111263
else
12121264
return convert_norep(src, precedence);
12131265

src/verilog/verilog_lowering.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <util/ieee_float.h>
1616

1717
#include "aval_bval_encoding.h"
18+
#include "convert_literals.h"
1819
#include "verilog_bits.h"
1920
#include "verilog_expr.h"
2021

@@ -182,12 +183,14 @@ exprt verilog_lowering(exprt expr)
182183

183184
if(expr.id() == ID_constant)
184185
{
186+
auto &constant_expr = to_constant_expr(expr);
187+
185188
if(
186189
expr.type().id() == ID_verilog_unsignedbv ||
187190
expr.type().id() == ID_verilog_signedbv)
188191
{
189192
// encode into aval/bval
190-
return lower_to_aval_bval(to_constant_expr(expr));
193+
return lower_to_aval_bval(constant_expr);
191194
}
192195
else if(
193196
expr.type().id() == ID_verilog_real ||
@@ -198,6 +201,12 @@ exprt verilog_lowering(exprt expr)
198201
// no need to change value
199202
expr.type() = lower_verilog_real_types(expr.type());
200203
}
204+
else if(expr.type().id() == ID_string)
205+
{
206+
auto result = convert_string_literal(constant_expr.get_value());
207+
result.add_source_location() = expr.source_location();
208+
expr = std::move(result);
209+
}
201210

202211
return expr;
203212
}

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,10 @@ exprt verilog_typecheck_exprt::typename_string(const exprt &expr)
756756
else
757757
s = "?";
758758

759-
return convert_constant(constant_exprt{s, string_typet{}});
759+
auto result = convert_string_literal(s);
760+
result.add_source_location() = expr.source_location();
761+
762+
return std::move(result);
760763
}
761764

762765
/*******************************************************************\
@@ -1301,8 +1304,8 @@ exprt verilog_typecheck_exprt::convert_constant(constant_exprt expr)
13011304
if(expr.type().id()==ID_string)
13021305
{
13031306
auto result = convert_string_literal(expr.get_value());
1304-
result.add_source_location() = source_location;
1305-
return std::move(result);
1307+
// only add a typecast for now
1308+
return typecast_exprt{std::move(expr), std::move(result.type())};
13061309
}
13071310
else if(expr.type().id()==ID_unsignedbv ||
13081311
expr.type().id()==ID_signedbv ||

0 commit comments

Comments
 (0)