MSIL 和 Java 字节码之间的区别?

发布于 2024-07-05 18:42:19 字数 53 浏览 6 评论 0 原文

我是 .Net 的新手,我想首先了解基础知识。 MSIL 和 Java 字节码有什么区别?

I'm new to .Net and I'm trying to understand the basics first. What is the difference between MSIL and Java bytecode?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

椵侞 2024-07-12 18:42:19

CIL 又名 MSIL 旨在人类可读。 Java 字节码不是。

将 Java 字节码视为不存在的硬件(但由 JVM 模拟)的机器代码。

CIL 更像是汇编语言——距离机器代码一步之遥,同时仍然是人类可读的。

CIL aka MSIL is intended to be human-readable. Java bytecode is not.

Think of Java bytecode as being machine code for hardware that does not exist (but which JVMs emulate).

CIL is more like assembly language - one step from machine code, while still being human-readable.

眸中客 2024-07-12 18:42:19

Serge Lidin 撰写了一本关于 MSIL 详细信息的不错的书:Expert .NET 2.0 IL Assembler。 我还能够通过使用 .NET ReflectorIldasm(教程)

MSIL 和 Java 字节码之间的概念非常相似。

Serge Lidin authored a decent book on the details of MSIL: Expert .NET 2.0 IL Assembler. I also was able to pick up MSIL quickly by looking at simple methods using .NET Reflector and Ildasm (Tutorial).

The concepts between MSIL and Java bytecode are very similar.

深爱成瘾 2024-07-12 18:42:19

没有那么多差异。 两者都是您编写的代码的中间格式。 执行时,虚拟机将执行托管的中间语言,这意味着虚拟机控制变量和调用。 甚至有一种我现在不记得的语言可以以同样的方式在 .Net 和 Java 上运行。

基本上,它只是同一事物的另一种格式

