Skip to content

Commit

Permalink
Simplify constant and ivar defined? logic in the compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius authored and Prathamesh Sonpatki committed Sep 18, 2012
1 parent 2998175 commit 970afc1
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 56 deletions.
12 changes: 12 additions & 0 deletions src/org/jruby/ast/executable/AbstractScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,18 @@ public IRubyObject run(ThreadContext context, IRubyObject self, IRubyObject[] ar
public final IRubyObject getConstant8(ThreadContext context, String name) {return runtimeCache.getConstant(context, name, 8);}
public final IRubyObject getConstant9(ThreadContext context, String name) {return runtimeCache.getConstant(context, name, 9);}

public final IRubyObject getConstantDefined(ThreadContext context, String name, int i) {return runtimeCache.getConstantDefined(context, name, i);}
public final IRubyObject getConstantDefined0(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 0);}
public final IRubyObject getConstantDefined1(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 1);}
public final IRubyObject getConstantDefined2(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 2);}
public final IRubyObject getConstantDefined3(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 3);}
public final IRubyObject getConstantDefined4(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 4);}
public final IRubyObject getConstantDefined5(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 5);}
public final IRubyObject getConstantDefined6(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 6);}
public final IRubyObject getConstantDefined7(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 7);}
public final IRubyObject getConstantDefined8(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 8);}
public final IRubyObject getConstantDefined9(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 9);}

public static final int NUMBERED_CONSTANTFROM_COUNT = 10;

public final IRubyObject getConstantFrom(RubyModule target, ThreadContext context, String name, int i) {return runtimeCache.getConstantFrom(target, context, name, i);}
Expand Down
8 changes: 7 additions & 1 deletion src/org/jruby/ast/executable/RuntimeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.callsite.CacheEntry;
import org.jruby.util.ByteList;
import org.jruby.util.DefinedMessage;
import org.jruby.util.RegexpOptions;

