Skip to content

Commit

Permalink
Merge pull request #211 from UniVE-SSV/offset-recomputation
Browse files Browse the repository at this point in the history
Cutoff recomputation in `NodeList`
  • Loading branch information
lucaneg authored Jul 21, 2022
2 parents 8f04e85 + 664bcfe commit 9bba50e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,17 @@ public void removeNode(N node) {
cutoff.remove(target);
}

nodes.remove(node);
// need to shift all successive cutoff back by one
List<Integer> interesting = cutoff.stream().filter(i -> i >= target).sorted().collect(Collectors.toList());
cutoff.removeAll(interesting);
interesting.forEach(i -> cutoff.add(i - 1));

if (target != 0)
if (target != nodes.size() - 1) {
if (target != nodes.size()) { // we don't remove 1 since we want to
// compare wrt the 'old' position
N pred = nodes.get(target - 1);
N succ = nodes.get(target + 1);
N succ = nodes.get(target); // before was at target+1
NodeEdges<G, N, E> predEdges = extraEdges.get(pred);
if (predEdges != null) {
E seq = sequentialSingleton.newInstance(pred, succ);
Expand All @@ -184,12 +191,6 @@ public void removeNode(N node) {
cutoff.add(target - 1);
}

nodes.remove(node);
// need to shift all successive cutoff back by one
List<Integer> interesting = cutoff.stream().filter(i -> i > target).sorted().collect(Collectors.toList());
cutoff.removeAll(interesting);
interesting.forEach(i -> cutoff.add(i - 1));

recomputeOffsets();
}

Expand Down Expand Up @@ -252,19 +253,20 @@ public void removeEdge(E e) {
if (e.isUnconditional() && src == dest - 1)
// just add the cutoff
cutoff.add(src);
else {
NodeEdges<G, N, E> edges = extraEdges.get(e.getSource());
if (edges != null) {
edges.outgoing.remove(e);
if (edges.ingoing.isEmpty() && edges.outgoing.isEmpty())
extraEdges.remove(e.getSource());
}
edges = extraEdges.get(e.getDestination());
if (edges != null) {
edges.ingoing.remove(e);
if (edges.ingoing.isEmpty() && edges.outgoing.isEmpty())
extraEdges.remove(e.getDestination());
}

// the edge might still be inside the extraEdges
// if this method has been invoked by removeNode
NodeEdges<G, N, E> edges = extraEdges.get(e.getSource());
if (edges != null) {
edges.outgoing.remove(e);
if (edges.ingoing.isEmpty() && edges.outgoing.isEmpty())
extraEdges.remove(e.getSource());
}
edges = extraEdges.get(e.getDestination());
if (edges != null) {
edges.ingoing.remove(e);
if (edges.ingoing.isEmpty() && edges.outgoing.isEmpty())
extraEdges.remove(e.getDestination());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,38 @@ public void testSimplificationAtTheEndWithBranch() throws ProgramValidationExcep
ControlFlowStructure act = first.getControlFlowStructures().iterator().next();
assertEquals("Simplification did not update control flow structures", exp, act);
}

@Test
public void testIssue210() throws ProgramValidationException {
SourceCodeLocation unknownLocation = new SourceCodeLocation("fake", 0, 0);
SourceCodeLocation unknownLocation2 = new SourceCodeLocation("fake", 0, 1);
CompilationUnit unit = new CompilationUnit(unknownLocation, "foo", false);
CFG first = new CFG(new CFGDescriptor(unknownLocation, unit, true, "foo"));
Assignment assign = new Assignment(first, unknownLocation, new VariableRef(first, unknownLocation, "x"),
new Int32Literal(first, unknownLocation, 5));
GreaterThan gt = new GreaterThan(first, unknownLocation, new VariableRef(first, unknownLocation, "x"),
new Int32Literal(first, unknownLocation, 2));
NoOp noop = new NoOp(first, unknownLocation);
Length print1 = new Length(first, unknownLocation, new StringLiteral(first, unknownLocation, "f"));
Return ret1 = new Return(first, unknownLocation, new VariableRef(first, unknownLocation, "x"));

Length print2 = new Length(first, unknownLocation2, new StringLiteral(first, unknownLocation2, "f"));
Return ret2 = new Return(first, unknownLocation2, new VariableRef(first, unknownLocation2, "f"));

first.addNode(assign, true);
first.addNode(gt);
first.addNode(noop);
first.addNode(print1);
first.addNode(ret1);
first.addNode(print2);
first.addNode(ret2);
first.addEdge(new SequentialEdge(assign, gt));
first.addEdge(new TrueEdge(gt, print1));
first.addEdge(new SequentialEdge(print1, ret1));
first.addEdge(new FalseEdge(gt, print2));
first.addEdge(new SequentialEdge(print2, ret2));

first.simplify();
first.validate();
}
}

0 comments on commit 9bba50e

Please sign in to comment.