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

Commit

Permalink
Replace Guava 16 with 27, fix live var analysis, create a SetWithInf …
Browse files Browse the repository at this point in the history
…wrapper for inf sets
  • Loading branch information
anmolkabra committed Apr 23, 2019
1 parent c6caeeb commit c3dd265
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 47 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
compile fileTree(dir: 'lib', include: ['commons-io-2.4.jar'])
compile fileTree(dir: 'lib', include: ['picocli-3.9.2.jar'])
compile fileTree(dir: 'lib', include: ['java_cup.jar'])
compile fileTree(dir: 'lib', include: ['guava-27.1-jre.jar'])
testCompile group: 'junit', name: 'junit', version: '4.12'
}

Expand Down
Binary file removed lib/guava-16.0.1.jar
Binary file not shown.
Binary file added lib/guava-27.1-jre.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kc875.asm.graph;
package kc875.asm.dfa;

import kc875.asm.ASMExprRegReplaceable;
import kc875.asm.ASMInstr;
Expand Down Expand Up @@ -26,7 +26,7 @@ public List<ASMExprRegReplaceable> getDef(ASMInstr i) {
//constructor that returns a new flow graph
public ASMFlowGraph(List<ASMInstr> instrs){
for (ASMInstr i : instrs){
GraphNode n = newNode();
GraphNode n = new GraphNode(this);
instrMap.put(n, i);
genMap.put(n, getUse(i));
killMap.put(n, getDef(i));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kc875.asm.graph;
package kc875.asm.dfa;

import kc875.asm.ASMExprRegReplaceable;
import kc875.cfg.Graph;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kc875.asm.graph;
package kc875.asm.dfa;

import kc875.asm.ASMExprTemp;
import kc875.asm.ASMInstr_2Arg;
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/kc875/asm/dfa/LiveVariableDFA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package kc875.asm.dfa;

import kc875.asm.ASMExprRegReplaceable;
import kc875.cfg.DFAFramework;
import kc875.cfg.Graph;
import kc875.cfg.GraphNode;
import kc875.utils.SetWithInf;

/**
* Live variable DFA (used in reg allocation). The lattice elements are sets
* of Temps/Regs.
*/
public class LiveVariableDFA extends DFAFramework<SetWithInf<ASMExprRegReplaceable>> {

public LiveVariableDFA(Graph graph) {
super(
graph,
Direction.BACKWARD,
(node, l) -> use(node).union(l.diff(def(node))),
SetWithInf::union,
SetWithInf.infSet()
);
}

// TODO
public static SetWithInf<ASMExprRegReplaceable> use(GraphNode node) {
return new SetWithInf<>();
}

// TODO
public static SetWithInf<ASMExprRegReplaceable> def(GraphNode node) {
return new SetWithInf<>();
}
}
43 changes: 0 additions & 43 deletions src/main/java/kc875/asm/graph/Liveness.java

This file was deleted.

66 changes: 66 additions & 0 deletions src/main/java/kc875/cfg/DFAFramework.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package kc875.cfg;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;

public abstract class DFAFramework<T> {
// T are the lattice elements.

// Graph associated with this DFA.
protected Graph graph;

// Direction of the DFA
public enum Direction {FORWARD, BACKWARD}
protected Direction direction;

// Transformer function or F. Takes in the node and a lattice element,
// and returns a lattice element.
protected BiFunction<GraphNode, T, T> F;

// Meet operator, combining l1 and l2 to produce l. A BinaryOperator<T>
// is a BiFunction<T, T, T>.
protected BinaryOperator<T> meet;

/**
* Applies the meet operator on the lattice elements ls. Returns Optional
* .empty() if ls is empty, otherwise reductively applies the meet operator.
* @param ls lattice elements.
*/
public Optional<T> applyMeet(Collection<T> ls) {
return ls.stream().reduce(meet);
}

// Maps from nodes to lattice elements in (after meet if applicable) and
// out (before meet if applicable) of the node.
protected Map<GraphNode, T> inMap = new HashMap<>();
protected Map<GraphNode, T> outMap = new HashMap<>();

/**
* Initialize the DFA Framework, with all nodes' inMap and outMap
* initialized to lattice element top.
* @param graph graph associated with this DFA.
* @param direction direction of DFA.
* @param F transformer function.
* @param meet meet operator.
* @param top top lattice element for initialization.
*/
public DFAFramework(Graph graph,
Direction direction,
BiFunction<GraphNode, T, T> F,
BinaryOperator<T> meet,
T top
) {
this.graph = graph;
this.direction = direction;
this.F = F;
this.meet = meet;
for (GraphNode node : graph.getAllNodes()) {
inMap.put(node, top);
outMap.put(node, top);
}
}
}
66 changes: 66 additions & 0 deletions src/main/java/kc875/utils/SetWithInf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package kc875.utils;

import com.google.common.collect.Sets;

import java.util.HashSet;
import java.util.Set;

/**
* A wrapper on Set, providing support for infinite sets.
*/
public class SetWithInf<E> {
// Class invariants:
// - An element s cannot be in both includeSet and excludeSet at the same
// time.
// - A set can be infinite and still have non-empty excludeSet. If a set
// is infinite, then we don't care about the includeSet.
private Set<E> set;
private boolean isInf = false;

public SetWithInf(Set<E> set) {
this.set = set;
isInf = false;
}

public SetWithInf() {
this(new HashSet<>());
}

public static<T> SetWithInf<T> infSet() {
SetWithInf<T> set = new SetWithInf<>();
set.isInf = true;
return set;
}

/**
* Returns a SetWithInf of T elements with includeSet as the wrapped set
* in the returned wrapper.
* @param includeSet set to wrap around.
*/
private static<T> SetWithInf<T> infSet(Set<T> includeSet) {
SetWithInf<T> set = new SetWithInf<>(includeSet);
set.isInf = true;
return set;
}

public SetWithInf<E> union(SetWithInf<E> other) {
Set<E> unionSet = Sets.union(this.set, other.set).immutableCopy();
if (this.isInf || other.isInf) {
// return an inf set but with the wrapped sets combined
return infSet(unionSet);
} else {
// both are non-inf sets
return new SetWithInf<>(unionSet);
}
}

// TODO
public SetWithInf<E> intersect(SetWithInf<E> other) {
return null;
}

// TODO
public SetWithInf<E> diff(SetWithInf<E> other) {
return null;
}
}

0 comments on commit c3dd265

Please sign in to comment.