Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API that allows the emitter to indicate presence of localloc #25267

Closed
tmat opened this issue Mar 1, 2018 · 2 comments
Closed

Add API that allows the emitter to indicate presence of localloc #25267

tmat opened this issue Mar 1, 2018 · 2 comments
Labels
api-approved API was approved in API review, it can be implemented area-System.Reflection.Metadata
Milestone

Comments

@tmat
Copy link
Member

tmat commented Mar 1, 2018

When encoding method body header the encoder needs to decide whether to emit tiny or fat header. A small method that has no locals but contains localloc instruction and has InitLocals set to true should not be encoded with tiny header since tiny header implies InitLocals is false. The presence of localloc instruction needs to be indicated by the caller of the method body encoder similarly to max stack and other info. Hence we need to add an overload that takes an extra bool parameter.

Addresses https://github.com/dotnet/corefx/issues/26910

Proposed API

namespace System.Reflection.Metadata.Ecma335
{
    public readonly struct MethodBodyStreamEncoder
    {
       // existing overloads

       public MethodBody AddMethodBody(
            int codeSize,
            int maxStack,
            int exceptionRegionCount,
            bool hasSmallExceptionRegions,
            StandaloneSignatureHandle localVariablesSignature,
            MethodBodyAttributes attributes);

       public int AddMethodBody(
            InstructionEncoder instructionEncoder,
            int maxStack,
            StandaloneSignatureHandle localVariablesSignature,
            MethodBodyAttributes attributes)

        // new overloads

        /// <summary>
        /// Encodes a method body and adds it to the method body stream.
        /// </summary>
        /// <param name="codeSize">Number of bytes to be reserved for instructions.</param>
        /// <param name="maxStack">Max stack.</param>
        /// <param name="exceptionRegionCount">Number of exception regions.</param>
        /// <param name="hasSmallExceptionRegions">True if the exception regions should be encoded in 'small' format.</param>
        /// <param name="localVariablesSignature">Local variables signature handle.</param>
        /// <param name="attributes">Attributes.</param>
        /// <param name="hasDynamicStackAllocation">True if the method allocates from dynamic local memory pool (<c>localloc</c> instruction).</param>
        /// <returns>The offset of the encoded body within the method body stream.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="codeSize"/>, <paramref name="exceptionRegionCount"/>, or <paramref name="maxStack"/> is out of allowed range.
        /// </exception>
        public MethodBody AddMethodBody(
            int codeSize,
            int maxStack = 8,
            int exceptionRegionCount = 0,
            bool hasSmallExceptionRegions = true,
            StandaloneSignatureHandle localVariablesSignature = default,
            MethodBodyAttributes attributes = MethodBodyAttributes.InitLocals,
            bool hasDynamicStackAllocation = false);

        /// <summary>
        /// Encodes a method body and adds it to the method body stream.
        /// </summary>
        /// <param name="instructionEncoder">Instruction encoder.</param>
        /// <param name="maxStack">Max stack.</param>
        /// <param name="localVariablesSignature">Local variables signature handle.</param>
        /// <param name="attributes">Attributes.</param>
        /// <param name="hasDynamicStackAllocation">True if the method allocates from dynamic local memory pool (the IL contains <c>localloc</c> instruction).
        /// </param>
        /// <returns>The offset of the encoded body within the method body stream.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="instructionEncoder"/> has default value.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="maxStack"/> is out of range [0, <see cref="ushort.MaxValue"/>].</exception>
        /// <exception cref="InvalidOperationException">
        /// A label targeted by a branch in the instruction stream has not been marked,
        /// or the distance between a branch instruction and the target label is doesn't fit the size of the instruction operand.
        /// </exception>
        public int AddMethodBody(
            InstructionEncoder instructionEncoder, 
            int maxStack = 8,
            StandaloneSignatureHandle localVariablesSignature = default,
            MethodBodyAttributes attributes = MethodBodyAttributes.InitLocals,
            bool hasDynamicStackAllocation = false)
    }
}
@weshaggard
Copy link
Member

Looks good.

@tmat
Copy link
Member Author

tmat commented Mar 8, 2018

Implemented by dotnet/corefx#27589

@tmat tmat closed this as completed Mar 8, 2018
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Reflection.Metadata
Projects
None yet
Development

No branches or pull requests

3 participants