Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
ASMGraph and dot output works now
Browse files Browse the repository at this point in the history
  • Loading branch information
anmolkabra committed Apr 28, 2019
1 parent b01f766 commit 34f72d3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ vm/shared/xic
*.typed
*.ir
*.s
*.dot

## Misc ignores
# Platform-specific and editor temporaries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import edu.cornell.cs.cs4120.xic.ir.dfa.IRGraph;
import edu.cornell.cs.cs4120.xic.ir.dfa.ReachingDefnsDFA;
import kc875.cfg.Graph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -22,7 +23,7 @@ public CopyPropagationVisitor() { }
public IRCompUnit propagateCopies(IRCompUnit irnode) {
IRCompUnit optimizedCompUnit = new IRCompUnit(irnode.name());
for (IRFuncDecl funcDecl : irnode.functions().values()) {
irGraph = IRGraph.buildCFG(funcDecl);
irGraph = new IRGraph(funcDecl);
//TODO: is reaching definitions analysis necessary here?
ReachingDefnsDFA reachingDefnsDFA = new ReachingDefnsDFA(irGraph);
reachingDefnsDFA.runWorklistAlgo();
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/kc875/asm/ASMExprName.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ public boolean equals(Object obj) {
public Set<ASMExprRT> vars() {
return new HashSet<>();
}

/**
* Returns true if this is for a function, false otherwise.
*/
public boolean isFunction() {
return name.startsWith("_I"); // based on the ABI spec
}
}
25 changes: 14 additions & 11 deletions src/main/java/kc875/asm/dfa/ASMGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ public class ASMGraph extends Graph<ASMInstr> {
ASMOpCode.JGE,
ASMOpCode.JL,
ASMOpCode.JLE,
ASMOpCode.JNE,
ASMOpCode.RET,
ASMOpCode.CALL
) // TODO
ASMOpCode.JNE
)
);

/**
Expand All @@ -45,11 +43,12 @@ public ASMGraph(List<ASMInstr> instrs) {
// set the start node
Iterator<ASMInstr> iter = instrs.iterator();
Node previous = new Node(iter.next());
setStartNode(previous);

while (iter.hasNext()) {
ASMInstr instr = iter.next();
Node node = new Node(instr);
this.addOtherNode(node);
addOtherNode(node);
nodeInstrMap.put(node, instr);

if (instr instanceof ASMInstrLabel) {
Expand All @@ -58,29 +57,33 @@ public ASMGraph(List<ASMInstr> instrs) {

// add an edge from the previous instruction's node if we need to
if (hasEdgeToNextInstr(previous.getT())) {
this.addEdge(previous, node);
addEdge(previous, node);
}
previous = node;
}

// SECOND PASS: add CFG edges for jumps to labelled nodes

for (Node node : this.getAllNodes()) {
for (Node node : getAllNodes()) {
ASMInstr instr = node.getT();

if (!jumps.contains(instr.getOpCode())) {
// this instr is not a jump node, continue to next node
// instr is not a jump node, continue to next node
continue;
}

// we need to add another edge to the jumped-to node!
if (instr instanceof ASMInstr_1Arg) {
// TODO: change for A7 since we'll be able to jump to non-labels
ASMExprName label = (ASMExprName) ((ASMInstr_1Arg) instr).getArg();
ASMExprName arg = (ASMExprName) ((ASMInstr_1Arg) instr).getArg();

// If the arg is not for a function, then we can jump to it
// inside this function
// get the node we jump to and add an edge to it
Node to = labelToNodeMap.get(label.getName());
this.addEdge(node, to);
if (!arg.isFunction()) {
Node to = labelToNodeMap.get(arg.getName());
addEdge(node, to);
}
} else {
throw new InternalCompilerError("Jmp instructions can't be " +
"2Arg");
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/kc875/cfg/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Sets;
import org.apache.commons.io.FilenameUtils;

import java.io.FileWriter;
import java.io.IOException;
Expand Down Expand Up @@ -87,6 +88,15 @@ public boolean comesFrom(Node n) {
public boolean isAdj(Node n) {
return in.contains(n) || out.contains(n);
}

@Override
public String toString() {
return "Node{" +
"t=" + t +
", in=" + in +
", out=" + out +
'}';
}
}

private Node startNode;
Expand Down Expand Up @@ -163,9 +173,12 @@ public void show(String path) throws IOException {
}

// Write prologue
String INDENT_TAB = " ";
String INDENT_TAB = "\t";
FileWriter f = new FileWriter(path);
f.write("digraph " + path + "\n");
String rawPath = FilenameUtils.getName(
FilenameUtils.removeExtension(path)
);
f.write("digraph " + rawPath + " {\n");
f.write(INDENT_TAB + "node [shape=record];\n");
f.write("\n");

Expand All @@ -188,8 +201,6 @@ public void show(String path) throws IOException {
}
f.write("\n");

BiMap<String, Node> idNodeMap = nodeIDMap.inverse();

Set<Node> visited = new HashSet<>();
Stack<Node> unvisited = new Stack<>();
unvisited.push(startNode);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/kc875/cli/CLIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static void fileoutError(String outputFilePath, String errMessage) {
static void fileoutIRPhase(IRNode ir, OptimPhases p, String path)
throws Exception {
// Get output file name and write the IR
String fName = path + "_initial.ir";
String fName = path + "_" + p.toString().toLowerCase() + ".ir";
FileWriter writer = new FileWriter(fName);
OptimalCodeWriter cw = new OptimalCodeWriter(writer, 80);
CodeWriterSExpPrinter printer = new CodeWriterSExpPrinter(cw);
Expand Down

0 comments on commit 34f72d3

Please sign in to comment.