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?
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...).
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With