Skip to content

Commit 9223813

Browse files
authored
Merge pull request #1137 from diffblue/verilog-unsized
Verilog: mark unsized literals
2 parents 14d9bb7 + ffc1021 commit 9223813

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

regression/verilog/case/case4.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module m(input [7:0] i, output reg [31:0] x);
44
casex(i)
55
8'b1x: x=1;
66
8'bxx: x=2;
7-
{ 'b11, 'bx, 'bx }: x=3;
7+
{ 2'b11, 1'bx, 1'bx }: x=3;
88
default: x=4;
99
endcase
1010

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
KNOWNBUG
1+
CORE
22
concatenation4.v
33

4-
^EXIT=0$
4+
^file .* line 5: unsized literals are not allowed in concatenations$
5+
^EXIT=2$
56
^SIGNAL=0$
67
--
78
^warning: ignoring
89
--
9-
This should yield an error.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
KNOWNBUG
1+
CORE
22
replication2.v
33

4-
^EXIT=0$
4+
^file .* line 6: unsized literals are not allowed in concatenations$
5+
^EXIT=2$
56
^SIGNAL=0$
67
--
78
^warning: ignoring
89
--
9-
This should yield an error.

src/hw_cbmc_irep_ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ IREP_ID_ONE(wire)
243243
IREP_ID_ONE(uwire)
244244
IREP_ID_ONE(wand)
245245
IREP_ID_ONE(automatic)
246+
IREP_ID_TWO(C_verilog_unsized, #verilog_unsized)
246247
IREP_ID_TWO(C_verilog_type, #verilog_type)
247248
IREP_ID_TWO(C_verilog_aval_bval, #verilog_aval_bval)
248249
IREP_ID_ONE(verilog_enum)

src/verilog/convert_literals.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ constant_exprt convert_real_literal(const irep_idt &value)
118118
return result;
119119
}
120120

121+
static constant_exprt unsized(constant_exprt expr)
122+
{
123+
expr.set(ID_C_verilog_unsized, true);
124+
return expr;
125+
}
126+
127+
static constant_exprt cond_unsized(constant_exprt expr, bool is_unsized)
128+
{
129+
if(is_unsized)
130+
return unsized(std::move(expr));
131+
else
132+
return expr;
133+
}
134+
121135
constant_exprt convert_integral_literal(const irep_idt &value)
122136
{
123137
// first, get rid of whitespace and underscores
@@ -135,19 +149,19 @@ constant_exprt convert_integral_literal(const irep_idt &value)
135149
// special case the "unbased unsized literals"
136150
if(rest == "'0")
137151
{
138-
return from_integer(0, unsignedbv_typet{1});
152+
return unsized(from_integer(0, unsignedbv_typet{1}));
139153
}
140154
else if(rest == "'1")
141155
{
142-
return from_integer(1, unsignedbv_typet{1});
156+
return unsized(from_integer(1, unsignedbv_typet{1}));
143157
}
144158
else if(rest == "'x" || rest == "'X")
145159
{
146-
return constant_exprt{"x", verilog_unsignedbv_typet{1}};
160+
return unsized(constant_exprt{"x", verilog_unsignedbv_typet{1}});
147161
}
148162
else if(rest == "'z" || rest == "'Z")
149163
{
150-
return constant_exprt{"z", verilog_unsignedbv_typet{1}};
164+
return unsized(constant_exprt{"z", verilog_unsignedbv_typet{1}});
151165
}
152166

153167
std::string::size_type pos = rest.find('\'');
@@ -183,15 +197,17 @@ constant_exprt convert_integral_literal(const irep_idt &value)
183197
auto type = s_flag_given
184198
? static_cast<typet>(verilog_signedbv_typet{final_bits})
185199
: verilog_unsignedbv_typet{final_bits};
186-
return constant_exprt{std::string(final_bits, 'x'), type};
200+
return cond_unsized(
201+
constant_exprt{std::string(final_bits, 'x'), type}, !bits_given);
187202
}
188203
else if(rest == "dz" || rest == "dZ")
189204
{
190205
std::size_t final_bits = bits_given ? bits : 32;
191206
auto type = s_flag_given
192207
? static_cast<typet>(verilog_signedbv_typet{final_bits})
193208
: verilog_unsignedbv_typet{final_bits};
194-
return constant_exprt{std::string(final_bits, 'z'), type};
209+
return cond_unsized(
210+
constant_exprt{std::string(final_bits, 'z'), type}, !bits_given);
195211
}
196212

197213
unsigned base = 10;
@@ -353,7 +369,7 @@ constant_exprt convert_integral_literal(const irep_idt &value)
353369
type = verilog_unsignedbv_typet(bits);
354370

355371
// stored as individual bits
356-
return constant_exprt{fvalue, type};
372+
return cond_unsized(constant_exprt{fvalue, type}, !bits_given);
357373
}
358374
else // two valued
359375
{
@@ -372,7 +388,8 @@ constant_exprt convert_integral_literal(const irep_idt &value)
372388
type = unsignedbv_typet(bits);
373389

374390
// stored as bvrep
375-
return constant_exprt{integer2bvrep(int_value, bits), type};
391+
return cond_unsized(
392+
constant_exprt{integer2bvrep(int_value, bits), type}, !bits_given);
376393
}
377394
}
378395
else
@@ -395,7 +412,8 @@ constant_exprt convert_integral_literal(const irep_idt &value)
395412
else
396413
type = unsignedbv_typet(bits);
397414

398-
return constant_exprt{integer2bvrep(int_value, bits), type};
415+
return cond_unsized(
416+
constant_exprt{integer2bvrep(int_value, bits), type}, !bits_given);
399417
}
400418

401419
UNREACHABLE;

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ exprt verilog_typecheck_exprt::convert_expr_concatenation(
286286
Forall_operands(it, expr)
287287
{
288288
convert_expr(*it);
289+
290+
// check if there's an unsized literal (1800-2017 11.4.12)
291+
if(it->get_bool(ID_C_verilog_unsized))
292+
{
293+
throw errort().with_location(it->source_location())
294+
<< "unsized literals are not allowed in concatenations";
295+
}
296+
289297
must_be_integral(*it);
290298

291299
const typet &type = it->type();

0 commit comments

Comments
 (0)