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