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