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