2007-07-19 Jb Evain <jbevain@novell.com>
[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 // (C) 2005 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 struct OpCode {
32
33                 string m_name;
34                 byte m_op1;
35                 byte m_op2;
36                 int m_size;
37
38                 Code m_code;
39                 FlowControl m_flowControl;
40                 OpCodeType m_opCodeType;
41                 OperandType m_operandType;
42                 StackBehaviour m_stackBehaviourPop;
43                 StackBehaviour m_stackBehaviourPush;
44
45                 public string Name {
46                         get { return m_name; }
47                 }
48
49                 public int Size {
50                         get { return m_size; }
51                 }
52
53                 public byte Op1 {
54                         get { return m_op1; }
55                 }
56
57                 public byte Op2 {
58                         get { return m_op2; }
59                 }
60
61                 public short Value {
62                         get { return m_size == 1 ? m_op2 : (short) ((m_op1 << 8) | m_op2); }
63                 }
64
65                 public Code Code {
66                         get { return m_code; }
67                 }
68
69                 public FlowControl FlowControl {
70                         get { return m_flowControl; }
71                 }
72
73                 public OpCodeType OpCodeType {
74                         get { return m_opCodeType; }
75                 }
76
77                 public OperandType OperandType {
78                         get { return m_operandType; }
79                 }
80
81                 public StackBehaviour StackBehaviourPop {
82                         get { return m_stackBehaviourPop; }
83                 }
84
85                 public StackBehaviour StackBehaviourPush {
86                         get { return m_stackBehaviourPush; }
87                 }
88
89                 internal OpCode (string name, byte op1, byte op2, int size,
90                         Code code, FlowControl flowControl,
91                         OpCodeType opCodeType, OperandType operandType,
92                         StackBehaviour pop, StackBehaviour push)
93                 {
94                         m_name = name;
95                         m_op1 = op1;
96                         m_op2 = op2;
97                         m_size = size;
98                         m_code = code;
99                         m_flowControl = flowControl;
100                         m_opCodeType = opCodeType;
101                         m_operandType = operandType;
102                         m_stackBehaviourPop = pop;
103                         m_stackBehaviourPush = push;
104
105                         if (op1 == 0xff)
106                                 OpCodes.OneByteOpCode [op2] = this;
107                         else
108                                 OpCodes.TwoBytesOpCode [op2] = this;
109                 }
110
111                 public override int GetHashCode ()
112                 {
113                         return this.Value;
114                 }
115
116                 public override bool Equals (object obj)
117                 {
118                         if (!(obj is OpCode))
119                                 return false;
120                         OpCode v = (OpCode) obj;
121                         return v.m_op1 == m_op1 && v.m_op2 == m_op2;
122                 }
123
124                 public static bool operator == (OpCode one, OpCode other)
125                 {
126                         return one.Equals (other);
127                 }
128
129                 public static bool operator != (OpCode one, OpCode other)
130                 {
131                         return !one.Equals (other);
132                 }
133
134                 public override string ToString ()
135                 {
136                         return m_name;
137                 }
138         }
139 }