Mercurial > hg > truffle
comparison graal/com.oracle.max.cri/src/com/oracle/max/cri/ci/CiUtil.java @ 4547:a3cdfa2be94e
Allow to intrinsify an invoke with a deoptimize node. Make debug output more relaxed wrt to null in FrameMap.method
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Thu, 09 Feb 2012 20:05:59 +0100 |
parents | 14a00ee82980 |
children | e43d36482d12 |
comparison
equal
deleted
inserted
replaced
4546:df329f268a05 | 4547:a3cdfa2be94e |
---|---|
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 |
565 * @param method | 568 * @param method |
566 * @param bci | 569 * @param bci |
567 * @return | 570 * @return |
568 */ | 571 */ |
569 public static StringBuilder appendLocation(StringBuilder sb, RiResolvedMethod method, int bci) { | 572 public static StringBuilder appendLocation(StringBuilder sb, RiResolvedMethod method, int bci) { |
570 StackTraceElement ste = method.toStackTraceElement(bci); | 573 if (method != null) { |
571 if (ste.getFileName() != null && ste.getLineNumber() > 0) { | 574 StackTraceElement ste = method.toStackTraceElement(bci); |
572 sb.append(ste); | 575 if (ste.getFileName() != null && ste.getLineNumber() > 0) { |
573 } else { | 576 sb.append(ste); |
574 sb.append(CiUtil.format("%H.%n(%p)", method)); | 577 } else { |
578 sb.append(CiUtil.format("%H.%n(%p)", method)); | |
579 } | |
575 } | 580 } |
576 return sb.append(" [bci: ").append(bci).append(']'); | 581 return sb.append(" [bci: ").append(bci).append(']'); |
577 } | 582 } |
578 | 583 |
579 /** | 584 /** |
627 | 632 |
628 /** | 633 /** |
629 * Formats a location present in a register or frame reference map. | 634 * Formats a location present in a register or frame reference map. |
630 */ | 635 */ |
631 public static class RefMapFormatter { | 636 public static class RefMapFormatter { |
637 | |
632 /** | 638 /** |
633 * The size of a stack slot. | 639 * The size of a stack slot. |
634 */ | 640 */ |
635 public final int slotSize; | 641 public final int slotSize; |
636 | 642 |
640 public final CiRegister fp; | 646 public final CiRegister fp; |
641 | 647 |
642 public final CiArchitecture arch; | 648 public final CiArchitecture arch; |
643 | 649 |
644 /** | 650 /** |
645 * The offset (in bytes) from the slot pointed to by {@link #fp} to the slot | 651 * The offset (in bytes) from the slot pointed to by {@link #fp} to the slot corresponding to bit 0 in the frame |
646 * corresponding to bit 0 in the frame reference map. | 652 * reference map. |
647 */ | 653 */ |
648 public final int refMapToFPOffset; | 654 public final int refMapToFPOffset; |
649 | 655 |
650 public RefMapFormatter(CiArchitecture arch, int slotSize, CiRegister fp, int refMapToFPOffset) { | 656 public RefMapFormatter(CiArchitecture arch, int slotSize, CiRegister fp, int refMapToFPOffset) { |
651 this.arch = arch; | 657 this.arch = arch; |
726 result[i + j] = signature.argumentKindAt(j, true); | 732 result[i + j] = signature.argumentKindAt(j, true); |
727 } | 733 } |
728 return result; | 734 return result; |
729 } | 735 } |
730 | 736 |
731 public static Class<?>[] signatureToTypes(RiSignature signature, RiResolvedType accessingClass) { | 737 public static Class< ? >[] signatureToTypes(RiSignature signature, RiResolvedType accessingClass) { |
732 int count = signature.argumentCount(false); | 738 int count = signature.argumentCount(false); |
733 Class<?>[] result = new Class<?>[count]; | 739 Class< ? >[] result = new Class< ? >[count]; |
734 for (int i = 0; i < result.length; ++i) { | 740 for (int i = 0; i < result.length; ++i) { |
735 result[i] = signature.argumentTypeAt(i, accessingClass).resolve(accessingClass).toJava(); | 741 result[i] = signature.argumentTypeAt(i, accessingClass).resolve(accessingClass).toJava(); |
736 } | 742 } |
737 return result; | 743 return result; |
738 } | 744 } |