Howto use invokedynamic with Jasmin?

2019-02-20 01:27发布

Here it says:

Since 2.1 :
[..]
added the invokedynamic instruction

Thus I suppose that it is possible to write instruction code containing invokedynamics with jasmin. However I could not find any documentation on the jasmin syntax and I just figured out how to use invokedynamic to get VerifyErrors with Jasmin, but not how to create a working example.

How is this instruction correctly used in Jasmin?

1条回答
Fickle 薄情
2楼-- · 2019-02-20 02:25

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
查看更多
登录 后发表回答