Ejemplo n.º 1
0
        private int OperandToBranchDelta(CilInstruction instruction)
        {
            bool isShort = instruction.OpCode.OperandType == CilOperandType.ShortInlineBrTarget;

            int delta;

            switch (instruction.Operand)
            {
            case sbyte x:
                delta = x;
                break;

            case int x:
                delta = x;
                break;

            case ICilLabel label:
                int operandSize = isShort ? sizeof(sbyte) : sizeof(int);
                delta = label.Offset - (int)(_writer.Offset + (ulong)operandSize);
                break;

            default:
                return(ThrowInvalidOperandType <sbyte>(instruction, typeof(ICilLabel), typeof(sbyte)));
            }

            if (isShort && (delta < sbyte.MinValue || delta > sbyte.MaxValue))
            {
                _errorListener.RegisterException(new OverflowException(
                                                     $"{_diagnosticPrefix}Branch target at IL_{instruction.Offset:X4} is too far away for a ShortInlineBr instruction."));
            }

            return(delta);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Registers an error, and returns a default value for the provided type.
 /// </summary>
 /// <param name="self">The error listener.</param>
 /// <param name="exception">The error.</param>
 /// <typeparam name="T">The type of value to return.</typeparam>
 public static T RegisterExceptionAndReturnDefault <T>(this IErrorListener self, Exception exception)
 {
     self.RegisterException(exception);
     return(default);
Ejemplo n.º 3
0
 /// <summary>
 /// Registers an instance of a <see cref="BadImageFormatException"/> class.
 /// </summary>
 /// <param name="self">The error listener.</param>
 /// <param name="message">The message of the error.</param>
 public static void BadImage(this IErrorListener self, string message)
 {
     self.RegisterException(new BadImageFormatException(message));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Registers an instance of a <see cref="NotSupportedException"/> class.
 /// </summary>
 /// <param name="self">The error listener.</param>
 /// <param name="message">The message of the error.</param>
 public static void NotSupported(this IErrorListener self, string message)
 {
     self.RegisterException(new NotSupportedException(message));
 }
Ejemplo n.º 5
0
 public static void MetadataBuilder(this IErrorListener listener, string message)
 {
     listener.RegisterException(new MetadataBuilderException(message));
 }