@Override public String toString() { SequentialNode curr = startNode; StringBuilder sb = new StringBuilder(); sb.append("CFG:\n"); sb.append(startNode); do { sb.append("\n--> "); if (curr.getSuccessor() instanceof SequentialNode) { curr = (SequentialNode) curr.getSuccessor(); sb.append(curr); } else { Node end = curr.getSuccessor(); sb.append(end); break; } } while (true); return sb.toString(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("if(" + condition + ")"); sb.append(",then={"); SequentialNode current = positive; while (!(current instanceof BlockEndNode)) { sb.append(current); sb.append(';'); current = (SequentialNode) current.getSuccessor(); } sb.append("}"); current = negative; if (!(current instanceof BlockEndNode)) { sb.append(",else={"); while (!(current instanceof BlockEndNode)) { sb.append(current); sb.append(';'); current = (SequentialNode) current.getSuccessor(); } sb.append("}"); } return sb.toString(); }
private void copySubtree(SequentialNode root, SequentialNode newBase) { if (root instanceof BlockEndNode) { ((BlockEndNode) root).updateBase(newBase); return; } else if (root.getSuccessor() instanceof SequentialNode) { SequentialNode successor = (SequentialNode) root.getSuccessor(); SequentialNode newSuccessor = successor.copy(); root.replaceSuccessor(successor, newSuccessor); copySubtree(newSuccessor, newBase); } }
private void copySubtree(SequentialNode root, SequentialNode newBase) { if (root instanceof BlockEndNode) { ((BlockEndNode) root).updateBase(newBase); return; } else if (root.getSuccessor() instanceof SequentialNode) { SequentialNode successor = (SequentialNode) root.getSuccessor(); SequentialNode newSuccessor = successor.copy(); root.replaceSuccessor(successor, newSuccessor); copySubtree(newSuccessor, newBase); } }
private void replaceSubtree(Node root, Expression old, Expression newExpression) { if (root instanceof BlockEndNode || root instanceof EndNode) { return; } else if (root instanceof SequentialNode) { root.replaceExpression(old, newExpression); replaceSubtree(((SequentialNode) root).getSuccessor(), old, newExpression); } }
private void replaceSubtree(Node root, Expression old, Expression newExpression) { if (root instanceof BlockEndNode || root instanceof EndNode) { return; } else if (root instanceof SequentialNode) { root.replaceExpression(old, newExpression); replaceSubtree(((SequentialNode) root).getSuccessor(), old, newExpression); } }
@Override public void visit(BlockEndNode node) { SequentialNode base = node.getBase(); if (base instanceof LoopNode) { for (Node p : node.getPredecessors()) { if (idMap.containsKey(p)) { addForwardEdge(p, base); } } } else { addForwardEdge(base, base.getSuccessor()); } }
/** * Removes the nodes given by <code>list</code> from the control flow graph and rewires them to be a sequence of * separate nodes, e.g. in the body of an if-statement. The first node has <code>base</code> as predecessor. For the * last node in the list, base's successor will be set as successor, e.g. the first statement after an if-then-else * statement. * * @param list list of nodes from a block * @param base basis of block, e.g. an if-statement */ private void rewireNodes(List<SequentialNode> list, SequentialNode base) { if (list == null || list.size() == 0) { return; } Iterator<SequentialNode> it = list.iterator(); SequentialNode current = it.next(); graph.removeNode(current); current.addPredecessor(base); // first node has if-then-else node as predecessor while (it.hasNext()) { SequentialNode next = it.next(); graph.removeNode(next); current.replaceSuccessor(current.getSuccessor(), next); // could be redundant next.addPredecessor(current); current = next; } current.replaceSuccessor(current.getSuccessor(), new BlockEndNode(graph, base)); // mark the end of this block }
/** * Removes the nodes given by <code>list</code> from the control flow graph and rewires them to be a sequence of * separate nodes, e.g. in the body of an if-statement. The first node has <code>base</code> as predecessor. For the * last node in the list, base's successor will be set as successor, e.g. the first statement after an if-then-else * statement. * * @param list list of nodes from a block * @param base basis of block, e.g. an if-statement */ private void rewireNodes(List<SequentialNode> list, SequentialNode base) { if (list == null || list.size() == 0) { return; } Iterator<SequentialNode> it = list.iterator(); SequentialNode current = it.next(); graph.removeNode(current); current.addPredecessor(base); // first node has if-then-else node as predecessor while (it.hasNext()) { SequentialNode next = it.next(); graph.removeNode(next); current.replaceSuccessor(current.getSuccessor(), next); // could be redundant next.addPredecessor(current); current = next; } current.replaceSuccessor(current.getSuccessor(), new BlockEndNode(graph, base)); // mark the end of this block }
/** * Removes the nodes given by <code>list</code> from the control flow graph and rewires them to be a sequence of separate * nodes, e.g. in the body of an if-statement. The first node has <code>base</code> as predecessor. For the last node in the * list, base's successor will be set as successor, e.g. the first statement after an if-then-else statement. * * @param list list of nodes from a block * @param base basis of block, e.g. an if-statement */ private void rewireNodes(List<SequentialNode> list, IfThenElseNode base) { if (list == null || list.size() == 0) { return; } Iterator<SequentialNode> it = list.iterator(); SequentialNode current = it.next(); graph.removeNode(current); current.addPredecessor(base); // first node has if-then-else node as predecessor while (it.hasNext()) { SequentialNode next = it.next(); graph.removeNode(next); current.replaceSuccessor(current.getSuccessor(), next); // could be redundant next.addPredecessor(current); current = next; } current.replaceSuccessor(current.getSuccessor(), new BlockEndNode(graph, current)); // mark the end of this block }
/** * Removes the given node from the control flow graph. The graph will be traversed in order to find the node to be * removed. Once the desired node is found, its predecessors are rewired to have the old node's successor as direct * successors. * * @param node node to be removed */ public void removeNode(SequentialNode node) { Node successor = node.getSuccessor(); successor.removePredecessor(node); Set<Node> predecessors = new HashSet<Node>(node.getPredecessors()); for (Node predecessor : predecessors) { successor.addPredecessor(predecessor); predecessor.replaceSuccessor(node, successor); } }