6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
package com.sun.org.apache.bcel.internal.generic;
|
|
6 |
|
|
7 |
/* ====================================================================
|
|
8 |
* The Apache Software License, Version 1.1
|
|
9 |
*
|
|
10 |
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
|
11 |
* reserved.
|
|
12 |
*
|
|
13 |
* Redistribution and use in source and binary forms, with or without
|
|
14 |
* modification, are permitted provided that the following conditions
|
|
15 |
* are met:
|
|
16 |
*
|
|
17 |
* 1. Redistributions of source code must retain the above copyright
|
|
18 |
* notice, this list of conditions and the following disclaimer.
|
|
19 |
*
|
|
20 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
21 |
* notice, this list of conditions and the following disclaimer in
|
|
22 |
* the documentation and/or other materials provided with the
|
|
23 |
* distribution.
|
|
24 |
*
|
|
25 |
* 3. The end-user documentation included with the redistribution,
|
|
26 |
* if any, must include the following acknowledgment:
|
|
27 |
* "This product includes software developed by the
|
|
28 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
29 |
* Alternately, this acknowledgment may appear in the software itself,
|
|
30 |
* if and wherever such third-party acknowledgments normally appear.
|
|
31 |
*
|
|
32 |
* 4. The names "Apache" and "Apache Software Foundation" and
|
|
33 |
* "Apache BCEL" must not be used to endorse or promote products
|
|
34 |
* derived from this software without prior written permission. For
|
|
35 |
* written permission, please contact apache@apache.org.
|
|
36 |
*
|
|
37 |
* 5. Products derived from this software may not be called "Apache",
|
|
38 |
* "Apache BCEL", nor may "Apache" appear in their name, without
|
|
39 |
* prior written permission of the Apache Software Foundation.
|
|
40 |
*
|
|
41 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
42 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
43 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
44 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
45 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
46 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
47 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
48 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
49 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
50 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
51 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
52 |
* SUCH DAMAGE.
|
|
53 |
* ====================================================================
|
|
54 |
*
|
|
55 |
* This software consists of voluntary contributions made by many
|
|
56 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
57 |
* information on the Apache Software Foundation, please see
|
|
58 |
* <http://www.apache.org/>.
|
|
59 |
*/
|
|
60 |
import java.io.*;
|
|
61 |
import com.sun.org.apache.bcel.internal.util.ByteSequence;
|
|
62 |
import com.sun.org.apache.bcel.internal.classfile.Utility;
|
|
63 |
import com.sun.org.apache.bcel.internal.Constants;
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Abstract super class for instructions dealing with local variables.
|
|
67 |
*
|
|
68 |
* @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
|
|
69 |
*/
|
|
70 |
public abstract class LocalVariableInstruction extends Instruction
|
|
71 |
implements TypedInstruction, IndexedInstruction {
|
|
72 |
protected int n = -1; // index of referenced variable
|
|
73 |
private short c_tag = -1; // compact version, such as ILOAD_0
|
|
74 |
private short canon_tag = -1; // canonical tag such as ILOAD
|
|
75 |
|
|
76 |
private final boolean wide() { return n > Constants.MAX_BYTE; }
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Empty constructor needed for the Class.newInstance() statement in
|
|
80 |
* Instruction.readInstruction(). Not to be used otherwise.
|
|
81 |
* tag and length are defined in readInstruction and initFromFile, respectively.
|
|
82 |
*/
|
|
83 |
LocalVariableInstruction(short canon_tag, short c_tag) {
|
|
84 |
super();
|
|
85 |
this.canon_tag = canon_tag;
|
|
86 |
this.c_tag = c_tag;
|
|
87 |
}
|
|
88 |
|
|
89 |
/**
|
|
90 |
* Empty constructor needed for the Class.newInstance() statement in
|
|
91 |
* Instruction.readInstruction(). Also used by IINC()!
|
|
92 |
*/
|
|
93 |
LocalVariableInstruction() {
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
|
97 |
* @param opcode Instruction opcode
|
|
98 |
* @param c_tag Instruction number for compact version, ALOAD_0, e.g.
|
|
99 |
* @param n local variable index (unsigned short)
|
|
100 |
*/
|
|
101 |
protected LocalVariableInstruction(short opcode, short c_tag, int n) {
|
|
102 |
super(opcode, (short)2);
|
|
103 |
|
|
104 |
this.c_tag = c_tag;
|
|
105 |
canon_tag = opcode;
|
|
106 |
|
|
107 |
setIndex(n);
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Dump instruction as byte code to stream out.
|
|
112 |
* @param out Output stream
|
|
113 |
*/
|
|
114 |
public void dump(DataOutputStream out) throws IOException {
|
|
115 |
if(wide()) // Need WIDE prefix ?
|
|
116 |
out.writeByte(Constants.WIDE);
|
|
117 |
|
|
118 |
out.writeByte(opcode);
|
|
119 |
|
|
120 |
if(length > 1) { // Otherwise ILOAD_n, instruction, e.g.
|
|
121 |
if(wide())
|
|
122 |
out.writeShort(n);
|
|
123 |
else
|
|
124 |
out.writeByte(n);
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Long output format:
|
|
130 |
*
|
|
131 |
* <name of opcode> "["<opcode number>"]"
|
|
132 |
* "("<length of instruction>")" "<"< local variable index>">"
|
|
133 |
*
|
|
134 |
* @param verbose long/short format switch
|
|
135 |
* @return mnemonic for instruction
|
|
136 |
*/
|
|
137 |
public String toString(boolean verbose) {
|
|
138 |
if(((opcode >= Constants.ILOAD_0) &&
|
|
139 |
(opcode <= Constants.ALOAD_3)) ||
|
|
140 |
((opcode >= Constants.ISTORE_0) &&
|
|
141 |
(opcode <= Constants.ASTORE_3)))
|
|
142 |
return super.toString(verbose);
|
|
143 |
else
|
|
144 |
return super.toString(verbose) + " " + n;
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Read needed data (e.g. index) from file.
|
|
149 |
* PRE: (ILOAD <= tag <= ALOAD_3) || (ISTORE <= tag <= ASTORE_3)
|
|
150 |
*/
|
|
151 |
protected void initFromFile(ByteSequence bytes, boolean wide)
|
|
152 |
throws IOException
|
|
153 |
{
|
|
154 |
if(wide) {
|
|
155 |
n = bytes.readUnsignedShort();
|
|
156 |
length = 4;
|
|
157 |
} else if(((opcode >= Constants.ILOAD) &&
|
|
158 |
(opcode <= Constants.ALOAD)) ||
|
|
159 |
((opcode >= Constants.ISTORE) &&
|
|
160 |
(opcode <= Constants.ASTORE))) {
|
|
161 |
n = bytes.readUnsignedByte();
|
|
162 |
length = 2;
|
|
163 |
} else if(opcode <= Constants.ALOAD_3) { // compact load instruction such as ILOAD_2
|
|
164 |
n = (opcode - Constants.ILOAD_0) % 4;
|
|
165 |
length = 1;
|
|
166 |
} else { // Assert ISTORE_0 <= tag <= ASTORE_3
|
|
167 |
n = (opcode - Constants.ISTORE_0) % 4;
|
|
168 |
length = 1;
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* @return local variable index referred by this instruction.
|
|
174 |
*/
|
|
175 |
public final int getIndex() { return n; }
|
|
176 |
|
|
177 |
/**
|
|
178 |
* Set the local variable index
|
|
179 |
*/
|
|
180 |
public void setIndex(int n) {
|
|
181 |
if((n < 0) || (n > Constants.MAX_SHORT))
|
|
182 |
throw new ClassGenException("Illegal value: " + n);
|
|
183 |
|
|
184 |
this.n = n;
|
|
185 |
|
|
186 |
if(n >= 0 && n <= 3) { // Use more compact instruction xLOAD_n
|
|
187 |
opcode = (short)(c_tag + n);
|
|
188 |
length = 1;
|
|
189 |
} else {
|
|
190 |
opcode = canon_tag;
|
|
191 |
|
|
192 |
if(wide()) // Need WIDE prefix ?
|
|
193 |
length = 4;
|
|
194 |
else
|
|
195 |
length = 2;
|
|
196 |
}
|
|
197 |
}
|
|
198 |
|
|
199 |
/** @return canonical tag for instruction, e.g., ALOAD for ALOAD_0
|
|
200 |
*/
|
|
201 |
public short getCanonicalTag() {
|
|
202 |
return canon_tag;
|
|
203 |
}
|
|
204 |
|
|
205 |
/**
|
|
206 |
* Returns the type associated with the instruction -
|
|
207 |
* in case of ALOAD or ASTORE Type.OBJECT is returned.
|
|
208 |
* This is just a bit incorrect, because ALOAD and ASTORE
|
|
209 |
* may work on every ReferenceType (including Type.NULL) and
|
|
210 |
* ASTORE may even work on a ReturnaddressType .
|
|
211 |
* @return type associated with the instruction
|
|
212 |
*/
|
|
213 |
public Type getType(ConstantPoolGen cp) {
|
|
214 |
switch(canon_tag) {
|
|
215 |
case Constants.ILOAD: case Constants.ISTORE:
|
|
216 |
return Type.INT;
|
|
217 |
case Constants.LLOAD: case Constants.LSTORE:
|
|
218 |
return Type.LONG;
|
|
219 |
case Constants.DLOAD: case Constants.DSTORE:
|
|
220 |
return Type.DOUBLE;
|
|
221 |
case Constants.FLOAD: case Constants.FSTORE:
|
|
222 |
return Type.FLOAT;
|
|
223 |
case Constants.ALOAD: case Constants.ASTORE:
|
|
224 |
return Type.OBJECT;
|
|
225 |
|
|
226 |
default: throw new ClassGenException("Oops: unknown case in switch" + canon_tag);
|
|
227 |
}
|
|
228 |
}
|
|
229 |
}
|