Skip to content
PDGGraph.java 7.14 KiB
Newer Older
Javier Costa's avatar
Javier Costa committed
package tfm.graphs;
Javier Costa's avatar
Javier Costa committed

import com.github.javaparser.Position;
Javier Costa's avatar
Javier Costa committed
import com.github.javaparser.ast.stmt.EmptyStmt;
import com.github.javaparser.ast.stmt.Statement;
import edg.graphlib.Arrow;
Javier Costa's avatar
Javier Costa committed
import edg.graphlib.Vertex;
import edg.graphlib.Visitor;
Javier Costa's avatar
Javier Costa committed
import tfm.arcs.Arc;
import tfm.arcs.cfg.ControlFlowArc;
Javier Costa's avatar
Javier Costa committed
import tfm.arcs.data.ArcData;
Javier Costa's avatar
Javier Costa committed
import tfm.arcs.pdg.ControlDependencyArc;
import tfm.arcs.pdg.DataDependencyArc;
import tfm.nodes.CFGNode;
Javier Costa's avatar
Javier Costa committed
import tfm.nodes.PDGNode;
import tfm.nodes.Node;
Javier Costa's avatar
Javier Costa committed
import tfm.slicing.SlicingCriterion;
import tfm.utils.Logger;
Javier Costa's avatar
Javier Costa committed
import tfm.utils.NodeNotFoundException;
Javier Costa's avatar
Javier Costa committed
import tfm.variables.*;
import tfm.variables.actions.VariableDeclaration;
import tfm.variables.actions.VariableUse;
import tfm.variables.actions.VariableDefinition;
Javier Costa's avatar
Javier Costa committed
import tfm.visitors.PDGCFGVisitor;
Javier Costa's avatar
Javier Costa committed

Javier Costa's avatar
Javier Costa committed
import javax.swing.plaf.nimbus.State;
Javier Costa's avatar
Javier Costa committed
import java.util.*;
import java.util.stream.Collectors;
Javier Costa's avatar
Javier Costa committed
import java.util.stream.Stream;
Javier Costa's avatar
Javier Costa committed

public class PDGGraph extends Graph<PDGNode> {
Javier Costa's avatar
Javier Costa committed

    public PDGGraph() {
Javier Costa's avatar
Javier Costa committed
        setRootVertex(new PDGNode(getNextVertexId(), getRootNodeData(), new EmptyStmt()));
Javier Costa's avatar
Javier Costa committed
    }

    protected String getRootNodeData() {
        return "Entry";
    }
Javier Costa's avatar
Javier Costa committed

Javier Costa's avatar
Javier Costa committed
    public PDGNode addNode(PDGNode node) {
        PDGNode vertex = new PDGNode(node);
Javier Costa's avatar
Javier Costa committed
        super.addVertex(vertex);

        return vertex;
    }

Javier Costa's avatar
Javier Costa committed
    @Override
Javier Costa's avatar
Javier Costa committed
    public PDGNode addNode(String instruction, Statement statement) {
Javier Costa's avatar
Javier Costa committed
        return addNode(getNextVertexId(), instruction, statement);
    }

    public PDGNode addNode(int id, String instruction, Statement statement) {
        PDGNode vertex = new PDGNode(id, instruction, statement);
Javier Costa's avatar
Javier Costa committed
        super.addVertex(vertex);

        return vertex;
    }

    @SuppressWarnings("unchecked")
    private void addArc(Arc arc) {
        super.addEdge(arc);
    }

Javier Costa's avatar
Javier Costa committed
    public void addControlDependencyArc(PDGNode from, PDGNode to) {
Javier Costa's avatar
Javier Costa committed
        ControlDependencyArc controlDependencyArc = new ControlDependencyArc(from, to);

        this.addArc(controlDependencyArc);
    }

Javier Costa's avatar
Javier Costa committed
    public void addDataDependencyArc(PDGNode from, PDGNode to, String variable) {
Javier Costa's avatar
Javier Costa committed
        DataDependencyArc dataDataDependencyArc = new DataDependencyArc(from, to, variable);

        this.addArc(dataDataDependencyArc);
    }

Javier Costa's avatar
Javier Costa committed
    public Set<PDGNode> getNodesAtLevel(int level) {
Javier Costa's avatar
Javier Costa committed
        return getVerticies().stream()
                .map(vertex -> (PDGNode) vertex)
                .filter(node -> node.getLevel() == level)
Javier Costa's avatar
Javier Costa committed
                .collect(Collectors.toSet());
Javier Costa's avatar
Javier Costa committed
    }

