I am writing a JVM.
I was implementing all opcodes one by one, until i faced dup2
.
The oracle instruction set https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.dup2 says
Duplicate the top one or two values on the operand stack and push the duplicated value or values back onto the operand stack in the original order
How am i supposed to choose which operation to perform? How can i know when i should duplicate only the top or i should duplicate the top two values?
CodePudding user response:
The description of the opcode says:
Form 1:
..., value2, value1 →
..., value2, value1, value2, value1
where both value1 and value2 are values of a category 1 computational type (§2.11.1).
Form 2:
..., value →
..., value, value
where value is a value of a category 2 computational type (§2.11.1).
Category 2 types are long
and double
, and category 1 are others. So in the original versions of Java Category 2 types meant 64 bit type and Category 1 meant 32 bit types. When 64 bit JVMs were introduced, that distinction no longer worked. Now an interpreter or JIT compiler needs to track the categories of values pushed and popped from the opstack to the extent that it knows whether the value on the top of stack is category 1 or 2.
Note that the JVM needs to this kind of analysis at verify time in order to check the requirements set out in 4.10.1.7. Type Checking Load and Store Instructions