comparison graal/com.oracle.max.cri/src/com/oracle/max/cri/ci/CiUtil.java @ 4560:e43d36482d12

Merge
author Gilles Duboscq <duboscq@ssw.jku.at>
date Fri, 10 Feb 2012 17:09:39 +0100
parents 3706975946e4 a3cdfa2be94e
children 2f2c6347fce4
comparison
equal deleted inserted replaced
4559:723df37192d6 4560:e43d36482d12
65 * @param withEnclosingClass specifies if the returned name should be qualified with the name(s) of the enclosing 65 * @param withEnclosingClass specifies if the returned name should be qualified with the name(s) of the enclosing
66 * class/classes of {@code clazz} (if any). This option is ignored if {@code clazz} denotes an anonymous 66 * class/classes of {@code clazz} (if any). This option is ignored if {@code clazz} denotes an anonymous
67 * or local class. 67 * or local class.
68 * @return the simple name 68 * @return the simple name
69 */ 69 */
70 public static String getSimpleName(Class<?> clazz, boolean withEnclosingClass) { 70 public static String getSimpleName(Class< ? > clazz, boolean withEnclosingClass) {
71 final String simpleName = clazz.getSimpleName(); 71 final String simpleName = clazz.getSimpleName();
72 if (simpleName.length() != 0) { 72 if (simpleName.length() != 0) {
73 if (withEnclosingClass) { 73 if (withEnclosingClass) {
74 String prefix = ""; 74 String prefix = "";
75 Class<?> enclosingClass = clazz; 75 Class< ? > enclosingClass = clazz;
76 while ((enclosingClass = enclosingClass.getEnclosingClass()) != null) { 76 while ((enclosingClass = enclosingClass.getEnclosingClass()) != null) {
77 prefix = prefix + enclosingClass.getSimpleName() + "."; 77 prefix = prefix + enclosingClass.getSimpleName() + ".";
78 } 78 }
79 return prefix + simpleName; 79 return prefix + simpleName;
80 } 80 }
93 return name.substring(index + 1); 93 return name.substring(index + 1);
94 } 94 }
95 95
96 public static final int K = 1024; 96 public static final int K = 1024;
97 public static final int M = 1024 * 1024; 97 public static final int M = 1024 * 1024;
98
98 public static boolean isOdd(int n) { 99 public static boolean isOdd(int n) {
99 return (n & 1) == 1; 100 return (n & 1) == 1;
100 } 101 }
101 102
102 public static boolean isEven(int n) { 103 public static boolean isEven(int n) {
122 public static boolean isPowerOf2(long val) { 123 public static boolean isPowerOf2(long val) {
123 return val != 0 && (val & val - 1) == 0; 124 return val != 0 && (val & val - 1) == 0;
124 } 125 }
125 126
126 /** 127 /**
127 * Computes the log (base 2) of the specified integer, rounding down. 128 * Computes the log (base 2) of the specified integer, rounding down. (E.g {@code log2(8) = 3}, {@code log2(21) = 4}
128 * (E.g {@code log2(8) = 3}, {@code log2(21) = 4}) 129 * )
129 * 130 *
130 * @param val the value 131 * @param val the value
131 * @return the log base 2 of the value 132 * @return the log base 2 of the value
132 */ 133 */
133 public static int log2(int val) { 134 public static int log2(int val) {
134 assert val > 0 && isPowerOf2(val); 135 assert val > 0 && isPowerOf2(val);
135 return 31 - Integer.numberOfLeadingZeros(val); 136 return 31 - Integer.numberOfLeadingZeros(val);
136 } 137 }
137 138
138 /** 139 /**
139 * Computes the log (base 2) of the specified long, rounding down. 140 * Computes the log (base 2) of the specified long, rounding down. (E.g {@code log2(8) = 3}, {@code log2(21) = 4})
140 * (E.g {@code log2(8) = 3}, {@code log2(21) = 4})
141 * 141 *
142 * @param val the value 142 * @param val the value
143 * @return the log base 2 of the value 143 * @return the log base 2 of the value
144 */ 144 */
145 public static int log2(long val) { 145 public static int log2(long val) {
152 return (size + align - 1) & ~(align - 1); 152 return (size + align - 1) & ~(align - 1);
153 } 153 }
154 154
155 /** 155 /**
156 * Gets a word with the nth bit set. 156 * Gets a word with the nth bit set.
157 *
157 * @param n the nth bit to set 158 * @param n the nth bit to set
158 * @return an integer value with the nth bit set 159 * @return an integer value with the nth bit set
159 */ 160 */
160 public static int nthBit(int n) { 161 public static int nthBit(int n) {
161 return n >= Integer.SIZE ? 0 : 1 << n; 162 return n >= Integer.SIZE ? 0 : 1 << n;
162 } 163 }
163 164
164 /** 165 /**
165 * Gets a word with the right-most n bits set. 166 * Gets a word with the right-most n bits set.
167 *
166 * @param n the number of right most bits to set 168 * @param n the number of right most bits to set
167 * @return an integer value with the right-most n bits set 169 * @return an integer value with the right-most n bits set
168 */ 170 */
169 public static int rightNBits(int n) { 171 public static int rightNBits(int n) {
170 return nthBit(n) - 1; 172 return nthBit(n) - 1;
194 if (kind.isPrimitive() || kind == CiKind.Void) { 196 if (kind.isPrimitive() || kind == CiKind.Void) {
195 return kind.javaName; 197 return kind.javaName;
196 } 198 }
197 return internalNameToJava(riType.name(), qualified); 199 return internalNameToJava(riType.name(), qualified);
198 } 200 }
201
199 /** 202 /**
200 * Converts a given type to its Java programming language name. The following are examples of strings returned by 203 * Converts a given type to its Java programming language name. The following are examples of strings returned by
201 * this method: 204 * this method:
202 * 205 *
203 * <pre> 206 * <pre>
328 sb.append(ch); 331 sb.append(ch);
329 } 332 }
330 } 333 }
331 return sb.toString(); 334 return sb.toString();
332 } 335 }
336
333 /** 337 /**
334 * Gets a string for a given field formatted according to a given format specification. A format specification is 338 * Gets a string for a given field formatted according to a given format specification. A format specification is
335 * composed of characters that are to be copied verbatim to the result and specifiers that denote an attribute of 339 * composed of characters that are to be copied verbatim to the result and specifiers that denote an attribute of
336 * the field that is to be copied to the result. A specifier is a single character preceded by a '%' character. The 340 * the field that is to be copied to the result. A specifier is a single character preceded by a '%' character. The
337 * accepted specifiers and the field attributes they denote are described below: 341 * accepted specifiers and the field attributes they denote are described below:
348 * '%' | A '%' character | "%" 352 * '%' | A '%' character | "%"
349 * </pre> 353 * </pre>
350 * 354 *
351 * @param format a format specification 355 * @param format a format specification
352 * @param field the field to be formatted 356 * @param field the field to be formatted
353 * @param kinds if {@code true} then {@code field}'s type is printed in the 357 * @param kinds if {@code true} then {@code field}'s type is printed in the {@linkplain CiKind#jniName JNI} form of
354 * {@linkplain CiKind#jniName JNI} form of its {@linkplain CiKind kind} 358 * its {@linkplain CiKind kind}
355 * @return the result of formatting this field according to {@code format} 359 * @return the result of formatting this field according to {@code format}
356 * @throws IllegalFormatException if an illegal specifier is encountered in {@code format} 360 * @throws IllegalFormatException if an illegal specifier is encountered in {@code format}
357 */ 361 */
358 public static String format(String format, RiField field) throws IllegalFormatException { 362 public static String format(String format, RiField field) throws IllegalFormatException {
359 final StringBuilder sb = new StringBuilder(); 363 final StringBuilder sb = new StringBuilder();
422 public static String toInternalName(String className) { 426 public static String toInternalName(String className) {
423 return "L" + className.replace('.', '/') + ";"; 427 return "L" + className.replace('.', '/') + ";";
424 } 428 }
425 429
426 /** 430 /**
427 * Creates a set that uses reference-equality instead of {@link Object#equals(Object)} 431 * Creates a set that uses reference-equality instead of {@link Object#equals(Object)} when comparing values.
428 * when comparing values.
429 * 432 *
430 * @param <T> the type of elements in the set 433 * @param <T> the type of elements in the set
431 * @return a set based on reference-equality 434 * @return a set based on reference-equality
432 */ 435 */
433 public static <T> Set<T> newIdentityHashSet() { 436 public static <T> Set<T> newIdentityHashSet() {
434 return Collections.newSetFromMap(new IdentityHashMap<T, Boolean>()); 437 return Collections.newSetFromMap(new IdentityHashMap<T, Boolean>());
435 } 438 }
436 439
437 /** 440 /**
438 * Prepends the String {@code indentation} to every line in String {@code lines}, 441 * Prepends the String {@code indentation} to every line in String {@code lines}, including a possibly non-empty
439 * including a possibly non-empty line following the final newline. 442 * line following the final newline.
440 */ 443 */
441 public static String indent(String lines, String indentation) { 444 public static String indent(String lines, String indentation) {
442 if (lines.length() == 0) { 445 if (lines.length() == 0) {
443 return lines; 446 return lines;
444 } 447 }
535 } 538 }
536 return sb.toString(); 539 return sb.toString();
537 } 540 }
538 541
539 /** 542 /**
540 * Convenient shortcut for calling {@link #appendLocation(StringBuilder, RiMethod, int)} 543 * Convenient shortcut for calling {@link #appendLocation(StringBuilder, RiMethod, int)} without having to supply a
541 * without having to supply a a {@link StringBuilder} instance and convert the result 544 * a {@link StringBuilder} instance and convert the result to a string.
542 * to a string.
543 */ 545 */
544 public static String toLocation(RiResolvedMethod method, int bci) { 546 public static String toLocation(RiResolvedMethod method, int bci) {
545 return appendLocation(new StringBuilder(), method, bci).toString(); 547 return appendLocation(new StringBuilder(), method, bci).toString();
546 } 548 }
547 549
548 550 /**
549 /** 551 * Appends a string representation of a location specified by a given method and bci to a given
550 * Appends a string representation of a location specified by a given method and bci to 552 * {@link StringBuilder}. If a stack trace element with a non-null file name and non-negative line number is
551 * a given {@link StringBuilder}. If a stack trace element with a non-null file name 553 * {@linkplain RiMethod#toStackTraceElement(int) available} for the given method, then the string returned is the
552 * and non-negative line number is {@linkplain RiMethod#toStackTraceElement(int) available} 554 * {@link StackTraceElement#toString()} value of the stack trace element, suffixed by the bci location. For example:
553 * for the given method, then the string returned is the {@link StackTraceElement#toString()} 555 *
554 * value of the stack trace element, suffixed by the bci location. For example:
555 * <pre> 556 * <pre>
556 * java.lang.String.valueOf(String.java:2930) [bci: 12] 557 * java.lang.String.valueOf(String.java:2930) [bci: 12]
557 * </pre> 558 * </pre>
558 * Otherwise, the string returned is the value of {@code CiUtil.format("%H.%n(%p)"}, suffixed 559 *
559 * by the bci location. For example: 560 * Otherwise, the string returned is the value of {@code CiUtil.format("%H.%n(%p)"}, suffixed by the bci location.
561 * For example:
562 *
560 * <pre> 563 * <pre>
561 * java.lang.String.valueOf(int) [bci: 12] 564 * java.lang.String.valueOf(int) [bci: 12]
562 * </pre> 565 * </pre>
563 * 566 *
564 * @param sb 567 * @param sb
631 634
632 /** 635 /**
633 * Formats a location present in a register or frame reference map. 636 * Formats a location present in a register or frame reference map.
634 */ 637 */
635 public static class RefMapFormatter { 638 public static class RefMapFormatter {
639
636 /** 640 /**
637 * The size of a stack slot. 641 * The size of a stack slot.
638 */ 642 */
639 public final int slotSize; 643 public final int slotSize;
640 644
644 public final CiRegister fp; 648 public final CiRegister fp;
645 649
646 public final CiArchitecture arch; 650 public final CiArchitecture arch;
647 651
648 /** 652 /**
649 * The offset (in bytes) from the slot pointed to by {@link #fp} to the slot 653 * The offset (in bytes) from the slot pointed to by {@link #fp} to the slot corresponding to bit 0 in the frame
650 * corresponding to bit 0 in the frame reference map. 654 * reference map.
651 */ 655 */
652 public final int refMapToFPOffset; 656 public final int refMapToFPOffset;
653 657
654 public RefMapFormatter(CiArchitecture arch, int slotSize, CiRegister fp, int refMapToFPOffset) { 658 public RefMapFormatter(CiArchitecture arch, int slotSize, CiRegister fp, int refMapToFPOffset) {
655 this.arch = arch; 659 this.arch = arch;
730 result[i + j] = signature.argumentKindAt(j, true); 734 result[i + j] = signature.argumentKindAt(j, true);
731 } 735 }
732 return result; 736 return result;
733 } 737 }
734 738
735 public static Class<?>[] signatureToTypes(RiSignature signature, RiResolvedType accessingClass) { 739 public static Class< ? >[] signatureToTypes(RiSignature signature, RiResolvedType accessingClass) {
736 int count = signature.argumentCount(false); 740 int count = signature.argumentCount(false);
737 Class<?>[] result = new Class<?>[count]; 741 Class< ? >[] result = new Class< ? >[count];
738 for (int i = 0; i < result.length; ++i) { 742 for (int i = 0; i < result.length; ++i) {
739 result[i] = signature.argumentTypeAt(i, accessingClass).resolve(accessingClass).toJava(); 743 result[i] = signature.argumentTypeAt(i, accessingClass).resolve(accessingClass).toJava();
740 } 744 }
741 return result; 745 return result;
742 } 746 }