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.type.StampFactory;
  31 import org.graalvm.compiler.core.common.type.TypeReference;
  32 import org.graalvm.compiler.graph.IterableNodeType;
  33 import org.graalvm.compiler.graph.Node;
  34 import org.graalvm.compiler.graph.NodeClass;
  35 import org.graalvm.compiler.nodeinfo.NodeInfo;
  36 import org.graalvm.compiler.nodes.FixedNode;
  37 import org.graalvm.compiler.nodes.ValueNode;
  38 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  39 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  40 
  41 import jdk.vm.ci.meta.JavaKind;
  42 import jdk.vm.ci.meta.MetaAccessProvider;
  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      */
 100     public abstract int entryIndexForOffset(MetaAccessProvider metaAccess, long constantOffset, JavaKind expectedEntryKind);
 101 
 102     /**
 103      * Returns the {@link JavaKind} of the entry at the given index.
 104      */
 105     public abstract JavaKind entryKind(int index);
 106 
 107     /**
 108      * Returns an exact duplicate of this virtual object node, which has not been added to the graph
 109      * yet.
 110      */
 111     public abstract VirtualObjectNode duplicate();
 112 
 113     /**
 114      * Specifies whether this virtual object has an object identity. If not, then the result of a
 115      * comparison of two virtual objects is determined by comparing their contents.
 116      */
 117     public boolean hasIdentity() {
 118         return hasIdentity;
 119     }
 120 
 121     public void setIdentity(boolean identity) {
 122         this.hasIdentity = identity;
 123     }
 124 
 125     /**
 126      * Returns a node that can be used to materialize this virtual object. If this returns an
 127      * {@link AllocatedObjectNode} then this node will be attached to a {@link CommitAllocationNode}
 128      * , otherwise the node will just be added to the graph.
 129      */
 130     public abstract ValueNode getMaterializedRepresentation(FixedNode fixed, ValueNode[] entries, LockState locks);
 131 
 132     @Override
 133     public void generate(NodeLIRBuilderTool gen) {
 134         // nothing to do...
 135     }
 136 }