1/*
2 * Copyright (c) 2013, 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//JaCoCo Exclude
24package org.graalvm.compiler.nodes.java;
25
26import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
27
28import org.graalvm.compiler.core.common.type.Stamp;
29import org.graalvm.compiler.core.common.type.StampFactory;
30import org.graalvm.compiler.core.common.type.TypeReference;
31import org.graalvm.compiler.graph.Node;
32import org.graalvm.compiler.graph.NodeClass;
33import org.graalvm.compiler.graph.spi.Canonicalizable;
34import org.graalvm.compiler.graph.spi.CanonicalizerTool;
35import org.graalvm.compiler.nodeinfo.NodeInfo;
36import org.graalvm.compiler.nodes.FrameState;
37import org.graalvm.compiler.nodes.ValueNode;
38
39import jdk.vm.ci.meta.JavaKind;
40import jdk.vm.ci.meta.MetaAccessProvider;
41import jdk.vm.ci.meta.ResolvedJavaType;
42
43/**
44 * The {@code DynamicNewArrayNode} is used for allocation of arrays when the type is not a
45 * compile-time constant.
46 */
47@NodeInfo
48public class DynamicNewArrayNode extends AbstractNewArrayNode implements Canonicalizable {
49    public static final NodeClass<DynamicNewArrayNode> TYPE = NodeClass.create(DynamicNewArrayNode.class);
50
51    @Input ValueNode elementType;
52
53    /**
54     * Class pointer to void.class needs to be exposed earlier than this node is lowered so that it
55     * can be replaced by the AOT machinery. If it's not needed for lowering this input can be
56     * ignored.
57     */
58    @OptionalInput ValueNode voidClass;
59
60    /**
61     * A non-null value indicating the worst case element type. Mainly useful for distinguishing
62     * Object arrays from primitive arrays.
63     */
64    protected final JavaKind knownElementKind;
65
66    public DynamicNewArrayNode(ValueNode elementType, ValueNode length, boolean fillContents) {
67        this(TYPE, elementType, length, fillContents, null, null, null);
68    }
69
70    public DynamicNewArrayNode(@InjectedNodeParameter MetaAccessProvider metaAccess, ValueNode elementType, ValueNode length, boolean fillContents, JavaKind knownElementKind) {
71        this(TYPE, elementType, length, fillContents, knownElementKind, null, metaAccess);
72    }
73
74    private static Stamp computeStamp(JavaKind knownElementKind, MetaAccessProvider metaAccess) {
75        if (knownElementKind != null && metaAccess != null) {
76            ResolvedJavaType arrayType = metaAccess.lookupJavaType(knownElementKind == JavaKind.Object ? Object.class : knownElementKind.toJavaClass()).getArrayClass();
77            return StampFactory.objectNonNull(TypeReference.createWithoutAssumptions(arrayType));
78        }
79        return StampFactory.objectNonNull();
80    }
81
82    protected DynamicNewArrayNode(NodeClass<? extends DynamicNewArrayNode> c, ValueNode elementType, ValueNode length, boolean fillContents, JavaKind knownElementKind, FrameState stateBefore,
83                    MetaAccessProvider metaAccess) {
84        super(c, computeStamp(knownElementKind, metaAccess), length, fillContents, stateBefore);
85        this.elementType = elementType;
86        this.knownElementKind = knownElementKind;
87        assert knownElementKind != JavaKind.Void && knownElementKind != JavaKind.Illegal;
88    }
89
90    public ValueNode getElementType() {
91        return elementType;
92    }
93
94    public JavaKind getKnownElementKind() {
95        return knownElementKind;
96    }
97
98    @Override
99    public Node canonical(CanonicalizerTool tool) {
100        if (elementType.isConstant()) {
101            if (GeneratePIC.getValue(tool.getOptions())) {
102                // Can't fold for AOT, because the resulting NewArrayNode will be missing its
103                // ResolveConstantNode for the array class.
104                return this;
105            }
106            ResolvedJavaType type = tool.getConstantReflection().asJavaType(elementType.asConstant());
107            if (type != null && !throwsIllegalArgumentException(type)) {
108                return createNewArrayNode(type);
109            }
110        }
111        return this;
112    }
113
114    /** Hook for subclasses to instantiate a subclass of {@link NewArrayNode}. */
115    protected NewArrayNode createNewArrayNode(ResolvedJavaType type) {
116        return new NewArrayNode(type, length(), fillContents(), stateBefore());
117    }
118
119    public static boolean throwsIllegalArgumentException(Class<?> elementType, Class<?> voidClass) {
120        return elementType == voidClass;
121    }
122
123    public static boolean throwsIllegalArgumentException(ResolvedJavaType elementType) {
124        return elementType.getJavaKind() == JavaKind.Void;
125    }
126
127    @NodeIntrinsic
128    private static native Object newArray(Class<?> componentType, int length, @ConstantNodeParameter boolean fillContents, @ConstantNodeParameter JavaKind knownElementKind);
129
130    public static Object newArray(Class<?> componentType, int length, JavaKind knownElementKind) {
131        return newArray(componentType, length, true, knownElementKind);
132    }
133
134    public static Object newUninitializedArray(Class<?> componentType, int length, JavaKind knownElementKind) {
135        return newArray(componentType, length, false, knownElementKind);
136    }
137
138    public ValueNode getVoidClass() {
139        return voidClass;
140    }
141
142    public void setVoidClass(ValueNode newVoidClass) {
143        updateUsages(voidClass, newVoidClass);
144        voidClass = newVoidClass;
145    }
146}
147