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

This page was automatically generated by Maven