1 /*
   2  * Copyright (c) 2013, 2016, 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.hotspot.meta;
  24 
  25 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.PRESERVES_REGISTERS;
  26 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition.SAFEPOINT;
  27 import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.JavaCall;
  28 import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.JavaCallee;
  29 
  30 import java.util.HashMap;
  31 import java.util.Map;
  32 
  33 import org.graalvm.compiler.core.common.LIRKind;
  34 import org.graalvm.compiler.core.common.LocationIdentity;
  35 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  36 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
  37 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect;
  38 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition;
  39 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkageImpl;
  40 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  41 import org.graalvm.compiler.hotspot.stubs.ForeignCallStub;
  42 import org.graalvm.compiler.hotspot.stubs.Stub;
  43 import org.graalvm.compiler.word.Word;
  44 import org.graalvm.compiler.word.WordTypes;
  45 
  46 import jdk.vm.ci.code.CallingConvention;
  47 import jdk.vm.ci.code.CodeCacheProvider;
  48 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  49 import jdk.vm.ci.meta.JavaKind;
  50 import jdk.vm.ci.meta.MetaAccessProvider;
  51 
  52 /**
  53  * HotSpot implementation of {@link HotSpotForeignCallsProvider}.
  54  */
  55 public abstract class HotSpotForeignCallsProviderImpl implements HotSpotForeignCallsProvider {
  56 
  57     public static final ForeignCallDescriptor OSR_MIGRATION_END = new ForeignCallDescriptor("OSR_migration_end", void.class, long.class);
  58     public static final ForeignCallDescriptor IDENTITY_HASHCODE = new ForeignCallDescriptor("identity_hashcode", int.class, Object.class);
  59     public static final ForeignCallDescriptor VERIFY_OOP = new ForeignCallDescriptor("verify_oop", Object.class, Object.class);
  60     public static final ForeignCallDescriptor LOAD_AND_CLEAR_EXCEPTION = new ForeignCallDescriptor("load_and_clear_exception", Object.class, Word.class);
  61 
  62     public static final ForeignCallDescriptor TEST_DEOPTIMIZE_CALL_INT = new ForeignCallDescriptor("test_deoptimize_call_int", int.class, int.class);
  63 
  64     protected final HotSpotJVMCIRuntimeProvider jvmciRuntime;
  65     protected final HotSpotGraalRuntimeProvider runtime;
  66 
  67     protected final Map<ForeignCallDescriptor, HotSpotForeignCallLinkage> foreignCalls = new HashMap<>();
  68     protected final MetaAccessProvider metaAccess;
  69     protected final CodeCacheProvider codeCache;
  70     protected final WordTypes wordTypes;
  71 
  72     public HotSpotForeignCallsProviderImpl(HotSpotJVMCIRuntimeProvider jvmciRuntime, HotSpotGraalRuntimeProvider runtime, MetaAccessProvider metaAccess, CodeCacheProvider codeCache,
  73                     WordTypes wordTypes) {
  74         this.jvmciRuntime = jvmciRuntime;
  75         this.runtime = runtime;
  76         this.metaAccess = metaAccess;
  77         this.codeCache = codeCache;
  78         this.wordTypes = wordTypes;
  79     }
  80 
  81     /**
  82      * Registers the linkage for a foreign call.
  83      */
  84     public HotSpotForeignCallLinkage register(HotSpotForeignCallLinkage linkage) {
  85         assert !foreignCalls.containsKey(linkage.getDescriptor()) : "already registered linkage for " + linkage.getDescriptor();
  86         foreignCalls.put(linkage.getDescriptor(), linkage);
  87         return linkage;
  88     }
  89 
  90     /**
  91      * Return true if the descriptor has already been registered.
  92      */
  93     public boolean isRegistered(ForeignCallDescriptor descriptor) {
  94         return foreignCalls.containsKey(descriptor);
  95     }
  96 
  97     /**
  98      * Creates and registers the details for linking a foreign call to a {@link Stub}.
  99      *
 100      * @param descriptor the signature of the call to the stub
 101      * @param reexecutable specifies if the stub call can be re-executed without (meaningful) side
 102      *            effects. Deoptimization will not return to a point before a stub call that cannot
 103      *            be re-executed.
 104      * @param transition specifies if this is a {@linkplain Transition#LEAF leaf} call
 105      * @param killedLocations the memory locations killed by the stub call
 106      */
 107     public HotSpotForeignCallLinkage registerStubCall(ForeignCallDescriptor descriptor, boolean reexecutable, Transition transition, LocationIdentity... killedLocations) {
 108         return register(HotSpotForeignCallLinkageImpl.create(metaAccess, codeCache, wordTypes, this, descriptor, 0L, PRESERVES_REGISTERS, JavaCall, JavaCallee, transition, reexecutable,
 109                         killedLocations));
 110     }
 111 
 112     /**
 113      * Creates and registers the linkage for a foreign call.
 114      *
 115      * @param descriptor the signature of the foreign call
 116      * @param address the address of the code to call
 117      * @param outgoingCcType outgoing (caller) calling convention type
 118      * @param effect specifies if the call destroys or preserves all registers (apart from
 119      *            temporaries which are always destroyed)
 120      * @param transition specifies if this is a {@linkplain Transition#LEAF leaf} call
 121      * @param reexecutable specifies if the foreign call can be re-executed without (meaningful)
 122      *            side effects. Deoptimization will not return to a point before a foreign call that
 123      *            cannot be re-executed.
 124      * @param killedLocations the memory locations killed by the foreign call
 125      */
 126     public HotSpotForeignCallLinkage registerForeignCall(ForeignCallDescriptor descriptor, long address, CallingConvention.Type outgoingCcType, RegisterEffect effect, Transition transition,
 127                     boolean reexecutable, LocationIdentity... killedLocations) {
 128         Class<?> resultType = descriptor.getResultType();
 129         assert address != 0;
 130         assert transition != SAFEPOINT || resultType.isPrimitive() || Word.class.isAssignableFrom(resultType) : "non-leaf foreign calls must return objects in thread local storage: " + descriptor;
 131         return register(HotSpotForeignCallLinkageImpl.create(metaAccess, codeCache, wordTypes, this, descriptor, address, effect, outgoingCcType, null, transition, reexecutable, killedLocations));
 132     }
 133 
 134     /**
 135      * Creates a {@linkplain ForeignCallStub stub} for a foreign call.
 136      *
 137      * @param descriptor the signature of the call to the stub
 138      * @param address the address of the foreign code to call
 139      * @param prependThread true if the JavaThread value for the current thread is to be prepended
 140      *            to the arguments for the call to {@code address}
 141      * @param transition specifies if this is a {@linkplain Transition#LEAF leaf} call
 142      * @param reexecutable specifies if the foreign call can be re-executed without (meaningful)
 143      *            side effects. Deoptimization will not return to a point before a foreign call that
 144      *            cannot be re-executed.
 145      * @param killedLocations the memory locations killed by the foreign call
 146      */
 147     public void linkForeignCall(HotSpotProviders providers, ForeignCallDescriptor descriptor, long address, boolean prependThread, Transition transition, boolean reexecutable,
 148                     LocationIdentity... killedLocations) {
 149         ForeignCallStub stub = new ForeignCallStub(jvmciRuntime, providers, address, descriptor, prependThread, transition, reexecutable, killedLocations);
 150         HotSpotForeignCallLinkage linkage = stub.getLinkage();
 151         HotSpotForeignCallLinkage targetLinkage = stub.getTargetLinkage();
 152         linkage.setCompiledStub(stub);
 153         register(linkage);
 154         register(targetLinkage);
 155     }
 156 
 157     public static final boolean PREPEND_THREAD = true;
 158     public static final boolean DONT_PREPEND_THREAD = !PREPEND_THREAD;
 159 
 160     public static final boolean REEXECUTABLE = true;
 161     public static final boolean NOT_REEXECUTABLE = !REEXECUTABLE;
 162 
 163     public static final LocationIdentity[] NO_LOCATIONS = {};
 164 
 165     @Override
 166     public HotSpotForeignCallLinkage lookupForeignCall(ForeignCallDescriptor descriptor) {
 167         assert foreignCalls != null : descriptor;
 168         HotSpotForeignCallLinkage callTarget = foreignCalls.get(descriptor);
 169         callTarget.finalizeAddress(runtime.getHostBackend());
 170         return callTarget;
 171     }
 172 
 173     @Override
 174     public boolean isReexecutable(ForeignCallDescriptor descriptor) {
 175         assert foreignCalls.containsKey(descriptor) : "unknown foreign call: " + descriptor;
 176         return foreignCalls.get(descriptor).isReexecutable();
 177     }
 178 
 179     @Override
 180     public boolean canDeoptimize(ForeignCallDescriptor descriptor) {
 181         assert foreignCalls.containsKey(descriptor) : "unknown foreign call: " + descriptor;
 182         return foreignCalls.get(descriptor).needsDebugInfo();
 183     }
 184 
 185     @Override
 186     public boolean isGuaranteedSafepoint(ForeignCallDescriptor descriptor) {
 187         assert foreignCalls.containsKey(descriptor) : "unknown foreign call: " + descriptor;
 188         return foreignCalls.get(descriptor).isGuaranteedSafepoint();
 189     }
 190 
 191     @Override
 192     public LocationIdentity[] getKilledLocations(ForeignCallDescriptor descriptor) {
 193         assert foreignCalls.containsKey(descriptor) : "unknown foreign call: " + descriptor;
 194         return foreignCalls.get(descriptor).getKilledLocations();
 195     }
 196 
 197     @Override
 198     public LIRKind getValueKind(JavaKind javaKind) {
 199         return LIRKind.fromJavaKind(codeCache.getTarget().arch, javaKind);
 200     }
 201 }