1 /*
   2  * Copyright (c) 2011, 2016, 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.UnsafeAccess.UNSAFE;
  26 import jdk.vm.ci.common.JVMCIError;
  27 import jdk.vm.ci.hotspot.HotSpotVMConfig.CompressEncoding;
  28 import jdk.vm.ci.meta.Constant;
  29 import jdk.vm.ci.meta.JavaConstant;
  30 import jdk.vm.ci.meta.JavaKind;
  31 import jdk.vm.ci.meta.MemoryAccessProvider;
  32 import jdk.vm.ci.meta.PrimitiveConstant;
  33 
  34 /**
  35  * HotSpot implementation of {@link MemoryAccessProvider}.
  36  */
  37 class HotSpotMemoryAccessProviderImpl implements HotSpotMemoryAccessProvider, HotSpotProxified {
  38 
  39     protected final HotSpotJVMCIRuntimeProvider runtime;
  40 
  41     HotSpotMemoryAccessProviderImpl(HotSpotJVMCIRuntimeProvider runtime) {
  42         this.runtime = runtime;
  43     }
  44 
  45     private static Object asObject(Constant base) {
  46         if (base instanceof HotSpotObjectConstantImpl) {
  47             return ((HotSpotObjectConstantImpl) base).object();
  48         } else {
  49             return null;
  50         }
  51     }
  52 
  53     private boolean isValidObjectFieldDisplacement(Constant base, long displacement) {
  54         if (base instanceof HotSpotMetaspaceConstant) {
  55             MetaspaceWrapperObject metaspaceObject = HotSpotMetaspaceConstantImpl.getMetaspaceObject(base);
  56             if (metaspaceObject instanceof HotSpotResolvedObjectTypeImpl) {
  57                 if (displacement == runtime.getConfig().classMirrorOffset) {
  58                     // Klass::_java_mirror is valid for all Klass* values
  59                     return true;
  60                 }
  61             } else {
  62                 throw new JVMCIError("%s", metaspaceObject);
  63             }
  64         }
  65         return false;
  66     }
  67 
  68     private static long asRawPointer(Constant base) {
  69         if (base instanceof HotSpotMetaspaceConstantImpl) {
  70             MetaspaceWrapperObject meta = HotSpotMetaspaceConstantImpl.getMetaspaceObject(base);
  71             return meta.getMetaspacePointer();
  72         } else if (base instanceof PrimitiveConstant) {
  73             PrimitiveConstant prim = (PrimitiveConstant) base;
  74             if (prim.getJavaKind().isNumericInteger()) {
  75                 return prim.asLong();
  76             }
  77         }
  78         throw new JVMCIError("%s", base);
  79     }
  80 
  81     private static long readRawValue(Constant baseConstant, long displacement, int bits) {
  82         Object base = asObject(baseConstant);
  83         if (base != null) {
  84             switch (bits) {
  85                 case Byte.SIZE:
  86                     return UNSAFE.getByte(base, displacement);
  87                 case Short.SIZE:
  88                     return UNSAFE.getShort(base, displacement);
  89                 case Integer.SIZE:
  90                     return UNSAFE.getInt(base, displacement);
  91                 case Long.SIZE:
  92                     return UNSAFE.getLong(base, displacement);
  93                 default:
  94                     throw new JVMCIError("%d", bits);
  95             }
  96         } else {
  97             long pointer = asRawPointer(baseConstant);
  98             switch (bits) {
  99                 case Byte.SIZE:
 100                     return UNSAFE.getByte(pointer + displacement);
 101                 case Short.SIZE:
 102                     return UNSAFE.getShort(pointer + displacement);
 103                 case Integer.SIZE:
 104                     return UNSAFE.getInt(pointer + displacement);
 105                 case Long.SIZE:
 106                     return UNSAFE.getLong(pointer + displacement);
 107                 default:
 108                     throw new JVMCIError("%d", bits);
 109             }
 110         }
 111     }
 112 
 113     private boolean verifyReadRawObject(Object expected, Constant base, long displacement, boolean compressed) {
 114         if (compressed == runtime.getConfig().useCompressedOops) {
 115             Object obj = asObject(base);
 116             if (obj != null) {
 117                 assert expected == UNSAFE.getObject(obj, displacement) : "readUnsafeOop doesn't agree with unsafe.getObject";
 118             }
 119         }
 120         if (base instanceof HotSpotMetaspaceConstant) {
 121             MetaspaceWrapperObject metaspaceObject = HotSpotMetaspaceConstantImpl.getMetaspaceObject(base);
 122             if (metaspaceObject instanceof HotSpotResolvedObjectTypeImpl) {
 123                 if (displacement == runtime.getConfig().classMirrorOffset) {
 124                     assert expected == ((HotSpotResolvedObjectTypeImpl) metaspaceObject).mirror();
 125                 }
 126             }
 127         }
 128         return true;
 129     }
 130 
 131     private Object readRawObject(Constant baseConstant, long initialDisplacement, boolean compressed) {
 132         long displacement = initialDisplacement;
 133 
 134         Object ret;
 135         Object base = asObject(baseConstant);
 136         if (base == null) {
 137             assert !compressed;
 138             displacement += asRawPointer(baseConstant);
 139             ret = runtime.getCompilerToVM().readUncompressedOop(displacement);
 140         } else {
 141             assert runtime.getConfig().useCompressedOops == compressed;
 142             ret = UNSAFE.getObject(base, displacement);
 143         }
 144         assert verifyReadRawObject(ret, baseConstant, initialDisplacement, compressed);
 145         return ret;
 146     }
 147 
 148     @Override
 149     public JavaConstant readUnsafeConstant(JavaKind kind, JavaConstant baseConstant, long displacement) {
 150         if (kind == JavaKind.Object) {
 151             Object o = readRawObject(baseConstant, displacement, runtime.getConfig().useCompressedOops);
 152             return HotSpotObjectConstantImpl.forObject(o);
 153         } else {
 154             int bits = kind.getByteCount() * Byte.SIZE;
 155             return readPrimitiveConstant(kind, baseConstant, displacement, bits);
 156         }
 157     }
 158 
 159     @Override
 160     public JavaConstant readPrimitiveConstant(JavaKind kind, Constant baseConstant, long initialDisplacement, int bits) {
 161         try {
 162             long rawValue = readRawValue(baseConstant, initialDisplacement, bits);
 163             switch (kind) {
 164                 case Boolean:
 165                     return JavaConstant.forBoolean(rawValue != 0);
 166                 case Byte:
 167                     return JavaConstant.forByte((byte) rawValue);
 168                 case Char:
 169                     return JavaConstant.forChar((char) rawValue);
 170                 case Short:
 171                     return JavaConstant.forShort((short) rawValue);
 172                 case Int:
 173                     return JavaConstant.forInt((int) rawValue);
 174                 case Long:
 175                     return JavaConstant.forLong(rawValue);
 176                 case Float:
 177                     return JavaConstant.forFloat(Float.intBitsToFloat((int) rawValue));
 178                 case Double:
 179                     return JavaConstant.forDouble(Double.longBitsToDouble(rawValue));
 180                 default:
 181                     throw new JVMCIError("Unsupported kind: %s", kind);
 182             }
 183         } catch (NullPointerException e) {
 184             return null;
 185         }
 186     }
 187 
 188     @Override
 189     public JavaConstant readObjectConstant(Constant base, long displacement) {
 190         if (!isValidObjectFieldDisplacement(base, displacement)) {
 191             return null;
 192         }
 193         return HotSpotObjectConstantImpl.forObject(readRawObject(base, displacement, false));
 194     }
 195 
 196     @Override
 197     public JavaConstant readNarrowOopConstant(Constant base, long displacement, CompressEncoding encoding) {
 198         assert encoding.equals(runtime.getConfig().getOopEncoding()) : "unexpected oop encoding: " + encoding + " != " + runtime.getConfig().getOopEncoding();
 199         return HotSpotObjectConstantImpl.forObject(readRawObject(base, displacement, true), true);
 200     }
 201 
 202     private HotSpotResolvedObjectTypeImpl readKlass(Constant base, long displacement, boolean compressed) {
 203         assert (base instanceof HotSpotMetaspaceConstantImpl) || (base instanceof HotSpotObjectConstantImpl) : base.getClass();
 204         Object baseObject = (base instanceof HotSpotMetaspaceConstantImpl) ? ((HotSpotMetaspaceConstantImpl) base).asResolvedJavaType() : ((HotSpotObjectConstantImpl) base).object();
 205         return runtime.getCompilerToVM().getResolvedJavaType(baseObject, displacement, compressed);
 206     }
 207 
 208     @Override
 209     public Constant readKlassPointerConstant(Constant base, long displacement) {
 210         HotSpotResolvedObjectTypeImpl klass = readKlass(base, displacement, false);
 211         if (klass == null) {
 212             return JavaConstant.NULL_POINTER;
 213         }
 214         return HotSpotMetaspaceConstantImpl.forMetaspaceObject(klass, false);
 215     }
 216 
 217     @Override
 218     public Constant readNarrowKlassPointerConstant(Constant base, long displacement, CompressEncoding encoding) {
 219         HotSpotResolvedObjectTypeImpl klass = readKlass(base, displacement, true);
 220         if (klass == null) {
 221             return HotSpotCompressedNullConstant.COMPRESSED_NULL;
 222         }
 223         return HotSpotMetaspaceConstantImpl.forMetaspaceObject(klass, true);
 224     }
 225 
 226     @Override
 227     public Constant readMethodPointerConstant(Constant base, long displacement) {
 228         assert (base instanceof HotSpotObjectConstantImpl);
 229         Object baseObject = ((HotSpotObjectConstantImpl) base).object();
 230         HotSpotResolvedJavaMethodImpl method = runtime.getCompilerToVM().getResolvedJavaMethod(baseObject, displacement);
 231         return HotSpotMetaspaceConstantImpl.forMetaspaceObject(method, false);
 232     }
 233 
 234     @Override
 235     public Constant readSymbolConstant(Constant base, long displacement) {
 236         int bits = runtime.getConfig().symbolPointerSize * Byte.SIZE;
 237         long pointer = readRawValue(base, displacement, bits);
 238         if (pointer == 0) {
 239             return JavaConstant.NULL_POINTER;
 240         } else {
 241             String symbol = runtime.getCompilerToVM().getSymbol(pointer);
 242             return new HotSpotSymbol(symbol, pointer).asConstant();
 243         }
 244     }
 245 }