18 #define LOG(message, irep) \ 19 debug() << "Case " << __LINE__ << " : " << message << "\n" \ 20 << irep.pretty() << eom; 33 symbol_table(symbol_table)
48 const exprt &base_expression,
64 const exprt &expression)
const 66 if(expression.
id()==ID_symbol)
72 if(symbol.
type.
id()!=ID_code)
89 exprt const_symbol_cleared_expr=expression;
90 const_symbol_cleared_expr.
operands().clear();
94 const_symbol_cleared_expr.
operands().push_back(const_symbol_cleared_op);
97 return const_symbol_cleared_expr;
123 assert(out_functions.empty());
127 if(simplified_expr.id()==ID_index)
132 else if(simplified_expr.id()==ID_member)
137 else if(simplified_expr.id()==ID_address_of)
141 address_expr, resolved_functions);
143 else if(simplified_expr.id()==ID_dereference)
148 else if(simplified_expr.id()==ID_typecast)
154 else if(simplified_expr.id()==ID_symbol)
156 if(simplified_expr.type().id()==ID_code)
158 resolved_functions.insert(simplified_expr);
163 LOG(
"Non const symbol wasn't squashed", simplified_expr);
167 else if(simplified_expr.id()==ID_constant)
169 if(simplified_expr.is_zero())
177 LOG(
"Non-zero constant value found", simplified_expr);
183 LOG(
"Unrecognised expression", simplified_expr);
189 out_functions.insert(resolved_functions.begin(), resolved_functions.end());
207 for(
const exprt &value : exprs)
215 out_functions.insert(
216 potential_out_functions.begin(),
217 potential_out_functions.end());
221 LOG(
"Could not resolve expression in array", value);
249 LOG(
"Could not resolve array", index_expr);
255 LOG(
"Array not const", index_expr);
281 LOG(
"Could not resolve struct", member_expr);
287 LOG(
"Struct was not const so can't resolve values on it", member_expr);
309 LOG(
"Failed to resolve address of", address_expr);
333 LOG(
"Failed to squash dereference", deref_expr);
339 LOG(
"Dereferenced value was not const so can't dereference", deref_expr);
368 out_functions.insert(typecast_values.begin(), typecast_values.end());
373 LOG(
"Failed to squash typecast", typecast_expr);
398 bool is_resolved_expression_const;
399 if(simplified_expr.
id()==ID_index)
404 index_expr, resolved_expressions, is_resolved_expression_const);
406 else if(simplified_expr.
id()==ID_member)
410 member_expr, resolved_expressions, is_resolved_expression_const);
412 else if(simplified_expr.
id()==ID_dereference)
417 deref, resolved_expressions, is_resolved_expression_const);
419 else if(simplified_expr.
id()==ID_typecast)
424 typecast_expr, resolved_expressions, is_resolved_expression_const);
426 else if(simplified_expr.
id()==ID_symbol)
428 LOG(
"Non const symbol will not be squashed", simplified_expr);
433 resolved_expressions.push_back(simplified_expr);
440 out_resolved_expression.insert(
441 out_resolved_expression.end(),
442 resolved_expressions.begin(),
443 resolved_expressions.end());
444 out_is_const=is_resolved_expression_const;
469 if(index_value_expressions.size()==1 &&
470 index_value_expressions.front().id()==ID_constant)
475 bool errors=
to_integer(constant_expr, array_index);
478 out_array_index=array_index;
510 bool array_const=
false;
514 potential_array_exprs,
519 bool all_possible_const=
true;
520 for(
const exprt &potential_array_expr : potential_array_exprs)
523 all_possible_const &&
526 if(potential_array_expr.id()==ID_array)
533 const exprt &func_expr=
535 bool value_const=
false;
541 out_expressions.insert(
542 out_expressions.end(),
543 array_out_functions.begin(),
544 array_out_functions.end());
548 LOG(
"Failed to resolve array value", func_expr);
556 for(
const exprt &array_entry : potential_array_expr.
operands())
562 array_entry, array_contents, is_entry_const);
566 LOG(
"Failed to resolve array value", array_entry);
570 for(
const exprt &resolved_array_entry : array_contents)
572 out_expressions.push_back(resolved_array_entry);
580 "Squashing index of did not result in an array",
581 potential_array_expr);
586 out_is_const=all_possible_const || array_const;
591 LOG(
"Failed to squash index of to array expression", index_expr);
610 bool is_struct_const;
613 bool resolved_struct=
615 member_expr.
compound(), potential_structs, is_struct_const);
618 for(
const exprt &potential_struct : potential_structs)
620 if(potential_struct.id()==ID_struct)
623 const exprt &component_value=
626 bool component_const=
false;
629 component_value, resolved_expressions, component_const);
632 out_expressions.insert(
633 out_expressions.end(),
634 resolved_expressions.begin(),
635 resolved_expressions.end());
639 LOG(
"Could not resolve component value", component_value);
646 "Squashing member access did not resolve in a struct",
651 out_is_const=is_struct_const;
656 LOG(
"Failed to squash struct access", member_expr);
683 if(resolved && pointer_const)
685 bool all_objects_const=
true;
686 for(
const exprt &pointer_val : pointer_values)
688 if(pointer_val.id()==ID_address_of)
691 bool object_const=
false;
695 address_expr.
object(), out_object_values, object_const);
699 out_expressions.insert(
700 out_expressions.end(),
701 out_object_values.begin(),
702 out_object_values.end());
704 all_objects_const&=object_const;
708 LOG(
"Failed to resolve value of a dereference", address_expr);
714 "Squashing dereference did not result in an address", pointer_val);
718 out_is_const=all_objects_const;
725 LOG(
"Failed to resolve pointer of dereference", deref_expr);
727 else if(!pointer_const)
729 LOG(
"Pointer value not const so can't squash", deref_expr);
751 typecast_expr.
op(), typecast_values, typecast_const);
755 out_expressions.insert(
756 out_expressions.end(),
757 typecast_values.begin(),
758 typecast_values.end());
759 out_is_const=typecast_const;
764 LOG(
"Could not resolve typecast value", typecast_expr);
773 const exprt &expression)
const 785 if(type.
id()==ID_array)
788 return qualifers.
is_constant || array_type_qualifers.is_constant;
805 size_t component_number=
808 return struct_expr.
operands()[component_number];
exprt replace_const_symbols(const exprt &expression) const
To collapse the symbols down to their values where possible This takes a very general approach...
The type of an expression.
const typecast_exprt & to_typecast_expr(const exprt &expr)
Cast a generic exprt to a typecast_exprt.
const typet & follow(const typet &src) const
symbolt & lookup(const irep_idt &identifier)
Find a symbol in the symbol table.
remove_const_function_pointerst(message_handlert &message_handler, const namespacet &ns, const symbol_tablet &symbol_table)
To take a function call on a function pointer, and if possible resolve it to a small collection of po...
bool try_resolve_typecast_function_call(const typecast_exprt &typecast_expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
exprt get_component_value(const struct_exprt &struct_expr, const member_exprt &member_expr)
To extract the value of the specific component within a struct.
std::list< exprt > expressionst
exprt simplify_expr(const exprt &src, const namespacet &ns)
bool try_resolve_member_function_call(const member_exprt &member_expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
bool try_resolve_index_of(const index_exprt &index_expr, expressionst &out_expressions, bool &out_is_const)
To squash an index access by first finding the array it is accessing Then if the index can be resolve...
const irep_idt & get_identifier() const
const member_exprt & to_member_expr(const exprt &expr)
Cast a generic exprt to a member_exprt.
bool try_resolve_typecast(const typecast_exprt &typecast_expr, expressionst &out_expressions, bool &out_is_const)
To squash a typecast access.
exprt value
Initial value of symbol.
const struct_exprt & to_struct_expr(const exprt &expr)
Cast a generic exprt to a struct_exprt.
bool try_resolve_dereference(const dereference_exprt &deref_expr, expressionst &out_expressions, bool &out_is_const)
To squash a dereference access by first finding the address_of the dereference is dereferencing...
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
A constant literal expression.
std::unordered_set< exprt, irep_hash > functionst
Extract member of struct or union.
bool try_resolve_function_calls(const expressionst &exprs, functionst &out_functions)
To resolve a collection of expressions to the specific function calls they can be.
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast a generic exprt to a dereference_exprt.
const index_exprt & to_index_expr(const exprt &expr)
Cast a generic exprt to an index_exprt.
const exprt & compound() const
const irep_idt & id() const
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast a generic exprt to an address_of_exprt.
bool try_resolve_member(const member_exprt &member_expr, expressionst &out_expressions, bool &out_is_const)
To squash an member access by first finding the struct it is accessing Then return the squashed value...
bool try_resolve_expression(const exprt &expr, expressionst &out_resolved_expression, bool &out_is_const)
To squash various expr types to simplify the expression.
exprt resolve_symbol(const symbol_exprt &symbol_expr) const
Look up a symbol in the symbol table and return its value.
bool is_const_expression(const exprt &expression) const
To evaluate the const-ness of the expression type.
bool try_resolve_index_value(const exprt &index_value_expr, mp_integer &out_array_index)
Given an index into an array, resolve, if possible, the index that is being accessed.
Operator to dereference a pointer.
const irep_idt & get(const irep_namet &name) const
bool try_resolve_index_of_function_call(const index_exprt &index_expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
#define LOG(message, irep)
bool operator()(const exprt &base_expression, functionst &out_functions)
To take a function call on a function pointer, and if possible resolve it to a small collection of po...
Operator to return the address of an object.
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
typet type
Type of symbol.
bool try_resolve_dereference_function_call(const dereference_exprt &deref_expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
bool try_resolve_address_of_function_call(const address_of_exprt &address_expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
Base class for all expressions.
std::size_t component_number(const irep_idt &component_name) const
irep_idt get_component_name() const
Expression to hold a symbol (variable)
bool try_resolve_function_call(const exprt &expr, functionst &out_functions)
To resolve an expression to the specific function calls it can be.
std::size_t integer2size_t(const mp_integer &n)
const constant_exprt & to_constant_expr(const exprt &expr)
Cast a generic exprt to a constant_exprt.
const typet & subtype() const
struct constructor from list of elements
bool is_const_type(const typet &type) const
To evaluate the const-ness of the type.
const symbol_tablet & symbol_table