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