frame.h 10.5 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 9
#include "src/base/bits.h"
#include "src/codegen/aligned-slot-allocator.h"
10
#include "src/execution/frame-constants.h"
11
#include "src/utils/bit-vector.h"
12 13 14 15 16

namespace v8 {
namespace internal {
namespace compiler {

17 18
class CallDescriptor;

19 20
// Collects the spill slot and other frame slot requirements for a compiled
// function. Frames are usually populated by the register allocator and are used
21 22 23 24 25
// 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.
26
//
27
// Frames are divided up into four regions.
28 29 30 31 32 33 34 35 36 37
// - 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.
38 39 40 41
// - The fourth region is a scratch area for return values from other functions
//   called, if multiple returns cannot all be passed in registers. This region
//   Must be last in a stack frame, so that it is positioned immediately below
//   the stack frame of a callee to store to.
42
//
43 44
// The frame region immediately below the fixed header contains spill slots
// starting at slot 4 for JSFunctions.  The callee-saved frame region below that
45 46
// starts at 4+spill_slot_count_.  Callee stack slots correspond to
// parameters that are accessible through negative slot ids.
47 48 49 50 51 52 53 54
//
// 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
55
//       +-----------------+--------------------------------
56
//  -n-1 |  parameter n    |                            ^
57
//       |- - - - - - - - -|                            |
58
//  -n   |  parameter n-1  |                          Caller
59
//  ...  |       ...       |                       frame slots
60
//  -2   |  parameter 1    |                       (slot < 0)
61
//       |- - - - - - - - -|                            |
62
//  -1   |  parameter 0    |                            v
63 64 65 66 67
//  -----+-----------------+--------------------------------
//   0   |   return addr   |   ^                        ^
//       |- - - - - - - - -|   |                        |
//   1   | saved frame ptr | Fixed                      |
//       |- - - - - - - - -| Header <-- frame ptr       |
68
//   2   |Context/Frm. Type|   |                        |
69
//       |- - - - - - - - -|   |                        |
70
//   3   |   [JSFunction]  |   v                        |
71 72 73 74 75
//       +-----------------+----                        |
//   4   |    spill 1      |   ^                      Callee
//       |- - - - - - - - -|   |                   frame slots
//  ...  |      ...        | Spill slots           (slot >= 0)
//       |- - - - - - - - -|   |                        |
76
//  m+3  |    spill m      |   v                        |
77
//       +-----------------+----                        |
78
//  m+4  |  callee-saved 1 |   ^                        |
79 80 81
//       |- - - - - - - - -|   |                        |
//       |      ...        | Callee-saved               |
//       |- - - - - - - - -|   |                        |
82 83 84 85 86 87 88
// m+r+3 |  callee-saved r |   v                        |
//       +-----------------+----                        |
// m+r+4 |    return 0     |   ^                        |
//       |- - - - - - - - -|   |                        |
//       |      ...        | Return                     |
//       |- - - - - - - - -|   |                        |
//       |    return q-1   |   v                        v
89
//  -----+-----------------+----- <-- stack ptr -------------
90
//
91
class V8_EXPORT_PRIVATE Frame : public ZoneObject {
92
 public:
93
  explicit Frame(int fixed_frame_size_in_slots);
94 95
  Frame(const Frame&) = delete;
  Frame& operator=(const Frame&) = delete;
96

97 98 99
  inline int GetTotalFrameSlotCount() const {
    return slot_allocator_.Size() + return_slot_count_;
  }
100
  inline int GetFixedSlotCount() const { return fixed_slot_count_; }
101
  inline int GetSpillSlotCount() const { return spill_slot_count_; }
102
  inline int GetReturnSlotCount() const { return return_slot_count_; }
103

104
  void SetAllocatedRegisters(BitVector* regs) {
105
    DCHECK_NULL(allocated_registers_);
106 107 108 109
    allocated_registers_ = regs;
  }

