1 /*
   2  * Copyright (c) 2012, 2018, 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 
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 
  32 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  33 import org.graalvm.compiler.bytecode.BytecodeProvider;
  34 import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
  35 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  36 import org.graalvm.compiler.hotspot.HotSpotBackend;
  37 import org.graalvm.compiler.hotspot.HotSpotBackendFactory;
  38 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  39 import org.graalvm.compiler.hotspot.HotSpotReplacementsImpl;
  40 import org.graalvm.compiler.hotspot.meta.AddressLoweringHotSpotSuitesProvider;
  41 import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProvider;
  42 import org.graalvm.compiler.hotspot.meta.HotSpotGraalConstantFieldProvider;
  43 import org.graalvm.compiler.hotspot.meta.HotSpotGraphBuilderPlugins;
  44 import org.graalvm.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
  45 import org.graalvm.compiler.hotspot.meta.HotSpotLoweringProvider;
  46 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  47 import org.graalvm.compiler.hotspot.meta.HotSpotRegisters;
  48 import org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider;
  49 import org.graalvm.compiler.hotspot.meta.HotSpotSnippetReflectionProvider;
  50 import org.graalvm.compiler.hotspot.meta.HotSpotStampProvider;
  51 import org.graalvm.compiler.hotspot.meta.HotSpotSuitesProvider;
  52 import org.graalvm.compiler.hotspot.word.HotSpotWordTypes;
  53 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
  54 import org.graalvm.compiler.nodes.spi.Replacements;
  55 import org.graalvm.compiler.options.OptionValues;
  56 import org.graalvm.compiler.phases.common.AddressLoweringPhase;
  57 import org.graalvm.compiler.phases.tiers.CompilerConfiguration;
  58 import org.graalvm.compiler.phases.util.Providers;
  59 import org.graalvm.compiler.replacements.amd64.AMD64GraphBuilderPlugins;
  60 import org.graalvm.compiler.replacements.classfile.ClassfileBytecodeProvider;
  61 import org.graalvm.compiler.serviceprovider.ServiceProvider;
  62 import org.graalvm.compiler.word.WordTypes;
  63 
  64 import jdk.vm.ci.amd64.AMD64;
  65 import jdk.vm.ci.code.Architecture;
  66 import jdk.vm.ci.code.Register;
  67 import jdk.vm.ci.code.RegisterConfig;
  68 import jdk.vm.ci.code.TargetDescription;
  69 import jdk.vm.ci.common.InitTimer;
  70 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
  71 import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
  72 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  73 import jdk.vm.ci.hotspot.HotSpotMetaAccessProvider;
  74 import jdk.vm.ci.meta.Value;
  75 import jdk.vm.ci.runtime.JVMCIBackend;
  76 
  77 @ServiceProvider(HotSpotBackendFactory.class)
  78 public class AMD64HotSpotBackendFactory implements HotSpotBackendFactory {
  79 
  80     @Override
  81     public String getName() {
  82         return "community";
  83     }
  84 
  85     @Override
  86     public Class<? extends Architecture> getArchitecture() {
  87         return AMD64.class;
  88     }
  89 
  90     @Override
  91     @SuppressWarnings("try")
  92     public HotSpotBackend createBackend(HotSpotGraalRuntimeProvider graalRuntime, CompilerConfiguration compilerConfiguration, HotSpotJVMCIRuntime jvmciRuntime, HotSpotBackend host) {
  93         assert host == null;
  94 
  95         OptionValues options = graalRuntime.getOptions();
  96         JVMCIBackend jvmci = jvmciRuntime.getHostJVMCIBackend();
  97         GraalHotSpotVMConfig config = graalRuntime.getVMConfig();
  98         HotSpotProviders providers;
  99         HotSpotRegistersProvider registers;
 100         HotSpotCodeCacheProvider codeCache = (HotSpotCodeCacheProvider) jvmci.getCodeCache();
 101         TargetDescription target = codeCache.getTarget();
 102         HotSpotHostForeignCallsProvider foreignCalls;
 103         Value[] nativeABICallerSaveRegisters;
 104         HotSpotMetaAccessProvider metaAccess = (HotSpotMetaAccessProvider) jvmci.getMetaAccess();
 105         HotSpotConstantReflectionProvider constantReflection = (HotSpotConstantReflectionProvider) jvmci.getConstantReflection();
 106         ConstantFieldProvider constantFieldProvider = new HotSpotGraalConstantFieldProvider(config, metaAccess);
 107         HotSpotLoweringProvider lowerer;
 108         HotSpotSnippetReflectionProvider snippetReflection;
 109         HotSpotReplacementsImpl replacements;
 110         HotSpotSuitesProvider suites;
 111         HotSpotWordTypes wordTypes;
 112         Plugins plugins;
 113         BytecodeProvider bytecodeProvider;
 114         try (InitTimer t = timer("create providers")) {
 115             try (InitTimer rt = timer("create HotSpotRegisters provider")) {
 116                 registers = createRegisters();
 117             }
 118             try (InitTimer rt = timer("create NativeABICallerSaveRegisters")) {
 119                 nativeABICallerSaveRegisters = createNativeABICallerSaveRegisters(config, codeCache.getRegisterConfig());
 120             }
 121             try (InitTimer rt = timer("create WordTypes")) {
 122                 wordTypes = new HotSpotWordTypes(metaAccess, target.wordJavaKind);
 123             }
 124             try (InitTimer rt = timer("create ForeignCalls provider")) {
 125                 foreignCalls = createForeignCalls(jvmciRuntime, graalRuntime, metaAccess, codeCache, wordTypes, nativeABICallerSaveRegisters);
 126             }
 127             try (InitTimer rt = timer("create Lowerer provider")) {
 128                 lowerer = createLowerer(graalRuntime, metaAccess, foreignCalls, registers, constantReflection, target);
 129             }
 130             HotSpotStampProvider stampProvider = new HotSpotStampProvider();
 131             Providers p = new Providers(metaAccess, codeCache, constantReflection, constantFieldProvider, foreignCalls, lowerer, null, stampProvider);
 132 
 133             try (InitTimer rt = timer("create SnippetReflection provider")) {
 134                 snippetReflection = createSnippetReflection(graalRuntime, constantReflection, wordTypes);
 135             }
 136             try (InitTimer rt = timer("create Bytecode provider")) {
 137                 bytecodeProvider = new ClassfileBytecodeProvider(metaAccess, snippetReflection);
 138             }
 139             try (InitTimer rt = timer("create Replacements provider")) {
 140                 replacements = createReplacements(options, p, snippetReflection, bytecodeProvider);
 141             }
 142             try (InitTimer rt = timer("create GraphBuilderPhase plugins")) {
 143                 plugins = createGraphBuilderPlugins(compilerConfiguration, config, target, constantReflection, foreignCalls, metaAccess, snippetReflection, replacements, wordTypes);
 144                 replacements.setGraphBuilderPlugins(plugins);
 145             }
 146             try (InitTimer rt = timer("create Suites provider")) {
 147                 suites = createSuites(config, graalRuntime, compilerConfiguration, plugins, registers, replacements, options);
 148             }
 149             providers = new HotSpotProviders(metaAccess, codeCache, constantReflection, constantFieldProvider, foreignCalls, lowerer, replacements, suites, registers,
 150                             snippetReflection, wordTypes,
 151                             plugins);
 152             replacements.setProviders(providers);
 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, 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, 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 }