Search code examples
jvmjvm-bytecode

Where is JVM PC stored during a call?


I am currently reading the last specification of the JVM. It is clear that each thread has its own call stack and its own program counter that keeps track of the (next) instruction to execute. My question is maybe dump, but from the description, I cannot find an answer.

Where is the current program counter stored when a new or a method is invoked? In other terms, how does the thread now where to continue after the invokation of a method?


Solution

  • The answer is implementation-dependent as different hardware architectures and even different JVMs may implement this behavior in different ways. In the standard Oracle JVM, most of your bytecode will be compiled to native code by JIT (Just in Time compiler) and method calls will be executed as for native code (give or take some extra code which may be added to handle checkpointing etc.). On a PC this means that current register values, including the instruction pointer / program counter will be saved on the stack before a method call. When returning from the call, the processor pops these values from the stack, among them the return address.