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.lir.amd64;
  24 
  25 import static jdk.vm.ci.code.ValueUtil.asStackSlot;
  26 
  27 import org.graalvm.compiler.asm.NumUtil;
  28 import org.graalvm.compiler.core.common.LIRKind;
  29 import org.graalvm.compiler.lir.framemap.FrameMap;
  30 
  31 import jdk.vm.ci.amd64.AMD64Kind;
  32 import jdk.vm.ci.code.CodeCacheProvider;
  33 import jdk.vm.ci.code.RegisterConfig;
  34 import jdk.vm.ci.code.StackSlot;
  35 
  36 /**
  37  * AMD64 specific frame map.
  38  *
  39  * This is the format of an AMD64 stack frame:
  40  *
  41  * <pre>
  42  *   Base       Contents
  43  *
  44  *            :                                :  -----
  45  *   caller   | incoming overflow argument n   |    ^
  46  *   frame    :     ...                        :    | positive
  47  *            | incoming overflow argument 0   |    | offsets
  48  *   ---------+--------------------------------+---------------------
  49  *            | return address                 |    |            ^
  50  *   current  +--------------------------------+    |            |    -----
  51  *   frame    |                                |    |            |      ^
  52  *            : callee save area               :    |            |      |
  53  *            |                                |    |            |      |
  54  *            +--------------------------------+    |            |      |
  55  *            | spill slot 0                   |    | negative   |      |
  56  *            :     ...                        :    v offsets    |      |
  57  *            | spill slot n                   |  -----        total  frame
  58  *            +--------------------------------+               frame  size
  59  *            | alignment padding              |               size     |
  60  *            +--------------------------------+  -----          |      |
  61  *            | outgoing overflow argument n   |    ^            |      |
  62  *            :     ...                        :    | positive   |      |
  63  *            | outgoing overflow argument 0   |    | offsets    v      v
  64  *    %sp--&gt;  +--------------------------------+---------------------------
  65  *
  66  * </pre>
  67  *
  68  * The spill slot area also includes stack allocated memory blocks (ALLOCA blocks). The size of such
  69  * a block may be greater than the size of a normal spill slot or the word size.
  70  * <p>
  71  * A runtime can reserve space at the beginning of the overflow argument area. The calling
  72  * convention can specify that the first overflow stack argument is not at offset 0, but at a
  73  * specified offset. Use {@link CodeCacheProvider#getMinimumOutgoingSize()} to make sure that
  74  * call-free methods also have this space reserved. Then the VM can use the memory at offset 0
  75  * relative to the stack pointer.
  76  */
  77 public class AMD64FrameMap extends FrameMap {
  78 
  79     private StackSlot rbpSpillSlot;
  80 
  81     public AMD64FrameMap(CodeCacheProvider codeCache, RegisterConfig registerConfig, ReferenceMapBuilderFactory referenceMapFactory) {
  82         super(codeCache, registerConfig, referenceMapFactory);
  83         // (negative) offset relative to sp + total frame size
  84         initialSpillSize = returnAddressSize();
  85         spillSize = initialSpillSize;
  86     }
  87 
  88     @Override
  89     public int totalFrameSize() {
  90         return frameSize() + returnAddressSize();
  91     }
  92 
  93     @Override
  94     public int currentFrameSize() {
  95         return alignFrameSize(outgoingSize + spillSize - returnAddressSize());
  96     }
  97 
  98     @Override
  99     protected int alignFrameSize(int size) {
 100         return NumUtil.roundUp(size + returnAddressSize(), getTarget().stackAlignment) - returnAddressSize();
 101     }
 102 
 103     @Override
 104     public int offsetForStackSlot(StackSlot slot) {
 105         // @formatter:off
 106         assert (!slot.getRawAddFrameSize() && slot.getRawOffset() <  outgoingSize) ||
 107                (slot.getRawAddFrameSize() && slot.getRawOffset()  <  0 && -slot.getRawOffset() <= spillSize) ||
 108                (slot.getRawAddFrameSize() && slot.getRawOffset()  >= 0) :
 109                    String.format("RawAddFrameSize: %b RawOffset: 0x%x spillSize: 0x%x outgoingSize: 0x%x", slot.getRawAddFrameSize(), slot.getRawOffset(), spillSize, outgoingSize);
 110         // @formatter:on
 111         return super.offsetForStackSlot(slot);
 112     }
 113 
 114     /**
 115      * For non-leaf methods, RBP is preserved in the special stack slot required by the HotSpot
 116      * runtime for walking/inspecting frames of such methods.
 117      */
 118     StackSlot allocateRBPSpillSlot() {
 119         assert spillSize == initialSpillSize : "RBP spill slot must be the first allocated stack slots";
 120         rbpSpillSlot = allocateSpillSlot(LIRKind.value(AMD64Kind.QWORD));
 121         assert asStackSlot(rbpSpillSlot).getRawOffset() == -16 : asStackSlot(rbpSpillSlot).getRawOffset();
 122         return rbpSpillSlot;
 123     }
 124 
 125     void freeRBPSpillSlot() {
 126         int size = spillSlotSize(LIRKind.value(AMD64Kind.QWORD));
 127         assert spillSize == NumUtil.roundUp(initialSpillSize + size, size) : "RBP spill slot can not be freed after allocation other stack slots";
 128         spillSize = initialSpillSize;
 129     }
 130 
 131     public StackSlot allocateDeoptimizationRescueSlot() {
 132         assert spillSize == initialSpillSize || spillSize == initialSpillSize +
 133                         spillSlotSize(LIRKind.value(AMD64Kind.QWORD)) : "Deoptimization rescue slot must be the first or second (if there is an RBP spill slot) stack slot";
 134         return allocateSpillSlot(LIRKind.value(AMD64Kind.QWORD));
 135     }
 136 }