1

Almost all bytecodes know the type of their operands to be found on the stack at runtime, the bytecode verifier will have checked that only these types will actually be found at runtime (for every path) so the interpreter can just go ahead poping values that it expects and all should be well.

There are however a few instructions that need to know the type of the operands e.g. some of the 'dup' bytecodes which have different forms if some of the operands are category 2 (longs & doubles). During verification this is easy as previous push instructions use pseudo verification types where a long is pushed as a type 'long' and a type 'top' so verifying the dup knows that there is a long (as it also finds a 'top').

But how does the runtime determine this?

Hi all, original question says "some of the 'dup' bytecodes", should have been more specific i.e. the more complex ones 'dup_x2', 'dup_2', 'dup2_x1', and 'dup2_x2' whwere the different forms are dependant on what is found on the stack, they will do different things if they encounter longs, or doubles.

My question is how can the runtime determine if a value is 1 entry int or a 2 valued long/double and respond accordingly. thanks

6
  • Maybe the type gets pushed to the stack too? I'm just guessing here. Commented Jan 10, 2015 at 18:49
  • What do you mean by "how does the runtime determine this"? It can't - that's why there are separate instructions for operations with ints and longs. Commented Jan 10, 2015 at 18:50
  • DUP is not used with long or double types (DUP2 is). Commented Jan 10, 2015 at 18:51
  • Runtime doesn't need to know. Commented Jan 10, 2015 at 19:28
  • 1
    Oh dear I'm being a bit stupid, the descriptions only appear to make the instructions do different things when they find category 2 entries, just looking at how the stack slots are moved they always do the same and all the cat 2 (2 slot) entries are always preserved intact Commented Jan 11, 2015 at 12:32

1 Answer 1

1

The dup instructions don't care about type. They just blindly duplicate x number of stack slots and place them appropriately. dup2 will duplicate 2 ints (or a float and a reference or whatever) just as well as a single long.

The one caveat is that the verifier still has to verify that single words out of a long/double are not split. But this isn't unique to the dup instructions.

Every stack or local slot at every point in the bytecode has an implicit type defined by the verifier's dataflow analysis. (In later bytecode versions, there are also type annotations (not to be confused with Java level Annotations) in the classfile to make the verifier more efficient).

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, the 1-word/2-word stack scheme is one of the half-dozen or so major screw-ups in the JVM architecture, but the verifier assures it will work, and the runtime doesn't need to know the details.
(The "type annotations" make the verifier theoretically faster, but a lot screwier, so "more efficient" is a matter of opinion.)
Thanks the descriptions only make it appear that different things happen, as you say the instructions just blindly shuffle slots and the verifier will have already checked that longs/doubles will not be split
The feature of later bytecode versions is StackMapTable. Calling it “type annotations” is a bit unlucky as it confuses readers with the Java 8 feature called “Type Annotations”

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.