1/*
2 * Copyright (c) 2011, 2016, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package org.graalvm.word;
26
27// JaCoCo Exclude
28
29/**
30 * Marker interface for location identities. A different location identity of two memory accesses
31 * guarantees that the two accesses do not interfere.
32 *
33 * Clients of {@link LocationIdentity} must use {@link #equals(Object)}, not {@code ==}, when
34 * comparing two {@link LocationIdentity} values for equality. Likewise, they must not use
35 * {@link java.util.IdentityHashMap}s with {@link LocationIdentity} values as keys.
36 */
37public abstract class LocationIdentity {
38
39    private static final class AnyLocationIdentity extends LocationIdentity {
40        @Override
41        public boolean isImmutable() {
42            return false;
43        }
44
45        @Override
46        public String toString() {
47            return "ANY_LOCATION";
48        }
49    }
50
51    private static final class InitLocationIdentity extends LocationIdentity {
52        @Override
53        public boolean isImmutable() {
54            return true;
55        }
56
57        @Override
58        public String toString() {
59            return "INIT_LOCATION";
60        }
61    }
62
63    public static final LocationIdentity ANY_LOCATION = new AnyLocationIdentity();
64    public static final LocationIdentity INIT_LOCATION = new InitLocationIdentity();
65
66    /**
67     * Indicates that the given location is the union of all possible mutable locations. A write to
68     * such a location kill all reads from mutable locations and a read from this location is killed
69     * by any write (except for initialization writes).
70     */
71    public static LocationIdentity any() {
72        return ANY_LOCATION;
73    }
74
75    /**
76     * Location only allowed to be used for writes. Indicates that a completely new memory location
77     * is written. Kills no read. The previous value at the given location must be either
78     * uninitialized or null. Writes to this location do not need a GC pre-barrier.
79     */
80    public static LocationIdentity init() {
81        return INIT_LOCATION;
82    }
83
84    /**
85     * Denotes a location is unchanging in all cases. Not that this is different than the Java
86     * notion of final which only requires definite assignment.
87     */
88    public abstract boolean isImmutable();
89
90    public final boolean isMutable() {
91        return !isImmutable();
92    }
93
94    public final boolean isAny() {
95        return this == ANY_LOCATION;
96    }
97
98    public final boolean isInit() {
99        return this == INIT_LOCATION;
100    }
101
102    public final boolean isSingle() {
103        return this != ANY_LOCATION;
104    }
105
106    public final boolean overlaps(LocationIdentity other) {
107        return isAny() || other.isAny() || this.equals(other);
108    }
109}
110