author | joehw |
Wed, 26 Jun 2019 05:49:59 +0000 | |
changeset 55496 | 8e0ae3830fca |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
6 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
5 |
/* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
7 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
10 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
11 |
* the License. You may obtain a copy of the License at |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
12 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
14 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
15 |
* Unless required by applicable law or agreed to in writing, software |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
18 |
* See the License for the specific language governing permissions and |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
19 |
* limitations under the License. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
20 |
*/ |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
21 |
|
6 | 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 |
* |
|
55496 | 29 |
* @version $Id$ |
6 | 30 |
*/ |
31 |
public final class SWITCH implements CompoundInstruction { |
|
32 |
||
46174 | 33 |
private int[] match; |
34 |
private InstructionHandle[] targets; |
|
35 |
private Select instruction; |
|
36 |
private int match_length; |
|
6 | 37 |
|
38 |
||
46174 | 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 |
} |
|
6 | 68 |
} |
69 |
||
70 |
||
46174 | 71 |
public SWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle target) { |
72 |
this(match, targets, target, 1); |
|
6 | 73 |
} |
74 |
||
75 |
||
46174 | 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 |
} |
|
6 | 100 |
|
101 |
||
46174 | 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 |
} |
|
6 | 136 |
|
137 |
||
46174 | 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 |
} |
|
6 | 149 |
|
46174 | 150 |
|
151 |
@Override |
|
152 |
public final InstructionList getInstructionList() { |
|
153 |
return new InstructionList(instruction); |
|
154 |
} |
|
6 | 155 |
|
46174 | 156 |
|
157 |
public final Instruction getInstruction() { |
|
158 |
return instruction; |
|
159 |
} |
|
6 | 160 |
} |