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     @Override
  63     public Stamp join(Stamp stamp) {
  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     @Override
  93     public boolean equals(Object obj) {
  94         if (this == obj) {
  95             return true;
  96         }
  97         if (obj == null || getClass() != obj.getClass()) {
  98             return false;
  99         }
 100         AbstractPointerStamp other = (AbstractPointerStamp) obj;
 101         return this.alwaysNull == other.alwaysNull && this.nonNull == other.nonNull;
 102     }
 103 
 104     @Override
 105     public Constant asConstant() {
 106         if (alwaysNull) {
 107             return JavaConstant.NULL_POINTER;
 108         } else {
 109             return null;
 110         }
 111     }
 112 
 113     @Override
 114     public JavaKind getStackKind() {
 115         return JavaKind.Illegal;
 116     }
 117 }