StoreLocalVarInstr.java

  1. package org.jruby.ir.instructions;

  2. import org.jruby.ir.IRScope;
  3. import org.jruby.ir.IRVisitor;
  4. import org.jruby.ir.Operation;
  5. import org.jruby.ir.operands.LocalVariable;
  6. import org.jruby.ir.operands.Operand;
  7. import org.jruby.ir.transformations.inlining.CloneInfo;
  8. import org.jruby.parser.StaticScope;
  9. import org.jruby.runtime.DynamicScope;
  10. import org.jruby.runtime.ThreadContext;
  11. import org.jruby.runtime.builtin.IRubyObject;

  12. import java.util.Map;

  13. public class StoreLocalVarInstr extends Instr implements FixedArityInstr {
  14.     private final IRScope scope;
  15.     private Operand value;

  16.     /** This is the variable that is being stored into in this scope.  This variable
  17.      * doesn't participate in the computation itself.  We just use it as a proxy for
  18.      * its (a) name (b) offset (c) scope-depth. */
  19.     private LocalVariable lvar;

  20.     public StoreLocalVarInstr(Operand value, IRScope scope, LocalVariable lvar) {
  21.         super(Operation.BINDING_STORE);

  22.         this.lvar = lvar;
  23.         this.value = value;
  24.         this.scope = scope;
  25.     }

  26.     public IRScope getScope() {
  27.         return scope;
  28.     }

  29.     @Override
  30.     public Operand[] getOperands() {
  31.         return new Operand[]{value, lvar};
  32.     }

  33.     @Override
  34.     public void simplifyOperands(Map<Operand, Operand> valueMap, boolean force) {
  35.         value = value.getSimplifiedOperand(valueMap, force);
  36.     }

  37.     @Override
  38.     public String toString() {
  39.         return "store_lvar(" + value + ", " + scope.getName() + ", " + lvar + ")";
  40.     }

  41.     public LocalVariable getLocalVar() {
  42.         return lvar;
  43.     }

  44.     // SSS FIXME: This feels dirty
  45.     public void decrementLVarScopeDepth() {
  46.         this.lvar = lvar.cloneForDepth(lvar.getScopeDepth()-1);
  47.     }

  48.     @Override
  49.     public Instr clone(CloneInfo ii) {
  50.         // SSS FIXME: Do we need to rename lvar really?  It is just a name-proxy!
  51.         return new StoreLocalVarInstr(value.cloneForInlining(ii), scope, (LocalVariable)lvar.cloneForInlining(ii));
  52.     }

  53.     @Override
  54.     public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
  55.         Object varValue = value.retrieve(context, self, currScope, currDynScope, temp);
  56.         currDynScope.setValue((IRubyObject)varValue, lvar.getLocation(), lvar.getScopeDepth());
  57.         return null;
  58.     }

  59.     @Override
  60.     public void visit(IRVisitor visitor) {
  61.         visitor.StoreLocalVarInstr(this);
  62.     }

  63.     public Operand getValue() {
  64.         return value;
  65.     }
  66. }