3

Java has a dup2_x2 instruction which, according to the documentation, has the following behavior:

Duplicate the top one or two values on the operand stack and insert the duplicated values, in the original order, into the operand stack.

Does javac produce bytecode with this instruction? What are its potential use cases?

fxshlein
  • 83
  • 1
  • 5
  • 1
    Careful. That's from FORTH. You're getting into the weeds. `: sumn DUP 1+ * 2/ ;` – Elliott Frisch May 04 '22 at 23:16
  • 2
    You can find out how the instruction is *actually* used within the JVM etc by searching the OpenJDK codebase. Of course, other programming languages target the JVM platform, and their compilers, etc may emit this bytecode. – Stephen C May 05 '22 at 01:37

1 Answers1

4

For example, the following code

public static Long example1(Long[] array, int i, long l) {
    return array[i]=l;
}
public static long example2(long[] array, int i, long l) {
    return array[i]=l;
}

compiles to

  public static java.lang.Long example1(java.lang.Long[], int, long);
    Code:
       0: aload_0
       1: iload_1
       2: lload_2
       3: invokestatic  #1        // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
       6: dup_x2
       7: aastore
       8: areturn

  public static long example2(long[], int, long);
    Code:
       0: aload_0
       1: iload_1
       2: lload_2
       3: dup2_x2
       4: lastore
       5: lreturn

See, demo on Ideone

Holger
  • 285,553
  • 42
  • 434
  • 765