Пример #1
0
void path_slicert::get_symbols(const exprt &expr, string_sett &s)
{
    if(expr.id()==ID_symbol)
    {
        s.insert(to_symbol_expr(expr).get_identifier());
    }
    else if(expr.id()==ID_index)
    {
        assert(expr.operands().size()==2);
        string_sett tmp;
        get_symbols(expr.op0(), tmp);
        get_symbols(expr.op1(), s);

        for(string_sett::const_iterator
                it=tmp.begin();
                it!=tmp.end();
                it++)
            s.insert(id2string(*it)+"[]");
    }
    else if(expr.id()==ID_member)
    {
        assert(expr.operands().size()==1);

        string_sett tmp;

        get_symbols(expr.op0(), tmp);

        std::string suffix="."+expr.get_string(ID_component_name);

        for(string_sett::const_iterator
                it=tmp.begin();
                it!=tmp.end();
                it++)
            s.insert(id2string(*it)+suffix);
    }
    else
        forall_operands(it, expr)
        get_symbols(*it, s);
}
Пример #2
0
exprt ranking_synthesis_qbf_bitwiset::coefficient(const exprt &expr)
{
  assert(expr.id()==ID_symbol);

  exprt &entry = coefficient_map[expr];

  if(entry==exprt())
  {
    irep_idt ident=expr.get_string(ID_identifier) + "$C";

    // set up a new coefficient
    entry.id(ID_symbol);
    entry.set(ID_identifier, ident);

    // adjust the coefficient type
    entry.type()=expr.type();

    assert(expr.type().id()==ID_signedbv ||
           expr.type().id()==ID_unsignedbv ||
           expr.type().id()==ID_bool);
  }

  return entry;
}
Пример #3
0
std::string as_vcd_binary(
  const exprt &expr,
  const namespacet &ns)
{
  const typet &type=ns.follow(expr.type());

  if(expr.id()==ID_constant)
  {
    if(type.id()==ID_unsignedbv ||
       type.id()==ID_signedbv ||
       type.id()==ID_bv ||
       type.id()==ID_fixedbv ||
       type.id()==ID_floatbv ||
       type.id()==ID_pointer)
      return expr.get_string(ID_value);
  }
  else if(expr.id()==ID_array)
  {
    std::string result;

    forall_operands(it, expr)
      result+=as_vcd_binary(*it, ns);

    return result;
  }
  else if(expr.id()==ID_struct)
  {
    std::string result;

    forall_operands(it, expr)
      result+=as_vcd_binary(*it, ns);

    return result;
  }
  else if(expr.id()==ID_union)
  {
    assert(expr.operands().size()==1);
    return as_vcd_binary(expr.op0(), ns);
  }

  // build "xxx"

  mp_integer width;

  if(type.id()==ID_unsignedbv ||
     type.id()==ID_signedbv ||
     type.id()==ID_floatbv ||
     type.id()==ID_fixedbv ||
     type.id()==ID_pointer ||
     type.id()==ID_bv)
    width=string2integer(type.get_string(ID_width));
  else
    width=pointer_offset_size(type, ns)*8;

  if(width>=0)
  {
    std::string result;

    for(; width!=0; --width)
      result+='x';

    return result;
  }

  return "";
}
Пример #4
0
void value_sett::get_value_set_rec(
  const exprt &expr,
  object_mapt &dest,
  const std::string &suffix,
  const typet &original_type,
  const namespacet &ns) const
{
  #if 0
  std::cout << "GET_VALUE_SET_REC EXPR: " << from_expr(ns, "", expr) << "\n";
  std::cout << "GET_VALUE_SET_REC SUFFIX: " << suffix << std::endl;
  #endif

  const typet &expr_type=ns.follow(expr.type());

  if(expr.id()==ID_unknown || expr.id()==ID_invalid)
  {
    insert(dest, exprt(ID_unknown, original_type));
  }
  else if(expr.id()==ID_index)
  {
    assert(expr.operands().size()==2);

    const typet &type=ns.follow(expr.op0().type());

    assert(type.id()==ID_array ||
           type.id()==ID_incomplete_array);

    get_value_set_rec(expr.op0(), dest, "[]"+suffix, original_type, ns);
  }
  else if(expr.id()==ID_member)
  {
    assert(expr.operands().size()==1);

    const typet &type=ns.follow(expr.op0().type());

    assert(type.id()==ID_struct ||
           type.id()==ID_union ||
           type.id()==ID_incomplete_struct ||
           type.id()==ID_incomplete_union);

    const std::string &component_name=
      expr.get_string(ID_component_name);

    get_value_set_rec(expr.op0(), dest,
      "."+component_name+suffix, original_type, ns);
  }
  else if(expr.id()==ID_symbol)
  {
    irep_idt identifier=to_symbol_expr(expr).get_identifier();

    // is it a pointer, integer, array or struct?
    if(expr_type.id()==ID_pointer ||
       expr_type.id()==ID_signedbv ||
       expr_type.id()==ID_unsignedbv ||
       expr_type.id()==ID_struct ||
       expr_type.id()==ID_union ||
       expr_type.id()==ID_array)
    {
      // look it up
      valuest::const_iterator v_it=
        values.find(id2string(identifier)+suffix);

      // try first component name as suffix if not yet found
      if(v_it==values.end() &&
          (expr_type.id()==ID_struct ||
           expr_type.id()==ID_union))
      {
        const struct_union_typet &struct_union_type=
          to_struct_union_type(expr_type);

        const std::string first_component_name=
          struct_union_type.components().front().get_string(ID_name);

        v_it=values.find(
            id2string(identifier)+"."+first_component_name+suffix);
      }

      // not found? try without suffix
      if(v_it==values.end())
        v_it=values.find(identifier);

      if(v_it!=values.end())
        make_union(dest, v_it->second.object_map);
      else
        insert(dest, exprt(ID_unknown, original_type));
    }
    else
      insert(dest, exprt(ID_unknown, original_type));
  }
  else if(expr.id()==ID_if)
  {
    if(expr.operands().size()!=3)
      throw "if takes three operands";

    get_value_set_rec(expr.op1(), dest, suffix, original_type, ns);
    get_value_set_rec(expr.op2(), dest, suffix, original_type, ns);
  }
  else if(expr.id()==ID_address_of)
  {
    if(expr.operands().size()!=1)
      throw expr.id_string()+" expected to have one operand";

    get_reference_set(expr.op0(), dest, ns);
  }
  else if(expr.id()==ID_dereference)
  {
    object_mapt reference_set;
    get_reference_set(expr, reference_set, ns);
    const object_map_dt &object_map=reference_set.read();

    if(object_map.begin()==object_map.end())
      insert(dest, exprt(ID_unknown, original_type));
    else
    {
      for(object_map_dt::const_iterator
          it1=object_map.begin();
          it1!=object_map.end();
          it1++)
      {
        const exprt &object=object_numbering[it1->first];
        get_value_set_rec(object, dest, suffix, original_type, ns);
      }
    }
  }
  else if(expr.id()=="reference_to")
  {
    // old stuff, will go away
    object_mapt reference_set;

    get_reference_set(expr, reference_set, ns);

    const object_map_dt &object_map=reference_set.read();

    if(object_map.begin()==object_map.end())
      insert(dest, exprt(ID_unknown, original_type));
    else
    {
      for(object_map_dt::const_iterator
          it=object_map.begin();
          it!=object_map.end();
          it++)
      {
        const exprt &object=object_numbering[it->first];
        get_value_set_rec(object, dest, suffix, original_type, ns);
      }
    }
  }
  else if(expr.is_constant())
  {
    // check if NULL
    if(expr.get(ID_value)==ID_NULL &&
       expr_type.id()==ID_pointer)
    {
      insert(dest, exprt("NULL-object", expr_type.subtype()), 0);
    }
    else if(expr_type.id()==ID_unsignedbv ||
            expr_type.id()==ID_signedbv)
    {
      // an integer constant got turned into a pointer
      insert(dest, exprt(ID_integer_address, unsigned_char_type()));
    }
    else
      insert(dest, exprt(ID_unknown, original_type));
  }
  else if(expr.id()==ID_typecast)
  {
    if(expr.operands().size()!=1)
      throw "typecast takes one operand";

    // let's see what gets converted to what

    const typet &op_type=ns.follow(expr.op0().type());

    if(op_type.id()==ID_pointer)
    {
      // pointer-to-pointer -- we just ignore these
      get_value_set_rec(expr.op0(), dest, suffix, original_type, ns);
    }
    else if(op_type.id()==ID_unsignedbv ||
            op_type.id()==ID_signedbv)
    {
      // integer-to-pointer

      if(expr.op0().is_zero())
        insert(dest, exprt("NULL-object", expr_type.subtype()), 0);
      else
      {
        // see if we have something for the integer
        object_mapt tmp;

        get_value_set_rec(expr.op0(), tmp, suffix, original_type, ns);

        if(tmp.read().size()==0)
        {
          // if not, throw in integer
          insert(dest, exprt(ID_integer_address, unsigned_char_type()));
        }
        else if(tmp.read().size()==1 &&
                object_numbering[tmp.read().begin()->first].id()==ID_unknown)
        {
          // if not, throw in integer
          insert(dest, exprt(ID_integer_address, unsigned_char_type()));
        }
        else
        {
          // use as is
          dest.write().insert(tmp.read().begin(), tmp.read().end());
        }
      }
    }
    else
      insert(dest, exprt(ID_unknown, original_type));
  }
  else if(expr.id()==ID_plus ||
          expr.id()==ID_minus)
  {
    if(expr.operands().size()<2)
      throw expr.id_string()+" expected to have at least two operands";

    object_mapt pointer_expr_set;
    mp_integer i;
    bool i_is_set=false;

    // special case for pointer+integer

    if(expr.operands().size()==2 &&
       expr_type.id()==ID_pointer)
    {
      exprt ptr_operand;

      if(expr.op0().type().id()!=ID_pointer &&
         expr.op0().is_constant())
      {
        i_is_set=!to_integer(expr.op0(), i);
        ptr_operand=expr.op1();
      }
      else
      {
        i_is_set=!to_integer(expr.op1(), i);
        ptr_operand=expr.op0();
      }

      if(i_is_set)
      {
        i*=pointer_offset_size(ptr_operand.type().subtype(), ns);

        if(expr.id()==ID_minus) i.negate();
      }

      get_value_set_rec(
        ptr_operand, pointer_expr_set, "", ptr_operand.type(), ns);
    }
    else
    {
      // we get the points-to for all operands, even integers
      forall_operands(it, expr)
      {
        get_value_set_rec(
          *it, pointer_expr_set, "", it->type(), ns);
      }
    }

    for(object_map_dt::const_iterator
        it=pointer_expr_set.read().begin();
        it!=pointer_expr_set.read().end();
        it++)
    {
      objectt object=it->second;

      // adjust by offset
      if(object.offset_is_zero() && i_is_set)
        object.offset=i;
      else
        object.offset_is_set=false;

      insert(dest, it->first, object);
    }
  }
Пример #5
0
xmlt xml(
  const exprt &expr,
  const namespacet &ns)
{
  xmlt result;
  
  const typet &type=ns.follow(expr.type());

  if(expr.id()==ID_constant)
  {
    if(type.id()==ID_unsignedbv ||
       type.id()==ID_signedbv ||
       type.id()==ID_c_bit_field)
    {
      std::size_t width=to_bitvector_type(type).get_width();
    
      result.name="integer";
      result.set_attribute("binary", expr.get_string(ID_value));
      result.set_attribute("width", width);
      
      const typet &underlying_type=
        type.id()==ID_c_bit_field?type.subtype():
        type;

      bool is_signed=underlying_type.id()==ID_signedbv;
        
      std::string sig=is_signed?"":"unsigned ";

      if(width==config.ansi_c.char_width)
        result.set_attribute("c_type", sig+"char");
      else if(width==config.ansi_c.int_width)
        result.set_attribute("c_type", sig+"int");
      else if(width==config.ansi_c.short_int_width)
        result.set_attribute("c_type", sig+"short int");
      else if(width==config.ansi_c.long_int_width)
        result.set_attribute("c_type", sig+"long int");
      else if(width==config.ansi_c.long_long_int_width)
        result.set_attribute("c_type", sig+"long long int");

      mp_integer i;
      if(!to_integer(expr, i))
        result.data=integer2string(i);
    }
    else if(type.id()==ID_c_enum)
    {
      result.name="integer";
      result.set_attribute("binary", expr.get_string(ID_value));
      result.set_attribute("width", type.subtype().get_string(ID_width));
      result.set_attribute("c_type", "enum");

      mp_integer i;
      if(!to_integer(expr, i))
        result.data=integer2string(i);
    }
    else if(type.id()==ID_c_enum_tag)
    {
      constant_exprt tmp;
      tmp.type()=ns.follow_tag(to_c_enum_tag_type(type));
      tmp.set_value(to_constant_expr(expr).get_value());
      return xml(tmp, ns);
    }
    else if(type.id()==ID_bv)
    {
      result.name="bitvector";
      result.set_attribute("binary", expr.get_string(ID_value));
    }
    else if(type.id()==ID_fixedbv)
    {
      result.name="fixed";
      result.set_attribute("width", type.get_string(ID_width));
      result.set_attribute("binary", expr.get_string(ID_value));
      result.data=fixedbvt(to_constant_expr(expr)).to_ansi_c_string();
    }
    else if(type.id()==ID_floatbv)
    {
      result.name="float";
      result.set_attribute("width", type.get_string(ID_width));
      result.set_attribute("binary", expr.get_string(ID_value));
      result.data=ieee_floatt(to_constant_expr(expr)).to_ansi_c_string();
    }
    else if(type.id()==ID_pointer)
    {
      result.name="pointer";
      result.set_attribute("binary", expr.get_string(ID_value));
      if(expr.get(ID_value)==ID_NULL)
        result.data="NULL";
    }
    else if(type.id()==ID_bool)
    {
      result.name="boolean";
      result.set_attribute("binary", expr.is_true()?"1":"0");
      result.data=expr.is_true()?"TRUE":"FALSE";
    }
    else
    {
      result.name="unknown";
    }
  }
  else if(expr.id()==ID_array)
  {
    result.name="array";
    
    unsigned index=0;
    
    forall_operands(it, expr)
    {
      xmlt &e=result.new_element("element");
      e.set_attribute("index", index);
      e.new_element(xml(*it, ns));
      index++;
    }
Пример #6
0
xmlt xml(
  const exprt &expr,
  const namespacet &ns)
{
  const typet &type=ns.follow(expr.type());
  xmlt result;
  
  if(expr.id()==ID_constant)
  {
    if(type.id()==ID_unsignedbv ||
       type.id()==ID_signedbv)
    {
      result.name="integer";
      result.set_attribute("binary", expr.get_string(ID_value));

      mp_integer i;
      if(!to_integer(expr, i))
        result.data=integer2string(i);
    }
    else if(type.id()==ID_bv)
    {
      result.name="bitvector";
      result.set_attribute("binary", expr.get_string(ID_value));
      result.data=expr.get_string(ID_value);
    }
    else if(type.id()==ID_fixedbv)
    {
      result.name="fixed";
      result.set_attribute("binary", expr.get_string(ID_value));
      result.data=fixedbvt(to_constant_expr(expr)).to_ansi_c_string();
    }
    else if(type.id()==ID_floatbv)
    {
      result.name="float";
      result.set_attribute("binary", expr.get_string(ID_value));
      result.data=ieee_floatt(to_constant_expr(expr)).to_ansi_c_string();
    }
    else if(type.id()==ID_pointer)
    {
      result.name="pointer";
      result.set_attribute("binary", expr.get_string(ID_value));
      if(expr.get(ID_value)==ID_NULL)
        result.data="NULL";
    }
    else if(type.id()==ID_bool)
    {
      result.name="boolean";
      result.set_attribute("binary", expr.is_true()?"1":"0");
      result.data=expr.is_true()?"TRUE":"FALSE";
    }
    else
    {
      result.name="unknown";
    }
  }
  else if(expr.id()==ID_array)
  {
    result.name="array";
    
    unsigned index=0;
    
    forall_operands(it, expr)
    {
      xmlt &e=result.new_element("element");
      e.set_attribute("index", index);
      e.new_element(xml(*it, ns));
      index++;
    }
Пример #7
0
void dplib_convt::convert_dplib_expr(const exprt &expr)
{
    if(expr.id()==ID_symbol)
    {
        convert_identifier(expr.get_string(ID_identifier));
    }
    else if(expr.id()==ID_nondet_symbol)
    {
        convert_identifier("nondet$"+expr.get_string(ID_identifier));
    }
    else if(expr.id()==ID_typecast)
    {
        assert(expr.operands().size()==1);
        const exprt &op=expr.op0();

        if(expr.type().id()==ID_bool)
        {
            if(op.type().id()==ID_signedbv ||
                    op.type().id()==ID_unsignedbv ||
                    op.type().id()==ID_pointer)
            {
                convert_dplib_expr(op);
                dplib_prop.out << "/=";
                convert_dplib_expr(gen_zero(op.type()));
            }
            else
            {
                throw "TODO typecast1 "+op.type().id_string()+" -> bool";
            }
        }
        else if(expr.type().id()==ID_signedbv ||
                expr.type().id()==ID_unsignedbv)
        {
            unsigned to_width=unsafe_string2unsigned(id2string(expr.type().get(ID_width)));

            if(op.type().id()==ID_signedbv)
            {
                unsigned from_width=unsafe_string2unsigned(id2string(op.type().get(ID_width)));

                if(from_width==to_width)
                    convert_dplib_expr(op);
                else if(from_width<to_width)
                {
                    dplib_prop.out << "SX(";
                    convert_dplib_expr(op);
                    dplib_prop.out << ", " << to_width << ")";
                }
                else
                {
                    dplib_prop.out << "(";
                    convert_dplib_expr(op);
                    dplib_prop.out << ")[" << (to_width-1) << ":0]";
                }
            }
            else if(op.type().id()==ID_unsignedbv)
            {
                unsigned from_width=unsafe_string2unsigned(id2string(op.type().get(ID_width)));

                if(from_width==to_width)
                    convert_dplib_expr(op);
                else if(from_width<to_width)
                {
                    dplib_prop.out << "(0bin";

                    for(unsigned i=from_width; i<to_width; i++)
                        dplib_prop.out << "0";

                    dplib_prop.out << " @ ";

                    dplib_prop.out << "(";
                    convert_dplib_expr(op);
                    dplib_prop.out << "))";
                }
                else
                {
                    dplib_prop.out << "(";
                    convert_dplib_expr(op);
                    dplib_prop.out << ")[" << (to_width-1) << ":0]";
                }
            }
            else if(op.type().id()==ID_bool)
            {
                if(to_width>1)
                {
                    dplib_prop.out << "(0bin";

                    for(unsigned i=1; i<to_width; i++)
                        dplib_prop.out << "0";

                    dplib_prop.out << " @ ";

                    dplib_prop.out << "IF ";
                    convert_dplib_expr(op);
                    dplib_prop.out << " THEN 0bin1 ELSE 0bin0 ENDIF)";
                }
                else
                {
                    dplib_prop.out << "IF ";
                    convert_dplib_expr(op);
                    dplib_prop.out << " THEN 0bin1 ELSE 0bin0 ENDIF";
                }
            }
            else
            {
                throw "TODO typecast2 "+op.type().id_string()+
                " -> "+expr.type().id_string();
            }
        }
        else if(expr.type().id()==ID_pointer)
        {
            if(op.type().id()==ID_pointer)
            {
                convert_dplib_expr(op);
            }
            else
                throw "TODO typecast3 "+op.type().id_string()+" -> pointer";
        }
        else
            throw "TODO typecast4 ? -> "+expr.type().id_string();
    }
    else if(expr.id()==ID_struct)
    {
        dplib_prop.out << "(# ";

        const struct_typet &struct_type=to_struct_type(expr.type());

        const struct_typet::componentst &components=
            struct_type.components();

        assert(components.size()==expr.operands().size());

        unsigned i=0;
        for(struct_typet::componentst::const_iterator
                it=components.begin();
                it!=components.end();
                it++, i++)
        {
            if(i!=0) dplib_prop.out << ", ";
            dplib_prop.out << it->get(ID_name);
            dplib_prop.out << ":=";
            convert_dplib_expr(expr.operands()[i]);
        }

        dplib_prop.out << " #)";
    }
    else if(expr.id()==ID_constant)
    {
        if(expr.type().id()==ID_unsignedbv ||
                expr.type().id()==ID_signedbv ||
                expr.type().id()==ID_bv)
        {
            dplib_prop.out << "0bin" << expr.get(ID_value);
        }
        else if(expr.type().id()==ID_pointer)
        {
            const irep_idt &value=expr.get(ID_value);

            if(value=="NULL")
            {
                dplib_prop.out << "(# object:="
                               << pointer_logic.get_null_object()
                               << ", offset:="
                               << bin_zero(config.ansi_c.pointer_width) << " #)";
            }
            else
                throw "unknown pointer constant: "+id2string(value);
        }
        else if(expr.type().id()==ID_bool)
        {
            if(expr.is_true())
                dplib_prop.out << "TRUE";
            else if(expr.is_false())
                dplib_prop.out << "FALSE";
            else
                throw "unknown boolean constant";
        }
        else if(expr.type().id()==ID_array)
        {
            dplib_prop.out << "ARRAY (i: " << array_index_type() << "):";

            assert(!expr.operands().empty());

            unsigned i=0;
            forall_operands(it, expr)
            {
                if(i==0)
                    dplib_prop.out << "\n  IF ";
                else
                    dplib_prop.out << "\n  ELSIF ";

                dplib_prop.out << "i=" << array_index(i) << " THEN ";
                convert_array_value(*it);
                i++;
            }

            dplib_prop.out << "\n  ELSE ";
            convert_dplib_expr(expr.op0());
            dplib_prop.out << "\n  ENDIF";
        }
        else if(expr.type().id()==ID_integer ||
Пример #8
0
void bv_arithmetict::from_expr(const exprt &expr)
{
  assert(expr.is_constant());
  spec=expr.type();
  value=binary2integer(expr.get_string(ID_value), spec.is_signed);
}
Пример #9
0
void ieee_floatt::from_expr(const exprt &expr)
{
  assert(expr.is_constant());
  spec=to_floatbv_type(expr.type());
  unpack(binary2integer(expr.get_string(ID_value), false));
}
Пример #10
0
bool path_slicert::depends_lhs_rec(
    const exprt &expr,
    const string_sett &suffixes_r,
    const string_sett &suffixes_w,
    string_sett &dependencies,
    string_sett &new_dependencies)
{
    if(expr.id()==ID_symbol)
    {
        string_sett tmp_r, tmp_w;

        const irep_idt &id=to_symbol_expr(expr).get_identifier();

        for(string_sett::const_iterator
                it=suffixes_w.begin();
                it!=suffixes_w.end();
                it++)
            tmp_w.insert(id2string(id)+id2string(*it));

        for(string_sett::const_iterator
                it=suffixes_r.begin();
                it!=suffixes_r.end();
                it++)
        {
            tmp_r.insert(id2string(id)+id2string(*it));
#ifdef DEBUG
            std::cout << "tmp_r: " << id2string(id)+id2string(*it) << std::endl;
#endif
        }

        // do we depend on it?
        if(!depends(tmp_r, dependencies))
        {
#ifdef DEBUG
            std::cout << "NO DEPN\n";
#endif
            return false; // no!
        }

#ifdef DEBUG
        std::cout << "DEP!\n";
#endif

        // yes! but we no longer do.
        for(string_sett::const_iterator it=tmp_w.begin();
                it!=tmp_w.end();
                it++)
            dependencies.erase(*it);
    }
    else if(expr.id()==ID_index)
    {
        assert(expr.operands().size()==2);
        get_symbols(expr.op0(), new_dependencies);
        get_symbols(expr.op1(), new_dependencies);

        string_sett tmp_r, tmp_w;

        for(string_sett::const_iterator it=suffixes_r.begin();
                it!=suffixes_r.end();
                it++)
        {
            tmp_r.insert(*it);
            tmp_r.insert("[]"+id2string(*it));
        }

        // can't get rid of array depenencies
        // thus, tmp_w stays empty

        return depends_lhs_rec(expr.op0(), tmp_r, tmp_w, dependencies, new_dependencies);
    }
    else if(expr.id()==ID_member)
    {
        assert(expr.operands().size()==1);

        string_sett tmp_r, tmp_w;

        for(string_sett::const_iterator it=suffixes_r.begin();
                it!=suffixes_r.end();
                it++)
        {
            tmp_r.insert(*it);
            tmp_r.insert("."+expr.get_string("component_name")+id2string(*it));
        }

        for(string_sett::const_iterator it=suffixes_w.begin();
                it!=suffixes_w.end();
                it++)
        {
            tmp_w.insert("."+expr.get_string("component_name")+id2string(*it));
        }

        return depends_lhs_rec(expr.op0(), tmp_r, tmp_w, dependencies, new_dependencies);
    }

    return true;
}
Пример #11
0
json_objectt json(
  const exprt &expr,
  const namespacet &ns)
{
  json_objectt result;
  
  const typet &type=ns.follow(expr.type());

  if(expr.id()==ID_constant)
  {
    if(type.id()==ID_unsignedbv ||
       type.id()==ID_signedbv ||
       type.id()==ID_c_bit_field)
    {
      std::size_t width=to_bitvector_type(type).get_width();
    
      result["name"]=json_stringt("integer");
      result["binary"]=json_stringt(expr.get_string(ID_value));
      result["width"]=json_numbert(i2string(width));
      
      const typet &underlying_type=
        type.id()==ID_c_bit_field?type.subtype():
        type;

      bool is_signed=underlying_type.id()==ID_signedbv;
        
      std::string sig=is_signed?"":"unsigned ";

      if(width==config.ansi_c.char_width)
        result["c_type"]=json_stringt(sig+"char");
      else if(width==config.ansi_c.int_width)
        result["c_type"]=json_stringt(sig+"int");
      else if(width==config.ansi_c.short_int_width)
        result["c_type"]=json_stringt(sig+"short int");
      else if(width==config.ansi_c.long_int_width)
        result["c_type"]=json_stringt(sig+"long int");
      else if(width==config.ansi_c.long_long_int_width)
        result["c_type"]=json_stringt(sig+"long long int");

      mp_integer i;
      if(!to_integer(expr, i))
        result["data"]=json_stringt(integer2string(i));
    }
    else if(type.id()==ID_c_enum)
    {
      result["name"]=json_stringt("integer");
      result["binary"]=json_stringt(expr.get_string(ID_value));
      result["width"]=json_numbert(type.subtype().get_string(ID_width));
      result["c_type"]=json_stringt("enum");

      mp_integer i;
      if(!to_integer(expr, i))
        result["data"]=json_stringt(integer2string(i));
    }
    else if(type.id()==ID_c_enum_tag)
    {
      constant_exprt tmp;
      tmp.type()=ns.follow_tag(to_c_enum_tag_type(type));
      tmp.set_value(to_constant_expr(expr).get_value());
      return json(tmp, ns);
    }
    else if(type.id()==ID_bv)
    {
      result["name"]=json_stringt("bitvector");
      result["binary"]=json_stringt(expr.get_string(ID_value));
    }
    else if(type.id()==ID_fixedbv)
    {
      result["name"]=json_stringt("fixed");
      result["width"]=json_numbert(type.get_string(ID_width));
      result["binary"]=json_stringt(expr.get_string(ID_value));
      result["data"]=
        json_stringt(fixedbvt(to_constant_expr(expr)).to_ansi_c_string());
    }
    else if(type.id()==ID_floatbv)
    {
      result["name"]=json_stringt("float");
      result["width"]=json_numbert(type.get_string(ID_width));
      result["binary"]=json_stringt(expr.get_string(ID_value));
      result["data"]=
        json_stringt(ieee_floatt(to_constant_expr(expr)).to_ansi_c_string());
    }
    else if(type.id()==ID_pointer)
    {
      result["name"]=json_stringt("pointer");
      result["binary"]=json_stringt(expr.get_string(ID_value));
      if(expr.get(ID_value)==ID_NULL)
        result["data"]=json_stringt("NULL");
    }
    else if(type.id()==ID_bool)
    {
      result["name"]=json_stringt("boolean");
      result["binary"]=json_stringt(expr.is_true()?"1":"0");
      result["data"]=jsont::json_boolean(expr.is_true());
    }
    else
    {
      result["name"]=json_stringt("unknown");
    }
  }
  else if(expr.id()==ID_array)
  {
    result["name"]=json_stringt("array");
    json_arrayt &elements=result["elements"].make_array();
    
    unsigned index=0;
    
    forall_operands(it, expr)
    {
      json_objectt &e=elements.push_back().make_object();
      e["index"]=json_numbert(i2string(index));
      e["value"]=json(*it, ns);
      index++;
    }