1 //
2 // Author:
3 //   Jb Evain (jbevain@gmail.com)
4 //
5 // Copyright (c) 2008 - 2015 Jb Evain
6 // Copyright (c) 2008 - 2011 Novell, Inc.
7 //
8 // Licensed under the MIT/X11 license.
9 //
10 
11 using System;
12 
13 namespace Mono.Cecil.Cil {
14 
15 	public enum FlowControl {
16 		Branch,
17 		Break,
18 		Call,
19 		Cond_Branch,
20 		Meta,
21 		Next,
22 		Phi,
23 		Return,
24 		Throw,
25 	}
26 
27 	public enum OpCodeType {
28 		Annotation,
29 		Macro,
30 		Nternal,
31 		Objmodel,
32 		Prefix,
33 		Primitive,
34 	}
35 
36 	public enum OperandType {
37 		InlineBrTarget,
38 		InlineField,
39 		InlineI,
40 		InlineI8,
41 		InlineMethod,
42 		InlineNone,
43 		InlinePhi,
44 		InlineR,
45 		InlineSig,
46 		InlineString,
47 		InlineSwitch,
48 		InlineTok,
49 		InlineType,
50 		InlineVar,
51 		InlineArg,
52 		ShortInlineBrTarget,
53 		ShortInlineI,
54 		ShortInlineR,
55 		ShortInlineVar,
56 		ShortInlineArg,
57 	}
58 
59 	public enum StackBehaviour {
60 		Pop0,
61 		Pop1,
62 		Pop1_pop1,
63 		Popi,
64 		Popi_pop1,
65 		Popi_popi,
66 		Popi_popi8,
67 		Popi_popi_popi,
68 		Popi_popr4,
69 		Popi_popr8,
70 		Popref,
71 		Popref_pop1,
72 		Popref_popi,
73 		Popref_popi_popi,
74 		Popref_popi_popi8,
75 		Popref_popi_popr4,
76 		Popref_popi_popr8,
77 		Popref_popi_popref,
78 		PopAll,
79 		Push0,
80 		Push1,
81 		Push1_push1,
82 		Pushi,
83 		Pushi8,
84 		Pushr4,
85 		Pushr8,
86 		Pushref,
87 		Varpop,
88 		Varpush,
89 	}
90 
91 	public struct OpCode : IEquatable<OpCode> {
92 
93 		readonly byte op1;
94 		readonly byte op2;
95 		readonly byte code;
96 		readonly byte flow_control;
97 		readonly byte opcode_type;
98 		readonly byte operand_type;
99 		readonly byte stack_behavior_pop;
100 		readonly byte stack_behavior_push;
101 
102 		public string Name {
103 			get { return OpCodeNames.names [(int) Code]; }
104 		}
105 
106 		public int Size {
107 			get { return op1 == 0xff ? 1 : 2; }
108 		}
109 
110 		public byte Op1 {
111 			get { return op1; }
112 		}
113 
114 		public byte Op2 {
115 			get { return op2; }
116 		}
117 
118 		public short Value {
119 			get { return op1 == 0xff ? op2 : (short) ((op1 << 8) | op2); }
120 		}
121 
122 		public Code Code {
123 			get { return (Code) code; }
124 		}
125 
126 		public FlowControl FlowControl {
127 			get { return (FlowControl) flow_control; }
128 		}
129 
130 		public OpCodeType OpCodeType {
131 			get { return (OpCodeType) opcode_type; }
132 		}
133 
134 		public OperandType OperandType {
135 			get { return (OperandType) operand_type; }
136 		}
137 
138 		public StackBehaviour StackBehaviourPop {
139 			get { return (StackBehaviour) stack_behavior_pop; }
140 		}
141 
142 		public StackBehaviour StackBehaviourPush {
143 			get { return (StackBehaviour) stack_behavior_push; }
144 		}
145 
OpCodeMono.Cecil.Cil.OpCode146 		internal OpCode (int x, int y)
147 		{
148 			this.op1 = (byte) ((x >> 0) & 0xff);
149 			this.op2 = (byte) ((x >> 8) & 0xff);
150 			this.code = (byte) ((x >> 16) & 0xff);
151 			this.flow_control = (byte) ((x >> 24) & 0xff);
152 
153 			this.opcode_type = (byte) ((y >> 0) & 0xff);
154 			this.operand_type = (byte) ((y >> 8) & 0xff);
155 			this.stack_behavior_pop = (byte) ((y >> 16) & 0xff);
156 			this.stack_behavior_push = (byte) ((y >> 24) & 0xff);
157 
158 			if (op1 == 0xff)
159 				OpCodes.OneByteOpCode [op2] = this;
160 			else
161 				OpCodes.TwoBytesOpCode [op2] = this;
162 		}
163 
GetHashCodeMono.Cecil.Cil.OpCode164 		public override int GetHashCode ()
165 		{
166 			return Value;
167 		}
168 
EqualsMono.Cecil.Cil.OpCode169 		public override bool Equals (object obj)
170 		{
171 			if (!(obj is OpCode))
172 				return false;
173 
174 			var opcode = (OpCode) obj;
175 			return op1 == opcode.op1 && op2 == opcode.op2;
176 		}
177 
EqualsMono.Cecil.Cil.OpCode178 		public bool Equals (OpCode opcode)
179 		{
180 			return op1 == opcode.op1 && op2 == opcode.op2;
181 		}
182 
operator ==Mono.Cecil.Cil.OpCode183 		public static bool operator == (OpCode one, OpCode other)
184 		{
185 			return one.op1 == other.op1 && one.op2 == other.op2;
186 		}
187 
operator !=Mono.Cecil.Cil.OpCode188 		public static bool operator != (OpCode one, OpCode other)
189 		{
190 			return one.op1 != other.op1 || one.op2 != other.op2;
191 		}
192 
ToStringMono.Cecil.Cil.OpCode193 		public override string ToString ()
194 		{
195 			return Name;
196 		}
197 	}
198 
199 	static class OpCodeNames {
200 
201 		internal static readonly string [] names;
202 
OpCodeNames()203 		static OpCodeNames ()
204 		{
205 			var table = new byte [] {
206 				3, 110, 111, 112,
207 				5, 98, 114, 101, 97, 107,
208 				7, 108, 100, 97, 114, 103, 46, 48,
209 				7, 108, 100, 97, 114, 103, 46, 49,
210 				7, 108, 100, 97, 114, 103, 46, 50,
211 				7, 108, 100, 97, 114, 103, 46, 51,
212 				7, 108, 100, 108, 111, 99, 46, 48,
213 				7, 108, 100, 108, 111, 99, 46, 49,
214 				7, 108, 100, 108, 111, 99, 46, 50,
215 				7, 108, 100, 108, 111, 99, 46, 51,
216 				7, 115, 116, 108, 111, 99, 46, 48,
217 				7, 115, 116, 108, 111, 99, 46, 49,
218 				7, 115, 116, 108, 111, 99, 46, 50,
219 				7, 115, 116, 108, 111, 99, 46, 51,
220 				7, 108, 100, 97, 114, 103, 46, 115,
221 				8, 108, 100, 97, 114, 103, 97, 46, 115,
222 				7, 115, 116, 97, 114, 103, 46, 115,
223 				7, 108, 100, 108, 111, 99, 46, 115,
224 				8, 108, 100, 108, 111, 99, 97, 46, 115,
225 				7, 115, 116, 108, 111, 99, 46, 115,
226 				6, 108, 100, 110, 117, 108, 108,
227 				9, 108, 100, 99, 46, 105, 52, 46, 109, 49,
228 				8, 108, 100, 99, 46, 105, 52, 46, 48,
229 				8, 108, 100, 99, 46, 105, 52, 46, 49,
230 				8, 108, 100, 99, 46, 105, 52, 46, 50,
231 				8, 108, 100, 99, 46, 105, 52, 46, 51,
232 				8, 108, 100, 99, 46, 105, 52, 46, 52,
233 				8, 108, 100, 99, 46, 105, 52, 46, 53,
234 				8, 108, 100, 99, 46, 105, 52, 46, 54,
235 				8, 108, 100, 99, 46, 105, 52, 46, 55,
236 				8, 108, 100, 99, 46, 105, 52, 46, 56,
237 				8, 108, 100, 99, 46, 105, 52, 46, 115,
238 				6, 108, 100, 99, 46, 105, 52,
239 				6, 108, 100, 99, 46, 105, 56,
240 				6, 108, 100, 99, 46, 114, 52,
241 				6, 108, 100, 99, 46, 114, 56,
242 				3, 100, 117, 112,
243 				3, 112, 111, 112,
244 				3, 106, 109, 112,
245 				4, 99, 97, 108, 108,
246 				5, 99, 97, 108, 108, 105,
247 				3, 114, 101, 116,
248 				4, 98, 114, 46, 115,
249 				9, 98, 114, 102, 97, 108, 115, 101, 46, 115,
250 				8, 98, 114, 116, 114, 117, 101, 46, 115,
251 				5, 98, 101, 113, 46, 115,
252 				5, 98, 103, 101, 46, 115,
253 				5, 98, 103, 116, 46, 115,
254 				5, 98, 108, 101, 46, 115,
255 				5, 98, 108, 116, 46, 115,
256 				8, 98, 110, 101, 46, 117, 110, 46, 115,
257 				8, 98, 103, 101, 46, 117, 110, 46, 115,
258 				8, 98, 103, 116, 46, 117, 110, 46, 115,
259 				8, 98, 108, 101, 46, 117, 110, 46, 115,
260 				8, 98, 108, 116, 46, 117, 110, 46, 115,
261 				2, 98, 114,
262 				7, 98, 114, 102, 97, 108, 115, 101,
263 				6, 98, 114, 116, 114, 117, 101,
264 				3, 98, 101, 113,
265 				3, 98, 103, 101,
266 				3, 98, 103, 116,
267 				3, 98, 108, 101,
268 				3, 98, 108, 116,
269 				6, 98, 110, 101, 46, 117, 110,
270 				6, 98, 103, 101, 46, 117, 110,
271 				6, 98, 103, 116, 46, 117, 110,
272 				6, 98, 108, 101, 46, 117, 110,
273 				6, 98, 108, 116, 46, 117, 110,
274 				6, 115, 119, 105, 116, 99, 104,
275 				8, 108, 100, 105, 110, 100, 46, 105, 49,
276 				8, 108, 100, 105, 110, 100, 46, 117, 49,
277 				8, 108, 100, 105, 110, 100, 46, 105, 50,
278 				8, 108, 100, 105, 110, 100, 46, 117, 50,
279 				8, 108, 100, 105, 110, 100, 46, 105, 52,
280 				8, 108, 100, 105, 110, 100, 46, 117, 52,
281 				8, 108, 100, 105, 110, 100, 46, 105, 56,
282 				7, 108, 100, 105, 110, 100, 46, 105,
283 				8, 108, 100, 105, 110, 100, 46, 114, 52,
284 				8, 108, 100, 105, 110, 100, 46, 114, 56,
285 				9, 108, 100, 105, 110, 100, 46, 114, 101, 102,
286 				9, 115, 116, 105, 110, 100, 46, 114, 101, 102,
287 				8, 115, 116, 105, 110, 100, 46, 105, 49,
288 				8, 115, 116, 105, 110, 100, 46, 105, 50,
289 				8, 115, 116, 105, 110, 100, 46, 105, 52,
290 				8, 115, 116, 105, 110, 100, 46, 105, 56,
291 				8, 115, 116, 105, 110, 100, 46, 114, 52,
292 				8, 115, 116, 105, 110, 100, 46, 114, 56,
293 				3, 97, 100, 100,
294 				3, 115, 117, 98,
295 				3, 109, 117, 108,
296 				3, 100, 105, 118,
297 				6, 100, 105, 118, 46, 117, 110,
298 				3, 114, 101, 109,
299 				6, 114, 101, 109, 46, 117, 110,
300 				3, 97, 110, 100,
301 				2, 111, 114,
302 				3, 120, 111, 114,
303 				3, 115, 104, 108,
304 				3, 115, 104, 114,
305 				6, 115, 104, 114, 46, 117, 110,
306 				3, 110, 101, 103,
307 				3, 110, 111, 116,
308 				7, 99, 111, 110, 118, 46, 105, 49,
309 				7, 99, 111, 110, 118, 46, 105, 50,
310 				7, 99, 111, 110, 118, 46, 105, 52,
311 				7, 99, 111, 110, 118, 46, 105, 56,
312 				7, 99, 111, 110, 118, 46, 114, 52,
313 				7, 99, 111, 110, 118, 46, 114, 56,
314 				7, 99, 111, 110, 118, 46, 117, 52,
315 				7, 99, 111, 110, 118, 46, 117, 56,
316 				8, 99, 97, 108, 108, 118, 105, 114, 116,
317 				5, 99, 112, 111, 98, 106,
318 				5, 108, 100, 111, 98, 106,
319 				5, 108, 100, 115, 116, 114,
320 				6, 110, 101, 119, 111, 98, 106,
321 				9, 99, 97, 115, 116, 99, 108, 97, 115, 115,
322 				6, 105, 115, 105, 110, 115, 116,
323 				9, 99, 111, 110, 118, 46, 114, 46, 117, 110,
324 				5, 117, 110, 98, 111, 120,
325 				5, 116, 104, 114, 111, 119,
326 				5, 108, 100, 102, 108, 100,
327 				6, 108, 100, 102, 108, 100, 97,
328 				5, 115, 116, 102, 108, 100,
329 				6, 108, 100, 115, 102, 108, 100,
330 				7, 108, 100, 115, 102, 108, 100, 97,
331 				6, 115, 116, 115, 102, 108, 100,
332 				5, 115, 116, 111, 98, 106,
333 				14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 49, 46, 117, 110,
334 				14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 50, 46, 117, 110,
335 				14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 52, 46, 117, 110,
336 				14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 56, 46, 117, 110,
337 				14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 49, 46, 117, 110,
338 				14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 50, 46, 117, 110,
339 				14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 52, 46, 117, 110,
340 				14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 56, 46, 117, 110,
341 				13, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 46, 117, 110,
342 				13, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 46, 117, 110,
343 				3, 98, 111, 120,
344 				6, 110, 101, 119, 97, 114, 114,
345 				5, 108, 100, 108, 101, 110,
346 				7, 108, 100, 101, 108, 101, 109, 97,
347 				9, 108, 100, 101, 108, 101, 109, 46, 105, 49,
348 				9, 108, 100, 101, 108, 101, 109, 46, 117, 49,
349 				9, 108, 100, 101, 108, 101, 109, 46, 105, 50,
350 				9, 108, 100, 101, 108, 101, 109, 46, 117, 50,
351 				9, 108, 100, 101, 108, 101, 109, 46, 105, 52,
352 				9, 108, 100, 101, 108, 101, 109, 46, 117, 52,
353 				9, 108, 100, 101, 108, 101, 109, 46, 105, 56,
354 				8, 108, 100, 101, 108, 101, 109, 46, 105,
355 				9, 108, 100, 101, 108, 101, 109, 46, 114, 52,
356 				9, 108, 100, 101, 108, 101, 109, 46, 114, 56,
357 				10, 108, 100, 101, 108, 101, 109, 46, 114, 101, 102,
358 				8, 115, 116, 101, 108, 101, 109, 46, 105,
359 				9, 115, 116, 101, 108, 101, 109, 46, 105, 49,
360 				9, 115, 116, 101, 108, 101, 109, 46, 105, 50,
361 				9, 115, 116, 101, 108, 101, 109, 46, 105, 52,
362 				9, 115, 116, 101, 108, 101, 109, 46, 105, 56,
363 				9, 115, 116, 101, 108, 101, 109, 46, 114, 52,
364 				9, 115, 116, 101, 108, 101, 109, 46, 114, 56,
365 				10, 115, 116, 101, 108, 101, 109, 46, 114, 101, 102,
366 				10, 108, 100, 101, 108, 101, 109, 46, 97, 110, 121,
367 				10, 115, 116, 101, 108, 101, 109, 46, 97, 110, 121,
368 				9, 117, 110, 98, 111, 120, 46, 97, 110, 121,
369 				11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 49,
370 				11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 49,
371 				11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 50,
372 				11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 50,
373 				11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 52,
374 				11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 52,
375 				11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 56,
376 				11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 56,
377 				9, 114, 101, 102, 97, 110, 121, 118, 97, 108,
378 				8, 99, 107, 102, 105, 110, 105, 116, 101,
379 				8, 109, 107, 114, 101, 102, 97, 110, 121,
380 				7, 108, 100, 116, 111, 107, 101, 110,
381 				7, 99, 111, 110, 118, 46, 117, 50,
382 				7, 99, 111, 110, 118, 46, 117, 49,
383 				6, 99, 111, 110, 118, 46, 105,
384 				10, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105,
385 				10, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117,
386 				7, 97, 100, 100, 46, 111, 118, 102,
387 				10, 97, 100, 100, 46, 111, 118, 102, 46, 117, 110,
388 				7, 109, 117, 108, 46, 111, 118, 102,
389 				10, 109, 117, 108, 46, 111, 118, 102, 46, 117, 110,
390 				7, 115, 117, 98, 46, 111, 118, 102,
391 				10, 115, 117, 98, 46, 111, 118, 102, 46, 117, 110,
392 				10, 101, 110, 100, 102, 105, 110, 97, 108, 108, 121,
393 				5, 108, 101, 97, 118, 101,
394 				7, 108, 101, 97, 118, 101, 46, 115,
395 				7, 115, 116, 105, 110, 100, 46, 105,
396 				6, 99, 111, 110, 118, 46, 117,
397 				7, 97, 114, 103, 108, 105, 115, 116,
398 				3, 99, 101, 113,
399 				3, 99, 103, 116,
400 				6, 99, 103, 116, 46, 117, 110,
401 				3, 99, 108, 116,
402 				6, 99, 108, 116, 46, 117, 110,
403 				5, 108, 100, 102, 116, 110,
404 				9, 108, 100, 118, 105, 114, 116, 102, 116, 110,
405 				5, 108, 100, 97, 114, 103,
406 				6, 108, 100, 97, 114, 103, 97,
407 				5, 115, 116, 97, 114, 103,
408 				5, 108, 100, 108, 111, 99,
409 				6, 108, 100, 108, 111, 99, 97,
410 				5, 115, 116, 108, 111, 99,
411 				8, 108, 111, 99, 97, 108, 108, 111, 99,
412 				9, 101, 110, 100, 102, 105, 108, 116, 101, 114,
413 				10, 117, 110, 97, 108, 105, 103, 110, 101, 100, 46,
414 				9, 118, 111, 108, 97, 116, 105, 108, 101, 46,
415 				5, 116, 97, 105, 108, 46,
416 				7, 105, 110, 105, 116, 111, 98, 106,
417 				12, 99, 111, 110, 115, 116, 114, 97, 105, 110, 101, 100, 46,
418 				5, 99, 112, 98, 108, 107,
419 				7, 105, 110, 105, 116, 98, 108, 107,
420 				3, 110, 111, 46,
421 				7, 114, 101, 116, 104, 114, 111, 119,
422 				6, 115, 105, 122, 101, 111, 102,
423 				10, 114, 101, 102, 97, 110, 121, 116, 121, 112, 101,
424 				9, 114, 101, 97, 100, 111, 110, 108, 121, 46,
425 			};
426 
427 			names = new string [219];
428 
429 			for (int i = 0, p = 0; i < names.Length; i++) {
430 				var buffer = new char [table [p++]];
431 
432 				for (int j = 0; j < buffer.Length; j++)
433 					buffer [j] = (char) table [p++];
434 
435 				names [i] = new string (buffer);
436 			}
437 		}
438 	}
439 }
440