~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Mono.Cecil/Mono.Cecil/Mono.Cecil.Cil/OpCode.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
// Author:
5
5
//   Jb Evain (jbevain@gmail.com)
6
6
//
7
 
// (C) 2005 Jb Evain
 
7
// Copyright (c) 2008 - 2010 Jb Evain
8
8
//
9
9
// Permission is hereby granted, free of charge, to any person obtaining
10
10
// a copy of this software and associated documentation files (the
28
28
 
29
29
namespace Mono.Cecil.Cil {
30
30
 
 
31
        public enum FlowControl {
 
32
                Branch,
 
33
                Break,
 
34
                Call,
 
35
                Cond_Branch,
 
36
                Meta,
 
37
                Next,
 
38
                Phi,
 
39
                Return,
 
40
                Throw,
 
41
        }
 
42
 
 
43
        public enum OpCodeType {
 
44
                Annotation,
 
45
                Macro,
 
46
                Nternal,
 
47
                Objmodel,
 
48
                Prefix,
 
49
                Primitive,
 
50
        }
 
51
 
 
52
        public enum OperandType {
 
53
                InlineBrTarget,
 
54
                InlineField,
 
55
                InlineI,
 
56
                InlineI8,
 
57
                InlineMethod,
 
58
                InlineNone,
 
59
                InlinePhi,
 
60
                InlineR,
 
61
                InlineSig,
 
62
                InlineString,
 
63
                InlineSwitch,
 
64
                InlineTok,
 
65
                InlineType,
 
66
                InlineVar,
 
67
                InlineArg,
 
68
                ShortInlineBrTarget,
 
69
                ShortInlineI,
 
70
                ShortInlineR,
 
71
                ShortInlineVar,
 
72
                ShortInlineArg,
 
73
        }
 
74
 
 
75
        public enum StackBehaviour {
 
76
                Pop0,
 
77
                Pop1,
 
78
                Pop1_pop1,
 
79
                Popi,
 
80
                Popi_pop1,
 
81
                Popi_popi,
 
82
                Popi_popi8,
 
83
                Popi_popi_popi,
 
84
                Popi_popr4,
 
85
                Popi_popr8,
 
86
                Popref,
 
87
                Popref_pop1,
 
88
                Popref_popi,
 
89
                Popref_popi_popi,
 
90
                Popref_popi_popi8,
 
91
                Popref_popi_popr4,
 
92
                Popref_popi_popr8,
 
93
                Popref_popi_popref,
 
94
                PopAll,
 
95
                Push0,
 
96
                Push1,
 
97
                Push1_push1,
 
98
                Pushi,
 
99
                Pushi8,
 
100
                Pushr4,
 
101
                Pushr8,
 
102
                Pushref,
 
103
                Varpop,
 
104
                Varpush,
 
105
        }
 
106
 
31
107
        public struct OpCode {
32
 
                short m_value;
33
 
                byte m_code;
34
 
                byte m_flowControl;
35
 
                byte m_opCodeType;
36
 
                byte m_operandType;
37
 
                byte m_stackBehaviourPop;
38
 
                byte m_stackBehaviourPush;
 
108
 
 
109
                readonly byte op1;
 
110
                readonly byte op2;
 
111
                readonly byte code;
 
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;
39
117
 
40
118
                public string Name {
41
 
                        get {
42
 
                                int index = (Size == 1) ? Op2 : (Op2 + 256);
43
 
                                return OpCodeNames.names [index];
44
 
                        }
 
119
                        get { return OpCodeNames.names [op1 == 0xff ? op2 : op2 + 256]; }
45
120
                }
46
121
 
47
122
                public int Size {
48
 
                        get { return ((m_value & 0xff00) == 0xff00) ? 1 : 2; }
 
123
                        get { return op1 == 0xff ? 1 : 2; }
49
124
                }
50
125
 
51
126
                public byte Op1 {
52
 
                        get { return (byte) (m_value >> 8); }
 
127
                        get { return op1; }
53
128
                }
54
129
 
55
130
                public byte Op2 {
56
 
                        get { return (byte) m_value; }
 
131
                        get { return op2; }
57
132
                }
58
133
 
59
134
                public short Value {
60
 
                        get { return (Size == 1) ? Op2 : m_value; }
 
135
                        get { return (short) ((op1 << 8) | op2); }
61
136
                }
62
137
 
63
138
                public Code Code {
64
 
                        get { return (Code) m_code; }
 
139
                        get { return (Code) code; }
65
140
                }
66
141
 
67
142
                public FlowControl FlowControl {
68
 
                        get { return (FlowControl) m_flowControl; }
 
143
                        get { return (FlowControl) flow_control; }
69
144
                }
70
145
 
71
146
                public OpCodeType OpCodeType {
72
 
                        get { return (OpCodeType) m_opCodeType; }
 
147
                        get { return (OpCodeType) opcode_type; }
73
148
                }
74
149
 
75
150
                public OperandType OperandType {
76
 
                        get { return (OperandType) m_operandType; }
 
151
                        get { return (OperandType) operand_type; }
77
152
                }
78
153
 
79
154
                public StackBehaviour StackBehaviourPop {
80
 
                        get { return (StackBehaviour) m_stackBehaviourPop; }
 
155
                        get { return (StackBehaviour) stack_behavior_pop; }
81
156
                }
82
157
 
83
158
                public StackBehaviour StackBehaviourPush {
84
 
                        get { return (StackBehaviour) m_stackBehaviourPush; }
 
159
                        get { return (StackBehaviour) stack_behavior_push; }
85
160
                }
86
161
 
87
 
