001/*
002 * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.compiler.common.type;
024
025import jdk.internal.jvmci.meta.*;
026
027/**
028 * Type describing primitive values.
029 */
030public abstract class PrimitiveStamp extends ArithmeticStamp {
031
032    private final int bits;
033
034    protected PrimitiveStamp(int bits, ArithmeticOpTable ops) {
035        super(ops);
036        this.bits = bits;
037    }
038
039    /**
040     * The width in bits of the value described by this stamp.
041     */
042    public int getBits() {
043        return bits;
044    }
045
046    public static int getBits(Stamp stamp) {
047        if (stamp instanceof PrimitiveStamp) {
048            return ((PrimitiveStamp) stamp).getBits();
049        } else {
050            return 0;
051        }
052    }
053
054    @Override
055    public Constant readConstant(MemoryAccessProvider provider, Constant base, long displacement) {
056        return provider.readPrimitiveConstant(getStackKind(), base, displacement, getBits());
057    }
058
059    @Override
060    public int hashCode() {
061        final int prime = 31;
062        int result = super.hashCode();
063        result = prime * result + bits;
064        return result;
065    }
066
067    @Override
068    public boolean equals(Object obj) {
069        if (this == obj) {
070            return true;
071        }
072        if (!(obj instanceof PrimitiveStamp)) {
073            return false;
074        }
075        PrimitiveStamp other = (PrimitiveStamp) obj;
076        if (bits != other.bits) {
077            return false;
078        }
079        return super.equals(obj);
080    }
081}