如何在.NET IL .maxstack指令工作? [英] How does the .NET IL .maxstack directive work?

查看:1406
本文介绍了如何在.NET IL .maxstack指令工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何.maxstack真正发挥作用。我知道这并不都与你在声明类型的实际大小,但他们的数量。我的问题是:

I'd like to know how does .maxstack really work. I know it doesn't have to do with the actual size of the types you are declaring but with the number of them. My questions are:

  1. 这是否只是申请 功能,或对所有的功能 我们呼吁?
  2. ,即使它只是功能 是.maxstack被宣布, 你怎么知道MAXSTACK是什么,如果 你有分支?你去看看 所有的路径,并返回 最大值可能吗?
  3. 在发生什么,如果我将它设置为16 居然有17变数?
  4. 有没有太大的罚款,如果我 它设置为256?
  1. does this apply just for the function, or to all the functions that we are calling for?
  2. 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?
  3. What happens if I set it to 16 and actually there are 17 variables?
  4. Is there a too big of a penalty if I set it to 256?

感谢

推荐答案

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

.maxstack is part of the IL verification. Basically .maxstack tells the JIT the max stack size it needs to reserve for the method. For example, x = y + (a - b) translates to

(伪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(或更少)的这种方法,code将无法运行。

As you can see, there are at most 3 items on the stack at each time. If you'd set .maxstack to 2 (or less) for this method, the code wouldn't run.

另外,你不能有这样的事情,因为这将需要一个无限的堆栈大小:

Also, you cannot have something like this as it would require an infinite stack size:

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

要回答你的问题:

  1. 只是为了功能
  2. 您去看看所有的路径,并返回最大可能值
  3. 这是无关的变量数,看的的Lasse五卡尔森的的答案
  4. 似乎不是一个好主意,但我不知道。
  1. Just for the function
  2. You go and see all the paths and return the maximum value possible
  3. It's unrelated to the number of variables, see Lasse V. Karlsen's answer
  4. Doesn't seem like a good idea, but I don't know.

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

Do you really have to calculate the .maxstack yourself? System.Reflection.Emit calculates it for you IIRC.

这篇关于如何在.NET IL .maxstack指令工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