1 /*
   2  * Copyright (c) 2009, 2019, 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.HotSpotJVMCIRuntime.runtime;
  26 
  27 import jdk.vm.ci.meta.Assumptions;
  28 import jdk.vm.ci.meta.JavaConstant;
  29 import jdk.vm.ci.meta.JavaKind;
  30 import jdk.vm.ci.meta.ResolvedJavaType;
  31 
  32 /**
  33  * Represents a constant non-{@code null} object reference, within the compiler and across the
  34  * compiler/runtime interface.
  35  */
  36 abstract class HotSpotObjectConstantImpl implements HotSpotObjectConstant {
  37 
  38     protected final boolean compressed;
  39 
  40     HotSpotObjectConstantImpl(boolean compressed) {
  41         this.compressed = compressed;
  42     }
  43 
  44     @Override
  45     public JavaKind getJavaKind() {
  46         return JavaKind.Object;
  47     }
  48 
  49     @Override
  50     public boolean isCompressed() {
  51         return compressed;
  52     }
  53 
  54     @Override
  55     public abstract JavaConstant compress();
  56 
  57     @Override
  58     public abstract JavaConstant uncompress();
  59 
  60     @Override
  61     public HotSpotResolvedObjectType getType() {
  62         return runtime().reflection.getType(this);
  63     }
  64 
  65     @Override
  66     public abstract int getIdentityHashCode();
  67 
  68     @Override
  69     public JavaConstant getCallSiteTarget(Assumptions assumptions) {
  70         if (runtime().getCallSite().isInstance(this)) {
  71             HotSpotObjectConstantImpl target = (HotSpotObjectConstantImpl) runtime().getHostJVMCIBackend().getConstantReflection().readFieldValue(
  72                             HotSpotMethodHandleAccessProvider.Internals.instance().callSiteTargetField, this);
  73             if (!runtime().getConstantCallSite().isInstance(this)) {
  74                 if (assumptions == null) {
  75                     return null;
  76                 }
  77                 assumptions.record(new Assumptions.CallSiteTargetValue(this, target));
  78             }
  79             return target;
  80         }
  81         return null;
  82     }
  83 
  84     @Override
  85     public boolean isInternedString() {
  86         return runtime().compilerToVm.isInternedString(this);
  87     }
  88 
  89     @Override
  90     public <T> T asObject(Class<T> type) {
  91         return runtime().reflection.asObject(this, type);
  92     }
  93 
  94     @Override
  95     public Object asObject(ResolvedJavaType type) {
  96         return runtime().reflection.asObject(this, (HotSpotResolvedJavaType) type);
  97     }
  98 
  99     @Override
 100     public boolean isNull() {
 101         return false;
 102     }
 103 
 104     @Override
 105     public boolean isDefaultForKind() {
 106         return false;
 107     }
 108 
 109     @Override
 110     public Object asBoxedPrimitive() {
 111         throw new IllegalArgumentException();
 112     }
 113 
 114     @Override
 115     public int asInt() {
 116         throw new IllegalArgumentException();
 117     }
 118 
 119     @Override
 120     public boolean asBoolean() {
 121         throw new IllegalArgumentException();
 122     }
 123 
 124     @Override
 125     public long asLong() {
 126         throw new IllegalArgumentException();
 127     }
 128 
 129     @Override
 130     public float asFloat() {
 131         throw new IllegalArgumentException();
 132     }
 133 
 134     @Override
 135     public double asDouble() {
 136         throw new IllegalArgumentException();
 137     }
 138 
 139     @Override
 140     public boolean equals(Object o) {
 141         if (o == this) {
 142             return true;
 143         } else if (o instanceof HotSpotObjectConstantImpl) {
 144             HotSpotObjectConstantImpl other = (HotSpotObjectConstantImpl) o;
 145             return runtime().reflection.equals(this, other);
 146         }
 147         return false;
 148     }
 149 
 150     @Override
 151     public int hashCode() {
 152         return getIdentityHashCode();
 153     }
 154 
 155     @Override
 156     public String toValueString() {
 157         if (runtime().getJavaLangString().isInstance(this)) {
 158             return "\"" + runtime().reflection.asString(this) + "\"";
 159         } else {
 160             return runtime().reflection.formatString(this);
 161         }
 162     }
 163 
 164     @Override
 165     public String toString() {
 166         return (compressed ? "NarrowOop" : getJavaKind().getJavaName()) + "[" + runtime().reflection.formatString(this) + "]";
 167     }
 168 
 169     public JavaConstant readFieldValue(HotSpotResolvedJavaField field, boolean isVolatile) {
 170         return runtime().reflection.readFieldValue(this, field, isVolatile);
 171     }
 172 
 173     public ResolvedJavaType asJavaType() {
 174         return runtime().reflection.asJavaType(this);
 175     }
 176 }