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. /**
  56. * SWITCH - Branch depending on int value, generates either LOOKUPSWITCH or
  57. * TABLESWITCH instruction, depending on whether the match values (int[]) can be
  58. * sorted with no gaps between the numbers.
  59. *
  60. * @version $Id: SWITCH.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. */
  63. public final class SWITCH implements CompoundInstruction {
  64. private int[] match;
  65. private InstructionHandle[] targets;
  66. private Select instruction;
  67. private int match_length;
  68. /**
  69. * Template for switch() constructs. If the match array can be
  70. * sorted in ascending order with gaps no larger than max_gap
  71. * between the numbers, a TABLESWITCH instruction is generated, and
  72. * a LOOKUPSWITCH otherwise. The former may be more efficient, but
  73. * needs more space.
  74. *
  75. * Note, that the key array always will be sorted, though we leave
  76. * the original arrays unaltered.
  77. *
  78. * @param match array of match values (case 2: ... case 7: ..., etc.)
  79. * @param targets the instructions to be branched to for each case
  80. * @param target the default target
  81. * @param max_gap maximum gap that may between case branches
  82. */
  83. public SWITCH(int[] match, InstructionHandle[] targets,
  84. InstructionHandle target, int max_gap) {
  85. this.match = (int[])match.clone();
  86. this.targets = (InstructionHandle[])targets.clone();
  87. if((match_length = match.length) < 2) // (almost) empty switch, or just default
  88. instruction = new TABLESWITCH(match, targets, target);
  89. else {
  90. sort(0, match_length - 1);
  91. if(matchIsOrdered(max_gap)) {
  92. fillup(max_gap, target);
  93. instruction = new TABLESWITCH(this.match, this.targets, target);
  94. }
  95. else
  96. instruction = new LOOKUPSWITCH(this.match, this.targets, target);
  97. }
  98. }
  99. public SWITCH(int[] match, InstructionHandle[] targets,
  100. InstructionHandle target) {
  101. this(match, targets, target, 1);
  102. }
  103. private final void fillup(int max_gap, InstructionHandle target) {
  104. int max_size = match_length + match_length * max_gap;
  105. int[] m_vec = new int[max_size];
  106. InstructionHandle[] t_vec = new InstructionHandle[max_size];
  107. int count = 1;
  108. m_vec[0] = match[0];
  109. t_vec[0] = targets[0];
  110. for(int i=1; i < match_length; i++) {
  111. int prev = match[i-1];
  112. int gap = match[i] - prev;
  113. for(int j=1; j < gap; j++) {
  114. m_vec[count] = prev + j;
  115. t_vec[count] = target;
  116. count++;
  117. }
  118. m_vec[count] = match[i];
  119. t_vec[count] = targets[i];
  120. count++;
  121. }
  122. match = new int[count];
  123. targets = new InstructionHandle[count];
  124. System.arraycopy(m_vec, 0, match, 0, count);
  125. System.arraycopy(t_vec, 0, targets, 0, count);
  126. }
  127. /**
  128. * Sort match and targets array with QuickSort.
  129. */
  130. private final void sort(int l, int r) {
  131. int i = l, j = r;
  132. int h, m = match[(l + r) / 2];
  133. InstructionHandle h2;
  134. do {
  135. while(match[i] < m) i++;
  136. while(m < match[j]) j--;
  137. if(i <= j) {
  138. h=match[i]; match[i]=match[j]; match[j]=h; // Swap elements
  139. h2=targets[i]; targets[i]=targets[j]; targets[j]=h2; // Swap instructions, too
  140. i++; j--;
  141. }
  142. } while(i <= j);
  143. if(l < j) sort(l, j);
  144. if(i < r) sort(i, r);
  145. }
  146. /**
  147. * @return match is sorted in ascending order with no gap bigger than max_gap?
  148. */
  149. private final boolean matchIsOrdered(int max_gap) {
  150. for(int i=1; i < match_length; i++)
  151. if(match[i] - match[i-1] > max_gap)
  152. return false;
  153. return true;
  154. }
  155. public final InstructionList getInstructionList() {
  156. return new InstructionList(instruction);
  157. }
  158. public final Instruction getInstruction() {
  159. return instruction;
  160. }
  161. }