What is an operand stack?

后端 未结 3 1920
醉话见心
醉话见心 2021-01-30 16:52

I am reading about JVM architecture. Today I read about the concept of the Operand Stack. According to an article:

The operand stack is used during the ex

3条回答
  •  攒了一身酷
    2021-01-30 17:04

    But could's not understand exactly that what it is and how it works in jvm?

    The JVM defines virtual computer, and the instruction set of that computer is stack based. What this means is that instructions in the JVM instruction set will typically push and pop operands from the stack. So for example,

    • a load instruction might fetch a value from a local variable, instance variable or class variable and push it onto the operand stack,
    • an arithmetical instruction will pop values from the operand stack, perform the computation and push the result back onto the stack, and
    • a store instruction will pop a value from the stack and store it ...

    @T.J.Crowder's answer gives a more concrete example in lots of detail.

    How the operand stack is implemented is platform specific, and it depends on whether code is being interpreted or whether it has been JIT compiled.

    • In the interpreted case, the operand stack is probably an array that is managed by the interpreter code. The push and pop micro-operations would be implemented something like:

          stack[top++] = value;
      

      and

          value = stack[--top];
      
    • When the code is JIT compiled, the bytecode instruction sequences are transformed into native instruction sequences that achieve the same thing as the bytecodes did. The operand stack locations get mapped to either native registers or memory locations; e.g. in the current native stack frame. The mapping involves various optimizations that are aimed at using registers (fast) in preference to memory (slower).

      Thus in the JIT compiled case, the operand stack no longer has a clear physical existence, but the overall behaviour of the compiled program is the same as if the operand stack did exist1.


    1 - Actually, it may not be exactly the same when you take the Java memory model into account. However, the memory model places clear boundary on what the differences may be. And in the case of a single threaded computation that doesn't interact with the outside (e.g. I/O, clocks, etc), there can be no observable differences.

提交回复
热议问题