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/A
286N/A/**
286N/A * SWITCH - Branch depending on int value, generates either LOOKUPSWITCH or
286N/A * TABLESWITCH instruction, depending on whether the match values (int[]) can be
286N/A * sorted with no gaps between the numbers.
286N/A *
286N/A * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
286N/A */
286N/Apublic final class SWITCH implements CompoundInstruction {
286N/A private int[] match;
286N/A private InstructionHandle[] targets;
286N/A private Select instruction;
286N/A private int match_length;
286N/A
286N/A /**
286N/A * Template for switch() constructs. If the match array can be
286N/A * sorted in ascending order with gaps no larger than max_gap
286N/A * between the numbers, a TABLESWITCH instruction is generated, and
286N/A * a LOOKUPSWITCH otherwise. The former may be more efficient, but
286N/A * needs more space.
286N/A *
286N/A * Note, that the key array always will be sorted, though we leave
286N/A * the original arrays unaltered.
286N/A *
286N/A * @param match array of match values (case 2: ... case 7: ..., etc.)
286N/A * @param targets the instructions to be branched to for each case
286N/A * @param target the default target
286N/A * @param max_gap maximum gap that may between case branches
286N/A */
286N/A public SWITCH(int[] match, InstructionHandle[] targets,
286N/A InstructionHandle target, int max_gap) {
286N/A this.match = (int[])match.clone();
286N/A this.targets = (InstructionHandle[])targets.clone();
286N/A
286N/A if((match_length = match.length) < 2) // (almost) empty switch, or just default
286N/A instruction = new TABLESWITCH(match, targets, target);
286N/A else {
286N/A sort(0, match_length - 1);
286N/A
286N/A if(matchIsOrdered(max_gap)) {
286N/A fillup(max_gap, target);
286N/A
286N/A instruction = new TABLESWITCH(this.match, this.targets, target);
286N/A }
286N/A else
286N/A instruction = new LOOKUPSWITCH(this.match, this.targets, target);
286N/A }
286N/A }
286N/A
286N/A public SWITCH(int[] match, InstructionHandle[] targets,
286N/A InstructionHandle target) {
286N/A this(match, targets, target, 1);
286N/A }
286N/A
286N/A private final void fillup(int max_gap, InstructionHandle target) {
286N/A int max_size = match_length + match_length * max_gap;
286N/A int[] m_vec = new int[max_size];
286N/A InstructionHandle[] t_vec = new InstructionHandle[max_size];
286N/A int count = 1;
286N/A
286N/A m_vec[0] = match[0];
286N/A t_vec[0] = targets[0];
286N/A
286N/A for(int i=1; i < match_length; i++) {
286N/A int prev = match[i-1];
286N/A int gap = match[i] - prev;
286N/A
286N/A for(int j=1; j < gap; j++) {
286N/A m_vec[count] = prev + j;
286N/A t_vec[count] = target;
286N/A count++;
286N/A }
286N/A
286N/A m_vec[count] = match[i];
286N/A t_vec[count] = targets[i];
286N/A count++;
286N/A }
286N/A
286N/A match = new int[count];
286N/A targets = new InstructionHandle[count];
286N/A
286N/A System.arraycopy(m_vec, 0, match, 0, count);
286N/A System.arraycopy(t_vec, 0, targets, 0, count);
286N/A }
286N/A
286N/A /**
286N/A * Sort match and targets array with QuickSort.
286N/A */
286N/A private final void sort(int l, int r) {
286N/A int i = l, j = r;
286N/A int h, m = match[(l + r) / 2];
286N/A InstructionHandle h2;
286N/A
286N/A do {
286N/A while(match[i] < m) i++;
286N/A while(m < match[j]) j--;
286N/A
286N/A if(i <= j) {
286N/A h=match[i]; match[i]=match[j]; match[j]=h; // Swap elements
286N/A h2=targets[i]; targets[i]=targets[j]; targets[j]=h2; // Swap instructions, too
286N/A i++; j--;
286N/A }
286N/A } while(i <= j);
286N/A
286N/A if(l < j) sort(l, j);
286N/A if(i < r) sort(i, r);
286N/A }
286N/A
286N/A /**
286N/A * @return match is sorted in ascending order with no gap bigger than max_gap?
286N/A */
286N/A private final boolean matchIsOrdered(int max_gap) {
286N/A for(int i=1; i < match_length; i++)
286N/A if(match[i] - match[i-1] > max_gap)
286N/A return false;
286N/A
286N/A return true;
286N/A }
286N/A
286N/A public final InstructionList getInstructionList() {
286N/A return new InstructionList(instruction);
286N/A }
286N/A
286N/A public final Instruction getInstruction() {
286N/A return instruction;
286N/A }
286N/A}