Fixed Optimize for ldc_i4
[mono.git] / mcs / class / Mono.Cecil / Mono.Cecil.Cil / Instruction.cs
1 //
2 // Instruction.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 sealed class Instruction : ICodeVisitable {
32
33                 int m_offset;
34                 OpCode m_opCode;
35                 object m_operand;
36
37                 Instruction m_previous;
38                 Instruction m_next;
39
40                 SequencePoint m_sequencePoint;
41
42                 public int Offset {
43                         get { return m_offset; }
44                         set { m_offset = value; }
45                 }
46
47                 public OpCode OpCode {
48                         get { return m_opCode; }
49                         set { m_opCode = value; }
50                 }
51
52                 public object Operand {
53                         get { return m_operand; }
54                         set { m_operand = value; }
55                 }
56
57                 public Instruction Previous {
58                         get { return m_previous; }
59                         set { m_previous = value; }
60                 }
61
62                 public Instruction Next {
63                         get { return m_next; }
64                         set { m_next = value; }
65                 }
66
67                 public SequencePoint SequencePoint {
68                         get { return m_sequencePoint; }
69                         set { m_sequencePoint = value; }
70                 }
71
72                 internal Instruction (int offset, OpCode opCode, object operand) : this (offset, opCode)
73                 {
74                         m_operand = operand;
75                 }
76
77                 internal Instruction (int offset, OpCode opCode)
78                 {
79                         m_offset = offset;
80                         m_opCode = opCode;
81                 }
82
83                 internal Instruction (OpCode opCode, object operand) : this (0, opCode, operand)
84                 {
85                 }
86
87                 internal Instruction (OpCode opCode) : this (0, opCode)
88                 {
89                 }
90
91                 public int GetSize ()
92                 {
93                         int size = m_opCode.Size;
94
95                         switch (m_opCode.OperandType) {
96                         case OperandType.InlineSwitch:
97                                 size += (1 + ((Instruction []) m_operand).Length) * 4;
98                                 break;
99                         case OperandType.InlineI8:
100                         case OperandType.InlineR:
101                                 size += 8;
102                                 break;
103                         case OperandType.InlineBrTarget:
104                         case OperandType.InlineField:
105                         case OperandType.InlineI:
106                         case OperandType.InlineMethod:
107                         case OperandType.InlineString:
108                         case OperandType.InlineTok:
109                         case OperandType.InlineType:
110                         case OperandType.ShortInlineR:
111                                 size += 4;
112                                 break;
113                         case OperandType.InlineParam:
114                         case OperandType.InlineVar:
115                                 size += 2;
116                                 break;
117                         case OperandType.ShortInlineBrTarget:
118                         case OperandType.ShortInlineI:
119                         case OperandType.ShortInlineParam:
120                         case OperandType.ShortInlineVar:
121                                 size += 1;
122                                 break;
123                         }
124
125                         return size;
126                 }
127
128                 public void Accept (ICodeVisitor visitor)
129                 {
130                         visitor.VisitInstruction (this);
131                 }
132         }
133 }