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