Skip to content

[API Proposal]: Make OpCode.StackChange() public #93419

Closed
@buyaa-n

Description

@buyaa-n

Background and motivation

OpCodes represents MS IL instructions that used for emitting IL with ILGenerator. An OpCode instruction may pop value(s) from stack as an operand of the instruction and also may push value(s) into the stack as a result. This stack change is needed for calculating MaxStack value that is essential for populating method body. I could use the StackBehaviourPop and StackBehaviourPush properties of OpCode to calculate this, but there is an internal method that gives this info directly:

internal int StackChange() =>
m_flags >> StackChangeShift;

Making this public will be useful for calculating MaxStack faster
Related to https://github.com/dotnet/runtime/pull/93244/files#r1353013060

API Proposal

namespace System.Reflection.Emit

public readonly partial struct OpCode : System.IEquatable<System.Reflection.Emit.OpCode>
{
    ...
    public StackBehaviour StackBehaviourPop { get }
    public StackBehaviour StackBehaviourPush { get }
    ...
+   public int EvaluationStackDelta { get }
}

Alternative Designs

Other naming options: EvaluationStackDelta, StackSizeDelta, StackTransitionDelta or StackDepthDelta as it is showing the difference of evaluation stack transition.

API Usage

    private void UpdateStackSize(OpCode opCode)
    {
        _currentStack += opCode.EvaluationStackDelta;
        _maxStackSize = Math.Max(_maxStackSize, _currentStack);
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions