5

作为我学校项目之一的一部分,我需要深入研究 Java 字节码。我开始编写简单的程序并使用javap实用程序查看生成的字节码,我有一个关于*ipush指令的问题。

当我查看此代码的字节码时:

public class Main{
  public static void main(String []args){
    int a;
    a=5;
    a=a+32765;
  }
}

我正进入(状态

public class Main
  SourceFile: "Main.java"
  minor version: 0
  major version: 51
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
  #1 = Methodref          #3.#12         //  java/lang/Object."<init>":()V
  #2 = Class              #13            //  Main
  #3 = Class              #14            //  java/lang/Object
  #4 = Utf8               <init>
  #5 = Utf8               ()V
  #6 = Utf8               Code
  #7 = Utf8               LineNumberTable
  #8 = Utf8               main
  #9 = Utf8               ([Ljava/lang/String;)V
  #10 = Utf8               SourceFile
  #11 = Utf8               Main.java
  #12 = NameAndType        #4:#5          //  "<init>":()V
  #13 = Utf8               Main
  #14 = Utf8               java/lang/Object
{
  public Main();
flags: ACC_PUBLIC
Code:
  stack=1, locals=1, args_size=1
    0: aload_0       
    1: invokespecial #1                  // Method java/lang/Object."<init>":()V
    4: return        
  LineNumberTable:
    line 1: 0

  public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
  stack=2, locals=2, args_size=1
    0: iconst_5      
    1: istore_1      
    2: iload_1       
    3: sipush        32765
    6: iadd          
    7: istore_1      
    8: return        
  LineNumberTable:
    line 4: 0
    line 5: 2
    line 6: 8
}

当我将 32765 换成不在短期范围内的 32769 时,我得到

public class Main
  SourceFile: "Main.java"
  minor version: 0
  major version: 51
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
  #1 = Methodref          #4.#13         //  java/lang/Object."<init>":()V
  #2 = Integer            32769
  #3 = Class              #14            //  Main
  #4 = Class              #15            //  java/lang/Object
  #5 = Utf8               <init>
  #6 = Utf8               ()V
  #7 = Utf8               Code
  #8 = Utf8               LineNumberTable
  #9 = Utf8               main
  #10 = Utf8               ([Ljava/lang/String;)V
  #11 = Utf8               SourceFile
  #12 = Utf8               Main.java
  #13 = NameAndType        #5:#6          //  "<init>":()V
  #14 = Utf8               Main
  #15 = Utf8               java/lang/Object
{
  public Main();
flags: ACC_PUBLIC
Code:
  stack=1, locals=1, args_size=1
    0: aload_0       
    1: invokespecial #1                  // Method java/lang/Object."<init>":()V
    4: return        
  LineNumberTable:
    line 1: 0

  public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
  stack=2, locals=2, args_size=1
    0: iconst_5      
    1: istore_1      
    2: iload_1       
    3: ldc           #2                  // int 32769
    5: iadd          
    6: istore_1      
    7: return        
  LineNumberTable:
    line 4: 0
    line 5: 2
    line 6: 7
}

所以现在这个数字存储在常量池中。我知道没有ipush可以用来将整数常量推送到堆栈的指令,但我想知道为什么没有这样的指令?

4

1 回答 1

3

基于简单的 CISC 设计,他们试图使每条指令尽可能短(以字节为单位) 使用 32 位或 64 位常量的指令更容易/更短地引用常量表。

于 2013-06-02T10:35:08.200 回答