001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.meta;
024
025import static com.oracle.graal.hotspot.HotSpotForeignCallLinkage.RegisterEffect.*;
026import static com.oracle.graal.hotspot.HotSpotForeignCallLinkage.Transition.*;
027import static jdk.internal.jvmci.code.CallingConvention.Type.*;
028
029import java.util.*;
030
031import jdk.internal.jvmci.code.*;
032import jdk.internal.jvmci.meta.*;
033
034import com.oracle.graal.compiler.common.spi.*;
035import com.oracle.graal.hotspot.*;
036import com.oracle.graal.hotspot.HotSpotForeignCallLinkage.RegisterEffect;
037import com.oracle.graal.hotspot.HotSpotForeignCallLinkage.Transition;
038import com.oracle.graal.hotspot.stubs.*;
039import com.oracle.graal.word.*;
040
041/**
042 * HotSpot implementation of {@link HotSpotForeignCallsProvider}.
043 */
044public abstract class HotSpotForeignCallsProviderImpl implements HotSpotForeignCallsProvider {
045
046    public static final ForeignCallDescriptor OSR_MIGRATION_END = new ForeignCallDescriptor("OSR_migration_end", void.class, long.class);
047    public static final ForeignCallDescriptor IDENTITY_HASHCODE = new ForeignCallDescriptor("identity_hashcode", int.class, Object.class);
048    public static final ForeignCallDescriptor VERIFY_OOP = new ForeignCallDescriptor("verify_oop", Object.class, Object.class);
049    public static final ForeignCallDescriptor LOAD_AND_CLEAR_EXCEPTION = new ForeignCallDescriptor("load_and_clear_exception", Object.class, Word.class);
050
051    public static final ForeignCallDescriptor TEST_DEOPTIMIZE_CALL_INT = new ForeignCallDescriptor("test_deoptimize_call_int", int.class, int.class);
052
053    protected final HotSpotGraalRuntimeProvider runtime;
054
055    protected final Map<ForeignCallDescriptor, HotSpotForeignCallLinkage> foreignCalls = new HashMap<>();
056    protected final MetaAccessProvider metaAccess;
057    protected final CodeCacheProvider codeCache;
058
059    public HotSpotForeignCallsProviderImpl(HotSpotGraalRuntimeProvider runtime, MetaAccessProvider metaAccess, CodeCacheProvider codeCache) {
060        this.runtime = runtime;
061        this.metaAccess = metaAccess;
062        this.codeCache = codeCache;
063    }
064
065    /**
066     * Registers the linkage for a foreign call.
067     */
068    public HotSpotForeignCallLinkage register(HotSpotForeignCallLinkage linkage) {
069        assert !foreignCalls.containsKey(linkage.getDescriptor()) : "already registered linkage for " + linkage.getDescriptor();
070        foreignCalls.put(linkage.getDescriptor(), linkage);
071        return linkage;
072    }
073
074    /**
075     * Creates and registers the details for linking a foreign call to a {@link Stub}.
076     *
077     * @param descriptor the signature of the call to the stub
078     * @param reexecutable specifies if the stub call can be re-executed without (meaningful) side
079     *            effects. Deoptimization will not return to a point before a stub call that cannot
080     *            be re-executed.
081     * @param transition specifies if this is a {@linkplain Transition#LEAF leaf} call
082     * @param killedLocations the memory locations killed by the stub call
083     */
084    public HotSpotForeignCallLinkage registerStubCall(ForeignCallDescriptor descriptor, boolean reexecutable, Transition transition, LocationIdentity... killedLocations) {
085        return register(HotSpotForeignCallLinkageImpl.create(metaAccess, codeCache, this, descriptor, 0L, PRESERVES_REGISTERS, JavaCall, JavaCallee, transition, reexecutable, killedLocations));
086    }
087
088    /**
089     * Creates and registers the linkage for a foreign call.
090     *
091     * @param descriptor the signature of the foreign call
092     * @param address the address of the code to call
093     * @param outgoingCcType outgoing (caller) calling convention type
094     * @param effect specifies if the call destroys or preserves all registers (apart from
095     *            temporaries which are always destroyed)
096     * @param transition specifies if this is a {@linkplain Transition#LEAF leaf} call
097     * @param reexecutable specifies if the foreign call can be re-executed without (meaningful)
098     *            side effects. Deoptimization will not return to a point before a foreign call that
099     *            cannot be re-executed.
100     * @param killedLocations the memory locations killed by the foreign call
101     */
102    public HotSpotForeignCallLinkage registerForeignCall(ForeignCallDescriptor descriptor, long address, CallingConvention.Type outgoingCcType, RegisterEffect effect, Transition transition,
103                    boolean reexecutable, LocationIdentity... killedLocations) {
104        Class<?> resultType = descriptor.getResultType();
105        assert address != 0;
106        assert transition != NOT_LEAF || resultType.isPrimitive() || Word.class.isAssignableFrom(resultType) : "non-leaf foreign calls must return objects in thread local storage: " + descriptor;
107        return register(HotSpotForeignCallLinkageImpl.create(metaAccess, codeCache, this, descriptor, address, effect, outgoingCcType, null, transition, reexecutable, killedLocations));
108    }
109
110    /**
111     * Creates a {@linkplain ForeignCallStub stub} for a foreign call.
112     *
113     * @param descriptor the signature of the call to the stub
114     * @param address the address of the foreign code to call
115     * @param prependThread true if the JavaThread value for the current thread is to be prepended
116     *            to the arguments for the call to {@code address}
117     * @param transition specifies if this is a {@linkplain Transition#LEAF leaf} call
118     * @param reexecutable specifies if the foreign call can be re-executed without (meaningful)
119     *            side effects. Deoptimization will not return to a point before a foreign call that
120     *            cannot be re-executed.
121     * @param killedLocations the memory locations killed by the foreign call
122     */
123    public void linkForeignCall(HotSpotProviders providers, ForeignCallDescriptor descriptor, long address, boolean prependThread, Transition transition, boolean reexecutable,
124                    LocationIdentity... killedLocations) {
125        ForeignCallStub stub = new ForeignCallStub(runtime, providers, address, descriptor, prependThread, transition, reexecutable, killedLocations);
126        HotSpotForeignCallLinkage linkage = stub.getLinkage();
127        HotSpotForeignCallLinkage targetLinkage = stub.getTargetLinkage();
128        linkage.setCompiledStub(stub);
129        register(linkage);
130        register(targetLinkage);
131    }
132
133    public static final boolean PREPEND_THREAD = true;
134    public static final boolean DONT_PREPEND_THREAD = !PREPEND_THREAD;
135
136    public static final boolean REEXECUTABLE = true;
137    public static final boolean NOT_REEXECUTABLE = !REEXECUTABLE;
138
139    public static final LocationIdentity[] NO_LOCATIONS = {};
140
141    public HotSpotForeignCallLinkage lookupForeignCall(ForeignCallDescriptor descriptor) {
142        assert foreignCalls != null : descriptor;
143        HotSpotForeignCallLinkage callTarget = foreignCalls.get(descriptor);
144        callTarget.finalizeAddress(runtime.getHostBackend());
145        return callTarget;
146    }
147
148    @Override
149    public boolean isReexecutable(ForeignCallDescriptor descriptor) {
150        assert foreignCalls.containsKey(descriptor) : "unknown foreign call: " + descriptor;
151        return foreignCalls.get(descriptor).isReexecutable();
152    }
153
154    public boolean canDeoptimize(ForeignCallDescriptor descriptor) {
155        assert foreignCalls.containsKey(descriptor) : "unknown foreign call: " + descriptor;
156        return foreignCalls.get(descriptor).needsDebugInfo();
157    }
158
159    public LocationIdentity[] getKilledLocations(ForeignCallDescriptor descriptor) {
160        assert foreignCalls.containsKey(descriptor) : "unknown foreign call: " + descriptor;
161        return foreignCalls.get(descriptor).getKilledLocations();
162    }
163}