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 package org.graalvm.compiler.nodes.virtual; 24 25 import org.graalvm.compiler.graph.NodeClass; 26 import org.graalvm.compiler.nodeinfo.NodeInfo; 27 import org.graalvm.compiler.nodeinfo.Verbosity; 28 import org.graalvm.compiler.nodes.FixedNode; 29 import org.graalvm.compiler.nodes.ValueNode; 30 31 import jdk.vm.ci.meta.JavaKind; 32 import jdk.vm.ci.meta.ResolvedJavaField; 33 import jdk.vm.ci.meta.ResolvedJavaType; 34 35 @NodeInfo(nameTemplate = "VirtualInstance {p#type/s}") 36 public class VirtualInstanceNode extends VirtualObjectNode { 37 38 public static final NodeClass<VirtualInstanceNode> TYPE = NodeClass.create(VirtualInstanceNode.class); 39 protected final ResolvedJavaType type; 40 protected final ResolvedJavaField[] fields; 41 42 public VirtualInstanceNode(ResolvedJavaType type, boolean hasIdentity) { 43 this(type, type.getInstanceFields(true), hasIdentity); 44 } 45 46 public VirtualInstanceNode(ResolvedJavaType type, ResolvedJavaField[] fields, boolean hasIdentity) { 47 this(TYPE, type, fields, hasIdentity); 48 } 49 50 protected VirtualInstanceNode(NodeClass<? extends VirtualInstanceNode> c, ResolvedJavaType type, boolean hasIdentity) { 51 this(c, type, type.getInstanceFields(true), hasIdentity); 52 } 53 54 protected VirtualInstanceNode(NodeClass<? extends VirtualInstanceNode> c, ResolvedJavaType type, ResolvedJavaField[] fields, boolean hasIdentity) { 55 super(c, type, hasIdentity); 56 this.type = type; 57 this.fields = fields; 58 } 59 60 @Override 61 public ResolvedJavaType type() { 62 return type; 63 } 64 65 @Override 66 public int entryCount() { 67 return fields.length; 68 } 69 70 public ResolvedJavaField field(int index) { 71 return fields[index]; 72 } 73 74 public ResolvedJavaField[] getFields() { 75 return fields; 76 } 77 78 @Override 79 public String toString(Verbosity verbosity) { 80 if (verbosity == Verbosity.Name) { 81 return super.toString(Verbosity.Name) + " " + type.toJavaName(false); 82 } else { 83 return super.toString(verbosity); 84 } 85 } 86 87 @Override 88 public String entryName(int index) { 89 return fields[index].getName(); 90 } 91 92 public int fieldIndex(ResolvedJavaField field) { 93 // on average fields.length == ~6, so a linear search is fast enough 94 for (int i = 0; i < fields.length; i++) { 95 if (fields[i].equals(field)) { 96 return i; 97 } 98 } 99 return -1; 100 } 101 102 @Override 103 public int entryIndexForOffset(long constantOffset, JavaKind expectedEntryKind) { 104 return fieldIndex(type.findInstanceFieldWithOffset(constantOffset, expectedEntryKind)); 105 } 106 107 @Override 108 public JavaKind entryKind(int index) { 109 assert index >= 0 && index < fields.length; 110 return fields[index].getJavaKind(); 111 } 112 113 @Override 114 public VirtualInstanceNode duplicate() { 115 return new VirtualInstanceNode(type, fields, super.hasIdentity()); 116 } 117 118 @Override 119 public ValueNode getMaterializedRepresentation(FixedNode fixed, ValueNode[] entries, LockState locks) { 120 return new AllocatedObjectNode(this); 121 } 122 }