编辑:找到语言(除了 Scala):它是 FAN(http:// www.fandev.org/),看起来很有趣,但还没时间评估

There are not that much differences. Both are intermediate formats of the code you wrote. When executed, the Virtual machines will execute the intermediate language managed that means that the Virtual Machine controls the variables and calls. There is even a language which I don't remeber right now that can run at .Net and Java the same way.

Basicly, it's just another format for the same thing

Edit: Found the language (besides Scala): It's FAN (http://www.fandev.org/), looks very interesting, but no time yet to evaluate

时光倒影 2024-07-12 18:42:19

同意,差异很小,作为初学者可以忽略。 如果您想从基础开始学习 .Net,我建议您查看公共语言基础结构和公共类型系统。

Agreed, the differences are minute enough to ingore as a beginner. If you want to learn .Net starting from the basics, I'd recommend looking at the Common Language Infrastructure, and the Common Type System.

静赏你的温柔 2024-07-12 18:42:19

我认为MSIL不应该与Java字节码相比较,而是“组成Java字节码的指令”。

没有反汇编的java字节码的名称。 “Java Bytecode”应该是一个非官方的别名,因为我在官方文档中找不到它的名字。
Java 类文件反汇编器

打印出类中每个方法的反汇编代码,即组成 Java 字节码的指令。 这些都记录在 Java 虚拟机规范中。

“Java VM指令”和“MSIL”都被组装成.NET字节码和Java代码,这些都是人类无法阅读的。

I think MSIL should not compare to Java bytecode, but "the instruction that comprise the Java bytecodes".

There is no name of disassembled java bytecode. "Java Bytecode" should be an unofficial alias, as I cannot find its name in official document.
The Java Class File Disassembler say

Prints out disassembled code, i.e., the instructions that comprise the Java bytecodes, for each of the methods in the class. These are documented in the Java Virtual Machine Specification.

Both "Java VM instructions" and "MSIL" are assembled into .NET bytecode and Java code, which are not human readable.

生活了然无味 2024-07-12 18:42:19

它们本质上在做同样的事情,MSIL 是 Microsoft 版本的 Java 字节码。

内部的主要区别是:

  1. 字节码是为编译和解释而开发的,而 MSIL 是为 JIT 编译而明确开发的
  2. MSIL 是为支持多种语言(C# 和 VB.NET 等)而开发的,而字节码只是为 Java 编写的,从而导致字节码在语法上比 IL 与任何特定 .NET 语言在语法上更类似于 Java
  3. MSIL 在值和引用类型之间有更明确的描述

更多信息和详细比较可以在 K John Gough 的这篇文章(后记文档)

They are essentially doing the same thing, MSIL is Microsoft's version of Java bytecode.

The main differences internally are:

  1. Bytecode was developed for both compilation and interpretation, while MSIL was developed explicitly for JIT compilation
  2. MSIL was developed to support multiple languages (C# and VB.NET, etc.) versus Bytecode being written for just Java, resulting in Bytecode being more similar to Java syntactically than IL is to any specific .NET language
  3. MSIL has more explicit delineation between value and reference types

A lot more information and a detailed comparison can be found in this article by K John Gough (postscript document)

温暖的光 2024-07-12 18:42:19

首先我要说的是,我不认为 Java 字节码和 MSIL 之间的细微差别会困扰 .NET 开发新手。 它们都具有相同的目的,即定义一个抽象目标机器,该目标机器是最终使用的物理机器之上的一层。

MSIL 和 Java 字节码非常相似,实际上有一个名为 Grasshopper 它将 MSIL 转换为 Java 字节码,我是 Grasshopper 开发团队的一员,所以我可以分享一些我的(褪色的)知识。
请注意,当 .NET Framework 2.0 发布时,我就停止了这方面的工作,因此其中一些内容可能不再正确(如果是这样,请发表评论,我会更正它)。

  • .NET 允许用户定义的类型具有与常规引用语义 (struct) 不同的值语义。
  • .NET支持无符号类型,这使得指令集更加丰富。
  • Java 在字节码中包含方法的异常规范。 虽然异常规范通常仅由编译器强制执行,但如果使用默认类加载器以外的类加载器,则可能由 JVM 强制执行。
  • .NET 泛型用 IL 表示,而 Java 泛型仅使用类型擦除
  • .NET 属性在 Java 中没有等效项(这仍然是真的吗?)。
  • .NET enum 只不过是整数类型的包装器,而 Java enums 几乎是完全成熟的类(感谢 网友评论)。
  • .NET 有 outref 参数。

还有其他语言差异,但大多数都不是在字节码级别上表达的,例如,如果没记错的话,Java 的非静态内部类(.NET 中不存在)不是一个字节码功能时,编译器会为内部类的构造函数生成一个附加参数并传递外部对象。 .NET lambda 表达式也是如此。

First off let me say that I don't think that the subtle differences between Java bytecode and MSIL is something that should bother a novice .NET developer. They both serve the same purpose of defining an abstract target machine which is a layer above the physical machine being used in the end.

MSIL and Java bytecode are very similar, in fact there is was a tool called Grasshopper which translates MSIL to Java bytecode, I was part of the development team for Grasshopper so I can share a bit of my (faded) knowledge.
Please note that I stopped working on this around when .NET framework 2.0 came out so some of these things may not be true any more (if so please leave a comment and I'll correct it).

  • .NET allows user defined types that have value semantics as apposed to the regular reference semantics (struct).
  • .NET supports unsigned types, this makes the instruction set a bit richer.
  • Java includes the exception specification of methods in the bytecode. Although exception specification is usually only enforced by the compiler, it may be enforced by the JVM if a class loader other than the default one is used.
  • .NET generics are expressed in IL while Java generics only use type erasure.
  • .NET attributes have no equivalent in Java (is this still true?).
  • .NET enums are not much more than wrappers around integer types while Java enums are pretty much fully fledged classes (thanks to Internet Friend for commenting).
  • .NET has out and ref parameters.

There are other language differences but most of them are not expressed at the byte code level, for example, if memory serves, Java's non-static inner classes (which do not exist in .NET) are not a bytecode feature, the compiler generates an additional argument to the inner class's constructor and passes the outer object. The same is true for .NET lambda expressions.

寄风 2024-07-12 18:42:19

CIL(MSIL 的专有名称)和 Java 字节码的相同点多于不同点。 但存在一些重要的区别:

1) CIL 从一开始就被设计为作为多种语言的目标。 因此,它支持更丰富的类型系统,包括有符号和无符号类型、值类型、指针、属性、委托、事件、泛型、具有单个根的对象系统等等。 CIL 支持初始 CLR 语言(C# 和 VB.NET)不需要的功能,例如全局函数和尾调用优化 。 相比之下,Java 字节码被设计为 Java 语言的目标,并反映了 Java 本身的许多限制。 使用Java字节码编写C或Scheme会困难得多。

2) CIL 旨在轻松集成到本机库和非托管代码中

3) Java 字节码设计为可解释或编译,而 CIL 的设计仅假设 JIT 编译。 也就是说,Mono 的初始实现使用了解释器而不是 JIT。

4) CIL 被设计(并指定)为人类可读可写的汇编语言直接映射到字节码形式的形式。 我相信 Java 字节码(顾名思义)只能是机器可读的。 当然,Java字节码相对容易反编译回原始Java,并且如下所示,它也可以被“反汇编”。

我应该指出,JVM(其中大多数)比 CLR(其中任何一个)优化程度更高。 因此,原始性能可能是首选针对 Java 字节码的一个原因。 但这是一个实现细节。

有人说 Java 字节码是为多平台设计的,而 CIL 是为 Windows 设计的。 不是这种情况。 .NET 框架中有一些“Windows”主义,但 CIL 中没有。

作为上面第 4) 点的示例,我不久前编写了一个 Java 到 CIL 编译器的玩具。 如果您向该编译器提供以下 Java 程序:

