Question

I'm currently playing with bit of Java bytecode. I have read about two opcodes: dup and dup2. If I understand it correctly, then the only difference between them is that first duplicates only one stack word, while second duplicates pair of stack words. This is good, but each of them has their own subopcodes (dup_x1, dup2_x2...).

When I look on function of for example dup_x2 and dup2_x1, they seem to do same thing: duplicating top three values in order (so from values val4, val3, val2, val1 there are val4, val3, val2, val1, val3, val2, val1). I'm confused, because there's no reason for defining two opcodes for same action.

What are these opcodes for then? Is there any official reference for their action? Please help.

Was it helpful?

Solution

Is there any official reference for their action?

Of course there is! It is the JVM specification - specifically section 6.5.

I won't copy it here. Follow the link and read it all yourself. But the basic idea is that the dup_x1 and dup_x2 variants are inserting the duplicate value/values lower down the stack.

I'm confused, because there's no reason for defining two opcodes for same action.

Look carefully (in the spec). They are not doing the same thing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top