-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathpointer_arithmetic.cpp
87 lines (77 loc) · 1.97 KB
/
pointer_arithmetic.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
/*******************************************************************\
Module:
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#include "pointer_arithmetic.h"
#include <util/arith_tools.h>
#include <util/pointer_expr.h>
#include <util/std_expr.h>
pointer_arithmetict::pointer_arithmetict(const exprt &src)
{
pointer.make_nil();
offset.make_nil();
read(src);
}
void pointer_arithmetict::read(const exprt &src)
{
if(src.id()==ID_plus)
{
for(const auto &op : src.operands())
{
if(op.type().id() == ID_pointer)
read(op);
else
add_to_offset(op);
}
}
else if(src.id()==ID_minus)
{
auto const &minus_src = to_minus_expr(src);
read(minus_src.op0());
const unary_minus_exprt unary_minus(
minus_src.op1(), minus_src.op1().type());
add_to_offset(unary_minus);
}
else if(src.id()==ID_address_of)
{
auto const &address_of_src = to_address_of_expr(src);
if(address_of_src.op().id() == ID_index)
{
const index_exprt &index_expr = to_index_expr(address_of_src.op());
if(index_expr.index().is_zero())
make_pointer(address_of_src);
else
{
add_to_offset(index_expr.index());
// produce &x[0] + i instead of &x[i]
auto new_address_of_src = address_of_src;
to_index_expr(new_address_of_src.op()).index() =
from_integer(0, index_expr.index().type());
make_pointer(new_address_of_src);
}
}
else
make_pointer(address_of_src);
}
else
make_pointer(src);
}
void pointer_arithmetict::add_to_offset(const exprt &src)
{
if(offset.is_nil())
offset=src;
else if(offset.id()==ID_plus)
offset.copy_to_operands(src);
else
{
offset =
plus_exprt(offset, typecast_exprt::conditional_cast(src, offset.type()));
}
}
void pointer_arithmetict::make_pointer(const exprt &src)
{
if(pointer.is_nil())
pointer=src;
else
add_to_offset(src);
}