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 org.graalvm.compiler.core.gen.DebugInfoBuilder;
  28 import org.graalvm.compiler.debug.GraalError;
  29 import org.graalvm.compiler.lir.VirtualStackSlot;
  30 import org.graalvm.compiler.nodes.FrameState;
  31 import org.graalvm.compiler.nodes.ValueNode;
  32 import org.graalvm.compiler.nodes.spi.NodeValueMap;
  33 
  34 import jdk.vm.ci.code.BytecodeFrame;
  35 import jdk.vm.ci.code.StackLockValue;
  36 import jdk.vm.ci.code.VirtualObject;
  37 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
  38 import jdk.vm.ci.meta.JavaValue;
  39 
  40 /**
  41  * Extends {@link DebugInfoBuilder} to allocate the extra debug information required for locks.
  42  */
  43 public class HotSpotDebugInfoBuilder extends DebugInfoBuilder {
  44 
  45     private final HotSpotLockStack lockStack;
  46 
  47     private int maxInterpreterFrameSize;
  48 
  49     private HotSpotCodeCacheProvider codeCacheProvider;
  50 
  51     public HotSpotDebugInfoBuilder(NodeValueMap nodeValueMap, HotSpotLockStack lockStack, HotSpotLIRGenerator gen) {
  52         super(nodeValueMap);
  53         this.lockStack = lockStack;
  54         this.codeCacheProvider = gen.getProviders().getCodeCache();
  55     }
  56 
  57     public HotSpotLockStack lockStack() {
  58         return lockStack;
  59     }
  60 
  61     public int maxInterpreterFrameSize() {
  62         return maxInterpreterFrameSize;
  63     }
  64 
  65     @Override
  66     protected JavaValue computeLockValue(FrameState state, int lockIndex) {
  67         int lockDepth = lockIndex;
  68         if (state.outerFrameState() != null) {
  69             lockDepth += state.outerFrameState().nestedLockDepth();
  70         }
  71         VirtualStackSlot slot = lockStack.makeLockSlot(lockDepth);
  72         ValueNode lock = state.lockAt(lockIndex);
  73         JavaValue object = toJavaValue(lock);
  74         boolean eliminated = object instanceof VirtualObject || state.monitorIdAt(lockIndex) == null;
  75         assert state.monitorIdAt(lockIndex) == null || state.monitorIdAt(lockIndex).getLockDepth() == lockDepth;
  76         return new StackLockValue(object, slot, eliminated);
  77     }
  78 
  79     @Override
  80     protected BytecodeFrame computeFrameForState(FrameState state) {
  81         if (isPlaceholderBci(state.bci) && state.bci != BytecodeFrame.BEFORE_BCI) {
  82             // This is really a hard error since an incorrect state could crash hotspot
  83             throw GraalError.shouldNotReachHere("Invalid state " + BytecodeFrame.getPlaceholderBciName(state.bci) + " " + state);
  84         }
  85         BytecodeFrame result = super.computeFrameForState(state);
  86         maxInterpreterFrameSize = Math.max(maxInterpreterFrameSize, codeCacheProvider.interpreterFrameSize(result));
  87         return result;
  88     }
  89 }