• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_COMPILER_FRAME_H_
6 #define V8_COMPILER_FRAME_H_
7 
8 #include "src/execution/frame-constants.h"
9 #include "src/utils/bit-vector.h"
10 
11 namespace v8 {
12 namespace internal {
13 namespace compiler {
14 
15 class CallDescriptor;
16 
17 // Collects the spill slot and other frame slot requirements for a compiled
18 // function. Frames are usually populated by the register allocator and are used
19 // by Linkage to generate code for the prologue and epilogue to compiled
20 // code. Frame objects must be considered immutable once they've been
21 // instantiated and the basic information about the frame has been collected
22 // into them. Mutable state associated with the frame is stored separately in
23 // FrameAccessState.
24 //
25 // Frames are divided up into four regions.
26 // - The first is the fixed header, which always has a constant size and can be
27 //   predicted before code generation begins depending on the type of code being
28 //   generated.
29 // - The second is the region for spill slots, which is immediately below the
30 //   fixed header and grows as the register allocator needs to spill to the
31 //   stack and asks the frame for more space.
32 // - The third region, which contains the callee-saved registers must be
33 //   reserved after register allocation, since its size can only be precisely
34 //   determined after register allocation once the number of used callee-saved
35 //   register is certain.
36 // - The fourth region is a scratch area for return values from other functions
37 //   called, if multiple returns cannot all be passed in registers. This region
38 //   Must be last in a stack frame, so that it is positioned immediately below
39 //   the stack frame of a callee to store to.
40 //
41 // The frame region immediately below the fixed header contains spill slots
42 // starting at slot 4 for JSFunctions.  The callee-saved frame region below that
43 // starts at 4+spill_slot_count_.  Callee stack slots correspond to
44 // parameters that are accessible through negative slot ids.
45 //
46 // Every slot of a caller or callee frame is accessible by the register
47 // allocator and gap resolver with a SpillSlotOperand containing its
48 // corresponding slot id.
49 //
50 // Below an example JSFunction Frame with slot ids, frame regions and contents:
51 //
52 //  slot      JS frame
53 //       +-----------------+--------------------------------
54 //  -n-1 |   parameter 0   |                            ^
55 //       |- - - - - - - - -|                            |
56 //  -n   |                 |                          Caller
57 //  ...  |       ...       |                       frame slots
58 //  -2   |  parameter n-1  |                       (slot < 0)
59 //       |- - - - - - - - -|                            |
60 //  -1   |   parameter n   |                            v
61 //  -----+-----------------+--------------------------------
62 //   0   |   return addr   |   ^                        ^
63 //       |- - - - - - - - -|   |                        |
64 //   1   | saved frame ptr | Fixed                      |
65 //       |- - - - - - - - -| Header <-- frame ptr       |
66 //   2   |Context/Frm. Type|   |                        |
67 //       |- - - - - - - - -|   |                        |
68 //   3   |   [JSFunction]  |   v                        |
69 //       +-----------------+----                        |
70 //   4   |    spill 1      |   ^                      Callee
71 //       |- - - - - - - - -|   |                   frame slots
72 //  ...  |      ...        | Spill slots           (slot >= 0)
73 //       |- - - - - - - - -|   |                        |
74 //  m+3  |    spill m      |   v                        |
75 //       +-----------------+----                        |
76 //  m+4  |  callee-saved 1 |   ^                        |
77 //       |- - - - - - - - -|   |                        |
78 //       |      ...        | Callee-saved               |
79 //       |- - - - - - - - -|   |                        |
80 // m+r+3 |  callee-saved r |   v                        |
81 //       +-----------------+----                        |
82 // m+r+4 |    return 0     |   ^                        |
83 //       |- - - - - - - - -|   |                        |
84 //       |      ...        | Return                     |
85 //       |- - - - - - - - -|   |                        |
86 //       |    return q-1   |   v                        v
87 //  -----+-----------------+----- <-- stack ptr -------------
88 //
89 class V8_EXPORT_PRIVATE Frame : public ZoneObject {
90  public:
91   explicit Frame(int fixed_frame_size_in_slots);
92   Frame(const Frame&) = delete;
93   Frame& operator=(const Frame&) = delete;
94 
GetTotalFrameSlotCount()95   inline int GetTotalFrameSlotCount() const { return frame_slot_count_; }
GetFixedSlotCount()96   inline int GetFixedSlotCount() const { return fixed_slot_count_; }
GetSpillSlotCount()97   inline int GetSpillSlotCount() const { return spill_slot_count_; }
GetReturnSlotCount()98   inline int GetReturnSlotCount() const { return return_slot_count_; }
99 
SetAllocatedRegisters(BitVector * regs)100   void SetAllocatedRegisters(BitVector* regs) {
101     DCHECK_NULL(allocated_registers_);
102     allocated_registers_ = regs;
103   }
104 
SetAllocatedDoubleRegisters(BitVector * regs)105   void SetAllocatedDoubleRegisters(BitVector* regs) {
106     DCHECK_NULL(allocated_double_registers_);
107     allocated_double_registers_ = regs;
108   }
109 
DidAllocateDoubleRegisters()110   bool DidAllocateDoubleRegisters() const {
111     return !allocated_double_registers_->IsEmpty();
112   }
113 
114   void AlignSavedCalleeRegisterSlots(int alignment = kDoubleSize) {
115     int alignment_slots = alignment / kSystemPointerSize;
116     int delta = alignment_slots - (frame_slot_count_ & (alignment_slots - 1));
117     if (delta != alignment_slots) {
118       frame_slot_count_ += delta;
119     }
120     spill_slot_count_ += delta;
121   }
122 
AllocateSavedCalleeRegisterSlots(int count)123   void AllocateSavedCalleeRegisterSlots(int count) {
124     frame_slot_count_ += count;
125   }
126 
127   int AllocateSpillSlot(int width, int alignment = 0) {
128     DCHECK_EQ(frame_slot_count_,
129               fixed_slot_count_ + spill_slot_count_ + return_slot_count_);
130     int frame_slot_count_before = frame_slot_count_;
131     if (alignment > kSystemPointerSize) {
132       // Slots are pointer sized, so alignment greater than a pointer size
133       // requires allocating additional slots.
134       width += alignment - kSystemPointerSize;
135     }
136     AllocateAlignedFrameSlots(width);
137     spill_slot_count_ += frame_slot_count_ - frame_slot_count_before;
138     return frame_slot_count_ - return_slot_count_ - 1;
139   }
140 
EnsureReturnSlots(int count)141   void EnsureReturnSlots(int count) {
142     if (count > return_slot_count_) {
143       count -= return_slot_count_;
144       frame_slot_count_ += count;
145       return_slot_count_ += count;
146     }
147   }
148 
149   int AlignFrame(int alignment = kDoubleSize);
150 
ReserveSpillSlots(size_t slot_count)151   int ReserveSpillSlots(size_t slot_count) {
152     DCHECK_EQ(0, spill_slot_count_);
153     spill_slot_count_ += static_cast<int>(slot_count);
154     frame_slot_count_ += static_cast<int>(slot_count);
155     return frame_slot_count_ - 1;
156   }
157 
158  private:
AllocateAlignedFrameSlots(int width)159   void AllocateAlignedFrameSlots(int width) {
160     DCHECK_LT(0, width);
161     int new_frame_slots = (width + kSystemPointerSize - 1) / kSystemPointerSize;
162     // Align to 8 bytes if width is a multiple of 8 bytes, and to 16 bytes if
163     // multiple of 16.
164     int align_to =
165         (width & 15) == 0 ? 16 : (width & 7) == 0 ? 8 : kSystemPointerSize;
166     frame_slot_count_ = RoundUp(frame_slot_count_ + new_frame_slots,
167                                 align_to / kSystemPointerSize);
168     DCHECK_LT(0, frame_slot_count_);
169   }
170 
171  private:
172   int fixed_slot_count_;
173   int frame_slot_count_;
174   int spill_slot_count_;
175   int return_slot_count_;
176   BitVector* allocated_registers_;
177   BitVector* allocated_double_registers_;
178 };
179 
180 // Represents an offset from either the stack pointer or frame pointer.
181 class FrameOffset {
182  public:
from_stack_pointer()183   inline bool from_stack_pointer() { return (offset_ & 1) == kFromSp; }
from_frame_pointer()184   inline bool from_frame_pointer() { return (offset_ & 1) == kFromFp; }
offset()185   inline int offset() { return offset_ & ~1; }
186 
FromStackPointer(int offset)187   inline static FrameOffset FromStackPointer(int offset) {
188     DCHECK_EQ(0, offset & 1);
189     return FrameOffset(offset | kFromSp);
190   }
191 
FromFramePointer(int offset)192   inline static FrameOffset FromFramePointer(int offset) {
193     DCHECK_EQ(0, offset & 1);
194     return FrameOffset(offset | kFromFp);
195   }
196 
197  private:
FrameOffset(int offset)198   explicit FrameOffset(int offset) : offset_(offset) {}
199 
200   int offset_;  // Encodes SP or FP in the low order bit.
201 
202   static const int kFromSp = 1;
203   static const int kFromFp = 0;
204 };
205 
206 // Encapsulates the mutable state maintained during code generation about the
207 // current function's frame.
208 class FrameAccessState : public ZoneObject {
209  public:
FrameAccessState(const Frame * const frame)210   explicit FrameAccessState(const Frame* const frame)
211       : frame_(frame),
212         access_frame_with_fp_(false),
213         sp_delta_(0),
214         has_frame_(false) {}
215 
frame()216   const Frame* frame() const { return frame_; }
217   V8_EXPORT_PRIVATE void MarkHasFrame(bool state);
218 
sp_delta()219   int sp_delta() const { return sp_delta_; }
ClearSPDelta()220   void ClearSPDelta() { sp_delta_ = 0; }
IncreaseSPDelta(int amount)221   void IncreaseSPDelta(int amount) { sp_delta_ += amount; }
222 
access_frame_with_fp()223   bool access_frame_with_fp() const { return access_frame_with_fp_; }
224 
225   // Regardless of how we access slots on the stack - using sp or fp - do we
226   // have a frame, at the current stage in code generation.
has_frame()227   bool has_frame() const { return has_frame_; }
228 
229   void SetFrameAccessToDefault();
SetFrameAccessToFP()230   void SetFrameAccessToFP() { access_frame_with_fp_ = true; }
SetFrameAccessToSP()231   void SetFrameAccessToSP() { access_frame_with_fp_ = false; }
232 
GetSPToFPSlotCount()233   int GetSPToFPSlotCount() const {
234     int frame_slot_count =
235         (has_frame() ? frame()->GetTotalFrameSlotCount() : kElidedFrameSlots) -
236         StandardFrameConstants::kFixedSlotCountAboveFp;
237     return frame_slot_count + sp_delta();
238   }
GetSPToFPOffset()239   int GetSPToFPOffset() const {
240     return GetSPToFPSlotCount() * kSystemPointerSize;
241   }
242 
243   // Get the frame offset for a given spill slot. The location depends on the
244   // calling convention and the specific frame layout, and may thus be
245   // architecture-specific. Negative spill slots indicate arguments on the
246   // caller's frame.
247   FrameOffset GetFrameOffset(int spill_slot) const;
248 
249  private:
250   const Frame* const frame_;
251   bool access_frame_with_fp_;
252   int sp_delta_;
253   bool has_frame_;
254 };
255 }  // namespace compiler
256 }  // namespace internal
257 }  // namespace v8
258 
259 #endif  // V8_COMPILER_FRAME_H_
260