Skip to content

Commit

Permalink
refactor CFG to separate instructions from CFGs; this is to allow cod…
Browse files Browse the repository at this point in the history
…e like CDGs to be reused without an IR.
  • Loading branch information
juliandolby committed Sep 26, 2016
1 parent 9b58450 commit f3b5d41
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 52 deletions.
Expand Up @@ -108,7 +108,7 @@ public static Process run(String appJar, String methodSig) throws IOException {
}

System.err.println(ir.toString());
ControlDependenceGraph<SSAInstruction, ISSABasicBlock> cdg = new ControlDependenceGraph<SSAInstruction, ISSABasicBlock>(ir.getControlFlowGraph());
ControlDependenceGraph<ISSABasicBlock> cdg = new ControlDependenceGraph<ISSABasicBlock>(ir.getControlFlowGraph());

Properties wp = null;
try {
Expand Down
41 changes: 1 addition & 40 deletions com.ibm.wala.core/src/com/ibm/wala/cfg/ControlFlowGraph.java
Expand Up @@ -20,17 +20,7 @@
/**
* An interface that is common to the Shrike and SSA CFG implementations.
*/
public interface ControlFlowGraph<I, T extends IBasicBlock<I>> extends NumberedGraph<T> {

/**
* Return the entry basic block in the CFG
*/
public T entry();

/**
* @return the synthetic exit block for the cfg
*/
public T exit();
public interface ControlFlowGraph<I, T extends IBasicBlock<I>> extends NumberedGraph<T>, MinimalCFG<T> {

/**
* @return the indices of the catch blocks, as a bit vector
Expand Down Expand Up @@ -60,33 +50,4 @@ public interface ControlFlowGraph<I, T extends IBasicBlock<I>> extends NumberedG
* @return the Method this CFG represents
*/
public IMethod getMethod();

/**
* The order of blocks returned must indicate the exception-handling scope. So the first block is the first candidate catch block,
* and so on. With this invariant one can compute the exceptional control flow for a given exception type.
*
* @return the basic blocks which may be reached from b via exceptional control flow
*/
public List<T> getExceptionalSuccessors(T b);

/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks which may be reached from b via normal control flow
*/
public Collection<T> getNormalSuccessors(T b);

/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks from which b may be reached via exceptional control flow
*/
public Collection<T> getExceptionalPredecessors(T b);

/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks from which b may be reached via normal control flow
*/
public Collection<T> getNormalPredecessors(T b);
}
59 changes: 59 additions & 0 deletions com.ibm.wala.core/src/com/ibm/wala/cfg/MinimalCFG.java
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2007 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.cfg;

import java.util.Collection;
import java.util.List;

import com.ibm.wala.util.graph.NumberedGraph;

public interface MinimalCFG<T> extends NumberedGraph<T> {

/**
* Return the entry basic block in the CFG
*/
public T entry();

/**
* @return the synthetic exit block for the cfg
*/
public T exit();

/**
* The order of blocks returned must indicate the exception-handling scope. So the first block is the first candidate catch block,
* and so on. With this invariant one can compute the exceptional control flow for a given exception type.
*
* @return the basic blocks which may be reached from b via exceptional control flow
*/
public List<T> getExceptionalSuccessors(T b);

/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks which may be reached from b via normal control flow
*/
public Collection<T> getNormalSuccessors(T b);

/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks from which b may be reached via exceptional control flow
*/
public Collection<T> getExceptionalPredecessors(T b);

/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks from which b may be reached via normal control flow
*/
public Collection<T> getNormalPredecessors(T b);

}
Expand Up @@ -15,8 +15,7 @@
import java.util.Map;
import java.util.Set;

import com.ibm.wala.cfg.ControlFlowGraph;
import com.ibm.wala.cfg.IBasicBlock;
import com.ibm.wala.cfg.MinimalCFG;
import com.ibm.wala.util.collections.EmptyIterator;
import com.ibm.wala.util.collections.HashMapFactory;
import com.ibm.wala.util.collections.HashSetFactory;
Expand All @@ -33,12 +32,12 @@
/**
* Control Dependence Graph
*/
public class ControlDependenceGraph<I, T extends IBasicBlock<I>> extends AbstractNumberedGraph<T> {
public class ControlDependenceGraph<T> extends AbstractNumberedGraph<T> {

/**
* Governing control flow-graph. The control dependence graph is computed from this cfg.
*/
private final ControlFlowGraph<I, T> cfg;
private final MinimalCFG<T> cfg;

/**
* the EdgeManager for the CDG. It implements the edge part of the standard Graph abstraction, using the control-dependence edges
Expand Down Expand Up @@ -127,7 +126,7 @@ public IntSet getPredNodeNumbers(T node) {
MutableIntSet x = IntSetUtil.make();
if (backwardEdges.containsKey(node)) {
for(T pred : backwardEdges.get(node)) {
x.add(pred.getNumber());
x.add(cfg.getNumber(pred));
}
}
return x;
Expand All @@ -154,7 +153,7 @@ public IntSet getSuccNodeNumbers(T node) {
MutableIntSet x = IntSetUtil.make();
if (forwardEdges.containsKey(node)) {
for(T succ : forwardEdges.get(node)) {
x.add(succ.getNumber());
x.add(cfg.getNumber(succ));
}
}
return x;
Expand Down Expand Up @@ -223,7 +222,7 @@ public String toString() {
* @param cfg governing control flow graph
* @param wantEdgeLabels whether to compute edge labels for CDG edges
*/
public ControlDependenceGraph(ControlFlowGraph<I, T> cfg, boolean wantEdgeLabels) {
public ControlDependenceGraph(MinimalCFG<T> cfg, boolean wantEdgeLabels) {
if (cfg == null) {
throw new IllegalArgumentException("null cfg");
}
Expand All @@ -234,11 +233,11 @@ public ControlDependenceGraph(ControlFlowGraph<I, T> cfg, boolean wantEdgeLabels
/**
* @param cfg governing control flow graph
*/
public ControlDependenceGraph(ControlFlowGraph<I, T> cfg) {
public ControlDependenceGraph(MinimalCFG<T> cfg) {
this(cfg, false);
}

public ControlFlowGraph getControlFlowGraph() {
public MinimalCFG getControlFlowGraph() {
return cfg;
}

Expand Down
2 changes: 1 addition & 1 deletion com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/PDG.java
Expand Up @@ -251,7 +251,7 @@ private void createControlDependenceEdges(ControlDependenceOptions cOptions, IR
Assertions.productionAssertion(cOptions.equals(ControlDependenceOptions.FULL));
}

ControlDependenceGraph<SSAInstruction, ISSABasicBlock> cdg = new ControlDependenceGraph<SSAInstruction, ISSABasicBlock>(
ControlDependenceGraph<ISSABasicBlock> cdg = new ControlDependenceGraph<ISSABasicBlock>(
controlFlowGraph);
for (ISSABasicBlock bb : cdg) {
if (bb.isExitBlock()) {
Expand Down
Expand Up @@ -16,7 +16,7 @@
import com.ibm.wala.util.graph.AbstractNumberedGraph;
import com.ibm.wala.util.intset.IntSet;

public abstract class AbstractNumberedLabeledGraph<T, U> extends AbstractNumberedGraph<T> implements LabeledGraph<T, U> {
public abstract class AbstractNumberedLabeledGraph<T, U> extends AbstractNumberedGraph<T> implements LabeledGraph<T, U>, NumberedLabeledGraph<T, U> {

/**
* @return the object which manages edges in the graph
Expand Down
@@ -0,0 +1,7 @@
package com.ibm.wala.util.graph.labeled;

import com.ibm.wala.util.graph.NumberedGraph;

public interface NumberedLabeledGraph<T, I> extends NumberedGraph<T>, NumberedLabeledEdgeManager<T,I> {

}

0 comments on commit f3b5d41

Please sign in to comment.