1

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?

yankee
  • 38,872
  • 15
  • 103
  • 162

1 Answers1

5

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
apangin
  • 92,924
  • 10
  • 193
  • 247
  • Did you test the code? Did it work for you? Because sable/jasmin seems to end up in an infinite loop on my machine and eventually fails with an OutOfMemoryError... *debugging*... – yankee Jun 11 '14 at 12:14
  • 2
    After debugging, I figured that I need to append a linefeed at the end of the file and indeed, that fixed the OutOfMemoryError... *facepalm*. Anyhow, thanks for your input :-). – yankee Jun 11 '14 at 12:43