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