frame.h 8.62 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_FRAME_H_
#define V8_COMPILER_FRAME_H_

8
#include "src/bit-vector.h"
9
#include "src/frames.h"
10 11 12 13 14

namespace v8 {
namespace internal {
namespace compiler {

15 16
class CallDescriptor;

17 18
// Collects the spill slot and other frame slot requirements for a compiled
// function. Frames are usually populated by the register allocator and are used
19 20 21 22 23
// by Linkage to generate code for the prologue and epilogue to compiled
// code. Frame objects must be considered immutable once they've been
// instantiated and the basic information about the frame has been collected
// into them. Mutable state associated with the frame is stored separately in
// FrameAccessState.
24
//
25
// Frames are divided up into three regions.
26 27 28 29 30 31 32 33 34 35
// - The first is the fixed header, which always has a constant size and can be
//   predicted before code generation begins depending on the type of code being
//   generated.
// - The second is the region for spill slots, which is immediately below the
//   fixed header and grows as the register allocator needs to spill to the
//   stack and asks the frame for more space.
// - The third region, which contains the callee-saved registers must be
//   reserved after register allocation, since its size can only be precisely
//   determined after register allocation once the number of used callee-saved
//   register is certain.
36
//
37 38 39 40
// The frame region immediately below the fixed header contains spill slots
// starting at slot 4 for JSFunctions.  The callee-saved frame region below that
// starts at 4+spill_slot_count_.  Callee stack slots corresponding to
// parameters are accessible through negative slot ids.
41 42 43 44 45 46 47 48
//
// Every slot of a caller or callee frame is accessible by the register
// allocator and gap resolver with a SpillSlotOperand containing its
// corresponding slot id.
//
// Below an example JSFunction Frame with slot ids, frame regions and contents:
//
//  slot      JS frame
49 50 51 52 53 54 55 56 57 58 59 60 61
//       +-----------------+--------------------------------
//  -n-1 |   parameter 0   |                            ^
//       |- - - - - - - - -|                            |
//  -n   |                 |                          Caller
//  ...  |       ...       |                       frame slots
//  -2   |  parameter n-1  |                       (slot < 0)
//       |- - - - - - - - -|                            |
//  -1   |   parameter n   |                            v
//  -----+-----------------+--------------------------------
//   0   |   return addr   |   ^                        ^
//       |- - - - - - - - -|   |                        |
//   1   | saved frame ptr | Fixed                      |
//       |- - - - - - - - -| Header <-- frame ptr       |
62
//   2   |Context/Frm. Type|   |                        |
63
//       |- - - - - - - - -|   |                        |
64
//   3   |   [JSFunction]  |   v                        |
65 66 67 68 69
//       +-----------------+----                        |
//   4   |    spill 1      |   ^                      Callee
//       |- - - - - - - - -|   |                   frame slots
//  ...  |      ...        | Spill slots           (slot >= 0)
//       |- - - - - - - - -|   |                        |
70
//  m+3  |    spill m      |   v                        |
71
//       +-----------------+----                        |
72
//  m+4  |  callee-saved 1 |   ^                        |
73 74 75
//       |- - - - - - - - -|   |                        |
//       |      ...        | Callee-saved               |
//       |- - - - - - - - -|   |                        |
76
// m+r+3 |  callee-saved r |   v                        v
77
//  -----+-----------------+----- <-- stack ptr -------------
78
//
79
class Frame : public ZoneObject {
80
 public:
81
  explicit Frame(int fixed_frame_size_in_slots);
82

83
  inline int GetTotalFrameSlotCount() const { return frame_slot_count_; }
84

85
  inline int GetSpillSlotCount() const { return spill_slot_count_; }
86

87
  void SetAllocatedRegisters(BitVector* regs) {
88
    DCHECK(allocated_registers_ == nullptr);
89 90 91 92
    allocated_registers_ = regs;
  }

