View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.jdo.impl.enhancer.classfile;
19  
20  
21  import java.io.PrintStream;
22  import java.util.Stack;
23  
24  /***
25   * Special instruction form for the opc_iinc instruction
26   */
27  public class InsnIInc extends Insn {
28  
29      /* The local variable slot to be incremented */
30      private int localVarIndex;
31  
32      /* The amount by which the slot is to be incremented */
33      private int value;
34  
35      /* public accessors */
36  
37      public int nStackArgs() {
38          return 0;
39      }
40  
41      public int nStackResults() {
42          return 0;
43      }
44  
45      /***
46       * What are the types of the stack operands ?
47       */
48      public String argTypes() {
49          return "";
50      }
51  
52      /***
53       * What are the types of the stack results?
54       */
55      public String resultTypes() {
56          return "";
57      }
58  
59      public boolean branches() {
60          return false;
61      }
62  
63      /***
64       * The local variable slot to be incremented
65       */
66      public int varIndex() {
67          return localVarIndex;
68      }
69  
70      /***
71       * The amount by which the slot is to be incremented 
72       */
73      public int incrValue() {
74          return value;
75      }
76    
77      /***
78       * Constructor for opc_iinc instruction
79       */
80      public InsnIInc (int localVarIndex, int value) {
81          this(localVarIndex, value, NO_OFFSET);
82      }
83  
84      /***
85       * Compares this instance with another for structural equality.
86       */
87      //@olsen: added method
88      public boolean isEqual(Stack msg, Object obj) {
89          if (!(obj instanceof InsnIInc)) {
90              msg.push("obj/obj.getClass() = "
91                       + (obj == null ? null : obj.getClass()));
92              msg.push("this.getClass() = "
93                       + this.getClass());
94              return false;
95          }
96          InsnIInc other = (InsnIInc)obj;
97  
98          if (!super.isEqual(msg, other)) {
99              return false;
100         }
101 
102         if (this.localVarIndex != other.localVarIndex) {
103             msg.push(String.valueOf("localVarIndex = "
104                                     + other.localVarIndex));
105             msg.push(String.valueOf("localVarIndex = "
106                                     + this.localVarIndex));
107             return false;
108         }
109         if (this.value != other.value) {
110             msg.push(String.valueOf("value = "
111                                     + other.value));
112             msg.push(String.valueOf("value = "
113                                     + this.value));
114             return false;
115         }
116         return true;
117     }
118 
119     /* package local methods *//package-summary/html">class="comment"> package local methods *//package-summary.html">/* package local methods *//package-summary.html">class="comment"> package local methods */
120 
121     InsnIInc (int localVarIndex, int value, int pc) {
122         super(opc_iinc, pc);
123 
124         this.localVarIndex = localVarIndex;
125         this.value =value;
126     }
127 
128     void print (PrintStream out, int indent) {
129         ClassPrint.spaces(out, indent);
130         out.println(offset() + "  opc_iinc  " + 
131                     localVarIndex + "," + value);
132     }
133 
134     int store(byte[] buf, int index) {
135         if (isWide())
136             buf[index++] = (byte) opc_wide;
137         buf[index++] = (byte) opcode();
138         if (isWide()) {
139             index = storeShort(buf, index, (short) localVarIndex);
140             index = storeShort(buf, index, (short) value);
141         } else {
142             buf[index++] = (byte)localVarIndex;
143             buf[index++] = (byte)value;
144         }
145         return index;
146     }
147 
148     int size() {
149         return isWide() ? 6 : 3;
150     }
151 
152     private boolean isWide() {
153         return (value > 127 || value < -128 || localVarIndex > 255);
154     }
155 
156 }