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.BytecodeFrame.isPlaceholderBci;
  26 
  27 import java.util.ArrayList;
  28 import java.util.List;
  29 
  30 import org.graalvm.compiler.api.replacements.MethodSubstitution;
  31 import org.graalvm.compiler.api.replacements.Snippet;
  32 import org.graalvm.compiler.core.gen.DebugInfoBuilder;
  33 import org.graalvm.compiler.graph.GraalGraphError;
  34 import org.graalvm.compiler.graph.NodeSourcePosition;
  35 import org.graalvm.compiler.lir.VirtualStackSlot;
  36 import org.graalvm.compiler.nodes.FrameState;
  37 import org.graalvm.compiler.nodes.ValueNode;
  38 import org.graalvm.compiler.nodes.spi.NodeValueMap;
  39 
  40 import jdk.vm.ci.code.BytecodeFrame;
  41 import jdk.vm.ci.code.StackLockValue;
  42 import jdk.vm.ci.code.VirtualObject;
  43 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
  44 import jdk.vm.ci.meta.JavaValue;
  45 import jdk.vm.ci.meta.MetaUtil;
  46 import jdk.vm.ci.meta.ResolvedJavaMethod;
  47 
  48 /**
  49  * Extends {@link DebugInfoBuilder} to allocate the extra debug information required for locks.
  50  */
  51 public class HotSpotDebugInfoBuilder extends DebugInfoBuilder {
  52 
  53     private final HotSpotLockStack lockStack;
  54 
  55     private int maxInterpreterFrameSize;
  56 
  57     private HotSpotCodeCacheProvider codeCacheProvider;
  58 
  59     public HotSpotDebugInfoBuilder(NodeValueMap nodeValueMap, HotSpotLockStack lockStack, HotSpotLIRGenerator gen) {
  60         super(nodeValueMap);
  61         this.lockStack = lockStack;
  62         this.codeCacheProvider = gen.getProviders().getCodeCache();
  63     }
  64 
  65     public HotSpotLockStack lockStack() {
  66         return lockStack;
  67     }
  68 
  69     public int maxInterpreterFrameSize() {
  70         return maxInterpreterFrameSize;
  71     }
  72 
  73     @Override
  74     protected JavaValue computeLockValue(FrameState state, int lockIndex) {
  75         int lockDepth = lockIndex;
  76         if (state.outerFrameState() != null) {
  77             lockDepth += state.outerFrameState().nestedLockDepth();
  78         }
  79         VirtualStackSlot slot = lockStack.makeLockSlot(lockDepth);
  80         ValueNode lock = state.lockAt(lockIndex);
  81         JavaValue object = toJavaValue(lock);
  82         boolean eliminated = object instanceof VirtualObject || state.monitorIdAt(lockIndex).isEliminated();
  83         assert state.monitorIdAt(lockIndex).getLockDepth() == lockDepth;
  84         return new StackLockValue(object, slot, eliminated);
  85     }
  86 
  87     @Override
  88     protected BytecodeFrame computeFrameForState(FrameState state) {
  89         if (isPlaceholderBci(state.bci) && state.bci != BytecodeFrame.BEFORE_BCI) {
  90             raiseInvalidFrameStateError(state);
  91         }
  92         BytecodeFrame result = super.computeFrameForState(state);
  93         maxInterpreterFrameSize = Math.max(maxInterpreterFrameSize, codeCacheProvider.interpreterFrameSize(result));
  94         return result;
  95     }
  96 
  97     protected void raiseInvalidFrameStateError(FrameState state) throws GraalGraphError {
  98         // This is a hard error since an incorrect state could crash hotspot
  99         NodeSourcePosition sourcePosition = state.getNodeSourcePosition();
 100         List<String> context = new ArrayList<>();
 101         ResolvedJavaMethod replacementMethodWithProblematicSideEffect = null;
 102         if (sourcePosition != null) {
 103             NodeSourcePosition pos = sourcePosition;
 104             while (pos != null) {
 105                 StringBuilder sb = new StringBuilder("parsing ");
 106                 ResolvedJavaMethod method = pos.getMethod();
 107                 MetaUtil.appendLocation(sb, method, pos.getBCI());
 108                 if (method.getAnnotation(MethodSubstitution.class) != null ||
 109                                 method.getAnnotation(Snippet.class) != null) {
 110                     replacementMethodWithProblematicSideEffect = method;
 111                 }
 112                 context.add(sb.toString());
 113                 pos = pos.getCaller();
 114             }
 115         }
 116         String message = "Invalid frame state " + state;
 117         if (replacementMethodWithProblematicSideEffect != null) {
 118             message += " associated with a side effect in " + replacementMethodWithProblematicSideEffect.format("%H.%n(%p)") + " at a position that cannot be deoptimized to";
 119         }
 120         GraalGraphError error = new GraalGraphError(message);
 121         for (String c : context) {
 122             error.addContext(c);
 123         }
 124         throw error;
 125     }
 126 }