JVM學習筆記-操做數棧(Operand Stack)

    操做數棧其實就是至關於操做系統中用來存儲接下來要被處理的數值的寄存器,只是JVM中用操做數棧來實現這類寄存器的功能數組

Like the local variables, the operand stack is organized as an array of words. But unlike the local variables, which are accessed via array indices, the operand stack is accessed by pushing and popping values. If an instruction pushes a value onto the operand stack, a later instruction can pop and use that value.this

和局部變量區同樣,操做數棧也是被組織成一個以字長爲單位的數組。可是和前者不一樣的是,它不是經過索引來訪問,而是經過標準的棧操做—壓棧和出棧—來訪問的。好比,若是某個指令把一個值壓入到操做數棧中,稍後另外一個指令就能夠彈出這個值來使用。spa

 

The virtual machine stores the same data types in the operand stack that it stores in the local variables: int, long, float, double, reference, and returnType. It converts values of type byte, short, and char to intbefore pushing them onto the operand stack.操作系統

虛擬機在操做數棧中存儲數據的類型和在局部變量區中是同樣的:如int、long、float、double、reference和returnType的存儲。對於byte、short以及char類型的值在壓入到操做數棧以前,也會被轉換爲int。code

 

Other than the program counter, which canít be directly accessed by instructions, the Java Virtual Machine has no registers. The Java Virtual Machine is stack-based rather than register-based because its instructions take their operands from the operand stack rather than from registers. Instructions can also take operands from other places, such as immediately following the opcode (the byte representing the instruction) in the bytecode stream, or from the constant pool. The Java Virtual Machine instruction set's main focus of attention, however, is the operand stack.索引

不一樣於程序計數器,Java虛擬機沒有寄存器,程序計數器也沒法被程序指令直接訪問。Java虛擬機的指令是從操做數棧中而不是從寄存器中取得操做數的,所以它的運行方式是基於棧的而不是基於寄存器的。雖然指令也能夠從其餘地方取得操做數,好比從字節碼流中跟隨在操做碼(表明指令的字節)以後的字節中或從常量池中,可是主要仍是從操做數棧中得到操做數。虛擬機

 

The Java Virtual Machine uses the operand stack as a work space. Many instructions pop values from the operand stack, operate on them, and push the result. For example, the iadd instruction adds two integers by popping two ints off the top of the operand stack, adding them, and pushing the int result. Here is how a Java Virtual Machine would add two local variables that contain ints and store the int result in a third local variable:it

虛擬機把操做數棧做爲它的工做區——大多數指令都要從這裏彈出數據,執行運算,而後把結果壓回操做數棧。好比,iadd指令就要從操做數棧中彈出兩個整數,執行加法運算,其結果又壓回到操做數棧中,看看下面的示例,它演示了虛擬機是如何把兩個int類型的局部變量相加,再把結果保存到第三個局部變量的:io

 

beginstream

iload_0 // push the int in local variable 0 onto the stack

iload_1 // push the int in local variable 1 onto the stack

iadd // pop two ints, add them, push result

istore_2 // pop int, store into local variable 2

end

 

In this sequence of bytecodes, the first two instructions, iload_0 and iload_1, push the ints stored in local variable positions zero and one onto the operand stack. The iadd instruction pops those two int values, adds them, and pushes the int result back onto the operand stack. The fourth instruction, istore_2, pops the result of the add off the top of the operand stack and stores it into local variable position two. In Figure 5-10, you can see a graphical depiction of the state of the local variables and operand stack while executing the above instructions. In this figure, unused slots of the local variables and operand stack are left blank.

在這個字節碼序列裏,前兩個指令iload_0和iload_1將存儲在局部變量中索引爲0和1的整數壓入操做數棧中,其後iadd指令從操做數棧中彈出那兩個整數相加,再將結果壓入操做數棧。第四條指令istore_2則從操做數棧中彈出結果,並把它存儲到局部變量區索引爲2的位置。圖5-10詳細表述了這個過程當中局部變量和操做數棧的狀態變化,圖中沒有使用的局部變量區和操做數棧區域以空白表示。

相關文章
相關標籤/搜索