Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add some examples of dirt-simple literal loading and method calls.

  • Loading branch information...
commit f35beadabe5858c2ae1d296acfa7f5620293c5b1 1 parent 175fd7e
@headius headius authored
View
20 src/org/jruby/ir/targets/dalvik/DalvikBytecodeAdapter.java
@@ -31,13 +31,12 @@ public DalvikBytecodeAdapter(DexMethodAdapter adapter) {
public void push(Long l) {
loadLocal(0);
- DalvikCallHelper.invoke(TypeId.get(IRubyObject.class), "fixnum", new TypeId[]{TypeId.get(ThreadContext.class)}, Bootstrap.fixnum(), l);
+ DalvikCallHelper.fixnum(adapter, l);
}
public void push(ByteList bl) {
loadLocal(0);
- DalvikCallHelper.invoke(TypeId.get(IRubyObject.class), "string", new TypeId[]{TypeId.get(ThreadContext.class)},
- Bootstrap.string(), new String(bl.bytes(), RubyEncoding.ISO), bl.getEncoding().getIndex());
+ DalvikCallHelper.string(adapter, bl);
}
/**
@@ -68,13 +67,13 @@ public void storeLocal(int i) {
public void invokeOther(String name, int arity) {
TypeId[] types = params(TypeId.get(ThreadContext.class), TypeId.get(IRubyObject.class), TypeId.get(IRubyObject.class), arity);
- DalvikCallHelper.invoke(TypeId.get(IRubyObject.class), "invoke:" + JavaNameMangler.mangleMethodName(name),
+ DalvikCallHelper.invokeOther(TypeId.get(IRubyObject.class), "invoke:" + JavaNameMangler.mangleMethodName(name),
types, Bootstrap.invoke());
}
public void invokeSelf(String name, int arity) {
- TypeId[] types = params(TypeId.get(ThreadContext.class), TypeId.get(IRubyObject.class), TypeId.get(IRubyObject.class), arity);
- DalvikCallHelper.invoke(TypeId.get(IRubyObject.class), "invokeSelf:" + JavaNameMangler.mangleMethodName(name), types, Bootstrap.invokeSelf());
+ TypeId[] types = params(TypeId.get(ThreadContext.class), TypeId.get(IRubyObject.class), TypeId.get(String.class), TypeId.get(IRubyObject.class), arity);
+ DalvikCallHelper.invokeSelf(adapter, TypeId.get(IRubyObject.class), JavaNameMangler.mangleMethodName(name), types);
}
public void invokeSuper(String name, int arity) {
@@ -199,6 +198,15 @@ public Label newLabel() {
types[1] = typ2;
return types;
}
+
+ public static TypeId[] params(TypeId typ1, TypeId typ2, TypeId typ3, TypeId typFill, int times) {
+ TypeId[] types = new TypeId[times + 3];
+ Arrays.fill(types, typFill);
+ types[0] = typ1;
+ types[1] = typ2;
+ types[2] = typ3;
+ return types;
+ }
public DexMethodAdapter adapter;
private int variableCount = 0;
View
58 src/org/jruby/ir/targets/dalvik/DalvikCallHelper.java
@@ -1,6 +1,13 @@
package org.jruby.ir.targets.dalvik;
import com.google.dexmaker.TypeId;
+import org.jcodings.Encoding;
+import org.jcodings.EncodingDB;
+import org.jruby.RubyFixnum;
+import org.jruby.javasupport.util.RuntimeHelpers;
+import org.jruby.runtime.ThreadContext;
+import org.jruby.runtime.builtin.IRubyObject;
+import org.jruby.util.ByteList;
import org.objectweb.asm.Handle;
/**
@@ -8,8 +15,55 @@
* @author lynnewallace
*/
public class DalvikCallHelper {
+ // TODO: These are all using slow, uncached paths to retrieve literals
+ // and call methods; they will need improvement to cache properly.
- public static void invoke(TypeId returnval, String name, TypeId[] params, Handle handle, Object... obj) {
-
+ public static void invokeSelf(DexMethodAdapter adapter, TypeId returnval, String name, TypeId[] params) {
+ adapter.ldc(name);
+
+ adapter.invokestatic(TypeId.get(RuntimeHelpers.class), returnval, "invoke", params);
+ }
+
+ // compile-time
+ public static void fixnum(DexMethodAdapter adapter, long value) {
+ adapter.ldc(value);
+
+ adapter.invokestatic(TypeId.get(DalvikCallHelper.class), TypeId.get(IRubyObject.class), "fixnum", TypeId.get(ThreadContext.class), TypeId.get(long.class));
+
+ }
+
+ // run-time
+ public static IRubyObject fixnum(ThreadContext context, long value) {
+ return context.runtime.newFixnum(value);
+ }
+
+ // compile-time
+ public static void string(DexMethodAdapter adapter, ByteList value) {
+ String asString = RuntimeHelpers.rawBytesToString(value.bytes());
+ String encName = new String(value.getEncoding().getName());
+
+ adapter.invokestatic(TypeId.get(DalvikCallHelper.class), TypeId.get(IRubyObject.class), "string", TypeId.get(ThreadContext.class), TypeId.get(String.class), TypeId.get(String.class));
+
+ }
+
+ // run-time
+ public static IRubyObject string(ThreadContext context, String value, String encoding) {
+ byte[] rawBytes = RuntimeHelpers.stringToRawBytes(value);
+ ByteList bl = new ByteList(rawBytes, Encoding.load(encoding));
+
+ return context.runtime.newString(bl);
+ }
+
+ // compile-time
+ public static void symbol(DexMethodAdapter adapter, String value) {
+ adapter.ldc(value);
+
+ adapter.invokestatic(TypeId.get(DalvikCallHelper.class), TypeId.get(IRubyObject.class), "symbol", TypeId.get(ThreadContext.class), TypeId.get(String.class));
+
+ }
+
+ // run-time
+ public static IRubyObject symbol(ThreadContext context, String value) {
+ return context.runtime.newSymbol(value);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.