package org.jruby.ir.instructions;
import java.util.Map;
import org.jruby.ir.IRScope;
import org.jruby.ir.IRVisitor;
import org.jruby.ir.Interp;
import org.jruby.ir.Operation;
import org.jruby.ir.operands.*;
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 LoadLocalVarInstr extends OneOperandResultBaseInstr implements FixedArityInstr {
private final IRScope scope;
public LoadLocalVarInstr(IRScope scope, TemporaryLocalVariable result, LocalVariable lvar) {
super(Operation.BINDING_LOAD, result, lvar);
assert result != null: "LoadLocalVarInstr result is null";
this.scope = scope;
}
public LocalVariable getLocalVar() {
return (LocalVariable) getOperand1();
}
public IRScope getScope() {
return scope;
}
@Override
public void simplifyOperands(Map<Operand, Operand> valueMap, boolean force) {
}
public void decrementLVarScopeDepth() {
setOperand(0, getLocalVar().cloneForDepth(getLocalVar().getScopeDepth()-1));
}
@Override
public String[] toStringNonOperandArgs() {
return new String[] { "scope: " + scope.getId() };
}
@Override
public Instr clone(CloneInfo ii) {
return new LoadLocalVarInstr(scope, (TemporaryLocalVariable)ii.getRenamedVariable(result),
(LocalVariable)ii.getRenamedVariable(getLocalVar()));
}
@Override
public void encode(IRWriterEncoder e) {
super.encode(e);
e.encode(getScope());
e.encode(getLocalVar());
}
public static LoadLocalVarInstr decode(IRReaderDecoder d) {
TemporaryLocalVariable result = (TemporaryLocalVariable) d.decodeVariable();
return new LoadLocalVarInstr(d.decodeScope(), result, (LocalVariable) d.decodeVariable());
}
@Interp
@Override
public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
return getLocalVar().retrieve(context, self, currScope, currDynScope, temp);
}
@Override
public void visit(IRVisitor visitor) {
visitor.LoadLocalVarInstr(this);
}
}