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