bool clang_c_convertert::convert_integer_literal( const clang::IntegerLiteral &integer_literal, exprt &dest) { typet type; if(get_type(integer_literal.getType(), type)) return true; assert(type.is_unsignedbv() || type.is_signedbv()); llvm::APInt val = integer_literal.getValue(); exprt the_val; if (type.is_unsignedbv()) { the_val = constant_exprt( integer2binary(val.getZExtValue(), bv_width(type)), integer2string(val.getZExtValue()), type); } else { the_val = constant_exprt( integer2binary(val.getSExtValue(), bv_width(type)), integer2string(val.getSExtValue()), type); } dest.swap(the_val); return false; }
exprt from_integer(const mp_integer &int_value, const typet &type) { exprt expr; expr.clear(); expr.type() = type; expr.id("constant"); const irep_idt &type_id = type.id(); if(type_id == "unsignedbv" || type_id == "signedbv") { expr.value(integer2binary(int_value, bv_width(type))); return expr; } if(type_id == "bool") { if(int_value == 0) { expr.make_false(); return expr; } if(int_value == 1) { expr.make_true(); return expr; } } expr.make_nil(); return expr; }
bool value_set_dereferencet::memory_model( exprt &value, const typet &to_type, const guardt &guard, const exprt &offset) { // we will allow more or less arbitrary pointer type cast const typet from_type=value.type(); // first, check if it's really just a type coercion, // i.e., both have exactly the same (constant) size if(is_a_bv_type(from_type) && is_a_bv_type(to_type)) { if(bv_width(from_type, ns)==bv_width(to_type, ns)) { // avoid semantic conversion in case of // cast to float or fixed-point, // or cast from float or fixed-point if(to_type.id()==ID_fixedbv || to_type.id()==ID_floatbv || from_type.id()==ID_fixedbv || from_type.id()==ID_floatbv) { } else return memory_model_conversion(value, to_type, guard, offset); } } // we are willing to do the same for pointers if(from_type.id()==ID_pointer && to_type.id()==ID_pointer) { if(bv_width(from_type, ns)==bv_width(to_type, ns)) return memory_model_conversion(value, to_type, guard, offset); } // otherwise, we will stitch it together from bytes return memory_model_bytes(value, to_type, guard, offset); }
inline static unsigned bv_width( const typet &type, const namespacet &ns) { if(type.id()==ID_c_enum_tag) { const typet &t=ns.follow_tag(to_c_enum_tag_type(type)); assert(t.id()==ID_c_enum); return bv_width(t.subtype(), ns); } return unsafe_string2unsigned(type.get_string(ID_width)); }
bool clang_c_convertert::convert_character_literal( const clang::CharacterLiteral &char_literal, exprt &dest) { typet type; if(get_type(char_literal.getType(), type)) return true; dest = constant_exprt( integer2binary(char_literal.getValue(), bv_width(type)), integer2string(char_literal.getValue()), type); return false; }
exprt gen_zero(const typet &type) { exprt result; const std::string type_id=type.id_string(); result=exprt("constant", type); if(type_id=="rational" || type_id=="real" || type_id=="integer" || type_id=="natural" || type_id=="complex") { result.value("0"); } else if(type_id=="unsignedbv" || type_id=="signedbv" || type_id=="floatbv" || type_id=="fixedbv" || type_id=="c_enum") { std::string value; unsigned width=bv_width(type); for(unsigned i=0; i<width; i++) value+='0'; result.value(value); } else if(type_id=="bool") { result.make_false(); } else if(type_id=="pointer") { result.value("NULL"); } else result.make_nil(); return result; }
bool is_string_type(const typet &t) const { return ((t.id()=="pointer" || t.id()=="array") && (t.subtype().id()=="signedbv" || t.subtype().id()=="unsignedbv") && (bv_width(t.subtype())==config.ansi_c.char_width)); }
bool clang_c_convertert::convert_float_literal( const clang::FloatingLiteral &floating_literal, exprt &dest) { if(!config.ansi_c.use_fixed_for_float) { std::cerr << "floatbv unsupported, sorry" << std::endl; return false; } typet type; if(get_type(floating_literal.getType(), type)) return true; llvm::APFloat val = floating_literal.getValue(); llvm::SmallVector<char, 32> string; val.toString(string, 32, 0); unsigned width = bv_width(type); mp_integer value; std::string float_string; if(!val.isInfinity()) { mp_integer significand; mp_integer exponent; float_string = parse_float(string, significand, exponent); unsigned fraction_bits; const std::string &integer_bits = type.integer_bits().as_string(); if (integer_bits == "") fraction_bits = width / 2; else fraction_bits = width - atoi(integer_bits.c_str()); mp_integer factor = mp_integer(1) << fraction_bits; value = significand * factor; if(exponent < 0) value /= power(10, -exponent); else { value *= power(10, exponent); if(value <= -power(2, width - 1) - 1) { // saturate: use "smallest value" value = -power(2, width - 1); } } } else { // saturate: use "biggest value" value = power(2, width - 1) - 1; float_string = "2147483647.99999999976716935634613037109375"; } dest = constant_exprt( integer2binary(value, bv_width(type)), float_string, type); return false; }