/Mono.Cecil.Cil/ILProcessor.cs

http://github.com/jbevain/cecil · C# · 287 lines · 217 code · 61 blank · 9 comment · 37 complexity · 858822a182c074668781a1ec2deb3df8 MD5 · raw file

  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 Mono.Collections.Generic;
  12. namespace Mono.Cecil.Cil {
  13. public sealed class ILProcessor {
  14. readonly MethodBody body;
  15. readonly Collection<Instruction> instructions;
  16. public MethodBody Body {
  17. get { return body; }
  18. }
  19. internal ILProcessor (MethodBody body)
  20. {
  21. this.body = body;
  22. this.instructions = body.Instructions;
  23. }
  24. public Instruction Create (OpCode opcode)
  25. {
  26. return Instruction.Create (opcode);
  27. }
  28. public Instruction Create (OpCode opcode, TypeReference type)
  29. {
  30. return Instruction.Create (opcode, type);
  31. }
  32. public Instruction Create (OpCode opcode, CallSite site)
  33. {
  34. return Instruction.Create (opcode, site);
  35. }
  36. public Instruction Create (OpCode opcode, MethodReference method)
  37. {
  38. return Instruction.Create (opcode, method);
  39. }
  40. public Instruction Create (OpCode opcode, FieldReference field)
  41. {
  42. return Instruction.Create (opcode, field);
  43. }
  44. public Instruction Create (OpCode opcode, string value)
  45. {
  46. return Instruction.Create (opcode, value);
  47. }
  48. public Instruction Create (OpCode opcode, sbyte value)
  49. {
  50. return Instruction.Create (opcode, value);
  51. }
  52. public Instruction Create (OpCode opcode, byte value)
  53. {
  54. if (opcode.OperandType == OperandType.ShortInlineVar)
  55. return Instruction.Create (opcode, body.Variables [value]);
  56. if (opcode.OperandType == OperandType.ShortInlineArg)
  57. return Instruction.Create (opcode, body.GetParameter (value));
  58. return Instruction.Create (opcode, value);
  59. }
  60. public Instruction Create (OpCode opcode, int value)
  61. {
  62. if (opcode.OperandType == OperandType.InlineVar)
  63. return Instruction.Create (opcode, body.Variables [value]);
  64. if (opcode.OperandType == OperandType.InlineArg)
  65. return Instruction.Create (opcode, body.GetParameter (value));
  66. return Instruction.Create (opcode, value);
  67. }
  68. public Instruction Create (OpCode opcode, long value)
  69. {
  70. return Instruction.Create (opcode, value);
  71. }
  72. public Instruction Create (OpCode opcode, float value)
  73. {
  74. return Instruction.Create (opcode, value);
  75. }
  76. public Instruction Create (OpCode opcode, double value)
  77. {
  78. return Instruction.Create (opcode, value);
  79. }
  80. public Instruction Create (OpCode opcode, Instruction target)
  81. {
  82. return Instruction.Create (opcode, target);
  83. }
  84. public Instruction Create (OpCode opcode, Instruction [] targets)
  85. {
  86. return Instruction.Create (opcode, targets);
  87. }
  88. public Instruction Create (OpCode opcode, VariableDefinition variable)
  89. {
  90. return Instruction.Create (opcode, variable);
  91. }
  92. public Instruction Create (OpCode opcode, ParameterDefinition parameter)
  93. {
  94. return Instruction.Create (opcode, parameter);
  95. }
  96. public void Emit (OpCode opcode)
  97. {
  98. Append (Create (opcode));
  99. }
  100. public void Emit (OpCode opcode, TypeReference type)
  101. {
  102. Append (Create (opcode, type));
  103. }
  104. public void Emit (OpCode opcode, MethodReference method)
  105. {
  106. Append (Create (opcode, method));
  107. }
  108. public void Emit (OpCode opcode, CallSite site)
  109. {
  110. Append (Create (opcode, site));
  111. }
  112. public void Emit (OpCode opcode, FieldReference field)
  113. {
  114. Append (Create (opcode, field));
  115. }
  116. public void Emit (OpCode opcode, string value)
  117. {
  118. Append (Create (opcode, value));
  119. }
  120. public void Emit (OpCode opcode, byte value)
  121. {
  122. Append (Create (opcode, value));
  123. }
  124. public void Emit (OpCode opcode, sbyte value)
  125. {
  126. Append (Create (opcode, value));
  127. }
  128. public void Emit (OpCode opcode, int value)
  129. {
  130. Append (Create (opcode, value));
  131. }
  132. public void Emit (OpCode opcode, long value)
  133. {
  134. Append (Create (opcode, value));
  135. }
  136. public void Emit (OpCode opcode, float value)
  137. {
  138. Append (Create (opcode, value));
  139. }
  140. public void Emit (OpCode opcode, double value)
  141. {
  142. Append (Create (opcode, value));
  143. }
  144. public void Emit (OpCode opcode, Instruction target)
  145. {
  146. Append (Create (opcode, target));
  147. }
  148. public void Emit (OpCode opcode, Instruction [] targets)
  149. {
  150. Append (Create (opcode, targets));
  151. }
  152. public void Emit (OpCode opcode, VariableDefinition variable)
  153. {
  154. Append (Create (opcode, variable));
  155. }
  156. public void Emit (OpCode opcode, ParameterDefinition parameter)
  157. {
  158. Append (Create (opcode, parameter));
  159. }
  160. public void InsertBefore (Instruction target, Instruction instruction)
  161. {
  162. if (target == null)
  163. throw new ArgumentNullException ("target");
  164. if (instruction == null)
  165. throw new ArgumentNullException ("instruction");
  166. var index = instructions.IndexOf (target);
  167. if (index == -1)
  168. throw new ArgumentOutOfRangeException ("target");
  169. instructions.Insert (index, instruction);
  170. }
  171. public void InsertAfter (Instruction target, Instruction instruction)
  172. {
  173. if (target == null)
  174. throw new ArgumentNullException ("target");
  175. if (instruction == null)
  176. throw new ArgumentNullException ("instruction");
  177. var index = instructions.IndexOf (target);
  178. if (index == -1)
  179. throw new ArgumentOutOfRangeException ("target");
  180. instructions.Insert (index + 1, instruction);
  181. }
  182. public void InsertAfter (int index, Instruction instruction)
  183. {
  184. if (index < 0 || index >= instructions.Count)
  185. throw new ArgumentOutOfRangeException ("index");
  186. if (instruction == null)
  187. throw new ArgumentNullException ("instruction");
  188. instructions.Insert (index + 1, instruction);
  189. }
  190. public void Append (Instruction instruction)
  191. {
  192. if (instruction == null)
  193. throw new ArgumentNullException ("instruction");
  194. instructions.Add (instruction);
  195. }
  196. public void Replace (Instruction target, Instruction instruction)
  197. {
  198. if (target == null)
  199. throw new ArgumentNullException ("target");
  200. if (instruction == null)
  201. throw new ArgumentNullException ("instruction");
  202. InsertAfter (target, instruction);
  203. Remove (target);
  204. }
  205. public void Replace (int index, Instruction instruction)
  206. {
  207. if (instruction == null)
  208. throw new ArgumentNullException ("instruction");
  209. InsertAfter (index, instruction);
  210. RemoveAt (index);
  211. }
  212. public void Remove (Instruction instruction)
  213. {
  214. if (instruction == null)
  215. throw new ArgumentNullException ("instruction");
  216. if (!instructions.Remove (instruction))
  217. throw new ArgumentOutOfRangeException ("instruction");
  218. }
  219. public void RemoveAt (int index)
  220. {
  221. if (index < 0 || index >= instructions.Count)
  222. throw new ArgumentOutOfRangeException ("index");
  223. instructions.RemoveAt (index);
  224. }
  225. }
  226. }