  void SetAllocatedDoubleRegisters(BitVector* regs) {
110
    DCHECK_NULL(allocated_double_registers_);
111 112 113
    allocated_double_registers_ = regs;
  }

114
  bool DidAllocateDoubleRegisters() const {
115 116 117
    return !allocated_double_registers_->IsEmpty();
  }

118
  void AlignSavedCalleeRegisterSlots(int alignment = kDoubleSize) {
119
    DCHECK(!frame_aligned_);
120 121 122 123 124 125 126 127
#if DEBUG
    spill_slots_finished_ = true;
#endif
    DCHECK(base::bits::IsPowerOfTwo(alignment));
    DCHECK_LE(alignment, kSimd128Size);
    int alignment_in_slots = AlignedSlotAllocator::NumSlotsForWidth(alignment);
    int padding = slot_allocator_.Align(alignment_in_slots);
    spill_slot_count_ += padding;
128 129
  }

130
  void AllocateSavedCalleeRegisterSlots(int count) {
131
    DCHECK(!frame_aligned_);
132 133 134 135
#if DEBUG
    spill_slots_finished_ = true;
#endif
    slot_allocator_.AllocateUnaligned(count);
136
  }
137

138
  int AllocateSpillSlot(int width, int alignment = 0) {
139
    DCHECK_EQ(GetTotalFrameSlotCount(),
140
              fixed_slot_count_ + spill_slot_count_ + return_slot_count_);
141 142
    // Never allocate spill slots after the callee-saved slots are defined.
    DCHECK(!spill_slots_finished_);
143
    DCHECK(!frame_aligned_);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    int actual_width = std::max({width, AlignedSlotAllocator::kSlotSize});
    int actual_alignment =
        std::max({alignment, AlignedSlotAllocator::kSlotSize});
    int slots = AlignedSlotAllocator::NumSlotsForWidth(actual_width);
    int old_end = slot_allocator_.Size();
    int slot;
    if (actual_width == actual_alignment) {
      // Simple allocation, alignment equal to width.
      slot = slot_allocator_.Allocate(slots);
    } else {
      // Complex allocation, alignment different from width.
      if (actual_alignment > AlignedSlotAllocator::kSlotSize) {
        // Alignment required.
        int alignment_in_slots =
            AlignedSlotAllocator::NumSlotsForWidth(actual_alignment);
        slot_allocator_.Align(alignment_in_slots);
      }
      slot = slot_allocator_.AllocateUnaligned(slots);
162
    }
163 164 165 166
    int end = slot_allocator_.Size();

    spill_slot_count_ += end - old_end;
    return slot + slots - 1;
167 168 169
  }

  void EnsureReturnSlots(int count) {
170
    DCHECK(!frame_aligned_);
171
    return_slot_count_ = std::max(return_slot_count_, count);
172 173
  }

174
  void AlignFrame(int alignment = kDoubleSize);
175

176
  int ReserveSpillSlots(size_t slot_count) {
177
    DCHECK_EQ(0, spill_slot_count_);
178
    DCHECK(!frame_aligned_);
179
    spill_slot_count_ += static_cast<int>(slot_count);
180 181
    slot_allocator_.AllocateUnaligned(static_cast<int>(slot_count));
    return slot_allocator_.Size() - 1;
182 183
  }

184
 private:
185
  int fixed_slot_count_;
186 187 188 189 190
  int spill_slot_count_ = 0;
  // Account for return slots separately. Conceptually, they follow all
  // allocated spill slots.
  int return_slot_count_ = 0;
  AlignedSlotAllocator slot_allocator_;
191 192
  BitVector* allocated_registers_;
  BitVector* allocated_double_registers_;
193 194
#if DEBUG
  bool spill_slots_finished_ = false;
195
  bool frame_aligned_ = false;
196
#endif
197 198 199 200 201 202 203 204 205 206
};

// 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) {
207
    DCHECK_EQ(0, offset & 1);
208 209 210 211
    return FrameOffset(offset | kFromSp);
  }

  inline static FrameOffset FromFramePointer(int offset) {
212
    DCHECK_EQ(0, offset & 1);
213 214 215 216 217 218 219 220 221 222 223
    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;
};
224 225 226 227 228

// Encapsulates the mutable state maintained during code generation about the
// current function's frame.
class FrameAccessState : public ZoneObject {
 public:
229
  explicit FrameAccessState(const Frame* const frame)
230 231 232 233
      : frame_(frame),
        access_frame_with_fp_(false),
        sp_delta_(0),
        has_frame_(false) {}
234

235
  const Frame* frame() const { return frame_; }
236
  V8_EXPORT_PRIVATE void MarkHasFrame(bool state);
237 238 239 240 241 242

  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_; }
243 244 245 246 247

  // 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_; }

248 249 250 251
  void SetFrameAccessToDefault();
  void SetFrameAccessToFP() { access_frame_with_fp_ = true; }
  void SetFrameAccessToSP() { access_frame_with_fp_ = false; }

252
  int GetSPToFPSlotCount() const {
253 254 255 256
    int frame_slot_count =
        (has_frame() ? frame()->GetTotalFrameSlotCount() : kElidedFrameSlots) -
        StandardFrameConstants::kFixedSlotCountAboveFp;
    return frame_slot_count + sp_delta();
257
  }
258 259 260
  int GetSPToFPOffset() const {
    return GetSPToFPSlotCount() * kSystemPointerSize;
  }
261

262 263 264 265 266 267 268
  // 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:
269
  const Frame* const frame_;
270 271
  bool access_frame_with_fp_;
  int sp_delta_;
272
  bool has_frame_;
273
};
274 275 276
}  // namespace compiler
}  // namespace internal
}  // namespace v8
277 278

#endif  // V8_COMPILER_FRAME_H_