1 /*
   2  * Copyright (c) 2010, 2014, 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 jdk.vm.ci.code;
  24 
  25 import jdk.vm.ci.meta.*;
  26 
  27 /**
  28  * Represents a compiler spill slot or an outgoing stack-based argument in a method's frame or an
  29  * incoming stack-based argument in a method's {@linkplain #isInCallerFrame() caller's frame}.
  30  */
  31 public final class StackSlot extends StackSlotValue {
  32 
  33     private final int offset;
  34     private final boolean addFrameSize;
  35 
  36     /**
  37      * Gets a {@link StackSlot} instance representing a stack slot at a given index holding a value
  38      * of a given kind.
  39      *
  40      * @param kind The kind of the value stored in the stack slot.
  41      * @param offset The offset of the stack slot (in bytes)
  42      * @param addFrameSize Specifies if the offset is relative to the stack pointer, or the
  43      *            beginning of the frame (stack pointer + total frame size).
  44      */
  45     public static StackSlot get(LIRKind kind, int offset, boolean addFrameSize) {
  46         assert addFrameSize || offset >= 0;
  47         return new StackSlot(kind, offset, addFrameSize);
  48     }
  49 
  50     /**
  51      * Private constructor to enforce use of {@link #get(LIRKind, int, boolean)} so that a cache can
  52      * be used.
  53      */
  54     private StackSlot(LIRKind kind, int offset, boolean addFrameSize) {
  55         super(kind);
  56         this.offset = offset;
  57         this.addFrameSize = addFrameSize;
  58     }
  59 
  60     /**
  61      * Gets the offset of this stack slot, relative to the stack pointer.
  62      *
  63      * @return The offset of this slot (in bytes).
  64      */
  65     public int getOffset(int totalFrameSize) {
  66         assert totalFrameSize > 0 || !addFrameSize;
  67         int result = offset + (addFrameSize ? totalFrameSize : 0);
  68         assert result >= 0;
  69         return result;
  70     }
  71 
  72     public boolean isInCallerFrame() {
  73         return addFrameSize && offset >= 0;
  74     }
  75 
  76     public int getRawOffset() {
  77         return offset;
  78     }
  79 
  80     public boolean getRawAddFrameSize() {
  81         return addFrameSize;
  82     }
  83 
  84     @Override
  85     public String toString() {
  86         if (!addFrameSize) {
  87             return "out:" + offset + getKindSuffix();
  88         } else if (offset >= 0) {
  89             return "in:" + offset + getKindSuffix();
  90         } else {
  91             return "stack:" + (-offset) + getKindSuffix();
  92         }
  93     }
  94 
  95     /**
  96      * Gets this stack slot used to pass an argument from the perspective of a caller.
  97      */
  98     public StackSlot asOutArg() {
  99         assert offset >= 0;
 100         if (addFrameSize) {
 101             return get(getLIRKind(), offset, false);
 102         }
 103         return this;
 104     }
 105 
 106     /**
 107      * Gets this stack slot used to pass an argument from the perspective of a callee.
 108      */
 109     public StackSlot asInArg() {
 110         assert offset >= 0;
 111         if (!addFrameSize) {
 112             return get(getLIRKind(), offset, true);
 113         }
 114         return this;
 115     }
 116 
 117     @Override
 118     public int hashCode() {
 119         final int prime = 37;
 120         int result = super.hashCode();
 121         result = prime * result + (addFrameSize ? 1231 : 1237);
 122         result = prime * result + offset;
 123         return result;
 124     }
 125 
 126     @Override
 127     public boolean equals(Object obj) {
 128         if (obj instanceof StackSlot) {
 129             StackSlot other = (StackSlot) obj;
 130             return super.equals(obj) && addFrameSize == other.addFrameSize && offset == other.offset;
 131         }
 132         return false;
 133     }
 134 }