Skip to content

Commit 050cc59

Browse files
Martin Brainmartin
Martin Brain
authored and
martin
committed
Make evaluation and/or simplification of conditions/expressions an ai domain operation.
1 parent ef406a0 commit 050cc59

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/analyses/ai.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, [email protected]
1010
#include <memory>
1111
#include <sstream>
1212

13+
#include <util/simplify_expr.h>
1314
#include <util/std_expr.h>
1415
#include <util/std_code.h>
1516

@@ -64,6 +65,58 @@ xmlt ai_domain_baset::output_xml(
6465

6566
/*******************************************************************\
6667
68+
Function: variable_sensitivity_domaint::ai_simplify_lhs
69+
70+
Inputs:
71+
condition - the expression to simplify
72+
ns - the namespace
73+
74+
Outputs: True if condition did not change. False otherwise. condition
75+
will be updated with the simplified condition if it has worked
76+
77+
Purpose: Use the information in the domain to simplify the expression
78+
on the LHS of an assignment. This for example won't simplify symbols
79+
to their values, but does simplify indices in arrays, members of
80+
structs and dereferencing of pointers
81+
\*******************************************************************/
82+
83+
bool ai_domain_baset::ai_simplify_lhs(
84+
exprt &condition, const namespacet &ns) const
85+
{
86+
// Care must be taken here to give something that is still writable
87+
if(condition.id()==ID_index)
88+
{
89+
index_exprt ie=to_index_expr(condition);
90+
bool changed=ai_simplify(ie.index(), ns);
91+
if(changed)
92+
condition=simplify_expr(ie, ns);
93+
94+
return !changed;
95+
}
96+
else if(condition.id()==ID_dereference)
97+
{
98+
dereference_exprt de=to_dereference_expr(condition);
99+
bool changed=ai_simplify(de.pointer(), ns);
100+
if(changed)
101+
condition=simplify_expr(de, ns); // So *(&x) -> x
102+
103+
return !changed;
104+
}
105+
else if(condition.id()==ID_member)
106+
{
107+
member_exprt me=to_member_expr(condition);
108+
bool changed=ai_simplify_lhs(me.compound(), ns); // <-- lhs!
109+
if(changed)
110+
condition=simplify_expr(me, ns);
111+
112+
return !changed;
113+
}
114+
else
115+
return true;
116+
}
117+
118+
/*******************************************************************\
119+
67120
Function: ai_baset::output
68121
69122
Inputs:

src/analyses/ai.h

+18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Author: Daniel Kroening, [email protected]
1414

1515
#include <util/json.h>
1616
#include <util/xml.h>
17+
#include <util/expr.h>
1718

1819
#include <goto-programs/goto_model.h>
1920

@@ -80,6 +81,23 @@ class ai_domain_baset
8081
//
8182
// This computes the join between "this" and "b".
8283
// Return true if "this" has changed.
84+
85+
// This method allows an expression to be simplified / evaluated using the
86+
// current state. It is used to evaluate assertions and in program
87+
// simplification
88+
89+
// return true if unchanged
90+
virtual bool ai_simplify(
91+
exprt &condition,
92+
const namespacet &ns) const
93+
{
94+
return true;
95+
}
96+
97+
// Simplifies the expression but keeps it as an l-value
98+
virtual bool ai_simplify_lhs(
99+
exprt &condition,
100+
const namespacet &ns) const;
83101
};
84102

85103
// don't use me -- I am just a base class

0 commit comments

Comments
 (0)