Merge branch 'cecil-light'
[mono.git] / mcs / class / Mono.Cecil / Mono.Cecil.Cil / OpCode.cs
1 //
2 // OpCode.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // Copyright (c) 2008 - 2010 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
29 namespace Mono.Cecil.Cil {
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
107         public struct OpCode {
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;
117
118                 public string Name {
119                         get { return OpCodeNames.names [op1 == 0xff ? op2 : op2 + 256]; }
120                 }
121
122                 public int Size {
123                         get { return op1 == 0xff ? 1 : 2; }
124                 }
125
126                 public byte Op1 {
127                         get { return op1; }
128                 }
129
130                 public byte Op2 {
131                         get { return op2; }
132                 }
133
134                 public short Value {
135                         get { return (short) ((op1 << 8) | op2); }
136                 }
137
138                 public Code Code {
139                         get { return (Code) code; }
140                 }
141
142                 public FlowControl FlowControl {
143                         get { return (FlowControl) flow_control; }
144                 }
145
146                 public OpCodeType OpCodeType {
147                         get { return (OpCodeType) opcode_type; }
148                 }
149
150                 public OperandType OperandType {
151                         get { return (OperandType) operand_type; }
152                 }
153
154                 public StackBehaviour StackBehaviourPop {
155                         get { return (StackBehaviour) stack_behavior_pop; }
156                 }
157
158                 public StackBehaviour StackBehaviourPush {
159                         get { return (StackBehaviour) stack_behavior_push; }
160                 }
161
162                 internal OpCode (int x, int y)
163                 {
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);
173
174                         if (op1 == 0xff)
175                                 OpCodes.OneByteOpCode [op2] = this;
176                         else
177                                 OpCodes.TwoBytesOpCode [op2] = this;
178                 }
179
180                 public override int GetHashCode ()
181                 {
182                         return Value;
183                 }
184
185                 public override bool Equals (object obj)
186                 {
187                         if (!(obj is OpCode))
188                                 return false;
189
190                         var opcode = (OpCode) obj;
191                         return op1 == opcode.op1 && op2 == opcode.op2;
192                 }
193
194                 public bool Equals (OpCode opcode)
195                 {
196                         return op1 == opcode.op1 && op2 == opcode.op2;
197                 }
198
199                 public static bool operator == (OpCode one, OpCode other)
200                 {
201                         return one.op1 == other.op1 && one.op2 == other.op2;
202                 }
203
204                 public static bool operator != (OpCode one, OpCode other)
205                 {
206                         return one.op1 != other.op1 || one.op2 != other.op2;
207                 }
208
209                 public override string ToString ()
210                 {
211                         return Name;
212                 }
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         }
524 }