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

📄 compiling.doc1.html

📁 Jvm 规范说明。The Java Virtual Machine was designed to support the Java programming language. Some concep
💻 HTML
📖 第 1 页 / 共 5 页
字号:
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	    ;		// Loop body is empty
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	}
</code><a name="24542"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>}
</code><br></pre><a name="24535"></a>
<p>
<a name="24534"></a>
It must be compiled for the Java Virtual Machine using instructions operating on 
another type, most likely <code>int</code>, converting between <code>short</code> and <code>int</code> values as necessary 
to ensure that the results of operations on <code>short</code> data stay within the appropriate 
range:
<p><a name="4277"></a>
 <i>method </i><code>void</code> <code>sspin()<p><Table Border="0">
<tr><td>          <i>   0 
</i><br><td>          <i>iconst_0
</i><br><td>

<tr><td>          <i>   1 
</i><br><td>          <i>istore_1
</i><br><td>

<tr><td>          <i>   2 
</i><br><td>          <i>goto 10
</i><br><td>

<tr><td>          <i>   5 
</i><br><td>          <i>iload_1
</i><br><td><i>// The </i><code>short</code><i> is stored in an </i><code>int
</code>
<tr><td>          <i>   6 
</i><br><td>          <i>iconst_1
</i><br><td>

<tr><td>          <i>   7
</i><br><td>          <i>iadd
</i><br><td>

<tr><td>          <i>   8 
</i><br><td>          <i>i2s
</i><br><td><i>// Truncate </i><code>int</code><i> to </i><code>short
</code>
<tr><td>          <i>   9 
</i><br><td>          <i>istore_1
</i><br><td>

<tr><td>          <i>  10 
</i><br><td>          <i>iload_1
</i><br><td>

<tr><td>          <i>  11 
</i><br><td>          <i>bipush 100
</i><br><td>

<tr><td>          <i>  13 
</i><br><td>          <i>if_icmplt 5
</i><br><td>

<tr><td>          <i>  16 
</i><br><td>          <i>return
</i><br><td>

</Table><br><br></code><p>
<a name="4589"></a>
The lack of direct support for <code>byte</code>, <code>char</code>, and <code>short</code> types in the Java Virtual Machine is 
not particularly painful, because values of those types are internally promoted to <code>int</code> 
(<code>byte</code> and <code>short</code> are sign-extended to <code>int</code>, <code>char</code> is zero-extended). Operations on <code>byte</code>, 
<code>char</code>, and <code>short</code> data can thus be done using <code>int</code> instructions. The only additional cost is 
that of truncating the values of <code>int</code> operations to valid ranges. 
<p><a name="5831"></a>
The <code>long</code> and floating-point types have an intermediate level of support in the Java Virtual Machine, lacking only the full complement of conditional control transfer instructions. <p>
<a name="4228"></a>
<h1>7.3	 Arithmetic</h1>
<a name="4307"></a>
The Java Virtual Machine generally does arithmetic on its operand stack (the exception is the <i>iinc</i> instruction, which directly increments the value of a local variable). 
For instance, the <code>align2grain</code> method aligns an <code>int</code> value to a given power of 2 grain 
size:
<p><pre><br><a name="4310"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>int align2grain(int i, int grain) {
</code></pre><pre>&nbsp;&nbsp;&nbsp;&nbsp;<code>	return ((i + grain-1) &amp; ~(grain-1));
</code><a name="4312"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>}
</code><br></pre><a name="9749"></a>
Operands for arithmetic operations are popped from the operand stack, and the results of operations are pushed back onto the operand stack. Results of arithmetic subcomputations can thus be made available as operands of their nesting computation. For instance, the calculation of <code>~(grain</code>-<code>1)</code> is handled by these instructions:<p><Table Border="0">
<tr><td>          <i>   5 
</i><br><td>          <i>iload_2
</i><br><td><i>// Load </i><code>grain</code><i> onto operand stack
</i>
<tr><td>          <i>   6 
</i><br><td>          <i>iconst_1
</i><br><td><i>// Load constant </i><code>1</code><i> onto operand stack
</i>
<tr><td>          <i>   7 
</i><br><td>          <i>isub
</i><br><td><i>// Subtract; push result onto stack
</i>
<tr><td>          <i>   8 
</i><br><td>          <i>iconst_m1
</i><br><td><i>// Load constant </i>-<code>1</code><i> onto operand stack
</i>
<tr><td>          <i>   9 
</i><br><td>          <i>ixor
</i><br><td><i>// Do XOR; push result onto stack
</i>
</Table><br><br><p>
<a name="9756"></a>
First <code>grain</code>-<code>1</code> is calculated using the contents of local variable <i>2</i> and an immediate <code>int</code> value <code>1</code>. These operands are popped from the operand stack and their difference pushed back onto the operand stack, where it is immediately available 
for use as one operand of the <i>ixor</i> instruction (recall that <code>~x</code> == -<code>1^x</code>). Similarly, 
the result of the <i>ixor</i> instruction becomes an operand for the subsequent <i>iand</i> 
instruction.
<p><a name="9759"></a>
The code for the entire method follows:<p>
<a name="6983"></a>
 <i>Method </i><code>int</code> <code>align2grain(int,int)<p><Table Border="0">
