// We have to spill the var that is defined in the closure as well because the load var pass
// will attempt to load the var always. So, if the call doesn't actually call the closure,
// we'll be in trouble in that scenario!
if (spillAllVars || cl.usesLocalVariable(v) || cl.definesLocalVariable(v)) {
addedStores = true;
instrs.add(new StoreLocalVarInstr(getLocalVarReplacement(v, scope, varRenameMap), scope, v));
newDirtyVars.remove(v);
}
}
dirtyVars = newDirtyVars;
instrs.next();
} else if (scopeBindingHasEscaped || call.targetRequiresCallersBinding()) { // Call has no closure && it requires stores
// Add before call -- hence instrs.previous & instrs.next
instrs.previous();
for (LocalVariable v : dirtyVars) {
addedStores = true;
instrs.add(new StoreLocalVarInstr(getLocalVarReplacement(v, scope, varRenameMap), scope, v));
}
instrs.next();
dirtyVars.clear();
} else {
instrs.previous();
// All variables not local to the current scope have to be always spilled because of
// multi-threading scenarios where some other scope could load this variable concurrently.
//
// Allocate a new hash-set and modify it to get around ConcurrentModificationException on dirtyVars
Set<LocalVariable> newDirtyVars = new HashSet<LocalVariable>(dirtyVars);
for (LocalVariable v : dirtyVars) {
if ((v instanceof ClosureLocalVariable) && ((ClosureLocalVariable)v).definingScope != scope) {
addedStores = true;
instrs.add(new StoreLocalVarInstr(getLocalVarReplacement(v, scope, varRenameMap), scope, v));
newDirtyVars.remove(v);
}
}
dirtyVars = newDirtyVars;
instrs.next();
}
} else if ((isClosure && (i instanceof ReturnInstr)) || (i instanceof BreakInstr)) {
// At closure return and break instructions (both of which are exits from the closure),
// we need a binding store on exit only for vars that are both:
//
// (a) dirty,
// (b) live on exit from the closure
// condition reqd. because the variable could be dirty but not used outside.
// Ex: s=0; a.each { |i| j = i+1; sum += j; return if j < 5; sum += 1; }; puts sum
// i,j are dirty inside the block, but not used outside
//
// If this also happens to be exit BB, we would have intersected already earlier -- so no need to do it again!
if (!amExitBB) {
LiveVariablesProblem lvp = (LiveVariablesProblem)scope.getDataFlowSolution(DataFlowConstants.LVP_NAME);
java.util.Collection<LocalVariable> liveVars = lvp.getVarsLiveOnScopeExit();
if (liveVars != null) {
dirtyVars.retainAll(liveVars); // Intersection with variables live on exit from the scope
} else {
dirtyVars.clear();
}
}
// Add before call
instrs.previous();
boolean f = addClosureExitStoreLocalVars(scope, instrs, dirtyVars, varRenameMap);
addedStores = addedStores || f;
instrs.next();
// Nothing is dirty anymore -- everything that needs spilling has been spilt
dirtyVars.clear();
}
if (scopeBindingHasEscaped && (i.getOperation() == Operation.PUT_GLOBAL_VAR)) {
// global-var tracing can execute closures set up in previous trace-var calls
// in which case we would have the 'scopeBindingHasEscaped' flag set to true
instrs.previous();
for (LocalVariable v : dirtyVars) {
addedStores = true;
instrs.add(new StoreLocalVarInstr(getLocalVarReplacement(v, scope, varRenameMap), scope, v));
}
instrs.next();
dirtyVars.clear();
}
if (i.canRaiseException()) {
if (rescueNode != null) {
// If exceptions will be rescued, spill every dirty var here
// Add before excepting instr -- hence instrs.previous & instrs.next
instrs.previous();
for (LocalVariable v : dirtyVars) {
addedStores = true;
instrs.add(new StoreLocalVarInstr(getLocalVarReplacement(v, scope, varRenameMap), scope, v));
}
instrs.next();
dirtyVars.clear();
} else if (excTargetDirtyVars != null) {
// If exceptions won't be rescued, pass them back to be spilled in the global ensure block