| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 | 
							- //
 
- // Author:
 
- //   Jb Evain (jbevain@gmail.com)
 
- //
 
- // Copyright (c) 2008 - 2015 Jb Evain
 
- // Copyright (c) 2008 - 2011 Novell, Inc.
 
- //
 
- // Licensed under the MIT/X11 license.
 
- //
 
- using System;
 
- using System.Text;
 
- namespace ILRuntime.Mono.Cecil.Cil {
 
- 	public sealed class Instruction {
 
- 		internal int offset;
 
- 		internal OpCode opcode;
 
- 		internal object operand;
 
- 		internal Instruction previous;
 
- 		internal Instruction next;
 
- 		public int Offset {
 
- 			get { return offset; }
 
- 			set { offset = value; }
 
- 		}
 
- 		public OpCode OpCode {
 
- 			get { return opcode; }
 
- 			set { opcode = value; }
 
- 		}
 
- 		public object Operand {
 
- 			get { return operand; }
 
- 			set { operand = value; }
 
- 		}
 
- 		public Instruction Previous {
 
- 			get { return previous; }
 
- 			set { previous = value; }
 
- 		}
 
- 		public Instruction Next {
 
- 			get { return next; }
 
- 			set { next = value; }
 
- 		}
 
- 		internal Instruction (int offset, OpCode opCode)
 
- 		{
 
- 			this.offset = offset;
 
- 			this.opcode = opCode;
 
- 		}
 
- 		internal Instruction (OpCode opcode, object operand)
 
- 		{
 
- 			this.opcode = opcode;
 
- 			this.operand = operand;
 
- 		}
 
- 		public int GetSize ()
 
- 		{
 
- 			int size = opcode.Size;
 
- 			switch (opcode.OperandType) {
 
- 			case OperandType.InlineSwitch:
 
- 				return size + (1 + ((Instruction []) operand).Length) * 4;
 
- 			case OperandType.InlineI8:
 
- 			case OperandType.InlineR:
 
- 				return size + 8;
 
- 			case OperandType.InlineBrTarget:
 
- 			case OperandType.InlineField:
 
- 			case OperandType.InlineI:
 
- 			case OperandType.InlineMethod:
 
- 			case OperandType.InlineString:
 
- 			case OperandType.InlineTok:
 
- 			case OperandType.InlineType:
 
- 			case OperandType.ShortInlineR:
 
- 			case OperandType.InlineSig:
 
- 				return size + 4;
 
- 			case OperandType.InlineArg:
 
- 			case OperandType.InlineVar:
 
- 				return size + 2;
 
- 			case OperandType.ShortInlineBrTarget:
 
- 			case OperandType.ShortInlineI:
 
- 			case OperandType.ShortInlineArg:
 
- 			case OperandType.ShortInlineVar:
 
- 				return size + 1;
 
- 			default:
 
- 				return size;
 
- 			}
 
- 		}
 
- 		public override string ToString ()
 
- 		{
 
- 			var instruction = new StringBuilder ();
 
- 			AppendLabel (instruction, this);
 
- 			instruction.Append (':');
 
- 			instruction.Append (' ');
 
- 			instruction.Append (opcode.Name);
 
- 			if (operand == null)
 
- 				return instruction.ToString ();
 
- 			instruction.Append (' ');
 
- 			switch (opcode.OperandType) {
 
- 			case OperandType.ShortInlineBrTarget:
 
- 			case OperandType.InlineBrTarget:
 
- 				AppendLabel (instruction, (Instruction) operand);
 
- 				break;
 
- 			case OperandType.InlineSwitch:
 
- 				var labels = (Instruction []) operand;
 
- 				for (int i = 0; i < labels.Length; i++) {
 
- 					if (i > 0)
 
- 						instruction.Append (',');
 
- 					AppendLabel (instruction, labels [i]);
 
- 				}
 
- 				break;
 
- 			case OperandType.InlineString:
 
- 				instruction.Append ('\"');
 
- 				instruction.Append (operand);
 
- 				instruction.Append ('\"');
 
- 				break;
 
- 			default:
 
- 				instruction.Append (operand);
 
- 				break;
 
- 			}
 
- 			return instruction.ToString ();
 
- 		}
 
- 		static void AppendLabel (StringBuilder builder, Instruction instruction)
 
- 		{
 
- 			builder.Append ("IL_");
 
- 			builder.Append (instruction.offset.ToString ("x4"));
 
- 		}
 
- 		public static Instruction Create (OpCode opcode)
 
- 		{
 
- 			if (opcode.OperandType != OperandType.InlineNone)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, null);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, TypeReference type)
 
- 		{
 
- 			if (type == null)
 
- 				throw new ArgumentNullException ("type");
 
- 			if (opcode.OperandType != OperandType.InlineType &&
 
- 				opcode.OperandType != OperandType.InlineTok)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, type);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, CallSite site)
 
