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 java.nio.ByteOrder; 28 29 import org.graalvm.compiler.graph.NodeClass; 30 import org.graalvm.compiler.nodeinfo.NodeInfo; 31 import org.graalvm.compiler.nodeinfo.Verbosity; 32 import org.graalvm.compiler.nodes.ConstantNode; 33 import org.graalvm.compiler.nodes.FixedNode; 34 import org.graalvm.compiler.nodes.ValueNode; 35 import org.graalvm.compiler.nodes.spi.ArrayLengthProvider; 36 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 37 38 import jdk.vm.ci.meta.ConstantReflectionProvider; 39 import jdk.vm.ci.meta.JavaKind; 40 import jdk.vm.ci.meta.MetaAccessProvider; 41 import jdk.vm.ci.meta.ResolvedJavaType; 42 43 @NodeInfo(nameTemplate = "VirtualArray({p#objectId}) {p#componentType/s}[{p#length}]") 44 public class VirtualArrayNode extends VirtualObjectNode implements ArrayLengthProvider { 45 46 public static final NodeClass<VirtualArrayNode> TYPE = NodeClass.create(VirtualArrayNode.class); 47 protected final ResolvedJavaType componentType; 48 protected final int length; 49 50 public VirtualArrayNode(ResolvedJavaType componentType, int length) { 51 this(TYPE, componentType, length); 52 } 53 54 protected VirtualArrayNode(NodeClass<? extends VirtualObjectNode> c, ResolvedJavaType componentType, int length) { 55 super(c, componentType.getArrayClass(), true); 56 this.componentType = componentType; 57 this.length = length; 58 } 59 60 @Override 61 public ResolvedJavaType type() { 62 return componentType.getArrayClass(); 63 } 64 65 public ResolvedJavaType componentType() { 66 return componentType; 67 } 68 69 @Override 70 public int entryCount() { 71 return length; 72 } 73 74 @Override 75 public void generate(NodeLIRBuilderTool gen) { 76 // nothing to do... 77 } 78 79 @Override 80 public String toString(Verbosity verbosity) { 81 if (verbosity == Verbosity.Name) { 82 return super.toString(Verbosity.Name) + "(" + getObjectId() + ") " + componentType.getName() + "[" + length + "]"; 83 } else { 84 return super.toString(verbosity); 85 } 86 } 87 88 @Override 89 public String entryName(int index) { 90 return "[" + index + "]"; 91 } 92 93 @Override 94 public int entryIndexForOffset(MetaAccessProvider metaAccess, long constantOffset, JavaKind expectedEntryKind) { 95 return entryIndexForOffset(metaAccess, constantOffset, expectedEntryKind, componentType, length); 96 } 97 98 public static int entryIndexForOffset(MetaAccessProvider metaAccess, long constantOffset, JavaKind expectedEntryKind, ResolvedJavaType componentType, int length) { 99 int baseOffset = metaAccess.getArrayBaseOffset(componentType.getJavaKind()); 100 int indexScale = metaAccess.getArrayIndexScale(componentType.getJavaKind()); 101 102 long offset; 103 if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN && componentType.isPrimitive()) { 104 // On big endian, we expect the value to be correctly aligned in memory 105 int componentByteCount = componentType.getJavaKind().getByteCount(); 106 offset = constantOffset - (componentByteCount - Math.min(componentByteCount, 4 + expectedEntryKind.getByteCount())); 107 } else { 108 offset = constantOffset; 109 } 110 long index = offset - baseOffset; 111 if (index % indexScale != 0) { 112 return -1; 113 } 114 long elementIndex = index / indexScale; 115 if (elementIndex < 0 || elementIndex >= length) { 116 return -1; 117 } 118 return (int) elementIndex; 119 } 120 121 @Override 122 public JavaKind entryKind(int index) { 123 assert index >= 0 && index < length; 124 return componentType.getJavaKind(); 125 } 126 127 @Override 128 public VirtualArrayNode duplicate() { 129 VirtualArrayNode node = new VirtualArrayNode(componentType, length); 130 node.setNodeSourcePosition(this.getNodeSourcePosition()); 131 return node; 132 } 133 134 @Override 135 public ValueNode getMaterializedRepresentation(FixedNode fixed, ValueNode[] entries, LockState locks) { 136 AllocatedObjectNode node = new AllocatedObjectNode(this); 137 node.setNodeSourcePosition(this.getNodeSourcePosition()); 138 return node; 139 } 140 141 @Override 142 public ValueNode findLength(FindLengthMode mode, ConstantReflectionProvider constantReflection) { 143 return ConstantNode.forInt(length); 144 } 145 }