5 // Jb Evain (jbevain@gmail.com)
7 // Copyright (c) 2008 - 2011 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.
29 namespace Mono.Cecil.Cil {
31 public enum FlowControl {
43 public enum OpCodeType {
52 public enum OperandType {
75 public enum StackBehaviour {
107 public struct OpCode {
112 readonly byte flow_control;
113 readonly byte opcode_type;
114 readonly byte operand_type;
115 readonly byte stack_behavior_pop;
116 readonly byte stack_behavior_push;
119 get { return OpCodeNames.names [(int) Code]; }
123 get { return op1 == 0xff ? 1 : 2; }
135 get { return op1 == 0xff ? op2 : (short) ((op1 << 8) | op2); }
139 get { return (Code) code; }
142 public FlowControl FlowControl {
143 get { return (FlowControl) flow_control; }
146 public OpCodeType OpCodeType {
147 get { return (OpCodeType) opcode_type; }
150 public OperandType OperandType {
151 get { return (OperandType) operand_type; }
154 public StackBehaviour StackBehaviourPop {
155 get { return (StackBehaviour) stack_behavior_pop; }
158 public StackBehaviour StackBehaviourPush {
159 get { return (StackBehaviour) stack_behavior_push; }
162 internal OpCode (int x, int y)
164 this.op1 = (byte) ((x >> 0) & 0xff);
165 this.op2 = (byte) ((x >> 8) & 0xff);
166 this.code = (byte) ((x >> 16) & 0xff);
167 this.flow_control = (byte) ((x >> 24) & 0xff);
169 this.opcode_type = (byte) ((y >> 0) & 0xff);
170 this.operand_type = (byte) ((y >> 8) & 0xff);
171 this.stack_behavior_pop = (byte) ((y >> 16) & 0xff);
172 this.stack_behavior_push = (byte) ((y >> 24) & 0xff);
175 OpCodes.OneByteOpCode [op2] = this;
177 OpCodes.TwoBytesOpCode [op2] = this;
180 public override int GetHashCode ()
185 public override bool Equals (object obj)
187 if (!(obj is OpCode))
190 var opcode = (OpCode) obj;
191 return op1 == opcode.op1 && op2 == opcode.op2;
194 public bool Equals (OpCode opcode)
196 return op1 == opcode.op1 && op2 == opcode.op2;
199 public static bool operator == (OpCode one, OpCode other)
201 return one.op1 == other.op1 && one.op2 == other.op2;
204 public static bool operator != (OpCode one, OpCode other)
206 return one.op1 != other.op1 || one.op2 != other.op2;
209 public override string ToString ()
215 static class OpCodeNames {
217 internal static readonly string [] names;
219 static OpCodeNames ()
221 var table = new byte [] {
223 5, 98, 114, 101, 97, 107,
224 7, 108, 100, 97, 114, 103, 46, 48,
225 7, 108, 100, 97, 114, 103, 46, 49,
226 7, 108, 100, 97, 114, 103, 46, 50,
227 7, 108, 100, 97, 114, 103, 46, 51,
228 7, 108, 100, 108, 111, 99, 46, 48,
229 7, 108, 100, 108, 111, 99, 46, 49,
230 7, 108, 100, 108, 111, 99, 46, 50,
231 7, 108, 100, 108, 111, 99, 46, 51,
232 7, 115, 116, 108, 111, 99, 46, 48,
233 7, 115, 116, 108, 111, 99, 46, 49,
234 7, 115, 116, 108, 111, 99, 46, 50,
235 7, 115, 116, 108, 111, 99, 46, 51,
236 7, 108, 100, 97, 114, 103, 46, 115,
237 8, 108, 100, 97, 114, 103, 97, 46, 115,
238 7, 115, 116, 97, 114, 103, 46, 115,
239 7, 108, 100, 108, 111, 99, 46, 115,
240 8, 108, 100, 108, 111, 99, 97, 46, 115,
241 7, 115, 116, 108, 111, 99, 46, 115,
242 6, 108, 100, 110, 117, 108, 108,
243 9, 108, 100, 99, 46, 105, 52, 46, 109, 49,
244 8, 108, 100, 99, 46, 105, 52, 46, 48,
245 8, 108, 100, 99, 46, 105, 52, 46, 49,
246 8, 108, 100, 99, 46, 105, 52, 46, 50,
247 8, 108, 100, 99, 46, 105, 52, 46, 51,
248 8, 108, 100, 99, 46, 105, 52, 46, 52,
249 8, 108, 100, 99, 46, 105, 52, 46, 53,
250 8, 108, 100, 99, 46, 105, 52, 46, 54,
251 8, 108, 100, 99, 46, 105, 52, 46, 55,
252 8, 108, 100, 99, 46, 105, 52, 46, 56,
253 8, 108, 100, 99, 46, 105, 52, 46, 115,
254 6, 108, 100, 99, 46, 105, 52,
255 6, 108, 100, 99, 46, 105, 56,
256 6, 108, 100, 99, 46, 114, 52,
257 6, 108, 100, 99, 46, 114, 56,
262 5, 99, 97, 108, 108, 105,
265 9, 98, 114, 102, 97, 108, 115, 101, 46, 115,
266 8, 98, 114, 116, 114, 117, 101, 46, 115,
267 5, 98, 101, 113, 46, 115,
268 5, 98, 103, 101, 46, 115,
269 5, 98, 103, 116, 46, 115,
270 5, 98, 108, 101, 46, 115,
271 5, 98, 108, 116, 46, 115,
272 8, 98, 110, 101, 46, 117, 110, 46, 115,
273 8, 98, 103, 101, 46, 117, 110, 46, 115,
274 8, 98, 103, 116, 46, 117, 110, 46, 115,
275 8, 98, 108, 101, 46, 117, 110, 46, 115,
276 8, 98, 108, 116, 46, 117, 110, 46, 115,
278 7, 98, 114, 102, 97, 108, 115, 101,
279 6, 98, 114, 116, 114, 117, 101,
285 6, 98, 110, 101, 46, 117, 110,
286 6, 98, 103, 101, 46, 117, 110,
287 6, 98, 103, 116, 46, 117, 110,
288 6, 98, 108, 101, 46, 117, 110,
289 6, 98, 108, 116, 46, 117, 110,
290 6, 115, 119, 105, 116, 99, 104,
291 8, 108, 100, 105, 110, 100, 46, 105, 49,
292 8, 108, 100, 105, 110, 100, 46, 117, 49,
293 8, 108, 100, 105, 110, 100, 46, 105, 50,
294 8, 108, 100, 105, 110, 100, 46, 117, 50,
295 8, 108, 100, 105, 110, 100, 46, 105, 52,
296 8, 108, 100, 105, 110, 100, 46, 117, 52,
297 8, 108, 100, 105, 110, 100, 46, 105, 56,
298 7, 108, 100, 105, 110, 100, 46, 105,
299 8, 108, 100, 105, 110, 100, 46, 114, 52,
300 8, 108, 100, 105, 110, 100, 46, 114, 56,
301 9, 108, 100, 105, 110, 100, 46, 114, 101, 102,
302 9, 115, 116, 105, 110, 100, 46, 114, 101, 102,
303 8, 115, 116, 105, 110, 100, 46, 105, 49,
304 8, 115, 116, 105, 110, 100, 46, 105, 50,
305 8, 115, 116, 105, 110, 100, 46, 105, 52,
306 8, 115, 116, 105, 110, 100, 46, 105, 56,
307 8, 115, 116, 105, 110, 100, 46, 114, 52,
308 8, 115, 116, 105, 110, 100, 46, 114, 56,
313 6, 100, 105, 118, 46, 117, 110,
315 6, 114, 101, 109, 46, 117, 110,
321 6, 115, 104, 114, 46, 117, 110,
324 7, 99, 111, 110, 118, 46, 105, 49,
325 7, 99, 111, 110, 118, 46, 105, 50,
326 7, 99, 111, 110, 118, 46, 105, 52,
327 7, 99, 111, 110, 118, 46, 105, 56,
328 7, 99, 111, 110, 118, 46, 114, 52,
329 7, 99, 111, 110, 118, 46, 114, 56,
330 7, 99, 111, 110, 118, 46, 117, 52,
331 7, 99, 111, 110, 118, 46, 117, 56,
332 8, 99, 97, 108, 108, 118, 105, 114, 116,
333 5, 99, 112, 111, 98, 106,
334 5, 108, 100, 111, 98, 106,
335 5, 108, 100, 115, 116, 114,
336 6, 110, 101, 119, 111, 98, 106,
337 9, 99, 97, 115, 116, 99, 108, 97, 115, 115,
338 6, 105, 115, 105, 110, 115, 116,
339 9, 99, 111, 110, 118, 46, 114, 46, 117, 110,
340 5, 117, 110, 98, 111, 120,
341 5, 116, 104, 114, 111, 119,
342 5, 108, 100, 102, 108, 100,
343 6, 108, 100, 102, 108, 100, 97,
344 5, 115, 116, 102, 108, 100,
345 6, 108, 100, 115, 102, 108, 100,
346 7, 108, 100, 115, 102, 108, 100, 97,
347 6, 115, 116, 115, 102, 108, 100,
348 5, 115, 116, 111, 98, 106,
349 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 49, 46, 117, 110,
350 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 50, 46, 117, 110,
351 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 52, 46, 117, 110,
352 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 56, 46, 117, 110,
353 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 49, 46, 117, 110,
354 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 50, 46, 117, 110,
355 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 52, 46, 117, 110,
356 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 56, 46, 117, 110,
357 13, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 46, 117, 110,
358 13, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 46, 117, 110,
360 6, 110, 101, 119, 97, 114, 114,
361 5, 108, 100, 108, 101, 110,
362 7, 108, 100, 101, 108, 101, 109, 97,
363 9, 108, 100, 101, 108, 101, 109, 46, 105, 49,
364 9, 108, 100, 101, 108, 101, 109, 46, 117, 49,
365 9, 108, 100, 101, 108, 101, 109, 46, 105, 50,
366 9, 108, 100, 101, 108, 101, 109, 46, 117, 50,
367 9, 108, 100, 101, 108, 101, 109, 46, 105, 52,
368 9, 108, 100, 101, 108, 101, 109, 46, 117, 52,
369 9, 108, 100, 101, 108, 101, 109, 46, 105, 56,
370 8, 108, 100, 101, 108, 101, 109, 46, 105,
371 9, 108, 100, 101, 108, 101, 109, 46, 114, 52,
372 9, 108, 100, 101, 108, 101, 109, 46, 114, 56,
373 10, 108, 100, 101, 108, 101, 109, 46, 114, 101, 102,
374 8, 115, 116, 101, 108, 101, 109, 46, 105,
375 9, 115, 116, 101, 108, 101, 109, 46, 105, 49,
376 9, 115, 116, 101, 108, 101, 109, 46, 105, 50,
377 9, 115, 116, 101, 108, 101, 109, 46, 105, 52,
378 9, 115, 116, 101, 108, 101, 109, 46, 105, 56,
379 9, 115, 116, 101, 108, 101, 109, 46, 114, 52,
380 9, 115, 116, 101, 108, 101, 109, 46, 114, 56,
381 10, 115, 116, 101, 108, 101, 109, 46, 114, 101, 102,
382 10, 108, 100, 101, 108, 101, 109, 46, 97, 110, 121,
383 10, 115, 116, 101, 108, 101, 109, 46, 97, 110, 121,
384 9, 117, 110, 98, 111, 120, 46, 97, 110, 121,
385 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 49,
386 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 49,
387 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 50,
388 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 50,
389 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 52,
390 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 52,
391 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 56,
392 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 56,
393 9, 114, 101, 102, 97, 110, 121, 118, 97, 108,
394 8, 99, 107, 102, 105, 110, 105, 116, 101,
395 8, 109, 107, 114, 101, 102, 97, 110, 121,
396 7, 108, 100, 116, 111, 107, 101, 110,
397 7, 99, 111, 110, 118, 46, 117, 50,
398 7, 99, 111, 110, 118, 46, 117, 49,
399 6, 99, 111, 110, 118, 46, 105,
400 10, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105,
401 10, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117,
402 7, 97, 100, 100, 46, 111, 118, 102,
403 10, 97, 100, 100, 46, 111, 118, 102, 46, 117, 110,
404 7, 109, 117, 108, 46, 111, 118, 102,
405 10, 109, 117, 108, 46, 111, 118, 102, 46, 117, 110,
406 7, 115, 117, 98, 46, 111, 118, 102,
407 10, 115, 117, 98, 46, 111, 118, 102, 46, 117, 110,
408 10, 101, 110, 100, 102, 105, 110, 97, 108, 108, 121,
409 5, 108, 101, 97, 118, 101,
410 7, 108, 101, 97, 118, 101, 46, 115,
411 7, 115, 116, 105, 110, 100, 46, 105,
412 6, 99, 111, 110, 118, 46, 117,
413 7, 97, 114, 103, 108, 105, 115, 116,
416 6, 99, 103, 116, 46, 117, 110,
418 6, 99, 108, 116, 46, 117, 110,
419 5, 108, 100, 102, 116, 110,
420 9, 108, 100, 118, 105, 114, 116, 102, 116, 110,
421 5, 108, 100, 97, 114, 103,
422 6, 108, 100, 97, 114, 103, 97,
423 5, 115, 116, 97, 114, 103,
424 5, 108, 100, 108, 111, 99,
425 6, 108, 100, 108, 111, 99, 97,
426 5, 115, 116, 108, 111, 99,
427 8, 108, 111, 99, 97, 108, 108, 111, 99,
428 9, 101, 110, 100, 102, 105, 108, 116, 101, 114,
429 10, 117, 110, 97, 108, 105, 103, 110, 101, 100, 46,
430 9, 118, 111, 108, 97, 116, 105, 108, 101, 46,
431 5, 116, 97, 105, 108, 46,
432 7, 105, 110, 105, 116, 111, 98, 106,
433 12, 99, 111, 110, 115, 116, 114, 97, 105, 110, 101, 100, 46,
434 5, 99, 112, 98, 108, 107,
435 7, 105, 110, 105, 116, 98, 108, 107,
437 7, 114, 101, 116, 104, 114, 111, 119,
438 6, 115, 105, 122, 101, 111, 102,
439 10, 114, 101, 102, 97, 110, 121, 116, 121, 112, 101,
440 9, 114, 101, 97, 100, 111, 110, 108, 121, 46,
443 names = new string [219];
445 for (int i = 0, p = 0; i < names.Length; i++) {
446 var buffer = new char [table [p++]];
448 for (int j = 0; j < buffer.Length; j++)
449 buffer [j] = (char) table [p++];
451 names [i] = new string (buffer);