Skip to content

Commit 943cdbb

Browse files
committed
Verilog: fix string literals
The conversion of string literals has assumed a binary number represenation.
1 parent 6806280 commit 943cdbb

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
string_literals1.v
3+
--bound 0
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module main;
2+
3+
// padded from left with zeros
4+
wire [6*8-1:0] hello = "hello";
5+
always assert p0: hello[0*8+7:0*8] == "o";
6+
always assert p1: hello[1*8+7:1*8] == "l";
7+
always assert p2: hello[2*8+7:2*8] == "l";
8+
always assert p3: hello[3*8+7:3*8] == "e";
9+
always assert p4: hello[4*8+7:4*8] == "h";
10+
always assert p5: hello[5*8+7:5*8] == 0;
11+
12+
endmodule

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -902,23 +902,21 @@ void verilog_typecheck_exprt::convert_constant(constant_exprt &expr)
902902
{
903903
if(expr.type().id()==ID_string)
904904
{
905-
exprt new_expr;
905+
// These are unsigned integer vectors with 8 bits per character.
906+
// The first character is the most significant one.
906907
const std::string &value=expr.get_string(ID_value);
908+
auto type = unsignedbv_typet(value.size() * 8);
907909

908-
new_expr.type()=unsignedbv_typet(value.size()*8);
909-
910-
std::string new_value;
911-
912-
for(unsigned i=0; i<value.size(); i++)
913-
for(unsigned bit=0; bit<8; bit++)
914-
{
915-
bool b=(value[i]&(1<<bit))!=0;
916-
new_value=(b?"1":"0")+new_value;
917-
}
910+
// The below is quadratic, and should be made linear.
911+
mp_integer new_value = 0;
912+
for(std::size_t i = 0; i < value.size(); i++)
913+
{
914+
unsigned char character = value[i];
915+
new_value += mp_integer(character) << ((value.size() - i - 1) * 8);
916+
}
918917

919-
new_expr.set(ID_value, new_value);
920-
921-
expr.swap(new_expr);
918+
expr =
919+
from_integer(new_value, type).with_source_location<constant_exprt>(expr);
922920
return;
923921
}
924922
else if(expr.type().id()==ID_unsignedbv ||

0 commit comments

Comments
 (0)