-1

I'm currently working on a Java decompiler.

I read the JVM specification, and I know pop2 can operate one stack value (category 2 values) or two stack values (other category values).

I just want to know when a two stack values operation of pop2 can happen? Could anyone show me some Java source code whose compilation result contains two stack values operations pop2?

Jiann Lan
  • 3
  • 2
  • 1
    pop2 (or any instruction in the JVM) only operates on **one** stack. Perhaps you mean "stack values" (the terminology in the JVM stack). The way your question is currently written, it doesn't make a lot of sense. In any case, pop2 pops 2 category 1 values (each category 1 value is 32 bits, so it pops 64 bits from the stack). Or it pops 1 category 2 value (each category 2 value is 64 bits, so it again pops 64 bits from the stack). pop2 just pops 64 bits or 8 bytes from the stack. – Erwin Bolwidt Apr 25 '18 at 03:40
  • @ErwinBolwidt Sorry, I mean "stack values". I have corrected my question. – Jiann Lan Apr 25 '18 at 06:49
  • @ErwinBolwidt I think Stephen C gives me a reasonable answer. – Jiann Lan Apr 25 '18 at 07:14

2 Answers2

2

POP2 will definitely be called if you pop a double or long, such as

thisMethodReturnsALong(); thisMethodReturnsADouble();

among other cases.

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • Thank you for your answer, but I have mentioned that I know the cases that `pop2` can pop ONE category 2 stack value (I have corrected my question: I should say "stack values" rather than "stacks"). I hope to see Java code examples that `pop2` pops two category 1 stack values. – Jiann Lan Apr 25 '18 at 06:58
1

Based on my brief reading of the source code for the javac compiler in Java 8, there are no places where it will emit a POP2 to pop 2 category-1 values from the stack. If two category-1 values need to be popped, then two POP bytecodes will be emitted by the compiler.

Caveats:

  • I only looked at one version of the OpenJDK javac compiler
  • there are other (non-Sun/Oracle) Java bytecode compilers
  • bytecodes can be generated or modified by other means .... including code obfuscators!

It is plausible that the two value pop behavior of POP2 was used in early Java bytecode compilers before the advent of JIT compilers. But now that we have JIT compilers, there is no point in the bytecode compilers "optimizing" two POP bytecodes to a POP2.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thank you for you answer. It is also what I guess. I don't know C++, and I didn't try to read `javac` source code. I tried to construct some java programs, and for those programs the compiler never optimize two `pop` to one `pop2`. I also found some bytecodes with `pop2` pops two category 1 values, but none of them are generated by `javac` from Java source code. – Jiann Lan Apr 25 '18 at 07:09
  • The `javac` compiler is written in Java. No excuses :-) – Stephen C Apr 25 '18 at 07:11
  • I should be more brave! However, I'm trying to learn Java by writing a Java decompiler. I'm actually using Scala :-). – Jiann Lan Apr 25 '18 at 07:19
  • 1
    @JiannLan I don’t think that “trying to learn Java by writing a Java decompiler” is a feasible strategy. Besides that, I don’t get the purpose of your question. The way you would have to handle the `pop2` instruction, is straight-forward, so why do you think it matters whether the compiler actually uses it? – Holger Apr 26 '18 at 11:08
  • @Holger “trying to learn Java by writing a Java decompiler” is one of my purpose, and it is NOT the main purpose of the whole project. I don't care if it is feasible strategy. "How the compiler actually uses it" is my personal curious -- too many personal interest things I don't think they are useful when I describe the background of my question. I ask a question and I think the answer for this question is useful for me. – Jiann Lan Apr 27 '18 at 03:53