Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 620
Miguel Gamboa Avatar asked Mar 03 '26 18:03

Miguel Gamboa


2 Answers

We have a double word value VV and ref value R. To get from VV... to VVR... you can add your R value to the top of the stack (RVV...) and then use dup_x2 (RVVR...) and then pop (VVR...).

like image 69
Eugene Kuleshov Avatar answered Mar 06 '26 06:03

Eugene Kuleshov


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);
    }
}
like image 38
Miguel Gamboa Avatar answered Mar 06 '26 08:03

Miguel Gamboa