1 /* 2 * Copyright (c) 2009, 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.java; 24 25 import java.util.Collections; 26 27 import org.graalvm.compiler.core.common.type.StampFactory; 28 import org.graalvm.compiler.core.common.type.TypeReference; 29 import org.graalvm.compiler.graph.NodeClass; 30 import org.graalvm.compiler.nodeinfo.NodeInfo; 31 import org.graalvm.compiler.nodes.ConstantNode; 32 import org.graalvm.compiler.nodes.FrameState; 33 import org.graalvm.compiler.nodes.ValueNode; 34 import org.graalvm.compiler.nodes.spi.VirtualizableAllocation; 35 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 36 import org.graalvm.compiler.nodes.virtual.VirtualArrayNode; 37 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 38 39 import jdk.vm.ci.meta.ResolvedJavaType; 40 41 /** 42 * The {@code NewArrayNode} is used for all array allocations where the element type is know at 43 * compile time. 44 */ 45 // JaCoCo Exclude 46 @NodeInfo 47 public class NewArrayNode extends AbstractNewArrayNode implements VirtualizableAllocation { 48 49 public static final NodeClass<NewArrayNode> TYPE = NodeClass.create(NewArrayNode.class); 50 private final ResolvedJavaType elementType; 51 52 public NewArrayNode(ResolvedJavaType elementType, ValueNode length, boolean fillContents) { 53 this(elementType, length, fillContents, null); 54 } 55 56 public NewArrayNode(ResolvedJavaType elementType, ValueNode length, boolean fillContents, FrameState stateBefore) { 57 this(TYPE, elementType, length, fillContents, stateBefore); 58 } 59 60 protected NewArrayNode(NodeClass<? extends NewArrayNode> c, ResolvedJavaType elementType, ValueNode length, boolean fillContents, FrameState stateBefore) { 61 super(c, StampFactory.objectNonNull(TypeReference.createExactTrusted(elementType.getArrayClass())), length, fillContents, stateBefore); 62 this.elementType = elementType; 63 } 64 65 @NodeIntrinsic 66 private static native Object newArray(@ConstantNodeParameter Class<?> elementType, int length, @ConstantNodeParameter boolean fillContents); 67 68 public static Object newUninitializedArray(Class<?> elementType, int length) { 69 return newArray(elementType, length, false); 70 } 71 72 /** 73 * Gets the element type of the array. 74 * 75 * @return the element type of the array 76 */ 77 public ResolvedJavaType elementType() { 78 return elementType; 79 } 80 81 @Override 82 public void virtualize(VirtualizerTool tool) { 83 ValueNode lengthAlias = tool.getAlias(length()); 84 if (lengthAlias.asConstant() != null) { 85 int constantLength = lengthAlias.asJavaConstant().asInt(); 86 if (constantLength >= 0 && constantLength < tool.getMaximumEntryCount()) { 87 ValueNode[] state = new ValueNode[constantLength]; 88 ConstantNode defaultForKind = constantLength == 0 ? null : defaultElementValue(); 89 for (int i = 0; i < constantLength; i++) { 90 state[i] = defaultForKind; 91 } 92 VirtualObjectNode virtualObject = createVirtualArrayNode(constantLength); 93 tool.createVirtualObject(virtualObject, state, Collections.<MonitorIdNode> emptyList(), false); 94 tool.replaceWithVirtual(virtualObject); 95 } 96 } 97 } 98 99 protected VirtualArrayNode createVirtualArrayNode(int constantLength) { 100 return new VirtualArrayNode(elementType(), constantLength); 101 } 102 103 /* Factored out in a separate method so that subclasses can override it. */ 104 protected ConstantNode defaultElementValue() { 105 return ConstantNode.defaultForKind(elementType().getJavaKind(), graph()); 106 } 107 }