1 /*
   2  * Copyright (c) 2013, 2018, 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 
  24 
  25 package org.graalvm.compiler.hotspot.amd64;
  26 
  27 import static jdk.vm.ci.amd64.AMD64.rax;
  28 import static jdk.vm.ci.amd64.AMD64.rdx;
  29 import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.NativeCall;
  30 import static jdk.vm.ci.meta.Value.ILLEGAL;
  31 import static org.graalvm.compiler.hotspot.HotSpotBackend.EXCEPTION_HANDLER;
  32 import static org.graalvm.compiler.hotspot.HotSpotBackend.EXCEPTION_HANDLER_IN_CALLER;
  33 import static org.graalvm.compiler.hotspot.HotSpotBackend.Options.GraalArithmeticStubs;
  34 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.JUMP_ADDRESS;
  35 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Reexecutability.REEXECUTABLE;
  36 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Reexecutability.REEXECUTABLE_ONLY_AFTER_EXCEPTION;
  37 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.PRESERVES_REGISTERS;
  38 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition.LEAF;
  39 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition.LEAF_NOFP;
  40 import static org.graalvm.compiler.hotspot.replacements.CRC32CSubstitutions.UPDATE_BYTES_CRC32C;
  41 import static org.graalvm.compiler.hotspot.replacements.CRC32Substitutions.UPDATE_BYTES_CRC32;
  42 import static org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation.POW;
  43 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.COS;
  44 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.EXP;
  45 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG;
  46 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.LOG10;
  47 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.SIN;
  48 import static org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation.TAN;
  49 import static jdk.internal.vm.compiler.word.LocationIdentity.any;
  50 
  51 import org.graalvm.compiler.core.common.LIRKind;

  52 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  53 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkageImpl;
  54 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  55 import org.graalvm.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
  56 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  57 import org.graalvm.compiler.options.OptionValues;
  58 import org.graalvm.compiler.replacements.amd64.AMD64ArrayIndexOf;
  59 import org.graalvm.compiler.word.WordTypes;
  60 
  61 import jdk.vm.ci.code.CallingConvention;
  62 import jdk.vm.ci.code.CodeCacheProvider;
  63 import jdk.vm.ci.code.RegisterValue;
  64 import jdk.vm.ci.code.TargetDescription;
  65 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  66 import jdk.vm.ci.meta.MetaAccessProvider;
  67 import jdk.vm.ci.meta.PlatformKind;
  68 import jdk.vm.ci.meta.Value;
  69 
  70 public class AMD64HotSpotForeignCallsProvider extends HotSpotHostForeignCallsProvider {
  71 








  72     private final Value[] nativeABICallerSaveRegisters;
  73 
  74     public AMD64HotSpotForeignCallsProvider(HotSpotJVMCIRuntime jvmciRuntime, HotSpotGraalRuntimeProvider runtime, MetaAccessProvider metaAccess, CodeCacheProvider codeCache,
  75                     WordTypes wordTypes, Value[] nativeABICallerSaveRegisters) {
  76         super(jvmciRuntime, runtime, metaAccess, codeCache, wordTypes);
  77         this.nativeABICallerSaveRegisters = nativeABICallerSaveRegisters;
  78     }
  79 
  80     @Override
  81     public void initialize(HotSpotProviders providers, OptionValues options) {
  82         GraalHotSpotVMConfig config = runtime.getVMConfig();
  83         TargetDescription target = providers.getCodeCache().getTarget();
  84         PlatformKind word = target.arch.getWordKind();
  85 
  86         // The calling convention for the exception handler stub is (only?) defined in
  87         // TemplateInterpreterGenerator::generate_throw_exception()
  88         // in templateInterpreter_x86_64.cpp around line 1923
  89         RegisterValue exception = rax.asValue(LIRKind.reference(word));
  90         RegisterValue exceptionPc = rdx.asValue(LIRKind.value(word));
  91         CallingConvention exceptionCc = new CallingConvention(0, ILLEGAL, exception, exceptionPc);
  92         register(new HotSpotForeignCallLinkageImpl(EXCEPTION_HANDLER, 0L, PRESERVES_REGISTERS, LEAF_NOFP, REEXECUTABLE_ONLY_AFTER_EXCEPTION, exceptionCc, null, any()));
  93         register(new HotSpotForeignCallLinkageImpl(EXCEPTION_HANDLER_IN_CALLER, JUMP_ADDRESS, PRESERVES_REGISTERS, LEAF_NOFP, REEXECUTABLE_ONLY_AFTER_EXCEPTION, exceptionCc, null, any()));
  94 








  95         if (config.useCRC32Intrinsics) {
  96             // This stub does callee saving
  97             registerForeignCall(UPDATE_BYTES_CRC32, config.updateBytesCRC32Stub, NativeCall, PRESERVES_REGISTERS, LEAF_NOFP, REEXECUTABLE_ONLY_AFTER_EXCEPTION, any());
  98         }
  99         if (config.useCRC32CIntrinsics) {
 100             registerForeignCall(UPDATE_BYTES_CRC32C, config.updateBytesCRC32C, NativeCall, PRESERVES_REGISTERS, LEAF_NOFP, REEXECUTABLE_ONLY_AFTER_EXCEPTION, any());
 101         }
 102 
 103         link(new AMD64ArrayIndexOfStub(AMD64ArrayIndexOf.STUB_INDEX_OF_TWO_CONSECUTIVE_BYTES, options, providers,
 104                         registerStubCall(AMD64ArrayIndexOf.STUB_INDEX_OF_TWO_CONSECUTIVE_BYTES, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 105         link(new AMD64ArrayIndexOfStub(AMD64ArrayIndexOf.STUB_INDEX_OF_TWO_CONSECUTIVE_CHARS, options, providers,
 106                         registerStubCall(AMD64ArrayIndexOf.STUB_INDEX_OF_TWO_CONSECUTIVE_CHARS, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 107         link(new AMD64ArrayIndexOfStub(AMD64ArrayIndexOf.STUB_INDEX_OF_1_BYTE, options, providers,
 108                         registerStubCall(AMD64ArrayIndexOf.STUB_INDEX_OF_1_BYTE, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 109         link(new AMD64ArrayIndexOfStub(AMD64ArrayIndexOf.STUB_INDEX_OF_2_BYTES, options, providers,
 110                         registerStubCall(AMD64ArrayIndexOf.STUB_INDEX_OF_2_BYTES, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 111         link(new AMD64ArrayIndexOfStub(AMD64ArrayIndexOf.STUB_INDEX_OF_3_BYTES, options, providers,
 112                         registerStubCall(AMD64ArrayIndexOf.STUB_INDEX_OF_3_BYTES, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 113         link(new AMD64ArrayIndexOfStub(AMD64ArrayIndexOf.STUB_INDEX_OF_4_BYTES, options, providers,
 114                         registerStubCall(AMD64ArrayIndexOf.STUB_INDEX_OF_4_BYTES, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 115         link(new AMD64ArrayIndexOfStub(AMD64ArrayIndexOf.STUB_INDEX_OF_1_CHAR, options, providers,
 116                         registerStubCall(AMD64ArrayIndexOf.STUB_INDEX_OF_1_CHAR, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 117         link(new AMD64ArrayIndexOfStub(AMD64ArrayIndexOf.STUB_INDEX_OF_2_CHARS, options, providers,
 118                         registerStubCall(AMD64ArrayIndexOf.STUB_INDEX_OF_2_CHARS, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 119         link(new AMD64ArrayIndexOfStub(AMD64ArrayIndexOf.STUB_INDEX_OF_3_CHARS, options, providers,
 120                         registerStubCall(AMD64ArrayIndexOf.STUB_INDEX_OF_3_CHARS, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 121         link(new AMD64ArrayIndexOfStub(AMD64ArrayIndexOf.STUB_INDEX_OF_4_CHARS, options, providers,
 122                         registerStubCall(AMD64ArrayIndexOf.STUB_INDEX_OF_4_CHARS, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 123 
 124         super.initialize(providers, options);
 125     }
 126 
 127     @Override
 128     public Value[] getNativeABICallerSaveRegisters() {
 129         return nativeABICallerSaveRegisters;
 130     }
 131 
 132     @Override
 133     protected void registerMathStubs(GraalHotSpotVMConfig hotSpotVMConfig, HotSpotProviders providers, OptionValues options) {
 134         if (GraalArithmeticStubs.getValue(options)) {
 135             link(new AMD64MathStub(SIN, options, providers, registerStubCall(SIN.foreignCallDescriptor, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 136             link(new AMD64MathStub(COS, options, providers, registerStubCall(COS.foreignCallDescriptor, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 137             link(new AMD64MathStub(TAN, options, providers, registerStubCall(TAN.foreignCallDescriptor, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 138             link(new AMD64MathStub(EXP, options, providers, registerStubCall(EXP.foreignCallDescriptor, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 139             link(new AMD64MathStub(LOG, options, providers, registerStubCall(LOG.foreignCallDescriptor, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 140             link(new AMD64MathStub(LOG10, options, providers, registerStubCall(LOG10.foreignCallDescriptor, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 141             link(new AMD64MathStub(POW, options, providers, registerStubCall(POW.foreignCallDescriptor, LEAF, REEXECUTABLE, NO_LOCATIONS)));
 142         } else {
 143             super.registerMathStubs(hotSpotVMConfig, providers, options);
 144         }
 145     }
 146 
 147 }
--- EOF ---