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