Skip to content

Commit 0284a85

Browse files
author
1475505
committed
Fix Evaluate bug and solved 45/52 tests
1 parent 44f5061 commit 0284a85

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

A2/tai-e/src/main/java/pascal/taie/analysis/dataflow/analysis/constprop/ConstantPropagation.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public Value meetValue(Value v1, Value v2) {
100100
if (v1.isUndef()) return v2;
101101
if (v2.isUndef()) return v1;
102102
if (v1.getConstant() == v2.getConstant()){
103-
return Value.makeConstant(v1.getConstant());
103+
return v1;
104104
} else {
105105
return Value.getNAC();
106106
}
@@ -119,7 +119,7 @@ public boolean transferNode(Stmt stmt, CPFact in, CPFact out) {
119119
} else {
120120
out.update((Var) gen.get(), Value.getNAC());
121121
}
122-
System.out.println("transferred (old_out:" + old_out.toString() + ")"+ stmt.toString() + " from" + in.toString() + "->" + out.toString());
122+
System.out.println(stmt.getLineNumber() + " | transferred (old_out:" + old_out.toString() + ")"+ stmt.toString() + " from" + in.toString() + "->" + out.toString());
123123
}
124124
return out.equals(old_out);
125125
}
@@ -163,9 +163,12 @@ public static Value evaluate(Exp exp, CPFact in) {
163163
} else if (exp instanceof BinaryExp){
164164
Value op1 = evaluate(((BinaryExp) exp).getOperand1(), in);
165165
Value op2 = evaluate(((BinaryExp) exp).getOperand2(), in);
166-
if ( !op1.isConstant() || !op2.isConstant()){
166+
if ( op1.isNAC() || op2.isNAC()){
167167
return Value.getNAC();
168168
}
169+
if ( !op1.isConstant() || !op2.isConstant()){
170+
return Value.getUndef();
171+
}
169172
BinaryExp.Op op = ((BinaryExp) exp).getOperator();
170173
if (op instanceof ArithmeticExp.Op) {
171174
switch ((ArithmeticExp.Op)op) {
@@ -228,7 +231,7 @@ public static Value evaluate(Exp exp, CPFact in) {
228231
}
229232
}
230233
if (op instanceof BitwiseExp.Op) {
231-
// maybe no need to process it? NO!!!
234+
// maybe no need to process it? NO!!! logic rather than bitwise.
232235
switch ((BitwiseExp.Op)op){
233236
case OR -> {
234237
return Value.makeConstant(op1.getConstant() | op2.getConstant());
@@ -241,7 +244,15 @@ public static Value evaluate(Exp exp, CPFact in) {
241244
}
242245
}
243246
}
247+
return Value.getUndef();
244248
}
249+
/*
250+
f(y,z) =
251+
val(y) op val(z) // if val(y) and val(z) are constants
252+
NAC // if val(y) or val(z) is NAC
253+
UNDEF // otherwise
254+
对于其它情况,该方法会像我们在第 2.1 节提到的那样返回 NAC。
255+
*/
245256
return Value.getNAC();
246257
}
247258
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Tai-e: A Static Analysis Framework for Java
3+
*
4+
* Copyright (C) 2022 Tian Tan <[email protected]>
5+
* Copyright (C) 2022 Yue Li <[email protected]>
6+
*
7+
* This file is part of Tai-e.
8+
*
9+
* Tai-e is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License
11+
* as published by the Free Software Foundation, either version 3
12+
* of the License, or (at your option) any later version.
13+
*
14+
* Tai-e is distributed in the hope that it will be useful,but WITHOUT
15+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
17+
* Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with Tai-e. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
package pascal.taie.analysis.dataflow.analysis.constprop;
24+
25+
import org.junit.Test;
26+
import pascal.taie.analysis.Tests;
27+
28+
public class MyCPTest {
29+
30+
void testCP(String inputClass) {
31+
Tests.test(inputClass, "src/test/resources/dataflow/constprop/my/",
32+
ConstantPropagation.ID, "edge-refine:false");
33+
}
34+
35+
@Test
36+
public void BinaryOp() {
37+
testCP("BinaryOp");
38+
}
39+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
------------------- <BinaryOp: void undefined()> (constprop) --------------------
2+
Expected: [1@L31] y = x + %intconst0; {%intconst0=1}
3+
Expected: [2@L31] return; {%intconst0=1}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class BinaryOp {
2+
3+
// void constant1() {
4+
// int i0 = 0, i1 = 1, i2 = 2;
5+
// int x = 1 + 2;
6+
// int y = i0 + 3;
7+
// int z = i1 + i2;
8+
// }
9+
//
10+
// void constant2() {
11+
// int i1 = 1;
12+
// int x = i1 + 10;
13+
// int y = x * 5;
14+
// int z = y - 12;
15+
// }
16+
//
17+
// void constant3() {
18+
// int i5 = 5;
19+
// int x = i5 * 77;
20+
// int y = x / 6;
21+
// int z = y % 5;
22+
// }
23+
//
24+
// void NAC(int p) {
25+
// int x = p;
26+
// int y = 1 + x;
27+
// }
28+
29+
void undefined() {
30+
int x;
31+
int y = x + 1;
32+
}
33+
34+
// void longExpressions() {
35+
// int x = 1, y = 2, z = 3;
36+
// int a = x + y * z;
37+
// int b = (x - y) * z;
38+
// int c = (x - y) / z;
39+
// int d = (x + y) % z;
40+
// }
41+
}

0 commit comments

Comments
 (0)