class Factorial{
    public static void main(String[] a){
    System.out.println(new Fac().ComputeFac(10));
    }
}

class Fac {
    public int ComputeFac(int num){
    int num_aux ;
    if (num < 1)
        num_aux = 1 ;
    else 
        num_aux = num * (this.ComputeFac(num-1)) ;
    return num_aux ;
    }
}

我的编译器将输出以下 CIL:

.assembly extern mscorlib { }
.assembly 'Factorial' { .ver  0:0:0:0 }
.class private auto ansi beforefieldinit Factorial extends [mscorlib]System.Object
{
   .method public static default void main (string[] a) cil managed
   {
      .entrypoint
      .maxstack 16
      newobj instance void class Fac::'.ctor'()
      ldc.i4 3
      callvirt instance int32 class Fac::ComputeFac (int32)
      call void class [mscorlib]System.Console::WriteLine(int32)
      ret
   }
}

.class private Fac extends [mscorlib]System.Object
{
   .method public instance default void '.ctor' () cil managed
   {
      ldarg.0
      call instance void object::'.ctor'()
      ret
   }

   .method public int32 ComputeFac(int32 num) cil managed
   {
      .locals init ( int32 num_aux )
      ldarg num
      ldc.i4 1
      clt
      brfalse L1
      ldc.i4 1
      stloc num_aux
      br L2
   L1:
      ldarg num
      ldarg.0
      ldarg num
      ldc.i4 1
      sub
      callvirt instance int32 class Fac::ComputeFac (int32)
      mul
      stloc num_aux
   L2:
      ldloc num_aux
      ret
   }
}

这是一个有效的 CIL 程序,可以将其输入到像 ilasm.exe 这样的 CIL 汇编器中以创建可执行文件。 正如您所看到的,CIL 是一种完全人类可读可写的语言。 您可以在任何文本编辑器中轻松创建有效的 CIL 程序。

您还可以使用 javac 编译器编译上面的 Java 程序,然后通过 javap“反汇编器”运行生成的类文件,得到以下内容:

class Factorial extends java.lang.Object{
Factorial();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   new #3; //class Fac
   6:   dup
   7:   invokespecial   #4; //Method Fac."<init>":()V
   10:  bipush  10
   12:  invokevirtual   #5; //Method Fac.ComputeFac:(I)I
   15:  invokevirtual   #6; //Method java/io/PrintStream.println:(I)V
   18:  return

}

class Fac extends java.lang.Object{
Fac();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public int ComputeFac(int);
  Code:
   0:   iload_1
   1:   iconst_1
   2:   if_icmpge   10
   5:   iconst_1
   6:   istore_2
   7:   goto    20
   10:  iload_1
   11:  aload_0
   12:  iload_1
   13:  iconst_1
   14:  isub
   15:  invokevirtual   #2; //Method ComputeFac:(I)I
   18:  imul
   19:  istore_2
   20:  iload_2
   21:  ireturn
}

javap< /code> 输出不可编译(据我所知),但如果将其与上面的 CIL 输出进行比较,您会发现两者非常相似。

CIL (the proper name for MSIL) and Java bytecode are more the same than they are different. There are some important differences though:

