Java中的哪段代码将生成JVM指令DUP2、DUP2_X1、SWAP?

c9qzyr3d  于 2022-11-29  发布在  Java
关注(0)|答案(2)|浏览(153)

我正在构建一个类,它收集了一些可能的JVM操作码。我发现了如何生成DUP2_X2和DUP_X2,而不是DUP2、DUP2_X1、SWAP。
下面是我开始收集一些jvm操作码的代码示例:

public class JvmOpCodes {

    long dup2x2(long[] array, int i, long l) {
        return array[i] = l;
    }

    int dupx2(int[] array, int i, int l) {
        return array[i] = l;
    }
    
    long lneg(long a) {
        return -a;
    }

    long lor(long a, long b) {
        return a | b;
    }

    long land(long a, long b) {
        return a & b;
    }

    long lushr(long a, long b) {
        return a >>> b;
    }

    int iushr(int a, int b) {
        return a >>> b;
    }

    long lshl(long a, long b) {
        return a << b;
    }

    float fsub(float a, float b) {
        return a - b;
    }

    float fadd(float a, float b) {
        return a + b;
    }

    float frem(float a, float b) {
        return a % b;
    }

    float fneg(float a) {
        return -a;
    }

    double drem(double a, double b) {
        return a % b;
    }
    
    double dneg(double a) {
        return -a;
    }
    
    void pop() {
        Math.round(0.5f);
    }
    
    void pop2() {
        Math.round(0.5d);
    }
}

在使用javac命令编译并运行javap -p -c之后,我能够识别输出中生成的JVM操作码:

Compiled from "JvmOpCodes.java"
public class org.apache.bcel.verifier.tests.JvmOpCodes {
  public org.apache.bcel.verifier.tests.JvmOpCodes();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  long dup2x2(long[], int, long);
    Code:
       0: aload_1
       1: iload_2
       2: lload_3
       3: dup2_x2
       4: lastore
       5: lreturn

  int dupx2(int[], int, int);
    Code:
       0: aload_1
       1: iload_2
       2: iload_3
       3: dup_x2
       4: iastore
       5: ireturn

  long lneg(long);
    Code:
       0: lload_1
       1: lneg
       2: lreturn

  long lor(long, long);
    Code:
       0: lload_1
       1: lload_3
       2: lor
       3: lreturn

  long land(long, long);
    Code:
       0: lload_1
       1: lload_3
       2: land
       3: lreturn

  long lushr(long, long);
    Code:
       0: lload_1
       1: lload_3
       2: l2i
       3: lushr
       4: lreturn

  int iushr(int, int);
    Code:
       0: iload_1
       1: iload_2
       2: iushr
       3: ireturn

  long lshl(long, long);
    Code:
       0: lload_1
       1: lload_3
       2: l2i
       3: lshl
       4: lreturn

  float fsub(float, float);
    Code:
       0: fload_1
       1: fload_2
       2: fsub
       3: freturn

  float fadd(float, float);
    Code:
       0: fload_1
       1: fload_2
       2: fadd
       3: freturn

  float frem(float, float);
    Code:
       0: fload_1
       1: fload_2
       2: frem
       3: freturn

  float fneg(float);
    Code:
       0: fload_1
       1: fneg
       2: freturn

  double drem(double, double);
    Code:
       0: dload_1
       1: dload_3
       2: drem
       3: dreturn

  double dneg(double);
    Code:
       0: dload_1
       1: dneg
       2: dreturn

  void pop();
    Code:
       0: ldc           #7                  // float 0.5f
       2: invokestatic  #8                  // Method java/lang/Math.round:(F)I
       5: pop
       6: return

  void pop2();
    Code:
       0: ldc2_w        #14                 // double 0.5d
       3: invokestatic  #16                 // Method java/lang/Math.round:(D)J
       6: pop2
       7: return
}

但是,Java中的哪段代码将生成JVM指令DUP2、DUP2_X1、SWAP?
此外,这里还有一个有趣的相关答案与演示:https://stackoverflow.com/a/72131218/8315843

muk1a3rh

muk1a3rh1#

副本2:

public static long example(long a) {
      return a = a + 1;
  }

考虑到缺少SWAP1_2、SWAP2_1和SWAP2_2这样的变体,我怀疑javac是否会发出SWAP。

y53ybaqx

y53ybaqx2#

DUP2_X1的示例

long l1;
    long l2;
    
    void test() {
        l2 = l1 = 1;
    }

javap输出:

void test();
    Code:
       0: aload_0
       1: aload_0
       2: lconst_1
       3: dup2_x1
       4: putfield      #2                  // Field l1:J
       7: putfield      #3                  // Field l2:J
      10: return

ideone上的演示
https://ideone.com/8dR8W7
但是,我没有找到一个具有其他形式的例子:

..., value3, value2, value1 => 
..., value2, value1, value3, value2, value1

相关问题