如果不解释 C#,那么为什么需要 VM?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9556354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If C# is not interpreted, then why is a VM needed?
提问by John V
I have read a lot of controversy about C#, where some say it's interpreted, some say it's not. I do know it's compiled into the MSIL and then JITed when run, depending on the processor etc...but isn't it still interpreted in the way it needs a VM (.NET) to run?
我读过很多关于 C# 的争议,有人说它是解释性的,有人说不是。我确实知道它被编译到 MSIL 中,然后在运行时被 JITed,具体取决于处理器等......但它是否仍然以需要 VM (.NET) 运行的方式进行解释?
采纳答案by Olivier Jacot-Descombes
The VM is just an abstraction of a microprocessor. It is just a definition and does not really exist. I.e. you cannot run code on the VM; however, you can generate IL code for it. The advantage is that language compilers do not need to know details about different kinds of real processors. Since different .NET languages like C# or VB (and many more) produce IL, they are compatible on this level. This, together with other conventions like a common type system, allows you to use a DLL generated from VB code in a C# program, for instance.
VM 只是微处理器的抽象。它只是一个定义,并不真正存在。即你不能在虚拟机上运行代码;但是,您可以为其生成 IL 代码。优点是语言编译器不需要知道不同种类的真实处理器的细节。由于不同的 .NET 语言(如 C# 或 VB(以及更多))产生 IL,因此它们在此级别上是兼容的。例如,这与通用类型系统等其他约定一起,允许您在 C# 程序中使用从 VB 代码生成的 DLL。
The IL is compiled just in time on Windows when you run a .NET application and can also be compiled ahead of time in Mono. In both cases, nativemachine code for the actual processor is generated. This fully compiled code is executed on the REAL microprocessor!
当您运行 .NET 应用程序时,IL 会在 Windows 上及时编译,也可以在 Mono 中提前编译。在这两种情况下,都会生成实际处理器的本地机器代码。这个完全编译的代码在真正的微处理器上执行!
A different aspect is the number of compliers you have to write. If you have nlanguages and you want to run them on mprocessor architectures, you need nlanguage-to-IL compliers+ mIL-to-native-code compliers. Without this intermediate abstraction layer you would need to have n × mcompliers and this can be a much higher number than just n + m!
另一个方面是您必须编写的编译器数量。如果您有n种语言,并且想在m 个处理器架构上运行它们,则需要n 个语言到 IL 编译器+ m 个IL 到本机代码编译器。如果没有这个中间抽象层,您将需要n × m编译器,这可能比n + m高得多!
回答by Jerry Coffin
The short answer is no, the requirement for the VM does not indicate that it's interpreted.
简短的回答是否定的,对 VM 的要求并不表示它已被解释。
The VM contains the JIT compiler that translates IL to native machine code. It also contains the .NET class library, upon which C# programs depend. It also contains some other mechanisms involved in dynamic linking and such (definitely built on top of Windows DLL mechanism, but .NET has features above and beyond what Windows provides on its own, which are implemented in the VM).
VM 包含将 IL 转换为本地机器代码的 JIT 编译器。它还包含 C# 程序所依赖的 .NET 类库。它还包含一些涉及动态链接等的其他机制(绝对建立在 Windows DLL 机制之上,但 .NET 具有超出 Windows 自身提供的功能,这些功能是在 VM 中实现的)。
回答by sinharaj
You are probably refering to CLR(an implementation of the specification CLI).
The CLIdefines a specific type system, semantics of all the operations on these types, a memory model, and run-time metadata.
在CLI定义了一个特定类型的系统,在这些类型中,一个存储器模型,和运行时间的元数据的所有操作的语义。
In order to provide all of the above, some instrumentation of the generated code must happen. One simple example is to ensure that larger-than-32-bit numerals are supported and that floating-point operations behave as per specification on every architecture.
为了提供以上所有内容,必须对生成的代码进行一些检测。一个简单的例子是确保支持大于 32 位的数字,并且浮点运算的行为符合每种架构的规范。
In addition, to ensure correct behaviour of memory allocation, correct management of metadata, static initialisation, generic type instantiation and similar some additional processes must be present during the execution of your CLRcode. This is all taken care of by the VM and is not readily provided by the CPU.
此外,为了确保内存分配的正确行为、元数据的正确管理、静态初始化、泛型类型实例化和类似的一些附加过程,必须在CLR代码执行期间出现。这一切都由 VM 处理,CPU 不容易提供。
A quote from Wikipedia, for example:
引用维基百科的一段话,例如:
The CLR provides additional services including memory management, type safety and exception handling.
CLR 提供额外的服务,包括内存管理、类型安全和异常处理。

