How to convert a DynamicConstantDesc to a MethodHandle?

50 views
Skip to first unread message

Steven Stewart-Gallus

unread,
Apr 30, 2020, 9:57:19 PM4/30/20
to mechanical-sympathy
Hello,

I've stumbled upon a puzzler while writing a compiler from System F embedded in Java to methodhandles.

Given a DynamicConstantDesc how do I convert it to a MethodHandle that lazily loads the value alike ldc ?
It seems absurd spin an entire class for this.
I could create a MutableCallSite or perhaps a SwitchPoint for this but I don't really understand which would be better.

A regularly old object that does lazy initialisation with a flag would probably be fine but I feel like I should be able to do better.

I think I might be able to abuse the jit's handling of private finals in lambda objects to do this but I'm not quite certain that would work.

Thanks
Steven Stewart-Gallus

Steven Stewart-Gallus

unread,
Apr 30, 2020, 10:03:03 PM4/30/20
to mechanical-sympathy
I think basically I want the following API, but ideally thread safe and optimizable like a real LDC.

public final class LazyConstant {
   
private ConstantDesc desc;
   
private Object result;


   
public LazyConstant(ConstantDesc desc) {
       
this.desc = desc;
   
}


   
public Object resolve(MethodHandles.Lookup lookup) throws ReflectiveOperationException {
       
if (null == result) {
            result
= desc.resolveConstantDesc(lookup);
       
}
       
return result;
   
}
}
Reply all
Reply to author
Forward
0 new messages