forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoto_program.h
1250 lines (1085 loc) · 36.4 KB
/
goto_program.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
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************\
Module: Concrete Goto Program
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Concrete Goto Program
#ifndef CPROVER_GOTO_PROGRAMS_GOTO_PROGRAM_H
#define CPROVER_GOTO_PROGRAMS_GOTO_PROGRAM_H
#include <util/invariant.h>
#include <util/source_location.h>
#include "goto_instruction_code.h"
#include <iosfwd>
#include <limits>
#include <list>
#include <set>
#include <string>
class code_gotot;
class namespacet;
enum class validation_modet;
/// The type of an instruction in a GOTO program.
enum goto_program_instruction_typet
{
NO_INSTRUCTION_TYPE = 0,
GOTO = 1, // branch, possibly guarded
ASSUME = 2, // non-failing guarded self loop
ASSERT = 3, // assertions
OTHER = 4, // anything else
SKIP = 5, // just advance the PC
START_THREAD = 6, // spawns an asynchronous thread
END_THREAD = 7, // end the current thread
LOCATION = 8, // semantically like SKIP
END_FUNCTION = 9, // exit point of a function
ATOMIC_BEGIN = 10, // marks a block without interleavings
ATOMIC_END = 11, // end of a block without interleavings
SET_RETURN_VALUE = 12, // set function return value (no control-flow change)
ASSIGN = 13, // assignment lhs:=rhs
DECL = 14, // declare a local variable
DEAD = 15, // marks the end-of-live of a local variable
FUNCTION_CALL = 16, // call a function
THROW = 17, // throw an exception
CATCH = 18, // push, pop or enter an exception handler
INCOMPLETE_GOTO = 19 // goto where target is yet to be determined
};
std::ostream &operator<<(std::ostream &, goto_program_instruction_typet);
/// A generic container class for the GOTO intermediate representation of one
/// function.
///
/// A function is represented by a std::list of instructions. Execution starts
/// in the first instruction of the list. Then, the execution of the i-th
/// instruction is followed by the execution of the (i+1)-th instruction unless
/// instruction i jumps to some other instruction in the list. See the internal
/// class instructiont for additional details
///
/// Although it is straightforward to compute the control flow graph (CFG) of a
/// function from the list of instructions and the goto target locations in
/// instructions, the GOTO intermediate representation is _not_ regarded as the
/// CFG of a function. See instead the class cfg_baset, which is based on grapht
/// and allows for easier implementation of generic graph algorithms (e.g.,
/// dominator analysis).
class goto_programt
{
public:
/// Copying is deleted as this class contains pointers that cannot be copied
goto_programt(const goto_programt &)=delete;
goto_programt &operator=(const goto_programt &)=delete;
// Move operations need to be explicitly enabled as they are deleted with the
// copy operations
// default for move operations isn't available on Windows yet, so define
// explicitly (see https://msdn.microsoft.com/en-us/library/hh567368.aspx
// under "Defaulted and Deleted Functions")
goto_programt(goto_programt &&other):
instructions(std::move(other.instructions))
{
}
goto_programt &operator=(goto_programt &&other)
{
instructions=std::move(other.instructions);
return *this;
}
/// This class represents an instruction in the GOTO intermediate
/// representation. Three fields are key:
///
/// - type: an enum value describing the action performed by this instruction
/// - guard: an (arbitrarily complex) expression (usually an \ref exprt) of
/// Boolean type
/// - code: a code statement (usually a \ref goto_instruction_codet)
///
/// The meaning of an instruction node depends on the `type` field. Different
/// kinds of instructions make use of the fields `guard` and `code` for
/// different purposes. We list below, using a mixture of pseudo code and
/// plain English, the meaning of different kinds of instructions.
/// We use `guard`, `code`, and `targets` to mean the value of the
/// respective fields in this class:
///
/// - GOTO:
/// goto `targets` if and only if `guard` is true.
/// More than one target is deprecated. Its semantics was a
/// non-deterministic choice.
/// - SET_RETURN_VALUE:
/// Set the value returned by `return_value()`. The control flow is
/// not altered.
/// Many analysis tools remove these instructions before they start.
/// - DECL:
/// Introduces a symbol denoted by the field `code` (an instance of
/// code_declt), the life-time of which is bounded by a corresponding DEAD
/// instruction. Non-static symbols must be DECL'd before they are used.
/// - DEAD:
/// Ends the life of the symbol denoted by the field `code`.
/// After a DEAD instruction the symbol must be DECL'd again before use.
/// - FUNCTION_CALL:
/// Invoke the function returned by `call_function` with the arguments
/// returned by `call_arguments`, then assign the return value (if any)
/// to `call_lhs`
/// - ASSIGN:
/// Update the left-hand side of `code` (an instance of code_assignt) to
/// the value of the right-hand side.
/// - OTHER:
/// Execute the `code` (an instance of goto_instruction_codet of kind
/// ID_fence, ID_printf, ID_array_copy, ID_array_set, ID_input,
/// ID_output, ...).
/// - ASSUME:
/// This thread of execution waits for `guard` to evaluate to true.
/// Assume does not "retro-actively" affect the thread or any ASSERTs.
/// - ASSERT:
/// Using ASSERT instructions is the one and only way to express
/// properties to be verified. An assertion is true / safe if `guard`
/// is true in all possible executions, otherwise it is false / unsafe.
/// The status of the assertion does not affect execution in any way.
/// - SKIP, LOCATION:
/// No-op.
/// - ATOMIC_BEGIN, ATOMIC_END:
/// When a thread executes ATOMIC_BEGIN, no thread other will be able to
/// execute any instruction until the same thread executes ATOMIC_END.
/// Concurrency is not supported by all analysis tools.
/// - END_FUNCTION:
/// Must occur as the last instruction of the list and nowhere else.
/// - START_THREAD:
/// Create a new thread and run the code of this function starting from
/// targets[0]. Quite often the instruction pointed by targets[0] will be
/// just a FUNCTION_CALL, followed by an END_THREAD.
/// Concurrency is not supported by all analysis tools.
/// - END_THREAD:
/// Terminate the calling thread.
/// Concurrency is not supported by all analysis tools.
/// - THROW:
/// throw `exception1`, ..., `exceptionN`
/// where the list of exceptions is extracted from the `code` field
/// Many analysis tools remove these instructions before they start.
/// - CATCH, when code.find(ID_exception_list) is non-empty:
/// Establishes that from here to the next occurrence of CATCH with an
/// empty list (see below) if
/// - `exception1` is thrown, then goto `target1`,
/// - ...
/// - `exceptionN` is thrown, then goto `targetN`.
/// The list of exceptions is obtained from the `code` field and the list
/// of targets from the `targets` field.
/// - CATCH, when empty code.find(ID_exception_list) is empty:
/// clears all the catch clauses established as per the above in this
/// function?
/// Many analysis tools remove these instructions before they start.
/// - INCOMPLETE GOTO:
/// goto for which the target is yet to be determined. The target set
/// shall be empty
class instructiont final
{
protected:
/// Do not read or modify directly -- use code() and code_nonconst() instead
goto_instruction_codet _code;
public:
/// Get the code represented by this instruction
const goto_instruction_codet &code() const
{
return _code;
}
/// Set the code represented by this instruction
goto_instruction_codet &code_nonconst()
{
return _code;
}
/// Get the lhs of the assignment for ASSIGN
const exprt &assign_lhs() const
{
PRECONDITION(is_assign());
return to_code_assign(_code).lhs();
}
/// Get the lhs of the assignment for ASSIGN
exprt &assign_lhs_nonconst()
{
PRECONDITION(is_assign());
return to_code_assign(_code).lhs();
}
/// Get the rhs of the assignment for ASSIGN
const exprt &assign_rhs() const
{
PRECONDITION(is_assign());
return to_code_assign(_code).rhs();
}
/// Get the rhs of the assignment for ASSIGN
exprt &assign_rhs_nonconst()
{
PRECONDITION(is_assign());
return to_code_assign(_code).rhs();
}
/// Get the declared symbol for DECL
const symbol_exprt &decl_symbol() const
{
PRECONDITION(is_decl());
auto &decl = expr_checked_cast<code_declt>(_code);
return decl.symbol();
}
/// Get the declared symbol for DECL
symbol_exprt &decl_symbol()
{
PRECONDITION(is_decl());
auto &decl = expr_checked_cast<code_declt>(_code);
return decl.symbol();
}
/// Get the symbol for DEAD
const symbol_exprt &dead_symbol() const
{
PRECONDITION(is_dead());
return to_code_dead(_code).symbol();
}
/// Get the symbol for DEAD
symbol_exprt &dead_symbol()
{
PRECONDITION(is_dead());
return to_code_dead(_code).symbol();
}
/// Get the return value of a SET_RETURN_VALUE instruction
const exprt &return_value() const
{
PRECONDITION(is_set_return_value());
return to_code_return(_code).return_value();
}
/// Get the return value of a SET_RETURN_VALUE instruction
exprt &return_value()
{
PRECONDITION(is_set_return_value());
return to_code_return(_code).return_value();
}
/// Get the function that is called for FUNCTION_CALL
const exprt &call_function() const
{
PRECONDITION(is_function_call());
return to_code_function_call(_code).function();
}
/// Get the function that is called for FUNCTION_CALL
exprt &call_function()
{
PRECONDITION(is_function_call());
return to_code_function_call(_code).function();
}
/// Get the lhs of a FUNCTION_CALL (may be nil)
const exprt &call_lhs() const
{
PRECONDITION(is_function_call());
return to_code_function_call(_code).lhs();
}
/// Get the lhs of a FUNCTION_CALL (may be nil)
exprt &call_lhs()
{
PRECONDITION(is_function_call());
return to_code_function_call(_code).lhs();
}
/// Get the arguments of a FUNCTION_CALL
const exprt::operandst &call_arguments() const
{
PRECONDITION(is_function_call());
return to_code_function_call(_code).arguments();
}
/// Get the arguments of a FUNCTION_CALL
exprt::operandst &call_arguments()
{
PRECONDITION(is_function_call());
return to_code_function_call(_code).arguments();
}
/// Get the statement for OTHER
const goto_instruction_codet &get_other() const
{
PRECONDITION(is_other());
return _code;
}
/// Set the statement for OTHER
void set_other(goto_instruction_codet &c)
{
PRECONDITION(is_other());
_code = std::move(c);
}
/// The location of the instruction in the source file.
/// Use source_location() to access.
protected:
source_locationt _source_location;
public:
const source_locationt &source_location() const
{
return _source_location;
}
source_locationt &source_location_nonconst()
{
return _source_location;
}
/// What kind of instruction?
goto_program_instruction_typet type() const
{
return _type;
}
protected:
// Use type() to access. To prevent malformed instructions, no non-const
// access method is provided.
goto_program_instruction_typet _type;
/// Guard for gotos, assume, assert
/// Use condition() method to access.
exprt guard;
public:
/// Does this instruction have a condition?
bool has_condition() const
{
return is_goto() || is_incomplete_goto() || is_assume() || is_assert();
}
/// Get the condition of gotos, assume, assert
const exprt &condition() const
{
PRECONDITION(has_condition());
return guard;
}
/// Get the condition of gotos, assume, assert
exprt &condition_nonconst()
{
PRECONDITION(has_condition());
return guard;
}
// The below will eventually become a single target only.
/// The target for gotos and for start_thread nodes
typedef std::list<instructiont>::iterator targett;
typedef std::list<instructiont>::const_iterator const_targett;
typedef std::list<targett> targetst;
typedef std::list<const_targett> const_targetst;
/// A total order over `targett` and `const_targett`. Note that the specific
/// ordering may vary from execution to execution for it uses comparison on
/// virtual memory locations. If a consistent ordering is required,
/// implement a comparison on some aspect of `instructiont` that is stable
/// at the particular call site.
struct target_less_than // NOLINT(readability/identifiers)
{
static inline bool
order_const_target(const const_targett i1, const const_targett i2)
{
const instructiont &_i1 = *i1;
const instructiont &_i2 = *i2;
return &_i1 < &_i2;
}
inline bool
operator()(const const_targett &i1, const const_targett &i2) const
{
return order_const_target(i1, i2);
}
inline bool operator()(const targett &i1, const targett &i2) const
{
return &(*i1) < &(*i2);
}
};
/// The list of successor instructions
targetst targets;
/// Returns the first (and only) successor for the usual case of a single
/// target
const_targett get_target() const
{
PRECONDITION(targets.size() == 1);
return targets.front();
}
/// Returns the first (and only) successor for the usual case of a single
/// target
targett get_target()
{
PRECONDITION(targets.size()==1);
return targets.front();
}
/// Sets the first (and only) successor for the usual case of a single
/// target
void set_target(targett t)
{
targets.clear();
targets.push_back(t);
}
bool has_target() const
{
return !targets.empty();
}
/// Goto target labels
typedef std::list<irep_idt> labelst;
labelst labels;
// will go away
std::set<targett, target_less_than> incoming_edges;
/// Is this node a branch target?
bool is_target() const
{ return target_number!=nil_target; }
/// Clear the node
void clear(goto_program_instruction_typet __type)
{
_type = __type;
targets.clear();
guard=true_exprt();
_code.make_nil();
}
/// Transforms an existing instruction into a skip,
/// retaining the source_location
void turn_into_skip()
{
clear(SKIP);
}
/// Transforms either an assertion or a GOTO instruction
/// into an assumption, with the same condition.
void turn_into_assume()
{
PRECONDITION(_type == GOTO || _type == ASSERT);
_type = ASSUME;
targets.clear();
_code.make_nil();
}
void complete_goto(targett _target)
{
PRECONDITION(_type == INCOMPLETE_GOTO);
_code.make_nil();
targets.push_back(_target);
_type = GOTO;
}
// clang-format off
bool is_goto () const { return _type == GOTO; }
bool is_set_return_value() const { return _type == SET_RETURN_VALUE; }
bool is_assign () const { return _type == ASSIGN; }
bool is_function_call () const { return _type == FUNCTION_CALL; }
bool is_throw () const { return _type == THROW; }
bool is_catch () const { return _type == CATCH; }
bool is_skip () const { return _type == SKIP; }
bool is_location () const { return _type == LOCATION; }
bool is_other () const { return _type == OTHER; }
bool is_decl () const { return _type == DECL; }
bool is_dead () const { return _type == DEAD; }
bool is_assume () const { return _type == ASSUME; }
bool is_assert () const { return _type == ASSERT; }
bool is_atomic_begin () const { return _type == ATOMIC_BEGIN; }
bool is_atomic_end () const { return _type == ATOMIC_END; }
bool is_start_thread () const { return _type == START_THREAD; }
bool is_end_thread () const { return _type == END_THREAD; }
bool is_end_function () const { return _type == END_FUNCTION; }
bool is_incomplete_goto () const { return _type == INCOMPLETE_GOTO; }
// clang-format on
instructiont():
instructiont(NO_INSTRUCTION_TYPE) // NOLINT(runtime/explicit)
{
}
explicit instructiont(goto_program_instruction_typet __type)
: _code(static_cast<const goto_instruction_codet &>(get_nil_irep())),
_source_location(source_locationt::nil()),
_type(__type),
guard(true_exprt())
{
}
/// Constructor that can set all members, passed by value
instructiont(
goto_instruction_codet __code,
source_locationt __source_location,
goto_program_instruction_typet __type,
exprt _guard,
targetst _targets)
: _code(std::move(__code)),
_source_location(std::move(__source_location)),
_type(__type),
guard(std::move(_guard)),
targets(std::move(_targets))
{
}
/// Swap two instructions
void swap(instructiont &instruction)
{
using std::swap;
swap(instruction._code, _code);
swap(instruction._source_location, _source_location);
swap(instruction._type, _type);
swap(instruction.guard, guard);
swap(instruction.targets, targets);
swap(instruction.labels, labels);
}
/// Uniquely identify an invalid target or location
static const unsigned nil_target=
std::numeric_limits<unsigned>::max();
/// A globally unique number to identify a program location.
/// It's guaranteed to be ordered in program order within
/// one goto_program.
unsigned location_number = 0;
/// Number unique per function to identify loops
unsigned loop_number = 0;
/// A number to identify branch targets.
/// This is \ref nil_target if it's not a target.
unsigned target_number = nil_target;
/// Returns true if the instruction is a backwards branch.
bool is_backwards_goto() const
{
if(!is_goto())
return false;
for(const auto &t : targets)
if(t->location_number<=location_number)
return true;
return false;
}
std::string to_string() const
{
std::ostringstream instruction_id_builder;
instruction_id_builder << _type;
return instruction_id_builder.str();
}
/// Syntactic equality: two instructiont are equal if they have the same
/// type, code, guard, number of targets, and labels. All other members can
/// only be evaluated in the context of a goto_programt (see
/// goto_programt::equals).
bool equals(const instructiont &other) const;
/// Check that the instruction is well-formed
///
/// The validation mode indicates whether well-formedness check failures are
/// reported via DATA_INVARIANT violations or exceptions.
void validate(const namespacet &ns, const validation_modet vm) const;
/// Apply given transformer to all expressions; no return value
/// means no change needed.
void transform(std::function<std::optional<exprt>(exprt)>);
/// Apply given function to all expressions
void apply(std::function<void(const exprt &)>) const;
/// Output this instruction to the given stream
std::ostream &output(std::ostream &) const;
};
// Never try to change this to vector-we mutate the list while iterating
typedef std::list<instructiont> instructionst;
typedef instructionst::iterator targett;
typedef instructionst::const_iterator const_targett;
typedef std::list<targett> targetst;
typedef std::list<const_targett> const_targetst;
// NOLINTNEXTLINE(readability/identifiers)
typedef instructiont::target_less_than target_less_than;
/// The list of instructions in the goto program
instructionst instructions;
/// Convert a const_targett to a targett - use with care and avoid
/// whenever possible
targett const_cast_target(const_targett t)
{
return instructions.erase(t, t);
}
/// Dummy for templates with possible const contexts
const_targett const_cast_target(const_targett t) const
{
return t;
}
template <typename Target>
std::list<Target> get_successors(Target target) const;
void compute_incoming_edges();
/// Insertion that preserves jumps to "target".
void insert_before_swap(targett target)
{
PRECONDITION(target!=instructions.end());
const auto next=std::next(target);
instructions.insert(next, instructiont())->swap(*target);
}
/// Insertion that preserves jumps to "target".
/// The instruction is destroyed.
///
/// Turns:
/// ```
/// ...->[a]->...
/// ^
/// target
/// ```
///
/// Into:
/// ```
/// ...->[i]->[a]->...
/// ^
/// target
/// ```
///
/// So that jumps to `a` now jump to the newly inserted `i`.
void insert_before_swap(targett target, instructiont &instruction)
{
insert_before_swap(target);
target->swap(instruction);
}
/// Insertion that preserves jumps to "target".
/// The program p is destroyed.
void insert_before_swap(
targett target,
goto_programt &p)
{
PRECONDITION(target!=instructions.end());
if(p.instructions.empty())
return;
insert_before_swap(target, p.instructions.front());
auto next=std::next(target);
p.instructions.erase(p.instructions.begin());
instructions.splice(next, p.instructions);
}
/// Insertion before the instruction pointed-to by the given instruction
/// iterator `target`.
/// \return newly inserted location
targett insert_before(const_targett target)
{
return instructions.insert(target, instructiont());
}
/// Insertion before the instruction pointed-to by the given instruction
/// iterator `target`.
/// \return newly inserted location
targett insert_before(const_targett target, const instructiont &i)
{
return instructions.insert(target, i);
}
/// Insertion after the instruction pointed-to by the given instruction
/// iterator `target`.
/// \return newly inserted location
targett insert_after(const_targett target)
{
return instructions.insert(std::next(target), instructiont());
}
/// Insertion after the instruction pointed-to by the given instruction
/// iterator `target`.
/// \return newly inserted location
targett insert_after(const_targett target, const instructiont &i)
{
return instructions.insert(std::next(target), i);
}
/// Appends the given program `p` to `*this`. `p` is destroyed.
void destructive_append(goto_programt &p)
{
instructions.splice(instructions.end(),
p.instructions);
}
/// Inserts the given program `p` before `target`.
/// The program `p` is destroyed.
void destructive_insert(
const_targett target,
goto_programt &p)
{
instructions.splice(target, p.instructions);
}
/// Adds a given instruction at the end.
/// \return The newly added instruction.
targett add(instructiont &&instruction)
{
instructions.push_back(std::move(instruction));
return --instructions.end();
}
/// Adds an instruction at the end.
/// \return The newly added instruction.
targett add_instruction()
{
return add(instructiont());
}
/// Adds an instruction of given type at the end.
/// \return The newly added instruction.
targett add_instruction(goto_program_instruction_typet type)
{
return add(instructiont(type));
}
/// Output goto-program to given stream
std::ostream &output(std::ostream &out) const;
/// Compute the target numbers
void compute_target_numbers();
/// Compute location numbers
void compute_location_numbers(unsigned &nr)
{
for(auto &i : instructions)
{
INVARIANT(
nr != std::numeric_limits<unsigned>::max(),
"Too many location numbers assigned");
i.location_number=nr++;
}
}
/// Compute location numbers
void compute_location_numbers()
{
unsigned nr=0;
compute_location_numbers(nr);
}
/// Compute loop numbers
void compute_loop_numbers();
/// Update all indices
void update();
/// Human-readable loop name
static irep_idt
loop_id(const irep_idt &function_id, const instructiont &instruction)
{
return id2string(function_id) + "." +
std::to_string(instruction.loop_number);
}
/// Is the program empty?
bool empty() const
{
return instructions.empty();
}
/// Constructor
goto_programt()
{
}
~goto_programt()
{
}
/// Swap the goto program
void swap(goto_programt &program)
{
program.instructions.swap(instructions);
}
/// Clear the goto program
void clear()
{
instructions.clear();
}
/// Get an instruction iterator pointing to the END_FUNCTION instruction of
/// the goto program
targett get_end_function()
{
PRECONDITION(!instructions.empty());
const auto end_function=std::prev(instructions.end());
DATA_INVARIANT(end_function->is_end_function(),
"goto program ends on END_FUNCTION");
return end_function;
}
/// Get an instruction iterator pointing to the END_FUNCTION instruction of
/// the goto program
const_targett get_end_function() const
{
PRECONDITION(!instructions.empty());
const auto end_function=std::prev(instructions.end());
DATA_INVARIANT(end_function->is_end_function(),
"goto program ends on END_FUNCTION");
return end_function;
}
/// Copy a full goto program, preserving targets
void copy_from(const goto_programt &src);
/// Does the goto program have an assertion?
bool has_assertion() const;
typedef std::set<irep_idt> decl_identifierst;
/// get the variables in decl statements
void get_decl_identifiers(decl_identifierst &decl_identifiers) const;
/// Syntactic equality: two goto_programt are equal if, and only if, they have
/// the same number of instructions, each pair of instructions compares equal,
/// and relative jumps have the same distance.
bool equals(const goto_programt &other) const;
/// Check that the goto program is well-formed
///
/// The validation mode indicates whether well-formedness check failures are
/// reported via DATA_INVARIANT violations or exceptions.
void validate(const namespacet &ns, const validation_modet vm) const
{
for(const instructiont &ins : instructions)
{
ins.validate(ns, vm);
}
}
static instructiont make_set_return_value(
exprt return_value,
const source_locationt &l = source_locationt::nil())
{
return instructiont(
code_returnt(std::move(return_value)),
l,
SET_RETURN_VALUE,
nil_exprt(),
{});
}
static instructiont make_set_return_value(
const code_returnt &code,
const source_locationt &l = source_locationt::nil()) = delete;
static instructiont
make_skip(const source_locationt &l = source_locationt::nil())
{
return instructiont(
static_cast<const goto_instruction_codet &>(get_nil_irep()),
l,
SKIP,
nil_exprt(),
{});
}
static instructiont make_location(const source_locationt &l)
{
return instructiont(
static_cast<const goto_instruction_codet &>(get_nil_irep()),
l,
LOCATION,
nil_exprt(),
{});
}
static instructiont
make_throw(const source_locationt &l = source_locationt::nil())
{
return instructiont(
static_cast<const goto_instruction_codet &>(get_nil_irep()),
l,
THROW,
nil_exprt(),
{});
}
static instructiont
make_catch(const source_locationt &l = source_locationt::nil())
{
return instructiont(
static_cast<const goto_instruction_codet &>(get_nil_irep()),
l,
CATCH,
nil_exprt(),
{});
}
static instructiont make_assertion(
const exprt &g,
const source_locationt &l = source_locationt::nil())
{
return instructiont(
static_cast<const goto_instruction_codet &>(get_nil_irep()),
l,
ASSERT,
exprt(g),
{});
}
static instructiont make_assumption(
const exprt &g,
const source_locationt &l = source_locationt::nil())
{
return instructiont(
static_cast<const goto_instruction_codet &>(get_nil_irep()),
l,
ASSUME,
g,
{});
}
static instructiont make_other(
const goto_instruction_codet &_code,
const source_locationt &l = source_locationt::nil())
{
return instructiont(_code, l, OTHER, nil_exprt(), {});
}
static instructiont make_decl(
const symbol_exprt &symbol,
const source_locationt &l = source_locationt::nil())
{
return instructiont(code_declt(symbol), l, DECL, nil_exprt(), {});
}
static instructiont make_dead(
const symbol_exprt &symbol,
const source_locationt &l = source_locationt::nil())
{
return instructiont(code_deadt(symbol), l, DEAD, nil_exprt(), {});
}
static instructiont
make_atomic_begin(const source_locationt &l = source_locationt::nil())
{
return instructiont(
static_cast<const goto_instruction_codet &>(get_nil_irep()),
l,
ATOMIC_BEGIN,
nil_exprt(),
{});
}
static instructiont
make_atomic_end(const source_locationt &l = source_locationt::nil())
{
return instructiont(
static_cast<const goto_instruction_codet &>(get_nil_irep()),
l,
ATOMIC_END,
nil_exprt(),
{});
}
static instructiont