Skip to content

Commit 6483d2f

Browse files
author
Daniel Kroening
committed
smt2: implement the cast from bv to floatbv
1 parent 0fcc504 commit 6483d2f

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

src/solvers/smt2/smt2_conv.cpp

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,19 @@ std::string smt2_convt::type2id(const typet &type) const
812812
{
813813
return type2id(ns.follow_tag(to_c_enum_tag_type(type)).subtype());
814814
}
815+
else if(type.id() == ID_c_bit_field)
816+
{
817+
const auto &c_bit_field_type = to_c_bit_field_type(type);
818+
// use underlying type
819+
if(c_bit_field_type.subtype().id() == ID_unsignedbv)
820+
return type2id(unsignedbv_typet(c_bit_field_type.get_width()));
821+
else if(c_bit_field_type.subtype().id() == ID_signedbv)
822+
return type2id(signedbv_typet(c_bit_field_type.get_width()));
823+
else if(c_bit_field_type.subtype().id() == ID_c_bool)
824+
return type2id(c_bool_typet(c_bit_field_type.get_width()));
825+
else
826+
UNIMPLEMENTED;
827+
}
815828
else
816829
{
817830
UNREACHABLE;
@@ -2352,46 +2365,28 @@ void smt2_convt::convert_typecast(const typecast_exprt &expr)
23522365
}
23532366
else if(dest_type.id()==ID_floatbv)
23542367
{
2368+
const auto &dest_floatbv_type = to_floatbv_type(dest_type);
2369+
23552370
// Typecast from integer to floating-point should have be been
23562371
// converted to ID_floatbv_typecast during symbolic execution,
23572372
// adding the rounding mode. See
23582373
// smt2_convt::convert_floatbv_typecast.
2359-
// The exception is bool and c_bool to float.
2374+
// The exceptions are bool and c_bool to float,
2375+
// and non-semantic conversion.
23602376

2361-
if(src_type.id()==ID_bool)
2377+
if(src_type.id() == ID_bool || src_type.id() == ID_c_bool)
23622378
{
2363-
constant_exprt val(irep_idt(), dest_type);
2364-
2365-
ieee_floatt a(to_floatbv_type(dest_type));
2366-
2367-
mp_integer significand;
2368-
mp_integer exponent;
2369-
2370-
out << "(ite ";
2371-
convert_expr(src);
2372-
out << " ";
2373-
2374-
significand = 1;
2375-
exponent = 0;
2376-
a.build(significand, exponent);
2377-
val.set(ID_value, integer2binary(a.pack(), a.spec.width()));
2378-
2379-
convert_constant(val);
2380-
out << " ";
2381-
2382-
significand = 0;
2383-
exponent = 0;
2384-
a.build(significand, exponent);
2385-
val.set(ID_value, integer2binary(a.pack(), a.spec.width()));
2386-
2387-
convert_constant(val);
2388-
out << ")";
2379+
convert_expr(float_bv(expr));
23892380
}
2390-
else if(src_type.id()==ID_c_bool)
2381+
else if(src_type.id() == ID_bv)
23912382
{
2392-
// turn into proper bool
2393-
const typecast_exprt tmp(src, bool_typet());
2394-
convert_typecast(typecast_exprt(tmp, dest_type));
2383+
if(to_bv_type(src_type).get_width() == dest_floatbv_type.get_width())
2384+
{
2385+
// preserve representation, this just changes the type
2386+
convert_expr(src);
2387+
}
2388+
else
2389+
UNEXPECTEDCASE("typecast bv -> float with wrong width");
23952390
}
23962391
else
23972392
UNEXPECTEDCASE("Unknown typecast "+src_type.id_string()+" -> float");
@@ -2429,7 +2424,7 @@ void smt2_convt::convert_typecast(const typecast_exprt &expr)
24292424
void smt2_convt::convert_floatbv_typecast(const floatbv_typecast_exprt &expr)
24302425
{
24312426
const exprt &src=expr.op();
2432-
// const exprt &rounding_mode=expr.rounding_mode();
2427+
const exprt &rounding_mode = expr.rounding_mode();
24332428
const typet &src_type=src.type();
24342429
const typet &dest_type=expr.type();
24352430

@@ -2533,6 +2528,14 @@ void smt2_convt::convert_floatbv_typecast(const floatbv_typecast_exprt &expr)
25332528
ns.follow_tag(to_c_enum_tag_type(src_type)).subtype());
25342529
convert_floatbv_typecast(tmp);
25352530
}
2531+
else if(src_type.id() == ID_c_bit_field)
2532+
{
2533+
// go through underlying type
2534+
convert_floatbv_typecast(floatbv_typecast_exprt(
2535+
typecast_exprt(src, to_c_bit_field_type(src_type).subtype()),
2536+
rounding_mode,
2537+
dest_type));
2538+
}
25362539
else
25372540
UNEXPECTEDCASE(
25382541
"TODO typecast11 "+src_type.id_string()+" -> "+dest_type.id_string());

0 commit comments

Comments
 (0)