summaryrefslogtreecommitdiff
path: root/vaporize/library/ControlFlowGraph.java
blob: 364cec19d54b2e73ebfd76c8388fbb018fdc90f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package vaporize.library;

import cs132.vapor.ast.*;
import misc.*;

import java.util.ArrayList;

public class ControlFlowGraph {

    private ArrayList<CFGNode> nodes;
    private CFGNode start;

    protected ControlFlowGraph() {
        this.nodes = new ArrayList<>();
        this.start = null;
    }

    protected CFGNode getNode(Object a) {
        CFGNode ret = null;
        for (CFGNode n : this.nodes) {
            if (n.equals(a)) {
                ret = n;
                break;
            }
        }

        if (ret == null) {
            String str = (a instanceof Node) ? ((Node) a).sourcePos.toString() :
                a.toString();
            MinimalLogger.severe(String.format("Could not find a node matching %s",
                                               str));
        }
        return ret;
    }

    protected void addNode(CFGNode node) {
        this.nodes.add(node);
    }

    protected String addEdge(CFGNode source, CFGNode dest) {
        String ret = String.format("%d -> %d",
                                   source.getInstruction().sourcePos.line,
                                   dest.getInstruction().sourcePos.line);
        MinimalLogger.info(String.format("Edge %s",
                                         ret));

        source.addDest(dest);
        dest.addSource(source);

        return ret +";";
    }

    protected void setStart(CFGNode start) {
        this.start = start;
    }

    protected ArrayList<CFGNode> getNodes() {
        return this.nodes;
    }

}