1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.bcel.generic;
20
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import org.apache.bcel.Const;
25 import org.apache.bcel.util.ByteSequence;
26
27 /**
28 * Abstract super class for instructions dealing with local variables.
29 */
30 public abstract class LocalVariableInstruction extends Instruction implements TypedInstruction, IndexedInstruction {
31
32 /**
33 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
34 */
35 @Deprecated
36 protected int n = -1; // index of referenced variable
37
38 private short cTag = -1; // compact version, such as ILOAD_0
39 private short canonTag = -1; // canonical tag such as ILOAD
40
41 /**
42 * Empty constructor needed for Instruction.readInstruction. Also used by IINC()!
43 */
44 LocalVariableInstruction() {
45 }
46
47 /**
48 * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise. tag and length are defined in
49 * readInstruction and initFromFile, respectively.
50 */
51 LocalVariableInstruction(final short canonTag, final short cTag) {
52 this.canonTag = canonTag;
53 this.cTag = cTag;
54 }
55
56 /**
57 * @param opcode Instruction opcode
58 * @param cTag Instruction number for compact version, ALOAD_0, for example.
59 * @param n local variable index (unsigned short)
60 */
61 protected LocalVariableInstruction(final short opcode, final short cTag, final int n) {
62 super(opcode, (short) 2);
63 this.cTag = cTag;
64 canonTag = opcode;
65 setIndex(n);
66 }
67
68 /**
69 * Dump instruction as byte code to stream out.
70 *
71 * @param out Output stream
72 */
73 @Override
74 public void dump(final DataOutputStream out) throws IOException {
75 if (wide()) {
76 out.writeByte(Const.WIDE);
77 }
78 out.writeByte(super.getOpcode());
79 if (super.getLength() > 1) { // Otherwise ILOAD_n, instruction, for example.
80 if (wide()) {
81 out.writeShort(n);
82 } else {
83 out.writeByte(n);
84 }
85 }
86 }
87
88 /**
89 * @return canonical tag for instruction, for example, ALOAD for ALOAD_0
90 */
91 public short getCanonicalTag() {
92 return canonTag;
93 }
94
95 /**
96 * @return local variable index (n) referred by this instruction.
97 */
98 @Override
99 public final int getIndex() {
100 return n;
101 }
102
103 /**
104 * Returns the type associated with the instruction - in case of ALOAD or ASTORE Type.OBJECT is returned. This is just a
105 * bit incorrect, because ALOAD and ASTORE may work on every ReferenceType (including Type.NULL) and ASTORE may even
106 * work on a ReturnaddressType.
107 *
108 * @return type associated with the instruction
109 */
110 @Override
111 public Type getType(final ConstantPoolGen cp) {
112 switch (canonTag) {
113 case Const.ILOAD:
114 case Const.ISTORE:
115 return Type.INT;
116 case Const.LLOAD:
117 case Const.LSTORE:
118 return Type.LONG;
119 case Const.DLOAD:
120 case Const.DSTORE:
121 return Type.DOUBLE;
122 case Const.FLOAD:
123 case Const.FSTORE:
124 return Type.FLOAT;
125 case Const.ALOAD:
126 case Const.ASTORE:
127 return Type.OBJECT;
128 default:
129 throw new ClassGenException("Unknown case in switch" + canonTag);
130 }
131 }
132
133 /**
134 * Reads needed data (for example index) from file.
135 *
136 * <pre>
137 * (ILOAD <= tag <= ALOAD_3) || (ISTORE <= tag <= ASTORE_3)
138 * </pre>
139 */
140 @Override
141 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
142 if (wide) {
143 n = bytes.readUnsignedShort();
144 super.setLength(4);
145 } else {
146 final short opcode = super.getOpcode();
147 if (opcode >= Const.ILOAD && opcode <= Const.ALOAD || opcode >= Const.ISTORE && opcode <= Const.ASTORE) {
148 n = bytes.readUnsignedByte();
149 super.setLength(2);
150 } else {
151 if (opcode <= Const.ALOAD_3) { // compact load instruction such as ILOAD_2
152 n = (opcode - Const.ILOAD_0) % 4;
153 } else { // Assert ISTORE_0 <= tag <= ASTORE_3
154 n = (opcode - Const.ISTORE_0) % 4;
155 }
156 super.setLength(1);
157 }
158 }
159 }
160
161 /**
162 * Sets the local variable index. also updates opcode and length TODO Why?
163 *
164 * @see #setIndexOnly(int)
165 */
166 @Override
167 public void setIndex(final int n) { // TODO could be package-protected?
168 if (n < 0 || n > Const.MAX_SHORT) {
169 throw new ClassGenException("Illegal value: " + n);
170 }
171 this.n = n;
172 // Cannot be < 0 as this is checked above
173 if (n <= 3) { // Use more compact instruction xLOAD_n
174 super.setOpcode((short) (cTag + n));
175 super.setLength(1);
176 } else {
177 super.setOpcode(canonTag);
178 if (wide()) {
179 super.setLength(4);
180 } else {
181 super.setLength(2);
182 }
183 }
184 }
185
186 /**
187 * Sets the index of the referenced variable (n) only
188 *
189 * @since 6.0
190 * @see #setIndex(int)
191 */
192 final void setIndexOnly(final int n) {
193 this.n = n;
194 }
195
196 /**
197 * Long output format:
198 *
199 * <name of opcode> "["<opcode number>"]" "("<length of instruction>")" "<"< local variable
200 * index>">"
201 *
202 * @param verbose long/short format switch
203 * @return mnemonic for instruction
204 */
205 @Override
206 public String toString(final boolean verbose) {
207 final short opcode = super.getOpcode();
208 if (opcode >= Const.ILOAD_0 && opcode <= Const.ALOAD_3 || opcode >= Const.ISTORE_0 && opcode <= Const.ASTORE_3) {
209 return super.toString(verbose);
210 }
211 return super.toString(verbose) + " " + n;
212 }
213
214 private boolean wide() {
215 return n > Const.MAX_BYTE;
216 }
217 }