Advertisement
Guest User

Untitled

a guest
May 6th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.89 KB | None | 0 0
  1. package tip.analysis
  2.  
  3. import tip.graph.ControlFlowGraph
  4. import tip.ast._
  5. import tip.graph.NodeOps._
  6. import tip.ast.AstOps._
  7. import tip.lattices._
  8. import tip.solvers._
  9. import tip.graph._
  10.  
  11. /**
  12.  * Base class for the live variables analysis
  13.  */
  14. abstract class LiveVarsAnalysis(cfg: ControlFlowGraph[AstNode]) extends FlowSensitiveAnalysis(cfg) {
  15.  
  16.   val allVars = cfg.nodes.map {
  17.     _.appearingIds
  18.   }.flatten
  19.  
  20.   /**
  21.    * The lattice for the liveness analysis
  22.    */
  23.   val lattice = new MapLattice(cfg.nodes, new PowersetLattice(allVars))
  24.  
  25.   def transfer(n: GNode[AstNode], s: lattice.l.Element, o: lattice.Element): lattice.l.Element = {
  26.     val succStates = n.succ.map { x => o(x) }
  27.     val joinState = succStates.foldLeft(lattice.l.bottom) { (lub, succ) => lattice.l.lub(lub, succ) }
  28.  
  29.     n match {
  30.       case exit: FunExit[AstNode] => lattice.l.bottom
  31.       case r: GRealNode[AstNode] =>
  32.         r.data match {
  33.           case cond: ABinaryOp => lattice.l.lub(joinState, cond.appearingIds) //This must be a guard, indeed
  34.           case ass: AAssignStmt => ass.left match {
  35.             case id: AIdentifier => lattice.l.lub((joinState - id), ass.right.appearingIds)
  36.             case un: AUnaryOp => lattice.l.lub(joinState, ass.right.appearingIds)
  37.           }
  38.           case varr: AVarStmt => joinState -- varr.appearingIds
  39.           case _ => joinState
  40.         }
  41.       case _ => joinState
  42.     }
  43.  
  44.   }
  45. }
  46.  
  47. /**
  48.  * A live variables analysis which uses the simple fixpoint solver
  49.  */
  50. class LiveVarsAnalysisSimpleSolver(cfg: ControlFlowGraph[AstNode])
  51.   extends LiveVarsAnalysis(cfg) with SimpleFixpointSolver with MapLatticeTransfer[GNode[AstNode]]
  52.  
  53. /**
  54.  * A live variables analysis which uses the worklist solver
  55.  */
  56. class LiveVarsAnalysisWorklistSolver(cfg: ControlFlowGraph[AstNode])
  57.   extends LiveVarsAnalysis(cfg) with WorklistFixpointSolver[GNode[AstNode]] with BackwardDependencies
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement