LARA

package minijava.controlflow 
 
abstract class TransferFunction[L, CFGStatement] {
  def apply(node : CFGStatement, x : L) : L
}
 
class AnalysisAlgoritm[L,CFGStatement]
               (transferFun : TransferFunction[L, CFGStatement],
		lattice : Lattice{type Elem=L},
		cfg : LabeledDirectedGraphImp[CFGStatement])
{
  type Vertex = VertexImp[CFGStatement]
 
  var facts : Map[Vertex, L] = Map[Vertex,L]()
 
  def init = {
    facts = Map[Vertex,L]().withDefaultValue(lattice.bottom)
  }
 
  def setNodeFact(v : Vertex, fact : L) = { 
    facts = facts.update(v, fact)
  }
 
  def computeFixpoint : Unit = {
    var change = true
    while (change) {
      change = false
      for (v <- cfg.V) {
	// print("node " + v)
	val oldFact : L = facts(v)
	var newFact : L = oldFact
	for (e <- cfg.inEdges(v)) {
	  val propagated = transferFun(e.lab, facts(e.v1))
	  newFact = lattice.join(newFact, propagated)
	}
	// println(" has fact " + newFact)
	if (newFact != oldFact) {
	  change = true;
	  facts = facts.update(v, newFact)
	}
      }
    }
  }
  def getResult : Map[Vertex,L] = facts
}