System.Xml.Xsl.IlGen.GenerateHelper.EmitUnconditionalBranch C# (CSharp) Method

EmitUnconditionalBranch() public method

Unconditional branch opcodes (OpCode.Br, OpCode.Br_S) can lead to unverifiable code in the following cases: # DEAD CODE CASE ldc_i4 1 # Stack depth == 1 br Label2 Label1: nop # Dead code, so IL rules assume stack depth == 0. This causes a verification error, # since next instruction has depth == 1 Label2: pop # Stack depth == 1 ret # LATE BRANCH CASE ldc_i4 1 # Stack depth == 1 br Label2 Label1: nop # Not dead code, but since branch comes from below, IL rules assume stack depth = 0. # This causes a verification error, since next instruction has depth == 1 Label2: pop # Stack depth == 1 ret Label3: br Label1 # Stack depth == 1 This method works around the above limitations by using Brtrue or Brfalse in the following way: ldc_i4 1 # Since this test is always true, this is a way of creating a path to the code that brtrue Label # follows the brtrue instruction. ldc_i4 1 # Since this test is always false, this is a way of creating a path to the code that brfalse Label # starts at Label. 1. If opcode == Brtrue or Brtrue_S, then 1 will be pushed and brtrue instruction will be generated. 2. If opcode == Brfalse or Brfalse_S, then 1 will be pushed and brfalse instruction will be generated. 3. If opcode == Br or Br_S, then a br instruction will be generated.
public EmitUnconditionalBranch ( OpCode opcode, Label lblTarget ) : void
opcode OpCode
lblTarget Label
return void
        public void EmitUnconditionalBranch(OpCode opcode, Label lblTarget)
        {
            if (!opcode.Equals(OpCodes.Br) && !opcode.Equals(OpCodes.Br_S))
            {
                Debug.Assert(opcode.Equals(OpCodes.Brtrue) || opcode.Equals(OpCodes.Brtrue_S) ||
                             opcode.Equals(OpCodes.Brfalse) || opcode.Equals(OpCodes.Brfalse_S));
                Emit(OpCodes.Ldc_I4_1);
            }

#if DEBUG
            if (XmlILTrace.IsEnabled)
                this.writerDump.WriteLine("  {0, -10} Label {1}", opcode.Name, this.symbols[lblTarget]);
#endif
            _ilgen.Emit(opcode, lblTarget);

            if (_lastSourceInfo != null && (opcode.Equals(OpCodes.Br) || opcode.Equals(OpCodes.Br_S)))
            {
                // Emit a "no source" sequence point, otherwise the following label will be preceded
                // with a dead Nop operation, which may lead to unverifiable code (SQLBUDT 423393).
                // We are guaranteed not to emit adjacent sequence points because Br or Br_S
                // instruction precedes this sequence point, and a Nop instruction precedes other
                // sequence points.
                MarkSequencePoint(SourceLineInfo.NoSource);
            }
        }
    }