Skip to content

Commit

Permalink
Interpreter erase int types + added TestLiftCustomBytecode::testErase…
Browse files Browse the repository at this point in the history
…Ints
  • Loading branch information
asotona committed Oct 9, 2024
1 parent 4950a8e commit 104ef7d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import sun.invoke.util.Wrapper;

import static java.util.stream.Collectors.toMap;

Expand Down Expand Up @@ -419,9 +420,9 @@ static Object exec(MethodHandles.Lookup l, OpContext oc, Op o) {
case SUPER -> l.in(target.parameterType(0));
};
MethodHandle mh = resolveToMethodHandle(il, co.invokeDescriptor(), co.invokeKind());

mh = mh.asType(target).asFixedArity();
Object[] values = o.operands().stream().map(oc::getValue).toArray();
target = eraseInts(mh.type(), target, values);
mh = mh.asType(target).asFixedArity();
return invoke(mh, values);
} else if (o instanceof CoreOp.NewOp no) {
Object[] values = o.operands().stream().map(oc::getValue).toArray();
Expand Down Expand Up @@ -589,6 +590,30 @@ static Object exec(MethodHandles.Lookup l, OpContext oc, Op o) {
}
}

// method type with converted int types and int values wrapped as expected by the handleType
static MethodType eraseInts(MethodType handleType, MethodType target, Object[] values) {
for (int i = 0; i < Math.min(values.length, handleType.parameterCount()); i++) {
Object v = values[i];
Class<?> ht = handleType.parameterType(i);
Class<?> vt;
Wrapper w;
if (v != null && ht != (vt = v.getClass()) && (w = intTypeWrapper(ht)) != null && w.wrapperType() != vt) {
values[i] = w.wrap(v);
target = target.changeParameterType(i, ht);
}
}
return target;
}

static Wrapper intTypeWrapper(Class<?> type) {
if (type == int.class) return Wrapper.INT;
if (type == boolean.class) return Wrapper.BOOLEAN;
if (type == byte.class) return Wrapper.BYTE;
if (type == char.class) return Wrapper.CHAR;
if (type == short.class) return Wrapper.SHORT;
return null;
}

static final MethodHandle INVOKE_LAMBDA_MH;
static {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.lang.classfile.ClassFile;
import java.lang.classfile.Label;
import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs;
import java.lang.constant.DynamicCallSiteDesc;
import java.lang.constant.DynamicConstantDesc;
import java.lang.constant.MethodTypeDesc;
Expand All @@ -36,7 +35,8 @@
import java.lang.reflect.code.op.CoreOp;
import java.lang.reflect.code.bytecode.BytecodeLift;
import java.lang.reflect.code.interpreter.Interpreter;
import java.lang.runtime.CodeReflection;

import static java.lang.constant.ConstantDescs.*;

/*
* @test
Expand All @@ -49,7 +49,7 @@ public class TestLiftCustomBytecode {
@Test
public void testBackJumps() throws Throwable {
CoreOp.FuncOp f = getFuncOp(ClassFile.of().build(ClassDesc.of("BackJumps"), clb ->
clb.withMethodBody("backJumps", MethodTypeDesc.of(ConstantDescs.CD_int, ConstantDescs.CD_int), ClassFile.ACC_STATIC, cob -> {
clb.withMethodBody("backJumps", MethodTypeDesc.of(CD_int, CD_int), ClassFile.ACC_STATIC, cob -> {
Label l1 = cob.newLabel();
Label l2 = cob.newLabel();
Label l3 = cob.newLabel();
Expand All @@ -70,10 +70,23 @@ public void testBackJumps() throws Throwable {
Assert.assertEquals((int) Interpreter.invoke(f, 42), 42);
}

@Test
public void testEraseInts() throws Throwable {
CoreOp.FuncOp f = getFuncOp(ClassFile.of().build(ClassDesc.of("EraseInts"), clb ->
clb.withMethodBody("compare", MethodTypeDesc.of(CD_short, CD_boolean, CD_char), ClassFile.ACC_STATIC, cob -> {
cob.iload(0)
.iload(1)
.invokestatic(Short.class.describeConstable().get(), "compare", MethodTypeDesc.of(CD_int, CD_short, CD_short))
.ireturn();
})), "compare");

Assert.assertEquals((int) Interpreter.invoke(f, true, 1), '\0');
}

@Test
public void testDeepStackJump() throws Throwable {
CoreOp.FuncOp f = getFuncOp(ClassFile.of().build(ClassDesc.of("DeepStackJump"), clb ->
clb.withMethodBody("deepStackJump", MethodTypeDesc.of(ConstantDescs.CD_long), ClassFile.ACC_STATIC, cob -> {
clb.withMethodBody("deepStackJump", MethodTypeDesc.of(CD_long), ClassFile.ACC_STATIC, cob -> {
Label l = cob.newLabel();
cob.lconst_1().iconst_1().iconst_2()
.goto_(l)
Expand Down Expand Up @@ -110,11 +123,11 @@ public void testObjectMethodsIndy() throws Throwable {
@Test
public void testConstantBootstrapsCondy() throws Throwable {
byte[] testCondy = ClassFile.of().build(ClassDesc.of("TestCondy"), clb ->
clb.withMethodBody("condyMethod", MethodTypeDesc.of(ConstantDescs.CD_Class), ClassFile.ACC_STATIC, cob ->
clb.withMethodBody("condyMethod", MethodTypeDesc.of(CD_Class), ClassFile.ACC_STATIC, cob ->
cob.ldc(DynamicConstantDesc.ofNamed(
ConstantDescs.ofConstantBootstrap(ConstantDescs.CD_ConstantBootstraps, "primitiveClass", ConstantDescs.CD_Class),
ofConstantBootstrap(CD_ConstantBootstraps, "primitiveClass", CD_Class),
int.class.descriptorString(),
ConstantDescs.CD_Class))
CD_Class))
.areturn()));

CoreOp.FuncOp primitiveInteger = getFuncOp(testCondy, "condyMethod");
Expand All @@ -126,11 +139,11 @@ public void testConstantBootstrapsCondy() throws Throwable {
@Test
public void testStringMakeConcat() throws Throwable {
byte[] testStringMakeConcat = ClassFile.of().build(ClassDesc.of("TestStringMakeConcat"), clb ->
clb.withMethodBody("concatMethod", MethodTypeDesc.of(ConstantDescs.CD_String), ClassFile.ACC_STATIC, cob ->
clb.withMethodBody("concatMethod", MethodTypeDesc.of(CD_String), ClassFile.ACC_STATIC, cob ->
cob.ldc("A").ldc("B").ldc("C")
.invokedynamic(DynamicCallSiteDesc.of(
ConstantDescs.ofCallsiteBootstrap(StringConcatFactory.class.describeConstable().get(), "makeConcat", ConstantDescs.CD_CallSite),
MethodTypeDesc.of(ConstantDescs.CD_String, ConstantDescs.CD_String, ConstantDescs.CD_String, ConstantDescs.CD_String)))
ofCallsiteBootstrap(StringConcatFactory.class.describeConstable().get(), "makeConcat", CD_CallSite),
MethodTypeDesc.of(CD_String, CD_String, CD_String, CD_String)))
.areturn()));

CoreOp.FuncOp concatMethod = getFuncOp(testStringMakeConcat, "concatMethod");
Expand Down

0 comments on commit 104ef7d

Please sign in to comment.