|
| 1 | +package org.jruby.ir.instructions; |
| 2 | + |
| 3 | +import org.jruby.RubyRegexp; |
| 4 | +import org.jruby.ir.IRFlags; |
| 5 | +import org.jruby.ir.IRScope; |
| 6 | +import org.jruby.ir.IRVisitor; |
| 7 | +import org.jruby.ir.Operation; |
| 8 | +import org.jruby.ir.operands.Variable; |
| 9 | +import org.jruby.ir.persistence.IRReaderDecoder; |
| 10 | +import org.jruby.ir.persistence.IRWriterEncoder; |
| 11 | +import org.jruby.ir.runtime.IRRuntimeHelpers; |
| 12 | +import org.jruby.ir.transformations.inlining.CloneInfo; |
| 13 | +import org.jruby.parser.StaticScope; |
| 14 | +import org.jruby.runtime.DynamicScope; |
| 15 | +import org.jruby.runtime.ThreadContext; |
| 16 | +import org.jruby.runtime.builtin.IRubyObject; |
| 17 | + |
| 18 | +import java.util.EnumSet; |
| 19 | + |
| 20 | +// Represents a backref node in Ruby code |
| 21 | +public class BuildNthRefInstr extends NoOperandResultBaseInstr { |
| 22 | + final public int group; |
| 23 | + |
| 24 | + public BuildNthRefInstr(Variable result, int group) { |
| 25 | + super(Operation.BUILD_NTHREF, result); |
| 26 | + this.group = group; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void encode(IRWriterEncoder e) { |
| 31 | + super.encode(e); |
| 32 | + e.encode(group); |
| 33 | + } |
| 34 | + |
| 35 | + public static BuildNthRefInstr decode(IRReaderDecoder d) { |
| 36 | + return new BuildNthRefInstr(d.decodeVariable(), d.decodeInt()); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Instr clone(CloneInfo ii) { |
| 41 | + return new BuildNthRefInstr(ii.getRenamedVariable(result), group); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public String[] toStringNonOperandArgs() { |
| 46 | + return new String[] {"$" + "'" + group + "'"}; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) { |
| 51 | + return IRRuntimeHelpers.nthMatch(context, group); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void visit(IRVisitor visitor) { |
| 56 | + visitor.BuildNthRefInstr(this); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean computeScopeFlags(IRScope scope, EnumSet<IRFlags> flags) { |
| 61 | + flags.add(IRFlags.REQUIRES_BACKREF); |
| 62 | + |
| 63 | + return true; |
| 64 | + } |
| 65 | +} |
0 commit comments