View Javadoc

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