PrimitiveStamp.java revision 12651:6ef01bd40ce2
1343181Sdim/*
2343181Sdim * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
3353358Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4353358Sdim *
5353358Sdim * This code is free software; you can redistribute it and/or modify it
6343181Sdim * under the terms of the GNU General Public License version 2 only, as
7343181Sdim * published by the Free Software Foundation.
8343181Sdim *
9343181Sdim * This code is distributed in the hope that it will be useful, but WITHOUT
10343181Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11343181Sdim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12343181Sdim * version 2 for more details (a copy is included in the LICENSE file that
13343181Sdim * accompanied this code).
14343181Sdim *
15343181Sdim * You should have received a copy of the GNU General Public License version
16343181Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17343181Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18343181Sdim *
19343181Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20343181Sdim * or visit www.oracle.com if you need additional information or have any
21343181Sdim * questions.
22343181Sdim */
23343181Sdimpackage org.graalvm.compiler.core.common.type;
24343181Sdim
25343181Sdimimport jdk.vm.ci.meta.Constant;
26343181Sdimimport jdk.vm.ci.meta.MemoryAccessProvider;
27343181Sdim
28343181Sdim/**
29343181Sdim * Type describing primitive values.
30353358Sdim */
31353358Sdimpublic abstract class PrimitiveStamp extends ArithmeticStamp {
32343181Sdim
33343181Sdim    private final int bits;
34343181Sdim
35343181Sdim    protected PrimitiveStamp(int bits, ArithmeticOpTable ops) {
36343181Sdim        super(ops);
37343181Sdim        this.bits = bits;
38343181Sdim    }
39343181Sdim
40343181Sdim    /**
41343181Sdim     * The width in bits of the value described by this stamp.
42343181Sdim     */
43343181Sdim    public int getBits() {
44343181Sdim        return bits;
45360784Sdim    }
46360784Sdim
47343181Sdim    public static int getBits(Stamp stamp) {
48343181Sdim        if (stamp instanceof PrimitiveStamp) {
49343181Sdim            return ((PrimitiveStamp) stamp).getBits();
50343181Sdim        } else {
51343181Sdim            return 0;
52343181Sdim        }
53343181Sdim    }
54360784Sdim
55360784Sdim    @Override
56343181Sdim    public Constant readConstant(MemoryAccessProvider provider, Constant base, long displacement) {
57343181Sdim        return provider.readPrimitiveConstant(getStackKind(), base, displacement, getBits());
58343181Sdim    }
59343181Sdim
60343181Sdim    @Override
61343181Sdim    public int hashCode() {
62343181Sdim        final int prime = 31;
63343181Sdim        int result = super.hashCode();
64343181Sdim        result = prime * result + bits;
65343181Sdim        return result;
66343181Sdim    }
67343181Sdim
68343181Sdim    @Override
69343181Sdim    public boolean equals(Object obj) {
70343181Sdim        if (this == obj) {
71343181Sdim            return true;
72343181Sdim        }
73343181Sdim        if (!(obj instanceof PrimitiveStamp)) {
74343181Sdim            return false;
75343181Sdim        }
76343181Sdim        PrimitiveStamp other = (PrimitiveStamp) obj;
77343181Sdim        if (bits != other.bits) {
78343181Sdim            return false;
79343181Sdim        }
80360784Sdim        return super.equals(obj);
81360784Sdim    }
82343181Sdim}
83343181Sdim