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.amd64;
  24 
  25 import static org.graalvm.compiler.core.common.LocationIdentity.any;
  26 import static org.graalvm.compiler.hotspot.HotSpotBackend.EXCEPTION_HANDLER;
  27 import static org.graalvm.compiler.hotspot.HotSpotBackend.EXCEPTION_HANDLER_IN_CALLER;
  28 import static org.graalvm.compiler.hotspot.HotSpotBackend.Options.PreferGraalStubs;
  29 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.JUMP_ADDRESS;
  30 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.PRESERVES_REGISTERS;
  31 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition.LEAF;
  32 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition.LEAF_NOFP;
  33 import static org.graalvm.compiler.hotspot.HotSpotHostBackend.DEOPTIMIZATION_HANDLER;
  34 import static org.graalvm.compiler.hotspot.HotSpotHostBackend.UNCOMMON_TRAP_HANDLER;
  35 import static org.graalvm.compiler.hotspot.replacements.CRC32Substitutions.UPDATE_BYTES_CRC32;
  36 import static jdk.vm.ci.amd64.AMD64.rax;
  37 import static jdk.vm.ci.amd64.AMD64.rdx;
  38 import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.NativeCall;
  39 import static jdk.vm.ci.meta.Value.ILLEGAL;
  40 
  41 import org.graalvm.compiler.core.common.LIRKind;
  42 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  43 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  44 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkageImpl;
  45 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  46 import org.graalvm.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
  47 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  48 import org.graalvm.compiler.word.WordTypes;
  49 
  50 import jdk.vm.ci.code.CallingConvention;
  51 import jdk.vm.ci.code.CodeCacheProvider;
  52 import jdk.vm.ci.code.RegisterValue;
  53 import jdk.vm.ci.code.TargetDescription;
  54 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  55 import jdk.vm.ci.meta.MetaAccessProvider;
  56 import jdk.vm.ci.meta.PlatformKind;
  57 import jdk.vm.ci.meta.Value;
  58 
  59 public class AMD64HotSpotForeignCallsProvider extends HotSpotHostForeignCallsProvider {
  60 
  61     public static final ForeignCallDescriptor ARITHMETIC_SIN_STUB = new ForeignCallDescriptor("arithmeticSinStub", double.class, double.class);
  62     public static final ForeignCallDescriptor ARITHMETIC_COS_STUB = new ForeignCallDescriptor("arithmeticCosStub", double.class, double.class);
  63     public static final ForeignCallDescriptor ARITHMETIC_TAN_STUB = new ForeignCallDescriptor("arithmeticTanStub", double.class, double.class);
  64     public static final ForeignCallDescriptor ARITHMETIC_EXP_STUB = new ForeignCallDescriptor("arithmeticExpStub", double.class, double.class);
  65     public static final ForeignCallDescriptor ARITHMETIC_POW_STUB = new ForeignCallDescriptor("arithmeticPowStub", double.class, double.class, double.class);
  66     public static final ForeignCallDescriptor ARITHMETIC_LOG_STUB = new ForeignCallDescriptor("arithmeticLogStub", double.class, double.class);
  67     public static final ForeignCallDescriptor ARITHMETIC_LOG10_STUB = new ForeignCallDescriptor("arithmeticLog10Stub", double.class, double.class);
  68 
  69     private final Value[] nativeABICallerSaveRegisters;
  70 
  71     public AMD64HotSpotForeignCallsProvider(HotSpotJVMCIRuntimeProvider jvmciRuntime, HotSpotGraalRuntimeProvider runtime, MetaAccessProvider metaAccess, CodeCacheProvider codeCache,
  72                     WordTypes wordTypes, Value[] nativeABICallerSaveRegisters) {
  73         super(jvmciRuntime, runtime, metaAccess, codeCache, wordTypes);
  74         this.nativeABICallerSaveRegisters = nativeABICallerSaveRegisters;
  75     }
  76 
  77     @Override
  78     public void initialize(HotSpotProviders providers) {
  79         GraalHotSpotVMConfig config = runtime.getVMConfig();
  80         TargetDescription target = providers.getCodeCache().getTarget();
  81         PlatformKind word = target.arch.getWordKind();
  82 
  83         // The calling convention for the exception handler stub is (only?) defined in
  84         // TemplateInterpreterGenerator::generate_throw_exception()
  85         // in templateInterpreter_x86_64.cpp around line 1923
  86         RegisterValue exception = rax.asValue(LIRKind.reference(word));
  87         RegisterValue exceptionPc = rdx.asValue(LIRKind.value(word));
  88         CallingConvention exceptionCc = new CallingConvention(0, ILLEGAL, exception, exceptionPc);
  89         register(new HotSpotForeignCallLinkageImpl(EXCEPTION_HANDLER, 0L, PRESERVES_REGISTERS, LEAF_NOFP, exceptionCc, null, NOT_REEXECUTABLE, any()));
  90         register(new HotSpotForeignCallLinkageImpl(EXCEPTION_HANDLER_IN_CALLER, JUMP_ADDRESS, PRESERVES_REGISTERS, LEAF_NOFP, exceptionCc, null, NOT_REEXECUTABLE, any()));
  91 
  92         if (PreferGraalStubs.getValue()) {
  93             link(new AMD64DeoptimizationStub(providers, target, config, registerStubCall(DEOPTIMIZATION_HANDLER, REEXECUTABLE, LEAF, NO_LOCATIONS)));
  94             link(new AMD64UncommonTrapStub(providers, target, config, registerStubCall(UNCOMMON_TRAP_HANDLER, REEXECUTABLE, LEAF, NO_LOCATIONS)));
  95         }
  96         link(new AMD64MathStub(ARITHMETIC_LOG_STUB, providers, registerStubCall(ARITHMETIC_LOG_STUB, REEXECUTABLE, LEAF, NO_LOCATIONS)));
  97         link(new AMD64MathStub(ARITHMETIC_LOG10_STUB, providers, registerStubCall(ARITHMETIC_LOG10_STUB, REEXECUTABLE, LEAF, NO_LOCATIONS)));
  98         link(new AMD64MathStub(ARITHMETIC_SIN_STUB, providers, registerStubCall(ARITHMETIC_SIN_STUB, REEXECUTABLE, LEAF, NO_LOCATIONS)));
  99         link(new AMD64MathStub(ARITHMETIC_COS_STUB, providers, registerStubCall(ARITHMETIC_COS_STUB, REEXECUTABLE, LEAF, NO_LOCATIONS)));
 100         link(new AMD64MathStub(ARITHMETIC_TAN_STUB, providers, registerStubCall(ARITHMETIC_TAN_STUB, REEXECUTABLE, LEAF, NO_LOCATIONS)));
 101         link(new AMD64MathStub(ARITHMETIC_EXP_STUB, providers, registerStubCall(ARITHMETIC_EXP_STUB, REEXECUTABLE, LEAF, NO_LOCATIONS)));
 102         link(new AMD64MathStub(ARITHMETIC_POW_STUB, providers, registerStubCall(ARITHMETIC_POW_STUB, REEXECUTABLE, LEAF, NO_LOCATIONS)));
 103 
 104         if (config.useCRC32Intrinsics) {
 105             // This stub does callee saving
 106             registerForeignCall(UPDATE_BYTES_CRC32, config.updateBytesCRC32Stub, NativeCall, PRESERVES_REGISTERS, LEAF_NOFP, NOT_REEXECUTABLE, any());
 107         }
 108 
 109         super.initialize(providers);
 110     }
 111 
 112     @Override
 113     public Value[] getNativeABICallerSaveRegisters() {
 114         return nativeABICallerSaveRegisters;
 115     }
 116 
 117 }