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 org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
  26 
  27 import java.util.Arrays;
  28 
  29 import org.graalvm.compiler.core.common.LIRKind;
  30 import org.graalvm.compiler.lir.LIRInstruction;
  31 import org.graalvm.compiler.lir.LIRInstructionClass;
  32 import org.graalvm.compiler.lir.VirtualStackSlot;
  33 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  34 import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
  35 
  36 import jdk.vm.ci.meta.AllocatableValue;
  37 
  38 /**
  39  * Manages allocation and re-use of lock slots in a scoped manner. The slots are used in HotSpot's
  40  * lightweight locking mechanism to store the mark word of an object being locked.
  41  */
  42 public class HotSpotLockStack extends LIRInstruction {
  43     public static final LIRInstructionClass<HotSpotLockStack> TYPE = LIRInstructionClass.create(HotSpotLockStack.class);
  44     private static final AllocatableValue[] EMPTY = new AllocatableValue[0];
  45 
  46     @Def({STACK}) private AllocatableValue[] locks;
  47     private final FrameMapBuilder frameMapBuilder;
  48     private final LIRKind slotKind;
  49 
  50     public HotSpotLockStack(FrameMapBuilder frameMapBuilder, LIRKind slotKind) {
  51         super(TYPE);
  52         this.frameMapBuilder = frameMapBuilder;
  53         this.slotKind = slotKind;
  54         this.locks = EMPTY;
  55     }
  56 
  57     /**
  58      * Gets a stack slot for a lock at a given lock nesting depth, allocating it first if necessary.
  59      */
  60     public VirtualStackSlot makeLockSlot(int lockDepth) {
  61         if (locks == EMPTY) {
  62             locks = new AllocatableValue[lockDepth + 1];
  63         } else if (locks.length < lockDepth + 1) {
  64             locks = Arrays.copyOf(locks, lockDepth + 1);
  65         }
  66         if (locks[lockDepth] == null) {
  67             locks[lockDepth] = frameMapBuilder.allocateSpillSlot(slotKind);
  68         }
  69         return (VirtualStackSlot) locks[lockDepth];
  70     }
  71 
  72     @Override
  73     public void emitCode(CompilationResultBuilder crb) {
  74         // do nothing
  75     }
  76 }