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.MemoryAccessProvider;
  29 
  30 /**
  31  * Type describing primitive values.
  32  */
  33 public abstract class PrimitiveStamp extends ArithmeticStamp {
  34 
  35     private final int bits;
  36 
  37     protected PrimitiveStamp(int bits, ArithmeticOpTable ops) {
  38         super(ops);
  39         this.bits = bits;
  40     }
  41 
  42     /**
  43      * The width in bits of the value described by this stamp.
  44      */
  45     public int getBits() {
  46         return bits;
  47     }
  48 
  49     public static int getBits(Stamp stamp) {
  50         if (stamp instanceof PrimitiveStamp) {
  51             return ((PrimitiveStamp) stamp).getBits();
  52         } else {
  53             return 0;
  54         }
  55     }
  56 
  57     @Override
  58     public Constant readConstant(MemoryAccessProvider provider, Constant base, long displacement) {
  59         try {
  60             return provider.readPrimitiveConstant(getStackKind(), base, displacement, getBits());
  61         } catch (IllegalArgumentException e) {
  62             /*
  63              * It's possible that the base and displacement aren't valid together so simply return
  64              * null.
  65              */
  66             return null;
  67         }
  68     }
  69 
  70     @Override
  71     public int hashCode() {
  72         final int prime = 31;
  73         int result = super.hashCode();
  74         result = prime * result + bits;
  75         return result;
  76     }
  77 
  78     @Override
  79     public boolean equals(Object obj) {
  80         if (this == obj) {
  81             return true;
  82         }
  83         if (!(obj instanceof PrimitiveStamp)) {
  84             return false;
  85         }
  86         PrimitiveStamp other = (PrimitiveStamp) obj;
  87         if (bits != other.bits) {
  88             return false;
  89         }
  90         return super.equals(obj);
  91     }
  92 }