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.
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 */
23package org.graalvm.compiler.nodes;
24
25import java.util.EnumMap;
26
27import org.graalvm.util.Equivalence;
28import org.graalvm.word.LocationIdentity;
29import org.graalvm.util.EconomicSet;
30
31import jdk.vm.ci.meta.JavaKind;
32import jdk.vm.ci.meta.JavaKind.FormatWithToString;
33
34/**
35 * A {@link LocationIdentity} with a name.
36 */
37public class NamedLocationIdentity extends LocationIdentity implements FormatWithToString {
38
39    /**
40     * Map for asserting all {@link NamedLocationIdentity} instances have a unique name.
41     */
42    static class DB {
43        private static final EconomicSet<String> map = EconomicSet.create(Equivalence.DEFAULT);
44
45        static boolean checkUnique(String name) {
46            if (!map.add(name)) {
47                throw new AssertionError("identity " + name + " already exists");
48            }
49            return true;
50        }
51    }
52
53    /**
54     * Denotes the location of a value that is guaranteed to be unchanging.
55     */
56    public static final LocationIdentity FINAL_LOCATION = NamedLocationIdentity.immutable("FINAL_LOCATION");
57
58    /**
59     * Denotes the location of the length field of a Java array.
60     */
61    public static final LocationIdentity ARRAY_LENGTH_LOCATION = NamedLocationIdentity.immutable("[].length");
62
63    private final String name;
64    private final boolean immutable;
65
66    protected NamedLocationIdentity(String name, boolean immutable) {
67        this.name = name;
68        this.immutable = immutable;
69        assert DB.checkUnique(name);
70    }
71
72    /**
73     * Creates a named unique location identity for read and write operations against mutable
74     * memory.
75     *
76     * @param name the name of the new location identity
77     */
78    public static NamedLocationIdentity mutable(String name) {
79        return create(name, false);
80    }
81
82    /**
83     * Creates a named unique location identity for read operations against immutable memory.
84     * Immutable memory will never have a visible write in the graph, which is more restictive than
85     * Java final.
86     *
87     * @param name the name of the new location identity
88     */
89    public static NamedLocationIdentity immutable(String name) {
90        return create(name, true);
91    }
92
93    /**
94     * Creates a named unique location identity for read and write operations.
95     *
96     * @param name the name of the new location identity
97     * @param immutable true if the location is immutable
98     */
99    private static NamedLocationIdentity create(String name, boolean immutable) {
100        return new NamedLocationIdentity(name, immutable);
101    }
102
103    @Override
104    public boolean isImmutable() {
105        return immutable;
106    }
107
108    @Override
109    public String toString() {
110        return name + (isImmutable() ? ":final" : "");
111    }
112
113    /**
114     * Returns the named location identity for an array of the given element kind. Array accesses of
115     * the same kind must have the same location identity unless an alias analysis guarantees that
116     * two distinct arrays are accessed.
117     */
118    public static LocationIdentity getArrayLocation(JavaKind elementKind) {
119        return ARRAY_LOCATIONS.get(elementKind);
120    }
121
122    private static final EnumMap<JavaKind, LocationIdentity> ARRAY_LOCATIONS = initArrayLocations();
123
124    private static EnumMap<JavaKind, LocationIdentity> initArrayLocations() {
125        EnumMap<JavaKind, LocationIdentity> result = new EnumMap<>(JavaKind.class);
126        for (JavaKind kind : JavaKind.values()) {
127            result.put(kind, NamedLocationIdentity.mutable("Array: " + kind.getJavaName()));
128        }
129        return result;
130    }
131}
132