You question “how JIT compiler handle class field members”, contains two wrong assumptions. First, there is not “the” JIT compiler that we can talk about, there are plenty of different existing JIT compilers/ optimizers and even if we restrict ourselves to the behavior of Oracle’s JVM, we can’t make a blanket statement. Second, there is no handling of a single feature, especially as simple as a field access, in isolation.
It’s not clear why you think there was an interaction between compiled and interpreted code during the execution of a getfield
instruction. Regardless of how a JVM implements object references, they are a kind of value and reading the value of field _b
just means reading a value from memory. The writing of a value to the field, i.e. the reference to a newly created object, happens inside A
’s constructor, not when the field is read. And the state of the code is completely irrelevant for that memory access.
When it comes to the invokvirtual
instruction, it’s execution may imply a change from interpreted mode to compiled code or vice versa. But, again, this has nothing to do with the preceding getfield
instruction. It wouldn’t be different, if the target object reference is the result of an aload
instruction or a preceding method invocation or a new allocation right inside the method. It only depends on the state of the code of the invoked method, that is MyObject.sayHello
or an overriding method of a subclass.
As Antimony has pointed out, when the method is being compiled/optimized, the resulting code is unlikely to model a stack on mainstream CPUs. The resulting native code won’t push a reference to the stack but just read the field value straight-forwardly and use it for the subsequent invocation. The reference might get temporarily stored into a CPU register but it is also possible that the read operation and the invocation get fused into a single instruction if the target CPU can handle such indirect addressing. It’s also possible that the code of sayHello
gets inlined and the optimizer’s overall result has no reminiscence of the original code at all.