comparison graal/com.oracle.max.cri/src/com/oracle/max/cri/ci/CiUtil.java @ 5507:dc71b06d09f8

Moving classes from cri.ri to api.meta.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Thu, 07 Jun 2012 18:24:06 +0200
parents 56860d3f9f39
children
comparison
equal deleted inserted replaced
5506:56860d3f9f39 5507:dc71b06d09f8
25 import static java.lang.reflect.Modifier.*; 25 import static java.lang.reflect.Modifier.*;
26 26
27 import java.lang.annotation.*; 27 import java.lang.annotation.*;
28 import java.util.*; 28 import java.util.*;
29 29
30 import com.oracle.max.cri.ri.*; 30 import com.oracle.graal.api.meta.*;
31 import com.oracle.max.cri.ri.RiTypeProfile.ProfiledType; 31 import com.oracle.graal.api.meta.RiTypeProfile.*;
32 32
33 /** 33 /**
34 * Miscellaneous collection of utility methods used in the {@code CRI} project. 34 * Miscellaneous collection of utility methods used in the {@code CRI} project.
35 */ 35 */
36 public class CiUtil { 36 public class CiUtil {
56 } 56 }
57 } 57 }
58 return null; 58 return null;
59 } 59 }
60 60
61 /**
62 * Extends the functionality of {@link Class#getSimpleName()} to include a non-empty string for anonymous and local
63 * classes.
64 *
65 * @param clazz the class for which the simple name is being requested
66 * @param withEnclosingClass specifies if the returned name should be qualified with the name(s) of the enclosing
67 * class/classes of {@code clazz} (if any). This option is ignored if {@code clazz} denotes an anonymous
68 * or local class.
69 * @return the simple name
70 */
71 public static String getSimpleName(Class< ? > clazz, boolean withEnclosingClass) {
72 final String simpleName = clazz.getSimpleName();
73 if (simpleName.length() != 0) {
74 if (withEnclosingClass) {
75 String prefix = "";
76 Class< ? > enclosingClass = clazz;
77 while ((enclosingClass = enclosingClass.getEnclosingClass()) != null) {
78 prefix = prefix + enclosingClass.getSimpleName() + ".";
79 }
80 return prefix + simpleName;
81 }
82 return simpleName;
83 }
84 // Must be an anonymous or local class
85 final String name = clazz.getName();
86 int index = name.indexOf('$');
87 if (index == -1) {
88 return name;
89 }
90 index = name.lastIndexOf('.', index);
91 if (index == -1) {
92 return name;
93 }
94 return name.substring(index + 1);
95 }
96
97 public static final int K = 1024; 61 public static final int K = 1024;
98 public static final int M = 1024 * 1024; 62 public static final int M = 1024 * 1024;
99 63
100 public static boolean isOdd(int n) { 64 public static boolean isOdd(int n) {
101 return (n & 1) == 1; 65 return (n & 1) == 1;
169 * @param n the number of right most bits to set 133 * @param n the number of right most bits to set
170 * @return an integer value with the right-most n bits set 134 * @return an integer value with the right-most n bits set
171 */ 135 */
172 public static int rightNBits(int n) { 136 public static int rightNBits(int n) {
173 return nthBit(n) - 1; 137 return nthBit(n) - 1;
174 }
175
176 /**
177 * Converts a given type to its Java programming language name. The following are examples of strings returned by
178 * this method:
179 *
180 * <pre>
181 * qualified == true:
182 * java.lang.Object
183 * int
184 * boolean[][]
185 * qualified == false:
186 * Object
187 * int
188 * boolean[][]
189 * </pre>
190 *
191 * @param riType the type to be converted to a Java name
192 * @param qualified specifies if the package prefix of the type should be included in the returned name
193 * @return the Java name corresponding to {@code riType}
194 */
195 public static String toJavaName(RiType riType, boolean qualified) {
196 RiKind kind = riType.kind(false);
197 if (kind.isPrimitive() || kind == RiKind.Void) {
198 return kind.javaName;
199 }
200 return internalNameToJava(riType.name(), qualified);
201 }
202
203 /**
204 * Converts a given type to its Java programming language name. The following are examples of strings returned by
205 * this method:
206 *
207 * <pre>
208 * java.lang.Object
209 * int
210 * boolean[][]
211 * </pre>
212 *
213 * @param riType the type to be converted to a Java name
214 * @return the Java name corresponding to {@code riType}
215 */
216 public static String toJavaName(RiType riType) {
217 return (riType == null) ? null : internalNameToJava(riType.name(), true);
218 }
219
220 public static String internalNameToJava(String name, boolean qualified) {
221 switch (name.charAt(0)) {
222 case 'L': {
223 String result = name.substring(1, name.length() - 1).replace('/', '.');
224 if (!qualified) {
225 final int lastDot = result.lastIndexOf('.');
226 if (lastDot != -1) {
227 result = result.substring(lastDot + 1);
228 }
229 }
230 return result;
231
232 }
233 case '[':
234 return internalNameToJava(name.substring(1), qualified) + "[]";
235 default:
236 if (name.length() != 1) {
237 throw new IllegalArgumentException("Illegal internal name: " + name);
238 }
239 return RiKind.fromPrimitiveOrVoidTypeChar(name.charAt(0)).javaName;
240 }
241 } 138 }
242 139
243 /** 140 /**
244 * Gets a string for a given method formatted according to a given format specification. A format specification is 141 * Gets a string for a given method formatted according to a given format specification. A format specification is
245 * composed of characters that are to be copied verbatim to the result and specifiers that denote an attribute of 142 * composed of characters that are to be copied verbatim to the result and specifiers that denote an attribute of
285 // fall through 182 // fall through
286 case 'r': { 183 case 'r': {
287 if (sig == null) { 184 if (sig == null) {
288 sig = method.signature(); 185 sig = method.signature();
289 } 186 }
290 sb.append(toJavaName(sig.returnType(null), qualified)); 187 sb.append(RiUtil.toJavaName(sig.returnType(null), qualified));
291 break; 188 break;
292 } 189 }
293 case 'H': 190 case 'H':
294 qualified = true; 191 qualified = true;
295 // fall through 192 // fall through
296 case 'h': { 193 case 'h': {
297 sb.append(toJavaName(method.holder(), qualified)); 194 sb.append(RiUtil.toJavaName(method.holder(), qualified));
298 break; 195 break;
299 } 196 }
300 case 'n': { 197 case 'n': {
301 sb.append(method.name()); 198 sb.append(method.name());
302 break; 199 break;
310 } 207 }
311 for (int i = 0; i < sig.argumentCount(false); i++) { 208 for (int i = 0; i < sig.argumentCount(false); i++) {
312 if (i != 0) { 209 if (i != 0) {
313 sb.append(", "); 210 sb.append(", ");
314 } 211 }
315 sb.append(toJavaName(sig.argumentTypeAt(i, null), qualified)); 212 sb.append(RiUtil.toJavaName(sig.argumentTypeAt(i, null), qualified));
316 } 213 }
317 break; 214 break;
318 } 215 }
319 case 'f': { 216 case 'f': {
320 sb.append(!(method instanceof RiResolvedMethod) ? "unresolved" : isStatic(((RiResolvedMethod) method).accessFlags()) ? "static" : "virtual"); 217 sb.append(!(method instanceof RiResolvedMethod) ? "unresolved" : isStatic(((RiResolvedMethod) method).accessFlags()) ? "static" : "virtual");
375 switch (specifier) { 272 switch (specifier) {
376 case 'T': 273 case 'T':
377 qualified = true; 274 qualified = true;
378 // fall through 275 // fall through
379 case 't': { 276 case 't': {
380 sb.append(toJavaName(type, qualified)); 277 sb.append(RiUtil.toJavaName(type, qualified));
381 break; 278 break;
382 } 279 }
383 case 'H': 280 case 'H':
384 qualified = true; 281 qualified = true;
385 // fall through 282 // fall through
386 case 'h': { 283 case 'h': {
387 sb.append(toJavaName(field.holder(), qualified)); 284 sb.append(RiUtil.toJavaName(field.holder(), qualified));
388 break; 285 break;
389 } 286 }
390 case 'n': { 287 case 'n': {
391 sb.append(field.name()); 288 sb.append(field.name());
392 break; 289 break;
678 */ 575 */
679 public static StringBuilder append(StringBuilder sb, CiDebugInfo info, RefMapFormatter formatter) { 576 public static StringBuilder append(StringBuilder sb, CiDebugInfo info, RefMapFormatter formatter) {
680 String nl = NEW_LINE; 577 String nl = NEW_LINE;
681 if (info.hasRegisterRefMap()) { 578 if (info.hasRegisterRefMap()) {
682 sb.append(" reg-ref-map:"); 579 sb.append(" reg-ref-map:");
683 CiBitMap bm = info.registerRefMap; 580 RiBitMap bm = info.registerRefMap;
684 if (formatter != null) { 581 if (formatter != null) {
685 for (int reg = bm.nextSetBit(0); reg >= 0; reg = bm.nextSetBit(reg + 1)) { 582 for (int reg = bm.nextSetBit(0); reg >= 0; reg = bm.nextSetBit(reg + 1)) {
686 sb.append(" " + formatter.formatRegister(reg)); 583 sb.append(" " + formatter.formatRegister(reg));
687 } 584 }
688 } 585 }
689 sb.append(' ').append(bm).append(nl); 586 sb.append(' ').append(bm).append(nl);
690 } 587 }
691 if (info.hasStackRefMap()) { 588 if (info.hasStackRefMap()) {
692 sb.append("frame-ref-map:"); 589 sb.append("frame-ref-map:");
693 CiBitMap bm = info.frameRefMap; 590 RiBitMap bm = info.frameRefMap;
694 if (formatter != null) { 591 if (formatter != null) {
695 for (int i = bm.nextSetBit(0); i >= 0; i = bm.nextSetBit(i + 1)) { 592 for (int i = bm.nextSetBit(0); i >= 0; i = bm.nextSetBit(i + 1)) {
696 sb.append(" " + formatter.formatStackSlot(i)); 593 sb.append(" " + formatter.formatStackSlot(i));
697 } 594 }
698 } 595 }
786 } 683 }
787 } 684 }
788 } 685 }
789 686
790 boolean firstDeoptReason = true; 687 boolean firstDeoptReason = true;
791 for (CiDeoptReason reason: CiDeoptReason.values()) { 688 for (RiDeoptReason reason: RiDeoptReason.values()) {
792 int count = info.getDeoptimizationCount(reason); 689 int count = info.getDeoptimizationCount(reason);
793 if (count > 0) { 690 if (count > 0) {
794 if (firstDeoptReason) { 691 if (firstDeoptReason) {
795 buf.append("deoptimization history").append(sep); 692 buf.append("deoptimization history").append(sep);
796 firstDeoptReason = false; 693 firstDeoptReason = false;