1 /*
   2  * Copyright (c) 2012, 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 
  24 
  25 package org.graalvm.compiler.hotspot.amd64;
  26 
  27 import static jdk.vm.ci.common.InitTimer.timer;
  28 import static org.graalvm.compiler.hotspot.HotSpotBackend.Options.GraalArithmeticStubs;
  29 
  30 import java.util.ArrayList;
  31 import java.util.List;
  32 
  33 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  34 import org.graalvm.compiler.bytecode.BytecodeProvider;
  35 import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
  36 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  37 import org.graalvm.compiler.hotspot.HotSpotBackend;
  38 import org.graalvm.compiler.hotspot.HotSpotBackendFactory;
  39 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  40 import org.graalvm.compiler.hotspot.HotSpotReplacementsImpl;
  41 import org.graalvm.compiler.hotspot.meta.AddressLoweringHotSpotSuitesProvider;
  42 import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProvider;
  43 import org.graalvm.compiler.hotspot.meta.HotSpotGraalConstantFieldProvider;
  44 import org.graalvm.compiler.hotspot.meta.HotSpotGraphBuilderPlugins;
  45 import org.graalvm.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
  46 import org.graalvm.compiler.hotspot.meta.HotSpotLoweringProvider;
  47 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  48 import org.graalvm.compiler.hotspot.meta.HotSpotRegisters;
  49 import org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider;
  50 import org.graalvm.compiler.hotspot.meta.HotSpotSnippetReflectionProvider;
  51 import org.graalvm.compiler.hotspot.meta.HotSpotStampProvider;
  52 import org.graalvm.compiler.hotspot.meta.HotSpotSuitesProvider;
  53 import org.graalvm.compiler.hotspot.word.HotSpotWordTypes;
  54 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  55 import org.graalvm.compiler.nodes.spi.Replacements;
  56 import org.graalvm.compiler.options.OptionValues;
  57 import org.graalvm.compiler.phases.common.AddressLoweringPhase;
  58 import org.graalvm.compiler.phases.tiers.CompilerConfiguration;
  59 import org.graalvm.compiler.phases.util.Providers;
  60 import org.graalvm.compiler.replacements.amd64.AMD64GraphBuilderPlugins;
  61 import org.graalvm.compiler.replacements.classfile.ClassfileBytecodeProvider;
  62 import org.graalvm.compiler.serviceprovider.ServiceProvider;
  63 import org.graalvm.compiler.word.WordTypes;
  64 
  65 import jdk.vm.ci.amd64.AMD64;
  66 import jdk.vm.ci.code.Architecture;
  67 import jdk.vm.ci.code.Register;
  68 import jdk.vm.ci.code.RegisterConfig;
  69 import jdk.vm.ci.code.TargetDescription;
  70 import jdk.vm.ci.common.InitTimer;
  71 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
  72 import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
  73 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  74 import jdk.vm.ci.hotspot.HotSpotMetaAccessProvider;
  75 import jdk.vm.ci.meta.Value;
  76 import jdk.vm.ci.runtime.JVMCIBackend;
  77 
  78 @ServiceProvider(HotSpotBackendFactory.class)
  79 public class AMD64HotSpotBackendFactory implements HotSpotBackendFactory {
  80 
  81     @Override
  82     public String getName() {
  83         return "community";
  84     }
  85 
  86     @Override
  87     public Class<? extends Architecture> getArchitecture() {
  88         return AMD64.class;
  89     }
  90 
  91     @Override
  92     @SuppressWarnings("try")
  93     public HotSpotBackend createBackend(HotSpotGraalRuntimeProvider graalRuntime, CompilerConfiguration compilerConfiguration, HotSpotJVMCIRuntime jvmciRuntime, HotSpotBackend host) {
  94         assert host == null;
  95 
  96         OptionValues options = graalRuntime.getOptions();
  97         JVMCIBackend jvmci = jvmciRuntime.getHostJVMCIBackend();
  98         GraalHotSpotVMConfig config = graalRuntime.getVMConfig();
  99         HotSpotProviders providers;
 100         HotSpotRegistersProvider registers;
 101         HotSpotCodeCacheProvider codeCache = (HotSpotCodeCacheProvider) jvmci.getCodeCache();
 102         TargetDescription target = codeCache.getTarget();
 103         HotSpotHostForeignCallsProvider foreignCalls;
 104         Value[] nativeABICallerSaveRegisters;
 105         HotSpotMetaAccessProvider metaAccess = (HotSpotMetaAccessProvider) jvmci.getMetaAccess();
 106         HotSpotConstantReflectionProvider constantReflection = (HotSpotConstantReflectionProvider) jvmci.getConstantReflection();
 107         ConstantFieldProvider constantFieldProvider = new HotSpotGraalConstantFieldProvider(config, metaAccess);
 108         HotSpotLoweringProvider lowerer;
 109         HotSpotSnippetReflectionProvider snippetReflection;
 110         HotSpotReplacementsImpl replacements;
 111         HotSpotSuitesProvider suites;
 112         HotSpotWordTypes wordTypes;
 113         Plugins plugins;
 114         BytecodeProvider bytecodeProvider;
 115         try (InitTimer t = timer("create providers")) {
 116             try (InitTimer rt = timer("create HotSpotRegisters provider")) {
 117                 registers = createRegisters();
 118             }
 119             try (InitTimer rt = timer("create NativeABICallerSaveRegisters")) {
 120                 nativeABICallerSaveRegisters = createNativeABICallerSaveRegisters(config, codeCache.getRegisterConfig());
 121             }
 122             try (InitTimer rt = timer("create WordTypes")) {
 123                 wordTypes = new HotSpotWordTypes(metaAccess, target.wordJavaKind);
 124             }
 125             try (InitTimer rt = timer("create ForeignCalls provider")) {
 126                 foreignCalls = createForeignCalls(jvmciRuntime, graalRuntime, metaAccess, codeCache, wordTypes, nativeABICallerSaveRegisters);
 127             }
 128             try (InitTimer rt = timer("create Lowerer provider")) {
 129                 lowerer = createLowerer(graalRuntime, metaAccess, foreignCalls, registers, constantReflection, target);
 130             }
 131             HotSpotStampProvider stampProvider = new HotSpotStampProvider();
 132             Providers p = new Providers(metaAccess, codeCache, constantReflection, constantFieldProvider, foreignCalls, lowerer, null, stampProvider);
 133 
 134             try (InitTimer rt = timer("create SnippetReflection provider")) {
 135                 snippetReflection = createSnippetReflection(graalRuntime, constantReflection, wordTypes);
 136             }
 137             try (InitTimer rt = timer("create Bytecode provider")) {
 138                 bytecodeProvider = new ClassfileBytecodeProvider(metaAccess, snippetReflection);
 139             }
 140             try (InitTimer rt = timer("create Replacements provider")) {
 141                 replacements = createReplacements(options, p, snippetReflection, bytecodeProvider);
 142             }
 143             try (InitTimer rt = timer("create GraphBuilderPhase plugins")) {
 144                 plugins = createGraphBuilderPlugins(compilerConfiguration, config, options, target, constantReflection, foreignCalls, metaAccess, snippetReflection, replacements, wordTypes);
 145                 replacements.setGraphBuilderPlugins(plugins);
 146             }
 147             try (InitTimer rt = timer("create Suites provider")) {
 148                 suites = createSuites(config, graalRuntime, compilerConfiguration, plugins, registers, replacements, options);
 149             }
 150             providers = new HotSpotProviders(metaAccess, codeCache, constantReflection, constantFieldProvider, foreignCalls, lowerer, replacements, suites, registers,
 151                             snippetReflection, wordTypes,
 152                             plugins);
 153         }
 154         try (InitTimer rt = timer("instantiate backend")) {
 155             return createBackend(config, graalRuntime, providers);
 156         }
 157     }
 158 
 159     protected Plugins createGraphBuilderPlugins(CompilerConfiguration compilerConfiguration, GraalHotSpotVMConfig config, OptionValues options, TargetDescription target,
 160                     HotSpotConstantReflectionProvider constantReflection, HotSpotHostForeignCallsProvider foreignCalls, HotSpotMetaAccessProvider metaAccess,
 161                     HotSpotSnippetReflectionProvider snippetReflection, HotSpotReplacementsImpl replacements, HotSpotWordTypes wordTypes) {
 162         Plugins plugins = HotSpotGraphBuilderPlugins.create(compilerConfiguration, config, wordTypes, metaAccess, constantReflection, snippetReflection, foreignCalls, replacements);
 163         AMD64GraphBuilderPlugins.register(plugins, replacements.getDefaultReplacementBytecodeProvider(), (AMD64) target.arch, GraalArithmeticStubs.getValue(options), false);
 164         return plugins;
 165     }
 166 
 167     protected AMD64HotSpotBackend createBackend(GraalHotSpotVMConfig config, HotSpotGraalRuntimeProvider runtime, HotSpotProviders providers) {
 168         return new AMD64HotSpotBackend(config, runtime, providers);
 169     }
 170 
 171     protected HotSpotRegistersProvider createRegisters() {
 172         return new HotSpotRegisters(AMD64.r15, AMD64.r12, AMD64.rsp);
 173     }
 174 
 175     protected HotSpotReplacementsImpl createReplacements(OptionValues options, Providers p, SnippetReflectionProvider snippetReflection, BytecodeProvider bytecodeProvider) {
 176         return new HotSpotReplacementsImpl(options, p, snippetReflection, bytecodeProvider, p.getCodeCache().getTarget());
 177     }
 178 
 179     protected AMD64HotSpotForeignCallsProvider createForeignCalls(HotSpotJVMCIRuntime jvmciRuntime, HotSpotGraalRuntimeProvider runtime, HotSpotMetaAccessProvider metaAccess,
 180                     HotSpotCodeCacheProvider codeCache, WordTypes wordTypes, Value[] nativeABICallerSaveRegisters) {
 181         return new AMD64HotSpotForeignCallsProvider(jvmciRuntime, runtime, metaAccess, codeCache, wordTypes, nativeABICallerSaveRegisters);
 182     }
 183 
 184     /**
 185      * @param replacements
 186      */
 187     protected HotSpotSuitesProvider createSuites(GraalHotSpotVMConfig config, HotSpotGraalRuntimeProvider runtime, CompilerConfiguration compilerConfiguration, Plugins plugins,
 188                     HotSpotRegistersProvider registers, Replacements replacements, OptionValues options) {
 189         return new AddressLoweringHotSpotSuitesProvider(new AMD64HotSpotSuitesCreator(compilerConfiguration, plugins), config, runtime,
 190                         new AddressLoweringPhase(new AMD64HotSpotAddressLowering(config, registers.getHeapBaseRegister(), options)));
 191     }
 192 
 193     protected HotSpotSnippetReflectionProvider createSnippetReflection(HotSpotGraalRuntimeProvider runtime, HotSpotConstantReflectionProvider constantReflection, WordTypes wordTypes) {
 194         return new HotSpotSnippetReflectionProvider(runtime, constantReflection, wordTypes);
 195     }
 196 
 197     protected HotSpotLoweringProvider createLowerer(HotSpotGraalRuntimeProvider runtime, HotSpotMetaAccessProvider metaAccess, HotSpotForeignCallsProvider foreignCalls,
 198                     HotSpotRegistersProvider registers, HotSpotConstantReflectionProvider constantReflection, TargetDescription target) {
 199         return new AMD64HotSpotLoweringProvider(runtime, metaAccess, foreignCalls, registers, constantReflection, target);
 200     }
 201 
 202     protected Value[] createNativeABICallerSaveRegisters(GraalHotSpotVMConfig config, RegisterConfig regConfig) {
 203         List<Register> callerSave = new ArrayList<>(regConfig.getAllocatableRegisters().asList());
 204         if (config.windowsOs) {
 205             // http://msdn.microsoft.com/en-us/library/9z1stfyw.aspx
 206             callerSave.remove(AMD64.rdi);
 207             callerSave.remove(AMD64.rsi);
 208             callerSave.remove(AMD64.rbx);
 209             callerSave.remove(AMD64.rbp);
 210             callerSave.remove(AMD64.rsp);
 211             callerSave.remove(AMD64.r12);
 212             callerSave.remove(AMD64.r13);
 213             callerSave.remove(AMD64.r14);
 214             callerSave.remove(AMD64.r15);
 215             callerSave.remove(AMD64.xmm6);
 216             callerSave.remove(AMD64.xmm7);
 217             callerSave.remove(AMD64.xmm8);
 218             callerSave.remove(AMD64.xmm9);
 219             callerSave.remove(AMD64.xmm10);
 220             callerSave.remove(AMD64.xmm11);
 221             callerSave.remove(AMD64.xmm12);
 222             callerSave.remove(AMD64.xmm13);
 223             callerSave.remove(AMD64.xmm14);
 224             callerSave.remove(AMD64.xmm15);
 225         } else {
 226             /*
 227              * System V Application Binary Interface, AMD64 Architecture Processor Supplement
 228              *
 229              * Draft Version 0.96
 230              *
 231              * http://www.uclibc.org/docs/psABI-x86_64.pdf
 232              *
 233              * 3.2.1
 234              *
 235              * ...
 236              *
 237              * This subsection discusses usage of each register. Registers %rbp, %rbx and %r12
 238              * through %r15 "belong" to the calling function and the called function is required to
 239              * preserve their values. In other words, a called function must preserve these
 240              * registers' values for its caller. Remaining registers "belong" to the called
 241              * function. If a calling function wants to preserve such a register value across a
 242              * function call, it must save the value in its local stack frame.
 243              */
 244             callerSave.remove(AMD64.rbp);
 245             callerSave.remove(AMD64.rbx);
 246             callerSave.remove(AMD64.r12);
 247             callerSave.remove(AMD64.r13);
 248             callerSave.remove(AMD64.r14);
 249             callerSave.remove(AMD64.r15);
 250         }
 251         Value[] nativeABICallerSaveRegisters = new Value[callerSave.size()];
 252         for (int i = 0; i < callerSave.size(); i++) {
 253             nativeABICallerSaveRegisters[i] = callerSave.get(i).asValue();
 254         }
 255         return nativeABICallerSaveRegisters;
 256     }
 257 
 258     @Override
 259     public String toString() {
 260         return "AMD64";
 261     }
 262 }