Repository navigation
Expand file tree
/
Copy pathVCFolding.java
More file actions
264 lines (228 loc) · 10 KB
/
Copy pathVCFolding.java
File metadata and controls
264 lines (228 loc) · 10 KB
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
package liquidjava.rj_language.opt;
import liquidjava.rj_language.ast.BinaryExpression;
import liquidjava.rj_language.ast.Enum;
import liquidjava.rj_language.ast.Expression;
import liquidjava.rj_language.ast.Ite;
import liquidjava.rj_language.ast.LiteralBoolean;
import liquidjava.rj_language.ast.LiteralInt;
import liquidjava.rj_language.ast.LiteralReal;
import liquidjava.rj_language.ast.UnaryExpression;
/**
* Simplifies VCImplication chains by folding constant expressions and other foldable patterns inside refinements
*/
public class VCFolding extends VCExpressionSimplificationPass<Void> {
@Override
protected Expression simplify(Expression expression, Void context) {
return fold(expression);
}
/**
* Folds the first foldable expression found
*/
private Expression fold(Expression expression) {
if (expression instanceof BinaryExpression binary)
return foldBinary(binary);
if (expression instanceof UnaryExpression unary)
return foldUnary(unary);
if (expression instanceof Ite ite)
return foldIte(ite);
return expression.clone();
}
/**
* Folds a binary expression and its operands
*/
private Expression foldBinary(BinaryExpression binary) {
Expression left = binary.getFirstOperand();
Expression foldedLeft = fold(left);
if (!left.equals(foldedLeft))
return new BinaryExpression(foldedLeft, binary.getOperator(), binary.getSecondOperand().clone());
Expression right = binary.getSecondOperand();
Expression foldedRight = fold(right);
if (!right.equals(foldedRight))
return new BinaryExpression(left.clone(), binary.getOperator(), foldedRight);
String op = binary.getOperator();
Expression foldedBinary = foldLiteralBinary(valueOf(left), valueOf(right), op);
if (foldedBinary != null)
return foldedBinary;
Expression foldedAdjacentInts = foldAdjacentInts(left, right, op);
if (foldedAdjacentInts != null)
return foldedAdjacentInts;
return new BinaryExpression(left, op, right);
}
/**
* Folds a unary expression and its operand
*/
private Expression foldUnary(UnaryExpression unary) {
Expression operand = unary.getExpression();
Expression foldedOperand = fold(operand);
if (!operand.equals(foldedOperand))
return new UnaryExpression(unary.getOp(), foldedOperand);
String op = unary.getOp();
Expression value = valueOf(operand);
// !true -> false
// !false -> true
if ("!".equals(op) && value instanceof LiteralBoolean literal)
return new LiteralBoolean(!literal.isBooleanTrue());
if ("-".equals(op)) {
// -(x) -> -x
if (value instanceof LiteralInt literal)
return new LiteralInt(-literal.getValue());
if (value instanceof LiteralReal literal)
return new LiteralReal(-literal.getValue());
}
return new UnaryExpression(op, operand);
}
/**
* Folds a conditional expression and its branches
*/
private Expression foldIte(Ite ite) {
Expression condition = ite.getCondition();
// true ? x : y -> x
// false ? x : y -> y
if (valueOf(condition)instanceof LiteralBoolean literal)
return literal.isBooleanTrue() ? ite.getThen().clone() : ite.getElse().clone();
Expression foldedCondition = fold(condition);
if (!condition.equals(foldedCondition))
return new Ite(foldedCondition, ite.getThen().clone(), ite.getElse().clone());
Expression thenExpression = ite.getThen();
Expression foldedThen = fold(thenExpression);
if (!thenExpression.equals(foldedThen))
return new Ite(condition.clone(), foldedThen, ite.getElse().clone());
Expression elseExpression = ite.getElse();
Expression foldedElse = fold(elseExpression);
if (!elseExpression.equals(foldedElse))
return new Ite(condition.clone(), thenExpression.clone(), foldedElse);
// y ? x : x -> x
if (thenExpression.equals(elseExpression))
return thenExpression;
return new Ite(condition, thenExpression, elseExpression);
}
/**
* The value of a static final constant, which only replaces its name when the expression around it folds, so a
* constant compared with a variable keeps the name the code or the spec wrote
*/
private Expression valueOf(Expression expression) {
return expression instanceof Enum en && en.getResolvedLiteral() != null ? en.getResolvedLiteral() : expression;
}
/**
* Folds a binary expression whose operands are both literals
*/
private Expression foldLiteralBinary(Expression left, Expression right, String op) {
if (left instanceof LiteralInt leftInt && right instanceof LiteralInt rightInt)
return foldInts(leftInt.getValue(), rightInt.getValue(), op);
if (left instanceof LiteralReal leftReal && right instanceof LiteralReal rightReal)
return foldReals(leftReal.getValue(), rightReal.getValue(), op);
if (isMixedNumeric(left, right)) {
double l = numericValue(left);
double r = numericValue(right);
return foldReals(l, r, op);
}
if (left instanceof LiteralBoolean leftBool && right instanceof LiteralBoolean rightBool)
return foldBooleans(leftBool.isBooleanTrue(), rightBool.isBooleanTrue(), op);
if (left instanceof Enum leftEnum && right instanceof Enum rightEnum
&& leftEnum.getTypeName().equals(rightEnum.getTypeName())) {
boolean equal = leftEnum.getConstName().equals(rightEnum.getConstName());
// Enum.A == Enum.A -> true
// Enum.A != Enum.B -> true
return switch (op) {
case "==" -> new LiteralBoolean(equal);
case "!=" -> new LiteralBoolean(!equal);
default -> null;
};
}
return null;
}
/**
* Combines adjacent integer constants in additions and subtractions
*/
private Expression foldAdjacentInts(Expression left, Expression right, String op) {
if (!"+".equals(op) && !"-".equals(op))
return null;
if (!(right instanceof LiteralInt rightLiteral))
return null;
if (!(left instanceof BinaryExpression leftBinary))
return null;
if (!"+".equals(leftBinary.getOperator()) && !"-".equals(leftBinary.getOperator()))
return null;
if (!(leftBinary.getSecondOperand()instanceof LiteralInt leftLiteral))
return null;
// treat subtraction as adding a negative constant and then add the two
int signedLeft = "+".equals(leftBinary.getOperator()) ? leftLiteral.getValue() : -leftLiteral.getValue();
int signedRight = "+".equals(op) ? rightLiteral.getValue() : -rightLiteral.getValue();
int constant = signedLeft + signedRight;
Expression base = leftBinary.getFirstOperand().clone();
// x + n - n -> x
if (constant == 0)
return base;
// x + n + m -> x + k
if (constant > 0)
return new BinaryExpression(base, "+", new LiteralInt(constant));
// x + n - m -> x - k
return new BinaryExpression(base, "-", new LiteralInt(-constant));
}
/**
* Folds integer operations
*/
private Expression foldInts(int left, int right, String op) {
return switch (op) {
case "+" -> new LiteralInt(left + right);
case "-" -> new LiteralInt(left - right);
case "*" -> new LiteralInt(left * right);
case "/" -> right != 0 ? new LiteralInt(left / right) : null;
case "%" -> right != 0 ? new LiteralInt(left % right) : null;
case "<" -> new LiteralBoolean(left < right);
case "<=" -> new LiteralBoolean(left <= right);
case ">" -> new LiteralBoolean(left > right);
case ">=" -> new LiteralBoolean(left >= right);
case "==" -> new LiteralBoolean(left == right);
case "!=" -> new LiteralBoolean(left != right);
default -> null;
};
}
/**
* Folds real number operations
*/
private Expression foldReals(double left, double right, String op) {
return switch (op) {
case "+" -> new LiteralReal(left + right);
case "-" -> new LiteralReal(left - right);
case "*" -> new LiteralReal(left * right);
case "/" -> right != 0.0 ? new LiteralReal(left / right) : null;
case "%" -> right != 0.0 ? new LiteralReal(left % right) : null;
case "<" -> new LiteralBoolean(left < right);
case "<=" -> new LiteralBoolean(left <= right);
case ">" -> new LiteralBoolean(left > right);
case ">=" -> new LiteralBoolean(left >= right);
case "==" -> new LiteralBoolean(left == right);
case "!=" -> new LiteralBoolean(left != right);
default -> null;
};
}
/**
* Folds boolean operations
*/
private Expression foldBooleans(boolean left, boolean right, String op) {
return switch (op) {
case "&&" -> new LiteralBoolean(left && right);
case "||" -> new LiteralBoolean(left || right);
case "-->" -> new LiteralBoolean(!left || right);
case "==" -> new LiteralBoolean(left == right);
case "!=" -> new LiteralBoolean(left != right);
default -> null;
};
}
/**
* Checks whether two expressions mix integer and real literals
*/
private boolean isMixedNumeric(Expression left, Expression right) {
return left instanceof LiteralInt && right instanceof LiteralReal
|| left instanceof LiteralReal && right instanceof LiteralInt;
}
/**
* Reads a numeric literal as a double
*/
private double numericValue(Expression expression) {
if (expression instanceof LiteralInt literal)
return literal.getValue();
return ((LiteralReal) expression).getValue();
}
}