comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CompilerDirectives.java @ 19309:c386ace07981

Truffle: move unsafe access methods out of CompilerDirectives
author Andreas Woess <andreas.woess@oracle.com>
date Wed, 11 Feb 2015 18:19:40 +0100
parents f7375de5eaa0
children 9c4168877444
comparison
equal deleted inserted replaced
19308:5b2589732c45 19309:c386ace07981
1 /* 1 /*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
23 * questions. 23 * questions.
24 */ 24 */
25 package com.oracle.truffle.api; 25 package com.oracle.truffle.api;
26 26
27 import java.lang.annotation.*; 27 import java.lang.annotation.*;
28 import java.lang.reflect.*;
29 import java.util.concurrent.*; 28 import java.util.concurrent.*;
30
31 import sun.misc.*;
32 29
33 /** 30 /**
34 * Directives that influence the optimizations of the Truffle compiler. All of the operations have 31 * Directives that influence the optimizations of the Truffle compiler. All of the operations have
35 * no effect when executed in the Truffle interpreter. 32 * no effect when executed in the Truffle interpreter.
36 */ 33 */
39 public static final double LIKELY_PROBABILITY = 0.75; 36 public static final double LIKELY_PROBABILITY = 0.75;
40 public static final double UNLIKELY_PROBABILITY = 1.0 - LIKELY_PROBABILITY; 37 public static final double UNLIKELY_PROBABILITY = 1.0 - LIKELY_PROBABILITY;
41 38
42 public static final double SLOWPATH_PROBABILITY = 0.0001; 39 public static final double SLOWPATH_PROBABILITY = 0.0001;
43 public static final double FASTPATH_PROBABILITY = 1.0 - SLOWPATH_PROBABILITY; 40 public static final double FASTPATH_PROBABILITY = 1.0 - SLOWPATH_PROBABILITY;
44
45 private static final Unsafe UNSAFE = getUnsafe();
46
47 private static Unsafe getUnsafe() {
48 try {
49 return Unsafe.getUnsafe();
50 } catch (SecurityException e) {
51 }
52 try {
53 Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
54 theUnsafeInstance.setAccessible(true);
55 return (Unsafe) theUnsafeInstance.get(Unsafe.class);
56 } catch (Exception e) {
57 throw new RuntimeException("exception while trying to get Unsafe.theUnsafe via reflection:", e);
58 }
59 }
60 41
61 /** 42 /**
62 * Directive for the compiler to discontinue compilation at this code position and instead 43 * Directive for the compiler to discontinue compilation at this code position and instead
63 * insert a transfer to the interpreter. 44 * insert a transfer to the interpreter.
64 */ 45 */
201 @Target({ElementType.FIELD}) 182 @Target({ElementType.FIELD})
202 public @interface CompilationFinal { 183 public @interface CompilationFinal {
203 } 184 }
204 185
205 /** 186 /**
206 * Casts the given value to the value of the given type without any checks. The class must
207 * evaluate to a constant. The condition parameter gives a hint to the compiler under which
208 * circumstances this cast can be moved to an earlier location in the program.
209 *
210 * @param value the value that is known to have the specified type
211 * @param type the specified new type of the value
212 * @param condition the condition that makes this cast safe also at an earlier location of the
213 * program
214 * @return the value to be casted to the new type
215 */
216 @Deprecated
217 public static <T> T unsafeCast(Object value, Class<T> type, boolean condition) {
218 return unsafeCast(value, type, condition, false);
219 }
220
221 /**
222 * Casts the given value to the value of the given type without any checks. The class must
223 * evaluate to a constant. The condition parameter gives a hint to the compiler under which
224 * circumstances this cast can be moved to an earlier location in the program.
225 *
226 * @param value the value that is known to have the specified type
227 * @param type the specified new type of the value
228 * @param condition the condition that makes this cast safe also at an earlier location of the
229 * program
230 * @param nonNull whether value is known to never be null
231 * @return the value to be casted to the new type
232 */
233 @Deprecated
234 @SuppressWarnings("unchecked")
235 public static <T> T unsafeCast(Object value, Class<T> type, boolean condition, boolean nonNull) {
236 return (T) value;
237 }
238
239 /**
240 * Unsafe access to a boolean value within an object. The condition parameter gives a hint to
241 * the compiler under which circumstances this access can be moved to an earlier location in the
242 * program. The location identity gives a hint to the compiler for improved global value
243 * numbering.
244 *
245 * @param receiver the object that is accessed
246 * @param offset the offset at which to access the object in bytes
247 * @param condition the condition that makes this access safe also at an earlier location in the
248 * program
249 * @param locationIdentity the location identity token that can be used for improved global
250 * value numbering or null
251 * @return the accessed value
252 */
253 @Deprecated
254 public static boolean unsafeGetBoolean(Object receiver, long offset, boolean condition, Object locationIdentity) {
255 return UNSAFE.getBoolean(receiver, offset);
256 }
257
258 /**
259 * Unsafe access to a byte value within an object. The condition parameter gives a hint to the
260 * compiler under which circumstances this access can be moved to an earlier location in the
261 * program. The location identity gives a hint to the compiler for improved global value
262 * numbering.
263 *
264 * @param receiver the object that is accessed
265 * @param offset the offset at which to access the object in bytes
266 * @param condition the condition that makes this access safe also at an earlier location in the
267 * program
268 * @param locationIdentity the location identity token that can be used for improved global
269 * value numbering or null
270 * @return the accessed value
271 */
272 @Deprecated
273 public static byte unsafeGetByte(Object receiver, long offset, boolean condition, Object locationIdentity) {
274 return UNSAFE.getByte(receiver, offset);
275 }
276
277 /**
278 * Unsafe access to a short value within an object. The condition parameter gives a hint to the
279 * compiler under which circumstances this access can be moved to an earlier location in the
280 * program. The location identity gives a hint to the compiler for improved global value
281 * numbering.
282 *
283 * @param receiver the object that is accessed
284 * @param offset the offset at which to access the object in bytes
285 * @param condition the condition that makes this access safe also at an earlier location in the
286 * program
287 * @param locationIdentity the location identity token that can be used for improved global
288 * value numbering or null
289 * @return the accessed value
290 */
291 @Deprecated
292 public static short unsafeGetShort(Object receiver, long offset, boolean condition, Object locationIdentity) {
293 return UNSAFE.getShort(receiver, offset);
294 }
295
296 /**
297 * Unsafe access to an int value within an object. The condition parameter gives a hint to the
298 * compiler under which circumstances this access can be moved to an earlier location in the
299 * program. The location identity gives a hint to the compiler for improved global value
300 * numbering.
301 *
302 * @param receiver the object that is accessed
303 * @param offset the offset at which to access the object in bytes
304 * @param condition the condition that makes this access safe also at an earlier location in the
305 * program
306 * @param locationIdentity the location identity token that can be used for improved global
307 * value numbering or null
308 * @return the accessed value
309 */
310 @Deprecated
311 public static int unsafeGetInt(Object receiver, long offset, boolean condition, Object locationIdentity) {
312 return UNSAFE.getInt(receiver, offset);
313 }
314
315 /**
316 * Unsafe access to a long value within an object. The condition parameter gives a hint to the
317 * compiler under which circumstances this access can be moved to an earlier location in the
318 * program. The location identity gives a hint to the compiler for improved global value
319 * numbering.
320 *
321 * @param receiver the object that is accessed
322 * @param offset the offset at which to access the object in bytes
323 * @param condition the condition that makes this access safe also at an earlier location in the
324 * program
325 * @param locationIdentity the location identity token that can be used for improved global
326 * value numbering or null
327 * @return the accessed value
328 */
329 @Deprecated
330 public static long unsafeGetLong(Object receiver, long offset, boolean condition, Object locationIdentity) {
331 return UNSAFE.getLong(receiver, offset);
332 }
333
334 /**
335 * Unsafe access to a float value within an object. The condition parameter gives a hint to the
336 * compiler under which circumstances this access can be moved to an earlier location in the
337 * program. The location identity gives a hint to the compiler for improved global value
338 * numbering.
339 *
340 * @param receiver the object that is accessed
341 * @param offset the offset at which to access the object in bytes
342 * @param condition the condition that makes this access safe also at an earlier location in the
343 * program
344 * @param locationIdentity the location identity token that can be used for improved global
345 * value numbering or null
346 * @return the accessed value
347 */
348 @Deprecated
349 public static float unsafeGetFloat(Object receiver, long offset, boolean condition, Object locationIdentity) {
350 return UNSAFE.getFloat(receiver, offset);
351 }
352
353 /**
354 * Unsafe access to a double value within an object. The condition parameter gives a hint to the
355 * compiler under which circumstances this access can be moved to an earlier location in the
356 * program. The location identity gives a hint to the compiler for improved global value
357 * numbering.
358 *
359 * @param receiver the object that is accessed
360 * @param offset the offset at which to access the object in bytes
361 * @param condition the condition that makes this access safe also at an earlier location in the
362 * program
363 * @param locationIdentity the location identity token that can be used for improved global
364 * value numbering or null
365 * @return the accessed value
366 */
367 @Deprecated
368 public static double unsafeGetDouble(Object receiver, long offset, boolean condition, Object locationIdentity) {
369 return UNSAFE.getDouble(receiver, offset);
370 }
371
372 /**
373 * Unsafe access to an Object value within an object. The condition parameter gives a hint to
374 * the compiler under which circumstances this access can be moved to an earlier location in the
375 * program. The location identity gives a hint to the compiler for improved global value
376 * numbering.
377 *
378 * @param receiver the object that is accessed
379 * @param offset the offset at which to access the object in bytes
380 * @param condition the condition that makes this access safe also at an earlier location in the
381 * program
382 * @param locationIdentity the location identity token that can be used for improved global
383 * value numbering or null
384 * @return the accessed value
385 */
386 @Deprecated
387 public static Object unsafeGetObject(Object receiver, long offset, boolean condition, Object locationIdentity) {
388 return UNSAFE.getObject(receiver, offset);
389 }
390
391 /**
392 * Write a boolean value within an object. The location identity gives a hint to the compiler
393 * for improved global value numbering.
394 *
395 * @param receiver the object that is written to
396 * @param offset the offset at which to write to the object in bytes
397 * @param value the value to be written
398 * @param locationIdentity the location identity token that can be used for improved global
399 * value numbering or null
400 */
401 @Deprecated
402 public static void unsafePutBoolean(Object receiver, long offset, boolean value, Object locationIdentity) {
403 UNSAFE.putBoolean(receiver, offset, value);
404 }
405
406 /**
407 * Write a byte value within an object. The location identity gives a hint to the compiler for
408 * improved global value numbering.
409 *
410 * @param receiver the object that is written to
411 * @param offset the offset at which to write to the object in bytes
412 * @param value the value to be written
413 * @param locationIdentity the location identity token that can be used for improved global
414 * value numbering or null
415 */
416 @Deprecated
417 public static void unsafePutByte(Object receiver, long offset, byte value, Object locationIdentity) {
418 UNSAFE.putByte(receiver, offset, value);
419 }
420
421 /**
422 * Write a short value within an object. The location identity gives a hint to the compiler for
423 * improved global value numbering.
424 *
425 * @param receiver the object that is written to
426 * @param offset the offset at which to write to the object in bytes
427 * @param value the value to be written
428 * @param locationIdentity the location identity token that can be used for improved global
429 * value numbering or null
430 */
431 @Deprecated
432 public static void unsafePutShort(Object receiver, long offset, short value, Object locationIdentity) {
433 UNSAFE.putShort(receiver, offset, value);
434 }
435
436 /**
437 * Write an int value within an object. The location identity gives a hint to the compiler for
438 * improved global value numbering.
439 *
440 * @param receiver the object that is written to
441 * @param offset the offset at which to write to the object in bytes
442 * @param value the value to be written
443 * @param locationIdentity the location identity token that can be used for improved global
444 * value numbering or null
445 */
446 @Deprecated
447 public static void unsafePutInt(Object receiver, long offset, int value, Object locationIdentity) {
448 UNSAFE.putInt(receiver, offset, value);
449 }
450
451 /**
452 * Write a long value within an object. The location identity gives a hint to the compiler for
453 * improved global value numbering.
454 *
455 * @param receiver the object that is written to
456 * @param offset the offset at which to write to the object in bytes
457 * @param value the value to be written
458 * @param locationIdentity the location identity token that can be used for improved global
459 * value numbering or null
460 */
461 @Deprecated
462 public static void unsafePutLong(Object receiver, long offset, long value, Object locationIdentity) {
463 UNSAFE.putLong(receiver, offset, value);
464 }
465
466 /**
467 * Write a float value within an object. The location identity gives a hint to the compiler for
468 * improved global value numbering.
469 *
470 * @param receiver the object that is written to
471 * @param offset the offset at which to write to the object in bytes
472 * @param value the value to be written
473 * @param locationIdentity the location identity token that can be used for improved global
474 * value numbering or null
475 */
476 @Deprecated
477 public static void unsafePutFloat(Object receiver, long offset, float value, Object locationIdentity) {
478 UNSAFE.putFloat(receiver, offset, value);
479 }
480
481 /**
482 * Write a double value within an object. The location identity gives a hint to the compiler for
483 * improved global value numbering.
484 *
485 * @param receiver the object that is written to
486 * @param offset the offset at which to write to the object in bytes
487 * @param value the value to be written
488 * @param locationIdentity the location identity token that can be used for improved global
489 * value numbering or null
490 */
491 @Deprecated
492 public static void unsafePutDouble(Object receiver, long offset, double value, Object locationIdentity) {
493 UNSAFE.putDouble(receiver, offset, value);
494 }
495
496 /**
497 * Write an Object value within an object. The location identity gives a hint to the compiler
498 * for improved global value numbering.
499 *
500 * @param receiver the object that is written to
501 * @param offset the offset at which to write to the object in bytes
502 * @param value the value to be written
503 * @param locationIdentity the location identity token that can be used for improved global
504 * value numbering or null
505 */
506 @Deprecated
507 public static void unsafePutObject(Object receiver, long offset, Object value, Object locationIdentity) {
508 UNSAFE.putObject(receiver, offset, value);
509 }
510
511 /**
512 * Unsafe access to a final boolean value within an object. The condition parameter gives a hint
513 * to the compiler under which circumstances this access can be moved to an earlier location in
514 * the program. The location identity gives a hint to the compiler for improved global value
515 * numbering.
516 *
517 * @param receiver the object that is accessed
518 * @param offset the offset at which to access the object in bytes
519 * @param condition the condition that makes this access safe also at an earlier location in the
520 * program
521 * @param locationIdentity the location identity token that can be used for improved global
522 * value numbering or null
523 * @return the accessed value
524 */
525 @Deprecated
526 public static boolean unsafeGetFinalBoolean(Object receiver, long offset, boolean condition, Object locationIdentity) {
527 return UNSAFE.getBoolean(receiver, offset);
528 }
529
530 /**
531 * Unsafe access to a final byte value within an object. The condition parameter gives a hint to
532 * the compiler under which circumstances this access can be moved to an earlier location in the
533 * program. The location identity gives a hint to the compiler for improved global value
534 * numbering.
535 *
536 * @param receiver the object that is accessed
537 * @param offset the offset at which to access the object in bytes
538 * @param condition the condition that makes this access safe also at an earlier location in the
539 * program
540 * @param locationIdentity the location identity token that can be used for improved global
541 * value numbering or null
542 * @return the accessed value
543 */
544 @Deprecated
545 public static byte unsafeGetFinalByte(Object receiver, long offset, boolean condition, Object locationIdentity) {
546 return UNSAFE.getByte(receiver, offset);
547 }
548
549 /**
550 * Unsafe access to a final short value within an object. The condition parameter gives a hint
551 * to the compiler under which circumstances this access can be moved to an earlier location in
552 * the program. The location identity gives a hint to the compiler for improved global value
553 * numbering.
554 *
555 * @param receiver the object that is accessed
556 * @param offset the offset at which to access the object in bytes
557 * @param condition the condition that makes this access safe also at an earlier location in the
558 * program
559 * @param locationIdentity the location identity token that can be used for improved global
560 * value numbering or null
561 * @return the accessed value
562 */
563 @Deprecated
564 public static short unsafeGetFinalShort(Object receiver, long offset, boolean condition, Object locationIdentity) {
565 return UNSAFE.getShort(receiver, offset);
566 }
567
568 /**
569 * Unsafe access to a final int value within an object. The condition parameter gives a hint to
570 * the compiler under which circumstances this access can be moved to an earlier location in the
571 * program. The location identity gives a hint to the compiler for improved global value
572 * numbering.
573 *
574 * @param receiver the object that is accessed
575 * @param offset the offset at which to access the object in bytes
576 * @param condition the condition that makes this access safe also at an earlier location in the
577 * program
578 * @param locationIdentity the location identity token that can be used for improved global
579 * value numbering or null
580 * @return the accessed value
581 */
582 @Deprecated
583 public static int unsafeGetFinalInt(Object receiver, long offset, boolean condition, Object locationIdentity) {
584 return UNSAFE.getInt(receiver, offset);
585 }
586
587 /**
588 * Unsafe access to a final long value within an object. The condition parameter gives a hint to
589 * the compiler under which circumstances this access can be moved to an earlier location in the
590 * program. The location identity gives a hint to the compiler for improved global value
591 * numbering.
592 *
593 * @param receiver the object that is accessed
594 * @param offset the offset at which to access the object in bytes
595 * @param condition the condition that makes this access safe also at an earlier location in the
596 * program
597 * @param locationIdentity the location identity token that can be used for improved global
598 * value numbering or null
599 * @return the accessed value
600 */
601 @Deprecated
602 public static long unsafeGetFinalLong(Object receiver, long offset, boolean condition, Object locationIdentity) {
603 return UNSAFE.getLong(receiver, offset);
604 }
605
606 /**
607 * Unsafe access to a final float value within an object. The condition parameter gives a hint
608 * to the compiler under which circumstances this access can be moved to an earlier location in
609 * the program. The location identity gives a hint to the compiler for improved global value
610 * numbering.
611 *
612 * @param receiver the object that is accessed
613 * @param offset the offset at which to access the object in bytes
614 * @param condition the condition that makes this access safe also at an earlier location in the
615 * program
616 * @param locationIdentity the location identity token that can be used for improved global
617 * value numbering or null
618 * @return the accessed value
619 */
620 @Deprecated
621 public static float unsafeGetFinalFloat(Object receiver, long offset, boolean condition, Object locationIdentity) {
622 return UNSAFE.getFloat(receiver, offset);
623 }
624
625 /**
626 * Unsafe access to a final double value within an object. The condition parameter gives a hint
627 * to the compiler under which circumstances this access can be moved to an earlier location in
628 * the program. The location identity gives a hint to the compiler for improved global value
629 * numbering.
630 *
631 * @param receiver the object that is accessed
632 * @param offset the offset at which to access the object in bytes
633 * @param condition the condition that makes this access safe also at an earlier location in the
634 * program
635 * @param locationIdentity the location identity token that can be used for improved global
636 * value numbering or null
637 * @return the accessed value
638 */
639 @Deprecated
640 public static double unsafeGetFinalDouble(Object receiver, long offset, boolean condition, Object locationIdentity) {
641 return UNSAFE.getDouble(receiver, offset);
642 }
643
644 /**
645 * Unsafe access to a final Object value within an object. The condition parameter gives a hint
646 * to the compiler under which circumstances this access can be moved to an earlier location in
647 * the program. The location identity gives a hint to the compiler for improved global value
648 * numbering.
649 *
650 * @param receiver the object that is accessed
651 * @param offset the offset at which to access the object in bytes
652 * @param condition the condition that makes this access safe also at an earlier location in the
653 * program
654 * @param locationIdentity the location identity token that can be used for improved global
655 * value numbering or null
656 * @return the accessed value
657 */
658 @Deprecated
659 public static Object unsafeGetFinalObject(Object receiver, long offset, boolean condition, Object locationIdentity) {
660 return UNSAFE.getObject(receiver, offset);
661 }
662
663 /**
664 * Marks a method that it is considered as a boundary for Truffle partial evaluation. 187 * Marks a method that it is considered as a boundary for Truffle partial evaluation.
665 */ 188 */
666 @Retention(RetentionPolicy.RUNTIME) 189 @Retention(RetentionPolicy.RUNTIME)
667 @Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) 190 @Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
668 public @interface TruffleBoundary { 191 public @interface TruffleBoundary {