- 		{
 
- 			if (site == null)
 
- 				throw new ArgumentNullException ("site");
 
- 			if (opcode.Code != Code.Calli)
 
- 				throw new ArgumentException ("code");
 
- 			return new Instruction (opcode, site);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, MethodReference method)
 
- 		{
 
- 			if (method == null)
 
- 				throw new ArgumentNullException ("method");
 
- 			if (opcode.OperandType != OperandType.InlineMethod &&
 
- 				opcode.OperandType != OperandType.InlineTok)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, method);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, FieldReference field)
 
- 		{
 
- 			if (field == null)
 
- 				throw new ArgumentNullException ("field");
 
- 			if (opcode.OperandType != OperandType.InlineField &&
 
- 				opcode.OperandType != OperandType.InlineTok)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, field);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, string value)
 
- 		{
 
- 			if (value == null)
 
- 				throw new ArgumentNullException ("value");
 
- 			if (opcode.OperandType != OperandType.InlineString)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, value);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, sbyte value)
 
- 		{
 
- 			if (opcode.OperandType != OperandType.ShortInlineI &&
 
- 				opcode != OpCodes.Ldc_I4_S)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, value);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, byte value)
 
- 		{
 
- 			if (opcode.OperandType != OperandType.ShortInlineI ||
 
- 				opcode == OpCodes.Ldc_I4_S)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, value);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, int value)
 
- 		{
 
- 			if (opcode.OperandType != OperandType.InlineI)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, value);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, long value)
 
- 		{
 
- 			if (opcode.OperandType != OperandType.InlineI8)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, value);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, float value)
 
- 		{
 
- 			if (opcode.OperandType != OperandType.ShortInlineR)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, value);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, double value)
 
- 		{
 
- 			if (opcode.OperandType != OperandType.InlineR)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, value);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, Instruction target)
 
- 		{
 
- 			if (target == null)
 
- 				throw new ArgumentNullException ("target");
 
- 			if (opcode.OperandType != OperandType.InlineBrTarget &&
 
- 				opcode.OperandType != OperandType.ShortInlineBrTarget)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, target);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, Instruction [] targets)
 
- 		{
 
- 			if (targets == null)
 
- 				throw new ArgumentNullException ("targets");
 
- 			if (opcode.OperandType != OperandType.InlineSwitch)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, targets);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, VariableDefinition variable)
 
- 		{
 
- 			if (variable == null)
 
- 				throw new ArgumentNullException ("variable");
 
- 			if (opcode.OperandType != OperandType.ShortInlineVar &&
 
- 				opcode.OperandType != OperandType.InlineVar)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, variable);
 
- 		}
 
- 		public static Instruction Create (OpCode opcode, ParameterDefinition parameter)
 
- 		{
 
- 			if (parameter == null)
 
- 				throw new ArgumentNullException ("parameter");
 
- 			if (opcode.OperandType != OperandType.ShortInlineArg &&
 
- 				opcode.OperandType != OperandType.InlineArg)
 
- 				throw new ArgumentException ("opcode");
 
- 			return new Instruction (opcode, parameter);
 
- 		}
 
- 	}
 
- }
 
 
  |