c# - .NET IL .maxstack 指令如何工作?

标签 c# .net vb.net reflection intermediate-language

我想知道 .maxstack 是如何工作的。我知道这与您声明的类型的实际大小无关,而是与它们的数量有关。我的问题是:

  1. 这是否仅适用于 功能,或所有功能 我们呼吁的是什么?
  2. 即使只是为了功能 正在声明 .maxstack, 你怎么知道 maxstack 是什么 你有分支?你去看看 所有“路径”并返回 可能的最大值?
  3. 如果我将它设置为 16 并且 实际上有17个变量?
  4. 如果我会受到太大的惩罚 将其设置为 256?

最佳答案

.maxstack 是 IL 验证的一部分。基本上 .maxstack 告诉 JIT 它需要为该方法保留的最大堆栈大小。例如,x = y + (a - b) 转换为

(伪 IL:)

1. Push y on the stack
2. Push a on the stack
3. Push b on the stack
4. Pop the last two items from the stack,
      substract them and
      push the result on the stack
5. Pop the last two items from the stack,
      add them and
      push the result on the stack
6. Store the last item on the stack in x and
      pop the last item from the stack

可以看到,每次栈中最多有3个项目。 如果您为此方法将 .maxstack 设置为 2(或更小),代码将不会运行。

另外,你不能有这样的东西,因为它需要无限的堆栈大小:

1. Push x on the stack
2. Jump to step 1

回答您的问题:

  1. does this apply just for the function, or to all the functions that we are calling for?

只是为了功能

  1. even if it's just for the function were .maxstack is being declared, how do you know what maxstack is if you have branching? You go and see all the "paths" and return the maximum value possible?

你去看看所有的路径并返回可能的最大值

  1. What happens if I set it to 16 and actually there are 17 variables?

与变量个数无关,见Lasse V. Karlsen的回答

  1. Is there a too big of a penalty if I set it to 256?

这似乎不是个好主意,但我不知道。

您真的必须自己计算.maxstack吗? System.Reflection.Emit 为您计算 IIRC。

关于c# - .NET IL .maxstack 指令如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1241308/

相关文章:

.net - PowerShell/.NET 4 GUI - 内部 Windows 图标?

vb.net - 如何在 Visual Basic 中编码多个 "Or"?

c# - 在 WinRT 中编译并运行外部 Java 程序?

c# - .NET 类的属性数量有限制吗?

c# - Win32Api USB SetupDiGetDeviceInterfaceDetail 失败

c# - 在运行时 ProtoBuf-net 模型中是否允许使用 <T>?

vb.net - 如何将无人图像放入datagridview的图像列中

python-3.x - 使用 VB.Net 通过 TCP 发送文件并使用 Python3.6 中的 Socket 接收时出现文件编码错误

c# - "the term ' 添加迁移 ' is not recognized as the name of a cmdlet" Visual Studio 2019

c# - 在循环中按下另一个应用程序中的按钮