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. * Select - Abstract super class for LOOKUPSWITCH and TABLESWITCH instructions.
  59. *
  60. * @version $Id: Select.java,v 1.1.1.1 2001/10/29 20:00:27 jvanzyl Exp $
  61. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  62. * @see LOOKUPSWITCH
  63. * @see TABLESWITCH
  64. * @see InstructionList
  65. */
  66. public abstract class Select extends BranchInstruction
  67. implements VariableLengthInstruction, StackProducer
  68. {
  69. protected int[] match; // matches, i.e., case 1: ...
  70. protected int[] indices; // target offsets
  71. protected InstructionHandle[] targets; // target objects in instruction list
  72. protected int fixed_length; // fixed length defined by subclasses
  73. protected int match_length; // number of cases
  74. protected int padding = 0; // number of pad bytes for alignment
  75. /**
  76. * Empty constructor needed for the Class.newInstance() statement in
  77. * Instruction.readInstruction(). Not to be used otherwise.
  78. */
  79. Select() {}
  80. /**
  81. * (Match, target) pairs for switch.
  82. * `Match' and `targets' must have the same length of course.
  83. *
  84. * @param match array of matching values
  85. * @param targets instruction targets
  86. * @param target default instruction target
  87. */
  88. Select(short opcode, int[] match, InstructionHandle[] targets,
  89. InstructionHandle target) {
  90. super(opcode, target);
  91. this.targets = targets;
  92. for(int i=0; i < targets.length; i++)
  93. notifyTarget(null, targets[i], this);
  94. this.match = match;
  95. if((match_length = match.length) != targets.length)
  96. throw new ClassGenException("Match and target array have not the same length");
  97. indices = new int[match_length];
  98. }
  99. /**
  100. * Since this is a variable length instruction, it may shift the following
  101. * instructions which then need to update their position.
  102. *
  103. * Called by InstructionList.setPositions when setting the position for every
  104. * instruction. In the presence of variable length instructions `setPositions'
  105. * performs multiple passes over the instruction list to calculate the
  106. * correct (byte) positions and offsets by calling this function.
  107. *
  108. * @param offset additional offset caused by preceding (variable length) instructions
  109. * @param max_offset the maximum offset that may be caused by these instructions
  110. * @return additional offset caused by possible change of this instruction's length
  111. */
  112. protected int updatePosition(int offset, int max_offset) {
  113. position += offset; // Additional offset caused by preceding SWITCHs, GOTOs, etc.
  114. short old_length = length;
  115. /* Alignment on 4-byte-boundary, + 1, because of tag byte.
  116. */
  117. padding = (4 - ((position + 1) % 4)) % 4;
  118. length = (short)(fixed_length + padding); // Update length
  119. return length - old_length;
  120. }
  121. /**
  122. * Dump instruction as byte code to stream out.
  123. * @param out Output stream
  124. */
  125. public void dump(DataOutputStream out) throws IOException {
  126. out.writeByte(opcode);
  127. for(int i=0; i < padding; i++) // Padding bytes
  128. out.writeByte(0);
  129. index = getTargetOffset(); // Write default target offset
  130. out.writeInt(index);
  131. }
  132. /**
  133. * Read needed data (e.g. index) from file.
  134. */
  135. protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
  136. {
  137. padding = (4 - (bytes.getIndex() % 4)) % 4; // Compute number of pad bytes
  138. for(int i=0; i < padding; i++) {
  139. byte b;
  140. if((b=bytes.readByte()) != 0)
  141. throw new ClassGenException("Padding byte != 0: " + b);
  142. }
  143. // Default branch target common for both cases (TABLESWITCH, LOOKUPSWITCH)
  144. index = bytes.readInt();
  145. }
  146. /**
  147. * @return mnemonic for instruction
  148. */
  149. public String toString(boolean verbose) {
  150. StringBuffer buf = new StringBuffer(super.toString(verbose));
  151. if(verbose) {
  152. for(int i=0; i < match_length; i++) {
  153. String s = "null";
  154. if(targets[i] != null)
  155. s = targets[i].getInstruction().toString();
  156. buf.append("(" + match[i] + ", " + s + " = {" + indices[i] + "})");
  157. }
  158. }
  159. else
  160. buf.append(" ...");
  161. return buf.toString();
  162. }
  163. /**
  164. * Set branch target for `i'th case
  165. */
  166. public void setTarget(int i, InstructionHandle target) {
  167. notifyTarget(targets[i], target, this);
  168. targets[i] = target;
  169. }
  170. /**
  171. * @param old_ih old target
  172. * @param new_ih new target
  173. */
  174. public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
  175. boolean targeted = false;
  176. if(target == old_ih) {
  177. targeted = true;
  178. setTarget(new_ih);
  179. }
  180. for(int i=0; i < targets.length; i++) {
  181. if(targets[i] == old_ih) {
  182. targeted = true;
  183. setTarget(i, new_ih);
  184. }
  185. }
  186. if(!targeted)
  187. throw new ClassGenException("Not targeting " + old_ih);
  188. }
  189. /**
  190. * @return true, if ih is target of this instruction
  191. */
  192. public boolean containsTarget(InstructionHandle ih) {
  193. if(target == ih)
  194. return true;
  195. for(int i=0; i < targets.length; i++)
  196. if(targets[i] == ih)
  197. return true;
  198. return false;
  199. }
  200. /**
  201. * Inform targets that they're not targeted anymore.
  202. */
  203. void dispose() {
  204. super.dispose();
  205. for(int i=0; i < targets.length; i++)
  206. targets[i].removeTargeter(this);
  207. }
  208. /**
  209. * @return array of match indices
  210. */
  211. public int[] getMatchs() { return match; }
  212. /**
  213. * @return array of match target offsets
  214. */
  215. public int[] getIndices() { return indices; }
  216. /**
  217. * @return array of match targets
  218. */
  219. public InstructionHandle[] getTargets() { return targets; }
  220. }