Instruction.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // Author:
  3. // Jb Evain (jbevain@gmail.com)
  4. //
  5. // Copyright (c) 2008 - 2015 Jb Evain
  6. // Copyright (c) 2008 - 2011 Novell, Inc.
  7. //
  8. // Licensed under the MIT/X11 license.
  9. //
  10. using System;
  11. using System.Text;
  12. namespace ILRuntime.Mono.Cecil.Cil {
  13. public sealed class Instruction {
  14. internal int offset;
  15. internal OpCode opcode;
  16. internal object operand;
  17. internal Instruction previous;
  18. internal Instruction next;
  19. public int Offset {
  20. get { return offset; }
  21. set { offset = value; }
  22. }
  23. public OpCode OpCode {
  24. get { return opcode; }
  25. set { opcode = value; }
  26. }
  27. public object Operand {
  28. get { return operand; }
  29. set { operand = value; }
  30. }
  31. public Instruction Previous {
  32. get { return previous; }
  33. set { previous = value; }
  34. }
  35. public Instruction Next {
  36. get { return next; }
  37. set { next = value; }
  38. }
  39. internal Instruction (int offset, OpCode opCode)
  40. {
  41. this.offset = offset;
  42. this.opcode = opCode;
  43. }
  44. internal Instruction (OpCode opcode, object operand)
  45. {
  46. this.opcode = opcode;
  47. this.operand = operand;
  48. }
  49. public int GetSize ()
  50. {
  51. int size = opcode.Size;
  52. switch (opcode.OperandType) {
  53. case OperandType.InlineSwitch:
  54. return size + (1 + ((Instruction []) operand).Length) * 4;
  55. case OperandType.InlineI8:
  56. case OperandType.InlineR:
  57. return size + 8;
  58. case OperandType.InlineBrTarget:
  59. case OperandType.InlineField:
  60. case OperandType.InlineI:
  61. case OperandType.InlineMethod:
  62. case OperandType.InlineString:
  63. case OperandType.InlineTok:
  64. case OperandType.InlineType:
  65. case OperandType.ShortInlineR:
  66. case OperandType.InlineSig:
  67. return size + 4;
  68. case OperandType.InlineArg:
  69. case OperandType.InlineVar:
  70. return size + 2;
  71. case OperandType.ShortInlineBrTarget:
  72. case OperandType.ShortInlineI:
  73. case OperandType.ShortInlineArg:
  74. case OperandType.ShortInlineVar:
  75. return size + 1;
  76. default:
  77. return size;
  78. }
  79. }
  80. public override string ToString ()
  81. {
  82. var instruction = new StringBuilder ();
  83. AppendLabel (instruction, this);
  84. instruction.Append (':');
  85. instruction.Append (' ');
  86. instruction.Append (opcode.Name);
  87. if (operand == null)
  88. return instruction.ToString ();
  89. instruction.Append (' ');
  90. switch (opcode.OperandType) {
  91. case OperandType.ShortInlineBrTarget:
  92. case OperandType.InlineBrTarget:
  93. AppendLabel (instruction, (Instruction) operand);
  94. break;
  95. case OperandType.InlineSwitch:
  96. var labels = (Instruction []) operand;
  97. for (int i = 0; i < labels.Length; i++) {
  98. if (i > 0)
  99. instruction.Append (',');
  100. AppendLabel (instruction, labels [i]);
  101. }
  102. break;
  103. case OperandType.InlineString:
  104. instruction.Append ('\"');
  105. instruction.Append (operand);
  106. instruction.Append ('\"');
  107. break;
  108. default:
  109. instruction.Append (operand);
  110. break;
  111. }
  112. return instruction.ToString ();
  113. }
  114. static void AppendLabel (StringBuilder builder, Instruction instruction)
  115. {
  116. builder.Append ("IL_");
  117. builder.Append (instruction.offset.ToString ("x4"));
  118. }
  119. public static Instruction Create (OpCode opcode)
  120. {
  121. if (opcode.OperandType != OperandType.InlineNone)
  122. throw new ArgumentException ("opcode");
  123. return new Instruction (opcode, null);
  124. }
  125. public static Instruction Create (OpCode opcode, TypeReference type)
  126. {
  127. if (type == null)
  128. throw new ArgumentNullException ("type");
  129. if (opcode.OperandType != OperandType.InlineType &&
  130. opcode.OperandType != OperandType.InlineTok)
  131. throw new ArgumentException ("opcode");
  132. return new Instruction (opcode, type);
  133. }
  134. public static Instruction Create (OpCode opcode, CallSite site)
  135. {
  136. if (site == null)
  137. throw new ArgumentNullException ("site");
  138. if (opcode.Code != Code.Calli)
  139. throw new ArgumentException ("code");
  140. return new Instruction (opcode, site);
  141. }
  142. public static Instruction Create (OpCode opcode, MethodReference method)
  143. {
  144. if (method == null)
  145. throw new ArgumentNullException ("method");
  146. if (opcode.OperandType != OperandType.InlineMethod &&
  147. opcode.OperandType != OperandType.InlineTok)
  148. throw new ArgumentException ("opcode");
  149. return new Instruction (opcode, method);
  150. }
  151. public static Instruction Create (OpCode opcode, FieldReference field)
  152. {
  153. if (field == null)
  154. throw new ArgumentNullException ("field");
  155. if (opcode.OperandType != OperandType.InlineField &&
  156. opcode.OperandType != OperandType.InlineTok)
  157. throw new ArgumentException ("opcode");
  158. return new Instruction (opcode, field);
  159. }
  160. public static Instruction Create (OpCode opcode, string value)
  161. {
  162. if (value == null)
  163. throw new ArgumentNullException ("value");
  164. if (opcode.OperandType != OperandType.InlineString)
  165. throw new ArgumentException ("opcode");
  166. return new Instruction (opcode, value);
  167. }
  168. public static Instruction Create (OpCode opcode, sbyte value)
  169. {
  170. if (opcode.OperandType != OperandType.ShortInlineI &&
  171. opcode != OpCodes.Ldc_I4_S)
  172. throw new ArgumentException ("opcode");
  173. return new Instruction (opcode, value);
  174. }
  175. public static Instruction Create (OpCode opcode, byte value)
  176. {
  177. if (opcode.OperandType != OperandType.ShortInlineI ||
  178. opcode == OpCodes.Ldc_I4_S)
  179. throw new ArgumentException ("opcode");
  180. return new Instruction (opcode, value);
  181. }
  182. public static Instruction Create (OpCode opcode, int value)
  183. {
  184. if (opcode.OperandType != OperandType.InlineI)
  185. throw new ArgumentException ("opcode");
  186. return new Instruction (opcode, value);
  187. }
  188. public static Instruction Create (OpCode opcode, long value)
  189. {
  190. if (opcode.OperandType != OperandType.InlineI8)
  191. throw new ArgumentException ("opcode");
  192. return new Instruction (opcode, value);
  193. }
  194. public static Instruction Create (OpCode opcode, float value)
  195. {
  196. if (opcode.OperandType != OperandType.ShortInlineR)
  197. throw new ArgumentException ("opcode");
  198. return new Instruction (opcode, value);
  199. }
  200. public static Instruction Create (OpCode opcode, double value)
  201. {
  202. if (opcode.OperandType != OperandType.InlineR)
  203. throw new ArgumentException ("opcode");
  204. return new Instruction (opcode, value);
  205. }
  206. public static Instruction Create (OpCode opcode, Instruction target)
  207. {
  208. if (target == null)
  209. throw new ArgumentNullException ("target");
  210. if (opcode.OperandType != OperandType.InlineBrTarget &&
  211. opcode.OperandType != OperandType.ShortInlineBrTarget)
  212. throw new ArgumentException ("opcode");
  213. return new Instruction (opcode, target);
  214. }
  215. public static Instruction Create (OpCode opcode, Instruction [] targets)
  216. {
  217. if (targets == null)
  218. throw new ArgumentNullException ("targets");
  219. if (opcode.OperandType != OperandType.InlineSwitch)
  220. throw new ArgumentException ("opcode");
  221. return new Instruction (opcode, targets);
  222. }
  223. public static Instruction Create (OpCode opcode, VariableDefinition variable)
  224. {
  225. if (variable == null)
  226. throw new ArgumentNullException ("variable");
  227. if (opcode.OperandType != OperandType.ShortInlineVar &&
  228. opcode.OperandType != OperandType.InlineVar)
  229. throw new ArgumentException ("opcode");
  230. return new Instruction (opcode, variable);
  231. }
  232. public static Instruction Create (OpCode opcode, ParameterDefinition parameter)
  233. {
  234. if (parameter == null)
  235. throw new ArgumentNullException ("parameter");
  236. if (opcode.OperandType != OperandType.ShortInlineArg &&
  237. opcode.OperandType != OperandType.InlineArg)
  238. throw new ArgumentException ("opcode");
  239. return new Instruction (opcode, parameter);
  240. }
  241. }
  242. }