<tr><td>          <i>   0 
</i><br><td><i>iload_1
</i>
<tr><td>          <i>   1 
</i><br><td><i>iload_2
</i>
<tr><td>          <i>   2 
</i><br><td><i>iadd
</i>
<tr><td>          <i>   3 
</i><br><td><i>iconst_1
</i>
<tr><td>          <i>   4 
</i><br><td><i>isub
</i>
<tr><td>          <i>   5 
</i><br><td><i>iload_2
</i>
<tr><td>          <i>   6 
</i><br><td><i>iconst_1
</i>
<tr><td>          <i>   7 
</i><br><td><i>isub
</i>
<tr><td>          <i>   8 
</i><br><td><i>iconst_m1
</i>
<tr><td>          <i>   9 
</i><br><td><i>ixor
</i>
<tr><td>          <i>  10
</i><br><td><i>iand
</i>
<tr><td>          <i>  11
</i><br><td><i>ireturn
</i>
</Table><br><br></code><p>
<a name="5841"></a>
<h1>7.4	 Accessing the Constant Pool</h1>
<a name="6040"></a>
Many numeric constants, as well as objects, fields, and methods, are accessed via 
the constant pool of the current class. Object access is considered later <a href="Compiling.doc1.html#4089">(&#167;7.8)</a>. Java 
data of types <code>int</code>, <code>long</code>, <code>float</code>, and <code>double</code>, as well as references to instances of <code>String</code> 
(constant pool items tagged <code>CONSTANT_String</code>), is managed using the <i>ldc</i>, <i>ldc_w</i>, 
and <i>ldc2_w</i> instructions. 
<p><a name="6072"></a>
The <i>ldc</i> and <i>ldc_w</i> instructions are used to access one-word values in the constant pool (including instances of class <code>String</code>), and <i>ldc2_w</i> is used to access two-word values. The <i>ldc_w</i> instruction is used in place of <i>ldc</i> only when there is a large number of constant pool items and a larger index is needed to access an item. The <i>ldc2_w</i> instruction is used to access all two-word items; there is no non-wide variant.<p>
<a name="6073"></a>
Integral constants of types <code>byte</code>, <code>char</code>, or <code>short</code>, as well as small <code>int</code> values, may be compiled using the <i>bipush</i>, <i>sipush</i>, or <i>iconst_&lt;i&gt;</i> instructions, as seen earlier <a href="Compiling.doc1.html#4182">(&#167;7.2)</a>. Certain small floating-point constants may be compiled using the <i>fconst_&lt;f&gt;</i> and <i>dconst_&lt;d&gt;</i> instructions. <p>
<a name="24477"></a>
In all of these cases compilation is straightforward. For instance, the constants for<p>
<pre><br><a name="24478"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>void useManyNumeric() {
</code></pre><pre>&nbsp;&nbsp;&nbsp;&nbsp;<code>	int i = 100;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	int j = 1000000;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	long l1 = 1;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	long l2 = 0xffffffff;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	double d = 2.2;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	...do some calculations...
</code><a name="24485"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>}
</code><br></pre><a name="24486"></a>
are set up as follows:
<p><a name="15336"></a>
 <i>Method </i><code>void</code> <code>useManyNumeric()<p><Table Border="0">
<tr><td>          <i>   0
</i><br><td>          <i>bipush 100
</i><br><td><i>// Push a small </i><code>int</code><i> with bipush
</i>
<tr><td>          <i>   2
</i><br><td>          <i>istore_1
</i><br><td>

<tr><td>          <i>   3 
</i><br><td>          <i>ldc #1 
</i><br><td><i>// Integer </i><code>1000000</code><i>; a larger </i><code>int
</code>
<tr><td>          <i>
</i><br><td>          <i>
</i><br><td><i>// value uses ldc
</i>
<tr><td>          <i>   5 
</i><br><td>          <i>istore_2
</i><br><td>

<tr><td>          <i>   6 
</i><br><td>          <i>lconst_1
</i><br><td><i>// A tiny </i><code>long</code><i> value uses short, fast lconst_1
</i>
<tr><td>          <i>   7 
</i><br><td>          <i>lstore_3
</i><br><td>

<tr><td>          <i>   8
</i><br><td>          <i>ldc2_w #6 
</i><br><td><i>// A </i><code>long</code><i> </i><code>0xffffffff</code><i> (that is, an </i><code>int</code><i> </i><code>-1</code><i>); any
</i>
<tr><td>          <i>
</i><br><td>          <i>
</i><br><td><i>// </i><code>long</code><i> constant value can be pushed by 
ldc2_w
</i>
<tr><td>          <i>  11
</i><br><td>          <i>lstore 5
</i><br><td>

<tr><td>          <i>  13 
</i><br><td>          <i>ldc2_w #8 
</i><br><td><i>// Double </i><code>2.200000</code><i>; so do
</i>
<tr><td>          <i>
</i><br><td>          <i>
</i><br><td><i>// uncommon </i><code>double</code><i> values
</i>
<tr><td>          <i>  16 
</i><br><td>          <i>dstore 7
</i><br><td>

</Table><br><br></code><p>
<pre><a name="15252"></a>&nbsp;&nbsp;&nbsp;&nbsp;<i>...do those calculations...
</i><br></pre><a name="8478"></a>
<h1>7.5	 More Control Examples</h1>
<a name="8479"></a>
Compilation of Java's <code>for</code> statement was shown in an earlier section <a href="Compiling.doc1.html#4182">(&#167;7.2)</a>. Most of 
Java's other intramethod control transfer constructs (<code>if-then-else</code>, <code>do</code>, <code>while</code>, <code>break</code>, and 
<code>continue</code>) are also compiled in the obvious ways. The compilation of Java's <code>switch</code> statement is handled in a separate section (<a href="Compiling.doc1.html#4095">Section 7.10, "Compiling Switches"</a>), as is the 
compilation of exceptions (<a href="Compiling.doc1.html#9934">Section 7.12, "Throwing and Handling Exceptions"</a>) and 
Java's <code>finally</code> statement (<a href="Compiling.doc1.html#13789">Section 7.13, "Compiling finally"</a>).
<p><a name="8489"></a>
As a further example, a <code>while</code> loop is compiled in an obvious way, although the specific control transfer instructions made available by the Java Virtual Machine vary by data type. As usual, there is more support for data of type <code>int</code>:<p>
<pre><br><a name="8490"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>void whileInt() {
</code></pre><pre>&nbsp;&nbsp;&nbsp;&nbsp;<code>	int i = 0;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	while (i &lt; 100) {
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	    i++;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	}
</code><a name="8495"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>}
</code><br></pre><a name="8497"></a>
is compiled to
<p><a name="8498"></a>
 <i>Method </i><code>void</code> <code>whileInt()<p><Table Border="0">
<tr><td>          <i>   0 
</i><br><td><i>iconst_0
</i>
<tr><td>          <i>   1 
</i><br><td><i>istore_1
</i>
<tr><td>          <i>   2 
</i><br><td><i>goto 8
</i>
<tr><td>          <i>   5 
</i><br><td><i>iinc 1 1
</i>
<tr><td>          <i>   8 
</i><br><td><i>iload_1
</i>
<tr><td>          <i>   9 
</i><br><td><i>bipush 100
</i>
<tr><td>          <i>  11 
</i><br><td><i>if_icmplt 5
</i>
<tr><td>          <i>  14 
</i><br><td><i>return
</i>
</Table><br><br></code><p>
<a name="8508"></a>

⌨️ 快捷键说明

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