Skip to content

Commit ad3e1a3

Browse files
author
1475505
committed
pass A1 tests with naive implement.
1 parent 4aa8caf commit ad3e1a3

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

A1/tai-e/src/main/java/pascal/taie/analysis/dataflow/analysis/LiveVariableAnalysis.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import pascal.taie.analysis.dataflow.fact.SetFact;
2626
import pascal.taie.analysis.graph.cfg.CFG;
2727
import pascal.taie.config.AnalysisConfig;
28+
import pascal.taie.ir.exp.LValue;
29+
import pascal.taie.ir.exp.RValue;
2830
import pascal.taie.ir.exp.Var;
2931
import pascal.taie.ir.stmt.Stmt;
3032

@@ -48,23 +50,44 @@ public boolean isForward() {
4850
@Override
4951
public SetFact<Var> newBoundaryFact(CFG<Stmt> cfg) {
5052
// TODO - finish me
51-
return null;
53+
return new SetFact<Var>();
5254
}
5355

5456
@Override
5557
public SetFact<Var> newInitialFact() {
5658
// TODO - finish me
57-
return null;
59+
return new SetFact<Var>();
5860
}
5961

6062
@Override
6163
public void meetInto(SetFact<Var> fact, SetFact<Var> target) {
6264
// TODO - finish me
65+
target.union(fact);
6366
}
6467

6568
@Override
6669
public boolean transferNode(Stmt stmt, SetFact<Var> in, SetFact<Var> out) {
6770
// TODO - finish me
68-
return false;
71+
var old_in = new SetFact<Var>();
72+
old_in.set(in);
73+
var use = stmt.getUses();
74+
var def = stmt.getDef();
75+
for (RValue val : use){
76+
if (val instanceof Var){
77+
in.add((Var)val);
78+
}
79+
}
80+
if (def.isPresent()) {
81+
var val = def.get();
82+
if (val instanceof Var) {
83+
SetFact<Var> tmp = new SetFact<>();
84+
tmp.set(out);
85+
tmp.remove((Var) val);
86+
in.union(tmp);
87+
return !in.equals(old_in);
88+
}
89+
}
90+
in.union(out);
91+
return !in.equals(old_in);
6992
}
7093
}

A1/tai-e/src/main/java/pascal/taie/analysis/dataflow/solver/IterativeSolver.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,22 @@ protected void doSolveForward(CFG<Node> cfg, DataflowResult<Node, Fact> result)
4040
@Override
4141
protected void doSolveBackward(CFG<Node> cfg, DataflowResult<Node, Fact> result) {
4242
// TODO - finish me
43+
// int rounds = 0;
44+
boolean changed = true;
45+
while (changed) {
46+
// System.out.println("Round: " + rounds); rounds++;
47+
changed = false;
48+
for (Node node : cfg){
49+
if (node == cfg.getExit()){
50+
continue;
51+
}
52+
for (Node n : cfg.getSuccsOf(node)) {
53+
analysis.meetInto(result.getInFact(n), result.getOutFact(node));
54+
}
55+
if (analysis.transferNode(node, result.getInFact(node), result.getOutFact(node))){
56+
changed = true;
57+
}
58+
}
59+
}
4360
}
4461
}

A1/tai-e/src/main/java/pascal/taie/analysis/dataflow/solver/Solver.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ protected void initializeForward(CFG<Node> cfg, DataflowResult<Node, Fact> resul
8282

8383
protected void initializeBackward(CFG<Node> cfg, DataflowResult<Node, Fact> result) {
8484
// TODO - finish me
85+
result.setInFact(cfg.getExit(), analysis.newBoundaryFact(cfg));
86+
for (Node node : cfg){
87+
result.setInFact(node, analysis.newInitialFact());
88+
// 为了实现 meet 策略,需要在初始化阶段给每条语句的 OUT[S] 赋上和 IN[S] 一样的初值。
89+
result.setOutFact(node, analysis.newInitialFact());
90+
}
8591
}
8692

8793
/**

Test/src/remove_set.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package src;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* @author ll ( created: 2022-09-17 14:07 )
8+
*/
9+
public class remove_set {
10+
public static void main(String[] args){
11+
var a = new HashSet<Integer>();
12+
a.add(1);
13+
a.add(2);
14+
var b = a.remove(1);
15+
System.out.println(a);
16+
}
17+
}

0 commit comments

Comments
 (0)