All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jruby.ir.dataflow.analyses.LiveVariablesProblem Maven / Gradle / Ivy

There is a newer version: 9.4.12.0
Show newest version
package org.jruby.ir.dataflow.analyses;

import org.jruby.ir.IREvalScript;
import org.jruby.ir.IRScope;
import org.jruby.ir.dataflow.DataFlowProblem;
import org.jruby.ir.operands.LocalVariable;
import org.jruby.ir.operands.Variable;
import org.jruby.ir.representations.BasicBlock;

import java.util.*;

public class LiveVariablesProblem extends DataFlowProblem {
    public static final String NAME = "Live Variables Analysis";

    public LiveVariablesProblem(IRScope scope) {
        super(DataFlowProblem.DF_Direction.BACKWARD);
        varsLiveOnScopeExit = new ArrayList();
        setup(scope);
    }

    public Integer getDFVar(Variable v) {
        return dfVarMap.get(v);
    }

    public boolean dfVarExists(Variable v) {
        return getDFVar(v) != null;
    }

    public Variable getVariable(int id) {
        return varDfVarMap.get(id);
    }

    @Override
    public LiveVariableNode buildFlowGraphNode(BasicBlock bb) {
        return new LiveVariableNode(this, bb);
    }

    public void addDFVar(Variable v) {
        Integer dfv = addDataFlowVar();
        dfVarMap.put(v, dfv);
        varDfVarMap.put(dfv, v);

        if (v instanceof LocalVariable && !v.isSelf()) {
            //System.out.println("Adding df var for " + v + ":" + dfv.id);
            localVars.add((LocalVariable) v);
        }
    }

    /**
     * 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 Collection getLocalVarsLiveOnScopeEntry() {
        List liveVars = new ArrayList();
        BitSet liveIn = getFlowGraphNode(getScope().getCFG().getEntryBB()).getLiveOutBitSet();

        for (int i = 0; i < liveIn.size(); i++) {
            if (!liveIn.get(i)) continue;

            Variable v = getVariable(i);
            if (v instanceof LocalVariable) {
                liveVars.add((LocalVariable)v);
            }
            // System.out.println("variable " + v + " is live on entry!");
        }

        return liveVars;
    }

    @Override
    public String getDataFlowVarsForOutput() {
        StringBuilder buf = new StringBuilder();
        for (Map.Entry entry : dfVarMap.entrySet()) {
            buf.append("DF Var ").append(entry.getValue()).append(" = ").append(entry.getKey()).append('\n');
        }

        return buf.toString();
    }

    public void markDeadInstructions() {
        for (LiveVariableNode n : flowGraphNodes) {
            n.markDeadInstructions();
        }
    }

    public void setVarsLiveOnScopeExit(Collection vars) {
        varsLiveOnScopeExit.addAll(vars);
    }

    public Collection getVarsLiveOnScopeExit() {
        return varsLiveOnScopeExit;
    }

    public Set getAllVars() {
        return dfVarMap.keySet();
    }

    public Set getNonSelfLocalVars() {
        return localVars;
    }

    @Override
    public String getName() {
        return NAME;
    }

    /* ----------- Private Interface ------------ */
    private HashMap dfVarMap = new HashMap();
    private HashMap varDfVarMap = new HashMap();
    private HashSet localVars = new HashSet(); // Local variables that can be live across dataflow barriers
    private Collection varsLiveOnScopeExit;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy