1 /*
   2  * Copyright (c) 2014, 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 
  24 
  25 package org.graalvm.compiler.hotspot.nodes.type;
  26 
  27 import org.graalvm.compiler.core.common.CompressEncoding;
  28 import org.graalvm.compiler.core.common.type.AbstractObjectStamp;
  29 import org.graalvm.compiler.core.common.type.ObjectStamp;
  30 import org.graalvm.compiler.core.common.type.Stamp;
  31 import org.graalvm.compiler.debug.GraalError;
  32 import org.graalvm.compiler.nodes.CompressionNode.CompressionOp;
  33 import org.graalvm.compiler.nodes.type.NarrowOopStamp;
  34 
  35 import jdk.vm.ci.hotspot.HotSpotCompressedNullConstant;
  36 import jdk.vm.ci.hotspot.HotSpotMemoryAccessProvider;
  37 import jdk.vm.ci.hotspot.HotSpotObjectConstant;
  38 import jdk.vm.ci.meta.Constant;
  39 import jdk.vm.ci.meta.JavaConstant;
  40 import jdk.vm.ci.meta.MemoryAccessProvider;
  41 import jdk.vm.ci.meta.ResolvedJavaType;
  42 
  43 public final class HotSpotNarrowOopStamp extends NarrowOopStamp {
  44     private HotSpotNarrowOopStamp(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull, CompressEncoding encoding) {
  45         super(type, exactType, nonNull, alwaysNull, encoding);
  46     }
  47 
  48     @Override
  49     protected AbstractObjectStamp copyWith(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull) {
  50         return new HotSpotNarrowOopStamp(type, exactType, nonNull, alwaysNull, getEncoding());
  51     }
  52 
  53     public static Stamp compressed(AbstractObjectStamp stamp, CompressEncoding encoding) {
  54         return new HotSpotNarrowOopStamp(stamp.type(), stamp.isExactType(), stamp.nonNull(), stamp.alwaysNull(), encoding);
  55     }
  56 
  57     @Override
  58     public Constant readConstant(MemoryAccessProvider provider, Constant base, long displacement) {
  59         try {
  60             HotSpotMemoryAccessProvider hsProvider = (HotSpotMemoryAccessProvider) provider;
  61             return hsProvider.readNarrowOopConstant(base, displacement);
  62         } catch (IllegalArgumentException e) {
  63             return null;
  64         }
  65     }
  66 
  67     @Override
  68     public JavaConstant asConstant() {
  69         if (alwaysNull()) {
  70             return HotSpotCompressedNullConstant.COMPRESSED_NULL;
  71         } else {
  72             return null;
  73         }
  74     }
  75 
  76     @Override
  77     public boolean isCompatible(Constant other) {
  78         if (other instanceof HotSpotObjectConstant) {
  79             return ((HotSpotObjectConstant) other).isCompressed();
  80         }
  81         return true;
  82     }
  83 
  84     public static Stamp mkStamp(CompressionOp op, Stamp input, CompressEncoding encoding) {
  85         switch (op) {
  86             case Compress:
  87                 if (input instanceof ObjectStamp) {
  88                     // compressed oop
  89                     return HotSpotNarrowOopStamp.compressed((ObjectStamp) input, encoding);
  90                 } else if (input instanceof KlassPointerStamp) {
  91                     // compressed klass pointer
  92                     return ((KlassPointerStamp) input).compressed(encoding);
  93                 }
  94                 break;
  95             case Uncompress:
  96                 if (input instanceof NarrowOopStamp) {
  97                     // oop
  98                     assert encoding.equals(((NarrowOopStamp) input).getEncoding());
  99                     return ((NarrowOopStamp) input).uncompressed();
 100                 } else if (input instanceof KlassPointerStamp) {
 101                     // metaspace pointer
 102                     assert encoding.equals(((KlassPointerStamp) input).getEncoding());
 103                     return ((KlassPointerStamp) input).uncompressed();
 104                 }
 105                 break;
 106         }
 107         throw GraalError.shouldNotReachHere(String.format("Unexpected input stamp %s", input));
 108     }
 109 
 110 }