You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LocalVariableInstruction.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package org.aspectj.apache.bcel.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.DataOutputStream;
  56. import java.io.IOException;
  57. import org.aspectj.apache.bcel.Constants;
  58. import org.aspectj.apache.bcel.util.ByteSequence;
  59. /**
  60. * Abstract super class for instructions dealing with local variables.
  61. *
  62. * @version $Id: LocalVariableInstruction.java,v 1.5 2006/11/29 13:43:01 aclement 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. * &lt;name of opcode&gt; "["&lt;opcode number&gt;"]"
  117. * "("&lt;length of instruction&gt;")" "&lt;"&lt; local variable index&gt;"&gt;"
  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. } else if(((opcode >= Constants.ILOAD) &&
  141. (opcode <= Constants.ALOAD)) ||
  142. ((opcode >= Constants.ISTORE) &&
  143. (opcode <= Constants.ASTORE))) {
  144. n = bytes.readUnsignedByte();
  145. } else if(opcode <= Constants.ALOAD_3) { // compact load instruction such as ILOAD_2
  146. n = (opcode - Constants.ILOAD_0) % 4;
  147. } else { // Assert ISTORE_0 <= tag <= ASTORE_3
  148. n = (opcode - Constants.ISTORE_0) % 4;
  149. }
  150. workOutLength();
  151. }
  152. private void workOutLength() {
  153. if(n >= 0 && n <= 3) { // Use more compact instruction xLOAD_n
  154. opcode = (short)(c_tag + n);
  155. length = 1;
  156. } else {
  157. opcode = canon_tag;
  158. if(wide()) length = 4;
  159. else length = 2;
  160. }
  161. }
  162. /**
  163. * @return local variable index referred by this instruction.
  164. */
  165. public final int getIndex() { return n; }
  166. /**
  167. * Set the local variable index
  168. */
  169. public void setIndex(int n) {
  170. if((n < 0) || (n > Constants.MAX_SHORT))
  171. throw new ClassGenException("Illegal value: " + n);
  172. this.n = n;
  173. workOutLength();
  174. }
  175. /** @return canonical tag for instruction, e.g., ALOAD for ALOAD_0
  176. */
  177. public short getCanonicalTag() {
  178. return canon_tag;
  179. }
  180. /**
  181. * Returns the type associated with the instruction -
  182. * in case of ALOAD or ASTORE Type.OBJECT is returned.
  183. * This is just a bit incorrect, because ALOAD and ASTORE
  184. * may work on every ReferenceType (including Type.NULL) and
  185. * ASTORE may even work on a ReturnaddressType .
  186. * @return type associated with the instruction
  187. */
  188. public Type getType(ConstantPoolGen cp) {
  189. switch(canon_tag) {
  190. case Constants.ILOAD: case Constants.ISTORE:
  191. return Type.INT;
  192. case Constants.LLOAD: case Constants.LSTORE:
  193. return Type.LONG;
  194. case Constants.DLOAD: case Constants.DSTORE:
  195. return Type.DOUBLE;
  196. case Constants.FLOAD: case Constants.FSTORE:
  197. return Type.FLOAT;
  198. case Constants.ALOAD: case Constants.ASTORE:
  199. return Type.OBJECT;
  200. default: throw new ClassGenException("Oops: unknown case in switch" + canon_tag);
  201. }
  202. }
  203. }