-
Notifications
You must be signed in to change notification settings - Fork 32
/
ConstantPropagation.java
224 lines (189 loc) 路 6.05 KB
/
ConstantPropagation.java
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
package it.unive.lisa.test.imp.tutorial;
import it.unive.lisa.analysis.SemanticDomain.Satisfiability;
import it.unive.lisa.analysis.SemanticException;
import it.unive.lisa.analysis.nonrelational.BaseNonRelationalValueDomain;
import it.unive.lisa.cfg.type.Type;
import it.unive.lisa.symbolic.value.BinaryOperator;
import it.unive.lisa.symbolic.value.Constant;
import it.unive.lisa.symbolic.value.Identifier;
import it.unive.lisa.symbolic.value.TernaryOperator;
import it.unive.lisa.symbolic.value.UnaryOperator;
public class ConstantPropagation extends BaseNonRelationalValueDomain<ConstantPropagation> {
public static final ConstantPropagation TOP = new ConstantPropagation(true, false);
private static final ConstantPropagation BOTTOM = new ConstantPropagation(false, true);
private final boolean isTop, isBottom;
private final Integer value;
public ConstantPropagation() {
this(null, true, false);
}
private ConstantPropagation(Integer value, boolean isTop, boolean isBottom) {
this.value = value;
this.isTop = isTop;
this.isBottom = isBottom;
}
private ConstantPropagation(Integer value) {
this(value, false, false);
}
private ConstantPropagation(boolean isTop, boolean isBottom) {
this(null, isTop, isBottom);
}
@Override
public ConstantPropagation top() {
return TOP;
}
@Override
public boolean isTop() {
return isTop;
}
@Override
public ConstantPropagation bottom() {
return BOTTOM;
}
@Override
public boolean isBottom() {
return isBottom;
}
@Override
public String representation() {
return isTop() ? "Non-constant" : isBottom() ? "Bot" : value.toString();
}
@Override
protected ConstantPropagation evalNullConstant() {
return top();
}
@Override
protected ConstantPropagation evalNonNullConstant(Constant constant) {
if (constant.getValue() instanceof Integer)
return new ConstantPropagation((Integer) constant.getValue());
return top();
}
@Override
protected ConstantPropagation evalTypeConversion(Type type, ConstantPropagation arg) {
return top();
}
@Override
protected ConstantPropagation evalUnaryExpression(UnaryOperator operator, ConstantPropagation arg) {
switch (operator) {
case NUMERIC_NEG:
return new ConstantPropagation(0 - value);
case STRING_LENGTH:
return top();
default:
return top();
}
}
@Override
protected ConstantPropagation evalBinaryExpression(BinaryOperator operator, ConstantPropagation left,
ConstantPropagation right) {
switch (operator) {
case NUMERIC_ADD:
return new ConstantPropagation(left.value + right.value);
case NUMERIC_DIV:
if (left.value % right.value != 0)
return top();
else
return new ConstantPropagation(left.value / right.value);
case NUMERIC_MOD:
return new ConstantPropagation(left.value % right.value);
case NUMERIC_MUL:
return new ConstantPropagation(left.value * right.value);
case NUMERIC_SUB:
return new ConstantPropagation(left.value - right.value);
default:
return top();
}
}
@Override
protected ConstantPropagation evalTernaryExpression(TernaryOperator operator, ConstantPropagation left,
ConstantPropagation middle, ConstantPropagation right) {
return top();
}
@Override
protected ConstantPropagation lubAux(ConstantPropagation other) throws SemanticException {
return equals(other) ? other : top();
}
@Override
protected ConstantPropagation wideningAux(ConstantPropagation other) throws SemanticException {
return lubAux(other);
}
@Override
protected boolean lessOrEqualAux(ConstantPropagation other) throws SemanticException {
return value == other.value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (isBottom ? 1231 : 1237);
result = prime * result + (isTop ? 1231 : 1237);
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ConstantPropagation other = (ConstantPropagation) obj;
if (isBottom != other.isBottom)
return false;
if (isTop != other.isTop)
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
@Override
protected Satisfiability satisfiesIdentifier(Identifier identifier) {
return Satisfiability.UNKNOWN;
}
@Override
protected Satisfiability satisfiesNullConstant() {
return Satisfiability.UNKNOWN;
}
@Override
protected Satisfiability satisfiesNonNullConstant(Constant constant) {
return Satisfiability.UNKNOWN;
}
@Override
protected Satisfiability satisfiesTypeConversion(Type type, ConstantPropagation right) {
return Satisfiability.UNKNOWN;
}
@Override
protected Satisfiability satisfiesUnaryExpression(UnaryOperator operator, ConstantPropagation arg) {
return Satisfiability.UNKNOWN;
}
@Override
protected Satisfiability satisfiesBinaryExpression(BinaryOperator operator, ConstantPropagation left,
ConstantPropagation right) {
if (left.isTop() || right.isTop())
return Satisfiability.UNKNOWN;
switch (operator) {
case COMPARISON_EQ:
return left.value == right.value ? Satisfiability.SATISFIED : Satisfiability.NOT_SATISFIED;
case COMPARISON_GE:
return left.value >= right.value ? Satisfiability.SATISFIED : Satisfiability.NOT_SATISFIED;
case COMPARISON_GT:
return left.value > right.value ? Satisfiability.SATISFIED : Satisfiability.NOT_SATISFIED;
case COMPARISON_LE:
return left.value <= right.value ? Satisfiability.SATISFIED : Satisfiability.NOT_SATISFIED;
case COMPARISON_LT:
return left.value < right.value ? Satisfiability.SATISFIED : Satisfiability.NOT_SATISFIED;
case COMPARISON_NE:
return left.value != right.value ? Satisfiability.SATISFIED : Satisfiability.NOT_SATISFIED;
default:
return Satisfiability.UNKNOWN;
}
}
@Override
protected Satisfiability satisfiesTernaryExpression(TernaryOperator operator, ConstantPropagation left,
ConstantPropagation middle, ConstantPropagation right) {
return Satisfiability.UNKNOWN;
}
}