PageRenderTime 34ms CodeModel.GetById 2ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

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