1 /*
   2  * Copyright (c) 2011, 2015, 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.virtual;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
  29 
  30 import org.graalvm.compiler.core.common.spi.ArrayOffsetProvider;
  31 import org.graalvm.compiler.core.common.type.StampFactory;
  32 import org.graalvm.compiler.core.common.type.TypeReference;
  33 import org.graalvm.compiler.graph.IterableNodeType;
  34 import org.graalvm.compiler.graph.Node;
  35 import org.graalvm.compiler.graph.NodeClass;
  36 import org.graalvm.compiler.nodeinfo.NodeInfo;
  37 import org.graalvm.compiler.nodes.FixedNode;
  38 import org.graalvm.compiler.nodes.ValueNode;
  39 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  40 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  41 
  42 import jdk.vm.ci.meta.JavaKind;
  43 import jdk.vm.ci.meta.ResolvedJavaType;
  44 
  45 @NodeInfo(cycles = CYCLES_0, size = SIZE_0)
  46 public abstract class VirtualObjectNode extends ValueNode implements LIRLowerable, IterableNodeType {
  47 
  48     public static final NodeClass<VirtualObjectNode> TYPE = NodeClass.create(VirtualObjectNode.class);
  49     protected boolean hasIdentity;
  50     private int objectId = -1;
  51 
  52     protected VirtualObjectNode(NodeClass<? extends VirtualObjectNode> c, ResolvedJavaType type, boolean hasIdentity) {
  53         super(c, StampFactory.objectNonNull(TypeReference.createExactTrusted(type)));
  54         this.hasIdentity = hasIdentity;
  55     }
  56 
  57     public final int getObjectId() {
  58         return objectId;
  59     }
  60 
  61     public final void resetObjectId() {
  62         this.objectId = -1;
  63     }
  64 
  65     public final void setObjectId(int objectId) {
  66         assert objectId != -1;
  67         this.objectId = objectId;
  68     }
  69 
  70     @Override
  71     protected void afterClone(Node other) {
  72         super.afterClone(other);
  73         resetObjectId();
  74     }
  75 
  76     /**
  77      * The type of object described by this {@link VirtualObjectNode}. In case of arrays, this is
  78      * the array type (and not the component type).
  79      */
  80     public abstract ResolvedJavaType type();
  81 
  82     /**
  83      * The number of entries this virtual object has. Either the number of fields or the number of
  84      * array elements.
  85      */
  86     public abstract int entryCount();
  87 
  88     /**
  89      * Returns the name of the entry at the given index. Only used for debugging purposes.
  90      */
  91     public abstract String entryName(int i);
  92 
  93     /**
  94      * If the given index denotes an entry in this virtual object, the index of this entry is
  95      * returned. If no such entry can be found, this method returns -1.
  96      *
  97      * @param constantOffset offset, where the value is placed.
  98      * @param expectedEntryKind Specifies which type is expected at this offset (Is important when
  99      *            doing implicit casts, especially on big endian systems.
 100      */
 101     public abstract int entryIndexForOffset(ArrayOffsetProvider arrayOffsetProvider, long constantOffset, JavaKind expectedEntryKind);
 102 
 103     /**
 104      * Returns the {@link JavaKind} of the entry at the given index.
 105      */
 106     public abstract JavaKind entryKind(int index);
 107 
 108     /**
 109      * Returns an exact duplicate of this virtual object node, which has not been added to the graph
 110      * yet.
 111      */
 112     public abstract VirtualObjectNode duplicate();
 113 
 114     /**
 115      * Specifies whether this virtual object has an object identity. If not, then the result of a
 116      * comparison of two virtual objects is determined by comparing their contents.
 117      */
 118     public boolean hasIdentity() {
 119         return hasIdentity;
 120     }
 121 
 122     public void setIdentity(boolean identity) {
 123         this.hasIdentity = identity;
 124     }
 125 
 126     /**
 127      * Returns a node that can be used to materialize this virtual object. If this returns an
 128      * {@link AllocatedObjectNode} then this node will be attached to a {@link CommitAllocationNode}
 129      * , otherwise the node will just be added to the graph.
 130      */
 131     public abstract ValueNode getMaterializedRepresentation(FixedNode fixed, ValueNode[] entries, LockState locks);
 132 
 133     @Override
 134     public void generate(NodeLIRBuilderTool gen) {
 135         // nothing to do...
 136     }
 137 }