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 package org.graalvm.compiler.nodes;
  24 
  25 import java.util.EnumMap;
  26 
  27 import org.graalvm.util.Equivalence;
  28 import org.graalvm.word.LocationIdentity;
  29 import org.graalvm.util.EconomicSet;
  30 
  31 import jdk.vm.ci.meta.JavaKind;
  32 import jdk.vm.ci.meta.JavaKind.FormatWithToString;
  33 
  34 /**
  35  * A {@link LocationIdentity} with a name.
  36  */
  37 public 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     /**
  64      * Denotes an off-heap address.
  65      */
  66     public static final LocationIdentity OFF_HEAP_LOCATION = NamedLocationIdentity.mutable("OFF_HEAP_LOCATION");
  67 
  68     private final String name;
  69     private final boolean immutable;
  70 
  71     protected NamedLocationIdentity(String name, boolean immutable) {
  72         this.name = name;
  73         this.immutable = immutable;
  74         assert DB.checkUnique(name);
  75     }
  76 
  77     /**
  78      * Creates a named unique location identity for read and write operations against mutable
  79      * memory.
  80      *
  81      * @param name the name of the new location identity
  82      */
  83     public static NamedLocationIdentity mutable(String name) {
  84         return create(name, false);
  85     }
  86 
  87     /**
  88      * Creates a named unique location identity for read operations against immutable memory.
  89      * Immutable memory will never have a visible write in the graph, which is more restrictive than
  90      * Java final.
  91      *
  92      * @param name the name of the new location identity
  93      */
  94     public static NamedLocationIdentity immutable(String name) {
  95         return create(name, true);
  96     }
  97 
  98     /**
  99      * Creates a named unique location identity for read and write operations.
 100      *
 101      * @param name the name of the new location identity
 102      * @param immutable true if the location is immutable
 103      */
 104     private static NamedLocationIdentity create(String name, boolean immutable) {
 105         return new NamedLocationIdentity(name, immutable);
 106     }
 107 
 108     @Override
 109     public boolean isImmutable() {
 110         return immutable;
 111     }
 112 
 113     @Override
 114     public String toString() {
 115         return name + (isImmutable() ? ":final" : "");
 116     }
 117 
 118     /**
 119      * Returns the named location identity for an array of the given element kind. Array accesses of
 120      * the same kind must have the same location identity unless an alias analysis guarantees that
 121      * two distinct arrays are accessed.
 122      */
 123     public static LocationIdentity getArrayLocation(JavaKind elementKind) {
 124         return ARRAY_LOCATIONS.get(elementKind);
 125     }
 126 
 127     private static final EnumMap<JavaKind, LocationIdentity> ARRAY_LOCATIONS = initArrayLocations();
 128 
 129     private static EnumMap<JavaKind, LocationIdentity> initArrayLocations() {
 130         EnumMap<JavaKind, LocationIdentity> result = new EnumMap<>(JavaKind.class);
 131         for (JavaKind kind : JavaKind.values()) {
 132             result.put(kind, NamedLocationIdentity.mutable("Array: " + kind.getJavaName()));
 133         }
 134         return result;
 135     }
 136 }