Skip to content

Commit feb4677

Browse files
authored
Merge pull request #913 from diffblue/verilog-string-literals
SystemVerilog: delay expansion of string literals
2 parents 2733cf9 + 4666a85 commit feb4677

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
@@ -16,6 +16,7 @@ Author: Daniel Kroening, [email protected]
1616
#include <util/std_expr.h>
1717

1818
#include "aval_bval_encoding.h"
19+
#include "convert_literals.h"
1920
#include "verilog_bits.h"
2021
#include "verilog_expr.h"
2122

@@ -322,12 +323,14 @@ exprt verilog_lowering(exprt expr)
322323

323324
if(expr.id() == ID_constant)
324325
{
326+
auto &constant_expr = to_constant_expr(expr);
327+
325328
if(
326329
expr.type().id() == ID_verilog_unsignedbv ||
327330
expr.type().id() == ID_verilog_signedbv)
328331
{
329332
// encode into aval/bval
330-
return lower_to_aval_bval(to_constant_expr(expr));
333+
return lower_to_aval_bval(constant_expr);
331334
}
332335
else if(
333336
expr.type().id() == ID_verilog_real ||
@@ -338,6 +341,12 @@ exprt verilog_lowering(exprt expr)
338341
// no need to change value
339342
expr.type() = verilog_lowering(expr.type());
340343
}
344+
else if(expr.type().id() == ID_string)
345+
{
346+
auto result = convert_string_literal(constant_expr.get_value());
347+
result.add_source_location() = expr.source_location();
348+
expr = std::move(result);
349+
}
341350

342351
return expr;
343352
}

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
/*******************************************************************\
@@ -1379,8 +1382,8 @@ exprt verilog_typecheck_exprt::convert_constant(constant_exprt expr)
13791382
if(expr.type().id()==ID_string)
13801383
{
13811384
auto result = convert_string_literal(expr.get_value());
1382-
result.add_source_location() = source_location;
1383-
return std::move(result);
1385+
// only add a typecast for now
1386+
return typecast_exprt{std::move(expr), std::move(result.type())};
13841387
}
13851388
else if(expr.type().id()==ID_unsignedbv ||
13861389
expr.type().id()==ID_signedbv ||

0 commit comments

Comments
 (0)