Repository: dotnet/runtime
File: src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCompiler.cs (C#)

Code before:
                LdindU2();

                // Subtract minValue to get 0-based index
                if (minValue != 0)
                {
                    Ldc(minValue);
                    Sub();
                }

                // Bounds check: if the value is outside the range, jump to noMatch
                // if ((uint)(ch - minValue) >= range) goto noMatchLabel;
                Dup();
                Ldc(range);
                Label boundsCheckPassed = DefineLabel();
                BltUnFar(boundsCheckPassed);
                Pop(); // Pop the duplicated value before jumping to noMatch
                BrFar(noMatchLabel);
                MarkLabel(boundsCheckPassed);

                // Emit the switch
                Switch(switchTable);

                // Fall-through after switch should never happen because the bounds check above
                // ensures the value is within [0, range) and the switch table has entries for all values.
                // This branch is just a safety net.
                BrFar(noMatchLabel);

                // Emit the code for each branch
                for (int i = 0; i < childCount; i++)
                {
                    MarkLabel(branchLabels[i]);
                    sliceStaticPos = startingTextSpanPos;

                    // This is used for atomic alternations and non-atomic alternations where no
                    // branch can backtrack. Failures should jump to the original done label.
                    // Set it before emitting the child so that any failing matches inside will
                    // jump to the right place.
                    doneLabel = originalDoneLabel;

                    RegexNode child = node.Child(i);
                    RegexNode? startingLiteralNode = child.FindStartingLiteralNode(allowZeroWidth: false);
                    Debug.Assert(startingLiteralNode is not null, "Unexpectedly couldn't find the branch starting node.");

                    // Emit the code for the branch, without the first character that was already matched in the switch.
                    switch (child.Kind)
                    {
                        case RegexNodeKind.One:
                        case RegexNodeKind.Set:
                            // The character was handled entirely by the switch. No additional matching is needed.
                            sliceStaticPos++;
                            break;

Review comment (refactor):
@copilot, why do we need the earlier bound check? Why not just let the switch fall through to here? That would then also simplify the need for the dup and the pop.

Code after:
                            }
                            break;

                        default:
                            EmitNode(child);
                            break;
                    }

                    // This is only ever used for alternations where no branch may backtrack
                    // (whether due to being atomic or simply because nothing in the branch
                    // can backtrack), so we can simply reset the doneLabel after emitting the
                    // child, as nothing will backtrack here (and we need to reset it so that
                    // all branches see the original).
                    doneLabel = originalDoneLabel;

                    // If we get here in the generated code, the branch completed successfully.
                    // Before jumping to the end, we need to zero out sliceStaticPos, so that no
                    // matter what the value is after the branch, whatever follows the alternate
                    // will see the same sliceStaticPos.
                    TransferSliceStaticPosToPos();
                    BrFar(matchLabel);
                }

                // Successfully completed the alternate.
                MarkLabel(matchLabel);
                Debug.Assert(sliceStaticPos == 0);

                // Creates a new Multi node with the first character sliced off
                static RegexNode CreateSlicedMulti(RegexNode multi)
                {
                    Debug.Assert(multi.Kind is RegexNodeKind.Multi, $"Expected a Multi node, got {multi.Kind}");
                    return multi.Str!.Length == 2 ?
                        new(RegexNodeKind.One, multi.Options, multi.Str[1]) :
                        new(RegexNodeKind.Multi, multi.Options, multi.Str.Substring(1));
                }
            }

            // Emits the code to handle a backreference.
            void EmitBackreference(RegexNode node)
            {
                Debug.Assert(node.Kind is RegexNodeKind.Backreference, $"Unexpected type: {node.Kind}");

                int capnum = RegexParser.MapCaptureNumber(node.M, _regexTree!.CaptureNumberSparseMapping);
                bool rtl = (node.Options & RegexOptions.RightToLeft) != 0;

                TransferSliceStaticPosToPos();

                Label backreferenceEnd = DefineLabel();

                // if (!base.IsMatched(capnum)) goto (ecmascript ? end : doneLabel);
                Ldthis();