001/*
002 * Copyright (c) 2012, 2015, 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.sparc;
024
025import java.util.*;
026
027import jdk.internal.jvmci.code.*;
028import jdk.internal.jvmci.hotspot.*;
029import jdk.internal.jvmci.meta.*;
030import jdk.internal.jvmci.runtime.*;
031import jdk.internal.jvmci.service.*;
032import jdk.internal.jvmci.sparc.*;
033
034import com.oracle.graal.compiler.sparc.*;
035import com.oracle.graal.graphbuilderconf.GraphBuilderConfiguration.Plugins;
036import com.oracle.graal.hotspot.*;
037import com.oracle.graal.hotspot.meta.*;
038import com.oracle.graal.hotspot.nodes.type.*;
039import com.oracle.graal.hotspot.word.*;
040import com.oracle.graal.java.*;
041import com.oracle.graal.nodes.spi.*;
042import com.oracle.graal.phases.util.*;
043import com.oracle.graal.replacements.sparc.*;
044
045@ServiceProvider(HotSpotBackendFactory.class)
046public class SPARCHotSpotBackendFactory implements HotSpotBackendFactory {
047
048    @Override
049    public HotSpotBackend createBackend(HotSpotGraalRuntimeProvider runtime, JVMCIBackend jvmci, HotSpotBackend host) {
050        assert host == null;
051
052        HotSpotRegistersProvider registers = createRegisters();
053        HotSpotMetaAccessProvider metaAccess = (HotSpotMetaAccessProvider) jvmci.getMetaAccess();
054        HotSpotCodeCacheProvider codeCache = (HotSpotCodeCacheProvider) jvmci.getCodeCache();
055        TargetDescription target = codeCache.getTarget();
056        HotSpotConstantReflectionProvider constantReflection = new HotSpotGraalConstantReflectionProvider(runtime.getJVMCIRuntime());
057        Value[] nativeABICallerSaveRegisters = createNativeABICallerSaveRegisters(runtime.getConfig(), codeCache.getRegisterConfig());
058        HotSpotForeignCallsProvider foreignCalls = new SPARCHotSpotForeignCallsProvider(runtime, metaAccess, codeCache, nativeABICallerSaveRegisters);
059        LoweringProvider lowerer = createLowerer(runtime, metaAccess, foreignCalls, registers, target);
060        HotSpotStampProvider stampProvider = new HotSpotStampProvider(codeCache.getTarget().wordKind);
061        Providers p = new Providers(metaAccess, codeCache, constantReflection, foreignCalls, lowerer, null, stampProvider);
062        HotSpotSnippetReflectionProvider snippetReflection = new HotSpotSnippetReflectionProvider(runtime);
063        HotSpotReplacementsImpl replacements = new HotSpotReplacementsImpl(p, snippetReflection, runtime.getConfig(), target);
064        HotSpotWordTypes wordTypes = new HotSpotWordTypes(metaAccess, target.wordKind, stampProvider.createHubStamp(false), MethodPointerStamp.method());
065        Plugins plugins = createGraphBuilderPlugins(runtime, metaAccess, constantReflection, foreignCalls, stampProvider, snippetReflection, replacements, wordTypes);
066        replacements.setGraphBuilderPlugins(plugins);
067        HotSpotSuitesProvider suites = createSuites(runtime, plugins, codeCache);
068        HotSpotProviders providers = new HotSpotProviders(metaAccess, codeCache, constantReflection, foreignCalls, lowerer, replacements, suites, registers, snippetReflection, wordTypes, plugins);
069
070        return createBackend(runtime, providers);
071    }
072
073    protected Plugins createGraphBuilderPlugins(HotSpotGraalRuntimeProvider runtime, HotSpotMetaAccessProvider metaAccess, HotSpotConstantReflectionProvider constantReflection,
074                    HotSpotForeignCallsProvider foreignCalls, HotSpotStampProvider stampProvider, HotSpotSnippetReflectionProvider snippetReflection, HotSpotReplacementsImpl replacements,
075                    HotSpotWordTypes wordTypes) {
076        Plugins plugins = HotSpotGraphBuilderPlugins.create(runtime.getConfig(), wordTypes, metaAccess, constantReflection, snippetReflection, foreignCalls, stampProvider, replacements);
077        SPARCGraphBuilderPlugins.register(plugins, foreignCalls);
078        return plugins;
079    }
080
081    protected HotSpotSuitesProvider createSuites(HotSpotGraalRuntimeProvider runtime, Plugins plugins, CodeCacheProvider codeCache) {
082        return new HotSpotSuitesProvider(new DefaultSuitesProvider(plugins), runtime, new SPARCAddressLowering(codeCache));
083    }
084
085    protected SPARCHotSpotBackend createBackend(HotSpotGraalRuntimeProvider runtime, HotSpotProviders providers) {
086        return new SPARCHotSpotBackend(runtime, providers);
087    }
088
089    protected HotSpotLoweringProvider createLowerer(HotSpotGraalRuntimeProvider runtime, HotSpotMetaAccessProvider metaAccess, HotSpotForeignCallsProvider foreignCalls,
090                    HotSpotRegistersProvider registers, TargetDescription target) {
091        return new SPARCHotSpotLoweringProvider(runtime, metaAccess, foreignCalls, registers, target);
092    }
093
094    protected HotSpotRegistersProvider createRegisters() {
095        return new HotSpotRegisters(SPARC.g2, SPARC.g6, SPARC.sp);
096    }
097
098    @SuppressWarnings("unused")
099    private static Value[] createNativeABICallerSaveRegisters(HotSpotVMConfig config, RegisterConfig regConfig) {
100        Set<Register> callerSavedRegisters = new HashSet<>();
101        Collections.addAll(callerSavedRegisters, regConfig.getCalleeSaveLayout().registers);
102        Collections.addAll(callerSavedRegisters, SPARC.fpuRegisters);
103        callerSavedRegisters.add(SPARC.g1);
104        callerSavedRegisters.add(SPARC.g4);
105        callerSavedRegisters.add(SPARC.g5);
106        Value[] nativeABICallerSaveRegisters = new Value[callerSavedRegisters.size()];
107        int i = 0;
108        for (Register reg : callerSavedRegisters) {
109            nativeABICallerSaveRegisters[i] = reg.asValue();
110            i++;
111        }
112        return nativeABICallerSaveRegisters;
113    }
114
115    @Override
116    public String getArchitecture() {
117        return "SPARC";
118    }
119
120    @Override
121    public String getGraalRuntimeName() {
122        return "basic";
123    }
124
125    @Override
126    public String toString() {
127        return getGraalRuntimeName() + ":" + getArchitecture();
128    }
129}