1 /*
   2  * Copyright (c) 2013, 2015, 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 org.graalvm.compiler.hotspot;
  24 
  25 import static jdk.vm.ci.code.CodeUtil.getCallingConvention;
  26 import static jdk.vm.ci.common.InitTimer.timer;
  27 
  28 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  29 import org.graalvm.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
  30 import org.graalvm.compiler.hotspot.meta.HotSpotLoweringProvider;
  31 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  32 import org.graalvm.compiler.hotspot.stubs.DeoptimizationStub;
  33 import org.graalvm.compiler.hotspot.stubs.Stub;
  34 import org.graalvm.compiler.hotspot.stubs.UncommonTrapStub;
  35 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  36 import org.graalvm.compiler.lir.framemap.ReferenceMapBuilder;
  37 import org.graalvm.compiler.nodes.StructuredGraph;
  38 
  39 import jdk.vm.ci.code.CallingConvention;
  40 import jdk.vm.ci.common.InitTimer;
  41 import jdk.vm.ci.hotspot.HotSpotCallingConventionType;
  42 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  43 import jdk.vm.ci.meta.JavaType;
  44 import jdk.vm.ci.runtime.JVMCICompiler;
  45 
  46 /**
  47  * Common functionality of HotSpot host backends.
  48  */
  49 public abstract class HotSpotHostBackend extends HotSpotBackend {
  50 
  51     /**
  52      * Descriptor for {@code SharedRuntime::deopt_blob()->unpack()} or
  53      * {@link DeoptimizationStub#deoptimizationHandler} depending on
  54      * {@link HotSpotBackend.Options#PreferGraalStubs}.
  55      */
  56     public static final ForeignCallDescriptor DEOPTIMIZATION_HANDLER = new ForeignCallDescriptor("deoptHandler", void.class);
  57 
  58     /**
  59      * Descriptor for {@code SharedRuntime::deopt_blob()->uncommon_trap()} or
  60      * {@link UncommonTrapStub#uncommonTrapHandler} depending on
  61      * {@link HotSpotBackend.Options#PreferGraalStubs}.
  62      */
  63     public static final ForeignCallDescriptor UNCOMMON_TRAP_HANDLER = new ForeignCallDescriptor("uncommonTrapHandler", void.class);
  64 
  65     protected final GraalHotSpotVMConfig config;
  66 
  67     public HotSpotHostBackend(GraalHotSpotVMConfig config, HotSpotGraalRuntimeProvider runtime, HotSpotProviders providers) {
  68         super(runtime, providers);
  69         this.config = config;
  70     }
  71 
  72     @Override
  73     @SuppressWarnings("try")
  74     public void completeInitialization(HotSpotJVMCIRuntime jvmciRuntime) {
  75         final HotSpotProviders providers = getProviders();
  76         HotSpotHostForeignCallsProvider foreignCalls = (HotSpotHostForeignCallsProvider) providers.getForeignCalls();
  77         final HotSpotLoweringProvider lowerer = (HotSpotLoweringProvider) providers.getLowerer();
  78 
  79         try (InitTimer st = timer("foreignCalls.initialize")) {
  80             foreignCalls.initialize(providers);
  81         }
  82         try (InitTimer st = timer("lowerer.initialize")) {
  83             lowerer.initialize(providers, config);
  84         }
  85     }
  86 
  87     protected CallingConvention makeCallingConvention(StructuredGraph graph, Stub stub) {
  88         if (stub != null) {
  89             return stub.getLinkage().getIncomingCallingConvention();
  90         }
  91 
  92         CallingConvention cc = getCallingConvention(getCodeCache(), HotSpotCallingConventionType.JavaCallee, graph.method(), this);
  93         if (graph.getEntryBCI() != JVMCICompiler.INVOCATION_ENTRY_BCI) {
  94             // for OSR, only a pointer is passed to the method.
  95             JavaType[] parameterTypes = new JavaType[]{getMetaAccess().lookupJavaType(long.class)};
  96             CallingConvention tmp = getCodeCache().getRegisterConfig().getCallingConvention(HotSpotCallingConventionType.JavaCallee, getMetaAccess().lookupJavaType(void.class), parameterTypes, this);
  97             cc = new CallingConvention(cc.getStackSize(), cc.getReturn(), tmp.getArgument(0));
  98         }
  99         return cc;
 100     }
 101 
 102     public void emitStackOverflowCheck(CompilationResultBuilder crb) {
 103         if (config.useStackBanging) {
 104             // Each code entry causes one stack bang n pages down the stack where n
 105             // is configurable by StackShadowPages. The setting depends on the maximum
 106             // depth of VM call stack or native before going back into java code,
 107             // since only java code can raise a stack overflow exception using the
 108             // stack banging mechanism. The VM and native code does not detect stack
 109             // overflow.
 110             // The code in JavaCalls::call() checks that there is at least n pages
 111             // available, so all entry code needs to do is bang once for the end of
 112             // this shadow zone.
 113             // The entry code may need to bang additional pages if the framesize
 114             // is greater than a page.
 115 
 116             int pageSize = config.vmPageSize;
 117             int bangEnd = config.stackShadowPages * pageSize;
 118 
 119             // This is how far the previous frame's stack banging extended.
 120             int bangEndSafe = bangEnd;
 121 
 122             int frameSize = Math.max(crb.frameMap.frameSize(), crb.compilationResult.getMaxInterpreterFrameSize());
 123             if (frameSize > pageSize) {
 124                 bangEnd += frameSize;
 125             }
 126 
 127             int bangOffset = bangEndSafe;
 128             if (bangOffset <= bangEnd) {
 129                 crb.blockComment("[stack overflow check]");
 130             }
 131             while (bangOffset <= bangEnd) {
 132                 // Need at least one stack bang at end of shadow zone.
 133                 bangStackWithOffset(crb, bangOffset);
 134                 bangOffset += pageSize;
 135             }
 136         }
 137     }
 138 
 139     protected abstract void bangStackWithOffset(CompilationResultBuilder crb, int bangOffset);
 140 
 141     @Override
 142     public ReferenceMapBuilder newReferenceMapBuilder(int totalFrameSize) {
 143         return new HotSpotReferenceMapBuilder(totalFrameSize, config.maxOopMapStackOffset);
 144     }
 145 
 146 }