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.sparc;
  24 
  25 import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.NativeCall;
  26 import static jdk.vm.ci.meta.Value.ILLEGAL;
  27 import static jdk.vm.ci.sparc.SPARC.i0;
  28 import static jdk.vm.ci.sparc.SPARC.i1;
  29 import static jdk.vm.ci.sparc.SPARC.o0;
  30 import static jdk.vm.ci.sparc.SPARC.o1;
  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.HotSpotForeignCallLinkage.JUMP_ADDRESS;
  34 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.PRESERVES_REGISTERS;
  35 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition.LEAF_NOFP;
  36 import static org.graalvm.compiler.hotspot.replacements.CRC32Substitutions.UPDATE_BYTES_CRC32;
  37 import static org.graalvm.compiler.hotspot.replacements.CRC32CSubstitutions.UPDATE_BYTES_CRC32C;
  38 import static org.graalvm.word.LocationIdentity.any;
  39 
  40 import org.graalvm.compiler.core.common.LIRKind;
  41 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  42 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkageImpl;
  43 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  44 import org.graalvm.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
  45 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  46 import org.graalvm.compiler.options.OptionValues;
  47 import org.graalvm.compiler.word.WordTypes;
  48 
  49 import jdk.vm.ci.code.CallingConvention;
  50 import jdk.vm.ci.code.CodeCacheProvider;
  51 import jdk.vm.ci.code.RegisterValue;
  52 import jdk.vm.ci.code.TargetDescription;
  53 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  54 import jdk.vm.ci.meta.JavaKind;
  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 SPARCHotSpotForeignCallsProvider extends HotSpotHostForeignCallsProvider {
  60 
  61     private final Value[] nativeABICallerSaveRegisters;
  62 
  63     public SPARCHotSpotForeignCallsProvider(HotSpotJVMCIRuntimeProvider jvmciRuntime, HotSpotGraalRuntimeProvider runtime, MetaAccessProvider metaAccess, CodeCacheProvider codeCache,
  64                     WordTypes wordTypes, Value[] nativeABICallerSaveRegisters) {
  65         super(jvmciRuntime, runtime, metaAccess, codeCache, wordTypes);
  66         this.nativeABICallerSaveRegisters = nativeABICallerSaveRegisters;
  67     }
  68 
  69     @Override
  70     public void initialize(HotSpotProviders providers, OptionValues options) {
  71         GraalHotSpotVMConfig config = runtime.getVMConfig();
  72         TargetDescription target = providers.getCodeCache().getTarget();
  73         PlatformKind word = target.arch.getWordKind();
  74 
  75         // The calling convention for the exception handler stub is (only?) defined in
  76         // TemplateInterpreterGenerator::generate_throw_exception()
  77         // in templateInterpreter_sparc.cpp around line 1925
  78         RegisterValue outgoingException = o0.asValue(LIRKind.fromJavaKind(target.arch, JavaKind.Object));
  79         RegisterValue outgoingExceptionPc = o1.asValue(LIRKind.value(word));
  80         RegisterValue incomingException = i0.asValue(LIRKind.fromJavaKind(target.arch, JavaKind.Object));
  81         RegisterValue incomingExceptionPc = i1.asValue(LIRKind.value(word));
  82         CallingConvention outgoingExceptionCc = new CallingConvention(0, ILLEGAL, outgoingException, outgoingExceptionPc);
  83         CallingConvention incomingExceptionCc = new CallingConvention(0, ILLEGAL, incomingException, incomingExceptionPc);
  84         register(new HotSpotForeignCallLinkageImpl(EXCEPTION_HANDLER, 0L, PRESERVES_REGISTERS, LEAF_NOFP, outgoingExceptionCc, incomingExceptionCc, NOT_REEXECUTABLE, any()));
  85         register(new HotSpotForeignCallLinkageImpl(EXCEPTION_HANDLER_IN_CALLER, JUMP_ADDRESS, PRESERVES_REGISTERS, LEAF_NOFP, outgoingExceptionCc, incomingExceptionCc, NOT_REEXECUTABLE, any()));
  86 
  87         if (config.useCRC32Intrinsics) {
  88             // This stub does callee saving
  89             registerForeignCall(UPDATE_BYTES_CRC32, config.updateBytesCRC32Stub, NativeCall, PRESERVES_REGISTERS, LEAF_NOFP, NOT_REEXECUTABLE, any());
  90         }
  91         if (config.useCRC32CIntrinsics) {
  92             registerForeignCall(UPDATE_BYTES_CRC32C, config.updateBytesCRC32C, NativeCall, PRESERVES_REGISTERS, LEAF_NOFP, NOT_REEXECUTABLE, any());
  93         }
  94 
  95         super.initialize(providers, options);
  96     }
  97 
  98     @Override
  99     public Value[] getNativeABICallerSaveRegisters() {
 100         return nativeABICallerSaveRegisters;
 101     }
 102 }