CBMC
state_encoding_targets.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: State Encoding Targets
4 
5 Author: Daniel Kroening, dkr@amazon.com
6 
7 \*******************************************************************/
8 
10 
11 #include <util/format_expr.h>
13 
14 #include <ostream>
15 
17 {
18  counter++;
19  if(counter < 10)
20  out << ' ';
21  out << '(' << counter << ')' << ' ';
22  out << format(expr) << '\n';
23 }
24 
26 {
27 #if 0
28  auto make_unary = [this](const char *f) {
29  return [this, f](const exprt &expr) {
30  const auto &unary_expr = to_unary_expr(expr);
31  out << '(' << f << ' ';
32  convert_expr(unary_expr.op());
33  out << ')';
34  };
35  };
36 #endif
37 
38  auto make_binary = [this](const char *f) {
39  return [this, f](const exprt &expr) {
40  const auto &binary_expr = to_binary_expr(expr);
41  out << '(' << f << ' ';
42  convert_expr(binary_expr.op0());
43  out << ' ';
44  convert_expr(binary_expr.op1());
45  out << ')';
46  };
47  };
48 
49  auto make_ternary = [this](const char *f) {
50  return [this, f](const exprt &expr) {
51  const auto &ternary_expr = to_ternary_expr(expr);
52  out << '(' << f << ' ';
53  convert_expr(ternary_expr.op0());
54  out << ' ';
55  convert_expr(ternary_expr.op1());
56  out << ' ';
57  convert_expr(ternary_expr.op2());
58  out << ')';
59  };
60  };
61 
62  set_converter(ID_update_state, [this](const exprt &expr) {
63  out << "(update-state-" << type2id(to_ternary_expr(expr).op2().type())
64  << ' ';
65  convert_expr(to_ternary_expr(expr).op0());
66  out << ' ';
67  convert_expr(to_ternary_expr(expr).op1());
68  out << ' ';
69  convert_expr(to_ternary_expr(expr).op2());
70  out << ')';
71  });
72 
73  set_converter(ID_enter_scope_state, [this](const exprt &expr) {
74  out << "(enter-scope-state-" << type2id(to_binary_expr(expr).op1().type())
75  << ' ';
76  convert_expr(to_binary_expr(expr).op0());
77  out << ' ';
78  convert_expr(to_binary_expr(expr).op1());
79  out << ' ';
80  auto size_opt = size_of_expr(
81  to_pointer_type(to_binary_expr(expr).op1().type()).base_type(), ns);
82  CHECK_RETURN(size_opt.has_value());
83  convert_expr(*size_opt);
84  out << ')';
85  });
86 
87  set_converter(ID_exit_scope_state, [this](const exprt &expr) {
88  out << "(exit-scope-state-" << type2id(to_binary_expr(expr).op1().type())
89  << ' ';
90  convert_expr(to_binary_expr(expr).op0());
91  out << ' ';
92  convert_expr(to_binary_expr(expr).op1());
93  out << ')';
94  });
95 
96  set_converter(ID_allocate, [this](const exprt &expr) {
97  out << "(allocate ";
98  convert_expr(to_binary_expr(expr).op0());
99  out << ' ';
100  convert_expr(to_binary_expr(expr).op1());
101  out << ')';
102  });
103 
104  set_converter(ID_allocate_state, [this](const exprt &expr) {
105  out << "(allocate ";
106  convert_expr(to_binary_expr(expr).op0());
107  out << ' ';
108  convert_expr(to_binary_expr(expr).op1());
109  out << ')';
110  });
111 
112  set_converter(ID_reallocate, [this](const exprt &expr) {
113  out << "(reallocate ";
114  convert_expr(to_ternary_expr(expr).op0());
115  out << ' ';
116  convert_expr(to_ternary_expr(expr).op1());
117  out << ' ';
118  convert_expr(to_ternary_expr(expr).op2());
119  out << ')';
120  });
121 
122  set_converter(ID_deallocate_state, [this](const exprt &expr) {
123  out << "(deallocate ";
124  convert_expr(to_binary_expr(expr).op0());
125  out << ' ';
126  convert_expr(to_binary_expr(expr).op1());
127  out << ')';
128  });
129 
130  set_converter(ID_evaluate, [this](const exprt &expr) {
131  out << "(evaluate-" << type2id(expr.type()) << ' ';
132  convert_expr(to_binary_expr(expr).op0());
133  out << ' ';
134  convert_expr(to_binary_expr(expr).op1());
135  out << ')';
136  });
137 
138  set_converter(ID_state_is_cstring, make_binary("state-is-cstring"));
139 
140  set_converter(ID_initial_state, [this](const exprt &expr) { out << "true"; });
141 
143  ID_state_is_dynamic_object, make_binary("state-is-dynamic-object"));
144 
145  set_converter(ID_state_live_object, make_binary("state-live-object"));
146 
148  ID_state_writeable_object, make_binary("state-writeable-object"));
149 
150  set_converter(ID_state_is_sentinel_dll, [this](const exprt &expr) {
151  if(expr.operands().size() == 3)
152  {
153  out << "(state-is-sentinel-dll ";
154  convert_expr(to_multi_ary_expr(expr).op0());
155  out << ' ';
156  convert_expr(to_multi_ary_expr(expr).op1());
157  out << ' ';
158  convert_expr(to_multi_ary_expr(expr).op2());
159  out << ')';
160  }
161  else
162  {
163  out << "(state-is-sentinel-dll-with-node ";
164  convert_expr(to_multi_ary_expr(expr).op0());
165  out << ' ';
166  convert_expr(to_multi_ary_expr(expr).op1());
167  out << ' ';
168  convert_expr(to_multi_ary_expr(expr).op2());
169  out << ' ';
170  convert_expr(to_multi_ary_expr(expr).op3());
171  out << ')';
172  }
173  });
174 
175  set_converter(ID_state_live_object, make_binary("state-live-object"));
176 
178  ID_state_writeable_object, make_binary("state-writeable-object"));
179 
180  set_converter(ID_state_r_ok, make_ternary("state-r-ok"));
181 
182  set_converter(ID_state_w_ok, make_ternary("state-w-ok"));
183 
184  set_converter(ID_state_rw_ok, make_ternary("state-rw-ok"));
185 }
void set_to_true(source_locationt, exprt) override
Base class for all expressions.
Definition: expr.h:56
typet & type()
Return the type of the expression.
Definition: expr.h:84
operandst & operands()
Definition: expr.h:94
const namespacet & ns
Definition: smt2_conv.h:98
std::ostream & out
Definition: smt2_conv.h:99
std::string type2id(const typet &) const
Definition: smt2_conv.cpp:1052
void set_converter(irep_idt id, std::function< void(const exprt &)> converter)
Definition: smt2_conv.h:92
void convert_expr(const exprt &)
Definition: smt2_conv.cpp:1162
exprt make_binary(const exprt &expr)
splits an expression with >=3 operands into nested binary expressions
Definition: expr_util.cpp:38
static format_containert< T > format(const T &o)
Definition: format.h:37
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: pointer_expr.h:93
std::optional< exprt > size_of_expr(const typet &type, const namespacet &ns)
Pointer Logic.
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
const binary_exprt & to_binary_expr(const exprt &expr)
Cast an exprt to a binary_exprt.
Definition: std_expr.h:715
const ternary_exprt & to_ternary_expr(const exprt &expr)
Cast an exprt to a ternary_exprt.
Definition: std_expr.h:116
const unary_exprt & to_unary_expr(const exprt &expr)
Cast an exprt to a unary_exprt.
Definition: std_expr.h:426