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 package org.graalvm.compiler.hotspot.nodes.type;
  24 
  25 import org.graalvm.compiler.core.common.LIRKind;
  26 import org.graalvm.compiler.core.common.spi.LIRKindTool;
  27 import org.graalvm.compiler.core.common.type.AbstractObjectStamp;
  28 import org.graalvm.compiler.core.common.type.ObjectStamp;
  29 import org.graalvm.compiler.core.common.type.Stamp;
  30 import org.graalvm.compiler.hotspot.CompressEncoding;
  31 
  32 import jdk.vm.ci.hotspot.HotSpotCompressedNullConstant;
  33 import jdk.vm.ci.hotspot.HotSpotMemoryAccessProvider;
  34 import jdk.vm.ci.hotspot.HotSpotObjectConstant;
  35 import jdk.vm.ci.meta.Constant;
  36 import jdk.vm.ci.meta.JavaConstant;
  37 import jdk.vm.ci.meta.MemoryAccessProvider;
  38 import jdk.vm.ci.meta.ResolvedJavaType;
  39 
  40 public class NarrowOopStamp extends AbstractObjectStamp {
  41 
  42     private final CompressEncoding encoding;
  43 
  44     public NarrowOopStamp(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull, CompressEncoding encoding) {
  45         super(type, exactType, nonNull, alwaysNull);
  46         this.encoding = encoding;
  47     }
  48 
  49     @Override
  50     protected AbstractObjectStamp copyWith(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull) {
  51         return new NarrowOopStamp(type, exactType, nonNull, alwaysNull, encoding);
  52     }
  53 
  54     public static Stamp compressed(AbstractObjectStamp stamp, CompressEncoding encoding) {
  55         return new NarrowOopStamp(stamp.type(), stamp.isExactType(), stamp.nonNull(), stamp.alwaysNull(), encoding);
  56     }
  57 
  58     public Stamp uncompressed() {
  59         return new ObjectStamp(type(), isExactType(), nonNull(), alwaysNull());
  60     }
  61 
  62     public CompressEncoding getEncoding() {
  63         return encoding;
  64     }
  65 
  66     @Override
  67     public LIRKind getLIRKind(LIRKindTool tool) {
  68         return ((HotSpotLIRKindTool) tool).getNarrowOopKind();
  69     }
  70 
  71     @Override
  72     public String toString() {
  73         StringBuilder str = new StringBuilder();
  74         str.append('n');
  75         appendString(str);
  76         return str.toString();
  77     }
  78 
  79     @Override
  80     public boolean isCompatible(Stamp other) {
  81         if (this == other) {
  82             return true;
  83         }
  84         if (other instanceof NarrowOopStamp) {
  85             NarrowOopStamp narrow = (NarrowOopStamp) other;
  86             return encoding.equals(narrow.encoding);
  87         }
  88         return false;
  89     }
  90 
  91     @Override
  92     public Constant readConstant(MemoryAccessProvider provider, Constant base, long displacement) {
  93         HotSpotMemoryAccessProvider hsProvider = (HotSpotMemoryAccessProvider) provider;
  94         return hsProvider.readNarrowOopConstant(base, displacement);
  95     }
  96 
  97     @Override
  98     public int hashCode() {
  99         final int prime = 31;
 100         int result = super.hashCode();
 101         result = prime * result + encoding.hashCode();
 102         return result;
 103     }
 104 
 105     @Override
 106     public boolean equals(Object obj) {
 107         if (this == obj) {
 108             return true;
 109         }
 110         if (obj == null || getClass() != obj.getClass()) {
 111             return false;
 112         }
 113         NarrowOopStamp other = (NarrowOopStamp) obj;
 114         if (!encoding.equals(other.encoding)) {
 115             return false;
 116         }
 117         return super.equals(other);
 118     }
 119 
 120     @Override
 121     public JavaConstant asConstant() {
 122         if (alwaysNull()) {
 123             return HotSpotCompressedNullConstant.COMPRESSED_NULL;
 124         } else {
 125             return null;
 126         }
 127     }
 128 
 129     @Override
 130     public boolean isCompatible(Constant other) {
 131         if (other instanceof HotSpotObjectConstant) {
 132             return ((HotSpotObjectConstant) other).isCompressed();
 133         }
 134         return true;
 135     }
 136 }