src/share/classes/sun/invoke/util/VerifyType.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File jdk Sdiff src/share/classes/sun/invoke/util

src/share/classes/sun/invoke/util/VerifyType.java

Print this page
rev 9491 : 8037209: Improvements and cleanups to bytecode assembly for lambda forms
Reviewed-by: ?
Contributed-by: john.r.rose@oracle.com


  23  * questions.
  24  */
  25 
  26 package sun.invoke.util;
  27 
  28 import java.lang.invoke.MethodType;
  29 import sun.invoke.empty.Empty;
  30 
  31 /**
  32  * This class centralizes information about the JVM verifier
  33  * and its requirements about type correctness.
  34  * @author jrose
  35  */
  36 public class VerifyType {
  37 
  38     private VerifyType() { }  // cannot instantiate
  39 
  40     /**
  41      * True if a value can be stacked as the source type and unstacked as the
  42      * destination type, without violating the JVM's type consistency.


















  43      *
  44      * @param src the type of a stacked value
  45      * @param dst the type by which we'd like to treat it

  46      * @return whether the retyping can be done without motion or reformatting
  47      */
  48     public static boolean isNullConversion(Class<?> src, Class<?> dst) {
  49         if (src == dst)            return true;
  50         // Verifier allows any interface to be treated as Object:

  51         if (dst.isInterface())     dst = Object.class;
  52         if (src.isInterface())     src = Object.class;
  53         if (src == dst)            return true;  // check again
  54         if (dst == void.class)     return true;  // drop any return value
  55         if (isNullType(src))       return !dst.isPrimitive();
  56         if (!src.isPrimitive())    return dst.isAssignableFrom(src);
  57         if (!dst.isPrimitive())    return false;
  58         // Verifier allows an int to carry byte, short, char, or even boolean:
  59         Wrapper sw = Wrapper.forPrimitiveType(src);
  60         if (dst == int.class)      return sw.isSubwordOrInt();
  61         Wrapper dw = Wrapper.forPrimitiveType(dst);
  62         if (!sw.isSubwordOrInt())  return false;
  63         if (!dw.isSubwordOrInt())  return false;
  64         if (!dw.isSigned() && sw.isSigned())  return false;
  65         return dw.bitWidth() > sw.bitWidth();
  66     }
  67 
  68     /**
  69      * Specialization of isNullConversion to reference types.
  70      * @param src the type of a stacked value
  71      * @param dst the reference type by which we'd like to treat it
  72      * @return whether the retyping can be done without a cast
  73      */
  74     public static boolean isNullReferenceConversion(Class<?> src, Class<?> dst) {
  75         assert(!dst.isPrimitive());
  76         if (dst.isInterface())  return true;   // verifier allows this
  77         if (isNullType(src))    return true;
  78         return dst.isAssignableFrom(src);
  79     }
  80 
  81     /**
  82      * Is the given type java.lang.Null or an equivalent null-only type?
  83      */
  84     public static boolean isNullType(Class<?> type) {
  85         if (type == null)  return false;
  86         return type == NULL_CLASS
  87             // This one may also be used as a null type.
  88             // TO DO: Decide if we really want to legitimize it here.
  89             // Probably we do, unless java.lang.Null really makes it into Java 7
  90             //|| type == Void.class
  91             // Locally known null-only class:
  92             || type == Empty.class
  93             ;
  94     }
  95     private static final Class<?> NULL_CLASS;
  96     static {
  97         Class<?> nullClass = null;
  98         try {
  99             nullClass = Class.forName("java.lang.Null");
 100         } catch (ClassNotFoundException ex) {
 101             // OK, we'll cope
 102         }
 103         NULL_CLASS = nullClass;
 104     }
 105 
 106     /**
 107      * True if a method handle can receive a call under a slightly different
 108      * method type, without moving or reformatting any stack elements.
 109      *
 110      * @param call the type of call being made
 111      * @param recv the type of the method handle receiving the call
 112      * @return whether the retyping can be done without motion or reformatting
 113      */
 114     public static boolean isNullConversion(MethodType call, MethodType recv) {
 115         if (call == recv)  return true;
 116         int len = call.parameterCount();
 117         if (len != recv.parameterCount())  return false;
 118         for (int i = 0; i < len; i++)
 119             if (!isNullConversion(call.parameterType(i), recv.parameterType(i)))
 120                 return false;
 121         return isNullConversion(recv.returnType(), call.returnType());
 122     }
 123 
 124     /**
 125      * Determine if the JVM verifier allows a value of type call to be
 126      * passed to a formal parameter (or return variable) of type recv.
 127      * Returns 1 if the verifier allows the types to match without conversion.
 128      * Returns -1 if the types can be made to match by a JVM-supported adapter.
 129      * Cases supported are:
 130      * <ul><li>checkcast
 131      * </li><li>conversion between any two integral types (but not floats)
 132      * </li><li>unboxing from a wrapper to its corresponding primitive type
 133      * </li><li>conversion in either direction between float and double
 134      * </li></ul>
 135      * (Autoboxing is not supported here; it must be done via Java code.)
 136      * Returns 0 otherwise.
 137      */
 138     public static int canPassUnchecked(Class<?> src, Class<?> dst) {
 139         if (src == dst)
 140             return 1;
 141 




  23  * questions.
  24  */
  25 
  26 package sun.invoke.util;
  27 
  28 import java.lang.invoke.MethodType;
  29 import sun.invoke.empty.Empty;
  30 
  31 /**
  32  * This class centralizes information about the JVM verifier
  33  * and its requirements about type correctness.
  34  * @author jrose
  35  */
  36 public class VerifyType {
  37 
  38     private VerifyType() { }  // cannot instantiate
  39 
  40     /**
  41      * True if a value can be stacked as the source type and unstacked as the
  42      * destination type, without violating the JVM's type consistency.
  43      * <p>
  44      * If both types are references, we apply the verifier's subclass check
  45      * (or subtyping, if keepInterfaces).
  46      * If the src type is a type guaranteed to be null (Void) it can be converted
  47      * to any other reference type.
  48      * <p>
  49      * If both types are primitives, we apply the verifier's primitive conversions.
  50      * These do not include Java conversions such as long to double, since those
  51      * require computation and (in general) stack depth changes.
  52      * But very simple 32-bit viewing changes, such as byte to int,
  53      * are null conversions, because they do not require any computation.
  54      * These conversions are from any type to a wider type up to 32 bits,
  55      * as long as the conversion is not signed to unsigned (byte to char).
  56      * <p>
  57      * The primitive type 'void' does not interconvert with any other type,
  58      * even though it is legal to drop any type from the stack and "return void".
  59      * The stack effects, though are difference between void and any other type,
  60      * so it is safer to report a non-trivial conversion.
  61      *
  62      * @param src the type of a stacked value
  63      * @param dst the type by which we'd like to treat it
  64      * @param keepInterfaces if false, we treat any interface as if it were Object
  65      * @return whether the retyping can be done without motion or reformatting
  66      */
  67     public static boolean isNullConversion(Class<?> src, Class<?> dst, boolean keepInterfaces) {
  68         if (src == dst)            return true;
  69         // Verifier allows any interface to be treated as Object:
  70         if (!keepInterfaces) {
  71             if (dst.isInterface())  dst = Object.class;
  72             if (src.isInterface())  src = Object.class;
  73             if (src == dst)         return true;  // check again
  74         }
  75         if (isNullType(src))       return !dst.isPrimitive();
  76         if (!src.isPrimitive())    return dst.isAssignableFrom(src);
  77         if (!dst.isPrimitive())    return false;
  78         // Verifier allows an int to carry byte, short, char, or even boolean:
  79         Wrapper sw = Wrapper.forPrimitiveType(src);
  80         if (dst == int.class)      return sw.isSubwordOrInt();
  81         Wrapper dw = Wrapper.forPrimitiveType(dst);
  82         if (!sw.isSubwordOrInt())  return false;
  83         if (!dw.isSubwordOrInt())  return false;
  84         if (!dw.isSigned() && sw.isSigned())  return false;
  85         return dw.bitWidth() > sw.bitWidth();
  86     }
  87 
  88     /**
  89      * Specialization of isNullConversion to reference types.
  90      * @param src the type of a stacked value
  91      * @param dst the reference type by which we'd like to treat it
  92      * @return whether the retyping can be done without a cast
  93      */
  94     public static boolean isNullReferenceConversion(Class<?> src, Class<?> dst) {
  95         assert(!dst.isPrimitive());
  96         if (dst.isInterface())  return true;   // verifier allows this
  97         if (isNullType(src))    return true;
  98         return dst.isAssignableFrom(src);
  99     }
 100 
 101     /**
 102      * Is the given type java.lang.Null or an equivalent null-only type?
 103      */
 104     public static boolean isNullType(Class<?> type) {
 105         // Any reference statically typed as Void is guaranteed to be null.
 106         // Therefore, it can be safely treated as a value of any
 107         // other type that admits null, i.e., a reference type.
 108         if (type == Void.class)  return true;


 109         // Locally known null-only class:
 110         if (type == Empty.class)  return true;
 111         return false;










 112     }
 113 
 114     /**
 115      * True if a method handle can receive a call under a slightly different
 116      * method type, without moving or reformatting any stack elements.
 117      *
 118      * @param call the type of call being made
 119      * @param recv the type of the method handle receiving the call
 120      * @return whether the retyping can be done without motion or reformatting
 121      */
 122     public static boolean isNullConversion(MethodType call, MethodType recv, boolean keepInterfaces) {
 123         if (call == recv)  return true;
 124         int len = call.parameterCount();
 125         if (len != recv.parameterCount())  return false;
 126         for (int i = 0; i < len; i++)
 127             if (!isNullConversion(call.parameterType(i), recv.parameterType(i), keepInterfaces))
 128                 return false;
 129         return isNullConversion(recv.returnType(), call.returnType(), keepInterfaces);
 130     }
 131 
 132     /**
 133      * Determine if the JVM verifier allows a value of type call to be
 134      * passed to a formal parameter (or return variable) of type recv.
 135      * Returns 1 if the verifier allows the types to match without conversion.
 136      * Returns -1 if the types can be made to match by a JVM-supported adapter.
 137      * Cases supported are:
 138      * <ul><li>checkcast
 139      * </li><li>conversion between any two integral types (but not floats)
 140      * </li><li>unboxing from a wrapper to its corresponding primitive type
 141      * </li><li>conversion in either direction between float and double
 142      * </li></ul>
 143      * (Autoboxing is not supported here; it must be done via Java code.)
 144      * Returns 0 otherwise.
 145      */
 146     public static int canPassUnchecked(Class<?> src, Class<?> dst) {
 147         if (src == dst)
 148             return 1;
 149 


src/share/classes/sun/invoke/util/VerifyType.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File