forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoto_convert_side_effect.cpp
727 lines (617 loc) · 19.7 KB
/
goto_convert_side_effect.cpp
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*******************************************************************\
Module: Program Transformation
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Program Transformation
#include "goto_convert_class.h"
#include <util/arith_tools.h>
#include <util/bitvector_expr.h>
#include <util/c_types.h>
#include <util/expr_util.h>
#include <util/fresh_symbol.h>
#include <util/mathematical_types.h>
#include <util/std_expr.h>
#include <util/symbol.h>
#include <ansi-c/c_expr.h>
bool goto_convertt::has_function_call(const exprt &expr)
{
forall_operands(it, expr)
if(has_function_call(*it))
return true;
if(expr.id()==ID_side_effect &&
expr.get(ID_statement)==ID_function_call)
return true;
return false;
}
void goto_convertt::remove_assignment(
side_effect_exprt &expr,
goto_programt &dest,
bool result_is_used,
bool address_taken,
const irep_idt &mode)
{
const irep_idt statement=expr.get_statement();
optionalt<exprt> replacement_expr_opt;
if(statement==ID_assign)
{
auto &old_assignment = to_side_effect_expr_assign(expr);
if(result_is_used && !address_taken && needs_cleaning(old_assignment.lhs()))
{
if(!old_assignment.rhs().is_constant())
make_temp_symbol(old_assignment.rhs(), "assign", dest, mode);
replacement_expr_opt = old_assignment.rhs();
}
exprt new_lhs = skip_typecast(old_assignment.lhs());
exprt new_rhs =
typecast_exprt::conditional_cast(old_assignment.rhs(), new_lhs.type());
code_assignt new_assignment(std::move(new_lhs), std::move(new_rhs));
new_assignment.add_source_location() = expr.source_location();
convert_assign(new_assignment, dest, mode);
}
else if(statement==ID_assign_plus ||
statement==ID_assign_minus ||
statement==ID_assign_mult ||
statement==ID_assign_div ||
statement==ID_assign_mod ||
statement==ID_assign_shl ||
statement==ID_assign_ashr ||
statement==ID_assign_lshr ||
statement==ID_assign_bitand ||
statement==ID_assign_bitxor ||
statement==ID_assign_bitor)
{
INVARIANT_WITH_DIAGNOSTICS(
expr.operands().size() == 2,
id2string(statement) + " expects two arguments",
expr.find_source_location());
irep_idt new_id;
if(statement==ID_assign_plus)
new_id=ID_plus;
else if(statement==ID_assign_minus)
new_id=ID_minus;
else if(statement==ID_assign_mult)
new_id=ID_mult;
else if(statement==ID_assign_div)
new_id=ID_div;
else if(statement==ID_assign_mod)
new_id=ID_mod;
else if(statement==ID_assign_shl)
new_id=ID_shl;
else if(statement==ID_assign_ashr)
new_id=ID_ashr;
else if(statement==ID_assign_lshr)
new_id=ID_lshr;
else if(statement==ID_assign_bitand)
new_id=ID_bitand;
else if(statement==ID_assign_bitxor)
new_id=ID_bitxor;
else if(statement==ID_assign_bitor)
new_id=ID_bitor;
else
{
UNREACHABLE;
}
const binary_exprt &binary_expr = to_binary_expr(expr);
const typet &op0_type = binary_expr.op0().type();
PRECONDITION(
op0_type.id() != ID_c_enum_tag && op0_type.id() != ID_c_enum &&
op0_type.id() != ID_c_bool && op0_type.id() != ID_bool);
exprt rhs = binary_exprt{binary_expr.op0(), new_id, binary_expr.op1()};
rhs.add_source_location() = expr.source_location();
if(result_is_used && !address_taken && needs_cleaning(binary_expr.op0()))
{
make_temp_symbol(rhs, "assign", dest, mode);
replacement_expr_opt = rhs;
}
exprt new_lhs = skip_typecast(binary_expr.op0());
rhs = typecast_exprt::conditional_cast(rhs, new_lhs.type());
rhs.add_source_location() = expr.source_location();
code_assignt assignment(new_lhs, rhs);
assignment.add_source_location()=expr.source_location();
convert(assignment, dest, mode);
}
else
UNREACHABLE;
// revert assignment in the expression to its LHS
if(replacement_expr_opt.has_value())
{
exprt new_lhs =
typecast_exprt::conditional_cast(*replacement_expr_opt, expr.type());
expr.swap(new_lhs);
}
else if(result_is_used)
{
exprt lhs = to_binary_expr(expr).op0();
// assign_* statements can have an lhs operand with a different type than
// that of the overall expression, because of integer promotion (which may
// have introduced casts to the lhs).
if(expr.type() != lhs.type())
{
// Skip over those type casts, but also
// make sure the resulting expression has the same type as before.
DATA_INVARIANT(
lhs.id() == ID_typecast,
id2string(expr.id()) +
" expression with different operand type expected to have a "
"typecast");
DATA_INVARIANT(
to_typecast_expr(lhs).op().type() == expr.type(),
id2string(expr.id()) + " type mismatch in lhs operand");
lhs = to_typecast_expr(lhs).op();
}
expr.swap(lhs);
}
else
expr.make_nil();
}
void goto_convertt::remove_pre(
side_effect_exprt &expr,
goto_programt &dest,
bool result_is_used,
bool address_taken,
const irep_idt &mode)
{
INVARIANT_WITH_DIAGNOSTICS(
expr.operands().size() == 1,
"preincrement/predecrement must have one operand",
expr.find_source_location());
const irep_idt statement=expr.get_statement();
DATA_INVARIANT(
statement == ID_preincrement || statement == ID_predecrement,
"expects preincrement or predecrement");
const auto &op = to_unary_expr(expr).op();
const typet &op_type = op.type();
PRECONDITION(
op_type.id() != ID_c_enum_tag && op_type.id() != ID_c_enum &&
op_type.id() != ID_c_bool && op_type.id() != ID_bool);
typet constant_type;
if(op_type.id() == ID_pointer)
constant_type = c_index_type();
else if(is_number(op_type))
constant_type = op_type;
else
{
UNREACHABLE;
}
exprt constant;
if(constant_type.id() == ID_complex)
{
exprt real = from_integer(1, constant_type.subtype());
exprt imag = from_integer(0, constant_type.subtype());
constant = complex_exprt(real, imag, to_complex_type(constant_type));
}
else
constant = from_integer(1, constant_type);
exprt rhs = binary_exprt{
op, statement == ID_preincrement ? ID_plus : ID_minus, std::move(constant)};
rhs.add_source_location() = expr.source_location();
const bool cannot_use_lhs =
result_is_used && !address_taken && needs_cleaning(op);
if(cannot_use_lhs)
make_temp_symbol(rhs, "pre", dest, mode);
code_assignt assignment(op, rhs);
assignment.add_source_location()=expr.find_source_location();
convert(assignment, dest, mode);
if(result_is_used)
{
if(cannot_use_lhs)
expr.swap(rhs);
else
{
// revert to argument of pre-inc/pre-dec
exprt tmp = op;
expr.swap(tmp);
}
}
else
expr.make_nil();
}
void goto_convertt::remove_post(
side_effect_exprt &expr,
goto_programt &dest,
const irep_idt &mode,
bool result_is_used)
{
goto_programt tmp1, tmp2;
// we have ...(op++)...
INVARIANT_WITH_DIAGNOSTICS(
expr.operands().size() == 1,
"postincrement/postdecrement must have one operand",
expr.find_source_location());
const irep_idt statement=expr.get_statement();
DATA_INVARIANT(
statement == ID_postincrement || statement == ID_postdecrement,
"expects postincrement or postdecrement");
const auto &op = to_unary_expr(expr).op();
const typet &op_type = op.type();
PRECONDITION(
op_type.id() != ID_c_enum_tag && op_type.id() != ID_c_enum &&
op_type.id() != ID_c_bool && op_type.id() != ID_bool);
typet constant_type;
if(op_type.id() == ID_pointer)
constant_type = c_index_type();
else if(is_number(op_type))
constant_type = op_type;
else
{
UNREACHABLE;
}
exprt constant;
if(constant_type.id() == ID_complex)
{
exprt real = from_integer(1, constant_type.subtype());
exprt imag = from_integer(0, constant_type.subtype());
constant = complex_exprt(real, imag, to_complex_type(constant_type));
}
else
constant = from_integer(1, constant_type);
binary_exprt rhs{
op,
statement == ID_postincrement ? ID_plus : ID_minus,
std::move(constant)};
rhs.add_source_location() = expr.source_location();
code_assignt assignment(op, rhs);
assignment.add_source_location()=expr.find_source_location();
convert(assignment, tmp2, mode);
// fix up the expression, if needed
if(result_is_used)
{
exprt tmp = op;
std::string suffix = "post";
if(auto sym_expr = expr_try_dynamic_cast<symbol_exprt>(tmp))
{
const irep_idt &base_name = ns.lookup(*sym_expr).base_name;
suffix += "_" + id2string(base_name);
}
make_temp_symbol(tmp, suffix, dest, mode);
expr.swap(tmp);
}
else
expr.make_nil();
dest.destructive_append(tmp1);
dest.destructive_append(tmp2);
}
void goto_convertt::remove_function_call(
side_effect_expr_function_callt &expr,
goto_programt &dest,
const irep_idt &mode,
bool result_is_used)
{
if(!result_is_used)
{
code_function_callt call(expr.function(), expr.arguments());
call.add_source_location()=expr.source_location();
convert_function_call(call, dest, mode);
expr.make_nil();
return;
}
// get name of function, if available
std::string new_base_name = "return_value";
irep_idt new_symbol_mode = mode;
if(expr.function().id() == ID_symbol)
{
const irep_idt &identifier =
to_symbol_expr(expr.function()).get_identifier();
const symbolt &symbol = ns.lookup(identifier);
new_base_name+='_';
new_base_name+=id2string(symbol.base_name);
new_symbol_mode = symbol.mode;
}
const symbolt &new_symbol = get_fresh_aux_symbol(
expr.type(),
tmp_symbol_prefix,
new_base_name,
expr.find_source_location(),
new_symbol_mode,
symbol_table);
{
code_frontend_declt decl(new_symbol.symbol_expr());
decl.add_source_location()=new_symbol.location;
convert_frontend_decl(decl, dest, mode);
}
{
goto_programt tmp_program2;
code_function_callt call(
new_symbol.symbol_expr(), expr.function(), expr.arguments());
call.add_source_location()=new_symbol.location;
convert_function_call(call, dest, mode);
}
static_cast<exprt &>(expr)=new_symbol.symbol_expr();
}
void goto_convertt::replace_new_object(
const exprt &object,
exprt &dest)
{
if(dest.id()=="new_object")
dest=object;
else
Forall_operands(it, dest)
replace_new_object(object, *it);
}
void goto_convertt::remove_cpp_new(
side_effect_exprt &expr,
goto_programt &dest,
bool result_is_used)
{
const symbolt &new_symbol = get_fresh_aux_symbol(
expr.type(),
tmp_symbol_prefix,
"new_ptr",
expr.find_source_location(),
ID_cpp,
symbol_table);
code_frontend_declt decl(new_symbol.symbol_expr());
decl.add_source_location()=new_symbol.location;
convert_frontend_decl(decl, dest, ID_cpp);
const code_assignt call(new_symbol.symbol_expr(), expr);
if(result_is_used)
static_cast<exprt &>(expr)=new_symbol.symbol_expr();
else
expr.make_nil();
convert(call, dest, ID_cpp);
}
void goto_convertt::remove_cpp_delete(
side_effect_exprt &expr,
goto_programt &dest)
{
DATA_INVARIANT(expr.operands().size() == 1, "cpp_delete expects one operand");
codet tmp(expr.get_statement());
tmp.add_source_location()=expr.source_location();
tmp.copy_to_operands(to_unary_expr(expr).op());
tmp.set(ID_destructor, expr.find(ID_destructor));
convert_cpp_delete(tmp, dest);
expr.make_nil();
}
void goto_convertt::remove_malloc(
side_effect_exprt &expr,
goto_programt &dest,
const irep_idt &mode,
bool result_is_used)
{
if(result_is_used)
{
const symbolt &new_symbol = get_fresh_aux_symbol(
expr.type(),
tmp_symbol_prefix,
"malloc_value",
expr.source_location(),
mode,
symbol_table);
code_frontend_declt decl(new_symbol.symbol_expr());
decl.add_source_location()=new_symbol.location;
convert_frontend_decl(decl, dest, mode);
code_assignt call(new_symbol.symbol_expr(), expr);
call.add_source_location()=expr.source_location();
static_cast<exprt &>(expr)=new_symbol.symbol_expr();
convert(call, dest, mode);
}
else
{
convert(code_expressiont(std::move(expr)), dest, mode);
}
}
void goto_convertt::remove_temporary_object(
side_effect_exprt &expr,
goto_programt &dest)
{
const irep_idt &mode = expr.get(ID_mode);
INVARIANT_WITH_DIAGNOSTICS(
expr.operands().size() <= 1,
"temporary_object takes zero or one operands",
expr.find_source_location());
symbolt &new_symbol = new_tmp_symbol(
expr.type(), "obj", dest, expr.find_source_location(), mode);
if(expr.operands().size()==1)
{
const code_assignt assignment(
new_symbol.symbol_expr(), to_unary_expr(expr).op());
convert(assignment, dest, mode);
}
if(expr.find(ID_initializer).is_not_nil())
{
INVARIANT_WITH_DIAGNOSTICS(
expr.operands().empty(),
"temporary_object takes zero operands",
expr.find_source_location());
exprt initializer=static_cast<const exprt &>(expr.find(ID_initializer));
replace_new_object(new_symbol.symbol_expr(), initializer);
convert(to_code(initializer), dest, mode);
}
static_cast<exprt &>(expr)=new_symbol.symbol_expr();
}
void goto_convertt::remove_statement_expression(
side_effect_exprt &expr,
goto_programt &dest,
const irep_idt &mode,
bool result_is_used)
{
// This is a gcc extension of the form ({ ....; expr; })
// The value is that of the final expression.
// The expression is copied into a temporary before the
// scope is destroyed.
codet &code = to_side_effect_expr_statement_expression(expr).statement();
if(!result_is_used)
{
convert(code, dest, mode);
expr.make_nil();
return;
}
INVARIANT_WITH_DIAGNOSTICS(
code.get_statement() == ID_block,
"statement_expression takes block as operand",
code.find_source_location());
INVARIANT_WITH_DIAGNOSTICS(
!code.operands().empty(),
"statement_expression takes non-empty block as operand",
expr.find_source_location());
// get last statement from block, following labels
codet &last=to_code_block(code).find_last_statement();
source_locationt source_location=last.find_source_location();
symbolt &new_symbol = new_tmp_symbol(
expr.type(), "statement_expression", dest, source_location, mode);
symbol_exprt tmp_symbol_expr(new_symbol.name, new_symbol.type);
tmp_symbol_expr.add_source_location()=source_location;
if(last.get(ID_statement)==ID_expression)
{
// we turn this into an assignment
exprt e=to_code_expression(last).expression();
last=code_assignt(tmp_symbol_expr, e);
last.add_source_location()=source_location;
}
else if(last.get(ID_statement)==ID_assign)
{
exprt e=to_code_assign(last).lhs();
code_assignt assignment(tmp_symbol_expr, e);
assignment.add_source_location()=source_location;
code.operands().push_back(assignment);
}
else
{
UNREACHABLE;
}
{
goto_programt tmp;
convert(code, tmp, mode);
dest.destructive_append(tmp);
}
static_cast<exprt &>(expr)=tmp_symbol_expr;
}
void goto_convertt::remove_overflow(
side_effect_expr_overflowt &expr,
goto_programt &dest,
bool result_is_used,
const irep_idt &mode)
{
const irep_idt &statement = expr.get_statement();
const exprt &lhs = expr.lhs();
const exprt &rhs = expr.rhs();
const exprt &result = expr.result();
if(result.type().id() != ID_pointer)
{
// result of the arithmetic operation is _not_ used, just evaluate side
// effects
exprt tmp = result;
clean_expr(tmp, dest, mode, false);
// the is-there-an-overflow result may be used
if(result_is_used)
{
binary_overflow_exprt overflow_check{
typecast_exprt::conditional_cast(lhs, result.type()),
statement,
typecast_exprt::conditional_cast(rhs, result.type())};
overflow_check.add_source_location() = expr.source_location();
expr.swap(overflow_check);
}
else
expr.make_nil();
}
else
{
const typet &arith_type = to_pointer_type(result.type()).base_type();
irep_idt arithmetic_operation =
statement == ID_overflow_plus
? ID_plus
: statement == ID_overflow_minus
? ID_minus
: statement == ID_overflow_mult ? ID_mult : ID_nil;
CHECK_RETURN(arithmetic_operation != ID_nil);
exprt overflow_with_result = overflow_result_exprt{
typecast_exprt::conditional_cast(lhs, arith_type),
arithmetic_operation,
typecast_exprt::conditional_cast(rhs, arith_type)};
overflow_with_result.add_source_location() = expr.source_location();
if(result_is_used)
make_temp_symbol(overflow_with_result, "overflow_result", dest, mode);
const struct_typet::componentst &result_comps =
to_struct_type(overflow_with_result.type()).components();
CHECK_RETURN(result_comps.size() == 2);
code_assignt result_assignment{
dereference_exprt{result},
typecast_exprt::conditional_cast(
member_exprt{overflow_with_result, result_comps[0]}, arith_type),
expr.source_location()};
convert_assign(result_assignment, dest, mode);
if(result_is_used)
{
member_exprt overflow_check{overflow_with_result, result_comps[1]};
overflow_check.add_source_location() = expr.source_location();
expr.swap(overflow_check);
}
else
expr.make_nil();
}
}
void goto_convertt::remove_side_effect(
side_effect_exprt &expr,
goto_programt &dest,
const irep_idt &mode,
bool result_is_used,
bool address_taken)
{
const irep_idt &statement=expr.get_statement();
if(statement==ID_function_call)
remove_function_call(
to_side_effect_expr_function_call(expr), dest, mode, result_is_used);
else if(statement==ID_assign ||
statement==ID_assign_plus ||
statement==ID_assign_minus ||
statement==ID_assign_mult ||
statement==ID_assign_div ||
statement==ID_assign_bitor ||
statement==ID_assign_bitxor ||
statement==ID_assign_bitand ||
statement==ID_assign_lshr ||
statement==ID_assign_ashr ||
statement==ID_assign_shl ||
statement==ID_assign_mod)
remove_assignment(expr, dest, result_is_used, address_taken, mode);
else if(statement==ID_postincrement ||
statement==ID_postdecrement)
remove_post(expr, dest, mode, result_is_used);
else if(statement==ID_preincrement ||
statement==ID_predecrement)
remove_pre(expr, dest, result_is_used, address_taken, mode);
else if(statement==ID_cpp_new ||
statement==ID_cpp_new_array)
remove_cpp_new(expr, dest, result_is_used);
else if(statement==ID_cpp_delete ||
statement==ID_cpp_delete_array)
remove_cpp_delete(expr, dest);
else if(statement==ID_allocate)
remove_malloc(expr, dest, mode, result_is_used);
else if(statement==ID_temporary_object)
remove_temporary_object(expr, dest);
else if(statement==ID_statement_expression)
remove_statement_expression(expr, dest, mode, result_is_used);
else if(statement==ID_nondet)
{
// these are fine
}
else if(statement==ID_skip)
{
expr.make_nil();
}
else if(statement==ID_throw)
{
codet code = code_expressiont(side_effect_expr_throwt(
expr.find(ID_exception_list), expr.type(), expr.source_location()));
code.op0().operands().swap(expr.operands());
code.add_source_location() = expr.source_location();
dest.add(goto_programt::instructiont(
std::move(code), expr.source_location(), THROW, nil_exprt(), {}));
// the result can't be used, these are void
expr.make_nil();
}
else if(
statement == ID_overflow_plus || statement == ID_overflow_minus ||
statement == ID_overflow_mult)
{
remove_overflow(
to_side_effect_expr_overflow(expr), dest, result_is_used, mode);
}
else
{
UNREACHABLE;
}
}