Beispiel #1
0
        /// <summary>
        /// Determines the smallest possible operation code and operand required to push the provided integer constant.
        /// </summary>
        /// <param name="value">The constant to push.</param>
        /// <returns>The operation code and operand.</returns>
        public static (CilOpCode code, object operand) GetLdcI4OpCodeOperand(int value)
        {
            CilOpCode code;
            object    operand = null;

            switch (value)
            {
            case -1:
                code = CilOpCodes.Ldc_I4_M1;
                break;

            case 0:
                code = CilOpCodes.Ldc_I4_0;
                break;

            case 1:
                code = CilOpCodes.Ldc_I4_1;
                break;

            case 2:
                code = CilOpCodes.Ldc_I4_2;
                break;

            case 3:
                code = CilOpCodes.Ldc_I4_3;
                break;

            case 4:
                code = CilOpCodes.Ldc_I4_4;
                break;

            case 5:
                code = CilOpCodes.Ldc_I4_5;
                break;

            case 6:
                code = CilOpCodes.Ldc_I4_6;
                break;

            case 7:
                code = CilOpCodes.Ldc_I4_7;
                break;

            case 8:
                code = CilOpCodes.Ldc_I4_8;
                break;

            case { } x when x >= sbyte.MinValue && x <= sbyte.MaxValue:
                code = CilOpCodes.Ldc_I4_S;
                operand = (sbyte)x;
                break;

            default:
                code    = CilOpCodes.Ldc_I4;
                operand = value;
                break;
            }

            return(code, operand);
        }
Beispiel #2
0
 private void WriteOpCode(CilOpCode opCode)
 {
     _writer.WriteByte(opCode.Byte1);
     if (opCode.IsLarge)
     {
         _writer.WriteByte(opCode.Byte2);
     }
 }
Beispiel #3
0
 /// <summary>
 /// Creates a new CIL instruction with an operand..
 /// </summary>
 /// <param name="offset">The offset of the instruction, relative to the start of the method body's code.</param>
 /// <param name="opCode">The operation to perform.</param>
 /// <param name="operand">The operand.</param>
 /// <remarks>
 /// This constructor does not do any verification on the correctness of the instruction.
 /// </remarks>
 public CilInstruction(int offset, CilOpCode opCode, object operand)
 {
     Offset  = offset;
     OpCode  = opCode;
     Operand = operand;
 }
Beispiel #4
0
 /// <summary>
 /// Creates a new CIL instruction with an operand..
 /// </summary>
 /// <param name="opCode">The operation to perform.</param>
 /// <param name="operand">The operand.</param>
 /// <remarks>
 /// This constructor does not do any verification on the correctness of the instruction.
 /// </remarks>
 public CilInstruction(CilOpCode opCode, object operand)
     : this(0, opCode, operand)
 {
 }
Beispiel #5
0
 /// <summary>
 /// Creates a new CIL instruction with no operand.
 /// </summary>
 /// <param name="offset">The offset of the instruction, relative to the start of the method body's code.</param>
 /// <param name="opCode">The operation to perform.</param>
 /// <remarks>
 /// This constructor does not do any verification on the correctness of the instruction.
 /// </remarks>
 public CilInstruction(int offset, CilOpCode opCode)
     : this(offset, opCode, null)
 {
 }
Beispiel #6
0
 /// <summary>
 /// Creates a new CIL instruction with no operand.
 /// </summary>
 /// <param name="opCode">The operation to perform.</param>
 /// <remarks>
 /// This constructor does not do any verification on the correctness of the instruction.
 /// </remarks>
 public CilInstruction(CilOpCode opCode)
     : this(0, opCode, null)
 {
 }