Package org.jruby.ir.instructions

Source Code of org.jruby.ir.instructions.Match3Instr

package org.jruby.ir.instructions;

import org.jruby.RubyRegexp;
import org.jruby.ir.IRScope;
import org.jruby.ir.IRVisitor;
import org.jruby.ir.Operation;
import org.jruby.ir.operands.Operand;
import org.jruby.ir.operands.Variable;
import org.jruby.ir.runtime.IRRuntimeHelpers;
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;

import java.util.Map;

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

public class Match3Instr extends Instr implements ResultInstr, FixedArityInstr {
    private Variable result;
    private Operand receiver;
    private Operand arg;

    public Match3Instr(Variable result, Operand receiver, Operand arg) {
        super(Operation.MATCH3);

        assert result != null: "Match3Instr result is null";

        this.result = result;
        this.receiver = receiver;
        this.arg = arg;
    }

    public Operand getArg() {
        return arg;
    }

    public Operand getReceiver() {
        return receiver;
    }

    @Override
    public Operand[] getOperands() {
        return new Operand[] { receiver, arg };
    }

    @Override
    public String toString() {
        return super.toString() + "(" + receiver + ", " + arg + ")";
    }

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

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

    public Variable getResult() {
        return result;
    }

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

    @Override
    public Instr clone(CloneInfo ii) {
        return new Match3Instr((Variable) result.cloneForInlining(ii),
                receiver.cloneForInlining(ii), arg.cloneForInlining(ii));
    }

    @Override
    public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
        RubyRegexp regexp = (RubyRegexp) receiver.retrieve(context, self, currScope, currDynScope, temp);
        IRubyObject argValue = (IRubyObject) arg.retrieve(context, self, currScope, currDynScope, temp);

        return IRRuntimeHelpers.match3(context, regexp, argValue);
    }

    @Override
    public void visit(IRVisitor visitor) {
        visitor.Match3Instr(this);
    }
}
TOP

Related Classes of org.jruby.ir.instructions.Match3Instr

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.