Skip to content

Commit

Permalink
IR2JVM: Implement standard const lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Apr 26, 2012
1 parent 9349a07 commit 3c481f4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/org/jruby/ir/instructions/SearchConstInstr.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jruby.ir.Operation;
import org.jruby.ir.operands.Operand;
import org.jruby.ir.operands.Variable;
import org.jruby.ir.targets.JVM;
import org.jruby.ir.transformations.inlining.InlinerInfo;

import org.jruby.Ruby;
Expand Down Expand Up @@ -106,4 +107,9 @@ public Object interpret(ThreadContext context, DynamicScope currDynScope, IRubyO
return constant;
}

public void compile(JVM jvm) {
jvm.method().searchConst(constName);
jvm.method().storeLocal(jvm.methodData().local(getResult()));
}

}
45 changes: 45 additions & 0 deletions src/org/jruby/ir/targets/Bootstrap.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jruby.ir.targets;

import com.headius.invokebinder.Binder;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.RubyEncoding;
Expand Down Expand Up @@ -119,6 +120,21 @@ public static CallSite ivar(Lookup lookup, String name, MethodType type) {
return new ConstantCallSite(handle);
}

public static CallSite searchConst(Lookup lookup, String name, MethodType type) {
MutableCallSite site = new MutableCallSite(type);
String[] bits = name.split(":");
String constName = bits[1];

MethodHandle handle = Binder
.from(type)
.insert(0, site, constName)
.invokeStaticQuiet(MethodHandles.lookup(), Bootstrap.class, "searchConst");

site.setTarget(handle);

return site;
}

public static Handle string() {
return new Handle(Opcodes.H_INVOKESTATIC, p(Bootstrap.class), "string", sig(CallSite.class, Lookup.class, String.class, MethodType.class, String.class, int.class));
}
Expand All @@ -143,6 +159,10 @@ public static Handle ivar() {
return new Handle(Opcodes.H_INVOKESTATIC, p(Bootstrap.class), "ivar", sig(CallSite.class, Lookup.class, String.class, MethodType.class));
}

public static Handle searchConst() {
return new Handle(Opcodes.H_INVOKESTATIC, p(Bootstrap.class), "searchConst", sig(CallSite.class, Lookup.class, String.class, MethodType.class));
}

public static IRubyObject string(String value, int encoding, ThreadContext context) {
// obviously wrong: not caching bytelist, not using encoding
return RubyString.newStringNoCopy(context.runtime, value.getBytes(RubyEncoding.ISO));
Expand Down Expand Up @@ -446,6 +466,31 @@ public static boolean testType(RubyClass original, ThreadContext context, IRubyO
return self.getMetaClass() == original;
}

///////////////////////////////////////////////////////////////////////////
// constant lookup

public static IRubyObject searchConst(MutableCallSite site, String constName, ThreadContext context, StaticScope staticScope) throws Throwable {
Ruby runtime = context.runtime;
SwitchPoint switchPoint = (SwitchPoint)runtime.getConstantInvalidator().getData();
IRubyObject value = staticScope.getConstant(runtime, constName, runtime.getObject());

if (value == null) {
return staticScope.getModule().callMethod(context, "const_missing", runtime.fastNewSymbol(constName));
}

// bind constant until invalidated
MethodHandle target = Binder.from(site.type())
.drop(0, 2)
.constant(value);
MethodHandle fallback = Binder.from(site.type())
.insert(0, site, constName)
.invokeStatic(MethodHandles.lookup(), Bootstrap.class, "searchConst");

site.setTarget(switchPoint.guardWithTest(target, fallback));

return value;
}

///////////////////////////////////////////////////////////////////////////
// COMPLETED WORK BELOW

Expand Down
7 changes: 7 additions & 0 deletions src/org/jruby/ir/targets/IRBytecodeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jruby.RubySymbol;
import org.jruby.compiler.impl.SkinnyMethodAdapter;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
Expand Down Expand Up @@ -112,6 +113,12 @@ public void invokeHelper(String name, String sig) {
adapter.invokestatic(p(RuntimeHelpers.class), name, sig);
}

public void searchConst(String name) {
loadLocal(0);
loadLocal(1);
adapter.invokedynamic("searchConst:" + name, sig(JVM.OBJECT, params(ThreadContext.class, StaticScope.class)), Bootstrap.searchConst());
}

public void goTo(org.objectweb.asm.Label label) {
adapter.go_to(label);
}
Expand Down

0 comments on commit 3c481f4

Please sign in to comment.