javajvmjava-bytecode-asm

Java bytecode SWAP for double and long values?


I am developing a Java instrumentation engine with ASM and there is a situation where the evaluation stack has a certain value on the top and I want to insert a ref (which is the result of a getstatic) before the existing value. I.e. considering the stack with just one value: value ->, then after the getstatic I want the stack become like this: ref, value ->.

To perform this behavior I must insert the following bytecodes: getsatic and swap. With ASM I will do something like (consider mv of the type MethodVisitor):

mv.visitFieldInsn(Opcodes.GETSTATIC, ...);
mv.visitInsn(Opcodes.SWAP);

The problem is that the swap bytecode does not support long and double values. So the code above is fine for single word types, but it does not work correctly for long and double types.

Is there any simple solution to resolve this problem for long and double types and without requiring an auxiliary local value?


Solution

  • Here you have an utility function for ASM that injects the bytecodes for swapping the operands of any type on the top of the stack:

    public static void swap(MethodVisitor mv, Type stackTop, Type belowTop) {
        if (stackTop.getNumberSlots() == 1) {
            if (belowTop.getNumberSlots() == 1) {
                // Top = 1, below = 1
                mv.visitInsn(Opcodes.SWAP);
            } else {
                // Top = 1, below = 2
                mv.visitInsn(Opcodes.DUP_X2);
                mv.visitInsn(Opcodes.POP);
            }
        } else {
            if (belowTop.getNumberSlots() == 1) {
                // Top = 2, below = 1
                mv.visitInsn(Opcodes.DUP2_X1);
            } else {
                // Top = 2, below = 2
                mv.visitInsn(Opcodes.DUP2_X2);
            }
            mv.visitInsn(Opcodes.POP2);
        }
    }