/Mono.Cecil/Mono.Cecil.Cil/ILProcessor.cs

http://github.com/icsharpcode/ILSpy · C# · 278 lines · 196 code · 55 blank · 27 comment · 29 complexity · 0b00f7cd2c9784a2cd7d37b8c5970987 MD5 · raw file

  1. //
  2. // ILProcessor.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using Mono.Collections.Generic;
  30. namespace Mono.Cecil.Cil {
  31. public sealed class ILProcessor {
  32. readonly MethodBody body;
  33. readonly Collection<Instruction> instructions;
  34. public MethodBody Body {
  35. get { return body; }
  36. }
  37. internal ILProcessor (MethodBody body)
  38. {
  39. this.body = body;
  40. this.instructions = body.Instructions;
  41. }
  42. public Instruction Create (OpCode opcode)
  43. {
  44. return Instruction.Create (opcode);
  45. }
  46. public Instruction Create (OpCode opcode, TypeReference type)
  47. {
  48. return Instruction.Create (opcode, type);
  49. }
  50. public Instruction Create (OpCode opcode, CallSite site)
  51. {
  52. return Instruction.Create (opcode, site);
  53. }
  54. public Instruction Create (OpCode opcode, MethodReference method)
  55. {
  56. return Instruction.Create (opcode, method);
  57. }
  58. public Instruction Create (OpCode opcode, FieldReference field)
  59. {
  60. return Instruction.Create (opcode, field);
  61. }
  62. public Instruction Create (OpCode opcode, string value)
  63. {
  64. return Instruction.Create (opcode, value);
  65. }
  66. public Instruction Create (OpCode opcode, sbyte value)
  67. {
  68. return Instruction.Create (opcode, value);
  69. }
  70. public Instruction Create (OpCode opcode, byte value)
  71. {
  72. if (opcode.OperandType == OperandType.ShortInlineVar)
  73. return Instruction.Create (opcode, body.Variables [value]);
  74. if (opcode.OperandType == OperandType.ShortInlineArg)
  75. return Instruction.Create (opcode, body.GetParameter (value));
  76. return Instruction.Create (opcode, value);
  77. }
  78. public Instruction Create (OpCode opcode, int value)
  79. {
  80. if (opcode.OperandType == OperandType.InlineVar)
  81. return Instruction.Create (opcode, body.Variables [value]);
  82. if (opcode.OperandType == OperandType.InlineArg)
  83. return Instruction.Create (opcode, body.GetParameter (value));
  84. return Instruction.Create (opcode, value);
  85. }
  86. public Instruction Create (OpCode opcode, long value)
  87. {
  88. return Instruction.Create (opcode, value);
  89. }
  90. public Instruction Create (OpCode opcode, float value)
  91. {
  92. return Instruction.Create (opcode, value);
  93. }
  94. public Instruction Create (OpCode opcode, double value)
  95. {
  96. return Instruction.Create (opcode, value);
  97. }
  98. public Instruction Create (OpCode opcode, Instruction target)
  99. {
  100. return Instruction.Create (opcode, target);
  101. }
  102. public Instruction Create (OpCode opcode, Instruction [] targets)
  103. {
  104. return Instruction.Create (opcode, targets);
  105. }
  106. public Instruction Create (OpCode opcode, VariableDefinition variable)
  107. {
  108. return Instruction.Create (opcode, variable);
  109. }
  110. public Instruction Create (OpCode opcode, ParameterDefinition parameter)
  111. {
  112. return Instruction.Create (opcode, parameter);
  113. }
  114. public void Emit (OpCode opcode)
  115. {
  116. Append (Create (opcode));
  117. }
  118. public void Emit (OpCode opcode, TypeReference type)
  119. {
  120. Append (Create (opcode, type));
  121. }
  122. public void Emit (OpCode opcode, MethodReference method)
  123. {
  124. Append (Create (opcode, method));
  125. }
  126. public void Emit (OpCode opcode, CallSite site)
  127. {
  128. Append (Create (opcode, site));
  129. }
  130. public void Emit (OpCode opcode, FieldReference field)
  131. {
  132. Append (Create (opcode, field));
  133. }
  134. public void Emit (OpCode opcode, string value)
  135. {
  136. Append (Create (opcode, value));
  137. }
  138. public void Emit (OpCode opcode, byte value)
  139. {
  140. Append (Create (opcode, value));
  141. }
  142. public void Emit (OpCode opcode, sbyte value)
  143. {
  144. Append (Create (opcode, value));
  145. }
  146. public void Emit (OpCode opcode, int value)
  147. {
  148. Append (Create (opcode, value));
  149. }
  150. public void Emit (OpCode opcode, long value)
  151. {
  152. Append (Create (opcode, value));
  153. }
  154. public void Emit (OpCode opcode, float value)
  155. {
  156. Append (Create (opcode, value));
  157. }
  158. public void Emit (OpCode opcode, double value)
  159. {
  160. Append (Create (opcode, value));
  161. }
  162. public void Emit (OpCode opcode, Instruction target)
  163. {
  164. Append (Create (opcode, target));
  165. }
  166. public void Emit (OpCode opcode, Instruction [] targets)
  167. {
  168. Append (Create (opcode, targets));
  169. }
  170. public void Emit (OpCode opcode, VariableDefinition variable)
  171. {
  172. Append (Create (opcode, variable));
  173. }
  174. public void Emit (OpCode opcode, ParameterDefinition parameter)
  175. {
  176. Append (Create (opcode, parameter));
  177. }
  178. public void InsertBefore (Instruction target, Instruction instruction)
  179. {
  180. if (target == null)
  181. throw new ArgumentNullException ("target");
  182. if (instruction == null)
  183. throw new ArgumentNullException ("instruction");
  184. var index = instructions.IndexOf (target);
  185. if (index == -1)
  186. throw new ArgumentOutOfRangeException ("target");
  187. instructions.Insert (index, instruction);
  188. }
  189. public void InsertAfter (Instruction target, Instruction instruction)
  190. {
  191. if (target == null)
  192. throw new ArgumentNullException ("target");
  193. if (instruction == null)
  194. throw new ArgumentNullException ("instruction");
  195. var index = instructions.IndexOf (target);
  196. if (index == -1)
  197. throw new ArgumentOutOfRangeException ("target");
  198. instructions.Insert (index + 1, instruction);
  199. }
  200. public void Append (Instruction instruction)
  201. {
  202. if (instruction == null)
  203. throw new ArgumentNullException ("instruction");
  204. instructions.Add (instruction);
  205. }
  206. public void Replace (Instruction target, Instruction instruction)
  207. {
  208. if (target == null)
  209. throw new ArgumentNullException ("target");
  210. if (instruction == null)
  211. throw new ArgumentNullException ("instruction");
  212. InsertAfter (target, instruction);
  213. Remove (target);
  214. }
  215. public void Remove (Instruction instruction)
  216. {
  217. if (instruction == null)
  218. throw new ArgumentNullException ("instruction");
  219. if (!instructions.Remove (instruction))
  220. throw new ArgumentOutOfRangeException ("instruction");
  221. }
  222. }
  223. }