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.aarch64;
  24 
  25 import static jdk.vm.ci.aarch64.AArch64.r0;
  26 import static jdk.vm.ci.aarch64.AArch64.r3;
  27 import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.NativeCall;
  28 import static jdk.vm.ci.meta.Value.ILLEGAL;
  29 import static org.graalvm.compiler.core.common.LocationIdentity.any;
  30 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.JUMP_ADDRESS;
  31 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.PRESERVES_REGISTERS;
  32 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition.LEAF;
  33 import static org.graalvm.compiler.hotspot.replacements.CRC32Substitutions.UPDATE_BYTES_CRC32;
  34 
  35 import org.graalvm.compiler.core.common.LIRKind;
  36 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  37 import org.graalvm.compiler.hotspot.HotSpotBackend;
  38 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkageImpl;
  39 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  40 import org.graalvm.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
  41 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  42 import org.graalvm.compiler.options.OptionValues;
  43 import org.graalvm.compiler.word.WordTypes;
  44 
  45 import jdk.vm.ci.code.CallingConvention;
  46 import jdk.vm.ci.code.CodeCacheProvider;
  47 import jdk.vm.ci.code.RegisterValue;
  48 import jdk.vm.ci.code.TargetDescription;
  49 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  50 import jdk.vm.ci.meta.MetaAccessProvider;
  51 import jdk.vm.ci.meta.PlatformKind;
  52 import jdk.vm.ci.meta.Value;
  53 
  54 public class AArch64HotSpotForeignCallsProvider extends HotSpotHostForeignCallsProvider {
  55 
  56     private final Value[] nativeABICallerSaveRegisters;
  57 
  58     public AArch64HotSpotForeignCallsProvider(HotSpotJVMCIRuntimeProvider jvmciRuntime, HotSpotGraalRuntimeProvider runtime, MetaAccessProvider metaAccess, CodeCacheProvider codeCache,
  59                     WordTypes wordTypes, Value[] nativeABICallerSaveRegisters) {
  60         super(jvmciRuntime, runtime, metaAccess, codeCache, wordTypes);
  61         this.nativeABICallerSaveRegisters = nativeABICallerSaveRegisters;
  62     }
  63 
  64     @Override
  65     public void initialize(HotSpotProviders providers, OptionValues options) {
  66         GraalHotSpotVMConfig config = runtime.getVMConfig();
  67         TargetDescription target = providers.getCodeCache().getTarget();
  68         PlatformKind word = target.arch.getWordKind();
  69 
  70         // The calling convention for the exception handler stub is (only?) defined in
  71         // TemplateInterpreterGenerator::generate_throw_exception()
  72         RegisterValue exception = r0.asValue(LIRKind.reference(word));
  73         RegisterValue exceptionPc = r3.asValue(LIRKind.value(word));
  74         CallingConvention exceptionCc = new CallingConvention(0, ILLEGAL, exception, exceptionPc);
  75         register(new HotSpotForeignCallLinkageImpl(HotSpotBackend.EXCEPTION_HANDLER, 0L, PRESERVES_REGISTERS, LEAF, exceptionCc, null, NOT_REEXECUTABLE, any()));
  76         register(new HotSpotForeignCallLinkageImpl(HotSpotBackend.EXCEPTION_HANDLER_IN_CALLER, JUMP_ADDRESS, PRESERVES_REGISTERS, LEAF, exceptionCc, null, NOT_REEXECUTABLE, any()));
  77 
  78         // These stubs do callee saving
  79         if (config.useCRC32Intrinsics) {
  80             registerForeignCall(UPDATE_BYTES_CRC32, config.updateBytesCRC32Stub, NativeCall, PRESERVES_REGISTERS, LEAF, NOT_REEXECUTABLE, any());
  81         }
  82 
  83         super.initialize(providers, options);
  84     }
  85 
  86     @Override
  87     public Value[] getNativeABICallerSaveRegisters() {
  88         return nativeABICallerSaveRegisters;
  89     }
  90 
  91 }