Skip to content

Commit b3d9e3d

Browse files
committed
#32 #35 simplification tests + refactoring on imp code visitor
1 parent 1639a0f commit b3d9e3d

File tree

4 files changed

+143
-24
lines changed

4 files changed

+143
-24
lines changed

lisa/imp-testcases/syntactic/expressions.imp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ class expressions {
44
def x = "s";
55
}
66

7-
_throw(x, i) {
8-
throw x;
7+
_throw1(x, i) {
98
throw i;
109
}
1110

12-
_return(x, i) {
11+
_throw2(x, i) {
12+
throw x;
13+
}
14+
15+
_return1(x, i) {
1316
return i;
17+
}
18+
19+
_return2(x, i) {
1420
return x;
1521
}
1622

@@ -19,11 +25,17 @@ class expressions {
1925
this.test(i, 5);
2026
}
2127

22-
nestedCall(i, x) {
28+
nestedCall1(i, x) {
2329
this.test(5, this.test(i, 5));
2430
this.test(x, this.test(i));
31+
x = this.test(i);
32+
}
33+
34+
nestedCall2(i, x) {
2535
return this.test(i);
36+
}
37+
38+
nestedCall3(i, x) {
2639
throw this.test(i);
27-
x = this.test(i);
2840
}
2941
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
22
"warnings" : [ {
3-
"message" : "['imp-testcases/syntactic/expressions.imp':13:9] on 'untyped expressions::_return(untyped x, untyped i)': [EXPRESSION] Found variable i"
3+
"message" : "['imp-testcases/syntactic/expressions.imp':16:9] on 'untyped expressions::_return1(untyped x, untyped i)': [EXPRESSION] Found variable i"
44
}, {
5-
"message" : "['imp-testcases/syntactic/expressions.imp':19:12] on 'untyped expressions::call(untyped i)': [EXPRESSION] Found variable i"
5+
"message" : "['imp-testcases/syntactic/expressions.imp':25:12] on 'untyped expressions::call(untyped i)': [EXPRESSION] Found variable i"
66
}, {
7-
"message" : "['imp-testcases/syntactic/expressions.imp':23:25] on 'untyped expressions::nestedCall(untyped i, untyped x)': [EXPRESSION] Found variable i"
7+
"message" : "['imp-testcases/syntactic/expressions.imp':29:25] on 'untyped expressions::nestedCall1(untyped i, untyped x)': [EXPRESSION] Found variable i"
88
}, {
9-
"message" : "['imp-testcases/syntactic/expressions.imp':24:25] on 'untyped expressions::nestedCall(untyped i, untyped x)': [EXPRESSION] Found variable i"
9+
"message" : "['imp-testcases/syntactic/expressions.imp':30:25] on 'untyped expressions::nestedCall1(untyped i, untyped x)': [EXPRESSION] Found variable i"
1010
}, {
11-
"message" : "['imp-testcases/syntactic/expressions.imp':25:19] on 'untyped expressions::nestedCall(untyped i, untyped x)': [EXPRESSION] Found variable i"
11+
"message" : "['imp-testcases/syntactic/expressions.imp':31:16] on 'untyped expressions::nestedCall1(untyped i, untyped x)': [EXPRESSION] Found variable i"
1212
}, {
13-
"message" : "['imp-testcases/syntactic/expressions.imp':26:18] on 'untyped expressions::nestedCall(untyped i, untyped x)': [EXPRESSION] Found variable i"
13+
"message" : "['imp-testcases/syntactic/expressions.imp':35:19] on 'untyped expressions::nestedCall2(untyped i, untyped x)': [EXPRESSION] Found variable i"
1414
}, {
15-
"message" : "['imp-testcases/syntactic/expressions.imp':27:16] on 'untyped expressions::nestedCall(untyped i, untyped x)': [EXPRESSION] Found variable i"
15+
"message" : "['imp-testcases/syntactic/expressions.imp':39:18] on 'untyped expressions::nestedCall3(untyped i, untyped x)': [EXPRESSION] Found variable i"
1616
}, {
1717
"message" : "['imp-testcases/syntactic/expressions.imp':3:6] on 'untyped expressions::assignment()': [EXPRESSION] Found variable i"
1818
}, {
19-
"message" : "['imp-testcases/syntactic/expressions.imp':9:8] on 'untyped expressions::_throw(untyped x, untyped i)': [EXPRESSION] Found variable i"
19+
"message" : "['imp-testcases/syntactic/expressions.imp':8:8] on 'untyped expressions::_throw1(untyped x, untyped i)': [EXPRESSION] Found variable i"
2020
} ],
2121
"files" : [ ]
2222
}

lisa/src/test/java/it/unive/lisa/test/cfg/CFGSimplificationTest.java

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import static org.junit.Assert.assertTrue;
44

5+
import org.junit.Test;
6+
57
import it.unive.lisa.analysis.AbstractState;
68
import it.unive.lisa.analysis.AnalysisState;
79
import it.unive.lisa.analysis.HeapDomain;
810
import it.unive.lisa.analysis.SemanticException;
911
import it.unive.lisa.analysis.ValueDomain;
1012
import it.unive.lisa.callgraph.CallGraph;
1113
import it.unive.lisa.program.CompilationUnit;
14+
import it.unive.lisa.program.ProgramValidationException;
1215
import it.unive.lisa.program.cfg.CFG;
1316
import it.unive.lisa.program.cfg.CFGDescriptor;
1417
import it.unive.lisa.program.cfg.edge.FalseEdge;
@@ -24,12 +27,11 @@
2427
import it.unive.lisa.program.cfg.statement.VariableRef;
2528
import it.unive.lisa.symbolic.SymbolicExpression;
2629
import it.unive.lisa.type.Untyped;
27-
import org.junit.Test;
2830

2931
public class CFGSimplificationTest {
3032

3133
@Test
32-
public void testSimpleSimplification() {
34+
public void testSimpleSimplification() throws ProgramValidationException {
3335
CompilationUnit unit = new CompilationUnit(null, -1, -1, "foo", false);
3436
CFG first = new CFG(new CFGDescriptor(unit, true, "foo"));
3537
Assignment assign = new Assignment(first, new VariableRef(first, "x"), new Literal(first, 5, Untyped.INSTANCE));
@@ -50,12 +52,15 @@ public void testSimpleSimplification() {
5052

5153
second.addEdge(new SequentialEdge(assign, ret));
5254

55+
first.validate();
56+
second.validate();
5357
first.simplify();
58+
first.validate();
5459
assertTrue("Different CFGs", second.isEqualTo(first));
5560
}
5661

5762
@Test
58-
public void testDoubleSimplification() {
63+
public void testDoubleSimplification() throws ProgramValidationException {
5964
CompilationUnit unit = new CompilationUnit(null, -1, -1, "foo", false);
6065
CFG first = new CFG(new CFGDescriptor(unit, true, "foo"));
6166
Assignment assign = new Assignment(first, new VariableRef(first, "x"), new Literal(first, 5, Untyped.INSTANCE));
@@ -79,12 +84,15 @@ public void testDoubleSimplification() {
7984

8085
second.addEdge(new SequentialEdge(assign, ret));
8186

87+
first.validate();
88+
second.validate();
8289
first.simplify();
90+
first.validate();
8391
assertTrue("Different CFGs", second.isEqualTo(first));
8492
}
8593

8694
@Test
87-
public void testConditionalSimplification() {
95+
public void testConditionalSimplification() throws ProgramValidationException {
8896
class GT extends BinaryNativeCall {
8997
protected GT(CFG cfg, Expression left, Expression right) {
9098
super(cfg, "gt", left, right);
@@ -155,33 +163,132 @@ V extends ValueDomain<V>> AnalysisState<A, H, V> unarySemantics(
155163
second.addEdge(new FalseEdge(gt, ret));
156164
second.addEdge(new SequentialEdge(print, ret));
157165

166+
first.validate();
167+
second.validate();
158168
first.simplify();
169+
first.validate();
159170
assertTrue("Different CFGs", second.isEqualTo(first));
160171
}
161172

162173
@Test
163-
public void testSimplificationWithDuplicateStatements() {
174+
public void testSimplificationWithDuplicateStatements() throws ProgramValidationException {
164175
CompilationUnit unit = new CompilationUnit(null, -1, -1, "foo", false);
165176
CFG first = new CFG(new CFGDescriptor(unit, true, "foo"));
166177
Assignment assign = new Assignment(first, new VariableRef(first, "x"), new Literal(first, 5, Untyped.INSTANCE));
167178
NoOp noop = new NoOp(first);
168-
Assignment ret = new Assignment(first, new VariableRef(first, "x"), new Literal(first, 5, Untyped.INSTANCE));
169-
first.addNode(assign);
179+
Return ret = new Return(first, new VariableRef(first, "x"));
180+
first.addNode(assign, true);
170181
first.addNode(noop);
171182
first.addNode(ret);
172183
first.addEdge(new SequentialEdge(assign, noop));
173184
first.addEdge(new SequentialEdge(noop, ret));
174185

175186
CFG second = new CFG(new CFGDescriptor(unit, true, "foo"));
176187
assign = new Assignment(second, new VariableRef(second, "x"), new Literal(second, 5, Untyped.INSTANCE));
177-
ret = new Assignment(first, new VariableRef(first, "x"), new Literal(first, 5, Untyped.INSTANCE));
188+
ret = new Return(second, new VariableRef(first, "x"));
178189

179-
second.addNode(assign);
190+
second.addNode(assign, true);
180191
second.addNode(ret);
181192

182193
second.addEdge(new SequentialEdge(assign, ret));
183194

195+
first.validate();
196+
second.validate();
197+
first.simplify();
198+
first.validate();
199+
assertTrue("Different CFGs", second.isEqualTo(first));
200+
}
201+
202+
@Test
203+
public void testSimplificationAtTheStart() throws ProgramValidationException {
204+
CompilationUnit unit = new CompilationUnit(null, -1, -1, "foo", false);
205+
CFG first = new CFG(new CFGDescriptor(unit, false, "foo"));
206+
NoOp start = new NoOp(first);
207+
Assignment assign = new Assignment(first, new VariableRef(first, "x"), new Literal(first, 5, Untyped.INSTANCE));
208+
Return ret = new Return(first, new VariableRef(first, "x"));
209+
first.addNode(start, true);
210+
first.addNode(assign);
211+
first.addNode(ret);
212+
first.addEdge(new SequentialEdge(assign, ret));
213+
first.addEdge(new SequentialEdge(start, assign));
214+
215+
CFG second = new CFG(new CFGDescriptor(unit, false, "foo"));
216+
assign = new Assignment(second, new VariableRef(second, "x"), new Literal(second, 5, Untyped.INSTANCE));
217+
ret = new Return(second, new VariableRef(first, "x"));
218+
219+
second.addNode(assign, true);
220+
second.addNode(ret);
221+
222+
second.addEdge(new SequentialEdge(assign, ret));
223+
224+
first.validate();
225+
second.validate();
226+
first.simplify();
227+
first.validate();
228+
assertTrue("Different CFGs", second.isEqualTo(first));
229+
}
230+
231+
@Test
232+
public void testSimplificationAtTheEnd() throws ProgramValidationException {
233+
CompilationUnit unit = new CompilationUnit(null, -1, -1, "foo", false);
234+
CFG first = new CFG(new CFGDescriptor(unit, false, "foo"));
235+
Assignment assign1 = new Assignment(first, new VariableRef(first, "x"), new Literal(first, 5, Untyped.INSTANCE));
236+
Assignment assign2 = new Assignment(first, new VariableRef(first, "y"), new Literal(first, 50, Untyped.INSTANCE));
237+
NoOp end = new NoOp(first);
238+
first.addNode(assign1, true);
239+
first.addNode(assign2);
240+
first.addNode(end);
241+
first.addEdge(new SequentialEdge(assign1, assign2));
242+
first.addEdge(new SequentialEdge(assign2, end));
243+
244+
CFG second = new CFG(new CFGDescriptor(unit, false, "foo"));
245+
assign1 = new Assignment(second, new VariableRef(first, "x"), new Literal(first, 5, Untyped.INSTANCE));
246+
assign2 = new Assignment(second, new VariableRef(first, "y"), new Literal(first, 50, Untyped.INSTANCE));
247+
248+
second.addNode(assign1, true);
249+
second.addNode(assign2);
250+
251+
second.addEdge(new SequentialEdge(assign1, assign2));
252+
253+
first.validate();
254+
second.validate();
255+
first.simplify();
256+
first.validate();
257+
assertTrue("Different CFGs", second.isEqualTo(first));
258+
}
259+
260+
@Test
261+
public void testSimplificationAtTheEndWithBranch() throws ProgramValidationException {
262+
CompilationUnit unit = new CompilationUnit(null, -1, -1, "foo", false);
263+
CFG first = new CFG(new CFGDescriptor(unit, false, "foo"));
264+
Assignment assign1 = new Assignment(first, new VariableRef(first, "b"),
265+
new Literal(first, true, Untyped.INSTANCE));
266+
Assignment assign2 = new Assignment(first, new VariableRef(first, "x"), new Literal(first, 5, Untyped.INSTANCE));
267+
Assignment assign3 = new Assignment(first, new VariableRef(first, "y"), new Literal(first, 50, Untyped.INSTANCE));
268+
NoOp end = new NoOp(first);
269+
first.addNode(end);
270+
first.addNode(assign1, true);
271+
first.addNode(assign2);
272+
first.addNode(assign3);
273+
first.addEdge(new TrueEdge(assign1, assign2));
274+
first.addEdge(new FalseEdge(assign1, assign3));
275+
first.addEdge(new SequentialEdge(assign2, end));
276+
first.addEdge(new SequentialEdge(assign3, end));
277+
278+
CFG second = new CFG(new CFGDescriptor(unit, false, "foo"));
279+
assign1 = new Assignment(second, new VariableRef(first, "b"), new Literal(second, true, Untyped.INSTANCE));
280+
assign2 = new Assignment(second, new VariableRef(first, "x"), new Literal(second, 5, Untyped.INSTANCE));
281+
assign3 = new Assignment(second, new VariableRef(first, "y"), new Literal(second, 50, Untyped.INSTANCE));
282+
second.addNode(assign1, true);
283+
second.addNode(assign2);
284+
second.addNode(assign3);
285+
second.addEdge(new TrueEdge(assign1, assign2));
286+
second.addEdge(new FalseEdge(assign1, assign3));
287+
288+
first.validate();
289+
second.validate();
184290
first.simplify();
291+
first.validate();
185292
assertTrue("Different CFGs", second.isEqualTo(first));
186293
}
187294
}

lisa/src/test/java/it/unive/lisa/test/imp/IMPCodeMemberVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ CFG visitCodeMember(BlockContext ctx) {
143143
Pair<Statement, Statement> visited = visitBlock(ctx);
144144
entrypoints.add(visited.getLeft());
145145

146-
if (cfg.getNormalExitpoints().isEmpty()) {
146+
if (cfg.getAllExitpoints().isEmpty()) {
147147
Ret ret = new Ret(cfg, file, descriptor.getLine(), descriptor.getCol());
148148
if (cfg.getNodesCount() == 0) {
149149
// empty method, so the ret is also the entrypoint
@@ -154,7 +154,7 @@ CFG visitCodeMember(BlockContext ctx) {
154154
// is ending the method
155155
Collection<Statement> preExits = new LinkedList<>();
156156
for (Statement st : matrix.getNodes())
157-
if (!(st instanceof Throw) && matrix.followersOf(st).isEmpty())
157+
if (!st.stopsExecution() && matrix.followersOf(st).isEmpty())
158158
preExits.add(st);
159159
matrix.addNode(ret);
160160
for (Statement st : preExits)

0 commit comments

Comments
 (0)