1 /*
   2  * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
   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  */
  23 package jdk.vm.ci.hotspot;
  24 
  25 import static jdk.vm.ci.hotspot.HotSpotResolvedObjectTypeImpl.*;
  26 
  27 import java.lang.invoke.*;
  28 
  29 import jdk.vm.ci.inittimer.*;
  30 import jdk.vm.ci.meta.*;
  31 
  32 /**
  33  * Represents a constant non-{@code null} object reference, within the compiler and across the
  34  * compiler/runtime interface.
  35  */
  36 public final class HotSpotObjectConstantImpl implements HotSpotObjectConstant, HotSpotProxified {
  37 
  38     public static JavaConstant forObject(Object object) {
  39         return forObject(object, false);
  40     }
  41 
  42     static JavaConstant forObject(Object object, boolean compressed) {
  43         if (object == null) {
  44             return compressed ? HotSpotCompressedNullConstant.COMPRESSED_NULL : JavaConstant.NULL_POINTER;
  45         } else {
  46             return new HotSpotObjectConstantImpl(object, compressed);
  47         }
  48     }
  49 
  50     static JavaConstant forStableArray(Object object, int stableDimension, boolean isDefaultStable) {
  51         if (object == null) {
  52             return JavaConstant.NULL_POINTER;
  53         } else {
  54             assert object.getClass().isArray();
  55             return new HotSpotObjectConstantImpl(object, false, stableDimension, isDefaultStable);
  56         }
  57     }
  58 
  59     public static JavaConstant forBoxedValue(JavaKind kind, Object value) {
  60         if (kind == JavaKind.Object) {
  61             return HotSpotObjectConstantImpl.forObject(value);
  62         } else {
  63             return JavaConstant.forBoxedPrimitive(value);
  64         }
  65     }
  66 
  67     static Object asBoxedValue(Constant constant) {
  68         if (JavaConstant.isNull(constant)) {
  69             return null;
  70         } else if (constant instanceof HotSpotObjectConstantImpl) {
  71             return ((HotSpotObjectConstantImpl) constant).object;
  72         } else {
  73             return ((JavaConstant) constant).asBoxedPrimitive();
  74         }
  75     }
  76 
  77     private final Object object;
  78     private final boolean compressed;
  79     private final byte stableDimension;
  80     private final boolean isDefaultStable;
  81 
  82     private HotSpotObjectConstantImpl(Object object, boolean compressed, int stableDimension, boolean isDefaultStable) {
  83         this.object = object;
  84         this.compressed = compressed;
  85         this.stableDimension = (byte) stableDimension;
  86         this.isDefaultStable = isDefaultStable;
  87         assert object != null;
  88         assert stableDimension == 0 || (object != null && object.getClass().isArray());
  89         assert stableDimension >= 0 && stableDimension <= 255;
  90         assert !isDefaultStable || stableDimension > 0;
  91     }
  92 
  93     private HotSpotObjectConstantImpl(Object object, boolean compressed) {
  94         this(object, compressed, 0, false);
  95     }
  96 
  97     @Override
  98     public JavaKind getJavaKind() {
  99         return JavaKind.Object;
 100     }
 101 
 102     /**
 103      * Package-private accessor for the object represented by this constant.
 104      */
 105     Object object() {
 106         return object;
 107     }
 108 
 109     /**
 110      * Determines if the object represented by this constant is {@link Object#equals(Object) equal}
 111      * to a given object.
 112      */
 113     public boolean isEqualTo(Object obj) {
 114         return object.equals(obj);
 115     }
 116 
 117     /**
 118      * Gets the class of the object represented by this constant.
 119      */
 120     public Class<?> getObjectClass() {
 121         return object.getClass();
 122     }
 123 
 124     public boolean isCompressed() {
 125         return compressed;
 126     }
 127 
 128     public JavaConstant compress() {
 129         assert !compressed;
 130         return new HotSpotObjectConstantImpl(object, true, stableDimension, isDefaultStable);
 131     }
 132 
 133     public JavaConstant uncompress() {
 134         assert compressed;
 135         return new HotSpotObjectConstantImpl(object, false, stableDimension, isDefaultStable);
 136     }
 137 
 138     public HotSpotResolvedObjectType getType() {
 139         return fromObjectClass(object.getClass());
 140     }
 141 
 142     public JavaConstant getClassLoader() {
 143         if (object instanceof Class) {
 144             /*
 145              * This is an intrinsic for getClassLoader0, which occurs after any security checks. We
 146              * can't call that directly so just call getClassLoader.
 147              */
 148             return HotSpotObjectConstantImpl.forObject(((Class<?>) object).getClassLoader());
 149         }
 150         return null;
 151     }
 152 
 153     public int getIdentityHashCode() {
 154         return System.identityHashCode(object);
 155     }
 156 
 157     public JavaConstant getComponentType() {
 158         if (object instanceof Class) {
 159             return HotSpotObjectConstantImpl.forObject(((Class<?>) object).getComponentType());
 160         }
 161         return null;
 162     }
 163 
 164     public JavaConstant getSuperclass() {
 165         if (object instanceof Class) {
 166             return HotSpotObjectConstantImpl.forObject(((Class<?>) object).getSuperclass());
 167         }
 168         return null;
 169     }
 170 
 171     public JavaConstant getCallSiteTarget(Assumptions assumptions) {
 172         if (object instanceof CallSite) {
 173             CallSite callSite = (CallSite) object;
 174             MethodHandle target = callSite.getTarget();
 175             if (!(callSite instanceof ConstantCallSite)) {
 176                 if (assumptions == null) {
 177                     return null;
 178                 }
 179                 assumptions.record(new Assumptions.CallSiteTargetValue(callSite, target));
 180             }
 181             return HotSpotObjectConstantImpl.forObject(target);
 182         }
 183         return null;
 184     }
 185 
 186     @SuppressFBWarnings(value = "ES_COMPARING_STRINGS_WITH_EQ", justification = "reference equality is what we want")
 187     public boolean isInternedString() {
 188         if (object instanceof String) {
 189             String s = (String) object;
 190             return s.intern() == s;
 191         }
 192         return false;
 193     }
 194 
 195     public <T> T asObject(Class<T> type) {
 196         if (type.isInstance(object)) {
 197             return type.cast(object);
 198         }
 199         return null;
 200     }
 201 
 202     public Object asObject(ResolvedJavaType type) {
 203         if (type.isInstance(this)) {
 204             return object;
 205         }
 206         return null;
 207     }
 208 
 209     @Override
 210     public boolean isNull() {
 211         return false;
 212     }
 213 
 214     @Override
 215     public boolean isDefaultForKind() {
 216         return false;
 217     }
 218 
 219     @Override
 220     public Object asBoxedPrimitive() {
 221         throw new IllegalArgumentException();
 222     }
 223 
 224     @Override
 225     public int asInt() {
 226         throw new IllegalArgumentException();
 227     }
 228 
 229     @Override
 230     public boolean asBoolean() {
 231         throw new IllegalArgumentException();
 232     }
 233 
 234     @Override
 235     public long asLong() {
 236         throw new IllegalArgumentException();
 237     }
 238 
 239     @Override
 240     public float asFloat() {
 241         throw new IllegalArgumentException();
 242     }
 243 
 244     @Override
 245     public double asDouble() {
 246         throw new IllegalArgumentException();
 247     }
 248 
 249     @Override
 250     public int hashCode() {
 251         return System.identityHashCode(object);
 252     }
 253 
 254     @Override
 255     public boolean equals(Object o) {
 256         if (o == this) {
 257             return true;
 258         } else if (o instanceof HotSpotObjectConstantImpl) {
 259             HotSpotObjectConstantImpl other = (HotSpotObjectConstantImpl) o;
 260             return object == other.object && compressed == other.compressed && stableDimension == other.stableDimension && isDefaultStable == other.isDefaultStable;
 261         }
 262         return false;
 263     }
 264 
 265     @Override
 266     public String toValueString() {
 267         if (object instanceof String) {
 268             return "\"" + (String) object + "\"";
 269         } else {
 270             return JavaKind.Object.format(object);
 271         }
 272     }
 273 
 274     @Override
 275     public String toString() {
 276         return (compressed ? "NarrowOop" : getJavaKind().getJavaName()) + "[" + JavaKind.Object.format(object) + "]";
 277     }
 278 
 279     /**
 280      * Number of stable dimensions if this constant is a stable array.
 281      */
 282     public int getStableDimension() {
 283         return stableDimension & 0xff;
 284     }
 285 
 286     /**
 287      * Returns {@code true} if this is a stable array constant and its elements should be considered
 288      * as stable regardless of whether they are default values.
 289      */
 290     public boolean isDefaultStable() {
 291         return isDefaultStable;
 292     }
 293 }