Skip to content

Commit

Permalink
Refactor three match instrs to one and always dyncall.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed May 5, 2015
1 parent 1e80aa2 commit 8d67e58
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 192 deletions.
14 changes: 9 additions & 5 deletions core/src/main/java/org/jruby/ir/IRBuilder.java
Expand Up @@ -2659,14 +2659,17 @@ public Operand buildLocalVar(LocalVarNode node) {
public Operand buildMatch(MatchNode matchNode) {
Operand regexp = build(matchNode.getRegexpNode());

return addResultInstr(new MatchInstr(createTemporaryVariable(), regexp));
Variable tempLastLine = createTemporaryVariable();
addResultInstr(new GetGlobalVariableInstr(tempLastLine, "$_"));
return addResultInstr(new MatchInstr(createTemporaryVariable(), regexp, tempLastLine));
}

public Operand buildMatch2(Match2Node matchNode) {
Operand receiver = build(matchNode.getReceiverNode());
Operand value = build(matchNode.getValueNode());
Variable result = createTemporaryVariable();
addInstr(new Match2Instr(result, receiver, value));
addInstr(new MatchInstr(result, receiver, value));

if (matchNode instanceof Match2CaptureNode) {
Match2CaptureNode m2c = (Match2CaptureNode)matchNode;
for (int slot: m2c.getScopeOffsets()) {
Expand All @@ -2690,10 +2693,11 @@ private String getVarNameFromScopeTree(IRScope scope, int depth, int offset) {
}

public Operand buildMatch3(Match3Node matchNode) {
Operand receiver = build(matchNode.getReceiverNode());
Operand value = build(matchNode.getValueNode());
// This reversal is intentional
Operand receiver = build(matchNode.getValueNode());
Operand value = build(matchNode.getReceiverNode());

return addResultInstr(new Match3Instr(createTemporaryVariable(), receiver, value));
return addResultInstr(new MatchInstr(createTemporaryVariable(), receiver, value));
}

private Operand getContainerFromCPath(Colon3Node cpath) {
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/org/jruby/ir/IRVisitor.java
Expand Up @@ -76,9 +76,7 @@ private void error(Object object) {
public void LoadLocalVarInstr(LoadLocalVarInstr loadlocalvarinstr) { error(loadlocalvarinstr); }
public void LoadImplicitClosure(LoadImplicitClosureInstr loadimplicitclosureinstr) { error(loadimplicitclosureinstr); }
public void LoadFrameClosure(LoadFrameClosureInstr loadframeclosureinstr) { error(loadframeclosureinstr); }
public void Match2Instr(Match2Instr match2instr) { error(match2instr); }
public void Match3Instr(Match3Instr match3instr) { error(match3instr); }
public void MatchInstr(MatchInstr matchinstr) { error(matchinstr); }
public void MatchInstr(MatchInstr matchInstr) { error(matchInstr); }
public void ModuleVersionGuardInstr(ModuleVersionGuardInstr moduleversionguardinstr) { error(moduleversionguardinstr); }
public void NonlocalReturnInstr(NonlocalReturnInstr nonlocalreturninstr) { error(nonlocalreturninstr); }
public void NopInstr(NopInstr nopinstr) { error(nopinstr); }
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/java/org/jruby/ir/Operation.java
Expand Up @@ -82,8 +82,6 @@ public enum Operation {
EQQ(OpFlags.f_has_side_effect | OpFlags.f_can_raise_exception), // a === call used in when
LAMBDA(OpFlags.f_has_side_effect | OpFlags.f_can_raise_exception),
MATCH(OpFlags.f_has_side_effect | OpFlags.f_can_raise_exception),
MATCH2(OpFlags.f_has_side_effect | OpFlags.f_can_raise_exception),
MATCH3(OpFlags.f_has_side_effect | OpFlags.f_can_raise_exception),

/* Yield: Is this a call? Implementing instr doesn't inherit from CallBase.java */
YIELD(OpFlags.f_has_side_effect | OpFlags.f_can_raise_exception),
Expand Down
71 changes: 0 additions & 71 deletions core/src/main/java/org/jruby/ir/instructions/Match2Instr.java

This file was deleted.

72 changes: 0 additions & 72 deletions core/src/main/java/org/jruby/ir/instructions/Match3Instr.java

This file was deleted.

35 changes: 21 additions & 14 deletions core/src/main/java/org/jruby/ir/instructions/MatchInstr.java
Expand Up @@ -10,21 +10,32 @@
import org.jruby.ir.persistence.IRWriterEncoder;
import org.jruby.ir.transformations.inlining.CloneInfo;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.CallType;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.callsite.CachingCallSite;
import org.jruby.runtime.callsite.NormalCachingCallSite;

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

public class MatchInstr extends ResultBaseInstr implements FixedArityInstr {
public MatchInstr(Variable result, Operand receiver) {
super(Operation.MATCH, result, new Operand[] { receiver });
public class MatchInstr extends CallBase implements FixedArityInstr, ResultInstr {
protected Variable result;

assert result != null: "MatchInstr result is null";
public MatchInstr(Variable result, Operand receiver, Operand arg) {
super(Operation.MATCH, CallType.NORMAL, "=~", receiver, new Operand[]{arg}, null, false);

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

this.result = result;
}

public Variable getResult() {
return result;
}

public Operand getReceiver() {
return operands[0];
public void updateResult(Variable v) {
this.result = v;
}

@Override
Expand All @@ -37,23 +48,19 @@ public boolean computeScopeFlags(IRScope scope) {

@Override
public Instr clone(CloneInfo ii) {
return new MatchInstr((Variable) result.cloneForInlining(ii), getReceiver().cloneForInlining(ii));
return new MatchInstr((Variable) result.cloneForInlining(ii),
getReceiver().cloneForInlining(ii), getArg1().cloneForInlining(ii));
}

@Override
public void encode(IRWriterEncoder e) {
super.encode(e);
e.encode(getReceiver());
e.encode(getArg1());
}

public static MatchInstr decode(IRReaderDecoder d) {
return new MatchInstr(d.decodeVariable(), d.decodeOperand());
}

@Override
public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
RubyRegexp regexp = (RubyRegexp) getReceiver().retrieve(context, self, currScope, currDynScope, temp);
return regexp.op_match2_19(context);
return new MatchInstr(d.decodeVariable(), d.decodeOperand(), d.decodeOperand());
}

@Override
Expand Down
Expand Up @@ -236,8 +236,6 @@ public Instr decodeInstr() {
case MASGN_REQD: return ReqdArgMultipleAsgnInstr.decode(this);
case MASGN_REST: return RestArgMultipleAsgnInstr.decode(this);
case MATCH: return MatchInstr.decode(this);
case MATCH2: return Match2Instr.decode(this);
case MATCH3: return Match3Instr.decode(this);
case NONLOCAL_RETURN: return NonlocalReturnInstr.decode(this);
case NOP: return NopInstr.NOP;
case NORESULT_CALL:
Expand Down
25 changes: 2 additions & 23 deletions core/src/main/java/org/jruby/ir/targets/JVMVisitor.java
Expand Up @@ -1245,29 +1245,8 @@ public void LoadFrameClosure(LoadFrameClosureInstr loadframeclosureinstr) {
}

@Override
public void Match2Instr(Match2Instr match2instr) {
visit(match2instr.getReceiver());
jvmMethod().loadContext();
visit(match2instr.getArg());
jvmAdapter().invokevirtual(p(RubyRegexp.class), "op_match19", sig(IRubyObject.class, ThreadContext.class, IRubyObject.class));
jvmStoreLocal(match2instr.getResult());
}

@Override
public void Match3Instr(Match3Instr match3instr) {
jvmMethod().loadContext();
visit(match3instr.getReceiver());
visit(match3instr.getArg());
jvmAdapter().invokestatic(p(IRRuntimeHelpers.class), "match3", sig(IRubyObject.class, ThreadContext.class, RubyRegexp.class, IRubyObject.class));
jvmStoreLocal(match3instr.getResult());
}

@Override
public void MatchInstr(MatchInstr matchinstr) {
visit(matchinstr.getReceiver());
jvmMethod().loadContext();
jvmAdapter().invokevirtual(p(RubyRegexp.class), "op_match2_19", sig(IRubyObject.class, ThreadContext.class));
jvmStoreLocal(matchinstr.getResult());
public void MatchInstr(MatchInstr matchInstr) {
compileCallCommon(jvmMethod(), "=~", matchInstr.getCallArgs(), matchInstr.getReceiver(), 1, null, false, CallType.NORMAL, matchInstr.getResult(), false);
}

@Override
Expand Down

0 comments on commit 8d67e58

Please sign in to comment.