What is F_SAME really for in ASM? I looked for this mnemonic in Java Virtual Machine Instruction Set and didn't find anything related.
I understand stack map frames and that they save space.
But, why is this mnemonic used precisely in cases of jump targets, exception handlers or
that follow unconditional jump instructions?
Still, it's not possible to "predict" the type of stack contents?
Is using this mandatory?
I searched a lot about this but I couldn't find anything related to it almost anywhere.
Thank you!
It's not a real opcode. The comment in the source code explains it: ASM specific stack map frame types, used in {@link ClassVisitor#visitFrame}. Bear in mind that the comment is wrong, and the visitFrame method is in MethodVisitor.
I think perhaps you're wondering why the frames appear at jump targets, etc. The StackMapTableAttribute is used to speed up bytecode verification. The verifier performs flow analysis to ensure that the operand stack and local variables are consistent at all basic block entry points. Within basic blocks, the operand stack and local variables are updated incrementally, as each instruction is visited. These incremental updates are cheap, and so there's no reason to record this information redundantly.
Before Java 1.6, the verifier didn't use the StackMapTableAttribute, but this forced it to perform more passes over the code to ensure that all execution paths were visited.
A more precise answer to your question, why does F_SAME exist? Because as you say, it's smaller than encoding a full frame.
If you're using ASM to generate code, then you should use the COMPUTE_MAXS and COMPUTE_FRAMES options so that you don't need to worry about this mess. And if you want to use something even easier, use the Cojen/Maker library that I wrote.