-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathgoto_instruction_code.h
564 lines (481 loc) · 15 KB
/
goto_instruction_code.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*******************************************************************\
Module: Data structures representing instructions in a GOTO program
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#ifndef CPROVER_UTIL_GOTO_INSTRUCTION_CODE_H
#define CPROVER_UTIL_GOTO_INSTRUCTION_CODE_H
#include <util/std_code_base.h>
#include <util/std_expr.h>
using goto_instruction_codet = codet;
/// A \ref goto_instruction_codet representing an assignment in the program.
/// For example, if an expression `e1` is represented as an \ref exprt `expr1`
/// and an expression `e2` is represented as an \ref exprt `expr2`, the
/// assignment `e1 = e2;` can be represented as `code_assignt(expr1, expr2)`.
class code_assignt : public goto_instruction_codet
{
public:
code_assignt() : goto_instruction_codet(ID_assign)
{
operands().resize(2);
}
code_assignt(exprt lhs, exprt rhs)
: goto_instruction_codet(ID_assign, {std::move(lhs), std::move(rhs)})
{
}
code_assignt(exprt lhs, exprt rhs, source_locationt loc)
: goto_instruction_codet(
ID_assign,
{std::move(lhs), std::move(rhs)},
std::move(loc))
{
}
exprt &lhs()
{
return op0();
}
exprt &rhs()
{
return op1();
}
const exprt &lhs() const
{
return op0();
}
const exprt &rhs() const
{
return op1();
}
static void check(
const goto_instruction_codet &code,
const validation_modet vm = validation_modet::INVARIANT)
{
DATA_CHECK(
vm, code.operands().size() == 2, "assignment must have two operands");
}
static void validate(
const goto_instruction_codet &code,
const namespacet &,
const validation_modet vm = validation_modet::INVARIANT)
{
check(code, vm);
DATA_CHECK(
vm,
code.op0().type() == code.op1().type(),
"lhs and rhs of assignment must have same type");
}
static void validate_full(
const goto_instruction_codet &code,
const namespacet &ns,
const validation_modet vm = validation_modet::INVARIANT)
{
for(const exprt &op : code.operands())
{
validate_full_expr(op, ns, vm);
}
validate(code, ns, vm);
}
protected:
using goto_instruction_codet::op0;
using goto_instruction_codet::op1;
using goto_instruction_codet::op2;
using goto_instruction_codet::op3;
};
template <>
inline bool can_cast_expr<code_assignt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_assign);
}
inline void validate_expr(const code_assignt &x)
{
code_assignt::check(x);
}
inline const code_assignt &to_code_assign(const goto_instruction_codet &code)
{
PRECONDITION(code.get_statement() == ID_assign);
code_assignt::check(code);
return static_cast<const code_assignt &>(code);
}
inline code_assignt &to_code_assign(goto_instruction_codet &code)
{
PRECONDITION(code.get_statement() == ID_assign);
code_assignt::check(code);
return static_cast<code_assignt &>(code);
}
/// A \ref goto_instruction_codet representing the removal of
/// a local variable going out of scope.
class code_deadt : public goto_instruction_codet
{
public:
explicit code_deadt(symbol_exprt symbol)
: goto_instruction_codet(ID_dead, {std::move(symbol)})
{
}
symbol_exprt &symbol()
{
return static_cast<symbol_exprt &>(op0());
}
const symbol_exprt &symbol() const
{
return static_cast<const symbol_exprt &>(op0());
}
const irep_idt &get_identifier() const
{
return symbol().get_identifier();
}
static void check(
const goto_instruction_codet &code,
const validation_modet vm = validation_modet::INVARIANT)
{
DATA_CHECK(
vm,
code.operands().size() == 1,
"removal (code_deadt) must have one operand");
DATA_CHECK(
vm,
code.op0().id() == ID_symbol,
"removing a non-symbol: " + id2string(code.op0().id()) + "from scope");
}
protected:
using goto_instruction_codet::op0;
using goto_instruction_codet::op1;
using goto_instruction_codet::op2;
using goto_instruction_codet::op3;
};
template <>
inline bool can_cast_expr<code_deadt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_dead);
}
inline void validate_expr(const code_deadt &x)
{
code_deadt::check(x);
}
inline const code_deadt &to_code_dead(const goto_instruction_codet &code)
{
PRECONDITION(code.get_statement() == ID_dead);
code_deadt::check(code);
return static_cast<const code_deadt &>(code);
}
inline code_deadt &to_code_dead(goto_instruction_codet &code)
{
PRECONDITION(code.get_statement() == ID_dead);
code_deadt::check(code);
return static_cast<code_deadt &>(code);
}
/// A `goto_instruction_codet` representing the declaration of a local variable.
/// For example, if a variable (symbol) `x` is represented as a
/// \ref symbol_exprt `sym`, then the declaration of this variable can be
/// represented as `code_declt(sym)`.
class code_declt : public goto_instruction_codet
{
public:
explicit code_declt(symbol_exprt symbol)
: goto_instruction_codet(ID_decl, {std::move(symbol)})
{
}
symbol_exprt &symbol()
{
return static_cast<symbol_exprt &>(op0());
}
const symbol_exprt &symbol() const
{
return static_cast<const symbol_exprt &>(op0());
}
const irep_idt &get_identifier() const
{
return symbol().get_identifier();
}
static void check(
const goto_instruction_codet &code,
const validation_modet vm = validation_modet::INVARIANT)
{
DATA_CHECK(
vm, code.operands().size() == 1, "declaration must have one operand");
DATA_CHECK(
vm,
code.op0().id() == ID_symbol,
"declaring a non-symbol: " +
id2string(to_symbol_expr(code.op0()).get_identifier()));
}
};
template <>
inline bool can_cast_expr<code_declt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_decl);
}
inline void validate_expr(const code_declt &x)
{
code_declt::check(x);
}
inline const code_declt &to_code_decl(const goto_instruction_codet &code)
{
PRECONDITION(code.get_statement() == ID_decl);
code_declt::check(code);
return static_cast<const code_declt &>(code);
}
inline code_declt &to_code_decl(goto_instruction_codet &code)
{
PRECONDITION(code.get_statement() == ID_decl);
code_declt::check(code);
return static_cast<code_declt &>(code);
}
/// \ref goto_instruction_codet representation of a function call statement.
/// The function call statement has three operands.
/// The first is the expression that is used to store the return value.
/// The second is the function called.
/// The third is a vector of argument values.
class code_function_callt : public goto_instruction_codet
{
public:
explicit code_function_callt(exprt _function)
: goto_instruction_codet(
ID_function_call,
{nil_exprt(), std::move(_function), exprt(ID_arguments)})
{
}
typedef exprt::operandst argumentst;
code_function_callt(exprt _lhs, exprt _function, argumentst _arguments)
: goto_instruction_codet(
ID_function_call,
{std::move(_lhs), std::move(_function), exprt(ID_arguments)})
{
arguments() = std::move(_arguments);
}
code_function_callt(exprt _function, argumentst _arguments)
: code_function_callt(std::move(_function))
{
arguments() = std::move(_arguments);
}
exprt &lhs()
{
return op0();
}
const exprt &lhs() const
{
return op0();
}
exprt &function()
{
return op1();
}
const exprt &function() const
{
return op1();
}
argumentst &arguments()
{
return op2().operands();
}
const argumentst &arguments() const
{
return op2().operands();
}
static void check(
const goto_instruction_codet &code,
const validation_modet vm = validation_modet::INVARIANT)
{
DATA_CHECK(
vm,
code.operands().size() == 3,
"function calls must have three operands:\n1) expression to store the "
"returned values\n2) the function being called\n3) the vector of "
"arguments");
}
static void validate(
const goto_instruction_codet &code,
const namespacet &,
const validation_modet vm = validation_modet::INVARIANT)
{
check(code, vm);
if(code.op0().id() != ID_nil)
DATA_CHECK(
vm,
code.op0().type() == to_code_type(code.op1().type()).return_type(),
"function returns expression of wrong type");
}
static void validate_full(
const goto_instruction_codet &code,
const namespacet &ns,
const validation_modet vm = validation_modet::INVARIANT)
{
for(const exprt &op : code.operands())
{
validate_full_expr(op, ns, vm);
}
validate(code, ns, vm);
}
protected:
using goto_instruction_codet::op0;
using goto_instruction_codet::op1;
using goto_instruction_codet::op2;
using goto_instruction_codet::op3;
};
template <>
inline bool can_cast_expr<code_function_callt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_function_call);
}
inline void validate_expr(const code_function_callt &x)
{
code_function_callt::check(x);
}
inline const code_function_callt &
to_code_function_call(const goto_instruction_codet &code)
{
PRECONDITION(code.get_statement() == ID_function_call);
code_function_callt::check(code);
return static_cast<const code_function_callt &>(code);
}
inline code_function_callt &to_code_function_call(goto_instruction_codet &code)
{
PRECONDITION(code.get_statement() == ID_function_call);
code_function_callt::check(code);
return static_cast<code_function_callt &>(code);
}
/// A `goto_instruction_codet` representing the declaration that an input of
/// a particular description has a value which corresponds to the value of a
/// given expression (or expressions).
/// When working with the C front end, calls to the `__CPROVER_input` intrinsic
/// can be added to the input code in order add instructions of this type to the
/// goto program.
/// The first argument is expected to be a C string denoting the input
/// identifier. The second argument is the expression for the input value.
class code_inputt : public goto_instruction_codet
{
public:
/// This constructor is for support of calls to `__CPROVER_input` in user
/// code. Where the first first argument is a description which may be any
/// `const char *` and one or more corresponding expression arguments follow.
explicit code_inputt(
std::vector<exprt> arguments,
std::optional<source_locationt> location = {});
/// This constructor is intended for generating input instructions as part of
/// synthetic entry point code, rather than as part of user code.
/// \param description: This is used to construct an expression for a pointer
/// to a string constant containing the description text. This expression
/// is then used as the first argument.
/// \param expression: This expression corresponds to a value which should be
/// recorded as an input.
/// \param location: A location to associate with this instruction.
code_inputt(
const irep_idt &description,
exprt expression,
std::optional<source_locationt> location = {});
static void check(
const goto_instruction_codet &code,
const validation_modet vm = validation_modet::INVARIANT);
};
template <>
inline bool can_cast_expr<code_inputt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_input);
}
inline void validate_expr(const code_inputt &input)
{
code_inputt::check(input);
}
/// A `goto_instruction_codet` representing the declaration that an output of
/// a particular description has a value which corresponds to the value of a
/// given expression (or expressions).
/// When working with the C front end, calls to the `__CPROVER_output` intrinsic
/// can be added to the input code in order add instructions of this type to the
/// goto program.
/// The first argument is expected to be a C string denoting the output
/// identifier. The second argument is the expression for the output value.
class code_outputt : public goto_instruction_codet
{
public:
/// This constructor is for support of calls to `__CPROVER_output` in user
/// code. Where the first first argument is a description which may be any
/// `const char *` and one or more corresponding expression arguments follow.
explicit code_outputt(
std::vector<exprt> arguments,
std::optional<source_locationt> location = {});
/// This constructor is intended for generating output instructions as part of
/// synthetic entry point code, rather than as part of user code.
/// \param description: This is used to construct an expression for a pointer
/// to a string constant containing the description text.
/// \param expression: This expression corresponds to a value which should be
/// recorded as an output.
/// \param location: A location to associate with this instruction.
code_outputt(
const irep_idt &description,
exprt expression,
std::optional<source_locationt> location = {});
static void check(
const goto_instruction_codet &code,
const validation_modet vm = validation_modet::INVARIANT);
};
template <>
inline bool can_cast_expr<code_outputt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_output);
}
inline void validate_expr(const code_outputt &output)
{
code_outputt::check(output);
}
/// \ref goto_instruction_codet representation of a "return from a
/// function" statement.
class code_returnt : public goto_instruction_codet
{
public:
explicit code_returnt(exprt _op)
: goto_instruction_codet(ID_return, {std::move(_op)})
{
}
const exprt &return_value() const
{
return op0();
}
exprt &return_value()
{
return op0();
}
static void check(
const goto_instruction_codet &code,
const validation_modet vm = validation_modet::INVARIANT)
{
DATA_CHECK(vm, code.operands().size() == 1, "return must have one operand");
}
protected:
using goto_instruction_codet::op0;
using goto_instruction_codet::op1;
using goto_instruction_codet::op2;
using goto_instruction_codet::op3;
};
template <>
inline bool can_cast_expr<code_returnt>(const exprt &base)
{
return detail::can_cast_code_impl(base, ID_return);
}
inline void validate_expr(const code_returnt &x)
{
code_returnt::check(x);
}
inline const code_returnt &to_code_return(const goto_instruction_codet &code)
{
PRECONDITION(code.get_statement() == ID_return);
code_returnt::check(code);
return static_cast<const code_returnt &>(code);
}
inline code_returnt &to_code_return(goto_instruction_codet &code)
{
PRECONDITION(code.get_statement() == ID_return);
code_returnt::check(code);
return static_cast<code_returnt &>(code);
}
/// \brief Builds a \ref code_function_callt
/// to `__CPROVER_havoc_slice(p, size)`.
///
/// \param p The pointer argument.
/// \param size The size argument.
/// \param ns Namespace where the `__CPROVER_havoc_slice symbol` can be found.
/// \remarks: It is a PRECONDITION that `__CPROVER_havoc_slice` exists
/// in the namespace
///
/// \return A \ref code_function_callt expression
/// `nil_exprt() := __CPROVER_havoc_slice(p, size)`.
inline code_function_callt
havoc_slice_call(const exprt &p, const exprt &size, const namespacet &ns);
#endif // CPROVER_GOTO_PROGRAMS_GOTO_INSTRUCTION_CODE_H