1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 package com.sun.org.apache.bcel.internal.generic; 6 7 /* ==================================================================== 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 2001 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, 26 * if any, must include the following acknowledgment: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowledgment may appear in the software itself, 30 * if and wherever such third-party acknowledgments normally appear. 31 * 32 * 4. The names "Apache" and "Apache Software Foundation" and 33 * "Apache BCEL" must not be used to endorse or promote products 34 * derived from this software without prior written permission. For 35 * written permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache", 38 * "Apache BCEL", nor may "Apache" appear in their name, without 39 * prior written permission of the Apache Software Foundation. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 */ 60 61 import java.io.*; 62 import com.sun.org.apache.bcel.internal.util.ByteSequence; 63 64 /** 65 * Abstract super class for branching instructions like GOTO, IFEQ, etc.. 66 * Branch instructions may have a variable length, namely GOTO, JSR, 67 * LOOKUPSWITCH and TABLESWITCH. 68 * 69 * @see InstructionList 70 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 71 */ 72 public abstract class BranchInstruction extends Instruction implements InstructionTargeter { 73 protected int index; // Branch target relative to this instruction 74 protected InstructionHandle target; // Target object in instruction list 75 protected int position; // Byte code offset 76 77 /** 78 * Empty constructor needed for the Class.newInstance() statement in 79 * Instruction.readInstruction(). Not to be used otherwise. 80 */ 81 BranchInstruction() {} 82 83 /** Common super constructor 84 * @param opcodee Instruction opcode 85 * @param target instruction to branch to 86 */ 87 protected BranchInstruction(short opcode, InstructionHandle target) { 88 super(opcode, (short)3); 89 setTarget(target); 90 } 91 92 /** 93 * Dump instruction as byte code to stream out. 94 * @param out Output stream 95 */ 96 public void dump(DataOutputStream out) throws IOException { 97 out.writeByte(opcode); 98 99 index = getTargetOffset(); 100 101 if(Math.abs(index) >= 32767) // too large for short 102 throw new ClassGenException("Branch target offset too large for short"); 103 104 out.writeShort(index); // May be negative, i.e., point backwards 105 } 106 107 /** 108 * @param target branch target 109 * @return the offset to `target' relative to this instruction 110 */ 111 protected int getTargetOffset(InstructionHandle target) { 112 if(target == null) 113 throw new ClassGenException("Target of " + super.toString(true) + 114 " is invalid null handle"); 115 116 int t = target.getPosition(); 117 118 if(t < 0) 119 throw new ClassGenException("Invalid branch target position offset for " + 120 super.toString(true) + ":" + t + ":" + target); 121 122 return t - position; 123 } 124 125 /** 126 * @return the offset to this instruction's target 127 */ 128 protected int getTargetOffset() { return getTargetOffset(target); } 129 130 /** 131 * Called by InstructionList.setPositions when setting the position for every 132 * instruction. In the presence of variable length instructions `setPositions' 133 * performs multiple passes over the instruction list to calculate the 134 * correct (byte) positions and offsets by calling this function. 135 * 136 * @param offset additional offset caused by preceding (variable length) instructions 137 * @param max_offset the maximum offset that may be caused by these instructions 138 * @return additional offset caused by possible change of this instruction's length 139 */ 140 protected int updatePosition(int offset, int max_offset) { 141 position += offset; 142 return 0; 143 } 144 145 /** 146 * Long output format: 147 * 148 * <position in byte code> 149 * <name of opcode> "["<opcode number>"]" 150 * "("<length of instruction>")" 151 * "<"<target instruction>">" "@"<branch target offset> 152 * 153 * @param verbose long/short format switch 154 * @return mnemonic for instruction 155 */ 156 public String toString(boolean verbose) { 157 String s = super.toString(verbose); 158 String t = "null"; 159 160 if(verbose) { 161 if(target != null) { 162 if(target.getInstruction() == this) 163 t = "<points to itself>"; 164 else if(target.getInstruction() == null) 165 t = "<null instruction!!!?>"; 166 else 167 t = target.getInstruction().toString(false); // Avoid circles 168 } 169 } else { 170 if(target != null) { 171 index = getTargetOffset(); 172 t = "" + (index + position); 173 } 174 } 175 176 return s + " -> " + t; 177 } 178 179 /** 180 * Read needed data (e.g. index) from file. Conversion to a InstructionHandle 181 * is done in InstructionList(byte[]). 182 * 183 * @param bytes input stream 184 * @param wide wide prefix? 185 * @see InstructionList 186 */ 187 protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException 188 { 189 length = 3; 190 index = bytes.readShort(); 191 } 192 193 /** 194 * @return target offset in byte code 195 */ 196 public final int getIndex() { return index; } 197 198 /** 199 * @return target of branch instruction 200 */ 201 public InstructionHandle getTarget() { return target; } 202 203 /** 204 * Set branch target 205 * @param target branch target 206 */ 207 public void setTarget(InstructionHandle target) { 208 notifyTarget(this.target, target, this); 209 this.target = target; 210 } 211 212 /** 213 * Used by BranchInstruction, LocalVariableGen, CodeExceptionGen 214 */ 215 static final void notifyTarget(InstructionHandle old_ih, InstructionHandle new_ih, 216 InstructionTargeter t) { 217 if(old_ih != null) 218 old_ih.removeTargeter(t); 219 if(new_ih != null) 220 new_ih.addTargeter(t); 221 } 222 223 /** 224 * @param old_ih old target 225 * @param new_ih new target 226 */ 227 public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) { 228 if(target == old_ih) 229 setTarget(new_ih); 230 else 231 throw new ClassGenException("Not targeting " + old_ih + ", but " + target); 232 } 233 234 /** 235 * @return true, if ih is target of this instruction 236 */ 237 public boolean containsTarget(InstructionHandle ih) { 238 return (target == ih); 239 } 240 241 /** 242 * Inform target that it's not targeted anymore. 243 */ 244 void dispose() { 245 setTarget(null); 246 index=-1; 247 position=-1; 248 } 249 }