.net - 微软的开发人员为什么选择使.NET成为基于堆栈的计算机?

标签 .net stack-based

今天,我在VS2008随附的工具之间找到了Disassembler IL。我试图反汇编程序,然后看一下结果。操作码并不是很难理解,但是有一件事让我感到惊讶:.NET是基于堆栈的?阅读“编写出色的代码,第II卷”,由于基于堆栈的计算机运行速度很慢,因此没有获得很好的画面。它们也很容易实现,但是我不认为MS开发人员会因为其简单性而选择这种方法,毕竟所有代码都必须转换为真实的机器代码,这样他们才可以解决问题。
你们任何人都可以解释这个奇怪的选择吗?

PS :
我在这里发表有关该主题的文章:

13.1.1 Stack-Based Machines Stack-based machines use memory for most calculations, employing a stack in memory to hold all operands and results. Computer systems employing a stack architecture offer some important advantages over other architectures:

  • The instructions are often smaller (each consuming fewer bytes) than those found in other architectures because the instructions generally don’t have to specify any operands.
  • It is generally easier to write compilers for stack architectures than for other machines because converting arithmetic expressions to a sequence of stack operations is very easy.
  • Temporary variables are rarely needed in a stack architecture, because the stack itself serves that purpose.
Unfortunately, stack machines also suffer from some serious disadvantages:
  • Almost every instruction references memory (which is slow on modern machines). Though caches can help mitigate this problem, memory performance is still a major problem on stack machines.
  • Even though conversion from HLLs to a stack machine is very easy, there is less opportunity for optimization than there is with other architectures.
  • Because stack machines are constantly accessing the same data elements (that is, data on the top of the stack), pipelining and instruction parallelism is difficult to achieve (see Write Great Code, Volume 1 for details on pipelining and instruction parallelism).
A stack is a data structure that allows operations only on a few limited elements of the stack (often called the top of stack and next on stack). With a stack you generally do one of three things: push new data onto the stack, pop data from the stack, or operate on the data that is currently sitting on the top of the stack (and possibly the data immediately below it).





13.1.1.5 Real-World Stack Machines
A big advantage of the stack architecture is that it is easy to write a compiler for such a machine. It’s also very easy to write an emulator for a stack-based machine. For these reasons, stack architectures are popular in virtual machines (VMs) such as the Java Virtual Machine and the Microsoft Visual Basic p-code interpreter. A few real-world stack-based CPUs do exist, such as a hardware implementation of the Java VM; however, they are not very popular because of the performance limitations of memory access. Nonetheless, understanding the basics of a stack architecture is important because many compilers translate HLL source code into a stack-based form prior to translating to actual machine code. Indeed, in the worst case (though rare), compilers are forced to emit code that emulates a stack-based machine when compiling complex arithmetic expressions.



编辑:我刚刚在@EricLippert的博客中找到an article来回答问题并确认@Aaron的回答

最佳答案

请记住,仅因为中间表示是基于堆栈的,并不意味着生成的机器代码是基于堆栈的。当代码从中间形式转换为机器代码时,基本上可以重新编译-允许进行本地优化。

关于使用基于堆栈的中间表示的一件好事是,您不受任何特定体系结构的束缚。

想象一下,如果他们决定使用理论上基于寄存器的系统作为中间形式。他们应该选择多少个寄存器? 8? 16吗64吗?如果目标处理器的实际寄存器多于中间形式,那么您就失去了可能的优化方法。如果目标的实际寄存器少于中间寄存器,则您的优化会适得其反,因为这些寄存器无论如何都会刷新到内存中。

即使在当前的CPU上,编译到x86与x64还是有很大的不同-更不用说备用架构(ARM)或将来的架构了。

对于这样的事情,最好将它们保留为最简单的形式,然后在最终代码生成过程中依靠优化来使其与实际硬件相匹配。

关于.net - 微软的开发人员为什么选择使.NET成为基于堆栈的计算机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5940449/

相关文章:

c# - 方法 x 没有支持的 SQL 转换

language-agnostic - 寄存器与堆栈

javascript - 识别基于堆栈的语言

c++ - 基于堆栈的缓冲区溢出

c# - DataGridViewCheckBoxColumn : FormatException on boolean-column

.net - .NET 3.5 中的 EnumerateFiles() 等效项

C# 在 AD 中链接组策略

c# - MySQL 随机关闭连接,在旧的 WinForms 应用程序 .Net 1.1 和 ODBC 中