  void SetAllocatedDoubleRegisters(BitVector* regs) {
93
    DCHECK(allocated_double_registers_ == nullptr);
94 95 96
    allocated_double_registers_ = regs;
  }

97
  bool DidAllocateDoubleRegisters() const {
98 99 100
    return !allocated_double_registers_->IsEmpty();
  }

101 102 103 104 105 106 107 108 109
  void AlignSavedCalleeRegisterSlots(int alignment = kDoubleSize) {
    int alignment_slots = alignment / kPointerSize;
    int delta = alignment_slots - (frame_slot_count_ & (alignment_slots - 1));
    if (delta != alignment_slots) {
      frame_slot_count_ += delta;
    }
    spill_slot_count_ += delta;
  }

110 111 112
  void AllocateSavedCalleeRegisterSlots(int count) {
    frame_slot_count_ += count;
  }
113

114 115 116
  int AllocateSpillSlot(int width) {
    int frame_slot_count_before = frame_slot_count_;
    int slot = AllocateAlignedFrameSlot(width);
117
    spill_slot_count_ += (frame_slot_count_ - frame_slot_count_before);
118
    return slot;
119 120
  }

121 122
  int AlignFrame(int alignment = kDoubleSize);

123
  int ReserveSpillSlots(size_t slot_count) {
124 125
    DCHECK_EQ(0, spill_slot_count_);
    spill_slot_count_ += static_cast<int>(slot_count);
126 127 128
    frame_slot_count_ += static_cast<int>(slot_count);
    return frame_slot_count_ - 1;
  }
129

130 131
  static const int kContextSlot = 2 + StandardFrameConstants::kCPSlotCount;
  static const int kJSFunctionSlot = 3 + StandardFrameConstants::kCPSlotCount;
132

133 134
 private:
  int AllocateAlignedFrameSlot(int width) {
135 136 137 138 139 140 141 142 143 144 145 146 147 148
    DCHECK(width == 4 || width == 8 || width == 16);
    if (kPointerSize == 4) {
      // Skip one slot if necessary.
      if (width > kPointerSize) {
        frame_slot_count_++;
        frame_slot_count_ |= 1;
        // 2 extra slots if width == 16.
        frame_slot_count_ += (width & 16) / 8;
      }
    } else {
      // No alignment when slots are 8 bytes.
      DCHECK_EQ(8, kPointerSize);
      // 1 extra slot if width == 16.
      frame_slot_count_ += (width & 16) / 16;
149
    }
150
    return frame_slot_count_++;
151 152
  }

153
 private:
154
  int frame_slot_count_;
155
  int spill_slot_count_;
156 157
  BitVector* allocated_registers_;
  BitVector* allocated_double_registers_;
158 159

  DISALLOW_COPY_AND_ASSIGN(Frame);
160 161 162 163 164 165 166 167 168 169 170
};


// Represents an offset from either the stack pointer or frame pointer.
class FrameOffset {
 public:
  inline bool from_stack_pointer() { return (offset_ & 1) == kFromSp; }
  inline bool from_frame_pointer() { return (offset_ & 1) == kFromFp; }
  inline int offset() { return offset_ & ~1; }

  inline static FrameOffset FromStackPointer(int offset) {
171
    DCHECK((offset & 1) == 0);
172 173 174 175
    return FrameOffset(offset | kFromSp);
  }

  inline static FrameOffset FromFramePointer(int offset) {
176
    DCHECK((offset & 1) == 0);
177 178 179 180 181 182 183 184 185 186 187
    return FrameOffset(offset | kFromFp);
  }

 private:
  explicit FrameOffset(int offset) : offset_(offset) {}

  int offset_;  // Encodes SP or FP in the low order bit.

  static const int kFromSp = 1;
  static const int kFromFp = 0;
};
188 189 190 191 192

// Encapsulates the mutable state maintained during code generation about the
// current function's frame.
class FrameAccessState : public ZoneObject {
 public:
193
  explicit FrameAccessState(const Frame* const frame)
194 195 196 197
      : frame_(frame),
        access_frame_with_fp_(false),
        sp_delta_(0),
        has_frame_(false) {}
198

199
  const Frame* frame() const { return frame_; }
200
  void MarkHasFrame(bool state);
201 202 203 204 205 206

  int sp_delta() const { return sp_delta_; }
  void ClearSPDelta() { sp_delta_ = 0; }
  void IncreaseSPDelta(int amount) { sp_delta_ += amount; }

  bool access_frame_with_fp() const { return access_frame_with_fp_; }
207 208 209 210 211

  // Regardless of how we access slots on the stack - using sp or fp - do we
  // have a frame, at the current stage in code generation.
  bool has_frame() const { return has_frame_; }

212 213 214 215
  void SetFrameAccessToDefault();
  void SetFrameAccessToFP() { access_frame_with_fp_ = true; }
  void SetFrameAccessToSP() { access_frame_with_fp_ = false; }

216
  int GetSPToFPSlotCount() const {
217 218 219 220
    int frame_slot_count =
        (has_frame() ? frame()->GetTotalFrameSlotCount() : kElidedFrameSlots) -
        StandardFrameConstants::kFixedSlotCountAboveFp;
    return frame_slot_count + sp_delta();
221 222 223
  }
  int GetSPToFPOffset() const { return GetSPToFPSlotCount() * kPointerSize; }

224 225 226 227 228 229 230
  // Get the frame offset for a given spill slot. The location depends on the
  // calling convention and the specific frame layout, and may thus be
  // architecture-specific. Negative spill slots indicate arguments on the
  // caller's frame.
  FrameOffset GetFrameOffset(int spill_slot) const;

 private:
231
  const Frame* const frame_;
232 233
  bool access_frame_with_fp_;
  int sp_delta_;
234
  bool has_frame_;
235
};
236 237 238
}  // namespace compiler
}  // namespace internal
}  // namespace v8
239 240

#endif  // V8_COMPILER_FRAME_H_