來源:http://denverj.iteye.com/blog/1218359
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.
和局部變量區一樣,操作數棧也是被組織成一個以字長為單位的數組。但是和前者不同的是,它不是通過索引來訪問,而是通過標准的棧操作—壓棧和出棧—來訪問的。比如,如果某個指令把一個值壓入到操作數棧中,稍后另一個指令就可以彈出這個值來使用。
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。
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:
虛擬機把操作數棧作為它的工作區——大多數指令都要從這里彈出數據,執行運算,然后把結果壓回操作數棧。比如,iadd指令就要從操作數棧中彈出兩個整數,執行加法運算,其結果又壓回到操作數棧中,看看下面的示例,它演示了虛擬機是如何把兩個int類型的局部變量相加,再把結果保存到第三個局部變量的:
begin
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詳細表述了這個過程中局部變量和操作數棧的狀態變化,圖中沒有使用的局部變量區和操作數棧區域以空白表示。