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. /**
  58. * Abstract super class for branching instructions like GOTO, IFEQ, etc..
  59. * Branch instructions may have a variable length, namely GOTO, JSR,
  60. * LOOKUPSWITCH and TABLESWITCH.
  61. *
  62. * @see InstructionList
  63. * @version $Id: BranchInstruction.java,v 1.1.1.1 2001/10/29 20:00:07 jvanzyl Exp $
  64. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  65. */
  66. public abstract class BranchInstruction extends Instruction implements InstructionTargeter {
  67. protected int index; // Branch target relative to this instruction
  68. protected InstructionHandle target; // Target object in instruction list
  69. protected int position; // Byte code offset
  70. /**
  71. * Empty constructor needed for the Class.newInstance() statement in
  72. * Instruction.readInstruction(). Not to be used otherwise.
  73. */
  74. BranchInstruction() {}
  75. /** Common super constructor
  76. * @param opcodee Instruction opcode
  77. * @param target instruction to branch to
  78. */
  79. protected BranchInstruction(short opcode, InstructionHandle target) {
  80. super(opcode, (short)3);
  81. setTarget(target);
  82. }
  83. /**
  84. * Dump instruction as byte code to stream out.
  85. * @param out Output stream
  86. */
  87. public void dump(DataOutputStream out) throws IOException {
  88. out.writeByte(opcode);
  89. index = getTargetOffset();
  90. if(Math.abs(index) >= 32767) // too large for short
  91. throw new ClassGenException("Branch target offset too large for short");
  92. out.writeShort(index); // May be negative, i.e., point backwards
  93. }
  94. /**
  95. * @param target branch target
  96. * @return the offset to `target' relative to this instruction
  97. */
  98. protected int getTargetOffset(InstructionHandle target) {
  99. if(target == null)
  100. throw new ClassGenException("Target of " + super.toString(true) +
  101. " is invalid null handle");
  102. int t = target.getPosition();
  103. if(t < 0)
  104. throw new ClassGenException("Invalid branch target position offset for " +
  105. super.toString(true) + ":" + t + ":" + target);
  106. return t - position;
  107. }
  108. /**
  109. * @return the offset to this instruction's target
  110. */
  111. protected int getTargetOffset() { return getTargetOffset(target); }
  112. /**
  113. * Called by InstructionList.setPositions when setting the position for every
  114. * instruction. In the presence of variable length instructions `setPositions'
  115. * performs multiple passes over the instruction list to calculate the
  116. * correct (byte) positions and offsets by calling this function.
  117. *
  118. * @param offset additional offset caused by preceding (variable length) instructions
  119. * @param max_offset the maximum offset that may be caused by these instructions
  120. * @return additional offset caused by possible change of this instruction's length
  121. */
  122. protected int updatePosition(int offset, int max_offset) {
  123. position += offset;
  124. return 0;
  125. }
  126. /**
  127. * Long output format:
  128. *
  129. * <position in byte code>
  130. * <name of opcode> "["<opcode number>"]"
  131. * "("<length of instruction>")"
  132. * "<"<target instruction>">" "@"<branch target offset>
  133. *
  134. * @param verbose long/short format switch
  135. * @return mnemonic for instruction
  136. */
  137. public String toString(boolean verbose) {
  138. String s = super.toString(verbose);
  139. String t = "null";
  140. if(verbose) {
  141. if(target != null) {
  142. if(target.getInstruction() == this)
  143. t = "<points to itself>";
  144. else if(target.getInstruction() == null)
  145. t = "<null instruction!!!?>";
  146. else
  147. t = target.getInstruction().toString(false); // Avoid circles
  148. }
  149. } else {
  150. if(target != null) {
  151. index = getTargetOffset();
  152. t = "" + (index + position);
  153. }
  154. }
  155. return s + " -> " + t;
  156. }
  157. /**
  158. * Read needed data (e.g. index) from file. Conversion to a InstructionHandle
  159. * is done in InstructionList(byte[]).
  160. *
  161. * @param bytes input stream
  162. * @param wide wide prefix?
  163. * @see InstructionList
  164. */
  165. protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
  166. {
  167. length = 3;
  168. index = bytes.readShort();
  169. }
  170. /**
  171. * @return target offset in byte code
  172. */
  173. public final int getIndex() { return index; }
  174. /**
  175. * @return target of branch instruction
  176. */
  177. public InstructionHandle getTarget() { return target; }
  178. /**
  179. * Set branch target
  180. * @param target branch target
  181. */
  182. public void setTarget(InstructionHandle target) {
  183. notifyTarget(this.target, target, this);
  184. this.target = target;
  185. }
  186. /**
  187. * Used by BranchInstruction, LocalVariableGen, CodeExceptionGen
  188. */
  189. static final void notifyTarget(InstructionHandle old_ih, InstructionHandle new_ih,
  190. InstructionTargeter t) {
  191. if(old_ih != null)
  192. old_ih.removeTargeter(t);
  193. if(new_ih != null)
  194. new_ih.addTargeter(t);
  195. }
  196. /**
  197. * @param old_ih old target
  198. * @param new_ih new target
  199. */
  200. public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
  201. if(target == old_ih)
  202. setTarget(new_ih);
  203. else
  204. throw new ClassGenException("Not targeting " + old_ih + ", but " + target);
  205. }
  206. /**
  207. * @return true, if ih is target of this instruction
  208. */
  209. public boolean containsTarget(InstructionHandle ih) {
  210. return (target == ih);
  211. }
  212. /**
  213. * Inform target that it's not targeted anymore.
  214. */
  215. void dispose() {
  216. setTarget(null);
  217. index=-1;
  218. position=-1;
  219. }
  220. }