comparison graal/com.oracle.jvmci.meta/src/com/oracle/jvmci/meta/NamedLocationIdentity.java @ 21556:48c1ebd24120

renamed com.oracle.graal.api[meta|code] modules to com.oracle.jvmci.[meta|code] (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Wed, 27 May 2015 00:36:16 +0200
parents graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/NamedLocationIdentity.java@f9024b74dd9e
children
comparison
equal deleted inserted replaced
21555:d12eaef9af72 21556:48c1ebd24120
1 /*
2 * Copyright (c) 2011, 2012, 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 */
23 package com.oracle.jvmci.meta;
24
25 import java.util.*;
26
27 import com.oracle.jvmci.meta.Kind.FormatWithToString;
28
29 /**
30 * A {@link LocationIdentity} with a name.
31 */
32 public final class NamedLocationIdentity extends LocationIdentity implements FormatWithToString {
33
34 /**
35 * Map for asserting all {@link NamedLocationIdentity} instances have a unique name.
36 */
37 static class DB {
38 private static final HashMap<String, NamedLocationIdentity> map = new HashMap<>();
39
40 static boolean checkUnique(NamedLocationIdentity identity) {
41 NamedLocationIdentity oldValue = map.put(identity.name, identity);
42 if (oldValue != null) {
43 throw new AssertionError("identity " + identity + " already exists");
44 }
45 return true;
46 }
47 }
48
49 private final String name;
50 private final boolean immutable;
51
52 private NamedLocationIdentity(String name, boolean immutable) {
53 this.name = name;
54 this.immutable = immutable;
55 }
56
57 /**
58 * Creates a named unique location identity for read and write operations against mutable
59 * memory.
60 *
61 * @param name the name of the new location identity
62 */
63 public static NamedLocationIdentity mutable(String name) {
64 return create(name, false);
65 }
66
67 /**
68 * Creates a named unique location identity for read operations against immutable memory.
69 * Immutable memory will never have a visible write in the graph, which is more restictive than
70 * Java final.
71 *
72 * @param name the name of the new location identity
73 */
74 public static NamedLocationIdentity immutable(String name) {
75 return create(name, true);
76 }
77
78 /**
79 * Creates a named unique location identity for read and write operations.
80 *
81 * @param name the name of the new location identity
82 * @param immutable true if the location is immutable
83 */
84 private static NamedLocationIdentity create(String name, boolean immutable) {
85 NamedLocationIdentity id = new NamedLocationIdentity(name, immutable);
86 assert DB.checkUnique(id);
87 return id;
88 }
89
90 @Override
91 public boolean isImmutable() {
92 return immutable;
93 }
94
95 @Override
96 public String toString() {
97 return name + (isImmutable() ? ":final" : "");
98 }
99
100 /**
101 * Returns the named location identity for an array of the given element kind. Array accesses of
102 * the same kind must have the same location identity unless an alias analysis guarantees that
103 * two distinct arrays are accessed.
104 */
105 public static LocationIdentity getArrayLocation(Kind elementKind) {
106 return ARRAY_LOCATIONS.get(elementKind);
107 }
108
109 private static final EnumMap<Kind, LocationIdentity> ARRAY_LOCATIONS = initArrayLocations();
110
111 private static EnumMap<Kind, LocationIdentity> initArrayLocations() {
112 EnumMap<Kind, LocationIdentity> result = new EnumMap<>(Kind.class);
113 for (Kind kind : Kind.values()) {
114 result.put(kind, NamedLocationIdentity.mutable("Array: " + kind.getJavaName()));
115 }
116 return result;
117 }
118 }