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.core.common.type;
  26 
  27 import jdk.vm.ci.meta.Constant;
  28 import jdk.vm.ci.meta.JavaConstant;
  29 import jdk.vm.ci.meta.JavaKind;
  30 
  31 /**
  32  * Abstract base class of all pointer types.
  33  */
  34 public abstract class AbstractPointerStamp extends Stamp {
  35 
  36     private final boolean nonNull;
  37     private final boolean alwaysNull;
  38 
  39     @Override
  40     public void accept(Visitor v) {
  41         v.visitBoolean(nonNull);
  42         v.visitBoolean(alwaysNull);
  43     }
  44 
  45     protected AbstractPointerStamp(boolean nonNull, boolean alwaysNull) {
  46         this.nonNull = nonNull;
  47         this.alwaysNull = alwaysNull;
  48     }
  49 
  50     public boolean nonNull() {
  51         assert !this.isEmpty() || nonNull;
  52         return nonNull;
  53     }
  54 
  55     public boolean alwaysNull() {
  56         return alwaysNull;
  57     }
  58 
  59     protected abstract AbstractPointerStamp copyWith(boolean newNonNull, boolean newAlwaysNull);
  60 
  61     @Override
  62     public int hashCode() {
  63         final int prime = 31;
  64         int result = 1;
  65         result = prime * result + (alwaysNull ? 1231 : 1237);
  66         result = prime * result + (nonNull ? 1231 : 1237);
  67         return result;
  68     }
  69 
  70     protected Stamp defaultPointerJoin(Stamp stamp) {
  71         assert getClass() == stamp.getClass();
  72         AbstractPointerStamp other = (AbstractPointerStamp) stamp;
  73         boolean joinNonNull = this.nonNull || other.nonNull;
  74         boolean joinAlwaysNull = this.alwaysNull || other.alwaysNull;
  75         if (joinNonNull && joinAlwaysNull) {
  76             return empty();
  77         } else {
  78             return copyWith(joinNonNull, joinAlwaysNull);
  79         }
  80     }
  81 
  82     @Override
  83     public Stamp improveWith(Stamp other) {
  84         return join(other);
  85     }
  86 
  87     @Override
  88     public Stamp meet(Stamp stamp) {
  89         AbstractPointerStamp other = (AbstractPointerStamp) stamp;
  90         boolean meetNonNull = this.nonNull && other.nonNull;
  91         boolean meetAlwaysNull = this.alwaysNull && other.alwaysNull;
  92         return copyWith(meetNonNull, meetAlwaysNull);
  93     }
  94 
  95     @Override
  96     public Stamp unrestricted() {
  97         return copyWith(false, false);
  98     }
  99 
 100     public static Stamp pointerNonNull(Stamp stamp) {
 101         AbstractPointerStamp pointer = (AbstractPointerStamp) stamp;
 102         return pointer.asNonNull();
 103     }
 104 
 105     public static Stamp pointerMaybeNull(Stamp stamp) {
 106         AbstractPointerStamp pointer = (AbstractPointerStamp) stamp;
 107         return pointer.asMaybeNull();
 108     }
 109 
 110     public static Stamp pointerAlwaysNull(Stamp stamp) {
 111         AbstractPointerStamp pointer = (AbstractPointerStamp) stamp;
 112         return pointer.asAlwaysNull();
 113     }
 114 
 115     public Stamp asNonNull() {
 116         if (isEmpty()) {
 117             return this;
 118         }
 119         return copyWith(true, false);
 120     }
 121 
 122     public Stamp asMaybeNull() {
 123         if (isEmpty()) {
 124             return this;
 125         }
 126         return copyWith(false, false);
 127     }
 128 
 129     public Stamp asAlwaysNull() {
 130         if (isEmpty()) {
 131             return this;
 132         }
 133         return copyWith(false, true);
 134     }
 135 
 136     @Override
 137     public boolean equals(Object obj) {
 138         if (this == obj) {
 139             return true;
 140         }
 141         if (obj == null || getClass() != obj.getClass()) {
 142             return false;
 143         }
 144         AbstractPointerStamp other = (AbstractPointerStamp) obj;
 145         return this.alwaysNull == other.alwaysNull && this.nonNull == other.nonNull;
 146     }
 147 
 148     @Override
 149     public Constant asConstant() {
 150         if (alwaysNull) {
 151             return nullConstant();
 152         }
 153         return super.asConstant();
 154     }
 155 
 156     public JavaConstant nullConstant() {
 157         return JavaConstant.NULL_POINTER;
 158     }
 159 
 160     @Override
 161     public JavaKind getStackKind() {
 162         return JavaKind.Illegal;
 163     }
 164 }