                internal OpCode (byte op1, byte op2,
88
 
                        Code code, FlowControl flowControl,
89
 
                        OpCodeType opCodeType, OperandType operandType,
90
 
                        StackBehaviour pop, StackBehaviour push)
 
162
                internal OpCode (int x, int y)
91
163
                {
92
 
                        m_value = (short) ((op1 << 8) | op2);
93
 
                        m_code = (byte) code;
94
 
                        m_flowControl = (byte) flowControl;
95
 
                        m_opCodeType = (byte) opCodeType;
96
 
                        m_operandType = (byte) operandType;
97
 
                        m_stackBehaviourPop = (byte) pop;
98
 
                        m_stackBehaviourPush = (byte) push;
 
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);
 
168
 
 
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);
99
173
 
100
174
                        if (op1 == 0xff)
101
175
                                OpCodes.OneByteOpCode [op2] = this;
105
179
 
106
180
                public override int GetHashCode ()
107
181
                {
108
 
                        return m_value;
 
182
                        return Value;
109
183
                }
110
184
 
111
185
                public override bool Equals (object obj)
112
186
                {
113
187
                        if (!(obj is OpCode))
114
188
                                return false;
115
 
                        OpCode v = (OpCode) obj;
116
 
                        return v.m_value == m_value;
 
189
 
 
190
                        var opcode = (OpCode) obj;
 
191
                        return op1 == opcode.op1 && op2 == opcode.op2;
117
192
                }
118
193
 
119
194
                public bool Equals (OpCode opcode)
120
195
                {
121
 
                        return (m_value == opcode.m_value);
 
196
                        return op1 == opcode.op1 && op2 == opcode.op2;
122
197
                }
123
198
 
124
199
                public static bool operator == (OpCode one, OpCode other)
125
200
                {
126
 
                        return (one.m_value == other.m_value);
 
201
                        return one.op1 == other.op1 && one.op2 == other.op2;
127
202
                }
128
203
 
129
204
                public static bool operator != (OpCode one, OpCode other)
130
205
                {
131
 
                        return (one.m_value != other.m_value);
 
206
                        return one.op1 != other.op1 || one.op2 != other.op2;
132
207
                }
133
208
 
134
209
                public override string ToString ()
136
211
                        return Name;
137
212
                }
138
213
        }
 
