Mono.Cecil.Cil.CodeWriter.ComputeStackDelta C# (CSharp) Method

ComputeStackDelta() static private method

static private ComputeStackDelta ( Instruction instruction, int &stack_size ) : void
instruction Instruction
stack_size int
return void
        static void ComputeStackDelta(Instruction instruction, ref int stack_size)
        {
            switch (instruction.opcode.FlowControl) {
            case FlowControl.Call: {
                var method = (IMethodSignature) instruction.operand;
                // pop 'this' argument
                if (method.HasImplicitThis() && instruction.opcode.Code != Code.Newobj)
                    stack_size--;
                // pop normal arguments
                if (method.HasParameters)
                    stack_size -= method.Parameters.Count;
                // pop function pointer
                if (instruction.opcode.Code == Code.Calli)
                    stack_size--;
                // push return value
                if (method.ReturnType.etype != ElementType.Void || instruction.opcode.Code == Code.Newobj)
                    stack_size++;
                break;
            }
            default:
                ComputePopDelta (instruction.opcode.StackBehaviourPop, ref stack_size);
                ComputePushDelta (instruction.opcode.StackBehaviourPush, ref stack_size);
                break;
            }
        }

Usage Example

        private static void ComputeStackSize(Instruction instruction, ref Dictionary <Instruction, int> stack_sizes, ref int stack_size, ref int max_stack)
        {
            int num;

            if (stack_sizes != null && stack_sizes.TryGetValue(instruction, out num))
            {
                stack_size = num;
            }
            max_stack = System.Math.Max(max_stack, stack_size);
            CodeWriter.ComputeStackDelta(instruction, ref stack_size);
            max_stack = System.Math.Max(max_stack, stack_size);
            CodeWriter.CopyBranchStackSize(instruction, ref stack_sizes, stack_size);
            CodeWriter.ComputeStackSize(instruction, ref stack_size);
        }