286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/Apackage com.sun.org.apache.bcel.internal.generic;
286N/A
286N/A/* ====================================================================
286N/A * The Apache Software License, Version 1.1
286N/A *
286N/A * Copyright (c) 2001 The Apache Software Foundation. All rights
286N/A * reserved.
286N/A *
286N/A * Redistribution and use in source and binary forms, with or without
286N/A * modification, are permitted provided that the following conditions
286N/A * are met:
286N/A *
286N/A * 1. Redistributions of source code must retain the above copyright
286N/A * notice, this list of conditions and the following disclaimer.
286N/A *
286N/A * 2. Redistributions in binary form must reproduce the above copyright
286N/A * notice, this list of conditions and the following disclaimer in
286N/A * the documentation and/or other materials provided with the
286N/A * distribution.
286N/A *
286N/A * 3. The end-user documentation included with the redistribution,
286N/A * if any, must include the following acknowledgment:
286N/A * "This product includes software developed by the
286N/A * Apache Software Foundation (http://www.apache.org/)."
286N/A * Alternately, this acknowledgment may appear in the software itself,
286N/A * if and wherever such third-party acknowledgments normally appear.
286N/A *
286N/A * 4. The names "Apache" and "Apache Software Foundation" and
286N/A * "Apache BCEL" must not be used to endorse or promote products
286N/A * derived from this software without prior written permission. For
286N/A * written permission, please contact apache@apache.org.
286N/A *
286N/A * 5. Products derived from this software may not be called "Apache",
286N/A * "Apache BCEL", nor may "Apache" appear in their name, without
286N/A * prior written permission of the Apache Software Foundation.
286N/A *
286N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
286N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
286N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
286N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
286N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
286N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
286N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
286N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
286N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
286N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
286N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286N/A * SUCH DAMAGE.
286N/A * ====================================================================
286N/A *
286N/A * This software consists of voluntary contributions made by many
286N/A * individuals on behalf of the Apache Software Foundation. For more
286N/A * information on the Apache Software Foundation, please see
286N/A * <http://www.apache.org/>.
286N/A */
286N/Aimport java.io.*;
286N/Aimport com.sun.org.apache.bcel.internal.util.ByteSequence;
286N/A
286N/A/**
286N/A * Select - Abstract super class for LOOKUPSWITCH and TABLESWITCH instructions.
286N/A *
286N/A * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
286N/A * @see LOOKUPSWITCH
286N/A * @see TABLESWITCH
286N/A * @see InstructionList
286N/A */
286N/Apublic abstract class Select extends BranchInstruction
286N/A implements VariableLengthInstruction, StackProducer
286N/A{
286N/A protected int[] match; // matches, i.e., case 1: ...
286N/A protected int[] indices; // target offsets
286N/A protected InstructionHandle[] targets; // target objects in instruction list
286N/A protected int fixed_length; // fixed length defined by subclasses
286N/A protected int match_length; // number of cases
286N/A protected int padding = 0; // number of pad bytes for alignment
286N/A
286N/A /**
286N/A * Empty constructor needed for the Class.newInstance() statement in
286N/A * Instruction.readInstruction(). Not to be used otherwise.
286N/A */
286N/A Select() {}
286N/A
286N/A /**
286N/A * (Match, target) pairs for switch.
286N/A * `Match' and `targets' must have the same length of course.
286N/A *
286N/A * @param match array of matching values
286N/A * @param targets instruction targets
286N/A * @param target default instruction target
286N/A */
286N/A Select(short opcode, int[] match, InstructionHandle[] targets,
286N/A InstructionHandle target) {
286N/A super(opcode, target);
286N/A
286N/A this.targets = targets;
570N/A for(int i=0; i < targets.length; i++) {
570N/A BranchInstruction.notifyTargetChanged(targets[i], this);
570N/A }
286N/A
286N/A this.match = match;
286N/A
286N/A if((match_length = match.length) != targets.length)
286N/A throw new ClassGenException("Match and target array have not the same length");
286N/A
286N/A indices = new int[match_length];
286N/A }
286N/A
286N/A /**
286N/A * Since this is a variable length instruction, it may shift the following
286N/A * instructions which then need to update their position.
286N/A *
286N/A * Called by InstructionList.setPositions when setting the position for every
286N/A * instruction. In the presence of variable length instructions `setPositions'
286N/A * performs multiple passes over the instruction list to calculate the
286N/A * correct (byte) positions and offsets by calling this function.
286N/A *
286N/A * @param offset additional offset caused by preceding (variable length) instructions
286N/A * @param max_offset the maximum offset that may be caused by these instructions
286N/A * @return additional offset caused by possible change of this instruction's length
286N/A */
570N/A @Override
286N/A protected int updatePosition(int offset, int max_offset) {
286N/A position += offset; // Additional offset caused by preceding SWITCHs, GOTOs, etc.
286N/A
286N/A short old_length = length;
286N/A
286N/A /* Alignment on 4-byte-boundary, + 1, because of tag byte.
286N/A */
286N/A padding = (4 - ((position + 1) % 4)) % 4;
286N/A length = (short)(fixed_length + padding); // Update length
286N/A
286N/A return length - old_length;
286N/A }
286N/A
286N/A /**
286N/A * Dump instruction as byte code to stream out.
286N/A * @param out Output stream
286N/A */
570N/A @Override
286N/A public void dump(DataOutputStream out) throws IOException {
286N/A out.writeByte(opcode);
286N/A
286N/A for(int i=0; i < padding; i++) // Padding bytes
286N/A out.writeByte(0);
286N/A
286N/A index = getTargetOffset(); // Write default target offset
286N/A out.writeInt(index);
286N/A }
286N/A
286N/A /**
286N/A * Read needed data (e.g. index) from file.
286N/A */
570N/A @Override
286N/A protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
286N/A {
286N/A padding = (4 - (bytes.getIndex() % 4)) % 4; // Compute number of pad bytes
286N/A
286N/A for(int i=0; i < padding; i++) {
286N/A bytes.readByte();
286N/A }
286N/A
286N/A // Default branch target common for both cases (TABLESWITCH, LOOKUPSWITCH)
286N/A index = bytes.readInt();
286N/A }
286N/A
286N/A /**
286N/A * @return mnemonic for instruction
286N/A */
570N/A @Override
286N/A public String toString(boolean verbose) {
570N/A final StringBuilder buf = new StringBuilder(super.toString(verbose));
286N/A
286N/A if(verbose) {
286N/A for(int i=0; i < match_length; i++) {
286N/A String s = "null";
286N/A
286N/A if(targets[i] != null)
286N/A s = targets[i].getInstruction().toString();
286N/A
570N/A buf.append("(").append(match[i]).append(", ")
570N/A .append(s).append(" = {").append(indices[i]).append("})");
286N/A }
286N/A }
286N/A else
286N/A buf.append(" ...");
286N/A
286N/A return buf.toString();
286N/A }
286N/A
286N/A /**
286N/A * Set branch target for `i'th case
286N/A */
570N/A public final void setTarget(int i, InstructionHandle target) {
570N/A notifyTargetChanging(targets[i], this);
286N/A targets[i] = target;
570N/A notifyTargetChanged(targets[i], this);
286N/A }
286N/A
286N/A /**
286N/A * @param old_ih old target
286N/A * @param new_ih new target
286N/A */
570N/A @Override
286N/A public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
286N/A boolean targeted = false;
286N/A
286N/A if(target == old_ih) {
286N/A targeted = true;
286N/A setTarget(new_ih);
286N/A }
286N/A
286N/A for(int i=0; i < targets.length; i++) {
286N/A if(targets[i] == old_ih) {
286N/A targeted = true;
286N/A setTarget(i, new_ih);
286N/A }
286N/A }
286N/A
286N/A if(!targeted)
286N/A throw new ClassGenException("Not targeting " + old_ih);
286N/A }
286N/A
286N/A /**
286N/A * @return true, if ih is target of this instruction
286N/A */
570N/A @Override
286N/A public boolean containsTarget(InstructionHandle ih) {
286N/A if(target == ih)
286N/A return true;
286N/A
286N/A for(int i=0; i < targets.length; i++)
286N/A if(targets[i] == ih)
286N/A return true;
286N/A
286N/A return false;
286N/A }
286N/A
286N/A /**
286N/A * Inform targets that they're not targeted anymore.
286N/A */
570N/A @Override
286N/A void dispose() {
286N/A super.dispose();
286N/A
286N/A for(int i=0; i < targets.length; i++)
286N/A targets[i].removeTargeter(this);
286N/A }
286N/A
286N/A /**
286N/A * @return array of match indices
286N/A */
286N/A public int[] getMatchs() { return match; }
286N/A
286N/A /**
286N/A * @return array of match target offsets
286N/A */
286N/A public int[] getIndices() { return indices; }
286N/A
286N/A /**
286N/A * @return array of match targets
286N/A */
286N/A public InstructionHandle[] getTargets() { return targets; }
286N/A}