views:

416

answers:

2
+1  Q: 

.maxstack question

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

Thanks

+1  A: 

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.

Lasse V. Karlsen
Ah. So if my expressions never have more than 8 operands and any function I call from my method never has more than 8 arguments, I'll never need more than .maxstack 8?
devoured elysium
`x = y + (a - b)` needs a stack size of 3 even though both operators have only 2 operands.
dtb
+4  A: 

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

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

dtb
I kinda do, although I will just let it be as it is. I am just editing some IL code from other executables, but the method I am inserting only has 2 arguments, so I think it will never interfere with the already existing .maxstack on the IL method.
devoured elysium