1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.generic; 23 24 /** 25 * SWITCH - Branch depending on int value, generates either LOOKUPSWITCH or 26 * TABLESWITCH instruction, depending on whether the match values (int[]) can be 27 * sorted with no gaps between the numbers. 28 * 29 * @version $Id$ 30 */ 31 public final class SWITCH implements CompoundInstruction { 32 33 private int[] match; 34 private InstructionHandle[] targets; 35 private Select instruction; 36 private int match_length; 37 38 39 /** 40 * Template for switch() constructs. If the match array can be 41 * sorted in ascending order with gaps no larger than max_gap 42 * between the numbers, a TABLESWITCH instruction is generated, and 43 * a LOOKUPSWITCH otherwise. The former may be more efficient, but 44 * needs more space. 45 * 46 * Note, that the key array always will be sorted, though we leave 47 * the original arrays unaltered. 48 * 49 * @param match array of match values (case 2: ... case 7: ..., etc.) 50 * @param targets the instructions to be branched to for each case 51 * @param target the default target 52 * @param max_gap maximum gap that may between case branches 53 */ 54 public SWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle target, final int max_gap) { 55 this.match = match.clone(); 56 this.targets = targets.clone(); 57 if ((match_length = match.length) < 2) { 58 instruction = new TABLESWITCH(match, targets, target); 59 } else { 60 sort(0, match_length - 1); 61 if (matchIsOrdered(max_gap)) { 62 fillup(max_gap, target); 63 instruction = new TABLESWITCH(this.match, this.targets, target); 64 } else { 65 instruction = new LOOKUPSWITCH(this.match, this.targets, target); 66 } 67 } 68 } 69 70 71 public SWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle target) { 72 this(match, targets, target, 1); 73 } 74 75 76 private void fillup( final int max_gap, final InstructionHandle target ) { 77 final int max_size = match_length + match_length * max_gap; 78 final int[] m_vec = new int[max_size]; 79 final InstructionHandle[] t_vec = new InstructionHandle[max_size]; 80 int count = 1; 81 m_vec[0] = match[0]; 82 t_vec[0] = targets[0]; 83 for (int i = 1; i < match_length; i++) { 84 final int prev = match[i - 1]; 85 final int gap = match[i] - prev; 86 for (int j = 1; j < gap; j++) { 87 m_vec[count] = prev + j; 88 t_vec[count] = target; 89 count++; 90 } 91 m_vec[count] = match[i]; 92 t_vec[count] = targets[i]; 93 count++; 94 } 95 match = new int[count]; 96 targets = new InstructionHandle[count]; 97 System.arraycopy(m_vec, 0, match, 0, count); 98 System.arraycopy(t_vec, 0, targets, 0, count); 99 } 100 101 102 /** 103 * Sort match and targets array with QuickSort. 104 */ 105 private void sort( final int l, final int r ) { 106 int i = l; 107 int j = r; 108 int h; 109 final int m = match[(l + r) / 2]; 110 InstructionHandle h2; 111 do { 112 while (match[i] < m) { 113 i++; 114 } 115 while (m < match[j]) { 116 j--; 117 } 118 if (i <= j) { 119 h = match[i]; 120 match[i] = match[j]; 121 match[j] = h; // Swap elements 122 h2 = targets[i]; 123 targets[i] = targets[j]; 124 targets[j] = h2; // Swap instructions, too 125 i++; 126 j--; 127 } 128 } while (i <= j); 129 if (l < j) { 130 sort(l, j); 131 } 132 if (i < r) { 133 sort(i, r); 134 } 135 } 136 137 138 /** 139 * @return match is sorted in ascending order with no gap bigger than max_gap? 140 */ 141 private boolean matchIsOrdered( final int max_gap ) { 142 for (int i = 1; i < match_length; i++) { 143 if (match[i] - match[i - 1] > max_gap) { 144 return false; 145 } 146 } 147 return true; 148 } 149 150 151 @Override 152 public final InstructionList getInstructionList() { 153 return new InstructionList(instruction); 154 } 155 156 157 public final Instruction getInstruction() { 158 return instruction; 159 } 160 }