1. roundcrisis
  2. CodeShape

Source

CodeShape / src / CodeShapes / Disassembler / Instruction.cs

using System.Reflection.Emit;

namespace CodeShapes.Disassembler
{
	internal sealed class Instruction
	{
		readonly int _offset;
		OpCode _opcode;
		object _operand;

		public int Offset
		{
			get { return _offset; }
		}

		public OpCode OpCode
		{
			get { return _opcode; }
		}

		public object Operand
		{
			get { return _operand; }
			internal set { _operand = value; }
		}

		public Instruction Previous { get; internal set; }

		public Instruction Next { get; internal set; }

		internal Instruction(int offset, OpCode opcode)
		{
			_offset = offset;
			_opcode = opcode;
		}

		public int GetSize()
		{
			int size = _opcode.Size;

			switch (_opcode.OperandType)
			{
				case OperandType.InlineSwitch:
					size += (1 + ((int[])_operand).Length) * 4;
					break;
				case OperandType.InlineI8:
				case OperandType.InlineR:
					size += 8;
					break;
				case OperandType.InlineBrTarget:
				case OperandType.InlineField:
				case OperandType.InlineI:
				case OperandType.InlineMethod:
				case OperandType.InlineString:
				case OperandType.InlineTok:
				case OperandType.InlineType:
				case OperandType.ShortInlineR:
					size += 4;
					break;
				case OperandType.InlineVar:
					size += 2;
					break;
				case OperandType.ShortInlineBrTarget:
				case OperandType.ShortInlineI:
				case OperandType.ShortInlineVar:
					size += 1;
					break;
			}

			return size;
		}

		public override string ToString()
		{
			return _opcode.Name;
		}
	}
}