Howto use invokedynamic with Jasmin?

前端 未结 1 2004
后悔当初
后悔当初 2021-01-16 07:25

Here it says:

Since 2.1 :
[..]
added the invokedynamic instruction

Thus I suppose that it is possible to write instruction code containing

相关标签:
1条回答
  • 2021-01-16 07:52

    Each invokedynamic bytecode should refer to a corresponding call site specifier (JVMS 6.5) which is actually a constant pool entry of CONSTANT_InvokeDynamic type (JVMS 4.4.10).

    Jasmin (http://jasmin.sourceforge.net) does not support CONSTANT_InvokeDynamic, but Sable/jasmin does. Though using invokedynamic in hand-written assembly is ungrateful job.

    Here is an example of dynamic method that returns a reference to System.out:

    .class public HelloWorld
    .super java/lang/Object
    
    .method public <init>()V
       aload_0
       invokespecial java/lang/Object/<init>()V
       return
    .end method
    
    .method public static main([Ljava/lang/String;)V
       .limit stack 2
       .limit locals 1
    
       invokedynamic "getPrintStream" ()Ljava/io/PrintStream; HelloWorld/bootstrap(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;()
       ldc "Hello, world"
       invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
    
       return
    .end method
    
    .method private static bootstrap(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
       .limit stack 6
       .limit locals 3
    
       new java/lang/invoke/ConstantCallSite
       dup
    
       aload_0
       ldc java/lang/System
       ldc "out"
       ldc java/io/PrintStream
       invokevirtual java/lang/invoke/MethodHandles$Lookup/findStaticGetter(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/invoke/MethodHandle;
    
       invokespecial java/lang/invoke/ConstantCallSite/<init>(Ljava/lang/invoke/MethodHandle;)V
    
       areturn
    .end method
    
    0 讨论(0)
提交回复
热议问题