214
 
 
215
        static class OpCodeNames {
 
216
 
 
217
                internal static readonly string [] names = {
 
218
                        "nop",
 
219
                        "break",
 
220
                        "ldarg.0",
 
221
                        "ldarg.1",
 
222
                        "ldarg.2",
 
223
                        "ldarg.3",
 
224
                        "ldloc.0",
 
225
                        "ldloc.1",
 
226
                        "ldloc.2",
 
227
                        "ldloc.3",
 
228
                        "stloc.0",
 
229
                        "stloc.1",
 
230
                        "stloc.2",
 
231
                        "stloc.3",
 
232
                        "ldarg.s",
 
233
                        "ldarga.s",
 
234
                        "starg.s",
 
235
                        "ldloc.s",
 
236
                        "ldloca.s",
 
237
                        "stloc.s",
 
238
                        "ldnull",
 
239
                        "ldc.i4.m1",
 
240
                        "ldc.i4.0",
 
241
                        "ldc.i4.1",
 
242
                        "ldc.i4.2",
 
243
                        "ldc.i4.3",
 
244
                        "ldc.i4.4",
 
245
                        "ldc.i4.5",
 
246
                        "ldc.i4.6",
 
247
                        "ldc.i4.7",
 
248
                        "ldc.i4.8",
 
249
                        "ldc.i4.s",
 
250
                        "ldc.i4",
 
251
                        "ldc.i8",
 
252
                        "ldc.r4",
 
253
                        "ldc.r8",
 
254
                        null,
 
255
                        "dup",
 
256
                        "pop",
 
257
                        "jmp",
 
258
                        "call",
 
259
                        "calli",
 
260
                        "ret",
 
261
                        "br.s",
 
262
                        "brfalse.s",
 
263
                        "brtrue.s",
 
264
                        "beq.s",
 
265
                        "bge.s",
 
266
                        "bgt.s",
 
267
                        "ble.s",
 
268
                        "blt.s",
 
269
                        "bne.un.s",
 
270
                        "bge.un.s",
 
271
                        "bgt.un.s",
 
272
                        "ble.un.s",
 
273
                        "blt.un.s",
 
274
                        "br",
 
275
                        "brfalse",
 
276
                        "brtrue",
 
277
                        "beq",
 
278
                        "bge",
 
279
                        "bgt",
 
280
                        "ble",
 
281
                        "blt",
 
282
                        "bne.un",
 
283
                        "bge.un",
 
284
                        "bgt.un",
 
285
                        "ble.un",
 
286
                        "blt.un",
 
287
                        "switch",
 
288
                        "ldind.i1",
 
289
                        "ldind.u1",
 
290
                        "ldind.i2",
 
291
                        "ldind.u2",
 
292
                        "ldind.i4",
 
293
                        "ldind.u4",
 
294
                        "ldind.i8",
 
295
                        "ldind.i",
 
296
                        "ldind.r4",
 
297
                        "ldind.r8",
 
298
                        "ldind.ref",
 
299
                        "stind.ref",
 
300
                        "stind.i1",
 
301
                        "stind.i2",
 
302
                        "stind.i4",
 
303
                        "stind.i8",
 
304
                        "stind.r4",
 
305
                        "stind.r8",
 
306
                        "add",
 
307
                        "sub",
 
308
                        "mul",
 
309
                        "div",
 
310
                        "div.un",
 
311
                        "rem",
 
312
                        "rem.un",
 
313
                        "and",
 
314
                        "or",
 
315
                        "xor",
 
316
                        "shl",
 
317
                        "shr",
 
318
                        "shr.un",
 
319
                        "neg",
 
320
                        "not",
 
321
                        "conv.i1",
 
322
                        "conv.i2",
 
323
                        "conv.i4",
 
324
                        "conv.i8",
 
325
                        "conv.r4",
 
326
                        "conv.r8",
 
327
                        "conv.u4",
 
328
                        "conv.u8",
 
329
                        "callvirt",
 
330
                        "cpobj",
 
331
                        "ldobj",
 
332
                        "ldstr",
 
333
                        "newobj",
 
334
                        "castclass",
 
335
                        "isinst",
 
336
                        "conv.r.un",
 
337
                        null,
 
338
                        null,
 
339
                        "unbox",
 
340
                        "throw",
 
341
                        "ldfld",
 
342
                        "ldflda",
 
343
                        "stfld",
 
344
                        "ldsfld",
 
345
                        "ldsflda",
 
346
                        "stsfld",
 
347
                        "stobj",
 
348
                        "conv.ovf.i1.un",
 
349
                        "conv.ovf.i2.un",
 
350
                        "conv.ovf.i4.un",
 
351
                        "conv.ovf.i8.un",
 
352
                        "conv.ovf.u1.un",
 
353
                        "conv.ovf.u2.un",
 
354
                        "conv.ovf.u4.un",
 
355
                        "conv.ovf.u8.un",
 
356
                        "conv.ovf.i.un",
 
357
                        "conv.ovf.u.un",
 
358
                        "box",
 
359
                        "newarr",
 
360
                        "ldlen",
 
361
                        "ldelema",
 
362
                        "ldelem.i1",
 
363
                        "ldelem.u1",
 
364
                        "ldelem.i2",
 
365
                        "ldelem.u2",
 
366
                        "ldelem.i4",
 
367
                        "ldelem.u4",
 
368
                        "ldelem.i8",
 
369
                        "ldelem.i",
 
370
                        "ldelem.r4",
 
371
                        "ldelem.r8",
 
372
                        "ldelem.ref",
 
373
                        "stelem.i",
 
374
                        "stelem.i1",
 
375
                        "stelem.i2",
 
376
                        "stelem.i4",
 
377
                        "stelem.i8",
 
378
                        "stelem.r4",
 
379
                        "stelem.r8",
 
380
                        "stelem.ref",
 
381
                        "ldelem.any",
 
382
                        "stelem.any",
 
383
                        "unbox.any",
 
384
                        null,
 
385
                        null,
 
386
                        null,
 
387
                        null,
 
388
                        null,
 
389
                        null,
 
390
                        null,
 
391
                        null,
 
392
                        null,
 
393
                        null,
 
394
                        null,
 
395
                        null,
 
396
                        null,
 
397
                        "conv.ovf.i1",
 
398
                        "conv.ovf.u1",
 
399
                        "conv.ovf.i2",
 
400
                        "conv.ovf.u2",
 
401
                        "conv.ovf.i4",
 
402
                        "conv.ovf.u4",
 
403
                        "conv.ovf.i8",
 
404
                        "conv.ovf.u8",
 
405
                        null,
 
406
                        null,
 
407
                        null,
 
408
                        null,
 
409
                        null,
 
410
                        null,
 
411
                        null,
 
412
                        "refanyval",
 
413
                        "ckfinite",
 
414
                        null,
 
415
                        null,
 
416
                        "mkrefany",
 
417
                        null,
 
418
                        null,
 
419
                        null,
 
420
                        null,
 
421
                        null,
 
422
                        null,
 
423
                        null,
 
424
                        null,
 
425
                        null,
 
426
                        "ldtoken",
 
427
                        "conv.u2",
 
428
                        "conv.u1",
 
429
                        "conv.i",
 
430
                        "conv.ovf.i",
 
431
                        "conv.ovf.u",
 
432
                        "add.ovf",
 
433
                        "add.ovf.un",
 
434
                        "mul.ovf",
 
435
                        "mul.ovf.un",
 
436
                        "sub.ovf",
 
437
                        "sub.ovf.un",
 
438
                        "endfinally",
 
439
                        "leave",
 
440
                        "leave.s",
 
441
                        "stind.i",
 
442
                        "conv.u",
 
443
                        null,
 
444
                        null,
 
445
                        null,
 
446
                        null,
 
447
                        null,
 
448
                        null,
 
449
                        null,
 
450
                        null,
 
451
                        null,
 
452
                        null,
 
453
                        null,
 
454
                        null,
 
455
                        null,
 
456
                        null,
 
457
                        null,
 
458
                        null,
 
459
                        null,
 
460
                        null,
 
461
                        null,
 
462
                        null,
 
463
                        null,
 
464
                        null,
 
465
                        null,
 
466
                        "prefix7",
 
467
                        "prefix6",
 
468
                        "prefix5",
 
469
                        "prefix4",
 
470
                        "prefix3",
 
471
                        "prefix2",
 
472
                        "prefix1",
 
473
                        "prefixref",
 
474
                        "arglist",
 
475
                        "ceq",
 
476
                        "cgt",
 
477
                        "cgt.un",
 
478
                        "clt",
 
479
                        "clt.un",
 
480
                        "ldftn",
 
481
                        "ldvirtftn",
 
482
                        null,
 
483
                        "ldarg",
 
484
                        "ldarga",
 
485
                        "starg",
 
486
                        "ldloc",
 
487
                        "ldloca",
 
488
                        "stloc",
 
489
                        "localloc",
 
490
                        null,
 
491
                        "endfilter",
 
492
                        "unaligned.",
 
493
                        "volatile.",
 
494
                        "tail.",
 
495
                        "initobj",
 
496
                        "constrained.",
 
497
                        "cpblk",
 
498
                        "initblk",
 
499
                        "no.",          // added by spouliot to match Cecil existing definitions
 
500
                        "rethrow",
 
501
                        null,
 
502
                        "sizeof",
 
503
                        "refanytype",
 
504
                        "readonly.",    // added by spouliot to match Cecil existing definitions
 
505
                        null,
 
506
                        null,
 
507
                        null,
 
508
                        null,
 
509
                        null,
 
510
                        null,
 
511
                        null,
 
512
                        null,
 
513
                        null,
 
514
                        null,
 
515
                        null,
 
516
                        null,
 
517
                        null,
 
518
                        null,
 
519
                        null,
 
520
                        null,
 
521
                        null,
 
522
                };
 
523
        }
139
524
}