我正在尝试使用自定义编译器编译以下代码:

public static void main([String] args)
{
    long i = 2L
    i *= 2L
    System out println i
}


使用javap时,编译结果为以下字节码:

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=3, locals=2, args_size=1
         0: ldc2_w        #14                 // long 2l
         3: lstore_1
         4: lload_1
         5: ldc2_w        #14                 // long 2l
         8: lmul
         9: lstore_1
        10: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
        13: lload_1
        14: invokevirtual #27                 // Method java/io/PrintStream.println:(J)V
        17: return
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      17     1     i   J
            0      17     0  args   [Ljava/lang/String;
    MethodParameters:
      Name                           Flags
      args


但是,当我尝试运行字节码时,JVM会抛出读取ClassFormatErrorInvalid index 1 in LocalVariableTable。这是否与LocalVariableTable中的插槽1位于插槽0之前有关?

编辑:

如果将long变量更改为int,即使LocalVariableTable仍未排序,JVM也完全不会抱怨:

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=2, args_size=1
         0: iconst_2
         1: istore_1
         2: iload_1
         3: iconst_2
         4: imul
         5: istore_1
         6: getstatic     #19                 // Field java/lang/System.out:Ljava/io/PrintStream;
         9: iload_1
        10: invokevirtual #25                 // Method java/io/PrintStream.println:(I)V
        13: return
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      13     1     i   I
            0      13     0  args   [Ljava/lang/String;
    MethodParameters:
      Name                           Flags
      args

最佳答案

这可能不是答案(无法发表评论..:P)

您的自定义编译器在某处出错。

  LocalVariableTable:
    Start  Length  Slot  Name   Signature
        0      17     1     i   J
        0      17     0  args   [Ljava/lang/String;


2个局部变量不能具有相同的起始索引。

Java编译器为您提供:

 Start  Length  Slot  Name   Signature
     0      18     0  args   [Ljava/lang/String;
     4      14     1     i   J

10-05 22:48