1 package com.sun.org.apache.bcel.internal.generic; 2 3 56 57 65 public final class SWITCH implements CompoundInstruction { 66 private int[] match; 67 private InstructionHandle[] targets; 68 private Select instruction; 69 private int match_length; 70 71 86 public SWITCH(int[] match, InstructionHandle[] targets, 87 InstructionHandle target, int max_gap) { 88 this.match = (int[])match.clone(); 89 this.targets = (InstructionHandle[])targets.clone(); 90 91 if((match_length = match.length) < 2) instruction = new TABLESWITCH(match, targets, target); 93 else { 94 sort(0, match_length - 1); 95 96 if(matchIsOrdered(max_gap)) { 97 fillup(max_gap, target); 98 99 instruction = new TABLESWITCH(this.match, this.targets, target); 100 } 101 else 102 instruction = new LOOKUPSWITCH(this.match, this.targets, target); 103 } 104 } 105 106 public SWITCH(int[] match, InstructionHandle[] targets, 107 InstructionHandle target) { 108 this(match, targets, target, 1); 109 } 110 111 private final void fillup(int max_gap, InstructionHandle target) { 112 int max_size = match_length + match_length * max_gap; 113 int[] m_vec = new int[max_size]; 114 InstructionHandle[] t_vec = new InstructionHandle[max_size]; 115 int count = 1; 116 117 m_vec[0] = match[0]; 118 t_vec[0] = targets[0]; 119 120 for(int i=1; i < match_length; i++) { 121 int prev = match[i-1]; 122 int gap = match[i] - prev; 123 124 for(int j=1; j < gap; j++) { 125 m_vec[count] = prev + j; 126 t_vec[count] = target; 127 count++; 128 } 129 130 m_vec[count] = match[i]; 131 t_vec[count] = targets[i]; 132 count++; 133 } 134 135 match = new int[count]; 136 targets = new InstructionHandle[count]; 137 138 System.arraycopy(m_vec, 0, match, 0, count); 139 System.arraycopy(t_vec, 0, targets, 0, count); 140 } 141 142 145 private final void sort(int l, int r) { 146 int i = l, j = r; 147 int h, m = match[(l + r) / 2]; 148 InstructionHandle h2; 149 150 do { 151 while(match[i] < m) i++; 152 while(m < match[j]) j--; 153 154 if(i <= j) { 155 h=match[i]; match[i]=match[j]; match[j]=h; h2=targets[i]; targets[i]=targets[j]; targets[j]=h2; i++; j--; 158 } 159 } while(i <= j); 160 161 if(l < j) sort(l, j); 162 if(i < r) sort(i, r); 163 } 164 165 168 private final boolean matchIsOrdered(int max_gap) { 169 for(int i=1; i < match_length; i++) 170 if(match[i] - match[i-1] > max_gap) 171 return false; 172 173 return true; 174 } 175 176 public final InstructionList getInstructionList() { 177 return new InstructionList(instruction); 178 } 179 180 public final Instruction getInstruction() { 181 return instruction; 182 } 183 } 184 | Popular Tags |