-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathallocate_objects.h
124 lines (102 loc) · 3.71 KB
/
allocate_objects.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
/*******************************************************************\
Module:
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#ifndef CPROVER_UTIL_ALLOCATE_OBJECTS_H
#define CPROVER_UTIL_ALLOCATE_OBJECTS_H
#include <util/namespace.h>
#include <util/std_code.h>
/// Selects the kind of objects allocated
enum class lifetimet
{
/// Allocate local objects with automatic lifetime
AUTOMATIC_LOCAL,
/// Allocate global objects with static lifetime
STATIC_GLOBAL,
/// Allocate dynamic objects (using ALLOCATE)
DYNAMIC
};
class allocate_objectst
{
public:
allocate_objectst(
const irep_idt &symbol_mode,
const source_locationt &source_location,
const irep_idt &name_prefix,
symbol_table_baset &symbol_table)
: symbol_mode(symbol_mode),
source_location(source_location),
name_prefix(name_prefix),
symbol_table(symbol_table),
ns(symbol_table)
{
}
exprt allocate_object(
code_blockt &assignments,
const exprt &target_expr,
const typet &allocate_type,
const lifetimet lifetime,
const irep_idt &basename_prefix = "tmp");
exprt allocate_automatic_local_object(
code_blockt &assignments,
const exprt &target_expr,
const typet &allocate_type,
const irep_idt &basename_prefix = "tmp");
exprt allocate_static_global_object(
code_blockt &assignments,
const exprt &target_expr,
const typet &allocate_type,
const irep_idt &basename_prefix = "tmp");
/// Generates code for allocating a dynamic object. A new variable with
/// basename prefix `alloc_site` is introduced to which the allocated memory
/// is assigned.
/// Then, the variable is assigned to `target_expr`. For example, with
/// `target_expr` being `*p` the following code is generated:
///
/// `alloc_site$1 = ALLOCATE(object_size, FALSE);`
/// `*p = alloc_site$1;`
///
/// \param output_code: Code block to which the necessary code is added
/// \param target_expr: A pointer to the allocated memory will be assigned to
/// this (lvalue) expression
/// \param allocate_type: Type of the object allocated
/// \return The pointer to the allocated memory, or an empty expression
/// when `allocate_type` is void
exprt allocate_dynamic_object_symbol(
code_blockt &output_code,
const exprt &target_expr,
const typet &allocate_type);
/// Generate the same code as \ref allocate_dynamic_object_symbol, but
/// return a dereference_exprt that dereferences the newly created pointer
/// to the allocated memory.
exprt allocate_dynamic_object(
code_blockt &output_code,
const exprt &target_expr,
const typet &allocate_type);
symbol_exprt allocate_automatic_local_object(
const typet &allocate_type,
const irep_idt &basename_prefix = "tmp");
void add_created_symbol(const symbolt &symbol);
void declare_created_symbols(code_blockt &init_code);
void mark_created_symbols_as_input(code_blockt &init_code);
private:
const irep_idt symbol_mode;
const source_locationt source_location;
const irep_idt name_prefix;
symbol_table_baset &symbol_table;
const namespacet ns;
std::vector<irep_idt> symbols_created;
exprt allocate_non_dynamic_object(
code_blockt &assignments,
const exprt &target_expr,
const typet &allocate_type,
const bool static_lifetime,
const irep_idt &basename_prefix);
};
/// Create code allocating an object of size `size` and assigning it to `lhs`
/// \param lhs: pointer which will be allocated
/// \param size: size of the object
/// \return code allocating the object and assigning it to `lhs`
code_frontend_assignt
make_allocate_code(const symbol_exprt &lhs, const exprt &size);
#endif // CPROVER_UTIL_ALLOCATE_OBJECTS_H