001/*
002 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.amd64;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.hotspot.*;
027import jdk.internal.jvmci.meta.*;
028import static com.oracle.graal.hotspot.HotSpotBackend.*;
029import static com.oracle.graal.hotspot.HotSpotBackend.Options.*;
030import static com.oracle.graal.hotspot.HotSpotForeignCallLinkage.*;
031import static com.oracle.graal.hotspot.HotSpotForeignCallLinkage.RegisterEffect.*;
032import static com.oracle.graal.hotspot.HotSpotForeignCallLinkage.Transition.*;
033import static com.oracle.graal.hotspot.HotSpotHostBackend.*;
034import static com.oracle.graal.hotspot.replacements.CRC32Substitutions.*;
035import static jdk.internal.jvmci.amd64.AMD64.*;
036import static jdk.internal.jvmci.code.CallingConvention.Type.*;
037import static jdk.internal.jvmci.meta.LocationIdentity.*;
038import static jdk.internal.jvmci.meta.Value.*;
039
040import com.oracle.graal.hotspot.*;
041import com.oracle.graal.hotspot.meta.*;
042
043public class AMD64HotSpotForeignCallsProvider extends HotSpotHostForeignCallsProvider {
044
045    private final Value[] nativeABICallerSaveRegisters;
046
047    public AMD64HotSpotForeignCallsProvider(HotSpotGraalRuntimeProvider runtime, MetaAccessProvider metaAccess, CodeCacheProvider codeCache, Value[] nativeABICallerSaveRegisters) {
048        super(runtime, metaAccess, codeCache);
049        this.nativeABICallerSaveRegisters = nativeABICallerSaveRegisters;
050    }
051
052    @Override
053    public void initialize(HotSpotProviders providers, HotSpotVMConfig config) {
054        TargetDescription target = providers.getCodeCache().getTarget();
055        Kind word = target.wordKind;
056
057        // The calling convention for the exception handler stub is (only?) defined in
058        // TemplateInterpreterGenerator::generate_throw_exception()
059        // in templateInterpreter_x86_64.cpp around line 1923
060        RegisterValue exception = rax.asValue(LIRKind.reference(Kind.Object));
061        RegisterValue exceptionPc = rdx.asValue(LIRKind.value(word));
062        CallingConvention exceptionCc = new CallingConvention(0, ILLEGAL, exception, exceptionPc);
063        register(new HotSpotForeignCallLinkageImpl(EXCEPTION_HANDLER, 0L, PRESERVES_REGISTERS, LEAF_NOFP, null, exceptionCc, NOT_REEXECUTABLE, any()));
064        register(new HotSpotForeignCallLinkageImpl(EXCEPTION_HANDLER_IN_CALLER, JUMP_ADDRESS, PRESERVES_REGISTERS, LEAF_NOFP, exceptionCc, null, NOT_REEXECUTABLE, any()));
065
066        if (PreferGraalStubs.getValue()) {
067            link(new AMD64DeoptimizationStub(providers, target, config, registerStubCall(DEOPTIMIZATION_HANDLER, REEXECUTABLE, LEAF, NO_LOCATIONS)));
068            link(new AMD64UncommonTrapStub(providers, target, config, registerStubCall(UNCOMMON_TRAP_HANDLER, REEXECUTABLE, LEAF, NO_LOCATIONS)));
069        }
070
071        if (config.useCRC32Intrinsics) {
072            // This stub does callee saving
073            registerForeignCall(UPDATE_BYTES_CRC32, config.updateBytesCRC32Stub, NativeCall, PRESERVES_REGISTERS, LEAF_NOFP, NOT_REEXECUTABLE, any());
074        }
075
076        super.initialize(providers, config);
077    }
078
079    @Override
080    public Value[] getNativeABICallerSaveRegisters() {
081        return nativeABICallerSaveRegisters;
082    }
083
084}