AbstractPointerStamp.java revision 12651:6ef01bd40ce2
1122715Sbde/*
2122715Sbde * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3122715Sbde * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4122715Sbde *
5122715Sbde * This code is free software; you can redistribute it and/or modify it
685909Simp * under the terms of the GNU General Public License version 2 only, as
788893Simp * published by the Free Software Foundation.
888893Simp *
988969Simp * This code is distributed in the hope that it will be useful, but WITHOUT
1085909Simp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11115572Sphk * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12115572Sphk * version 2 for more details (a copy is included in the LICENSE file that
13115572Sphk * accompanied this code).
14115572Sphk *
15191794Sjhb * You should have received a copy of the GNU General Public License version
16191794Sjhb * 2 along with this work; if not, write to the Free Software Foundation,
17115572Sphk * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18205640Snetchild *
19205640Snetchild * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20205640Snetchild * or visit www.oracle.com if you need additional information or have any
21205640Snetchild * questions.
22206082Snetchild */
23206082Snetchildpackage org.graalvm.compiler.core.common.type;
24206082Snetchild
25206082Snetchildimport jdk.vm.ci.meta.Constant;
26111211Sruimport jdk.vm.ci.meta.JavaConstant;
2785909Simpimport jdk.vm.ci.meta.JavaKind;
28111802Sru
29111802Sru/**
30111211Sru * Abstract base class of all pointer types.
31111211Sru */
32111211Srupublic abstract class AbstractPointerStamp extends Stamp {
33111211Sru
34111802Sru    private final boolean nonNull;
35111802Sru    private final boolean alwaysNull;
36111211Sru
37111211Sru    protected AbstractPointerStamp(boolean nonNull, boolean alwaysNull) {
3885909Simp        this.nonNull = nonNull;
39137596Simp        this.alwaysNull = alwaysNull;
40147155Simp    }
41137596Simp
42147011Smux    public boolean nonNull() {
43142424Simp        assert !this.isEmpty() || nonNull;
44142413Simp        return nonNull;
45137596Simp    }
46147011Smux
47137596Simp    public boolean alwaysNull() {
48137596Simp        return alwaysNull;
49137596Simp    }
50137596Simp
51111211Sru    protected abstract AbstractPointerStamp copyWith(boolean newNonNull, boolean newAlwaysNull);
52111211Sru
53167845Simp    @Override
54111211Sru    public int hashCode() {
55155427Sru        final int prime = 31;
56111802Sru        int result = 1;
57111802Sru        result = prime * result + (alwaysNull ? 1231 : 1237);
58111766Sru        result = prime * result + (nonNull ? 1231 : 1237);
59111211Sru        return result;
60111766Sru    }
61111766Sru
62111211Sru    @Override
63111211Sru    public Stamp join(Stamp stamp) {
64111211Sru        AbstractPointerStamp other = (AbstractPointerStamp) stamp;
65111211Sru        boolean joinNonNull = this.nonNull || other.nonNull;
66111211Sru        boolean joinAlwaysNull = this.alwaysNull || other.alwaysNull;
67111211Sru        if (joinNonNull && joinAlwaysNull) {
68111211Sru            return empty();
69111211Sru        } else {
70151636Simp            return copyWith(joinNonNull, joinAlwaysNull);
71151636Simp        }
72151636Simp    }
73151636Simp
74151750Sru    @Override
75151750Sru    public Stamp improveWith(Stamp other) {
76151731Sru        return join(other);
77151750Sru    }
78151731Sru
79151646Sru    @Override
80151646Sru    public Stamp meet(Stamp stamp) {
81116252Sgrog        AbstractPointerStamp other = (AbstractPointerStamp) stamp;
82123965Sbde        boolean meetNonNull = this.nonNull && other.nonNull;
83116252Sgrog        boolean meetAlwaysNull = this.alwaysNull && other.alwaysNull;
84123965Sbde        return copyWith(meetNonNull, meetAlwaysNull);
85123965Sbde    }
86135611Sphk
87124776Sru    @Override
88116252Sgrog    public Stamp unrestricted() {
89123965Sbde        return copyWith(false, false);
9085909Simp    }
91124776Sru
9285909Simp    @Override
93151636Simp    public boolean equals(Object obj) {
9485909Simp        if (this == obj) {
9585909Simp            return true;
9685909Simp        }
97206082Snetchild        if (obj == null || getClass() != obj.getClass()) {
98125775Sru            return false;
99125775Sru        }
100125775Sru        AbstractPointerStamp other = (AbstractPointerStamp) obj;
10185909Simp        return this.alwaysNull == other.alwaysNull && this.nonNull == other.nonNull;
102159560Scognet    }
103175984Sraj
104159560Scognet    @Override
10585909Simp    public Constant asConstant() {
106116691Sru        if (alwaysNull) {
107131129Simp            return JavaConstant.NULL_POINTER;
10885909Simp        } else {
10985909Simp            return null;
110116341Smarkm        }
111116341Smarkm    }
11285909Simp
113102082Sbde    @Override
114102082Sbde    public JavaKind getStackKind() {
115102082Sbde        return JavaKind.Illegal;
116102082Sbde    }
11785909Simp}
11895844Sobrien