1 /*
   2  * Copyright (c) 2015, 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 jdk.vm.ci.hotspot.aarch64;
  24 
  25 import static jdk.vm.ci.common.InitTimer.timer;
  26 
  27 import java.util.EnumSet;
  28 
  29 import jdk.vm.ci.aarch64.AArch64;
  30 import jdk.vm.ci.code.Architecture;
  31 import jdk.vm.ci.code.RegisterConfig;
  32 import jdk.vm.ci.code.TargetDescription;
  33 import jdk.vm.ci.code.stack.StackIntrospection;
  34 import jdk.vm.ci.common.InitTimer;
  35 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
  36 import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
  37 import jdk.vm.ci.hotspot.HotSpotJVMCIBackendFactory;
  38 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  39 import jdk.vm.ci.hotspot.HotSpotMetaAccessProvider;
  40 import jdk.vm.ci.hotspot.HotSpotStackIntrospection;
  41 import jdk.vm.ci.meta.ConstantReflectionProvider;
  42 import jdk.vm.ci.runtime.JVMCIBackend;
  43 
  44 public class AArch64HotSpotJVMCIBackendFactory implements HotSpotJVMCIBackendFactory {
  45 
  46     protected EnumSet<AArch64.CPUFeature> computeFeatures(@SuppressWarnings("unused") AArch64HotSpotVMConfig config) {
  47         // Configure the feature set using the HotSpot flag settings.
  48         EnumSet<AArch64.CPUFeature> features = EnumSet.noneOf(AArch64.CPUFeature.class);
  49         
  50         if ((config.vmVersionFeatures & config.aarch64FP) != 0) {
  51             features.add(AArch64.CPUFeature.FP);
  52         }
  53         if ((config.vmVersionFeatures & config.aarch64ASIMD) != 0) {
  54             features.add(AArch64.CPUFeature.ASIMD);
  55         }
  56         if ((config.vmVersionFeatures & config.aarch64EVTSTRM) != 0) {
  57             features.add(AArch64.CPUFeature.EVTSTRM);
  58         }
  59         if ((config.vmVersionFeatures & config.aarch64AES) != 0) {
  60             features.add(AArch64.CPUFeature.AES);
  61         }
  62         if ((config.vmVersionFeatures & config.aarch64PMULL) != 0) {
  63             features.add(AArch64.CPUFeature.PMULL);
  64         }
  65         if ((config.vmVersionFeatures & config.aarch64SHA1) != 0) {
  66             features.add(AArch64.CPUFeature.SHA1);
  67         }
  68         if ((config.vmVersionFeatures & config.aarch64SHA2) != 0) {
  69             features.add(AArch64.CPUFeature.SHA2);
  70         }
  71         if ((config.vmVersionFeatures & config.aarch64CRC32) != 0) {
  72             features.add(AArch64.CPUFeature.CRC32);
  73         }
  74         if ((config.vmVersionFeatures & config.aarch64LSE) != 0) {
  75             features.add(AArch64.CPUFeature.LSE);
  76         }
  77         if ((config.vmVersionFeatures & config.aarch64STXR_PREFETCH) != 0) {
  78             features.add(AArch64.CPUFeature.STXR_PREFETCH);
  79         }
  80         if ((config.vmVersionFeatures & config.aarch64A53MAC) != 0) {
  81             features.add(AArch64.CPUFeature.A53MAC);
  82         }
  83         if ((config.vmVersionFeatures & config.aarch64DMB_ATOMICS) != 0) {
  84             features.add(AArch64.CPUFeature.DMB_ATOMICS);
  85         }
  86 
  87         return features;
  88     }
  89 
  90     protected EnumSet<AArch64.Flag> computeFlags(@SuppressWarnings("unused") AArch64HotSpotVMConfig config) {
  91         EnumSet<AArch64.Flag> flags = EnumSet.noneOf(AArch64.Flag.class);
  92         
  93         if (config.useBarriersForVolatile) {
  94             flags.add(AArch64.Flag.UseBarriersForVolatile);
  95         }
  96         if (config.useCRC32) {
  97             flags.add(AArch64.Flag.UseCRC32);
  98         }
  99         if (config.useNeon) {
 100             flags.add(AArch64.Flag.UseNeon);
 101         }
 102         if (config.useSIMDForMemoryOps) {
 103             flags.add(AArch64.Flag.UseSIMDForMemoryOps);
 104         }
 105         if (config.avoidUnalignedAccesses) {
 106             flags.add(AArch64.Flag.AvoidUnalignedAccesses);
 107         }
 108         if (config.useLSE) {
 109             flags.add(AArch64.Flag.UseLSE);
 110         }
 111         if (config.useBlockZeroing) {
 112             flags.add(AArch64.Flag.UseBlockZeroing);
 113         }
 114         
 115         return flags;
 116     }
 117 
 118     protected TargetDescription createTarget(AArch64HotSpotVMConfig config) {
 119         final int stackFrameAlignment = 16;
 120         final int implicitNullCheckLimit = 4096;
 121         final boolean inlineObjects = true;
 122         Architecture arch = new AArch64(computeFeatures(config), computeFlags(config));
 123         return new TargetDescription(arch, true, stackFrameAlignment, implicitNullCheckLimit, inlineObjects);
 124     }
 125 
 126     protected HotSpotConstantReflectionProvider createConstantReflection(HotSpotJVMCIRuntimeProvider runtime) {
 127         return new HotSpotConstantReflectionProvider(runtime);
 128     }
 129 
 130     protected RegisterConfig createRegisterConfig(AArch64HotSpotVMConfig config, TargetDescription target) {
 131         return new AArch64HotSpotRegisterConfig(target, config.useCompressedOops);
 132     }
 133 
 134     protected HotSpotCodeCacheProvider createCodeCache(HotSpotJVMCIRuntimeProvider runtime, TargetDescription target, RegisterConfig regConfig) {
 135         return new HotSpotCodeCacheProvider(runtime, runtime.getConfig(), target, regConfig);
 136     }
 137 
 138     protected HotSpotMetaAccessProvider createMetaAccess(HotSpotJVMCIRuntimeProvider runtime) {
 139         return new HotSpotMetaAccessProvider(runtime);
 140     }
 141 
 142     @Override
 143     public String getArchitecture() {
 144         return "aarch64";
 145     }
 146 
 147     @Override
 148     public String toString() {
 149         return "JVMCIBackend:" + getArchitecture();
 150     }
 151 
 152     @SuppressWarnings("try")
 153     public JVMCIBackend createJVMCIBackend(HotSpotJVMCIRuntimeProvider runtime, JVMCIBackend host) {
 154 
 155         assert host == null;
 156         AArch64HotSpotVMConfig config = new AArch64HotSpotVMConfig(runtime.getConfigStore());
 157         TargetDescription target = createTarget(config);
 158 
 159         RegisterConfig regConfig;
 160         HotSpotCodeCacheProvider codeCache;
 161         ConstantReflectionProvider constantReflection;
 162         HotSpotMetaAccessProvider metaAccess;
 163         StackIntrospection stackIntrospection;
 164         try (InitTimer t = timer("create providers")) {
 165             try (InitTimer rt = timer("create MetaAccess provider")) {
 166                 metaAccess = createMetaAccess(runtime);
 167             }
 168             try (InitTimer rt = timer("create RegisterConfig")) {
 169                 regConfig = createRegisterConfig(config, target);
 170             }
 171             try (InitTimer rt = timer("create CodeCache provider")) {
 172                 codeCache = createCodeCache(runtime, target, regConfig);
 173             }
 174             try (InitTimer rt = timer("create ConstantReflection provider")) {
 175                 constantReflection = createConstantReflection(runtime);
 176             }
 177             try (InitTimer rt = timer("create StackIntrospection provider")) {
 178                 stackIntrospection = new HotSpotStackIntrospection(runtime);
 179             }
 180         }
 181         try (InitTimer rt = timer("instantiate backend")) {
 182             return createBackend(metaAccess, codeCache, constantReflection, stackIntrospection);
 183         }
 184     }
 185 
 186     protected JVMCIBackend createBackend(HotSpotMetaAccessProvider metaAccess, HotSpotCodeCacheProvider codeCache, ConstantReflectionProvider constantReflection,
 187                     StackIntrospection stackIntrospection) {
 188         return new JVMCIBackend(metaAccess, codeCache, constantReflection, stackIntrospection);
 189     }
 190 }