package org.jruby.ir.instructions;
import java.util.Map;
import org.jruby.ir.IRScope;
import org.jruby.ir.IRVisitor;
import org.jruby.ir.Operation;
import org.jruby.ir.operands.LocalVariable;
import org.jruby.ir.operands.Operand;
import org.jruby.ir.persistence.IRReaderDecoder;
import org.jruby.ir.persistence.IRWriterEncoder;
import org.jruby.ir.transformations.inlining.CloneInfo;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
public class StoreLocalVarInstr extends TwoOperandInstr implements FixedArityInstr {
private final IRScope scope;
public StoreLocalVarInstr(IRScope scope, Operand value, LocalVariable lvar) {
super(Operation.BINDING_STORE, value, lvar);
this.scope = scope;
}
public Operand getValue() {
return getOperand1();
}
public LocalVariable getLocalVar() {
return (LocalVariable) getOperand2();
}
public IRScope getScope() {
return scope;
}
@Override
public String[] toStringNonOperandArgs() {
return new String[] { "scope_name: " + scope.getId()};
}
public void decrementLVarScopeDepth() {
setOperand2(getLocalVar().cloneForDepth(getLocalVar().getScopeDepth()-1));
}
@Override
public void simplifyOperands(Map<Operand, Operand> valueMap, boolean force) {
setOperand1(getValue().getSimplifiedOperand(valueMap, force));
}
@Override
public Instr clone(CloneInfo ii) {
return new StoreLocalVarInstr(scope, getValue().cloneForInlining(ii),
(LocalVariable) getLocalVar().cloneForInlining(ii));
}
@Override
public void encode(IRWriterEncoder e) {
super.encode(e);
e.encode(getScope());
e.encode(getValue());
e.encode(getLocalVar());
}
public static StoreLocalVarInstr decode(IRReaderDecoder d) {
return new StoreLocalVarInstr(d.decodeScope(), d.decodeOperand(), (LocalVariable) d.decodeOperand());
}
@Override
public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
Object varValue = getValue().retrieve(context, self, currScope, currDynScope, temp);
currDynScope.setValue((IRubyObject)varValue, getLocalVar().getLocation(), getLocalVar().getScopeDepth());
return null;
}
@Override
public void visit(IRVisitor visitor) {
visitor.StoreLocalVarInstr(this);
}
}