21 #define LOG(message, irep) \ 22 debug() << "Case " << __LINE__ << " : " << message << "\n" \ 23 << irep.pretty() << eom; 36 symbol_table(symbol_table)
51 const exprt &base_expression,
67 const exprt &expression)
const 69 if(expression.
id()==ID_symbol)
75 if(symbol.
type.
id()!=ID_code)
92 exprt const_symbol_cleared_expr=expression;
93 const_symbol_cleared_expr.
operands().clear();
97 const_symbol_cleared_expr.
operands().push_back(const_symbol_cleared_op);
100 return const_symbol_cleared_expr;
126 assert(out_functions.empty());
130 if(simplified_expr.id()==ID_index)
135 else if(simplified_expr.id()==ID_member)
140 else if(simplified_expr.id()==ID_address_of)
144 address_expr, resolved_functions);
146 else if(simplified_expr.id()==ID_dereference)
151 else if(simplified_expr.id()==ID_typecast)
157 else if(simplified_expr.id()==ID_symbol)
159 if(simplified_expr.type().id()==ID_code)
166 LOG(
"Non const symbol wasn't squashed", simplified_expr);
170 else if(simplified_expr.id()==ID_constant)
172 if(simplified_expr.is_zero())
180 LOG(
"Non-zero constant value found", simplified_expr);
186 LOG(
"Unrecognised expression", simplified_expr);
192 out_functions.insert(resolved_functions.begin(), resolved_functions.end());
210 for(
const exprt &value : exprs)
218 out_functions.insert(
219 potential_out_functions.begin(),
220 potential_out_functions.end());
224 LOG(
"Could not resolve expression in array", value);
252 LOG(
"Could not resolve array", index_expr);
258 LOG(
"Array not const", index_expr);
284 LOG(
"Could not resolve struct", member_expr);
290 LOG(
"Struct was not const so can't resolve values on it", member_expr);
312 LOG(
"Failed to resolve address of", address_expr);
336 LOG(
"Failed to squash dereference", deref_expr);
342 LOG(
"Dereferenced value was not const so can't dereference", deref_expr);
371 out_functions.insert(typecast_values.begin(), typecast_values.end());
376 LOG(
"Failed to squash typecast", typecast_expr);
401 bool is_resolved_expression_const =
false;
402 if(simplified_expr.
id()==ID_index)
407 index_expr, resolved_expressions, is_resolved_expression_const);
409 else if(simplified_expr.
id()==ID_member)
413 member_expr, resolved_expressions, is_resolved_expression_const);
415 else if(simplified_expr.
id()==ID_dereference)
420 deref, resolved_expressions, is_resolved_expression_const);
422 else if(simplified_expr.
id()==ID_typecast)
427 typecast_expr, resolved_expressions, is_resolved_expression_const);
429 else if(simplified_expr.
id()==ID_symbol)
431 LOG(
"Non const symbol will not be squashed", simplified_expr);
436 resolved_expressions.push_back(simplified_expr);
443 out_resolved_expression.insert(
444 out_resolved_expression.end(),
445 resolved_expressions.begin(),
446 resolved_expressions.end());
447 out_is_const=is_resolved_expression_const;
472 if(index_value_expressions.size()==1 &&
473 index_value_expressions.front().id()==ID_constant)
478 bool errors=
to_integer(constant_expr, array_index);
481 out_array_index=array_index;
513 bool array_const=
false;
517 potential_array_exprs,
522 bool all_possible_const=
true;
523 for(
const exprt &potential_array_expr : potential_array_exprs)
526 all_possible_const &&
529 if(potential_array_expr.id()==ID_array)
536 const exprt &func_expr=
538 bool value_const=
false;
544 out_expressions.insert(
545 out_expressions.end(),
546 array_out_functions.begin(),
547 array_out_functions.end());
551 LOG(
"Failed to resolve array value", func_expr);
559 for(
const exprt &array_entry : potential_array_expr.
operands())
565 array_entry, array_contents, is_entry_const);
569 LOG(
"Failed to resolve array value", array_entry);
573 for(
const exprt &resolved_array_entry : array_contents)
575 out_expressions.push_back(resolved_array_entry);
583 "Squashing index of did not result in an array",
584 potential_array_expr);
589 out_is_const=all_possible_const || array_const;
594 LOG(
"Failed to squash index of to array expression", index_expr);
613 bool is_struct_const;
616 bool resolved_struct=
618 member_expr.
compound(), potential_structs, is_struct_const);
621 for(
const exprt &potential_struct : potential_structs)
623 if(potential_struct.id()==ID_struct)
626 const exprt &component_value=
629 bool component_const=
false;
632 component_value, resolved_expressions, component_const);
635 out_expressions.insert(
636 out_expressions.end(),
637 resolved_expressions.begin(),
638 resolved_expressions.end());
642 LOG(
"Could not resolve component value", component_value);
649 "Squashing member access did not resolve in a struct",
654 out_is_const=is_struct_const;
659 LOG(
"Failed to squash struct access", member_expr);
686 if(resolved && pointer_const)
688 bool all_objects_const=
true;
689 for(
const exprt &pointer_val : pointer_values)
691 if(pointer_val.id()==ID_address_of)
694 bool object_const=
false;
698 address_expr.
object(), out_object_values, object_const);
702 out_expressions.insert(
703 out_expressions.end(),
704 out_object_values.begin(),
705 out_object_values.end());
707 all_objects_const&=object_const;
711 LOG(
"Failed to resolve value of a dereference", address_expr);
717 "Squashing dereference did not result in an address", pointer_val);
721 out_is_const=all_objects_const;
728 LOG(
"Failed to resolve pointer of dereference", deref_expr);
730 else if(!pointer_const)
732 LOG(
"Pointer value not const so can't squash", deref_expr);
754 typecast_expr.
op(), typecast_values, typecast_const);
758 out_expressions.insert(
759 out_expressions.end(),
760 typecast_values.begin(),
761 typecast_values.end());
762 out_is_const=typecast_const;
767 LOG(
"Could not resolve typecast value", typecast_expr);
776 const exprt &expression)
const 790 return type.
get_bool(ID_C_constant);
802 size_t component_number=
805 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.
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
Goto Programs with Functions.
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.
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...
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast a generic exprt to an address_of_exprt.
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
A constant literal expression.
bool get_bool(const irep_namet &name) const
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast a generic exprt to a dereference_exprt.
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 exprt & compound() const
const irep_idt & id() const
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.
API to expression classes.
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)
const member_exprt & to_member_expr(const exprt &expr)
Cast a generic exprt to a member_exprt.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
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...
const typet & follow(const typet &) const
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
Operator to return the address of an object.
const struct_exprt & to_struct_expr(const exprt &expr)
Cast a generic exprt to a struct_exprt.
const constant_exprt & to_constant_expr(const exprt &expr)
Cast a generic exprt to a constant_exprt.
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
const typecast_exprt & to_typecast_expr(const exprt &expr)
Cast a generic exprt to a typecast_exprt.
std::unordered_set< symbol_exprt, irep_hash > functionst
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.
goto_programt coverage_criteriont message_handlert & message_handler
std::size_t integer2size_t(const mp_integer &n)
const typet & subtype() const
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
struct constructor from list of elements
bool is_const_type(const typet &type) const
To evaluate the const-ness of the type.
const index_exprt & to_index_expr(const exprt &expr)
Cast a generic exprt to an index_exprt.
const symbol_tablet & symbol_table