public class RuntimeCache {
Expand Down Expand Up @@ -186,7 +187,7 @@ public final IRubyObject getVariable(ThreadContext context, int index, String na
}

public final IRubyObject getVariableDefined(ThreadContext context, int index, String name, IRubyObject object) {
return getValue(context, index, name, object);
return getValue(context, index, name, object) == null ? null : context.runtime.getDefinedMessage(DefinedMessage.INSTANCE_VARIABLE);
}

private final IRubyObject getValue(ThreadContext context, int index, String name, IRubyObject object) {
Expand Down Expand Up @@ -399,6 +400,11 @@ public final IRubyObject getConstant(ThreadContext context, String name, int ind
return value != null ? value : context.getCurrentScope().getStaticScope().getModule().callMethod(context, "const_missing", context.runtime.fastNewSymbol(name));
}

public final IRubyObject getConstantDefined(ThreadContext context, String name, int index) {
IRubyObject value = getValue(context, name, index);
return value == null ? null : context.runtime.getDefinedMessage(DefinedMessage.CONSTANT);
}

public IRubyObject getValue(ThreadContext context, String name, int index) {
IRubyObject value = constants[index]; // Store to temp so it does null out on us mid-stream
return isCached(context, value, index) ? value : reCache(context, name, index);
Expand Down
28 changes: 2 additions & 26 deletions src/org/jruby/compiler/ASTCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1549,34 +1549,10 @@ public void branch(BodyCompiler context) {
});
break;
case INSTVARNODE:
context.isInstanceVariableDefined(((InstVarNode) node).getName(),
new BranchCallback() {

public void branch(BodyCompiler context) {
context.pushDefinedMessage(DefinedMessage.INSTANCE_VARIABLE);
}
},
new BranchCallback() {

public void branch(BodyCompiler context) {
context.pushNull();
}
});
context.isInstanceVariableDefined(((InstVarNode) node).getName());
break;
case CONSTNODE:
context.isConstantDefined(((ConstNode) node).getName(),
new BranchCallback() {

public void branch(BodyCompiler context) {
context.pushDefinedMessage(DefinedMessage.CONSTANT);
}
},
new BranchCallback() {

public void branch(BodyCompiler context) {
context.pushNull();
}
});
context.isConstantDefined(((ConstNode) node).getName());
break;
case FCALLNODE:
context.loadSelf();
Expand Down
4 changes: 2 additions & 2 deletions src/org/jruby/compiler/BodyCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ public void defineNewMethod(String name, int methodArity, StaticScope scope,
public void isMethodBound(String name, BranchCallback trueBranch, BranchCallback falseBranch);
public void hasBlock(BranchCallback trueBranch, BranchCallback falseBranch);
public void isGlobalDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch);
public void isConstantDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch);
public void isInstanceVariableDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch);
public void isConstantDefined(String name);
public void isInstanceVariableDefined(String name);
public void isClassVarDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch);
public Object getNewEnding();
public void ifNull(Object gotoToken);
Expand Down
2 changes: 2 additions & 0 deletions src/org/jruby/compiler/CacheCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public interface CacheCompiler {

public void cacheConstant(BaseBodyCompiler method, String constantName);

public void cacheConstantDefined(BaseBodyCompiler method, String constantName);

public void cacheConstantFrom(BaseBodyCompiler method, String constantName);

public int cacheStaticScope(BaseBodyCompiler method, StaticScope scope);
Expand Down
32 changes: 5 additions & 27 deletions src/org/jruby/compiler/impl/BaseBodyCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1448,12 +1448,8 @@ public void pollThreadEvents() {
}

public void nullToNil() {
Label notNull = new Label();
method.dup();
method.ifnonnull(notNull);
method.pop();
loadNil();
method.label(notNull);
loadThreadContext();
invokeUtilityMethod("nullToNil", sig(IRubyObject.class, IRubyObject.class, ThreadContext.class));
}

public void isInstanceOf(Class clazz, BranchCallback trueBranch, BranchCallback falseBranch) {
Expand Down Expand Up @@ -1960,30 +1956,12 @@ public void isGlobalDefined(String name, BranchCallback trueBranch, BranchCallba
method.label(exitLabel);
}

public void isConstantDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch) {
loadThreadContext();
method.ldc(name);
invokeThreadContext("getConstantDefined", sig(boolean.class, params(String.class)));
Label falseLabel = new Label();
Label exitLabel = new Label();
method.ifeq(falseLabel); // EQ == 0 (i.e. false)
trueBranch.branch(this);
method.go_to(exitLabel);
method.label(falseLabel);
falseBranch.branch(this);
method.label(exitLabel);
public void isConstantDefined(String name) {
script.getCacheCompiler().cacheConstantDefined(this, name);
}

public void isInstanceVariableDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch) {
public void isInstanceVariableDefined(String name) {
script.getCacheCompiler().cachedGetVariableDefined(this, name);
Label trueLabel = new Label();
Label exitLabel = new Label();
method.ifnonnull(trueLabel);
falseBranch.branch(this);
method.go_to(exitLabel);
method.label(trueLabel);
trueBranch.branch(this);
method.label(exitLabel);
}

public void isClassVarDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch) {
Expand Down
14 changes: 14 additions & 0 deletions src/org/jruby/compiler/impl/InheritedCacheCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,20 @@ public void cacheConstant(BaseBodyCompiler method, String constantName) {
inheritedConstantCount++;
}

public void cacheConstantDefined(BaseBodyCompiler method, String constantName) {
method.loadThis();
method.loadThreadContext();
method.method.ldc(constantName);
if (inheritedConstantCount < AbstractScript.NUMBERED_CONSTANT_COUNT) {
method.method.invokevirtual(scriptCompiler.getClassname(), "getConstantDefined" + inheritedConstantCount, sig(IRubyObject.class, ThreadContext.class, String.class));
} else {
method.method.pushInt(inheritedConstantCount);
method.method.invokevirtual(scriptCompiler.getClassname(), "getConstantDefined", sig(IRubyObject.class, ThreadContext.class, String.class, int.class));
}

inheritedConstantCount++;
}

public void cacheConstantFrom(BaseBodyCompiler method, String constantName) {
// module is on top of stack
method.loadThis();
Expand Down
4 changes: 4 additions & 0 deletions src/org/jruby/javasupport/util/RuntimeHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ public static IRubyObject getConstant(ThreadContext context, String internedName

return context.getCurrentScope().getStaticScope().getConstantWithConstMissing(runtime, internedName, runtime.getObject());
}

public static IRubyObject nullToNil(IRubyObject value, ThreadContext context) {
return value != null ? value : context.nil;
}

public static IRubyObject nullToNil(IRubyObject value, Ruby runtime) {
return value != null ? value : runtime.getNil();
Expand Down

0 comments on commit 970afc1

Please sign in to comment.