update Mono.Cecil
[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 - 2011 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 [(int) Code]; }
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 op1 == 0xff ? op2 : (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
219                 static OpCodeNames ()
220                 {
221                         var table = new byte [] {
222                                 3, 110, 111, 112,
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,
258                                 3, 100, 117, 112,
259                                 3, 112, 111, 112,
260                                 3, 106, 109, 112,
261                                 4, 99, 97, 108, 108,
262                                 5, 99, 97, 108, 108, 105,
263                                 3, 114, 101, 116,
264                                 4, 98, 114, 46, 115,
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,
277                                 2, 98, 114,
278                                 7, 98, 114, 102, 97, 108, 115, 101,
279                                 6, 98, 114, 116, 114, 117, 101,
280                                 3, 98, 101, 113,
281                                 3, 98, 103, 101,
282                                 3, 98, 103, 116,
283                                 3, 98, 108, 101,
284                                 3, 98, 108, 116,
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,
309                                 3, 97, 100, 100,
310                                 3, 115, 117, 98,
311                                 3, 109, 117, 108,
312                                 3, 100, 105, 118,
313                                 6, 100, 105, 118, 46, 117, 110,
314                                 3, 114, 101, 109,
315                                 6, 114, 101, 109, 46, 117, 110,
316                                 3, 97, 110, 100,
317                                 2, 111, 114,
318                                 3, 120, 111, 114,
319                                 3, 115, 104, 108,
320                                 3, 115, 104, 114,
321                                 6, 115, 104, 114, 46, 117, 110,
322                                 3, 110, 101, 103,
323                                 3, 110, 111, 116,
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,
359                                 3, 98, 111, 120,
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,
414                                 3, 99, 101, 113,
415                                 3, 99, 103, 116,
416                                 6, 99, 103, 116, 46, 117, 110,
417                                 3, 99, 108, 116,
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,
436                                 3, 110, 111, 46,
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,
441                         };
442
443                         names = new string [219];
444
445                         for (int i = 0, p = 0; i < names.Length; i++) {
446                                 var buffer = new char [table [p++]];
447
448                                 for (int j = 0; j < buffer.Length; j++)
449                                         buffer [j] = (char) table [p++];
450
451                                 names [i] = new string (buffer);
452                         }
453                 }
454         }
455 }