You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SwitchBuilder.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package org.aspectj.apache.bcel.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 (https://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. * <https://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: SwitchBuilder.java,v 1.2 2008/05/28 23:52:57 aclement Exp $
  61. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  62. */
  63. public final class SwitchBuilder {
  64. private int[] match;
  65. private InstructionHandle[] targets;
  66. private InstructionSelect 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 SwitchBuilder(int[] match, InstructionHandle[] targets,InstructionHandle target, int max_gap) {
  84. this.match = match.clone();
  85. this.targets = targets.clone();
  86. if((match_length = match.length) < 2) // (almost) empty switch, or just default
  87. if (match.length==0) {
  88. instruction = new LOOKUPSWITCH(match,targets,target);
  89. } else {
  90. instruction = new TABLESWITCH(match,targets,target);
  91. }
  92. else {
  93. sort(0, match_length - 1);
  94. if(matchIsOrdered(max_gap)) {
  95. fillup(max_gap, target);
  96. instruction = new TABLESWITCH(this.match, this.targets, target);
  97. }
  98. else
  99. instruction = new LOOKUPSWITCH(this.match, this.targets, target);
  100. }
  101. }
  102. public SwitchBuilder(int[] match, InstructionHandle[] targets, InstructionHandle target) {
  103. this(match, targets, target, 1);
  104. }
  105. private final void fillup(int max_gap, InstructionHandle target) {
  106. int max_size = match_length + match_length * max_gap;
  107. int[] m_vec = new int[max_size];
  108. InstructionHandle[] t_vec = new InstructionHandle[max_size];
  109. int count = 1;
  110. m_vec[0] = match[0];
  111. t_vec[0] = targets[0];
  112. for(int i=1; i < match_length; i++) {
  113. int prev = match[i-1];
  114. int gap = match[i] - prev;
  115. for(int j=1; j < gap; j++) {
  116. m_vec[count] = prev + j;
  117. t_vec[count] = target;
  118. count++;
  119. }
  120. m_vec[count] = match[i];
  121. t_vec[count] = targets[i];
  122. count++;
  123. }
  124. match = new int[count];
  125. targets = new InstructionHandle[count];
  126. System.arraycopy(m_vec, 0, match, 0, count);
  127. System.arraycopy(t_vec, 0, targets, 0, count);
  128. }
  129. /**
  130. * Sort match and targets array with QuickSort.
  131. */
  132. private final void sort(int l, int r) {
  133. int i = l, j = r;
  134. int h, m = match[(l + r) / 2];
  135. InstructionHandle h2;
  136. do {
  137. while(match[i] < m) i++;
  138. while(m < match[j]) j--;
  139. if(i <= j) {
  140. h=match[i]; match[i]=match[j]; match[j]=h; // Swap elements
  141. h2=targets[i]; targets[i]=targets[j]; targets[j]=h2; // Swap instructions, too
  142. i++; j--;
  143. }
  144. } while(i <= j);
  145. if(l < j) sort(l, j);
  146. if(i < r) sort(i, r);
  147. }
  148. /**
  149. * @return match is sorted in ascending order with no gap bigger than max_gap?
  150. */
  151. private final boolean matchIsOrdered(int max_gap) {
  152. for(int i=1; i < match_length; i++) {
  153. int diff = (match[i]-match[i-1]);
  154. if(diff > max_gap || diff<0) return false;
  155. }
  156. return true;
  157. }
  158. public final InstructionSelect getInstruction() {
  159. return instruction;
  160. }
  161. }