-
Couldn't load subscription status.
- Fork 5.2k
Description
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:
runtime/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/Opcode.cs
Lines 48 to 49 in 946c524
| internal int StackChange() => | |
| m_flags >> StackChangeShift; |
Making this public will be useful for calculating
MaxStack fasterRelated 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);
}