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