⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap10.html

📁 Inside the java virtualMachine,深入研究java虚拟机
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<P>Three opcodes push constants from the constant pool. These opcodes take operands that specify a constant pool index. The Java Virtual Machine looks up the constant pool entry given the index, determines the constant's type and value, and pushes it onto the stack.</P>
<P>The constant pool index is an unsigned value that immediately follows the opcode in the bytecode stream. Opcodes <CODE>ldc</CODE> and <CODE>ldc_w</CODE> push a single-word item onto the stack, either an <FONT FACE="Courier New">int</FONT>, <FONT FACE="Courier New">float</FONT>, or an object reference to a <FONT FACE="Courier New">String</FONT>. The difference between <CODE>ldc</CODE> and <CODE>ldc_w</CODE> is that <CODE>ldc</CODE> can only refer to constant pool locations one through 255 because its index is just 1 byte. (Constant pool location zero is unused.) <CODE>ldc_w</CODE> has a 2-byte index, so it can refer to any constant pool location. <CODE>lcd2_w</CODE> also has a 2-byte index, and it is used to refer to any constant pool location containing a <FONT FACE="Courier New">long</FONT> or <FONT FACE="Courier New">double</FONT>, which occupy two words. The opcodes that push constants from the constant pool are shown in Table 10-5. </P>
<P>Table 10-5. Pushing constant pool entries onto the stack</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Opcode</STRONG></TD><TD VALIGN="TOP"><STRONG>Operand(s)</STRONG></TD><TD VALIGN="TOP"><STRONG>Description</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ldc</FONT></TD><TD VALIGN="TOP">indexbyte1</TD><TD VALIGN="TOP">pushes single-word value from constant pool entry specified by indexbyte1 onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ldc_w</FONT></TD><TD VALIGN="TOP">indexbyte1, indexbyte2</TD><TD VALIGN="TOP">pushes single-word value from constant pool entry specified by indexbyte1, indexbyte2 onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ldc2_w</FONT></TD><TD VALIGN="TOP">indexbyte1, indexbyte2</TD><TD VALIGN="TOP">pushes dual-word value from constant pool entry specified by indexbyte1, indexbyte2 onto the stack</TD></TR>
</TABLE>
<P>All string literals from Java source code end up as entries in a constant pool. If multiple classes of the same application use the same string literal, that string literal will appear in the class file of every class that uses it. For example, if three classes use the string literal <FONT FACE="Courier New">"Harumph!"</FONT>, that string will appear in the constant pool of each of three class files. Methods of those classes can use the <FONT FACE="Courier New">ldc</FONT> or <FONT FACE="Courier New">ldc_w</FONT> instructions to push onto the operand stack a reference to a <FONT FACE="Courier New">String</FONT> object that has the value <FONT FACE="Courier New">"Harumph!"</FONT>.</P>
<P>As mentioned in Chapter 8, &quot;The Linking Model,&quot; the Java Virtual Machine resolves all string literals that have the same sequence of characters into the same <FONT FACE="Courier New">String</FONT> object. In other words, if multiple classes use the same literal string, say <FONT FACE="Courier New">"Harumph!"</FONT>, the Java Virtual Machine will only create one <FONT FACE="Courier New">String</FONT> object with the value <FONT FACE="Courier New">"Harumph!"</FONT> to represent all of those string literals.</P>
<P>When the virtual machine resolves the constant pool entry for a literal string, it &quot;interns&quot; the string. First, it checks to see if the string韘 sequence of characters have already been interned. If so, it just uses the same reference as the already-interned string. Otherwise, it creates a new <FONT FACE="Courier New">String</FONT> object, adds a reference to the new <FONT FACE="Courier New">String</FONT> object to its set of interned strings, and uses the reference to the newly-interned string.</P>
<H3><EM><P>Generic Stack Operations</P>
</EM></H3><P>Although most instructions in the Java Virtual Machine韘 instruction set operate on a particular type, some instructions manipulate the stack independent of type. As mentioned in Chapter 5, &quot;The Java Virtual Machine,&quot; these generic (typeless) instructions cannot be used to break up dual-word values. These instructions are shown in Table 10-6.</P>
<P>Table 10-6. Stack manipulation</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Opcode</STRONG></TD><TD VALIGN="TOP"><STRONG>Operand(s)</STRONG></TD><TD VALIGN="TOP"><STRONG>Description</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">nop</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">do nothing</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">pop</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pop the top word from the operand stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">pop2</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pop the top two words from the operand stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">swap</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">swap the top operand stack two words </TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dup</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">duplicate top operand stack word </TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dup2</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">duplicate top two operand stack words</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dup_x1</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">duplicate top operand stack word and put two down</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dup_x2</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">duplicate top operand stack word and put three down</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dup2_x1</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">duplicate top two operand stack words and put three down</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dup2_x2</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">duplicate top two operand stack words and put four down</TD></TR>
</TABLE>
<P>The last four instructions shown in Table 10-6 can be a bit difficult to understand. Consult the description of these instructions in Appendix A for a picture of the stack before and after these instructions have been executed.</P>
<I><STRONG><P>Pushing Local Variables Onto the Stack</P>
</I><P></STRONG>Several opcodes exist that push <FONT FACE="Courier New">int</FONT> and <FONT FACE="Courier New">float</FONT> local variables onto the operand stack. Some opcodes are defined that implicitly refer to a commonly used local variable position. For example, <CODE>iload_0</CODE> loads the <FONT FACE="Courier New">int</FONT> local variable at position zero. Other local variables are pushed onto the stack by an opcode that takes the local variable index from the first byte following the opcode. The <CODE>iload</CODE> instruction is an example of this type of opcode. The first byte following <CODE>iload</CODE> is interpreted as an unsigned 8-bit index that refers to a local variable. </P>
<P>The opcodes that push <FONT FACE="Courier New">int</FONT> and <FONT FACE="Courier New">float</FONT> local variables onto the stack are shown in Table 10-7. </P>
<P>Table 10-7. Pushing single-word local variables onto the stack</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Opcode</STRONG></TD><TD VALIGN="TOP"><STRONG>Operand(s)</STRONG></TD><TD VALIGN="TOP"><STRONG>Description</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iload</FONT></TD><TD VALIGN="TOP">vindex</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> from local variable position vindex</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iload</FONT>_0</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> from local variable position zero</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iload</FONT>_1</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> from local variable position one</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iload</FONT>_2</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> from local variable position two</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iload</FONT>_3</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> from local variable position three</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">fload</FONT></TD><TD VALIGN="TOP">vindex</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">float</FONT> from local variable position vindex</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">fload</FONT>_0</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">float</FONT> from local variable position zero</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">fload</FONT>_1</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">float</FONT> from local variable position one</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">fload</FONT>_2</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">float</FONT> from local variable position two</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">fload</FONT>_3</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">float</FONT> from local variable position three</TD></TR>
</TABLE>
<P>Table 10-8 shows the instructions that push local variables of type <FONT FACE="Courier New">long</FONT> and <FONT FACE="Courier New">double</FONT> onto the stack. These instructions move two words from the local variable section of the stack frame to the operand stack section. </P>
<P>Table 10-8. Pushing dual-word local variables onto the stack</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Opcode</STRONG></TD><TD VALIGN="TOP"><STRONG>Operand(s)</STRONG></TD><TD VALIGN="TOP"><STRONG>Description</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lload</FONT></TD><TD VALIGN="TOP">vindex</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">long</FONT> from local variable positions vindex and (vindex + 1)</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lload</FONT>_0</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">long</FONT> from local variable positions zero and one</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lload</FONT>_1</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">long</FONT> from local variable positions one and two</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lload</FONT>_2</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">long</FONT> from local variable positions two and three</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lload</FONT>_3</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">long</FONT> from local variable positions three and four</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dload</FONT></TD><TD VALIGN="TOP">vindex</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">double</FONT> from local variable positions vindex and (vindex + 1)</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dload</FONT>_0</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">double</FONT> from local variable positions zero and one</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dload</FONT>_1</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">double</FONT> from local variable positions one and two</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dload</FONT>_2</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">double</FONT> from local variable positions two and three</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dload</FONT>_3</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">double</FONT> from local variable positions three and four</TD></TR>
</TABLE>
<P>The final group of opcodes that push local variables move object references (which occupy one word) from the local variables section of the stack frame to the operand section. These opcodes are shown in Table 10-9.</P>
<P>Table 10-9. Pushing object reference local variables onto the stack</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Opcode</STRONG></TD><TD VALIGN="TOP"><STRONG>Operand(s)</STRONG></TD><TD VALIGN="TOP"><STRONG>Description</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">aload</FONT></TD><TD VALIGN="TOP">vindex</TD><TD VALIGN="TOP">pushes object reference from local variable position vindex</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">aload</FONT>_0</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes object reference from local variable position zero</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">aload</FONT>_1</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes object reference from local variable position one</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">aload</FONT>_2</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes object reference from local variable position two</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">aload</FONT>_3</TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes object reference from local variable position three</TD></TR>
</TABLE>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -