How does the .NET IL .maxstack directive work?

妖精的绣舞 提交于 2020-01-29 04:18:08

问题


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. 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?

回答1:


.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

(Pseudo 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

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

To answer your questions:

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

Just for the function

  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?

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?

It's unrelated to the number of variables, see Lasse V. Karlsen's answer

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

Doesn't seem like a good idea, but I don't know.

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




回答2:


It has nothing to do with the number of variables declared, but instead everything to do with how many values you need to push on a stack at any given time in order to compute some expression.

For instance, in the following expression, I would assume 2 values needs to be pushed onto the stack:

x = y + z;

This is unrelated to the fact that there are at least 3 variables present, x, y, and z, and possibly others as well.

Unfortunately I don't know the answer to your other questions, and I would guess experimentation would be one way to find some answers.




回答3:


You can refer to the following and the ECMA STANDARD to get a better understanding:

void  msd(string a,
  string b,
  string c,
  string d,
  string e)
  {
  Console.WriteLine(a);
}

msd("a","b","c","d","e");

When I run ildasm.exe I got this:

{
  .entrypoint
  // Code size       40 (0x28)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  nop
  IL_0002:  ldstr      "a"
  IL_0007:  ldstr      "b"
  IL_000c:  ldstr      "c"
  IL_0011:  ldstr      "d"
  IL_0016:  ldstr      "e"
  IL_001b:  call       void sf.Program::'<Main>g__msd|0_0'(string,
                                                           string,
                                                           string,
                                                           string,
                                                           string)
  IL_0020:  nop
  IL_0021:  call       string [mscorlib]System.Console::ReadLine()
  IL_0026:  pop
  IL_0027:  ret
} // end of method Program::Main

from the above. I found the max stakc value which isn't determined by the push & pop instructions.

I didn't know what the real stack number values are. So, I reference the ildasm disassembly code to determine the real max stack value.



来源:https://stackoverflow.com/questions/1241308/how-does-the-net-il-maxstack-directive-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!