Skip to content

Commit 4306f65

Browse files
authored
Merge pull request #417 from diffblue/get_width_mp_integer
Verilog: `get_width` now returns `mp_integer`
2 parents 609912f + 8bfd27f commit 4306f65

File tree

5 files changed

+50
-41
lines changed

5 files changed

+50
-41
lines changed

src/verilog/verilog_synthesis.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -299,33 +299,33 @@ void verilog_synthesist::assignment_rec(
299299
{
300300
// TODO: split it up more intelligently;
301301
// bit-wise is wasteful.
302-
unsigned offset=0;
303-
302+
mp_integer offset = 0;
303+
304304
// do it from right to left
305305

306306
for(exprt::operandst::const_reverse_iterator
307307
it=lhs.operands().rbegin();
308308
it!=lhs.operands().rend();
309309
it++)
310310
{
311-
constant_exprt offset_constant{std::to_string(offset), natural_typet{}};
311+
auto offset_constant = from_integer(offset, natural_typet{});
312312

313313
if(it->type().id()==ID_bool)
314314
{
315315
exprt bit_extract(ID_extractbit, it->type());
316316
bit_extract.add_to_operands(rhs);
317317
bit_extract.add_to_operands(offset_constant);
318-
offset++;
318+
++offset;
319319

320320
assignment_rec(*it, bit_extract, blocking);
321321
}
322322
else if(it->type().id()==ID_signedbv ||
323323
it->type().id()==ID_unsignedbv)
324324
{
325-
unsigned width=get_width(it->type());
325+
auto width = get_width(it->type());
326326

327-
constant_exprt offset_constant2{std::to_string(offset + width - 1),
328-
natural_typet{}};
327+
auto offset_constant2 =
328+
from_integer(offset + width - 1, natural_typet{});
329329

330330
// extractbits requires that upper >= lower, i.e. op1 >= op2
331331
extractbits_exprt bit_extract(rhs, offset_constant2, offset_constant,
@@ -1682,29 +1682,30 @@ void verilog_synthesist::synth_force_rec(
16821682
if(lhs.id()==ID_concatenation)
16831683
{
16841684
// split it up
1685-
unsigned offset=0;
1686-
1685+
mp_integer offset = 0;
1686+
16871687
// do it from right to left
16881688

16891689
for(exprt::operandst::const_reverse_iterator
16901690
it=lhs.operands().rbegin();
16911691
it!=lhs.operands().rend();
16921692
it++)
16931693
{
1694-
constant_exprt offset_constant{std::to_string(offset), natural_typet{}};
1694+
auto offset_constant = from_integer(offset, natural_typet{});
16951695

16961696
if(it->type().id()==ID_bool)
16971697
{
1698-
extractbit_exprt bit_extract(rhs, offset);
1699-
offset++;
1698+
extractbit_exprt bit_extract(rhs, offset_constant);
1699+
++offset;
17001700
synth_force_rec(*it, bit_extract);
17011701
}
17021702
else if(it->type().id()==ID_signedbv ||
17031703
it->type().id()==ID_unsignedbv)
17041704
{
1705-
unsigned width=get_width(it->type());
1705+
auto width = get_width(it->type());
1706+
auto sum_constant = from_integer(offset + width - 1, natural_typet{});
17061707
extractbits_exprt bit_extract(
1707-
rhs, offset, offset + width - 1, it->type());
1708+
rhs, offset_constant, sum_constant, it->type());
17081709
synth_force_rec(*it, bit_extract);
17091710
offset+=width;
17101711
}

src/verilog/verilog_typecheck_base.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Function: verilog_typecheck_baset::get_width
167167
168168
\*******************************************************************/
169169

170-
std::size_t verilog_typecheck_baset::get_width(const typet &type)
170+
mp_integer verilog_typecheck_baset::get_width(const typet &type)
171171
{
172172
if(type.id()==ID_bool)
173173
return 1;
@@ -188,7 +188,7 @@ std::size_t verilog_typecheck_baset::get_width(const typet &type)
188188
mp_integer sum = 0;
189189
for(auto &component : to_struct_type(type).components())
190190
sum += get_width(component.type());
191-
return sum.to_ulong();
191+
return sum;
192192
}
193193

194194
if(type.id() == ID_verilog_shortint)

src/verilog/verilog_typecheck_base.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ class verilog_typecheck_baset:public typecheckt
3838
protected:
3939
const namespacet &ns;
4040
const irep_idt mode;
41-
42-
std::size_t get_width(const exprt &expr) { return get_width(expr.type()); }
43-
std::size_t get_width(const typet &type);
41+
42+
mp_integer get_width(const exprt &expr)
43+
{
44+
return get_width(expr.type());
45+
}
46+
mp_integer get_width(const typet &type);
4447
mp_integer array_size(const array_typet &);
4548
mp_integer array_offset(const array_typet &);
4649
typet index_type(const array_typet &);

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ exprt verilog_typecheck_exprt::convert_expr_rec(exprt expr)
266266
throw errort().with_location(expr.source_location())
267267
<< "concatenation expected to have at least one operand";
268268
}
269-
270-
unsigned width=0;
269+
270+
mp_integer width = 0;
271271
bool has_verilogbv=false;
272272

273273
Forall_operands(it, expr)
@@ -300,23 +300,24 @@ exprt verilog_typecheck_exprt::convert_expr_rec(exprt expr)
300300
if(op.type().id() == ID_signedbv || op.type().id() == ID_verilog_signedbv)
301301
{
302302
auto width = get_width(op);
303+
auto width_int = numeric_cast_v<std::size_t>(width);
303304
if(op.type().id() == ID_verilog_signedbv)
304-
op = typecast_exprt(op, verilog_unsignedbv_typet(width));
305+
op = typecast_exprt{op, verilog_unsignedbv_typet{width_int}};
305306
else
306-
op = typecast_exprt(op, unsignedbv_typet(width));
307+
op = typecast_exprt{op, unsignedbv_typet{width_int}};
307308
}
308309
}
309310

310311
expr.type()=typet(has_verilogbv?ID_verilog_unsignedbv:ID_unsignedbv);
311-
expr.type().set(ID_width, width);
312-
312+
expr.type().set(ID_width, integer2string(width));
313+
313314
if(has_verilogbv)
314315
{
315316
Forall_operands(it, expr)
316317
if(it->type().id()!=ID_verilog_unsignedbv)
317318
{
318-
unsigned width=get_width(*it);
319-
*it = typecast_exprt{*it, verilog_unsignedbv_typet{width}};
319+
auto width_int = numeric_cast_v<std::size_t>(get_width(*it));
320+
*it = typecast_exprt{*it, verilog_unsignedbv_typet{width_int}};
320321
}
321322
}
322323

@@ -2045,13 +2046,14 @@ exprt verilog_typecheck_exprt::convert_replication_expr(replication_exprt expr)
20452046
{
20462047
expr.op0()=from_integer(op0, natural_typet());
20472048

2048-
std::size_t new_width = op0.to_ulong() * width;
2049+
auto new_width = op0 * width;
2050+
auto new_width_int = numeric_cast_v<std::size_t>(new_width);
20492051

20502052
if(op1.type().id()==ID_verilog_unsignedbv ||
20512053
op1.type().id()==ID_verilog_signedbv)
2052-
expr.type()=verilog_unsignedbv_typet(new_width);
2054+
expr.type() = verilog_unsignedbv_typet{new_width_int};
20532055
else
2054-
expr.type()=unsignedbv_typet(new_width);
2056+
expr.type() = unsignedbv_typet{new_width_int};
20552057
}
20562058

20572059
return std::move(expr);
@@ -2327,10 +2329,10 @@ exprt verilog_typecheck_exprt::convert_trinary_expr(ternary_exprt expr)
23272329
if(op2 < offset)
23282330
{
23292331
auto padding_width = offset - op2;
2330-
auto padding =
2331-
from_integer(0, unsignedbv_typet{(padding_width).to_ulong()});
2332-
auto new_type =
2333-
unsignedbv_typet{(get_width(op0.type()) + padding_width).to_ulong()};
2332+
auto padding = from_integer(
2333+
0, unsignedbv_typet{numeric_cast_v<std::size_t>(padding_width)});
2334+
auto new_type = unsignedbv_typet{
2335+
numeric_cast_v<std::size_t>(get_width(op0.type()) + padding_width)};
23342336
expr.op0() = concatenation_exprt(expr.op0(), padding, new_type);
23352337
op2 += padding_width;
23362338
op1 += padding_width;
@@ -2339,16 +2341,17 @@ exprt verilog_typecheck_exprt::convert_trinary_expr(ternary_exprt expr)
23392341
if(op1 >= width + offset)
23402342
{
23412343
auto padding_width = op1 - (width + offset) + 1;
2342-
auto padding =
2343-
from_integer(0, unsignedbv_typet{padding_width.to_ulong()});
2344-
auto new_type =
2345-
unsignedbv_typet{(get_width(op0.type()) + padding_width).to_ulong()};
2344+
auto padding = from_integer(
2345+
0, unsignedbv_typet{numeric_cast_v<std::size_t>(padding_width)});
2346+
auto new_type = unsignedbv_typet{
2347+
numeric_cast_v<std::size_t>(get_width(op0.type()) + padding_width)};
23462348
expr.op0() = concatenation_exprt(padding, expr.op0(), new_type);
23472349
}
23482350

23492351
// Part-select expressions are unsigned, even if the
23502352
// entire expression is selected!
2351-
auto expr_type = unsignedbv_typet((op1 - op2).to_ulong() + 1);
2353+
auto expr_type =
2354+
unsignedbv_typet{numeric_cast_v<std::size_t>(op1 - op2 + 1)};
23522355

23532356
op2 -= offset;
23542357
op1 -= offset;

src/verilog/verilog_types.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class verilog_signedbv_typet:public bitvector_typet
6565
{
6666
}
6767

68-
explicit inline verilog_signedbv_typet(unsigned width):bitvector_typet(ID_verilog_signedbv, width)
68+
explicit inline verilog_signedbv_typet(std::size_t width)
69+
: bitvector_typet(ID_verilog_signedbv, width)
6970
{
7071
}
7172
};
@@ -102,7 +103,8 @@ class verilog_unsignedbv_typet:public bitvector_typet
102103
{
103104
}
104105

105-
explicit inline verilog_unsignedbv_typet(unsigned width):bitvector_typet(ID_verilog_unsignedbv, width)
106+
explicit inline verilog_unsignedbv_typet(std::size_t width)
107+
: bitvector_typet(ID_verilog_unsignedbv, width)
106108
{
107109
}
108110
};

0 commit comments

Comments
 (0)