| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.mozilla.javascript.tools.idswitch; |
| 7 |
|
| 8 |
import org.mozilla.javascript.EvaluatorException; |
| 9 |
import org.mozilla.javascript.tools.ToolErrorReporter; |
| 10 |
|
| 11 |
public class SwitchGenerator { |
| 12 |
|
| 13 |
String v_switch_label = "L0"; |
| 14 |
String v_label = "L"; |
| 15 |
String v_s = "s"; |
| 16 |
String v_c = "c"; |
| 17 |
String v_guess = "X"; |
| 18 |
String v_id = "id"; |
| 19 |
String v_length_suffix = "_length"; |
| 20 |
|
| 21 |
int use_if_threshold = 3; |
| 22 |
int char_tail_test_threshold = 2; |
| 23 |
|
| 24 |
private IdValuePair[] pairs; |
| 25 |
private String default_value; |
| 26 |
private int[] columns; |
| 27 |
private boolean c_was_defined; |
| 28 |
|
| 29 |
private CodePrinter P; |
| 30 |
private ToolErrorReporter R; |
| 31 |
private String source_file; |
| 32 |
|
| 33 |
public CodePrinter getCodePrinter() { return P; } |
| 34 |
public void setCodePrinter(CodePrinter value) { P = value; } |
| 35 |
|
| 36 |
public ToolErrorReporter getReporter() { return R; } |
| 37 |
public void setReporter(ToolErrorReporter value) { R = value; } |
| 38 |
|
| 39 |
public String getSourceFileName() { return source_file; } |
| 40 |
public void setSourceFileName(String value) { source_file = value; } |
| 41 |
|
| 42 |
public void generateSwitch(String[] pairs, String default_value) { |
| 43 |
int N = pairs.length / 2; |
| 44 |
IdValuePair[] id_pairs = new IdValuePair[N]; |
| 45 |
for (int i = 0; i != N; ++i) { |
| 46 |
id_pairs[i] = new IdValuePair(pairs[2 * i], pairs[2 * i + 1]); |
| 47 |
} |
| 48 |
generateSwitch(id_pairs, default_value); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
public void generateSwitch(IdValuePair[] pairs, String default_value) { |
| 53 |
int begin = 0; |
| 54 |
int end = pairs.length; |
| 55 |
if (begin == end) { return; } |
| 56 |
this.pairs = pairs; |
| 57 |
this.default_value = default_value; |
| 58 |
|
| 59 |
generate_body(begin, end, 2); |
| 60 |
} |
| 61 |
|
| 62 |
private void generate_body(int begin, int end, int indent_level) { |
| 63 |
P.indent(indent_level); |
| 64 |
P.p(v_switch_label); P.p(": { "); |
| 65 |
P.p(v_id); P.p(" = "); P.p(default_value); |
| 66 |
P.p("; String "); P.p(v_guess); P.p(" = null;"); |
| 67 |
|
| 68 |
c_was_defined = false; |
| 69 |
int c_def_begin = P.getOffset(); |
| 70 |
P.p(" int "); P.p(v_c); P.p(';'); |
| 71 |
int c_def_end = P.getOffset(); |
| 72 |
P.nl(); |
| 73 |
|
| 74 |
generate_length_switch(begin, end, indent_level + 1); |
| 75 |
|
| 76 |
if (!c_was_defined) { |
| 77 |
P.erase(c_def_begin, c_def_end); |
| 78 |
} |
| 79 |
|
| 80 |
P.indent(indent_level + 1); |
| 81 |
P.p("if ("); P.p(v_guess); P.p("!=null && "); |
| 82 |
P.p(v_guess); P.p("!="); P.p(v_s); |
| 83 |
P.p(" && !"); P.p(v_guess); P.p(".equals("); P.p(v_s); P.p(")) "); |
| 84 |
P.p(v_id); P.p(" = "); P.p(default_value); P.p(";"); P.nl(); |
| 85 |
|
| 86 |
|
| 87 |
P.indent(indent_level + 1); |
| 88 |
P.p("break "); P.p(v_switch_label); P.p(";"); P.nl(); |
| 89 |
|
| 90 |
P.line(indent_level, "}"); |
| 91 |
} |
| 92 |
|
| 93 |
private void generate_length_switch(int begin, int end, int indent_level) { |
| 94 |
|
| 95 |
sort_pairs(begin, end, -1); |
| 96 |
|
| 97 |
check_all_is_different(begin, end); |
| 98 |
|
| 99 |
int lengths_count = count_different_lengths(begin, end); |
| 100 |
|
| 101 |
columns = new int[pairs[end - 1].idLength]; |
| 102 |
|
| 103 |
boolean use_if; |
| 104 |
if (lengths_count <= use_if_threshold) { |
| 105 |
use_if = true; |
| 106 |
if (lengths_count != 1) { |
| 107 |
P.indent(indent_level); |
| 108 |
P.p("int "); P.p(v_s); P.p(v_length_suffix); |
| 109 |
P.p(" = "); P.p(v_s); P.p(".length();"); |
| 110 |
P.nl(); |
| 111 |
} |
| 112 |
} |
| 113 |
else { |
| 114 |
use_if = false; |
| 115 |
P.indent(indent_level); |
| 116 |
P.p(v_label); P.p(": switch ("); |
| 117 |
P.p(v_s); P.p(".length()) {"); |
| 118 |
P.nl(); |
| 119 |
} |
| 120 |
|
| 121 |
int same_length_begin = begin; |
| 122 |
int cur_l = pairs[begin].idLength, l = 0; |
| 123 |
for (int i = begin;;) { |
| 124 |
++i; |
| 125 |
if (i == end || (l = pairs[i].idLength) != cur_l) { |
| 126 |
int next_indent; |
| 127 |
if (use_if) { |
| 128 |
P.indent(indent_level); |
| 129 |
if (same_length_begin != begin) { P.p("else "); } |
| 130 |
P.p("if ("); |
| 131 |
if (lengths_count == 1) { |
| 132 |
P.p(v_s); P.p(".length()=="); |
| 133 |
} |
| 134 |
else { |
| 135 |
P.p(v_s); P.p(v_length_suffix); P.p("=="); |
| 136 |
} |
| 137 |
P.p(cur_l); |
| 138 |
P.p(") {"); |
| 139 |
next_indent = indent_level + 1; |
| 140 |
} |
| 141 |
else { |
| 142 |
P.indent(indent_level); |
| 143 |
P.p("case "); P.p(cur_l); P.p(":"); |
| 144 |
next_indent = indent_level + 1; |
| 145 |
} |
| 146 |
generate_letter_switch |
| 147 |
(same_length_begin, i, next_indent, !use_if, use_if); |
| 148 |
if (use_if) { |
| 149 |
P.p("}"); P.nl(); |
| 150 |
} |
| 151 |
else { |
| 152 |
P.p("break "); P.p(v_label); P.p(";"); P.nl(); |
| 153 |
} |
| 154 |
|
| 155 |
if (i == end) { break; } |
| 156 |
same_length_begin = i; |
| 157 |
cur_l = l; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (!use_if) { |
| 162 |
P.indent(indent_level); P.p("}"); P.nl(); |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
private void generate_letter_switch |
| 168 |
(int begin, int end, |
| 169 |
int indent_level, boolean label_was_defined, boolean inside_if) |
| 170 |
{ |
| 171 |
int L = pairs[begin].idLength; |
| 172 |
|
| 173 |
for (int i = 0; i != L; ++i) { |
| 174 |
columns[i] = i; |
| 175 |
} |
| 176 |
|
| 177 |
generate_letter_switch_r |
| 178 |
(begin, end, L, indent_level, label_was_defined, inside_if); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
private boolean generate_letter_switch_r |
| 183 |
(int begin, int end, int L, |
| 184 |
int indent_level, boolean label_was_defined, boolean inside_if) |
| 185 |
{ |
| 186 |
boolean next_is_unreachable = false; |
| 187 |
if (begin + 1 == end) { |
| 188 |
P.p(' '); |
| 189 |
IdValuePair pair = pairs[begin]; |
| 190 |
if (L > char_tail_test_threshold) { |
| 191 |
P.p(v_guess); P.p("="); P.qstring(pair.id); P.p(";"); |
| 192 |
P.p(v_id); P.p("="); P.p(pair.value); P.p(";"); |
| 193 |
} |
| 194 |
else { |
| 195 |
if (L == 0) { |
| 196 |
next_is_unreachable = true; |
| 197 |
P.p(v_id); P.p("="); P.p(pair.value); |
| 198 |
P.p("; break "); P.p(v_switch_label); P.p(";"); |
| 199 |
} |
| 200 |
else { |
| 201 |
P.p("if ("); |
| 202 |
int column = columns[0]; |
| 203 |
P.p(v_s); P.p(".charAt("); P.p(column); P.p(")=="); |
| 204 |
P.qchar(pair.id.charAt(column)); |
| 205 |
for (int i = 1; i != L; ++i) { |
| 206 |
P.p(" && "); |
| 207 |
column = columns[i]; |
| 208 |
P.p(v_s); P.p(".charAt("); P.p(column); P.p(")=="); |
| 209 |
P.qchar(pair.id.charAt(column)); |
| 210 |
} |
| 211 |
P.p(") {"); |
| 212 |
P.p(v_id); P.p("="); P.p(pair.value); |
| 213 |
P.p("; break "); P.p(v_switch_label); P.p(";}"); |
| 214 |
} |
| 215 |
} |
| 216 |
P.p(' '); |
| 217 |
return next_is_unreachable; |
| 218 |
} |
| 219 |
|
| 220 |
int max_column_index = find_max_different_column(begin, end, L); |
| 221 |
int max_column = columns[max_column_index]; |
| 222 |
int count = count_different_chars(begin, end, max_column); |
| 223 |
|
| 224 |
columns[max_column_index] = columns[L - 1]; |
| 225 |
|
| 226 |
if (inside_if) { P.nl(); P.indent(indent_level); } |
| 227 |
else { P.p(' '); } |
| 228 |
|
| 229 |
boolean use_if; |
| 230 |
if (count <= use_if_threshold) { |
| 231 |
use_if = true; |
| 232 |
c_was_defined = true; |
| 233 |
P.p(v_c); P.p("="); P.p(v_s); |
| 234 |
P.p(".charAt("); P.p(max_column); P.p(");"); |
| 235 |
} |
| 236 |
else { |
| 237 |
use_if = false; |
| 238 |
if (!label_was_defined) { |
| 239 |
label_was_defined = true; |
| 240 |
P.p(v_label); P.p(": "); |
| 241 |
} |
| 242 |
P.p("switch ("); P.p(v_s); |
| 243 |
P.p(".charAt("); P.p(max_column); P.p(")) {"); |
| 244 |
} |
| 245 |
|
| 246 |
int same_char_begin = begin; |
| 247 |
int cur_ch = pairs[begin].id.charAt(max_column), ch = 0; |
| 248 |
for (int i = begin;;) { |
| 249 |
++i; |
| 250 |
if (i == end || (ch = pairs[i].id.charAt(max_column)) != cur_ch) { |
| 251 |
int next_indent; |
| 252 |
if (use_if) { |
| 253 |
P.nl(); P.indent(indent_level); |
| 254 |
if (same_char_begin != begin) { P.p("else "); } |
| 255 |
P.p("if ("); P.p(v_c); P.p("=="); |
| 256 |
P.qchar(cur_ch); P.p(") {"); |
| 257 |
next_indent = indent_level + 1; |
| 258 |
} |
| 259 |
else { |
| 260 |
P.nl(); P.indent(indent_level); |
| 261 |
P.p("case "); P.qchar(cur_ch); P.p(":"); |
| 262 |
next_indent = indent_level + 1; |
| 263 |
} |
| 264 |
boolean after_unreachable = generate_letter_switch_r |
| 265 |
(same_char_begin, i, L - 1, |
| 266 |
next_indent, label_was_defined, use_if); |
| 267 |
if (use_if) { |
| 268 |
P.p("}"); |
| 269 |
} |
| 270 |
else { |
| 271 |
if (!after_unreachable) { |
| 272 |
P.p("break "); P.p(v_label); P.p(";"); |
| 273 |
} |
| 274 |
} |
| 275 |
if (i == end) { break; } |
| 276 |
same_char_begin = i; |
| 277 |
cur_ch = ch; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
if (use_if) { |
| 282 |
P.nl(); |
| 283 |
if (inside_if) { P.indent(indent_level - 1); } |
| 284 |
else { P.indent(indent_level); } |
| 285 |
} |
| 286 |
else { |
| 287 |
P.nl(); P.indent(indent_level); P.p("}"); |
| 288 |
if (inside_if) { P.nl(); P.indent(indent_level - 1);} |
| 289 |
else { P.p(' '); } |
| 290 |
} |
| 291 |
|
| 292 |
columns[max_column_index] = max_column; |
| 293 |
|
| 294 |
return next_is_unreachable; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
private int count_different_lengths(int begin, int end) { |
| 299 |
int lengths_count = 0; |
| 300 |
int cur_l = -1; |
| 301 |
for (; begin != end; ++begin) { |
| 302 |
int l = pairs[begin].idLength; |
| 303 |
if (cur_l != l) { |
| 304 |
++lengths_count; cur_l = l; |
| 305 |
} |
| 306 |
} |
| 307 |
return lengths_count; |
| 308 |
} |
| 309 |
|
| 310 |
private int find_max_different_column(int begin, int end, int L) { |
| 311 |
int max_count = 0; |
| 312 |
int max_index = 0; |
| 313 |
|
| 314 |
for (int i = 0; i != L; ++i) { |
| 315 |
int column = columns[i]; |
| 316 |
sort_pairs(begin, end, column); |
| 317 |
int count = count_different_chars(begin, end, column); |
| 318 |
if (count == end - begin) { return i; } |
| 319 |
if (max_count < count) { |
| 320 |
max_count = count; |
| 321 |
max_index = i; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
if (max_index != L - 1) { |
| 326 |
sort_pairs(begin, end, columns[max_index]); |
| 327 |
} |
| 328 |
|
| 329 |
return max_index; |
| 330 |
} |
| 331 |
|
| 332 |
private int count_different_chars(int begin, int end, int column) { |
| 333 |
int chars_count = 0; |
| 334 |
int cur_ch = -1; |
| 335 |
for (; begin != end; ++begin) { |
| 336 |
int ch = pairs[begin].id.charAt(column); |
| 337 |
if (ch != cur_ch) { |
| 338 |
++chars_count; cur_ch = ch; |
| 339 |
} |
| 340 |
} |
| 341 |
return chars_count; |
| 342 |
} |
| 343 |
|
| 344 |
private void check_all_is_different(int begin, int end) { |
| 345 |
if (begin != end) { |
| 346 |
IdValuePair prev = pairs[begin]; |
| 347 |
while (++begin != end) { |
| 348 |
IdValuePair current = pairs[begin]; |
| 349 |
if (prev.id.equals(current.id)) { |
| 350 |
throw on_same_pair_fail(prev, current); |
| 351 |
} |
| 352 |
prev = current; |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
private EvaluatorException on_same_pair_fail(IdValuePair a, IdValuePair b) { |
| 358 |
int line1 = a.getLineNumber(), line2 = b.getLineNumber(); |
| 359 |
if (line2 > line1) { int tmp = line1; line1 = line2; line2 = tmp; } |
| 360 |
String error_text = ToolErrorReporter.getMessage( |
| 361 |
"msg.idswitch.same_string", a.id, new Integer(line2)); |
| 362 |
return R.runtimeError(error_text, source_file, line1, null, 0); |
| 363 |
} |
| 364 |
|
| 365 |
private void sort_pairs(int begin, int end, int comparator) { |
| 366 |
heap4Sort(pairs, begin, end - begin, comparator); |
| 367 |
} |
| 368 |
|
| 369 |
private static boolean bigger |
| 370 |
(IdValuePair a, IdValuePair b, int comparator) |
| 371 |
{ |
| 372 |
if (comparator < 0) { |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
int diff = a.idLength - b.idLength; |
| 377 |
if (diff != 0) { return diff > 0; } |
| 378 |
return a.id.compareTo(b.id) > 0; |
| 379 |
} |
| 380 |
else { |
| 381 |
return a.id.charAt(comparator) > b.id.charAt(comparator); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
private static void heap4Sort |
| 386 |
(IdValuePair[] array, int offset, int size, int comparator) |
| 387 |
{ |
| 388 |
if (size <= 1) { return; } |
| 389 |
makeHeap4(array, offset, size, comparator); |
| 390 |
while (size > 1) { |
| 391 |
--size; |
| 392 |
IdValuePair v1 = array[offset + size]; |
| 393 |
IdValuePair v2 = array[offset + 0]; |
| 394 |
array[offset + size] = v2; |
| 395 |
array[offset + 0] = v1; |
| 396 |
heapify4(array, offset, size, 0, comparator); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
private static void makeHeap4 |
| 401 |
(IdValuePair[] array, int offset, int size, int comparator) |
| 402 |
{ |
| 403 |
for (int i = ((size + 2) >> 2); i != 0;) { |
| 404 |
--i; |
| 405 |
heapify4(array, offset, size, i, comparator); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
private static void heapify4 |
| 410 |
(IdValuePair[] array, int offset, int size, int i, int comparator) |
| 411 |
{ |
| 412 |
int new_i1, new_i2, new_i3; |
| 413 |
IdValuePair i_val = array[offset + i]; |
| 414 |
for (;;) { |
| 415 |
int base = (i << 2); |
| 416 |
new_i1 = base | 1; |
| 417 |
new_i2 = base | 2; |
| 418 |
new_i3 = base | 3; |
| 419 |
int new_i4 = base + 4; |
| 420 |
if (new_i4 >= size) { break; } |
| 421 |
IdValuePair val1 = array[offset + new_i1]; |
| 422 |
IdValuePair val2 = array[offset + new_i2]; |
| 423 |
IdValuePair val3 = array[offset + new_i3]; |
| 424 |
IdValuePair val4 = array[offset + new_i4]; |
| 425 |
if (bigger(val2, val1, comparator)) { |
| 426 |
val1 = val2; new_i1 = new_i2; |
| 427 |
} |
| 428 |
if (bigger(val4, val3, comparator)) { |
| 429 |
val3 = val4; new_i3 = new_i4; |
| 430 |
} |
| 431 |
if (bigger(val3, val1, comparator)) { |
| 432 |
val1 = val3; new_i1 = new_i3; |
| 433 |
} |
| 434 |
if (bigger(i_val, val1, comparator)) { return; } |
| 435 |
array[offset + i] = val1; |
| 436 |
array[offset + new_i1] = i_val; |
| 437 |
i = new_i1; |
| 438 |
} |
| 439 |
if (new_i1 < size) { |
| 440 |
IdValuePair val1 = array[offset + new_i1]; |
| 441 |
if (new_i2 != size) { |
| 442 |
IdValuePair val2 = array[offset + new_i2]; |
| 443 |
if (bigger(val2, val1, comparator)) { |
| 444 |
val1 = val2; new_i1 = new_i2; |
| 445 |
} |
| 446 |
if (new_i3 != size) { |
| 447 |
IdValuePair val3 = array[offset + new_i3]; |
| 448 |
if (bigger(val3, val1, comparator)) { |
| 449 |
val1 = val3; new_i1 = new_i3; |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
if (bigger(val1, i_val, comparator)) { |
| 454 |
array[offset + i] = val1; |
| 455 |
array[offset + new_i1] = i_val; |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
} |