Mercurial > hg > graal-jvmci-8
annotate jvmci/jdk.vm.ci.code/src/jdk/vm/ci/code/CodeUtil.java @ 24125:0c5404418ec2
Backout getMarkName fix
author | Tom Rodriguez <tom.rodriguez@oracle.com> |
---|---|
date | Tue, 11 Apr 2017 08:59:43 -0700 |
parents | b4838d622f3f |
children |
rev | line source |
---|---|
22054 | 1 /* |
23396
9ed5b586018b
Replace LIRKind with abstract base class (JDK-8156942).
Roland Schatz <roland.schatz@oracle.com>
parents:
22783
diff
changeset
|
2 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. |
22054 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
22672
1bbd4a7c274b
Rename jdk.internal.jvmci to jdk.vm.ci
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
22662
diff
changeset
|
23 package jdk.vm.ci.code; |
22054 | 24 |
22569
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22054
diff
changeset
|
25 import java.util.ArrayList; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22054
diff
changeset
|
26 import java.util.Arrays; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22054
diff
changeset
|
27 import java.util.Collections; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22054
diff
changeset
|
28 import java.util.Map; |
22054 | 29 |
22672
1bbd4a7c274b
Rename jdk.internal.jvmci to jdk.vm.ci
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
22662
diff
changeset
|
30 import jdk.vm.ci.meta.JavaType; |
1bbd4a7c274b
Rename jdk.internal.jvmci to jdk.vm.ci
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
22662
diff
changeset
|
31 import jdk.vm.ci.meta.MetaUtil; |
1bbd4a7c274b
Rename jdk.internal.jvmci to jdk.vm.ci
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
22662
diff
changeset
|
32 import jdk.vm.ci.meta.ResolvedJavaMethod; |
1bbd4a7c274b
Rename jdk.internal.jvmci to jdk.vm.ci
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
22662
diff
changeset
|
33 import jdk.vm.ci.meta.Signature; |
22054 | 34 |
35 /** | |
22672
1bbd4a7c274b
Rename jdk.internal.jvmci to jdk.vm.ci
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
22662
diff
changeset
|
36 * Miscellaneous collection of utility methods used by {@code jdk.vm.ci.code} and its clients. |
22054 | 37 */ |
38 public class CodeUtil { | |
39 | |
40 public static final String NEW_LINE = String.format("%n"); | |
41 | |
42 public static final int K = 1024; | |
43 public static final int M = 1024 * 1024; | |
44 | |
45 public static boolean isOdd(int n) { | |
46 return (n & 1) == 1; | |
47 } | |
48 | |
49 public static boolean isEven(int n) { | |
50 return (n & 1) == 0; | |
51 } | |
52 | |
53 /** | |
54 * Checks whether the specified integer is a power of two. | |
55 * | |
56 * @param val the value to check | |
57 * @return {@code true} if the value is a power of two; {@code false} otherwise | |
58 */ | |
59 public static boolean isPowerOf2(int val) { | |
60 return val > 0 && (val & val - 1) == 0; | |
61 } | |
62 | |
63 /** | |
64 * Checks whether the specified long is a power of two. | |
65 * | |
66 * @param val the value to check | |
67 * @return {@code true} if the value is a power of two; {@code false} otherwise | |
68 */ | |
69 public static boolean isPowerOf2(long val) { | |
70 return val > 0 && (val & val - 1) == 0; | |
71 } | |
72 | |
73 /** | |
74 * Computes the log (base 2) of the specified integer, rounding down. (E.g {@code log2(8) = 3}, | |
75 * {@code log2(21) = 4} ) | |
76 * | |
77 * @param val the value | |
78 * @return the log base 2 of the value | |
79 */ | |
80 public static int log2(int val) { | |
81 assert val > 0; | |
82 return (Integer.SIZE - 1) - Integer.numberOfLeadingZeros(val); | |
83 } | |
84 | |
85 /** | |
86 * Computes the log (base 2) of the specified long, rounding down. (E.g {@code log2(8) = 3}, | |
87 * {@code log2(21) = 4}) | |
88 * | |
89 * @param val the value | |
90 * @return the log base 2 of the value | |
91 */ | |
92 public static int log2(long val) { | |
93 assert val > 0; | |
94 return (Long.SIZE - 1) - Long.numberOfLeadingZeros(val); | |
95 } | |
96 | |
97 /** | |
98 * Narrow an integer value to a given bit width, and return the result as a signed long. | |
99 * | |
100 * @param value the value | |
101 * @param resultBits the result bit width | |
102 * @return {@code value} interpreted as {@code resultBits} bit number, encoded as signed long | |
103 */ | |
104 public static long narrow(long value, int resultBits) { | |
105 long ret = value & mask(resultBits); | |
106 return signExtend(ret, resultBits); | |
107 } | |
108 | |
109 /** | |
110 * Sign extend an integer. | |
111 * | |
112 * @param value the input value | |
113 * @param inputBits the bit width of the input value | |
114 * @return a signed long with the same value as the signed {@code inputBits}-bit number | |
115 * {@code value} | |
116 */ | |
117 public static long signExtend(long value, int inputBits) { | |
118 if (inputBits < 64) { | |
119 if ((value >>> (inputBits - 1) & 1) == 1) { | |
120 return value | (-1L << inputBits); | |
121 } else { | |
122 return value & ~(-1L << inputBits); | |
123 } | |
124 } else { | |
125 return value; | |
126 } | |
127 } | |
128 | |
129 /** | |
130 * Zero extend an integer. | |
131 * | |
132 * @param value the input value | |
133 * @param inputBits the bit width of the input value | |
134 * @return an unsigned long with the same value as the unsigned {@code inputBits}-bit number | |
135 * {@code value} | |
136 */ | |
137 public static long zeroExtend(long value, int inputBits) { | |
138 if (inputBits < 64) { | |
139 return value & ~(-1L << inputBits); | |
140 } else { | |
141 return value; | |
142 } | |
143 } | |
144 | |
145 /** | |
146 * Convert an integer to long. | |
147 * | |
148 * @param value the input value | |
149 * @param inputBits the bit width of the input value | |
150 * @param unsigned whether the values should be interpreted as signed or unsigned | |
151 * @return a long with the same value as the {@code inputBits}-bit number {@code value} | |
152 */ | |
153 public static long convert(long value, int inputBits, boolean unsigned) { | |
154 if (unsigned) { | |
155 return zeroExtend(value, inputBits); | |
156 } else { | |
157 return signExtend(value, inputBits); | |
158 } | |
159 } | |
160 | |
161 /** | |
162 * Get a bitmask with the low {@code bits} bit set and the high {@code 64 - bits} bit clear. | |
163 */ | |
164 public static long mask(int bits) { | |
165 assert 0 <= bits && bits <= 64; | |
166 if (bits == 64) { | |
167 return 0xffffffffffffffffL; | |
168 } else { | |
169 return (1L << bits) - 1; | |
170 } | |
171 } | |
172 | |
173 /** | |
174 * Get the minimum value representable in a {@code bits} bit signed integer. | |
175 */ | |
176 public static long minValue(int bits) { | |
177 assert 0 < bits && bits <= 64; | |
178 return -1L << (bits - 1); | |
179 } | |
180 | |
181 /** | |
182 * Get the maximum value representable in a {@code bits} bit signed integer. | |
183 */ | |
184 public static long maxValue(int bits) { | |
185 assert 0 < bits && bits <= 64; | |
186 return mask(bits - 1); | |
187 } | |
188 | |
189 /** | |
190 * Formats the values in a frame as a tabulated string. | |
191 * | |
192 * @param frame | |
193 * @return the values in {@code frame} as a tabulated string | |
194 */ | |
195 public static String tabulateValues(BytecodeFrame frame) { | |
196 int cols = Math.max(frame.numLocals, Math.max(frame.numStack, frame.numLocks)); | |
23733
b4838d622f3f
Fix exception when printing BytecodeFrame in test
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
23396
diff
changeset
|
197 if (frame.values == null || frame.values.length == 0) { |
b4838d622f3f
Fix exception when printing BytecodeFrame in test
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
23396
diff
changeset
|
198 assert cols == 0; |
b4838d622f3f
Fix exception when printing BytecodeFrame in test
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
23396
diff
changeset
|
199 return null; |
b4838d622f3f
Fix exception when printing BytecodeFrame in test
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
23396
diff
changeset
|
200 } |
b4838d622f3f
Fix exception when printing BytecodeFrame in test
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
23396
diff
changeset
|
201 if (cols == 0) { |
b4838d622f3f
Fix exception when printing BytecodeFrame in test
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
23396
diff
changeset
|
202 return null; |
b4838d622f3f
Fix exception when printing BytecodeFrame in test
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
23396
diff
changeset
|
203 } |
22054 | 204 ArrayList<Object> cells = new ArrayList<>(); |
205 cells.add(""); | |
206 for (int i = 0; i < cols; i++) { | |
207 cells.add(i); | |
208 } | |
209 cols++; | |
210 if (frame.numLocals != 0) { | |
211 cells.add("locals:"); | |
212 cells.addAll(Arrays.asList(frame.values).subList(0, frame.numLocals)); | |
213 cells.addAll(Collections.nCopies(cols - frame.numLocals - 1, "")); | |
214 } | |
215 if (frame.numStack != 0) { | |
216 cells.add("stack:"); | |
217 cells.addAll(Arrays.asList(frame.values).subList(frame.numLocals, frame.numLocals + frame.numStack)); | |
218 cells.addAll(Collections.nCopies(cols - frame.numStack - 1, "")); | |
219 } | |
220 if (frame.numLocks != 0) { | |
221 cells.add("locks:"); | |
222 cells.addAll(Arrays.asList(frame.values).subList(frame.numLocals + frame.numStack, frame.values.length)); | |
223 cells.addAll(Collections.nCopies(cols - frame.numLocks - 1, "")); | |
224 } | |
225 Object[] cellArray = cells.toArray(); | |
226 for (int i = 0; i < cellArray.length; i++) { | |
227 if ((i % cols) != 0) { | |
228 cellArray[i] = "|" + cellArray[i]; | |
229 } | |
230 } | |
231 return CodeUtil.tabulate(cellArray, cols, 1, 1); | |
232 } | |
233 | |
234 /** | |
235 * Formats a given table as a string. The value of each cell is produced by | |
236 * {@link String#valueOf(Object)}. | |
237 * | |
238 * @param cells the cells of the table in row-major order | |
239 * @param cols the number of columns per row | |
240 * @param lpad the number of space padding inserted before each formatted cell value | |
241 * @param rpad the number of space padding inserted after each formatted cell value | |
242 * @return a string with one line per row and each column left-aligned | |
243 */ | |
244 public static String tabulate(Object[] cells, int cols, int lpad, int rpad) { | |
245 int rows = (cells.length + (cols - 1)) / cols; | |
246 int[] colWidths = new int[cols]; | |
247 for (int col = 0; col < cols; col++) { | |
248 for (int row = 0; row < rows; row++) { | |
249 int index = col + (row * cols); | |
250 if (index < cells.length) { | |
251 Object cell = cells[index]; | |
252 colWidths[col] = Math.max(colWidths[col], String.valueOf(cell).length()); | |
253 } | |
254 } | |
255 } | |
256 StringBuilder sb = new StringBuilder(); | |
257 String nl = NEW_LINE; | |
258 for (int row = 0; row < rows; row++) { | |
259 for (int col = 0; col < cols; col++) { | |
260 int index = col + (row * cols); | |
261 if (index < cells.length) { | |
262 for (int i = 0; i < lpad; i++) { | |
263 sb.append(' '); | |
264 } | |
265 Object cell = cells[index]; | |
266 String s = String.valueOf(cell); | |
267 int w = s.length(); | |
268 sb.append(s); | |
269 while (w < colWidths[col]) { | |
270 sb.append(' '); | |
271 w++; | |
272 } | |
273 for (int i = 0; i < rpad; i++) { | |
274 sb.append(' '); | |
275 } | |
276 } | |
277 } | |
278 sb.append(nl); | |
279 } | |
280 return sb.toString(); | |
281 } | |
282 | |
283 /** | |
284 * Appends a formatted code position to a {@link StringBuilder}. | |
285 * | |
286 * @param sb the {@link StringBuilder} to append to | |
287 * @param pos the code position to format and append to {@code sb} | |
288 * @return the value of {@code sb} | |
289 */ | |
290 public static StringBuilder append(StringBuilder sb, BytecodePosition pos) { | |
291 MetaUtil.appendLocation(sb.append("at "), pos.getMethod(), pos.getBCI()); | |
292 if (pos.getCaller() != null) { | |
293 sb.append(NEW_LINE); | |
294 append(sb, pos.getCaller()); | |
295 } | |
296 return sb; | |
297 } | |
298 | |
299 /** | |
300 * Appends a formatted frame to a {@link StringBuilder}. | |
301 * | |
302 * @param sb the {@link StringBuilder} to append to | |
303 * @param frame the frame to format and append to {@code sb} | |
304 * @return the value of {@code sb} | |
305 */ | |
306 public static StringBuilder append(StringBuilder sb, BytecodeFrame frame) { | |
307 MetaUtil.appendLocation(sb.append("at "), frame.getMethod(), frame.getBCI()); | |
308 assert sb.charAt(sb.length() - 1) == ']'; | |
309 sb.deleteCharAt(sb.length() - 1); | |
310 sb.append(", duringCall: ").append(frame.duringCall).append(", rethrow: ").append(frame.rethrowException).append(']'); | |
23733
b4838d622f3f
Fix exception when printing BytecodeFrame in test
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
23396
diff
changeset
|
311 String table = tabulateValues(frame); |
b4838d622f3f
Fix exception when printing BytecodeFrame in test
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
23396
diff
changeset
|
312 if (table != null) { |
22054 | 313 sb.append(NEW_LINE); |
314 String[] rows = table.split(NEW_LINE); | |
315 for (int i = 0; i < rows.length; i++) { | |
316 String row = rows[i]; | |
317 if (!row.trim().isEmpty()) { | |
318 sb.append(" ").append(row); | |
319 if (i != rows.length - 1) { | |
320 sb.append(NEW_LINE); | |
321 } | |
322 } | |
323 } | |
324 } | |
325 if (frame.caller() != null) { | |
326 sb.append(NEW_LINE); | |
327 append(sb, frame.caller()); | |
328 } else if (frame.getCaller() != null) { | |
329 sb.append(NEW_LINE); | |
330 append(sb, frame.getCaller()); | |
331 } | |
332 return sb; | |
333 } | |
334 | |
335 public interface RefMapFormatter { | |
336 | |
337 String formatStackSlot(int frameRefMapIndex); | |
338 } | |
339 | |
340 /** | |
22688
ef7d87db544a
Remove unused reference map index.
Roland Schatz <roland.schatz@oracle.com>
parents:
22672
diff
changeset
|
341 * Formats a location present in a reference map. |
22054 | 342 */ |
22688
ef7d87db544a
Remove unused reference map index.
Roland Schatz <roland.schatz@oracle.com>
parents:
22672
diff
changeset
|
343 public static class DefaultRefMapFormatter implements RefMapFormatter { |
22054 | 344 |
345 /** | |
346 * The size of a stack slot. | |
347 */ | |
348 public final int slotSize; | |
349 | |
350 /** | |
351 * The register used as the frame pointer. | |
352 */ | |
353 public final Register fp; | |
354 | |
355 /** | |
356 * The offset (in bytes) from the slot pointed to by {@link #fp} to the slot corresponding | |
357 * to bit 0 in the frame reference map. | |
358 */ | |
359 public final int refMapToFPOffset; | |
360 | |
22688
ef7d87db544a
Remove unused reference map index.
Roland Schatz <roland.schatz@oracle.com>
parents:
22672
diff
changeset
|
361 public DefaultRefMapFormatter(int slotSize, Register fp, int refMapToFPOffset) { |
22054 | 362 this.slotSize = slotSize; |
363 this.fp = fp; | |
364 this.refMapToFPOffset = refMapToFPOffset; | |
365 } | |
366 | |
367 @Override | |
368 public String formatStackSlot(int frameRefMapIndex) { | |
369 int refMapOffset = frameRefMapIndex * slotSize; | |
370 int fpOffset = refMapOffset + refMapToFPOffset; | |
371 if (fpOffset >= 0) { | |
372 return fp + "+" + fpOffset; | |
373 } | |
374 return fp.name + fpOffset; | |
375 } | |
376 } | |
377 | |
378 public static class NumberedRefMapFormatter implements RefMapFormatter { | |
379 | |
380 public String formatStackSlot(int frameRefMapIndex) { | |
381 return "s" + frameRefMapIndex; | |
382 } | |
383 | |
384 public String formatRegister(int regRefMapIndex) { | |
385 return "r" + regRefMapIndex; | |
386 } | |
387 } | |
388 | |
389 /** | |
390 * Appends a formatted debug info to a {@link StringBuilder}. | |
391 * | |
392 * @param sb the {@link StringBuilder} to append to | |
393 * @param info the debug info to format and append to {@code sb} | |
394 * @return the value of {@code sb} | |
395 */ | |
396 public static StringBuilder append(StringBuilder sb, DebugInfo info, RefMapFormatter formatterArg) { | |
397 RefMapFormatter formatter = formatterArg; | |
398 if (formatter == null) { | |
399 formatter = new NumberedRefMapFormatter(); | |
400 } | |
401 String nl = NEW_LINE; | |
402 ReferenceMap refMap = info.getReferenceMap(); | |
403 if (refMap != null) { | |
404 sb.append(refMap.toString()); | |
405 } | |
406 RegisterSaveLayout calleeSaveInfo = info.getCalleeSaveInfo(); | |
407 if (calleeSaveInfo != null) { | |
408 sb.append("callee-save-info:").append(nl); | |
409 Map<Integer, Register> map = calleeSaveInfo.slotsToRegisters(true); | |
410 for (Map.Entry<Integer, Register> e : map.entrySet()) { | |
411 sb.append(" ").append(e.getValue()).append(" -> ").append(formatter.formatStackSlot(e.getKey())).append(nl); | |
412 } | |
413 } | |
414 BytecodeFrame frame = info.frame(); | |
415 if (frame != null) { | |
416 append(sb, frame); | |
417 } else if (info.getBytecodePosition() != null) { | |
418 append(sb, info.getBytecodePosition()); | |
419 } | |
420 return sb; | |
421 } | |
422 | |
423 /** | |
424 * Create a calling convention from a {@link ResolvedJavaMethod}. | |
425 */ | |
23396
9ed5b586018b
Replace LIRKind with abstract base class (JDK-8156942).
Roland Schatz <roland.schatz@oracle.com>
parents:
22783
diff
changeset
|
426 public static CallingConvention getCallingConvention(CodeCacheProvider codeCache, CallingConvention.Type type, ResolvedJavaMethod method, ValueKindFactory<?> valueKindFactory) { |
22054 | 427 Signature sig = method.getSignature(); |
428 JavaType retType = sig.getReturnType(null); | |
429 int sigCount = sig.getParameterCount(false); | |
430 JavaType[] argTypes; | |
431 int argIndex = 0; | |
432 if (!method.isStatic()) { | |
433 argTypes = new JavaType[sigCount + 1]; | |
434 argTypes[argIndex++] = method.getDeclaringClass(); | |
435 } else { | |
436 argTypes = new JavaType[sigCount]; | |
437 } | |
438 for (int i = 0; i < sigCount; i++) { | |
439 argTypes[argIndex++] = sig.getParameterType(i, null); | |
440 } | |
441 | |
442 RegisterConfig registerConfig = codeCache.getRegisterConfig(); | |
23396
9ed5b586018b
Replace LIRKind with abstract base class (JDK-8156942).
Roland Schatz <roland.schatz@oracle.com>
parents:
22783
diff
changeset
|
443 return registerConfig.getCallingConvention(type, retType, argTypes, valueKindFactory); |
22054 | 444 } |
445 } |