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     public void accept(Visitor v) {
  49         super.accept(v);
  50         v.visitLong(encoding.getBase());
  51         v.visitInt(encoding.getShift());
  52     }
  53 
  54     @Override
  55     protected abstract AbstractObjectStamp copyWith(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull);
  56 
  57     public Stamp uncompressed() {
  58         return new ObjectStamp(type(), isExactType(), nonNull(), alwaysNull());
  59     }
  60 
  61     public CompressEncoding getEncoding() {
  62         return encoding;
  63     }
  64 
  65     @Override
  66     public LIRKind getLIRKind(LIRKindTool tool) {
  67         return tool.getNarrowOopKind();
  68     }
  69 
  70     @Override
  71     public String toString() {
  72         StringBuilder str = new StringBuilder();
  73         str.append('n');
  74         appendString(str);
  75         return str.toString();
  76     }
  77 
  78     @Override
  79     public boolean isCompatible(Stamp other) {
  80         if (this == other) {
  81             return true;
  82         }
  83         if (other instanceof NarrowOopStamp) {
  84             NarrowOopStamp narrow = (NarrowOopStamp) other;
  85             return encoding.equals(narrow.encoding);
  86         }
  87         return false;
  88     }
  89 
  90     @Override
  91     public abstract Constant readConstant(MemoryAccessProvider provider, Constant base, long displacement);
  92 
  93     @Override
  94     public int hashCode() {
  95         final int prime = 31;
  96         int result = super.hashCode();
  97         result = prime * result + encoding.hashCode();
  98         return result;
  99     }
 100 
 101     @Override
 102     public boolean equals(Object obj) {
 103         if (this == obj) {
 104             return true;
 105         }
 106         if (obj == null || getClass() != obj.getClass()) {
 107             return false;
 108         }
 109         NarrowOopStamp other = (NarrowOopStamp) obj;
 110         if (!encoding.equals(other.encoding)) {
 111             return false;
 112         }
 113         return super.equals(other);
 114     }
 115 
 116     @Override
 117     public abstract boolean isCompatible(Constant other);
 118 }