    public int getLevels() {
        return getVerticies().stream()
                .map(vertex -> (PDGNode) vertex)
                .max(Comparator.comparingInt(PDGNode::getLevel))
                .map(PDGNode::getLevel)
                .get() + 1;
    }

Javier Costa's avatar
Javier Costa committed
    @Override
    public String toGraphvizRepresentation() {
        String lineSep = System.lineSeparator();

Javier Costa's avatar
Javier Costa committed
        String nodesDeclaration = getNodes().stream()
                .sorted(Comparator.comparingInt(Node::getId))
                .map(Node::toGraphvizRepresentation)
Javier Costa's avatar
Javier Costa committed
                .collect(Collectors.joining(lineSep));

Javier Costa's avatar
Javier Costa committed
        StringBuilder rankedNodes = new StringBuilder();

        // No level 0 is needed (only one node)
        for (int i = 0; i < getLevels(); i++) {
Javier Costa's avatar
Javier Costa committed
            Set<PDGNode> levelNodes = getNodesAtLevel(i);
Javier Costa's avatar
Javier Costa committed

            if (levelNodes.size() <= 1) {
                continue;
            }

            // rank same
            rankedNodes.append("{ rank = same; ")
                    .append(levelNodes.stream()
                        .map(node -> String.valueOf(node.getId()))
                        .collect(Collectors.joining(";")))
                    .append(" }")
                    .append(lineSep);

            // invisible arrows for ordering
            rankedNodes.append(levelNodes.stream()
                        .sorted(Comparator.comparingInt(PDGNode::getId))
                        .map(node -> String.valueOf(node.getId()))
                        .collect(Collectors.joining(" -> ")))
                    .append("[style = invis];")
                    .append(lineSep);
        }

Javier Costa's avatar
Javier Costa committed
        String arrows =
Javier Costa's avatar
Javier Costa committed
                getArcs().stream()
Javier Costa's avatar
Javier Costa committed
                        .sorted(Comparator.comparingInt(arrow -> ((Node) arrow.getFrom()).getId()))
Javier Costa's avatar
Javier Costa committed
                        .map(Arc::toGraphvizRepresentation)
Javier Costa's avatar
Javier Costa committed
                        .collect(Collectors.joining(lineSep));


        return "digraph g{" + lineSep +
                "splines=true;" + lineSep +
                nodesDeclaration + lineSep +
                arrows + lineSep +
                rankedNodes.toString() +
Javier Costa's avatar
Javier Costa committed
                "}";
    }
Javier Costa's avatar
Javier Costa committed
    public PDGGraph slice(SlicingCriterion slicingCriterion) {
        Optional<PDGNode> optionalPDGNode = slicingCriterion.findNode(this);
Javier Costa's avatar
Javier Costa committed
        if (!optionalPDGNode.isPresent()) {
            throw new NodeNotFoundException(slicingCriterion);
        }
Javier Costa's avatar
Javier Costa committed
        PDGNode node = optionalPDGNode.get();
Javier Costa's avatar
Javier Costa committed
        Logger.format("Slicing node: %s", node);
Javier Costa's avatar
Javier Costa committed
        Set<Integer> sliceNodes = getSliceNodes(new HashSet<>(), node);
Javier Costa's avatar
Javier Costa committed
        PDGGraph sliceGraph = new PDGGraph();

Javier Costa's avatar
Javier Costa committed
        com.github.javaparser.ast.Node astCopy = node.getAstNode().findRootNode().clone();

        astCopy.accept(new PDGCFGVisitor(sliceGraph), sliceGraph.getRootNode());
Javier Costa's avatar
Javier Costa committed
        for (PDGNode sliceNode : sliceGraph.getNodes()) {
            if (!sliceNodes.contains(sliceNode.getId())) {
Javier Costa's avatar
Javier Costa committed
                Logger.log("Removing node " + sliceNode.getId());
Javier Costa's avatar
Javier Costa committed
                sliceNode.getAstNode().removeForced();
Javier Costa's avatar
Javier Costa committed
                sliceGraph.removeNode(sliceNode);
Javier Costa's avatar
Javier Costa committed
            }
Javier Costa's avatar
Javier Costa committed
//        for (Arc arc : getArcs()) {
//            Optional<PDGNode> fromOptional = sliceGraph.findNodeById(arc.getFromNode().getId());
//            Optional<PDGNode> toOptional = sliceGraph.findNodeById(arc.getToNode().getId());
//
//            if (fromOptional.isPresent() && toOptional.isPresent()) {
//                PDGNode from = fromOptional.get();
//                PDGNode to = toOptional.get();
//
//                if (arc.isControlDependencyArrow()) {
//                    sliceGraph.addControlDependencyArc(from, to);
//                } else {
//                    DataDependencyArc dataDependencyArc = (DataDependencyArc) arc;
//                    sliceGraph.addDataDependencyArc(from, to, dataDependencyArc.getData().getVariables().get(0));
//                }
//            }
//        }
Javier Costa's avatar
Javier Costa committed

        return sliceGraph;
Javier Costa's avatar
Javier Costa committed
    private Set<Integer> getSliceNodes(Set<Integer> visited, PDGNode root) {
        visited.add(root.getId());

//        Set<String> searchVariables = new HashSet<>(variables);

        for (Arrow arrow : root.getIncomingArrows()) {
            Arc arc = (Arc) arrow;

Javier Costa's avatar
Javier Costa committed
//            if (arc.isDataDependencyArrow()
//                    && Collections.disjoint(((DataDependencyArc) arc).getData().getVariables(), searchVariables)) {
//                continue;
//            }

            PDGNode from = (PDGNode) arc.getFromNode();

Javier Costa's avatar
Javier Costa committed
//            Logger.log("Arrow from node: " + from);
Javier Costa's avatar
Javier Costa committed
            if (visited.contains(from.getId())) {
//                Logger.log("It's already visited. Continuing...");
                continue;
            }

            getSliceNodes(visited, from);
        }

Javier Costa's avatar
Javier Costa committed
//        Logger.format("Done with node %s", root.getId());
Javier Costa's avatar
Javier Costa committed
}