5
// Jb Evain (jbevain@gmail.com)
7
// Copyright (c) 2008 - 2010 Jb Evain
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
31
using Mono.Collections.Generic;
33
namespace Mono.Cecil.Cil {
35
public sealed class ILProcessor {
37
readonly MethodBody body;
38
readonly Collection<Instruction> instructions;
40
public MethodBody Body {
44
internal ILProcessor (MethodBody body)
47
this.instructions = body.Instructions;
50
public Instruction Create (OpCode opcode)
52
return Instruction.Create (opcode);
55
public Instruction Create (OpCode opcode, TypeReference type)
57
return Instruction.Create (opcode, type);
60
public Instruction Create (OpCode opcode, CallSite site)
62
return Instruction.Create (opcode, site);
65
public Instruction Create (OpCode opcode, MethodReference method)
67
return Instruction.Create (opcode, method);
70
public Instruction Create (OpCode opcode, FieldReference field)
72
return Instruction.Create (opcode, field);
75
public Instruction Create (OpCode opcode, string value)
77
return Instruction.Create (opcode, value);
80
public Instruction Create (OpCode opcode, sbyte value)
82
return Instruction.Create (opcode, value);
85
public Instruction Create (OpCode opcode, byte value)
87
if (opcode.OperandType == OperandType.ShortInlineVar)
88
return Instruction.Create (opcode, body.Variables [value]);
90
if (opcode.OperandType == OperandType.ShortInlineArg)
91
return Instruction.Create (opcode, body.GetParameter (value));
93
return Instruction.Create (opcode, value);
96
public Instruction Create (OpCode opcode, int value)
98
if (opcode.OperandType == OperandType.InlineVar)
99
return Instruction.Create (opcode, body.Variables [value]);
101
if (opcode.OperandType == OperandType.InlineArg)
102
return Instruction.Create (opcode, body.GetParameter (value));
104
return Instruction.Create (opcode, value);
107
public Instruction Create (OpCode opcode, long value)
109
return Instruction.Create (opcode, value);
112
public Instruction Create (OpCode opcode, float value)
114
return Instruction.Create (opcode, value);
117
public Instruction Create (OpCode opcode, double value)
119
return Instruction.Create (opcode, value);
122
public Instruction Create (OpCode opcode, Instruction target)
124
return Instruction.Create (opcode, target);
127
public Instruction Create (OpCode opcode, Instruction [] targets)
129
return Instruction.Create (opcode, targets);
132
public Instruction Create (OpCode opcode, VariableDefinition variable)
134
return Instruction.Create (opcode, variable);
137
public Instruction Create (OpCode opcode, ParameterDefinition parameter)
139
return Instruction.Create (opcode, parameter);
142
public void Emit (OpCode opcode)
144
Append (Create (opcode));
147
public void Emit (OpCode opcode, TypeReference type)
149
Append (Create (opcode, type));
152
public void Emit (OpCode opcode, MethodReference method)
154
Append (Create (opcode, method));
157
public void Emit (OpCode opcode, CallSite site)
159
Append (Create (opcode, site));
162
public void Emit (OpCode opcode, FieldReference field)
164
Append (Create (opcode, field));
167
public void Emit (OpCode opcode, string value)
169
Append (Create (opcode, value));
172
public void Emit (OpCode opcode, byte value)
174
Append (Create (opcode, value));
177
public void Emit (OpCode opcode, sbyte value)
179
Append (Create (opcode, value));
182
public void Emit (OpCode opcode, int value)
184
Append (Create (opcode, value));
187
public void Emit (OpCode opcode, long value)
189
Append (Create (opcode, value));
192
public void Emit (OpCode opcode, float value)
194
Append (Create (opcode, value));
197
public void Emit (OpCode opcode, double value)
199
Append (Create (opcode, value));
202
public void Emit (OpCode opcode, Instruction target)
204
Append (Create (opcode, target));
207
public void Emit (OpCode opcode, Instruction [] targets)
209
Append (Create (opcode, targets));
212
public void Emit (OpCode opcode, VariableDefinition variable)
214
Append (Create (opcode, variable));
217
public void Emit (OpCode opcode, ParameterDefinition parameter)
219
Append (Create (opcode, parameter));
222
public void InsertBefore (Instruction target, Instruction instruction)
225
throw new ArgumentNullException ("target");
226
if (instruction == null)
227
throw new ArgumentNullException ("instruction");
229
var index = instructions.IndexOf (target);
231
throw new ArgumentOutOfRangeException ("target");
233
instructions.Insert (index, instruction);
236
public void InsertAfter (Instruction target, Instruction instruction)
239
throw new ArgumentNullException ("target");
240
if (instruction == null)
241
throw new ArgumentNullException ("instruction");
243
var index = instructions.IndexOf (target);
245
throw new ArgumentOutOfRangeException ("target");
247
instructions.Insert (index + 1, instruction);
250
public void Append (Instruction instruction)
252
if (instruction == null)
253
throw new ArgumentNullException ("instruction");
255
instructions.Add (instruction);
258
public void Replace (Instruction target, Instruction instruction)
261
throw new ArgumentNullException ("target");
262
if (instruction == null)
263
throw new ArgumentNullException ("instruction");
265
InsertAfter (target, instruction);
269
public void Remove (Instruction instruction)
271
if (instruction == null)
272
throw new ArgumentNullException ("instruction");
274
if (!instructions.Remove (instruction))
275
throw new ArgumentOutOfRangeException ("instruction");