Modified blocks so that the condition is squared and labeled

This commit is contained in:
Carlos Galindo 2019-04-03 18:00:14 +02:00
parent d7abcb7b55
commit 498966d76e
Signed by untrusted user who does not match committer: kauron
GPG key ID: 83E68706DEE119A3
2 changed files with 30 additions and 16 deletions

View file

@ -24,8 +24,14 @@ public class CFG {
System.out.println("NODO: " + node2str(stmt)); System.out.println("NODO: " + node2str(stmt));
} }
public void beginBlock(Node node) { public void beginBlock(Node node, Node condition) {
Block b = new Block(node, this); beginBlock(node, condition, true);
}
public void beginBlock(Node node, Node condition, boolean addNow) {
Block b = new Block(node, condition, this);
if (condition != null && addNow)
addNode(condition);
if (blockStack.isEmpty()) if (blockStack.isEmpty())
blocks.addFirst(b); blocks.addFirst(b);
else else
@ -98,12 +104,13 @@ public class CFG {
static class Block extends LinkedList<Node> { static class Block extends LinkedList<Node> {
private static int clusterId = 0; private static int clusterId = 0;
private final Node container; private final Node container, condition;
private final CFG cfg; private final CFG cfg;
private final List<Block> inners = new LinkedList<>(); private final List<Block> inners = new LinkedList<>();
Block(Node container, CFG cfg) { Block(Node container, Node condition, CFG cfg) {
this.container = container; this.container = container;
this.condition = condition;
this.cfg = cfg; this.cfg = cfg;
} }
@ -115,6 +122,10 @@ public class CFG {
List<String> res = new LinkedList<>(); List<String> res = new LinkedList<>();
res.add(gv.start_subgraph(clusterId++)); res.add(gv.start_subgraph(clusterId++));
res.add("label = \"" + getTitle() + "\""); res.add("label = \"" + getTitle() + "\"");
if (condition != null) {
res.add("node [ shape = rectangle ]; " + cfg.node2str(condition));
res.add("node [ shape = ellipse ]");
}
for (Block b : inners) { for (Block b : inners) {
res.addAll(b.toStringList(gv)); res.addAll(b.toStringList(gv));
} }

View file

@ -52,14 +52,14 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
@Override @Override
public void visit(IfStmt n, CFG graph) { public void visit(IfStmt n, CFG graph) {
graph.beginBlock(n);
Node ifStart = n.getCondition(); Node ifStart = n.getCondition();
graph.addNode(ifStart); graph.beginBlock(n, ifStart);
graph.connect(prevNode, ifStart); graph.connect(prevNode, ifStart);
// TODO: shortcut conditions (||, &&) // TODO: shortcut conditions (||, &&)
prevNode = Collections.singletonList(ifStart); prevNode = Collections.singletonList(ifStart);
graph.setNextLabel(true);
List<Node> prevNodeBegin = prevNode; List<Node> prevNodeBegin = prevNode;
n.getThenStmt().accept(this, graph); n.getThenStmt().accept(this, graph);
List<Node> newPrev = new LinkedList<>(); List<Node> newPrev = new LinkedList<>();
@ -67,6 +67,7 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
newPrev.add(ifStart); newPrev.add(ifStart);
else newPrev.addAll(prevNode); else newPrev.addAll(prevNode);
prevNode = Collections.singletonList(ifStart); prevNode = Collections.singletonList(ifStart);
graph.setNextLabel(false);
if (n.getElseStmt().isPresent()) { if (n.getElseStmt().isPresent()) {
n.getElseStmt().get().accept(this, graph); n.getElseStmt().get().accept(this, graph);
if (prevNode == prevNodeBegin) if (prevNode == prevNodeBegin)
@ -81,36 +82,38 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
@Override @Override
public void visit(WhileStmt n, CFG graph) { public void visit(WhileStmt n, CFG graph) {
graph.beginBlock(n);
Node whileStart = n.getCondition(); Node whileStart = n.getCondition();
graph.addNode(whileStart); graph.beginBlock(n, whileStart);
graph.connect(prevNode, whileStart); graph.connect(prevNode, whileStart);
// TODO: shortcut conditions (||, &&) // TODO: shortcut conditions (||, &&)
prevNode = Collections.singletonList(whileStart); prevNode = Collections.singletonList(whileStart);
graph.setNextLabel(true);
n.getBody().accept(this, graph); n.getBody().accept(this, graph);
graph.connect(prevNode, whileStart); graph.connect(prevNode, whileStart);
prevNode = Collections.singletonList(whileStart); prevNode = Collections.singletonList(whileStart);
graph.setNextLabel(false);
graph.endBlock(); graph.endBlock();
} }
@Override @Override
public void visit(DoStmt n, CFG graph) { public void visit(DoStmt n, CFG graph) {
graph.beginBlock(n);
Node condition = n.getCondition(); Node condition = n.getCondition();
graph.beginBlock(n, condition, false);
prevNode = new LinkedList<>(prevNode); prevNode = new LinkedList<>(prevNode);
prevNode.add(condition); prevNode.add(condition);
n.getBody().accept(this, graph); n.getBody().accept(this, graph);
graph.addNode(condition); graph.addNode(condition);
graph.connect(prevNode, condition); graph.connect(prevNode, condition);
prevNode = Collections.singletonList(condition); prevNode = Collections.singletonList(condition);
graph.setNextLabel(false);
graph.endBlock(); graph.endBlock();
} }
@Override @Override
public void visit(ForStmt n, CFG graph) { public void visit(ForStmt n, CFG graph) {
graph.beginBlock(n); graph.beginBlock(n, n.getCompare().orElse(null));
// Initialization expressions // Initialization expressions
if (n.getInitialization() != null) { if (n.getInitialization() != null) {
for (Expression e : n.getInitialization()) { for (Expression e : n.getInitialization()) {
@ -125,6 +128,7 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
graph.addNode(n.getCompare().get()); graph.addNode(n.getCompare().get());
graph.connect(prevNode, n.getCompare().get()); graph.connect(prevNode, n.getCompare().get());
prevNode = Collections.singletonList(n.getCompare().get()); prevNode = Collections.singletonList(n.getCompare().get());
graph.setNextLabel(true);
} }
// Loop body // Loop body
n.getBody().accept(this, graph); n.getBody().accept(this, graph);
@ -143,18 +147,18 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
// TODO: connect last update / end of loop with first element of loop body // TODO: connect last update / end of loop with first element of loop body
} }
// Set comparison as last possible statement // Set comparison as last possible statement
if (n.getCompare().isPresent()) if (n.getCompare().isPresent()) {
prevNode = Collections.singletonList(n.getCompare().get()); prevNode = Collections.singletonList(n.getCompare().get());
else // There is no comparison, can't exit the loop TODO implement break and continue as way to exit graph.setNextLabel(false);
} else // There is no comparison, can't exit the loop TODO implement break and continue as way to exit
prevNode = Collections.emptyList(); prevNode = Collections.emptyList();
graph.endBlock(); graph.endBlock();
} }
@Override @Override
public void visit(ForeachStmt n, CFG graph) { public void visit(ForeachStmt n, CFG graph) {
graph.beginBlock(n);
ForeachStmt copy = new ForeachStmt(n.getTokenRange().orElse(null), n.getVariable(), n.getIterable(), new EmptyStmt()); ForeachStmt copy = new ForeachStmt(n.getTokenRange().orElse(null), n.getVariable(), n.getIterable(), new EmptyStmt());
graph.addNode(copy); graph.beginBlock(n, copy);
graph.connect(prevNode, copy); graph.connect(prevNode, copy);
prevNode = Collections.singletonList(copy); prevNode = Collections.singletonList(copy);
n.getBody().accept(this, graph); n.getBody().accept(this, graph);
@ -176,10 +180,9 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
@Override @Override
public void visit(SwitchStmt n, CFG graph) { public void visit(SwitchStmt n, CFG graph) {
graph.beginBlock(n);
// Link previous statement to the switch's selector // Link previous statement to the switch's selector
Node selectorNode = n.getSelector(); Node selectorNode = n.getSelector();
graph.addNode(selectorNode); graph.beginBlock(n, selectorNode);
graph.connect(prevNode, selectorNode); graph.connect(prevNode, selectorNode);
// Analyze switch's cases // Analyze switch's cases
prevNode = Collections.singletonList(selectorNode); prevNode = Collections.singletonList(selectorNode);