Merged into single file, added assertions
[mono.git] / mcs / class / corlib / System.Reflection.Emit / OpCode.cs
1 //
2 // System.Reflection.Emit.OpCode
3 //
4 // Author:
5 //   Duncan Mak (duncan@ximian.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
27 //
28
29 using System;
30 using System.Reflection;
31 using System.Reflection.Emit;
32 using System.Runtime.InteropServices;
33
34 namespace System.Reflection.Emit {
35
36         [ComVisible (true)]
37         public struct OpCode {
38
39                 internal byte op1, op2;
40                 byte push, pop, size, type, args, flow;
41
42                 //
43                 // The order is:
44                 //       Op1, Op2, StackBehaviourPush, StackBehaviourPop
45                 //       Size, OpCodeType, OperandType, FlowControl
46                 //
47                 internal OpCode (int p, int q)
48                 {
49
50                         op1  = (byte) ((p >> 0)  & 0xFF);
51                         op2  = (byte) ((p >> 8)  & 0xFF);
52                         push = (byte) ((p >> 16) & 0xFF);
53                         pop  = (byte) ((p >> 24) & 0xFF);
54
55                         size = (byte) ((q >> 0)  & 0xFF);
56                         type = (byte) ((q >> 8)  & 0xFF);
57                         args = (byte) ((q >> 16) & 0xFF);
58                         flow = (byte) ((q >> 24) & 0xFF);
59                 }
60
61
62                 public override int GetHashCode ()
63                 {
64                         return Name.GetHashCode ();
65                 }
66
67                 public override bool Equals (Object obj)
68                 {
69                         if (obj == null || !(obj is OpCode))
70                                 return false;
71
72                         OpCode v = (OpCode) obj;
73
74                         return v.op1 == op1 && v.op2 == op2;
75                 }
76
77                 public bool Equals (OpCode obj)
78                 {
79                         return obj.op1 == op1 && obj.op2 == op2;
80                 }
81
82                 public override string ToString ()
83                 {
84                         return Name;
85                 }
86
87                 public string Name {
88                         get {
89                                 if (op1 == 0xFF)
90                                         return OpCodeNames.names [op2];
91
92                                 return OpCodeNames.names [256 + op2];
93                         }
94                 }
95
96                 public int Size {
97                         get {
98                                 return (int) size;
99                         }
100                 }
101
102                 public OpCodeType OpCodeType {
103                         get {
104                                 return (OpCodeType) type;
105                         }
106                 }
107
108                 public OperandType OperandType {
109                         get {
110                                 return (OperandType) args;
111                         }
112                 }
113
114                 public FlowControl FlowControl {
115                         get {
116                                 return (FlowControl) flow;
117                         }
118                 }
119
120                 public StackBehaviour StackBehaviourPop {
121                         get {
122                                 return (StackBehaviour) pop;
123                         }
124                 }
125
126                 public StackBehaviour StackBehaviourPush {
127                         get {
128                                 return (StackBehaviour) push;
129                         }
130                 }
131
132                 public short Value {
133                         get {
134                                 if (size == 1) {
135                                         return op2;
136                                 } else {
137                                         // two byte instruction - combine
138                                         // Some old MS betas returned (op1 << 2) | op2 here...
139                                         return (short) ((op1 << 8) | op2);
140                                 }
141                         }
142                 }
143
144                 public static bool operator == (OpCode a, OpCode b)
145                 {
146                         return a.op1 == b.op1 && a.op2 == b.op2;
147                 }
148
149                 public static bool operator != (OpCode a, OpCode b)
150                 {
151                         return a.op1 != b.op1 || a.op2 != b.op2;
152                 }
153         }
154