1) CIL was designed from the beginning to serve as a target for multiple languages. As such, it supports a much richer type system including signed and unsigned types, value types, pointers, properties, delegates, events, generics, an object-system with a single root, and more. CIL supports features not required for the initial CLR languages (C# and VB.NET) such as global functions and tail-call optimizations. In comparision, Java bytecode was designed as a target for the Java language and reflects many of the constraints found in Java itself. It would be a lot harder to write C or Scheme using Java bytecode.

2) CIL was designed to integrate easily into native libraries and unmanaged code

3) Java bytecode was designed to be either interpreted or compiled while CIL was designed assuming JIT compilation only. That said, the initial implementation of Mono used an interpreter instead of a JIT.

4) CIL was designed (and specified) to have a human readable and writable assembly language form that maps directly to the bytecode form. I believe that Java bytecode was (as the name implies) meant to be only machine readable. Of course, Java bytecode is relatively easily decompiled back to the original Java and, as shown below, it can also be "disassembled".

I should note that the JVM (most of them) is more highly optimized than the CLR (any of them). So, raw performance might be a reason to prefer targeting Java bytecode. This is an implementation detail though.

Some people say that the Java bytecode was designed to be multi-platform while CIL was designed to be Windows only. This is not the case. There are some "Windows"isms in the .NET framework but there are none in CIL.

As an example of point number 4) above, I wrote a toy Java to CIL compiler a while back. If you feed this compiler the following Java program:

class Factorial{
    public static void main(String[] a){
    System.out.println(new Fac().ComputeFac(10));
    }
}

class Fac {
    public int ComputeFac(int num){
    int num_aux ;
    if (num < 1)
        num_aux = 1 ;
    else 
        num_aux = num * (this.ComputeFac(num-1)) ;
    return num_aux ;
    }
}

my compiler will spit out the following CIL:

.assembly extern mscorlib { }
.assembly 'Factorial' { .ver  0:0:0:0 }
.class private auto ansi beforefieldinit Factorial extends [mscorlib]System.Object
{
   .method public static default void main (string[] a) cil managed
   {
      .entrypoint
      .maxstack 16
      newobj instance void class Fac::'.ctor'()
      ldc.i4 3
      callvirt instance int32 class Fac::ComputeFac (int32)
      call void class [mscorlib]System.Console::WriteLine(int32)
      ret
   }
}

.class private Fac extends [mscorlib]System.Object
{
   .method public instance default void '.ctor' () cil managed
   {
      ldarg.0
      call instance void object::'.ctor'()
      ret
   }

   .method public int32 ComputeFac(int32 num) cil managed
   {
      .locals init ( int32 num_aux )
      ldarg num
      ldc.i4 1
      clt
      brfalse L1
      ldc.i4 1
      stloc num_aux
      br L2
   L1:
      ldarg num
      ldarg.0
      ldarg num
      ldc.i4 1
      sub
      callvirt instance int32 class Fac::ComputeFac (int32)
      mul
      stloc num_aux
   L2:
      ldloc num_aux
      ret
   }
}

This is a valid CIL program that can be fed into a CIL assembler like ilasm.exe to create an executable. As you can see, CIL is a fully human readable and writable language. You can easily create valid CIL programs in any text editor.

You can also compile the Java program above with the javac compiler and then run the resulting class files through the javap "disassembler" to get the following:

class Factorial extends java.lang.Object{
Factorial();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   new #3; //class Fac
   6:   dup
   7:   invokespecial   #4; //Method Fac."<init>":()V
   10:  bipush  10
   12:  invokevirtual   #5; //Method Fac.ComputeFac:(I)I
   15:  invokevirtual   #6; //Method java/io/PrintStream.println:(I)V
   18:  return

}

class Fac extends java.lang.Object{
Fac();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public int ComputeFac(int);
  Code:
   0:   iload_1
   1:   iconst_1
   2:   if_icmpge   10
   5:   iconst_1
   6:   istore_2
   7:   goto    20
   10:  iload_1
   11:  aload_0
   12:  iload_1
   13:  iconst_1
   14:  isub
   15:  invokevirtual   #2; //Method ComputeFac:(I)I
   18:  imul
   19:  istore_2
   20:  iload_2
   21:  ireturn
}

The javap output is not compilable (to my knowledge) but if you compare it to the CIL output above you can see that the two are very similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文