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     @Override
  43     public void accept(Visitor v) {
  44         v.visitInt(bits);
  45     }
  46 
  47     /**
  48      * The width in bits of the value described by this stamp.
  49      */
  50     public int getBits() {
  51         return bits;
  52     }
  53 
  54     public static int getBits(Stamp stamp) {
  55         if (stamp instanceof PrimitiveStamp) {
  56             return ((PrimitiveStamp) stamp).getBits();
  57         } else {
  58             return 0;
  59         }
  60     }
  61 
  62     @Override
  63     public Constant readConstant(MemoryAccessProvider provider, Constant base, long displacement) {
  64         try {
  65             return provider.readPrimitiveConstant(getStackKind(), base, displacement, getBits());
  66         } catch (IllegalArgumentException e) {
  67             /*
  68              * It's possible that the base and displacement aren't valid together so simply return
  69              * null.
  70              */
  71             return null;
  72         }
  73     }
  74 
  75     @Override
  76     public int hashCode() {
  77         final int prime = 31;
  78         int result = super.hashCode();
  79         result = prime * result + bits;
  80         return result;
  81     }
  82 
  83     @Override
  84     public boolean equals(Object obj) {
  85         if (this == obj) {
  86             return true;
  87         }
  88         if (!(obj instanceof PrimitiveStamp)) {
  89             return false;
  90         }
  91         PrimitiveStamp other = (PrimitiveStamp) obj;
  92         if (bits != other.bits) {
  93             return false;
  94         }
  95         return super.equals(obj);
  96     }
  97 }