make use of iterable interface for graphs
This commit is contained in:
@ -10,11 +10,13 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public final class DataFlowGraph {
|
||||
public final class DataFlowGraph implements Iterable<DataFlowNode> {
|
||||
|
||||
// TODO: Why use list, its a graph
|
||||
private final List<DataFlowNode> graph;
|
||||
|
||||
private DataFlowGraph(List<DataFlowNode> graph) {
|
||||
@ -25,8 +27,8 @@ public final class DataFlowGraph {
|
||||
public static DataFlowGraph fromSourceGraph(FlowGraph srcGraph) {
|
||||
final List<DataFlowNode> graph = new LinkedList<>();
|
||||
|
||||
for (FlowBasicBlock block : srcGraph.getBasicBlocks()) {
|
||||
for (FlowInstruction inst : block.getInstructions()) {
|
||||
for (FlowBasicBlock block : srcGraph) {
|
||||
for (FlowInstruction inst : block) {
|
||||
graph.add(DataFlowNode.fromFlowNode(inst, block));
|
||||
}
|
||||
}
|
||||
@ -37,8 +39,8 @@ public final class DataFlowGraph {
|
||||
}
|
||||
|
||||
private static void initSuccPred(FlowGraph srcGraph, List<DataFlowNode> graph) {
|
||||
for (FlowBasicBlock block : srcGraph.getBasicBlocks()) {
|
||||
for (FlowInstruction inst : block.getInstructions()) {
|
||||
for (FlowBasicBlock block : srcGraph) {
|
||||
for (FlowInstruction inst : block) {
|
||||
final DataFlowNode current = getNodeByInstruction(inst, graph);
|
||||
|
||||
for (FlowInstruction pred : block.getPredecessors(inst)) {
|
||||
@ -66,8 +68,12 @@ public final class DataFlowGraph {
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public List<DataFlowNode> getNodes() {
|
||||
return this.graph;
|
||||
public int indexOf(DataFlowNode node) {
|
||||
return this.graph.indexOf(node);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.graph.size();
|
||||
}
|
||||
|
||||
// Printing
|
||||
@ -137,4 +143,11 @@ public final class DataFlowGraph {
|
||||
|
||||
return "Finished.";
|
||||
}
|
||||
|
||||
// Overrides
|
||||
|
||||
@Override
|
||||
public Iterator<DataFlowNode> iterator() {
|
||||
return this.graph.iterator();
|
||||
}
|
||||
}
|
||||
|
@ -9,11 +9,13 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public final class InterferenceGraph {
|
||||
public final class InterferenceGraph implements Iterable<InterferenceNode> {
|
||||
|
||||
// TODO: Why use list, its a graph
|
||||
private final List<InterferenceNode> nodes;
|
||||
|
||||
private InterferenceGraph(List<InterferenceNode> nodes) {
|
||||
@ -29,7 +31,7 @@ public final class InterferenceGraph {
|
||||
}
|
||||
|
||||
// Determine neighbours
|
||||
for (DataFlowNode node : graph.getNodes()) {
|
||||
for (DataFlowNode node : graph) {
|
||||
Logger.log("NODE " + node.getInst() + " - OUT: " + node.getOut());
|
||||
|
||||
for (String left : node.getOut()) {
|
||||
@ -58,12 +60,6 @@ public final class InterferenceGraph {
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
// Getters, Setters
|
||||
|
||||
public List<InterferenceNode> getNodes() {
|
||||
return this.nodes;
|
||||
}
|
||||
|
||||
// Printing
|
||||
|
||||
public String printToImage() {
|
||||
@ -113,4 +109,11 @@ public final class InterferenceGraph {
|
||||
|
||||
return "Finished.";
|
||||
}
|
||||
|
||||
// Overrides
|
||||
|
||||
@Override
|
||||
public Iterator<InterferenceNode> iterator() {
|
||||
return this.nodes.iterator();
|
||||
}
|
||||
}
|
||||
|
@ -28,8 +28,9 @@ public final class LivenessAnalysis {
|
||||
do {
|
||||
change = false;
|
||||
|
||||
for (DataFlowNode node : graph.getNodes()) {
|
||||
if (graph.getNodes().indexOf(node) == graph.getNodes().size() - 1) {
|
||||
for (DataFlowNode node : graph) {
|
||||
// TODO: Indexof mega unnötig
|
||||
if (graph.indexOf(node) == graph.size() - 1) {
|
||||
// Skip END
|
||||
|
||||
continue;
|
||||
@ -39,10 +40,11 @@ public final class LivenessAnalysis {
|
||||
}
|
||||
} while (change);
|
||||
|
||||
// TODO: Indexof mega unnötig
|
||||
Logger.log("IN, OUT Sets:");
|
||||
for (DataFlowNode node : graph.getNodes()) {
|
||||
Logger.log(graph.getNodes().indexOf(node) + ": " + node.getInst() + " IN: " + node.getIn());
|
||||
Logger.log(graph.getNodes().indexOf(node) + ": " + node.getInst() + " OUT: " + node.getOut());
|
||||
for (DataFlowNode node : graph) {
|
||||
Logger.log(graph.indexOf(node) + ": " + node.getInst() + " IN: " + node.getIn());
|
||||
Logger.log(graph.indexOf(node) + ": " + node.getInst() + " OUT: " + node.getOut());
|
||||
}
|
||||
Logger.log("\n");
|
||||
}
|
||||
@ -79,7 +81,7 @@ public final class LivenessAnalysis {
|
||||
int colors = 0;
|
||||
int currentColor;
|
||||
|
||||
for (InterferenceNode node : this.interferenceGraph.getNodes()) {
|
||||
for (InterferenceNode node : this.interferenceGraph) {
|
||||
|
||||
currentColor = 1;
|
||||
|
||||
|
@ -2,12 +2,13 @@ package codegen.flowgraph;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FlowBasicBlock {
|
||||
public class FlowBasicBlock implements Iterable<FlowInstruction> {
|
||||
|
||||
private final String id;
|
||||
private final String label;
|
||||
@ -51,26 +52,14 @@ public class FlowBasicBlock {
|
||||
instruction, args));
|
||||
}
|
||||
|
||||
public List<FlowInstruction> getInstructions() {
|
||||
return this.instructions;
|
||||
}
|
||||
|
||||
public void addSuccessor(FlowBasicBlock block) {
|
||||
this.successors.add(block);
|
||||
}
|
||||
|
||||
public void addSuccessors(Set<FlowBasicBlock> successors) {
|
||||
this.successors.addAll(successors);
|
||||
}
|
||||
|
||||
public void addPredecessor(FlowBasicBlock block) {
|
||||
this.predecessors.add(block);
|
||||
}
|
||||
|
||||
public void addPredecessors(Set<FlowBasicBlock> predecessors) {
|
||||
this.predecessors.addAll(predecessors);
|
||||
}
|
||||
|
||||
public Set<FlowBasicBlock> getSuccessorSet() {
|
||||
return this.successors;
|
||||
}
|
||||
@ -135,7 +124,7 @@ public class FlowBasicBlock {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Print + Overrides
|
||||
// Printing
|
||||
|
||||
public String printInst() {
|
||||
return this.instructions.stream()
|
||||
@ -146,6 +135,8 @@ public class FlowBasicBlock {
|
||||
.collect(Collectors.joining());
|
||||
}
|
||||
|
||||
// Overrides
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.id);
|
||||
@ -174,4 +165,9 @@ public class FlowBasicBlock {
|
||||
return this.label + ":\n"
|
||||
+ linesString;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<FlowInstruction> iterator() {
|
||||
return this.instructions.iterator();
|
||||
}
|
||||
}
|
||||
|
@ -9,12 +9,13 @@ import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FlowGraph {
|
||||
public class FlowGraph implements Iterable<FlowBasicBlock> {
|
||||
|
||||
private final FlowGraphHead head;
|
||||
private final List<FlowBasicBlock> basicBlocks;
|
||||
@ -122,8 +123,6 @@ public class FlowGraph {
|
||||
this.basicBlocks.removeAll(toRemove);
|
||||
}
|
||||
|
||||
// Getters, Setters
|
||||
|
||||
private FlowBasicBlock getBlockByLabel(String label) {
|
||||
return this.basicBlocks.stream()
|
||||
.filter(block -> block.getLabel().equals(label))
|
||||
@ -135,11 +134,7 @@ public class FlowGraph {
|
||||
return this.basicBlocks.get(this.basicBlocks.size() - 1);
|
||||
}
|
||||
|
||||
public List<FlowBasicBlock> getBasicBlocks() {
|
||||
return this.basicBlocks;
|
||||
}
|
||||
|
||||
// Print + Overrides
|
||||
// Printing
|
||||
|
||||
public String print() {
|
||||
final String blocksString = this.basicBlocks.stream()
|
||||
@ -222,6 +217,8 @@ public class FlowGraph {
|
||||
return "Finished.";
|
||||
}
|
||||
|
||||
// Overrides
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final String blocksString = this.basicBlocks.stream()
|
||||
@ -233,4 +230,8 @@ public class FlowGraph {
|
||||
+ this.tail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<FlowBasicBlock> iterator() {
|
||||
return this.basicBlocks.iterator();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user