Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jruby.ir.dataflow.analyses;
  
  import java.util.BitSet;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Set;
 
 public class LiveVariablesProblem extends DataFlowProblem {
     public static final String NAME = "Live Variables Analysis";
     private static final Set<LocalVariableEMPTY_SET = new HashSet<LocalVariable>();
 
     public LiveVariablesProblem(IRScope scope) {
         this(scope);
     }
 
     LiveVariablesProblem(IRScope scopeSet<LocalVariablenonSelfLocalVars) {
         super(..);
 
         setup(scopenonSelfLocalVars);
     }
 
     public DataFlowVar getDFVar(Variable v) {
         return .get(v);
     }
 
     public boolean dfVarExists(Variable v) {
         return getDFVar(v) != null;
     }
 
     public Variable getVariable(int id) {
         return .get(id);
     }
 
     public FlowGraphNode buildFlowGraphNode(BasicBlock bb) {
         return new LiveVariableNode(thisbb);
     }
 
     public void addDFVar(Variable v) {
         DataFlowVar dfv = new DataFlowVar(this);
         .put(vdfv);
         .put(dfv.idv);
         if ((v instanceof LocalVariable) && !((LocalVariablev).isSelf()) {
             //System.out.println("Adding df var for " + v + ":" + dfv.id);
             int n = ((LocalVariable)v).getScopeDepth();
             IRScope s = getScope();
             while ((s != null) && (n >= 0)) {
                 if (s instanceof IREvalScript) {
                     // If a variable is at the topmost scope of the eval OR crosses an eval boundary,
                     // it is going to be marked always live since it could be used by other evals (n = 0)
                     // or by enclosing scopes (n > 0)
                     .add((LocalVariable)v);
                     break;
                 }
                 s = s.getLexicalParent();
                 n--;
             }
             .add((LocalVariablev);
         }
     }

    
Get variables that are live on entry to the cfg. This is the case for closures which access variables from the parent scope. sum = 0; a.each { |i| sum += i }; return sum In the code snippet above, 'sum' is live on entry to the closure
 
     public List<VariablegetVarsLiveOnScopeEntry() {
         List<VariableliveVars = new ArrayList<Variable>();
 
         for (int i = 0; i < liveIn.size(); i++) {
             if (liveIn.get(i) == true) {
                 Variable v = getVariable(i);
                 liveVars.add(v);
                 // System.out.println("variable " + v + " is live on entry!");
             }
         }
 
         return liveVars;
     }

    
Initialize the problem with all vars from the surrounding scope variables. In closures, vars defined in the closure (or accessed from the surrounding scope) can be used outside the closure. sum = 0; a.each { |i| sum += i }; return sum In the code snippet above, 'sum' is live on entry to and exit from the closure.
    public final void setup(IRScope scopeCollection<LocalVariableallVars) {
        // System.out.println("\nCFG:\n" + scope.cfg().toStringGraph());
        // System.out.println("\nInstrs:\n" + scope.cfg().toStringInstrs());
         = new ArrayList<LocalVariable>();
        setup(scope);
        // Init vars live on scope exit to vars that always live throughout the scope
        for (Variable v : allVars) {
            if (getDFVar(v) == nulladdDFVar(v);
        }
    }
    @Override
    public String getDataFlowVarsForOutput() {
        StringBuilder buf = new StringBuilder();
        for (Variable v : .keySet()) {
            buf.append("DF Var ").append(.get(v).getId()).append(" = ").append(v).append("\n");
        }
        return buf.toString();
    }
    public void markDeadInstructions() {
        for (FlowGraphNode n : ) {
            ((LiveVariableNoden).markDeadInstructions();
        }
    }
    public void setVarsLiveOnScopeExit(Collection<LocalVariablevars) {
        .addAll(vars);
    }
        return ;
    }
    public Set<VariablegetAllVars() {
        return .keySet();
    }
        return ;
    }
    public String getName() {
        return ;
    }
    /* ----------- Private Interface ------------ */
    private HashMap<VariableDataFlowVardfVarMap = new HashMap<VariableDataFlowVar>();
    private HashMap<IntegerVariablevarDfVarMap = new HashMap<IntegerVariable>();
    private HashSet<LocalVariablelocalVars = new HashSet<LocalVariable>(); // Local variables that can be live across dataflow barriers
    // SSS FIXME: Should this be part of IRScope??
    private List<LocalVariablealwaysLiveVars// Variables that cross eval boundaries and are always live in this scope
New to GrepCode? Check out our FAQ X