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