1/*
2 * Copyright (c) 2012, 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.hotspot.stubs;
24
25import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG;
26import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.arrayPrototypeMarkWord;
27import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.getAndClearObjectResult;
28import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.layoutHelperElementTypeMask;
29import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.layoutHelperElementTypeShift;
30import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.layoutHelperHeaderSizeMask;
31import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.layoutHelperHeaderSizeShift;
32import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.layoutHelperLog2ElementSizeMask;
33import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.layoutHelperLog2ElementSizeShift;
34import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.loadKlassLayoutHelperIntrinsic;
35import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.registerAsWord;
36import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.wordSize;
37import static org.graalvm.compiler.hotspot.replacements.NewObjectSnippets.MAX_ARRAY_FAST_PATH_ALLOCATION_LENGTH;
38import static org.graalvm.compiler.hotspot.replacements.NewObjectSnippets.formatArray;
39import static org.graalvm.compiler.hotspot.stubs.NewInstanceStub.refillAllocate;
40import static org.graalvm.compiler.hotspot.stubs.StubUtil.handlePendingException;
41import static org.graalvm.compiler.hotspot.stubs.StubUtil.newDescriptor;
42import static org.graalvm.compiler.hotspot.stubs.StubUtil.printf;
43import static org.graalvm.compiler.hotspot.stubs.StubUtil.verifyObject;
44import static jdk.vm.ci.hotspot.HotSpotMetaAccessProvider.computeArrayAllocationSize;
45
46import org.graalvm.compiler.api.replacements.Fold;
47import org.graalvm.compiler.api.replacements.Snippet;
48import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
49import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
50import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
51import org.graalvm.compiler.graph.Node.NodeIntrinsic;
52import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
53import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
54import org.graalvm.compiler.hotspot.nodes.GraalHotSpotVMConfigNode;
55import org.graalvm.compiler.hotspot.nodes.StubForeignCallNode;
56import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp;
57import org.graalvm.compiler.hotspot.replacements.NewObjectSnippets;
58import org.graalvm.compiler.hotspot.word.KlassPointer;
59import org.graalvm.compiler.nodes.ConstantNode;
60import org.graalvm.compiler.word.Word;
61
62import jdk.vm.ci.code.Register;
63import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
64
65/**
66 * Stub implementing the fast path for TLAB refill during instance class allocation. This stub is
67 * called from the {@linkplain NewObjectSnippets inline} allocation code when TLAB allocation fails.
68 * If this stub fails to refill the TLAB or allocate the object, it calls out to the HotSpot C++
69 * runtime to complete the allocation.
70 */
71public class NewArrayStub extends SnippetStub {
72
73    public NewArrayStub(HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
74        super("newArray", providers, linkage);
75    }
76
77    @Override
78    protected Object[] makeConstArgs() {
79        HotSpotResolvedObjectType intArrayType = (HotSpotResolvedObjectType) providers.getMetaAccess().lookupJavaType(int[].class);
80        int count = method.getSignature().getParameterCount(false);
81        Object[] args = new Object[count];
82        assert checkConstArg(3, "intArrayHub");
83        assert checkConstArg(4, "threadRegister");
84        args[3] = ConstantNode.forConstant(KlassPointerStamp.klassNonNull(), intArrayType.klass(), null);
85        args[4] = providers.getRegisters().getThreadRegister();
86        return args;
87    }
88
89    @Fold
90    static boolean logging() {
91        return StubOptions.TraceNewArrayStub.getValue();
92    }
93
94    /**
95     * Re-attempts allocation after an initial TLAB allocation failed or was skipped (e.g., due to
96     * -XX:-UseTLAB).
97     *
98     * @param hub the hub of the object to be allocated
99     * @param length the length of the array
100     * @param fillContents Should the array be filled with zeroes?
101     * @param intArrayHub the hub for {@code int[].class}
102     */
103    @Snippet
104    private static Object newArray(KlassPointer hub, int length, boolean fillContents, @ConstantParameter KlassPointer intArrayHub, @ConstantParameter Register threadRegister) {
105        int layoutHelper = loadKlassLayoutHelperIntrinsic(hub);
106        int log2ElementSize = (layoutHelper >> layoutHelperLog2ElementSizeShift(INJECTED_VMCONFIG)) & layoutHelperLog2ElementSizeMask(INJECTED_VMCONFIG);
107        int headerSize = (layoutHelper >> layoutHelperHeaderSizeShift(INJECTED_VMCONFIG)) & layoutHelperHeaderSizeMask(INJECTED_VMCONFIG);
108        int elementKind = (layoutHelper >> layoutHelperElementTypeShift(INJECTED_VMCONFIG)) & layoutHelperElementTypeMask(INJECTED_VMCONFIG);
109        int sizeInBytes = computeArrayAllocationSize(length, wordSize(), headerSize, log2ElementSize);
110        if (logging()) {
111            printf("newArray: element kind %d\n", elementKind);
112            printf("newArray: array length %d\n", length);
113            printf("newArray: array size %d\n", sizeInBytes);
114            printf("newArray: hub=%p\n", hub.asWord().rawValue());
115        }
116
117        // check that array length is small enough for fast path.
118        Word thread = registerAsWord(threadRegister);
119        boolean inlineContiguousAllocationSupported = GraalHotSpotVMConfigNode.inlineContiguousAllocationSupported();
120        if (inlineContiguousAllocationSupported && length >= 0 && length <= MAX_ARRAY_FAST_PATH_ALLOCATION_LENGTH) {
121            Word memory = refillAllocate(thread, intArrayHub, sizeInBytes, logging());
122            if (memory.notEqual(0)) {
123                if (logging()) {
124                    printf("newArray: allocated new array at %p\n", memory.rawValue());
125                }
126                return verifyObject(
127                                formatArray(hub, sizeInBytes, length, headerSize, memory, Word.unsigned(arrayPrototypeMarkWord(INJECTED_VMCONFIG)), fillContents, false, false));
128            }
129        }
130        if (logging()) {
131            printf("newArray: calling new_array_c\n");
132        }
133
134        newArrayC(NEW_ARRAY_C, thread, hub, length);
135        handlePendingException(thread, true);
136        return verifyObject(getAndClearObjectResult(thread));
137    }
138
139    public static final ForeignCallDescriptor NEW_ARRAY_C = newDescriptor(NewArrayStub.class, "newArrayC", void.class, Word.class, KlassPointer.class, int.class);
140
141    @NodeIntrinsic(StubForeignCallNode.class)
142    public static native void newArrayC(@ConstantNodeParameter ForeignCallDescriptor newArrayC, Word thread, KlassPointer hub, int length);
143}
144