MatchInstr.java

  1. package org.jruby.ir.instructions;

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

  13. import java.util.Map;

  14. import static org.jruby.ir.IRFlags.USES_BACKREF_OR_LASTLINE;

  15. public class MatchInstr extends Instr implements ResultInstr, FixedArityInstr {
  16.     private Variable result;
  17.     private Operand receiver;

  18.     public MatchInstr(Variable result, Operand receiver) {
  19.         super(Operation.MATCH);

  20.         assert result != null: "MatchInstr result is null";

  21.         this.result = result;
  22.         this.receiver = receiver;
  23.     }

  24.     @Override
  25.     public Operand[] getOperands() {
  26.         return new Operand[] { receiver };
  27.     }

  28.     public Operand getReceiver() {
  29.         return receiver;
  30.     }

  31.     @Override
  32.     public String toString() {
  33.         return super.toString() + "(" + receiver + ")";
  34.     }

  35.     @Override
  36.     public boolean computeScopeFlags(IRScope scope) {
  37.         // $~ is implicitly used since Backref and NthRef operands
  38.         // access it and $~ is not made explicit in those operands.
  39.         scope.getFlags().add(USES_BACKREF_OR_LASTLINE);
  40.         return true;
  41.     }

  42.     @Override
  43.     public void simplifyOperands(Map<Operand, Operand> valueMap, boolean force) {
  44.         receiver = receiver.getSimplifiedOperand(valueMap, force);
  45.     }

  46.     public Variable getResult() {
  47.         return result;
  48.     }

  49.     public void updateResult(Variable v) {
  50.         this.result = v;
  51.     }

  52.     @Override
  53.     public Instr clone(CloneInfo ii) {
  54.         return new MatchInstr((Variable) result.cloneForInlining(ii), receiver.cloneForInlining(ii));
  55.     }

  56.     @Override
  57.     public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
  58.         RubyRegexp regexp = (RubyRegexp) receiver.retrieve(context, self, currScope, currDynScope, temp);
  59.         return regexp.op_match2_19(context);
  60.     }

  61.     @Override
  62.     public void visit(IRVisitor visitor) {
  63.         visitor.MatchInstr(this);
  64.     }
  65. }