Skip to content

Add InstructionEncoder.Switch to enable emitting switch instructions with LabelHandles #40546

Closed

Description

@SeeminglyScience

Background and Motivation

It doesn't look like there's a way to use InstructionEncoder.DefineLabel's LabelHandles with a switch instruction. I'm mainly asking if there is a way to do it that I'm missing, but if not here's a basic API proposal to enable it.

Proposed API

namespace System.Reflection.Metadata.Ecma335
{
    public readonly struct InstructionEncoder
    {
+       public void Switch(params LabelHandle[] label);
    }
}

Usage Examples

var ie = new InstructionEncoder(new BlobBuilder(), new ControlFlowBuilder());
var case0 = ie.DefineLabel();
var case1 = ie.DefineLabel();
var case2 = ie.DefineLabel();
var afterCases = ie.DefineLabel();

ie.OpCode(ILOpCode.Ldc_i4_0);
ie.Switch(case0, case1, case2);
ie.Branch(ILOpCode.Br_s, afterCases);

ie.MarkLabel(case0);
ie.OpCode(ILOpCode.Ldc_i4_0);
ie.OpCode(ILOpCode.Ret);

ie.MarkLabel(case1);
ie.OpCode(ILOpCode.Ldc_i4_1);
ie.OpCode(ILOpCode.Ret);

ie.MarkLabel(case2);
ie.OpCode(ILOpCode.Ldc_i4_2);
ie.OpCode(ILOpCode.Ret);

ie.MarkLabel(afterCases);
ie.OpCode(ILOpCode.Ldc_i4_s);
ie.CodeBuilder.WriteCompressedSignedInteger(10);
ie.OpCode(ILOpCode.Ret);

new MethodBodyStreamEncoder(new BlobBuilder())
    .AddMethodBody(ie, maxStack: 8, localVariablesSignature: default, MethodBodyAttributes.None);

Alternative Designs

Originally this proposal was based on emitting the label by itself:

namespace System.Reflection.Metadata.Ecma335
{
    public readonly struct InstructionEncoder
    {
+       public void Label(LabelHandle label);
    }
}

This won't work because the target offset is based on the end of the switch instruction, not the offset of the label.

Also for it to work with switch like intended, it would need to only emit int32 offsets. That's not super clear from the name and would generate bad IL if someone were to do OpCode(ILOpCode.Br_s); Label(label);, even if it were based on the label's offset.

Risks

Activity

  1. added
    api-suggestion
    on Aug 7, 2020
  2. added
    untriaged
    on Aug 7, 2020
  3. changed the title [-]Add InstructionEncoder.Label to emit LabelHandle offset for switch instructions[/-] [+]Add InstructionEncoder.Switch to enable emitting switch instructions with LabelHandles[/+] on Aug 9, 2020
  4. added this to the Future milestone on Aug 26, 2020
  5. removed
    untriaged
    on Aug 26, 2020
  6. modified the milestones: Future, 6.0.0 on Aug 26, 2020
  7. modified the milestones: 6.0.0, 7.0.0 on Jul 8, 2021
  8. teo-tsirpanis commented on Aug 5, 2021

    @teo-tsirpanis
    Contributor

    I think it should also have an overload for IReadOnlyCollection<LabelHandle>, to allow high-performance apps to reuse a List<LabelHandle>.

  9. ghost added
    needs-further-triage
    on Aug 5, 2021
  10. removed
    needs-further-triage
    on Aug 9, 2021
  11. 19 remaining items

  12. added
    api-approved
    on Aug 9, 2022
  13. self-assigned this
    on Aug 9, 2022
  14. teo-tsirpanis commented on Aug 9, 2022

    @teo-tsirpanis
    Contributor

    I will do it in a couple of months in time for 8.0.0, if someone else wants to do it, feel free to.

  15. August-Alm commented on Aug 15, 2022

    @August-Alm

    The original proposal had a clear usage example but the approved API leaves the intended functionality implicit. Am I correct in understanding the approved proposal as supposed to work in such a way that the originally suggested method is equivalent to the following InstructionEncoder method?

    public void SwitchOfLabelHandles(params LabelHandle[] labels)
    {
        var switchEncoder = Switch(labels.Length);
        foreach (var label in labels)
        {
            switchEncoder.Branch(label);
        }
    }
  16. teo-tsirpanis commented on Aug 15, 2022

    @teo-tsirpanis
    Contributor
  17. removed
    needs-further-triage
    on Aug 15, 2022
  18. modified the milestones: 8.0.0, Future on Sep 26, 2022
  19. ghost added
    in-pr
    on Oct 3, 2022
  20. modified the milestones: Future, 8.0.0 on Nov 7, 2022
  21. Repository owner moved this from Future to Done in Triage POD for Reflection, META, etc.on Dec 6, 2022
  22. ghost removed
    in-pr
    on Dec 6, 2022
  23. ghost locked as resolved and limited conversation to collaborators on Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Reflection.Metadata

Type

No type

Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions