Skip to content

Commit

Permalink
Store callsites directly
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Dec 24, 2023
1 parent 8ba4d52 commit 7991d7e
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions bosk-core/src/main/java/io/vena/bosk/bytecode/ClassBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.lang.invoke.CallSite;
import java.lang.invoke.ConstantCallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -187,7 +186,18 @@ public LocalVariable popToLocal(Type type) {
* @param type
*/
public void pushObject(String name, Object object, Class<?> type) {
CurriedValue field = curry(name, object, type);
type.cast(object);

String fullName = "CallSite_" + CALL_SITE_COUNT.incrementAndGet() + "_" + name;
CurriedValue result = new CurriedValue(
fullName,
Type.getDescriptor(type),
object);
curriedValues.add(result);

CALL_SITES_BY_NAME.put(fullName, new ConstantCallSite(MethodHandles.constant(type, object)));
LOGGER.warn("{} = {} {}", fullName, type.getSimpleName(), object);

beginPush();
/*
// Dynamic constants aren't supported in Java 8
Expand All @@ -198,33 +208,12 @@ public void pushObject(String name, Object object, Class<?> type) {
));
*/
methodVisitor().visitInvokeDynamicInsn(
field.name(),
"()" + field.typeDescriptor(),
result.name(),
"()" + result.typeDescriptor(),
CONSTANT_CALL_SITE
);
}

private CurriedValue curry(String name, Object object, Class<?> type) {
type.cast(object);
for (CurriedValue candidate: curriedValues) {
if (candidate.value() == object) {
return candidate;
}
}

String fullName = "CURRIED_" + METHOD_HANDLE_COUNT.incrementAndGet() + "_" + name;
CurriedValue result = new CurriedValue(
fullName,
Type.getDescriptor(type),
object);
curriedValues.add(result);

METHOD_HANDLES_BY_NAME.put(fullName, MethodHandles.constant(type, object));
LOGGER.warn("curry({}) = {} {}", fullName, type.getSimpleName(), object);

return result;
}

/**
* Emit LDC: <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.ldc">...</a>
*/
Expand Down Expand Up @@ -400,12 +389,12 @@ public Class<?> loadThemBytes(String dottyName, byte[] b) {
* These need to be static because they need to be accessible from static initializers
* without having any object to start from.
*/
private static final AtomicLong METHOD_HANDLE_COUNT = new AtomicLong(0);
private static final Map<String, MethodHandle> METHOD_HANDLES_BY_NAME = new ConcurrentHashMap<>();
private static final AtomicLong CALL_SITE_COUNT = new AtomicLong(0);
private static final Map<String, CallSite> CALL_SITES_BY_NAME = new ConcurrentHashMap<>();

public static CallSite constantCallSite(MethodHandles.Lookup lookup, String name, MethodType methodType) {
LOGGER.warn("constantCallSite({})", name);
return new ConstantCallSite(METHOD_HANDLES_BY_NAME.remove(name).asType(methodType));
return CALL_SITES_BY_NAME.remove(name);
}

private static final Method CONSTANT_CALL_SITE_METHOD;
Expand Down

0 comments on commit 7991d7e

Please sign in to comment.