/// <summary>
        /// Verifies and inserts a branch instruction into the collection.
        /// </summary>
        /// <param name="index">The zero-based index at which the instruction should be inserted at.</param>
        /// <param name="code">The branch opcode.</param>
        /// <param name="label">The label referenced by the branch instruction.</param>
        /// <returns>The created instruction.</returns>
        /// <exception cref="InvalidCilInstructionException">
        /// Occurs when the provided operation is not a branch opcode.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Occurs when <paramref name="label"/> is null.
        /// </exception>
        public CilInstruction Insert(int index, CilOpCode code, ICilLabel label)
        {
            if (code.OperandType != CilOperandType.InlineBrTarget && code.OperandType != CilOperandType.ShortInlineBrTarget)
            {
                throw new InvalidCilInstructionException(code);
            }
            if (label is null)
            {
                throw new ArgumentNullException(nameof(label));
            }

            return(InsertAndReturn(index, code, label));
        }
Ejemplo n.º 2
0
        private void VerifyBranchLabel(int offset, ICilLabel label)
        {
            switch (label)
            {
            case null:
                AddDiagnostic($"Branch target of IL_{offset:X4} is null.");
                break;

            case CilInstructionLabel {
                    Instruction: { } instruction
            } :
                if (!IsPresentInBody(instruction))
                {
                    AddDiagnostic($"IL_{offset:X4} references an instruction that is not present in the method body.");
                }
                break;
 /// <inheritdoc />
 public bool Equals(ICilLabel other) => other != null && Offset == other.Offset;
 public CilInstruction Add(CilOpCode code, ICilLabel label) => Insert(Count, code, label);
Ejemplo n.º 5
0
        private static ICilLabel TryResolveLabel(IList <CilInstruction> instructions, ICilLabel label)
        {
            int index = instructions.GetIndexByOffset(label.Offset);

            if (index != -1)
            {
                label = instructions[index].CreateLabel();
            }
            return(label);
        }